/** * Renders a F0FForm and returns the corresponding HTML * * @param F0FForm &$form The form to render * @param F0FModel $model The model providing our data * @param F0FInput $input The input object * @param string $formType The form type: edit, browse or read * @param boolean $raw If true, the raw form fields rendering (without the surrounding form tag) is returned. * * @return string The HTML rendering of the form */ public function renderForm(F0FForm &$form, F0FModel $model, F0FInput $input, $formType = null, $raw = false) { if (is_null($formType)) { $formType = $form->getAttribute('type', 'edit'); } else { $formType = strtolower($formType); } switch ($formType) { case 'browse': return $this->renderFormBrowse($form, $model, $input); break; case 'read': if ($raw) { return $this->renderFormRaw($form, $model, $input, 'read'); } else { return $this->renderFormRead($form, $model, $input); } break; default: if ($raw) { return $this->renderFormRaw($form, $model, $input, 'edit'); } else { return $this->renderFormEdit($form, $model, $input); } break; } }
public function onBeforePreprocessForm(F0FForm &$form, &$data) { if (isset($data['body_source']) && $data['body_source'] == 'file') { // Codemirror or Editor None should be enabled $db = JFactory::getDbo(); $query = $db->getQuery(true)->select('COUNT(*)')->from('#__extensions as a')->where('(a.name =' . $db->quote('plg_editors_codemirror') . ' AND a.enabled = 1) OR (a.name =' . $db->quote('plg_editors_none') . ' AND a.enabled = 1)'); $db->setQuery($query); $state = $db->loadResult(); if ((int) $state < 1) { $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_EDITOR_DISABLED'), 'warning'); } //get the file name $filename = $data['body_source_file']; if (!empty($filename) && $filename != '-1') { $source = $this->getSource($filename); $data['source'] = $source->source; } else { $form->removeField('source'); } //remove fields $form->removeField('body'); } else { $form->removeField('body_source_file'); $form->removeField('source'); } }
/** * Loads the validation script for an edit form * * @param F0FForm &$form The form we are rendering * * @return void */ protected function loadValidationScript(F0FForm &$form) { $message = $form->getView()->escape(JText::_('JGLOBAL_VALIDATION_FORM_FAILED')); $js = <<<JS Joomla.submitbutton = function(task) { \tif (task == 'cancel' || document.formvalidator.isValid(document.id('adminForm'))) \t{ \t\tJoomla.submitform(task, document.getElementById('adminForm')); \t} \telse { \t\talert('{$message}'); \t} }; JS; $document = F0FPlatform::getInstance()->getDocument(); if ($document instanceof JDocument) { $document->addScriptDeclaration($js); } }
/** * Renders a label for a fieldset. * * @param object $field The field of the label to render * @param F0FForm &$form The form to render * @param string $title The title of the label * * @return string The rendered label */ protected function renderFieldsetLabel($field, F0FForm &$form, $title) { $html = ''; $labelClass = $field->labelClass; $required = $field->required; $tooltip = $form->getFieldAttribute($field->fieldname, 'tooltip', '', $field->group); if (!empty($tooltip)) { if (version_compare(JVERSION, '3.0', 'ge')) { static $loadedTooltipScript = false; if (!$loadedTooltipScript) { $js = <<<JS (function(\$) { \t\$(document).ready(function() \t{ \t\t\$('.fof-tooltip').tooltip({placement: 'top'}); \t}); })(akeeba.jQuery); JS; $document = F0FPlatform::getInstance()->getDocument(); if ($document instanceof JDocument) { $document->addScriptDeclaration($js); } $loadedTooltipScript = true; } $tooltipText = '<strong>' . JText::_($title) . '</strong><br />' . JText::_($tooltip); $html .= "\t\t\t\t" . '<label class="control-label fof-tooltip ' . $labelClass . '" for="' . $field->id . '" title="' . $tooltipText . '" data-toggle="fof-tooltip">'; } else { // Joomla! 2.5 has a conflict with the jQueryUI tooltip, therefore we // have to use native Joomla! 2.5 tooltips JHtml::_('behavior.tooltip'); $tooltipText = JText::_($title) . '::' . JText::_($tooltip); $html .= "\t\t\t\t" . '<label class="control-label hasTip ' . $labelClass . '" for="' . $field->id . '" title="' . $tooltipText . '" rel="tooltip">'; } } else { $html .= "\t\t\t\t" . '<label class="control-label ' . $labelClass . '" for="' . $field->id . '">'; } $html .= JText::_($title); if ($required) { $html .= ' *'; } $html .= '</label>' . PHP_EOL; return $html; }
/** * Method to validate the form data. * * @param F0FForm $form The form to validate against. * @param array $data The data to validate. * @param string $group The name of the field group to validate. * * @return mixed Array of filtered data if valid, false otherwise. * * @see JFormRule * @see JFilterInput * @since 2.0 */ public function validateForm($form, $data, $group = null) { // Filter and validate the form data. $data = $form->filter($data); $return = $form->validate($data, $group); // Check for an error. if ($return instanceof Exception) { $this->setError($return->getMessage()); return false; } // Check the validation results. if ($return === false) { // Get the validation messages from the form. foreach ($form->getErrors() as $message) { if ($message instanceof Exception) { $this->setError($message->getMessage()); } else { $this->setError($message); } } return false; } return $data; }
/** * Renders a label for a fieldset. * * @param object $field The field of the label to render * @param F0FForm &$form The form to render * @param string $title The title of the label * * @return string The rendered label */ protected function renderFieldsetLabel($field, F0FForm &$form, $title) { $html = ''; $labelClass = $field->labelClass ? $field->labelClass : $field->labelclass; // Joomla! 2.5/3.x use different case for the same name $required = $field->required; $tooltip = $form->getFieldAttribute($field->fieldname, 'tooltip', '', $field->group); if (!empty($tooltip)) { JHtml::_('bootstrap.tooltip'); $tooltipText = '<strong>' . JText::_($title) . '</strong><br />' . JText::_($tooltip); $html .= "\t\t\t\t" . '<label class="control-label hasTooltip ' . $labelClass . '" for="' . $field->id . '" title="' . $tooltipText . '" rel="tooltip">'; } else { $html .= "\t\t\t\t" . '<label class="control-label ' . $labelClass . '" for="' . $field->id . '">'; } $html .= JText::_($title); if ($required) { $html .= ' *'; } $html .= '</label>' . PHP_EOL; return $html; }
/** * Get the filter value for this header field * * @return mixed The filter value */ protected function getValue() { $model = $this->form->getModel(); return $model->getState($this->filterSource); }
/** * Method to get a form object. * * @param string $name The name of the form. * @param string $source The form filename (e.g. form.browse) * @param array $options Optional array of options for the form creation. * @param boolean $clear Optional argument to force load a new form. * @param bool|string $xpath An optional xpath to search for the fields. * * @return mixed F0FForm object on success, False on error. * * @throws Exception * * @see F0FForm * @since 2.0 */ protected function loadForm($name, $source, $options = array(), $clear = false, $xpath = 'config') { if (empty($this->_element)) { return parent::loadForm($name, $source, $options, $clear, $xpath); } // Handle the optional arguments. $options['control'] = isset($options['control']) ? $options['control'] : false; // Create a signature hash. $hash = md5($source . serialize($options)); // Check if we can use a previously loaded form. if (isset($this->_forms[$hash]) && !$clear) { return $this->_forms[$hash]; } // Try to find the name and path of the form to load $paths = array(); $paths[] = JPATH_SITE . '/plugins/j2store/' . $this->_element; $name = $this->_element; $source = $this->_element; $formFilename = $this->findFormFilename($source, $paths); // No form found? Quit! if ($formFilename === false) { return false; } // Set up the form name and path $source = basename($formFilename, '.xml'); F0FForm::addFormPath(dirname($formFilename)); // Set up field paths $option = $this->input->getCmd('option', 'com_foobar'); $componentPaths = F0FPlatform::getInstance()->getComponentBaseDirs($option); $view = $this->name; $file_root = $componentPaths['main']; $alt_file_root = $componentPaths['alt']; F0FForm::addFieldPath($file_root . '/fields'); F0FForm::addFieldPath($file_root . '/models/fields'); F0FForm::addFieldPath($alt_file_root . '/fields'); F0FForm::addFieldPath($alt_file_root . '/models/fields'); F0FForm::addHeaderPath($file_root . '/fields/header'); F0FForm::addHeaderPath($file_root . '/models/fields/header'); F0FForm::addHeaderPath($alt_file_root . '/fields/header'); F0FForm::addHeaderPath($alt_file_root . '/models/fields/header'); // Get the form. try { $form = F0FForm::getInstance($name, $source, $options, false, $xpath); if (isset($options['load_data']) && $options['load_data']) { // Get the data for the form. $data = $this->loadFormData(); } else { $data = array(); } // Allows data and form manipulation before preprocessing the form $this->onBeforePreprocessForm($form, $data); // Allow for additional modification of the form, and events to be triggered. // We pass the data because plugins may require it. $this->preprocessForm($form, $data); // Allows data and form manipulation After preprocessing the form $this->onAfterPreprocessForm($form, $data); // Load the data into the form after the plugins have operated. $form->bind($data); } catch (Exception $e) { // The above try-catch statement will catch EVERYTHING, even PhpUnit exceptions while testing if (stripos(get_class($e), 'phpunit') !== false) { throw $e; } else { $this->setError($e->getMessage()); return false; } } // Store the form for later. $this->_forms[$hash] = $form; return $form; }