/**
  * Method to perform sanity checks on the JTable instance properties to ensure
  * they are safe to store in the database.  Child classes should override this
  * method to make sure the data they are storing in the database is safe and
  * as expected before storage.
  *
  * @return  boolean  True if the instance is sane and able to be stored in the database.
  *
  * @link     http://docs.joomla.org/JTable/check
  * @since    1.1.0
  */
 public function check()
 {
     //If there is an ordering column and this is a new row then get the next ordering value
     if ($this->id == 0) {
         $this->ordering = self::getNextOrder();
     }
     try {
         // Check for exist mode field
         if (!in_array($this->mode, (array) FieldsandfiltersModes::getModes(null, array(), true))) {
             throw new RuntimeException(JText::_('COM_FIELDSANDFILTERS_DATABASE_ERROR_VALID_FIELD_MODE'));
             return false;
         }
         // Check for a field name.
         if (trim($this->name) == '') {
             throw new RuntimeException(JText::_('COM_FIELDSANDFILTERS_DATABASE_ERROR_VALID_FIELD_NAME'));
         }
         // Check for a field type.
         if (trim($this->type) == '') {
             throw new RuntimeException(JText::_('COM_FIELDSANDFILTERS_DATABASE_ERROR_VALID_FIELD_TYPE'));
         } elseif (!FieldsandfiltersFactory::getTypes()->getTypes()->get($this->type)) {
             throw new RuntimeException(JText::sprintf('COM_FIELDSANDFILTERS_DATABASE_ERROR_FIELD_TYPE_NOT_EXISTS', $this->type));
         }
         // Check for a field extension type id.
         if (!$this->content_type_id) {
             throw new RuntimeException(JText::_('COM_FIELDSANDFILTERS_DATABASE_ERROR_VALID_FIELD_CONTENT_TYPE_ID'));
         } elseif (!FieldsandfiltersFactory::getExtensions()->getExtensionsByTypeID($this->content_type_id)->get($this->content_type_id)) {
             throw new RuntimeException(JText::sprintf('COM_FIELDSANDFILTERS_DATABASE_ERROR_CONTENT_TYPE_ID_NOT_EXISTS', $this->content_type_id));
         }
     } catch (RuntimeException $e) {
         $this->setError($e->getMessage());
         return false;
     }
     // Check mode field
     if (in_array($this->mode, (array) FieldsandfiltersModes::getMode(FieldsandfiltersModes::MODE_FILTER))) {
         // Check for a field alias
         if (trim($this->alias) == '') {
             $this->alias = $this->name;
         }
         $this->alias = JApplication::stringURLSafe($this->alias);
         if (trim(str_replace('-', '', $this->alias)) == '') {
             $this->alias = JFactory::getDate()->format('Y-m-d-H-i-s');
         }
     } elseif (trim($this->alias) != '') {
         $this->alias = '';
     }
     return true;
 }
 /**
  * Method to delete one or more records.
  *
  * @param   array &$pks An array of record primary keys.
  *
  * @return  boolean  True if successful, false if an error occurs.
  * @since       1.1.0
  */
 public function delete(&$pks)
 {
     $app = JFactory::getApplication();
     $pks = (array) $pks;
     $table = $this->getTable();
     $valueTable = $this->getTable('Fieldvalue', 'FieldsandfiltersTable');
     $elementTable = $this->getTable('Element', 'FieldsandfiltersTable');
     $filterMode = (array) FieldsandfiltersModes::getMode(FieldsandfiltersModes::MODE_FILTER);
     $otherMode = (array) FieldsandfiltersModes::getModes(null, array(), true, $filterMode);
     // Include the content plugins for the on delete events.
     JPluginHelper::importPlugin('content');
     // Include the fieldsandfilters Types plugins for the on save events.
     JPluginHelper::importPlugin(FieldsandfiltersTypes::PLUGIN_FOLDER);
     try {
         // Iterate the items to delete each one.
         foreach ($pks as $i => $pk) {
             if (!$table->load($pk) || !$this->canDelete($table)) {
                 throw new Exception($table->getError());
             }
             $context = $this->option . '.' . $this->name;
             // Trigger the onContentBeforeDelete event.
             $result = $app->triggerEvent($this->event_before_delete, array($context, $table));
             if (in_array(false, $result, true)) {
                 throw new Exception($table->getError());
             }
             // Get old item
             $item = $this->getItem($pk);
             // Trigger the onFieldsandfiltersBeforeSaveData event.
             $result = $app->triggerEvent('onFieldsandfiltersBeforeDeleteData', array($context, $item));
             if (in_array(false, $result, true)) {
                 throw new Exception($table->getError());
             }
             $objectTable = new stdClass();
             $objectTable->field_id = $table->id;
             $objectTable->element_id = null;
             //delete fields values and connections
             if (in_array($table->mode, $filterMode)) {
                 if (!$valueTable->deleteByFieldID($table->id)) {
                     throw new Exception($valueTable->getError());
                 }
                 if (!$elementTable->deleteConnections($objectTable)) {
                     throw new Exception($elementTable->getError());
                 }
             } else {
                 if (in_array($table->mode, $otherMode) && !$elementTable->deleteData($objectTable)) {
                     throw new Exception($elementTable->getError());
                 }
             }
             if (!$table->delete($pk)) {
                 throw new Exception($table->getError());
             }
             // Trigger the onContentAfterDelete event.
             $app->triggerEvent($this->event_after_delete, array($context, $table));
         }
         // Clear the component's cache
         $this->cleanCache();
     } catch (Exception $e) {
         $this->setError($e->getMessage());
         return false;
     }
     return true;
 }
 /**
  * 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 getFieldsByTypeIDColumnFieldType($contentTypeID, $withStatic = null, $states = array(1, -1), $getAllextensions = true)
 {
     $withStatic = !is_null($withStatic) ? $withStatic : JComponentHelper::getParams('com_fieldsandfilters')->get('show_static_fields', true);
     $contentTypeID = (array) $contentTypeID;
     if ($getAllextensions && ($extension = FieldsandfiltersFactory::getExtensions()->getExtensionsByName(FieldsandfiltersExtensions::EXTENSION_DEFAULT)->get(FieldsandfiltersExtensions::EXTENSION_DEFAULT))) {
         $contentTypeID[] = (int) $extension->content_type_id;
     }
     $fieldsHelper = FieldsandfiltersFactory::getFields();
     if ($withStatic) {
         $fields = $fieldsHelper->getFieldsPivot('type', $contentTypeID, $states, FieldsandfiltersFields::VALUES_BOTH);
     } else {
         $staticMode = (array) FieldsandfiltersModes::getMode(FieldsandfiltersModes::MODE_STATIC, array());
         $otherMode = (array) FieldsandfiltersModes::getModes(null, array(), true, $staticMode);
         $fields = $fieldsHelper->getFieldsByModeIDPivot('type', $contentTypeID, $otherMode, $states, FieldsandfiltersFields::VALUES_BOTH);
     }
     return $fields;
 }
 /**
  * Method to get the available options fields item. value & text
  *
  * @return    array    array associate value and text
  * @since       1.1.0
  */
 public static function fields($modes = FieldsandfiltersModes::MODE_FILTER, $state = 1)
 {
     $options = array();
     // Load Extensions Helper
     $extenionsID = FieldsandfiltersFactory::getExtensions()->getExtensionsColumn('content_type_id');
     $modes = FieldsandfiltersModes::getModes($modes, array(), true);
     // Load Fields Helper
     $fieldsHelper = FieldsandfiltersFactory::getFields();
     if (!empty($modes)) {
         $fields = $fieldsHelper->getFieldsByModeID($extenionsID, $modes, $state);
     } else {
         $fields = $fieldsHelper->getFields($extenionsID, $state);
     }
     if ($fields = get_object_vars($fields)) {
         while ($field = array_shift($fields)) {
             $options[] = JHtml::_('select.option', $field->id, $field->name);
         }
     }
     return $options;
 }