/**
  * @since       1.1.0
  **/
 public function getExtensions($withXML = false)
 {
     if (!property_exists($this->_data, self::PLUGIN_FOLDER)) {
         $data = $this->_getData(self::PLUGIN_FOLDER);
         $elements = $data->elements;
         $default = new JObject();
         $default->name = self::EXTENSION_DEFAULT;
         $default->type = 'com_fieldsandfilters';
         $default->option = self::EXTENSION_DEFAULT;
         $default->content_type_alias = 'com_fieldsandfilters.' . $default->option;
         $data->elements->set($default->option, $default);
         JPluginHelper::importPlugin(self::PLUGIN_FOLDER);
         // Trigger the onFieldsandfiltersPrepareFormField event.
         JFactory::getApplication()->triggerEvent('onFieldsandfiltersPrepareExtensions', array($data->elements));
         $typesAlias = KextensionsArray::getColumn($data->elements, 'content_type_alias');
         $query = $this->_getQuery($typesAlias);
         $types = (array) $this->_db->setQuery($query)->loadObjectList('type_alias');
         $data->set('xml', (bool) $withXML);
         foreach ($data->elements as &$element) {
             $element->content_type_id = (int) array_key_exists($element->content_type_alias, $types) ? $types[$element->content_type_alias]->type_id : 0;
             /* @deprecated  1.2.0 */
             // $element->extension_type_id = $element->content_type_id;
             /* @end deprecated  1.2.0 */
             if ($data->xml) {
                 FieldsandfiltersXml::getPluginOptionsForms($element);
             }
         }
     } elseif ($withXML && !$this->_getData(self::PLUGIN_FOLDER)->xml) {
         $data = $this->_getData(self::PLUGIN_FOLDER);
         $data->set('xml', (bool) $withXML);
         foreach ($data->elements as &$element) {
             FieldsandfiltersXml::getPluginOptionsForms($element);
         }
     }
     return $this->_getData(self::PLUGIN_FOLDER)->elements;
 }
 /**
  * @since       1.1.0
  */
 public function getFieldsandfiltersFieldsHTML(JObject $layoutFields, Jobject $fields, stdClass $element = null, $context = 'fields', JRegistry $params = null, $ordering = 'ordering')
 {
     if (!($fields = $fields->get($this->_name))) {
         return;
     }
     $fields = is_array($fields) ? $fields : array($fields);
     $variables = new JObject();
     $variables->type = $this->_type;
     $variables->name = $this->_name;
     $variables->params = $this->params;
     $variables->element = $element;
     while ($field = array_shift($fields)) {
         $modeName = FieldsandfiltersModes::getModeName($field->mode);
         $isStaticMode = $modeName == FieldsandfiltersModes::MODE_STATIC;
         if ($isStaticMode && empty($field->data) || $modeName == 'field' && (!isset($element->data) || !property_exists($element->data, $field->id))) {
             continue;
         }
         if ($params) {
             $paramsTemp = $field->params;
             $paramsField = clone $field->params;
             $paramsField->merge($params);
             $field->params = $paramsField;
         }
         if ($field->params->get('base.show_name')) {
             FieldsandfiltersFieldsField::preparationContent('base.prepare_name', $field, 'name', $context, $field->id, $params);
         }
         if ($field->params->get('base.site_enabled_description')) {
             FieldsandfiltersFieldsField::preparationContent('base.prepare_description', $field, 'description', $context, $field->id, $params);
         }
         if ($isStaticMode) {
             FieldsandfiltersFieldsField::preparationContent('type.prepare_data', $field, 'data', $context, $field->id, $params);
         } else {
             FieldsandfiltersFieldsField::preparationContentData('type.prepare_data', $field, $element, $context, $field->id, $params);
         }
         $layoutField = FieldsandfiltersFieldsField::getLayout('field', $modeName, $field->params);
         $variables->field = $field;
         $layout = KextensionsPlugin::renderLayout($variables, $layoutField);
         $layoutFields->set(KextensionsArray::getEmptySlotObject($layoutFields, $field->{$ordering}, false), $layout);
         if ($params) {
             $field->params = $paramsTemp;
             unset($paramsField);
         }
     }
     unset($variables);
 }
 /**
  * @since       1.1.0
  */
 public function getFieldsandfiltersFieldsHTML(JObject $layoutFields, Jobject $fields, stdClass $element = null, $context = 'fields', JRegistry $params = null, $ordering = 'ordering')
 {
     if (!($fields = $fields->get($this->_name))) {
         return;
     }
     $fields = is_array($fields) ? $fields : array($fields);
     $variables = new JObject();
     $variables->type = $this->_type;
     $variables->name = $this->_name;
     $variables->params = $this->params;
     $variables->element = $element;
     jimport('joomla.filesystem.file');
     while ($field = array_shift($fields)) {
         $modeName = FieldsandfiltersModes::getModeName($field->mode);
         $isStaticMode = $modeName == FieldsandfiltersModes::MODE_STATIC;
         if ($isStaticMode && empty($field->data) || $modeName == 'field' && (!isset($element->data) || !property_exists($element->data, $field->id))) {
             continue;
         }
         $dataElement = $isStaticMode ? $field->data : $element->data->get($field->id);
         if (is_string($dataElement)) {
             if ($isStaticMode) {
                 $field->data = new JRegistry($dataElement);
             } else {
                 $element->data->set($field->id, new JRegistry($dataElement));
             }
         }
         if ($params) {
             $paramsTemp = $field->params;
             $paramsField = clone $field->params;
             $paramsField->merge($params);
             $field->params = $paramsField;
         }
         if ($field->params->get('base.show_name')) {
             FieldsandfiltersFieldsField::preparationContent('base.prepare_name', $field, 'name', $context, $field->id, $params);
         }
         if ($field->params->get('base.site_enabled_description')) {
             FieldsandfiltersFieldsField::preparationContent('base.prepare_description', $field, 'description', $context, $field->id, $params);
         }
         // create new image if not exists
         $scaleImage = (int) $field->params->def('type.scale', $this->params->get('scale', 0));
         $createThumb = (bool) $field->params->get('type.create_thumb');
         $scaleThumb = (int) $field->params->def('type.scale_thumb', $this->params->get('scale_thumb', 0));
         if ($scaleImage || $createThumb && $scaleThumb) {
             $data = $isStaticMode ? $field->data : $element->data->get($field->id, new JRegistry());
             if (($image = $data->get('image')) && file_exists(JPath::clean(JPATH_ROOT . '/' . $image))) {
                 if ($scaleImage && ($src = $data->get('src')) && !file_exists(JPath::clean(JPATH_ROOT . '/' . $src))) {
                     $imageInfo = self::prepareImageInfo($field, $element, $image, basename($src), $scaleImage);
                     try {
                         if (!FieldsandfiltersImage::createImage($field->name, $imageInfo)) {
                             throw new RuntimeException(JText::sprintf('COM_FIELDSANDFILTERS_ERROR_NOT_CREATE_IMAGE', $field->name));
                         }
                     } catch (Exception $e) {
                         JLog::add($e->getMessage(), JLog::ERROR, 'plgFieldsandfiltersTypesImage');
                     }
                 }
                 if ($createThumb && $scaleThumb && ($src = $data->get('src_thumb')) && !file_exists(JPath::clean(JPATH_ROOT . '/' . $src))) {
                     $imageInfo = $this->prepareImageInfo($field, $element, $image, basename($src), $scaleThumb, 'thumb');
                     try {
                         if (!FieldsandfiltersImage::createImage($field->name . ' Thumbs', $imageInfo)) {
                             throw new RuntimeException(JText::sprintf('COM_FIELDSANDFILTERS_ERROR_NOT_CREATE_IMAGE', $field->name . ' Thumb'));
                         }
                     } catch (Exception $e) {
                         JLog::add($e->getMessage(), JLog::ERROR, 'plgFieldsandfiltersTypesImage');
                     }
                 }
             }
             unset($data, $imageInfo);
         }
         unset($fieldTypeParams);
         $layoutField = FieldsandfiltersFieldsField::getLayout('field', $modeName, $field->params);
         $variables->field = $field;
         $layout = KextensionsPlugin::renderLayout($variables, $layoutField);
         $layoutFields->set(KextensionsArray::getEmptySlotObject($layoutFields, $field->{$ordering}, false), $layout);
         if ($params) {
             $field->params = $paramsTemp;
             unset($paramsField);
         }
     }
     unset($variables, $imageInfo);
 }
 /**
  * Method to save the form data.
  *
  * @param   array $data The form data.
  *
  * @return  boolean  True on success, False on error.
  *
  * @since       1.1.0
  */
 public function save($data)
 {
     $app = JFactory::getApplication();
     $table = $this->getTable();
     $key = $table->getKeyName();
     $pk = !empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id');
     $isNew = true;
     // Include the content plugins for the on save events.
     JPluginHelper::importPlugin('content');
     JPluginHelper::importPlugin('fieldsandfilterstypes');
     // Allow an exception to be thrown.
     try {
         // Load the row if saving an existing record.
         if ($pk > 0) {
             $table->load($pk);
             $isNew = false;
         }
         // Bind the data.
         if (!$table->bind($data)) {
             throw new Exception($table->getError());
         }
         // Prepare the row for saving
         $this->prepareTable($table);
         // Check the data.
         if (!$table->check()) {
             throw new Exception($table->getError());
         }
         // Load PluginExtensions Helper
         $extensionName = ($extension = FieldsandfiltersFactory::getExtensions()->getExtensionsByTypeIDPivot('content_type_id', $table->content_type_id)->get($table->content_type_id)) ? $extension->name : '';
         $context = $this->option . '.' . $this->name . '.' . $extensionName;
         // Trigger the onContentBeforeSave event.
         $result = $app->triggerEvent($this->event_before_save, array($context, $table, $isNew));
         if (in_array(false, $result, true)) {
             throw new Exception($table->getError());
         }
         $tableFields = (array) $table->get('fields');
         // Get old item
         $item = $this->getItem($table->{$key});
         // Store the data.
         if (!$table->store()) {
             throw new Exception($table->getError());
         }
         $table->set('fields', JArrayHelper::toObject($tableFields, 'JObject'));
         // Store fields data and connections
         // Trigger the onFieldsandfiltersBeforeSaveData event.
         $result = $app->triggerEvent('onFieldsandfiltersBeforeSaveData', array($this->option . '.' . $this->name, $table, $item, $isNew));
         // array($newItem, $oldItem)
         if (in_array(false, $result, true)) {
             throw new Exception($table->getError());
         }
         $item = $item->get('fields', new JObject());
         $dataItem = $item->get('data', new JObject());
         $connectionsItem = $item->get('connections', new JObject());
         $tableFields = $table->get('fields', new JObject());
         $data = $tableFields->get('data', new JObject());
         $connections = $tableFields->get('connections', new JObject());
         $filterMode = (array) FieldsandfiltersModes::getMode(FieldsandfiltersModes::MODE_FILTER, array());
         $otherMode = (array) FieldsandfiltersModes::getModes(null, array(), true, $filterMode);
         $fields = FieldsandfiltersFieldsHelper::getFieldsByTypeIDColumnFieldType($table->content_type_id);
         $fields = KextensionsArray::flatten(get_object_vars($fields));
         while ($field = array_shift($fields)) {
             $_data = (string) $data->get($field->id, '');
             $_dataItem = (string) $dataItem->get($field->id, '');
             $_connections = (array) $connections->get($field->id, new JObject())->getProperties(true);
             $_connectionsItem = (array) $connectionsItem->get($field->id, new JObject())->getProperties(true);
             // other (field/static)
             if (in_array($field->mode, $otherMode) && (!empty($_data) || !empty($_dataItem))) {
                 $tableObject = new stdClass();
                 $tableObject->field_id = (int) $field->id;
                 // delete text
                 if (!empty($_dataItem) && empty($_data)) {
                     $table->deleteData($tableObject);
                 } elseif (empty($_dataItem) && !empty($_data)) {
                     $tableObject->data = $_data;
                     $table->insertData($tableObject);
                 } elseif ($_dataItem != $_data) {
                     $tableObject->data = $_data;
                     $table->updateData($tableObject);
                 }
             } elseif (in_array($field->mode, $filterMode) && (!empty($_connections) || !empty($_connectionsItem))) {
                 $tableObject = new stdClass();
                 $tableObject->field_id = (int) $field->id;
                 $field_valuesID = array_keys($field->values->getProperties(true));
                 $_connections = array_intersect($field_valuesID, $_connections);
                 $__connections = array_unique(array_diff($_connections, $_connectionsItem));
                 JArrayHelper::toInteger($__connections);
                 if (!empty($__connections)) {
                     $tableObject->field_value_id = $__connections;
                     $table->insertConnections($tableObject);
                 }
                 $__connections = array_unique(array_diff($_connectionsItem, $_connections));
                 JArrayHelper::toInteger($__connections);
                 if (!empty($__connections)) {
                     $tableObject = new stdClass();
                     $tableObject->field_value_id = $__connections;
                     $table->deleteConnections($tableObject);
                 }
             }
         }
         // Trigger the onContentAfterSave event.
         $app->triggerEvent($this->event_after_save, array($context, $table, $isNew));
         // Clean the cache.
         $this->cleanCache();
     } catch (Exception $e) {
         $this->setError($e->getMessage());
         return false;
     }
     $pkName = $table->getKeyName();
     if (isset($table->{$pkName})) {
         $this->setState($this->getName() . '.id', $table->{$pkName});
     }
     $this->setState($this->getName() . '.new', $isNew);
     return true;
 }
 /**
  * @since       1.2.0
  **/
 public static function preparationContent(&$text, $context = '', $option = null, $itemID = null, array $excluded = array(), $syntax = null, $syntaxType = self::SYNTAX_SIMPLE)
 {
     $syntax = $syntax ? $syntax : KextensionsPlugin::getParams('system', 'fieldsandfilters')->get('syntax', '#{%s}');
     $syntaxType = $syntaxType ? $syntaxType : KextensionsPlugin::getParams('system', 'fieldsandfilters')->get('syntax_type', self::SYNTAX_SIMPLE);
     if (strpos($syntax, '%s') !== false) {
         $prefix = explode('%s', $syntax);
         // simple performance check to determine whether bot should process further
         if (!($prefix = $prefix[0]) || strpos($text, $prefix) === false) {
             return true;
         }
         /* @deprecated  1.2.0 */
         if ($syntaxType == self::SYNTAX_OLD) {
             $regex = '(?P<field_id>\\d+)(?:,(?P<item_id>\\d+)|)(?:,(?P<option>[\\w.-]+)|)';
         } else {
             $regex = '(?P<field_id>\\d+)(?:,(?P<item_id>\\d+|)(?::(?P<option>[\\w.-]+)|)|)(?:,(?P<context>[\\w.-]+)|)(?:,(?P<params>{.*?})|)';
         }
         $regex = '/' . sprintf($syntax, $regex) . '/i';
         // Find all instances of plugin and put in $matches for loadposition
         // $matches[0] is full pattern match
         preg_match_all($regex, $text, $matches, PREG_SET_ORDER);
         if (!$matches) {
             return true;
         }
         $jinput = JFactory::getApplication()->input;
         $itemID = ($itemID = (int) $itemID) ? $itemID : $jinput->get('id', 0, 'int');
         $option = $option ? $option : $jinput->get('option');
         $extensionsOptions = FieldsandfiltersFactory::getExtensions()->getExtensionsColumn('option');
         $combinations = array();
         $getAllextensions = true;
         $excludes = array();
         $isExtended = $syntaxType == self::SYNTAX_EXTENDED;
         foreach ($matches as $match) {
             /* field id */
             if (!($fieldID = (int) $match['field_id']) || in_array($fieldID, $excluded)) {
                 $excludes[] = $match[0];
                 continue;
             }
             /* context */
             if (!empty($match['context']) && $match['context'] != $context) {
                 $excludes[] = $match[0];
                 continue;
             }
             /* component - option + item id */
             $_itemID = isset($match['item_id']) && ($val = (int) $match['item_id']) ? $val : $itemID;
             $_option = !empty($match['option']) ? $match['option'] : $option;
             if (!in_array($_option, $extensionsOptions)) {
                 $excludes[] = $match[0];
                 continue;
             }
             $key = $_option . '-' . $_itemID;
             if (!array_key_exists($key, $combinations)) {
                 $combinations[$key] = array('item_id' => $_itemID, 'option' => $_option, 'fields_id' => array());
                 if ($isExtended) {
                     $combinations[$key]['elements'] = array();
                 } else {
                     $combinations[$key]['matches'] = array();
                 }
             }
             /* params */
             if ($isExtended) {
                 $keyElement = $params = null;
                 if (!empty($match['params'])) {
                     $params = new JRegistry($match['params']);
                     $keyElement = $params->toString();
                     if ($keyElement != '{}') {
                         $keyElement = md5($keyElement);
                     } else {
                         $keyElement = $params = null;
                     }
                 }
                 if (!array_key_exists($keyElement, $combinations[$key]['elements'])) {
                     $combinations[$key]['elements'][$keyElement] = array('matches' => array(), 'params' => $params);
                 }
                 $combinations[$key]['elements'][$keyElement]['matches'][$fieldID][] = $match[0];
             } else {
                 $combinations[$key]['matches'][$fieldID][] = $match[0];
             }
             if (!in_array($fieldID, $combinations[$key]['fields_id'])) {
                 $combinations[$key]['fields_id'][] = $fieldID;
             }
         }
         if (!empty($combinations)) {
             while ($combination = array_shift($combinations)) {
                 $object = self::getFieldsByItemID($combination['option'], $combination['item_id'], $combination['fields_id'], $getAllextensions);
                 if ($isExtended) {
                     $isFields = $object->fields->getProperties(true);
                     $isFields = !empty($isFields);
                     while ($element = array_shift($combination['elements'])) {
                         if (!$isFields) {
                             $excludes = array_merge($excludes, KextensionsArray::flatten($element['matches']));
                             continue;
                         }
                         $_object = clone $object;
                         $_fieldsID = array_keys($element['matches']);
                         foreach ($_fieldsID as $_fieldID) {
                             if (!isset($_object->fields->{$_fieldID})) {
                                 unset($_object->fields->{$_fieldID});
                                 $excludes = array_merge($excludes, $element['matches'][$_fieldID]);
                                 unset($element['matches'][$_fieldID]);
                             }
                         }
                         $fieldsLayouts = self::getFieldsLayouts($_object, $context, $element['params'], 'id');
                         foreach ($element['matches'] as $fieldID => &$match) {
                             $text = str_replace($match, $fieldsLayouts->get($fieldID, ''), $text);
                         }
                     }
                 } else {
                     $fieldsLayouts = self::getFieldsLayouts($object, $context, null, 'id');
                     foreach ($combination['matches'] as $fieldID => &$match) {
                         $text = str_replace($match, $fieldsLayouts->get($fieldID, ''), $text);
                     }
                 }
             }
         }
         if (!empty($excludes)) {
             $text = str_replace(array_unique($excludes), '', $text);
         }
     }
     return true;
 }
 /**
  * @since    1.1.0
  */
 public function getFieldsandfiltersFiltersHTML(JObject $layoutFilters, JObject $fields, $context = 'filters', JRegistry $params = null, $ordering = 'ordering')
 {
     if (!($fields = $fields->get($this->_name))) {
         return;
     }
     $fields = is_array($fields) ? $fields : array($fields);
     $variables = new JObject();
     $variables->type = $this->_type;
     $variables->name = $this->_name;
     $variables->params = $this->params;
     while ($field = array_shift($fields)) {
         if ($params) {
             $paramsTemp = $field->params;
             $paramsField = clone $field->params;
             $paramsField->merge($params);
             $field->params = $paramsField;
         }
         if ($field->params->get('base.show_name')) {
             FieldsandfiltersFieldsField::preparationContent('base.prepare_name', $field, 'name', $context, $field->id, $params);
         }
         if ($field->params->get('base.site_enabled_description')) {
             FieldsandfiltersFieldsField::preparationContent('base.prepare_description', $field, 'description', $context, $field->id, $params);
         }
         FieldsandfiltersFieldsField::preparationContentValues('type.prepare_values', $field, $context, $field->id, $params);
         $layoutFilter = FieldsandfiltersFieldsField::getLayout('filter', 'filter', $field->params);
         $variables->field = $field;
         $layout = KextensionsPlugin::renderLayout($variables, $layoutFilter);
         $layoutFilters->set(KextensionsArray::getEmptySlotObject($layoutFilters, $field->{$ordering}, false), $layout);
         if ($params) {
             $field->params = $paramsTemp;
             unset($paramsFilter);
         }
     }
     unset($variables);
 }
 /**
  *
  * @since       1.1.0
  */
 protected function _beforeSearchElements($data, $key = 'id')
 {
     if ($this->method == 'getElementsByItemID') {
         // Get elements id from elements where is items id
         $this->_elementsItemsID = KextensionsArray::getColumn($data->get('elements', new stdClass()), 'item_id', 'id');
         $key = 'item_id';
     }
     return parent::_beforeSearchElements($data, $key);
 }
 /**
  *
  * @since       1.0.0
  */
 protected function _beforeSearchElements($data, $key = 'id')
 {
     $elements = array();
     foreach ($this->_data as $_data) {
         if ($_data == $data) {
             continue;
         }
         $elements = array_merge($elements, KextensionsArray::getColumn($_data->get('elements', new stdClass()), 'item_id', 'id'));
     }
     return $elements;
 }
 /**
  * Get a mode type values.
  *
  * @param   string  $paths    Array mode paths (e.g. array(values.single, values.multi)
  * @param   mixed   $default  Optional default value, returned if the internal value is null.
  * @param   boolean $pathKey  Keys of array is the name of modes
  * @param   boolean $flatten  Flatten array
  * @param   array   $excluded Excluded items array
  *
  * @return  mixed  Value of entry or null
  *
  * @since       1.1.0
  */
 public static function getModes($paths = null, $default = array(), $flatten = false, $excluded = false, $pathKey = false)
 {
     $modes = array();
     $isExcluded = $excluded && is_array($excluded);
     if (is_null($paths)) {
         $modes = self::$modes;
     } else {
         if (is_array($paths)) {
             while ($path = array_shift($paths)) {
                 if ($mode = self::getMode($path, false)) {
                     if ($pathKey) {
                         $modes[$path] = $mode;
                     } else {
                         $modes[] = $mode;
                     }
                 }
             }
         } else {
             if (is_string($paths)) {
                 $modes = (array) self::getMode($paths);
             }
         }
     }
     if (!empty($modes)) {
         if ($flatten || $isExcluded) {
             $modes = KextensionsArray::flatten($modes);
         }
         if ($isExcluded) {
             $modes = array_diff($modes, $excluded);
         }
     } else {
         $modes = $default;
     }
     return $modes;
 }
 /**
  * @since       1.0.0
  **/
 protected function _getBufferColumn()
 {
     $arguments = func_get_args();
     $hash = md5($this->method . serialize($arguments));
     if (!isset($this->_columns[$hash])) {
         $column = $arguments[0];
         unset($arguments[0]);
         if ($this->_isMethod($this->method) && is_string($column)) {
             $buffer = call_user_func_array(array($this, $this->method), $arguments);
             $this->_columns[$hash] = KextensionsArray::getColumn($buffer, $column);
             unset($buffer);
         } else {
             $this->_columns[$hash] = array();
         }
     } else {
         // Reset arguments
         $this->_resetArgs();
     }
     return $this->_columns[$hash];
 }