/**
  * onFieldsandfiltersPrepareFormField
  *
  * @param    KextensionsForm $form     The form to be altered.
  * @param    JObject         $data     The associated data for the form.
  * @param   boolean          $isNew    Is element is new
  * @param   string           $fieldset Fieldset name
  *
  * @return    boolean
  * @since    1.1.0
  */
 public function onFieldsandfiltersPrepareFormField(KextensionsForm $form, JObject $data, $isNew = false, $fieldset = 'fieldsandfilters')
 {
     if (!($fields = $data->get($this->_name))) {
         return true;
     }
     $fields = is_array($fields) ? $fields : array($fields);
     $staticMode = (array) FieldsandfiltersModes::getMode(FieldsandfiltersModes::MODE_STATIC);
     $syntax = KextensionsPlugin::getParams('system', 'fieldsandfilters')->get('syntax', '#{%s}');
     while ($field = array_shift($fields)) {
         $root = new SimpleXMLElement('<fields />');
         $root->addAttribute('name', 'data');
         $rootJson = $root->addChild('fields');
         $rootJson->addAttribute('name', $field->id);
         $label = '<strong>' . $field->name . '</strong> ' . sprintf($syntax, $field->id);
         if ($field->state == -1) {
             $label .= ' [' . JText::_('PLG_FIELDSANDFILTERS_FORM_ONLY_ADMIN') . ']';
         }
         if (!($isStaticMode = in_array($field->mode, $staticMode))) {
             // name spacer
             $element = $rootJson->addChild('field');
             $element->addAttribute('type', 'spacer');
             $element->addAttribute('name', 'name_spacer_' . $field->id);
             $element->addAttribute('label', $label);
             $element->addAttribute('translate_label', 'false');
             $element->addAttribute('class', 'text');
             $element->addAttribute('fieldset', $fieldset);
         }
         if (!empty($field->description) && $field->params->get('base.admin_enabled_description', 0)) {
             switch ($field->params->get('base.admin_description_type', 'description')) {
                 case 'tip':
                     $element->addAttribute('description', $field->description);
                     $element->addAttribute('translate_description', 'false');
                     break;
                 case 'description':
                 default:
                     $element = $rootJson->addChild('field');
                     $element->addAttribute('type', 'spacer');
                     $element->addAttribute('name', 'description_spacer_' . $field->id);
                     $element->addAttribute('label', $field->description);
                     $element->addAttribute('translate_label', 'false');
                     $element->addAttribute('fieldset', $fieldset);
                     break;
             }
         }
         $element = $rootJson->addChild('field');
         $element->addAttribute('labelclass', 'control-label');
         $element->addAttribute('fieldset', $fieldset);
         if ($isStaticMode) {
             $label .= ' [' . JText::_('PLG_FIELDSANDFILTERS_FORM_GROUP_STATIC_TITLE') . ']';
             $element->addAttribute('type', 'spacer');
             $element->addAttribute('description', $field->data);
             $element->addAttribute('name', $field->id);
             $element->addAttribute('label', $label);
             $element->addAttribute('translate_label', 'false');
             $element->addAttribute('translate_description', 'false');
         } else {
             //image
             $element->addAttribute('name', 'image');
             $element->addAttribute('type', 'media');
             $element->addAttribute('class', 'inputbox');
             $element->addAttribute('label', 'PLG_FAF_TS_IE_FORM_IMAGE_LBL');
             $element->addAttribute('description', 'PLG_FAF_TS_IE_FORM_IMAGE_DESC');
             $element->addAttribute('filter', 'safehtml');
             if ($field->required) {
                 $element->addAttribute('required', 'true');
             }
             //src
             $element = $rootJson->addChild('field');
             $element->addAttribute('name', 'src');
             $element->addAttribute('type', 'hidden');
             $element->addAttribute('filter', 'safehtml');
             $element->addAttribute('fieldset', $fieldset);
             // caption
             $element = $rootJson->addChild('field');
             $element->addAttribute('name', 'caption');
             $element->addAttribute('type', 'text');
             $element->addAttribute('class', 'inputbox');
             $element->addAttribute('labelclass', 'control-label');
             $element->addAttribute('label', 'PLG_FAF_TS_IE_FORM_CAPTION_LBL');
             $element->addAttribute('description', 'PLG_FAF_TS_IE_FORM_CAPTION_DESC');
             $element->addAttribute('filter', 'safehtml');
             $element->addAttribute('fieldset', $fieldset);
             // alt
             $element = $rootJson->addChild('field');
             $element->addAttribute('name', 'alt');
             $element->addAttribute('type', 'text');
             $element->addAttribute('class', 'inputbox');
             $element->addAttribute('labelclass', 'control-label');
             $element->addAttribute('label', 'PLG_FAF_TS_IE_FORM_ALT_LBL');
             $element->addAttribute('description', 'PLG_FAF_TS_IE_FORM_ALT_DESC');
             $element->addAttribute('filter', 'safehtml');
             $element->addAttribute('fieldset', $fieldset);
             if ($field->params->get('type.create_thumb')) {
                 //src thumb
                 $element = $rootJson->addChild('field');
                 $element->addAttribute('name', 'src_thumb');
                 $element->addAttribute('type', 'hidden');
                 $element->addAttribute('filter', 'safehtml');
                 $element->addAttribute('fieldset', $fieldset);
             } else {
                 // link
                 $element = $rootJson->addChild('field');
                 $element->addAttribute('name', 'link');
                 $element->addAttribute('type', 'text');
                 $element->addAttribute('class', 'inputbox');
                 $element->addAttribute('labelclass', 'control-label');
                 $element->addAttribute('label', 'PLG_FAF_TS_IE_FORM_LINK_LBL');
                 $element->addAttribute('description', 'PLG_FAF_TS_IE_FORM_LINK_DESC');
                 $element->addAttribute('filter', 'safehtml');
                 $element->addAttribute('fieldset', $fieldset);
                 // link target
                 $element = $rootJson->addChild('field');
                 $element->addAttribute('name', 'target');
                 $element->addAttribute('type', 'list');
                 $element->addAttribute('class', 'inputbox');
                 $element->addAttribute('labelclass', 'control-label');
                 $element->addAttribute('label', 'PLG_FAF_TS_IE_FORM_TARGET_LBL');
                 $element->addAttribute('description', 'PLG_FAF_TS_IE_FORM_TARGET_DESC');
                 $element->addAttribute('fieldset', $fieldset);
                 KextensionsXML::addOptionsNode($element, array('PLG_FAF_TS_IE_FORM_TARGET_OPTION_DEFAULT' => '', 'PLG_FAF_TS_IE_FORM_TARGET_OPTION_BLANK' => 1, 'PLG_FAF_TS_IE_FORM_TARGET_OPTION_POPUP' => 2, 'PLG_FAF_TS_IE_FORM_TARGET_OPTION_MODAL' => 3, 'PLG_FAF_TS_IE_FORM_TARGET_OPTION_PARENT' => 4));
             }
         }
         // hr bottom spacer
         $element = $rootJson->addChild('field');
         $element->addAttribute('type', 'spacer');
         $element->addAttribute('name', 'hr_bottom_spacer_' . $field->id);
         $element->addAttribute('hr', 'true');
         $element->addAttribute('fieldset', $fieldset);
         $form->addOrder($field->id, $field->ordering)->setField($field->id, $root);
     }
     return true;
 }
 /**
  * @param    JForm $form The form to be altered.
  * @param    array $data The associated data for the form.
  *
  * @return    boolean
  * @since       1.0.0
  */
 public function onFieldsandfiltersPrepareForm(JForm $form, $data)
 {
     $context = $form->getName();
     if (!($context == $this->_context || $context == 'com_fieldsandfilters.elements.filter')) {
         return true;
     }
     $app = JFactory::getApplication();
     if ($context == 'com_fieldsandfilters.elements.filter') {
         $form->addFieldPath(JPATH_ADMINISTRATOR . '/components/com_categories/models/fields');
         $addform = new SimpleXMLElement('<form />');
         $fields = $addform->addChild('fields');
         $fields->addAttribute('name', 'filter');
         $field = $fields->addChild('field');
         $field->addAttribute('name', 'item_category');
         $field->addAttribute('type', 'category');
         $field->addAttribute('label', 'JOPTION_FILTER_CATEGORY');
         $field->addAttribute('description', 'JOPTION_FILTER_CATEGORY_DESC');
         $field->addAttribute('extension', 'com_content');
         $field->addAttribute('onchange', 'this.form.submit();');
         KextensionsXML::addOptionsNode($field, array('JOPTION_SELECT_CATEGORY' => ''));
         $form->load($addform, false);
     } elseif ($context == $this->_context) {
         if ($app->isSite() && !$this->params->get('frontend_edit', false) || !($extension = FieldsandfiltersFactory::getExtensions()->getExtensionsByName($this->_name)->get($this->_name))) {
             return true;
         }
         JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fieldsandfilters/models', 'FieldsandfiltersModel');
         if (!($elementModel = JModelLegacy::getInstance('element', 'FieldsandfiltersModel', array('ignore_request' => true, 'table_path' => JPATH_ADMINISTRATOR . '/components/com_fieldsandfilters/tables')))) {
             return true;
         }
         // Load Extension Language
         KextensionsLanguage::load('com_fieldsandfilters', JPATH_ADMINISTRATOR);
         $fieldsForm = new KextensionsForm($this->_context . '.' . $this->_name);
         $fieldsData = FieldsandfiltersFieldsHelper::getFieldsByTypeIDColumnFieldType($extension->content_type_id);
         $formPath = $app->isAdmin() ? 'attribs.fieldsandfilters' : 'fieldsandfilters';
         $fieldsForm->setPath($formPath);
         $fieldsetXML = new SimpleXMLElement('<fieldset />');
         $fieldsetXML->addAttribute('name', 'fieldsandfilters');
         $fieldsetXML->addAttribute('label', 'COM_FIELDSANDFILTERS');
         // $fieldsetXML->addAttribute( 'description', 'COM_FIELDSANDFILTERS_FIELDSET_DESC' );
         $fielsXML = $fieldsetXML->addChild('fields');
         $fielsXML->addAttribute('name', 'fieldsandfilters');
         $fieldXML = $fielsXML->addChild('field');
         $fieldXML->addAttribute('name', '_fieldsandfilters');
         $fieldXML->addAttribute('type', 'hidden');
         $fieldXML->addAttribute('fieldset', 'fieldsandfilters');
         if (!empty($data)) {
             $data = (object) $data;
             $elementModel->setState($elementModel->getName() . '.item_id', $data->id);
             $elementModel->setState($elementModel->getName() . '.content_type_id', $extension->content_type_id);
             $elementItem = $elementModel->getItem();
         }
         $isNew = empty($elementItem->id);
         JPluginHelper::importPlugin('fieldsandfilterstypes');
         // Trigger the onFieldsandfiltersPrepareFormField event.
         $app->triggerEvent('onFieldsandfiltersPrepareFormField', array($fieldsForm, $fieldsData, $isNew));
         if ($fieldsFormXML = $fieldsForm->getFormFields()) {
             // Load the XML Helper
             KextensionsXML::setFields($fielsXML, $fieldsFormXML);
             $form->setField($fieldsetXML, $app->isAdmin() ? 'attribs' : null);
             // For joomla 2.5 && Key Reference
             if (!FieldsandfiltersFactory::isVersion()) {
                 $fieldsetXML = new SimpleXMLElement('<fieldset />');
                 $fieldsetXML->addAttribute('name', 'key_reference');
                 $fieldsetXML->addAttribute('label', 'Key Reference');
                 $form->setField($fieldsetXML, 'attribs');
             }
             if ($default = $fieldsForm->getData()) {
                 $form->bind($default);
             }
             if (!$isNew) {
                 $data = new JRegistry();
                 $data->set($formPath, $elementItem->get('fields', new JObject()));
                 $form->bind($data);
             }
         }
         return true;
     }
 }