datamapper /application/datamapper/htmlform.php

Language PHP Lines 799
MD5 Hash 348a0f17d0a44e84c19218128004331b Estimated Cost $15,553 (why?)
Repository https://bitbucket.org/ramo_carlo/datamapper View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
<?php


/**
 * HTMLForm Extension for DataMapper classes.
 *
 * A powerful extension that allows one to quickly
 * generate standardized forms off a DMZ object.
 *
 * @license 	MIT License
 * @package		DMZ-Included-Extensions
 * @category	DMZ
 * @author  	Phil DeJarnett
 * @link    	http://www.overzealous.com/dmz/pages/extensions/htmlform.html
 * @version 	1.0
 */

// --------------------------------------------------------------------------

/**
 * DMZ_HTMLForm Class
 *
 * @package		DMZ-Included-Extensions
 */
class DMZ_HTMLForm {

	// this is the default template (view) to use for the overall form
	var $form_template = 'dmz_htmlform/form';
	// this is the default template (view) to use for the individual rows
	var $row_template = 'dmz_htmlform/row';
	// this is the default template (view) to use for the individual rows
	var $section_template = 'dmz_htmlform/section';

	var $auto_rule_classes = array(
		'integer' => 'integer',
		'numeric' => 'numeric',
		'is_natural' => 'natural',
		'is_natural_no_zero' => 'positive_int',
		'valid_email' => 'email',
		'valid_ip' => 'ip',
		'valid_base64' => 'base64',
		'valid_date' => 'date',
		'alpha_dash_dot' => 'alpha_dash_dot',
		'alpha_slash_dot' => 'alpha_slash_dot',
		'alpha' => 'alpha',
		'alpha_numeric' => 'alpha_numeric',
		'alpha_dash' => 'alpha_dash',
		'required' => 'required'
	);

	function __construct($options = array(), $object = NULL) {

		if (is_array($options) )
		{
			foreach($options as $k => $v)
			{
				$this->{$k} = $v;
			}
		}

		$this->CI =& get_instance();
		$this->load = $this->CI->load;
	}

	// --------------------------------------------------------------------------

	/**
	 * Render a single field.  Can be used to chain together multiple fields in a column.
	 *
	 * @param object $object The DataMapper Object to use.
	 * @param string $field The field to render.
	 * @param string $type  The type of field to render.
	 * @param array  $options  Various options to modify the output.
	 * @return Rendered String.
	 */
	function render_field($object, $field, $type = NULL, $options = NULL)
	{
		$value = '';

		if(array_key_exists($field, $object->has_one) || array_key_exists($field, $object->has_many))
		{
			// Create a relationship field
			$one = array_key_exists($field, $object->has_one);

			// attempt to look up the current value(s)
			if( ! isset($options['value']))
			{
				if($this->CI->input->post($field))
				{
					$value = $this->CI->input->post($field);
				}
				else
				{
					// load the related object(s)
					$sel = $object->{$field}->select('id')->get();
					if($one)
					{
						// only a single value is allowed
						$value = $sel->id;
					}
					else
					{
						// save what might be multiple values
						$value = array();
						foreach($sel as $s)
						{
							$value[] = $s->id;
						}
					}
				}

			}
			else
			{
				// value was already set in the options
				$value = $options['value'];
				unset($options['value']);
			}

			// Attempt to get a list of possible values
			if( ! isset($options['list']) || is_object($options['list']))
			{
				if( ! isset($options['list']))
				{
					// look up all of the related values
					$c = get_class($object->{$field});
					$total_items = new $c;
					// See if the custom method is defined
					if(method_exists($total_items, 'get_htmlform_list'))
					{
						// Get customized list
						$total_items->get_htmlform_list($object, $field);
					}
					else
					{
						// Get all items
						$total_items->get_iterated();
					}
				}
				else
				{
					// process a passed-in DataMapper object
					$total_items = $options['list'];
				}
				$list = array();
				foreach($total_items as $item)
				{
					// use the __toString value of the item for the label
					$list[$item->id] = (string)$item;
				}
				$options['list'] = $list;
			}

			// By if there can be multiple items, use a dropdown for large lists,
			// and a set of checkboxes for a small one.
			if($one || count($options['list']) > 6)
			{
				$default_type = 'dropdown';
				if( ! $one && ! isset($options['size']))
				{
					// limit to no more than 8 items high.
					$options['size'] = min(count($options['list']), 8);
				}
			}
			else
			{
				$default_type = 'checkbox';
			}
		}
		else
		{
			// attempt to look up the current value(s)
			if( ! isset($options['value']))
			{
				if($this->CI->input->post($field))
				{
					$value = $this->CI->input->post($field);
					// clear default if set
					unset($options['default_value']);
				}
				else
				{
					if(isset($options['default_value']))
					{
						$value = $options['default_value'];
						unset($options['default_value']);
					}
					else
					{
						// the field IS the value.
						$value = $object->{$field};
					}
				}

			}
			else
			{
				// value was already set in the options
				$value = $options['value'];
				unset($options['value']);
			}
			// default to text
			$default_type = ($field == 'id') ? 'hidden' : 'text';

			// determine default attributes
			$a = array();
			// such as the size of the field.
			$max = $this->_get_validation_rule($object, $field, 'max_length');
			if($max === FALSE)
			{
				$max = $this->_get_validation_rule($object, $field, 'exact_length');
			}
			if($max !== FALSE)
			{
				$a['maxlength'] = $max;
				$a['size'] = min($max, 30);
			}
			$list = $this->_get_validation_info($object, $field, 'values', FALSE);
			if($list !== FALSE)
			{
				$a['list'] = $list;
			}
			$options = $options + $a;
			$extra_class = array();

			// Add any of the known rules as classes (for JS processing)
			foreach($this->auto_rule_classes as $rule => $c)
			{
				if($this->_get_validation_rule($object, $field, $rule) !== FALSE)
				{
					$extra_class[] = $c;
				}
			}

			// add or set the class on the field.
			if( ! empty($extra_class))
			{
				$extra_class = implode(' ', $extra_class);
				if(isset($options['class']))
				{
					$options['class'] .= ' ' . $extra_class;
				}
				else
				{
					$options['class'] = $extra_class;
				}
			}
		}

		// determine the renderer type
		$type = $this->_get_type($object, $field, $type);
		if(empty($type))
		{
			$type = $default_type;
		}

		// attempt to find the renderer function
		if(method_exists($this, '_input_' . $type))
		{
			return $this->{'_input_' . $type}($object, $field, $value, $options);
		}
		else if(function_exists('input_' . $type))
		{
			return call_user_func('input_' . $type, $object, $field, $value, $options);
		}
		else
		{
			log_message('error', 'FormMaker: Unable to find a renderer for '.$type);
			return '<span style="color: Maroon; background-color: White; font-weight: bold">FormMaker: UNABLE TO FIND A RENDERER FOR '.$type.'</span>';
		}

	}

