/**
  * 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;
 }
 /**
  * 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.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.0.0
  **/
 protected function _getBufferPivot()
 {
     $arguments = func_get_args();
     $pivot = $arguments[0];
     unset($arguments[0]);
     $hash = md5($this->method . serialize($arguments));
     if (!isset($this->_pivots[$hash])) {
         if ($this->_isMethod($this->method) && is_string($pivot)) {
             $buffer = call_user_func_array(array($this, $this->method), $arguments);
             $this->_pivots[$hash] = new JObject();
             $this->_pivots[$hash]->elements = new JObject(KextensionsArray::pivot((array) get_object_vars($buffer), $pivot));
             $this->_pivots[$hash]->_pivot = $pivot;
             unset($buffer);
         } else {
             $this->_pivots[$hash] = new JObject();
         }
     } elseif ($this->_pivots[$hash] && $this->_pivots[$hash]->_pivot != $pivot) {
         $buffer = (array) get_object_vars($this->_pivots[$hash]->elements);
         $this->_pivots[$hash]->elements = new JObject(KextensionsArray::pivot(KextensionsArray::flatten($buffer), $pivot));
         $this->_pivots[$hash]->_pivot = $pivot;
     } else {
         // Reset arguments
         $this->_resetArgs();
     }
     return $this->_pivots[$hash]->get('elements', new JObject());
 }