Ejemplo n.º 1
0
 /**
  * Test the JForm::getFormControl method.
  *
  * @return void
  */
 public function testGetFormControl()
 {
     $form = new JForm('form8ion');
     $this->assertThat($form->getFormControl(), $this->equalTo(''), 'Line:' . __LINE__ . ' A form control that has not been specified should return nothing.');
     $form = new JForm('form8ion', array('control' => 'jform'));
     $this->assertThat($form->getFormControl(), $this->equalTo('jform'), 'Line:' . __LINE__ . ' The form control should agree with the options passed in the constructor.');
 }
Ejemplo n.º 2
0
 /**
  * Method to attach a JForm object to the field.
  *
  * @param   JForm  $form  The JForm object to attach to the form field.
  *
  * @return  JFormField  The form field object so that the method can be used in a chain.
  *
  * @since   11.1
  */
 public function setForm(JForm $form)
 {
     $this->form = $form;
     $this->formControl = $form->getFormControl();
     return $this;
 }
Ejemplo n.º 3
0
 public function onContentPrepareForm(JForm $form, $data)
 {
     $context = $form->getName();
     // Transform categories form name to a valid context
     if (strpos($context, 'com_categories.category') !== false) {
         $context = str_replace('com_categories.category', '', $context) . '.category';
     }
     // Extracting the component and section
     $parts = $this->getParts($context);
     if (!$parts) {
         return true;
     }
     // If we are on the save command we need the actual data
     $jformData = JFactory::getApplication()->input->get('jform', array(), 'array');
     if ($jformData && !$data) {
         $data = $jformData;
     }
     if (is_array($data)) {
         $data = (object) $data;
     }
     $component = $parts[0];
     $section = $parts[1];
     $catid = isset($data->catid) ? $data->catid : (isset($data->dpfieldscatid) ? $data->dpfieldscatid : null);
     if (!$catid && $form->getField('catid')) {
         // Choose the first category available
         $xml = new DOMDocument();
         $xml->loadHTML($form->getField('catid')->__get('input'));
         $options = $xml->getElementsByTagName('option');
         if ($firstChoice = $options->item(0)) {
             $catid = $firstChoice->getAttribute('value');
             $data->dpfieldscatid = $catid;
         }
     }
     // Getting the fields
     $fields = DPFieldsHelper::getFields($parts[0] . '.' . $parts[1], $data);
     // If there is a catid field we need to reload the page when the catid
     // is changed
     if ($form->getField('catid') && $parts[0] != 'com_dpfields') {
         // The uri to submit to
         $uri = clone JUri::getInstance('index.php');
         // Removing the catid parameter from the actual url and set it as
         // return
         $returnUri = clone JUri::getInstance();
         $returnUri->setVar('catid', null);
         $uri->setVar('return', base64_encode($returnUri->toString()));
         // Setting the options
         $uri->setVar('option', 'com_dpfields');
         $uri->setVar('task', 'field.catchange');
         $uri->setVar('context', $parts[0] . '.' . $parts[1]);
         $uri->setVar('formcontrol', $form->getFormControl());
         $uri->setVar('view', null);
         $uri->setVar('layout', null);
         // Setting the onchange event to reload the page when the category
         // has changed
         $form->setFieldAttribute('catid', 'onchange', "categoryHasChanged(this);");
         JFactory::getDocument()->addScriptDeclaration("function categoryHasChanged(element){\n\t\t\t\tvar cat = jQuery(element);\n\t\t\t\tif (cat.val() == '" . $catid . "')return;\n\t\t\t\tjQuery('input[name=task]').val('field.catchange');\n\t\t\t\telement.form.action='" . $uri . "';\n\t\t\t\telement.form.submit();\n\t\t\t}\n\t\t\tjQuery( document ).ready(function() {\n\t\t\t\tvar formControl = '#" . $form->getFormControl() . "_catid';\n\t\t\t\tif (!jQuery(formControl).val() != '" . $catid . "'){jQuery(formControl).val('" . $catid . "');}\n\t\t\t});");
     }
     if (!$fields) {
         return true;
     }
     // Creating the dom
     $xml = new DOMDocument('1.0', 'UTF-8');
     $fieldsNode = $xml->appendChild(new DOMElement('form'))->appendChild(new DOMElement('fields'));
     $fieldsNode->setAttribute('name', 'params');
     $fieldsNode->setAttribute('addfieldpath', '/administrator/components/com_dpfields/models/fields');
     $fieldsNode->setAttribute('addrulepath', '/administrator/components/com_dpfields/models/rules');
     // Defining the field set
     $fieldset = $fieldsNode->appendChild(new DOMElement('fieldset'));
     $fieldset->setAttribute('name', 'params');
     $fieldset->setAttribute('name', 'dpfields');
     $fieldset->setAttribute('addfieldpath', '/administrator/components/' . $component . '/models/fields');
     $fieldset->setAttribute('addrulepath', '/administrator/components/' . $component . '/models/rules');
     $lang = JFactory::getLanguage();
     $key = strtoupper($component . '_FIELDS_' . $section . '_LABEL');
     if (!$lang->hasKey($key)) {
         $key = 'PLG_SYSTEM_DPFIELDS_FIELDS';
     }
     $fieldset->setAttribute('label', JText::_($key));
     $key = strtoupper($component . '_FIELDS_' . $section . '_DESC');
     if ($lang->hasKey($key)) {
         $fieldset->setAttribute('description', JText::_($key));
     }
     // Looping trough the fields for that context
     foreach ($fields as $field) {
         // Creating the XML form data
         $type = DPFieldsHelper::loadTypeObject($field->type, $field->context);
         if ($type === false) {
             continue;
         }
         try {
             // Rendering the type
             $node = $type->appendXMLFieldTag($field, $fieldset, $form);
             // If the field belongs to a catid but the catid in the data is
             // not known, set the required flag to false on any circumstance
             if (!$catid && $field->catid) {
                 $node->setAttribute('required', 'false');
             }
         } catch (Exception $e) {
             JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
         }
     }
     // Loading the XML fields string into the form
     $form->load($xml->saveXML());
     $model = JModelLegacy::getInstance('Field', 'DPFieldsModel', array('ignore_request' => true));
     // Looping trough the fields again to set the value
     if (isset($data->id) && $data->id) {
         foreach ($fields as $field) {
             $value = $model->getFieldValue($field->id, $field->context, $data->id);
             if (!$value) {
                 continue;
             }
             // Setting the value on the field
             $form->setValue($field->alias, 'params', $value);
         }
     }
     return true;
 }