	// --------------------------------------------------------------------------

	/**
	 * Render a row with a single field.  If $field does not exist on
	 * $object->validation, then $field is output as-is.
	 *
	 * @param object $object The DataMapper Object to use.
	 * @param string $field The field to render (or content)
	 * @param string $type  The type of field to render.
	 * @param array  $options  Various options to modify the output.
	 * @param string $row_template  The template to use, or NULL to use the default.
	 * @return Rendered String.
	 */
	function render_row($object, $field, $type = NULL, $options = array(), $row_template = NULL)
	{
		// try to determine type automatically
		$type = $this->_get_type($object, $field, $type);

		if( ! isset($object->validation[$field]) && (empty($type) || $type == 'section' || $type == 'none'))
		{
			// this could be a multiple-field row, or just some text.
			// if $type is 'section, it will be rendered using the section template.
			$error = '';
			$label = '';
			$content = $field;
			$id = NULL;
		}
		else
		{
			// use validation information to render the field.
			$content = $this->render_field($object, $field, $type, $options);
			if(empty($row_template))
			{
				if($type == 'hidden' || $field == 'id')
				{
					$row_template = 'none';
				}
				else
				{
					$row_template = $this->row_template;
				}
			}
			// determine if there is an existing error
			$error = isset($object->error->{$field}) ? $object->error->{$field} : '';
			// determine if there is a pre-defined label
			$label = $this->_get_validation_info($object, $field, 'label', $field);
			// the field IS the id
			$id = $field;
		}

		$required = $this->_get_validation_rule($object, $field, 'required');

		// Append these items.  Values in $options have priority
		$view_data = $options + array(
			'object' => $object,
			'content' => $content,
			'field' => $field,
			'label' => $label,
			'error' => $error,
			'id' => $id,
			'required' => $required
		);

		if(is_null($row_template))
		{
			if(empty($type))
			{
				$row_template = 'none';
			}
			else if($type == 'section')
			{
				$row_template = $this->section_template;
			}
			else
			{
				$row_template = $this->row_template;
			}
		}

		if($row_template == 'none')
		{
			return $content;
		}
		else
		{
			return $this->load->view($row_template, $view_data, TRUE);
		}
	}

	// --------------------------------------------------------------------------

