/** * Internal method to build a control from its options array and its template. Outputs the * prefix template, a label (if in the options), a control, the control's errors and a * suffix template. * * @param string $template Name of the control template, from the global $indicia_templates variable. * @param array $options Options array containing the control replacement values for the templates. * Options can contain a setting for prefixTemplate or suffixTemplate to override the standard templates. */ public static function apply_template($template, $options) { global $indicia_templates; // Don't need the extraParams - they are just for service communication. $options['extraParams'] = null; // Set default validation error output mode if (!array_key_exists('validation_mode', $options)) { $options['validation_mode'] = self::$validation_mode; } // Decide if the main control has an error. If so, highlight with the error class and set it's title. $error = ""; if (self::$validation_errors !== null) { if (array_key_exists('fieldname', $options)) { $error = self::check_errors($options['fieldname'], true); } } // Add a hint to the control if there is an error and this option is set, or a hint option if ($error && in_array('hint', $options['validation_mode']) || isset($options['hint'])) { $hint = $error && in_array('hint', $options['validation_mode']) ? array($error) : array(); if (isset($options['hint'])) { $hint[] = $options['hint']; } $options['title'] = 'title="' . implode(' : ', $hint) . '"'; } else { $options['title'] = ''; } if (!array_key_exists('class', $options)) { $options['class'] = ''; } if (!array_key_exists('disabled', $options)) { $options['disabled'] = ''; } if (!array_key_exists('readonly', $options)) { $options['readonly'] = ''; } if (array_key_exists('maxlength', $options)) { $options['maxlength'] = 'maxlength="' . $options['maxlength'] . '"'; } else { $options['maxlength'] = ''; } // Add an error class to colour the control if there is an error and this option is set if ($error && in_array('colour', $options['validation_mode'])) { $options['class'] .= ' ui-state-error'; if (array_key_exists('outerClass', $options)) { $options['outerClass'] .= ' ui-state-error'; } else { $options['outerClass'] = 'ui-state-error'; } } // add validation metadata to the control if specified, as long as control has a fieldname if (array_key_exists('fieldname', $options)) { $validationClasses = self::build_validation_class($options); $options['class'] .= ' ' . $validationClasses; } // replace html attributes with their wrapped versions, e.g. a class becomes class="..." foreach (self::$html_attributes as $name => $attr) { if (!empty($options[$name])) { $options[$name] = ' ' . $attr . '="' . $options[$name] . '"'; } } // If options contain a help text, output it at the end if that is the preferred position $r = self::get_help_text($options, 'before'); //Add prefix $r .= self::apply_static_template('prefix', $options); // Add a label only if specified in the options array. Link the label to the inputId if available, // otherwise the fieldname (as the fieldname control could be a hidden control). if (!empty($options['label'])) { $r .= str_replace(array('{label}', '{id}', '{labelClass}'), array($options['label'], array_key_exists('inputId', $options) ? $options['inputId'] : $options['id'], array_key_exists('labelClass', $options) ? ' class="' . $options['labelClass'] . '"' : ''), isset($options['labelTemplate']) ? $indicia_templates[$options['labelTemplate']] : $indicia_templates['label']); } // Output the main control $r .= self::apply_replacements_to_template($indicia_templates[$template], $options); // Add a lock icon to the control if the lockable option is set to true if (array_key_exists('lockable', $options) && $options['lockable'] === true) { $r .= self::apply_replacements_to_template($indicia_templates['lock_icon'], $options); if (!self::$using_locking) { self::$using_locking = true; $options['lock_form_mode'] = self::$form_mode ? self::$form_mode : 'NEW'; // write lock javascript at the start of the late javascript so after control setup but before any other late javascript self::$late_javascript = self::apply_replacements_to_template($indicia_templates['lock_javascript'], $options) . self::$late_javascript; self::add_resource('indicia_locks'); } } if (isset($validationClasses) && !empty($validationClasses) && strpos($validationClasses, 'required') !== false) { $r .= self::apply_static_template('requiredsuffix', $options); } // Add an error icon to the control if there is an error and this option is set if ($error && in_array('icon', $options['validation_mode'])) { $r .= $indicia_templates['validation_icon']; } // Add a message to the control if there is an error and this option is set if ($error && in_array('message', $options['validation_mode'])) { $r .= self::apply_error_template($error, $options['fieldname']); } if (isset($options['afterControl'])) { $r .= $options['afterControl']; } // Add suffix $r .= self::apply_static_template('suffix', $options); // If options contain a help text, output it at the end if that is the preferred position $r .= self::get_help_text($options, 'after'); if (isset($options['id'])) { $wrap = empty($options['controlWrapTemplate']) ? $indicia_templates['controlWrap'] : $indicia_templates[$options['controlWrapTemplate']]; $r = str_replace(array('{control}', '{id}'), array($r, str_replace(':', '-', $options['id'])), $wrap); } if (!empty($options['tooltip'])) { // preliminary support for $id = str_replace(':', '\\\\:', array_key_exists('inputId', $options) ? $options['inputId'] : $options['id']); self::$javascript .= "\$('#{$id}').attr('title', '{$options['tooltip']}');\n"; } return $r; }