	/**
	 * Renders an entire form.
	 *
	 * @param object $object The DataMapper Object to use.
	 * @param string $fields An associative array that defines the form.
	 * @param string $template  The template to use.
	 * @param string $row_template  The template to use for rows.
	 * @param array  $template_options  The template to use for rows.
	 * @return Rendered String.
	 */
	function render_form($object, $fields, $url = '', $options = array(), $template = NULL, $row_template = NULL)
	{
		if(empty($url))
		{
			// set url to current url
			$url =$this->CI->uri->uri_string();
		}

		if(is_null($template))
		{
			$template = $this->form_template;
		}

		$rows = '';
		foreach($fields as $field => $field_options)
		{
			$rows .= $this->_render_row_from_form($object, $field, $field_options, $row_template);
		}

		$view_data = $options + array(
			'object' => $object,
			'fields' => $fields,
			'url' => $url,
			'rows' => $rows
		);

		return $this->load->view($template, $view_data, TRUE);
	}

	// --------------------------------------------------------------------------
	// Private Methods
	// --------------------------------------------------------------------------

	// Converts information from render_form into a row of objects.
	function _render_row_from_form($object, $field, $options, $row_template, $row = TRUE)
	{
		if(is_int($field))
		{
			// simple form, or HTML-content
			$field = $options;
			$options = NULL;
		}
		if(is_null($options))
		{
			// always have an array for options
			$options = array();
		}

		$type = '';
		if( ! is_array($options))
		{
			// if options is a single string, assume it is the type.
			$type = $options;
			$options = array();
		}

		if(isset($options['type']))
		{
			// type was set in options
			$type = $options['type'];
			unset($options['type']);
		}

		// see if a different row_template was in the options
		$rt = $row_template;
		if(isset($options['template']))
		{
			$rt = $options['template'];
			unset($options['template']);
		}

		// Multiple fields, render them all as one.
		if(is_array($field))
		{
			if(isset($field['row_options']))
			{
				$options = $field['row_options'];
				unset($field['row_options']);
			}
			$ret = '';
			$sep = ' ';
			if(isset($field['input_separator']))
			{
				$sep = $field['input_separator'];
				unset($field['input_separator']);
			}
			foreach($field as $f => $fo)
			{
				// add each field to a list
				if( ! empty($ret))
				{
					$ret .= $sep;
				}
				$ret .= $this->_render_row_from_form($object, $f, $fo, $row_template, FALSE);
			}

			// renders into a row or field below.
			$field = $ret;
		}
		if($row)
		{
			// if row is set, render the whole row.
			return $this->render_row($object, $field, $type, $options, $rt);
		}
		else
		{
			// render just the field.
			return $this->render_field($object, $field, $type, $options);
		}
	}

	// --------------------------------------------------------------------------

	// Attempts to look up the field's type
	function _get_type($object, $field, $type)
	{
		if(empty($type))
		{
			$type = $this->_get_validation_info($object, $field, 'type', NULL);
		}
		return $type;
	}

	// --------------------------------------------------------------------------

	// Returns a field from the validation array
	function _get_validation_info($object, $field, $val, $default = '')
	{
		if(isset($object->validation[$field][$val]))
		{
			return $object->validation[$field][$val];
		}
		return $default;
	}

	// --------------------------------------------------------------------------

	// Returns the value (or TRUE) of the validation rule, or FALSE if it does not exist.
	function _get_validation_rule($object, $field, $rule)
	{
		$r = $this->_get_validation_info($object, $field, 'rules', FALSE);
		if($r !== FALSE)
		{
			if(isset($r[$rule]))
			{
				return $r[$rule];
			}
			else if(in_array($rule, $r, TRUE))
			{
				return TRUE;
			}
		}
		return FALSE;
	}

	// --------------------------------------------------------------------------
	// Input Types
	// --------------------------------------------------------------------------

	// Render a hidden input
	function _input_hidden($object, $id, $value, $options)
	{
		return $this->_render_simple_input('hidden', $id, $value, $options);
	}

	// render a single-line text input
	function _input_text($object, $id, $value, $options)
	{
		return $this->_render_simple_input('text', $id, $value, $options);
	}

	// render a password input
	function _input_password($object, $id, $value, $options)
	{
		if(isset($options['send_value']))
		{
			unset($options['send_value']);
		}
		else
		{
			$value = '';
		}
		return $this->_render_simple_input('password', $id, $value, $options);
	}

	// render a multiline text input
	function _input_textarea($object, $id, $value, $options)
	{
		if(isset($options['value']))
		{
			$value = $options['value'];
			unset($options['value']);
		}
		$a = $options + array(
			'name' => $id,
			'id' => $id
		);
		return $this->_render_node('textarea', $a, htmlspecialchars($value));
	}

	// render a dropdown
	function _input_dropdown($object, $id, $value, $options)
	{
		$list = $options['list'];
		unset($options['list']);
		$selected = $value;
		if(isset($options['value']))
		{
			$selected = $options['value'];
			unset($options['value']);
		}
		if( ! is_array($selected))
		{
			$selected = array($selected);
		}
		else
		{
			// force multiple
			$options['multiple'] = 'multiple';
		}
		$l = $this->_options($list, $selected);

		$name = $id;
		if(isset($options['multiple']))
		{
			$name .= '[]';
		}
		$a = $options + array(
			'name' => $name,
			'id' => $id
		);
		return $this->_render_node('select', $a, $l);
	}

	// used to render an options list.
	function _options($list, $sel)
	{
		$l = '';
		foreach($list as $opt => $label)
		{
			if(is_array($label))
			{
				$l .= '<optgroup label="' . htmlspecialchars($key) . '">';
				$l .= $this->_options($label, $sel);
				$l .= '</optgroup>';
			}
			else
			{
				$a = array('value' => $opt);
				if(in_array($opt, $sel))
				{
					$a['selected'] = 'selected';
				}
				$l .= $this->_render_node('option', $a, htmlspecialchars($label));
			}
		}
		return $l;
	}

	// render a checkbox or series of checkboxes
	function _input_checkbox($object, $id, $value, $options)
	{
		return $this->_checkbox('checkbox', $id, $value, $options);
	}

	// render a series of radio buttons
	function _input_radio($object, $id, $value, $options)
	{
		return $this->_checkbox('radio', $id, $value, $options);
	}

	// renders one or more checkboxes or radio buttons
	function _checkbox($type, $id, $value, $options, $sub_id = '', $label = '')
	{
		if(isset($options['value']))
		{
			$value = $options['value'];
			unset($options['value']);
		}
		// if there is a list in options, render our multiple checkboxes.
		if(isset($options['list']))
		{
			$list = $options['list'];
			unset($options['list']);
			$ret = '';
			if( ! is_array($value))
			{
				if(is_null($value) || $value === FALSE || $value === '')
				{
					$value = array();
				}
				else
				{
					$value = array($value);
				}
			}
			$sep = '<br/>';
			if(isset($options['input_separator']))
			{
				$sep = $options['input_separator'];
				unset($options['input_separator']);
			}
			foreach($list as $k => $v)
			{
				if( ! empty($ret))
				{
					// put each node on one line.
					$ret .= $sep;
				}
				$ret .= $this->_checkbox($type, $id, $value, $options, $k, $v);
			}
			return $ret;
		}
		else
		{
			// just render the single checkbox.
			$node_id = $id;
			if( ! empty($sub_id))
			{
				// there are multiple nodes with this id, append the sub_id
				$node_id .= '_' . $sub_id;
				$field_value = $sub_id;
			}
			else
			{
				// sub_id is the same as the node's id
				$sub_id = $id;
				$field_value = '1';
			}
			$name = $id;
			if(is_array($value))
			{
				// allow for multiple results
				$name .= '[]';
			}
			// node attributes
			$a = $options + array(
				'type' => $type,
				'id' => $node_id,
				'name' => $name,
				'value' => $field_value
			);
			// if checked wasn't overridden
			if( ! isset($a['checked']))
			{
				// determine if this is a multiple checkbox or not.
				$checked = $value;
				if(is_array($checked))
				{
					$checked = in_array($sub_id, $value);
				}
				if($checked)
				{
					$a['checked'] = 'checked';
				}
			}
			$ret = $this->_render_node('input', $a);
			if( ! empty($label))
			{
				$ret .= ' ' . $this->_render_node('label', array('for' => $node_id), $label);
			}
			return $ret;
		}
	}

	// render a file upload input
    function _input_file($object, $id, $value, $options)
    {
        $a = $options + array(
			'type' => 'file',
			'name' => $id,
			'id' => $id
		);
		return $this->_render_node('input', $a);
    }

	// Utility method to render a normal <input>
	function _render_simple_input($type, $id, $value, $options)
	{
		$a = $options + array(
			'type' => $type,
			'name' => $id,
			'id' => $id,
			'value' => $value
		);
		return $this->_render_node('input', $a);
	}

	// Utility method to render a node.
	function _render_node($type, $attributes, $content = FALSE)
	{
		// generate node
		$res = '<' . $type;
		foreach($attributes as $att => $v)
		{
			// the special attribute '_' is rendered directly.
			if($att == '_')
			{
				$res .= ' ' . $v;
			}
			else
			{
				if($att != 'label')
				{
					$res .= ' ' . $att . '="' . htmlspecialchars((string)$v) . '"';
				}
			}
		}
		// allow for content-containing nodes
		if($content !== FALSE)
		{
			$res .= '>' . $content . '</' . $type .'>';
		}
		else
		{
			$res .= ' />';
		}
		return $res;
	}

}

/* End of file htmlform.php */
/* Location: ./application/datamapper/htmlform.php */
Back to Top