public static function setUserStateFieldID($context, $name = 'field_id')
 {
     $app = JFactory::getApplication();
     $userState = (array) $app->getUserState($context);
     if ($fieldID = $app->input->getInt($name)) {
         if ($fieldID != JArrayHelper::getValue($userState, $name)) {
             $userState[$name] = $fieldID;
             $app->setUserState($context, $userState);
             return true;
         }
     } else {
         if (!JArrayHelper::getValue($userState, $name)) {
             $filterMode = FieldsandfiltersModes::getMode(FieldsandfiltersModes::MODE_FILTER);
             $extensionsID = FieldsandfiltersFactory::getExtensions()->getExtensionsColumn('content_type_id');
             $fieldsID = FieldsandfiltersFactory::getFields()->getFieldsByModeIDColumn('id', $extensionsID, $filterMode, array(1, -1));
             $fieldID = current($fieldsID);
             $userState[$name] = $fieldID;
             $app->setUserState($context, $userState);
             return true;
         }
     }
     return false;
 }
 /**
  * @since       1.1.0
  */
 public function fields()
 {
     // Check for a valid token. If invalid, send a 403 with the error message.
     JSession::checkToken('request') or die;
     $app = JFactory::getApplication();
     $fieldID = $app->input->get('fid', 0, 'int');
     $extensionTypeID = $app->input->get('etid', 0, 'int');
     if ($app->input->get('format') == 'raw' && $extensionTypeID && $fieldID) {
         // Load PluginExtensions Helper
         if (!($extension = FieldsandfiltersFactory::getExtensions()->getExtensionsByTypeID($extensionTypeID)->get($extensionTypeID))) {
             // $app->enqueueMessage( JText::_( JText::_( 'COM_FILEDSANDFILTERS_FILTERS_ERROR_EXTENSION_NOT_EXISTS' ), 'error' ) );
         } else {
             if (!($field = FieldsandfiltersFactory::getFields()->getFieldsByID($extension->extension_type_id, $fieldID)->get($fieldID))) {
                 // $app->enqueueMessage( JText::_( JText::_( 'COM_FILEDSANDFILTERS_FILTERS_ERROR_FIELD_NOT_EXISTS' ), 'error' ) );
             } else {
                 JPluginHelper::importPlugin('fieldsandfilterstypes');
                 $context = 'com_fieldsandfilters.fields.' . $field->field_type;
                 $app->triggerEvent('onFieldsandfiltersFieldsRequestRaw', array($context, $field));
             }
         }
     }
     $this->setRedirect(JURI::root(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;
 }
 /**
  * @since       1.1.0
  */
 public function onFieldsandfiltersPrepareFiltersHTML($context, Jobject $filters, $fieldsID = null, $getAllextensions = true, JRegistry $params = null, $ordering = 'ordering')
 {
     if (!in_array($context, $this->_contexts)) {
         return;
     }
     $app = JFactory::getApplication();
     $jinput = $app->input;
     $id = $jinput->get('id', 0, 'int');
     // Load PluginExtensions Helper
     $extensionsHelper = FieldsandfiltersFactory::getExtensions();
     $extensionContent = $extensionsHelper->getExtensionsByName($this->_name)->get($this->_name);
     if ($context == $this->_contexts['category'] && !$id || !$extensionContent) {
         return;
     }
     // add model path
     $prefix = get_class($this);
     list($option, $class) = explode('.', $context);
     JModelLegacy::addIncludePath(sprintf('%s/%s/%s/overrides/models', JPATH_PLUGINS, $this->_type, $this->_name), $prefix . 'Model');
     if (!($model = JModelLegacy::getInstance($class, $prefix . 'Model', array('ignore_request' => false, 'table_path' => JPATH_ADMINISTRATOR . '/components/' . $option . '/tables')))) {
         return;
     }
     $itemsID = $model->getContentItemsID();
     if (empty($itemsID)) {
         return;
     }
     // Load Filters Helper
     $counts = FieldsandfiltersFiltersHelper::getFiltersValuesCount($extensionContent->content_type_id, $fieldsID, $itemsID, $model->getState('filter.published'));
     if (empty($counts)) {
         return;
     }
     $extensionsID = $getAllextensions ? $extensionsHelper->getExtensionsByNameColumn('content_type_id', array(FieldsandfiltersExtensions::EXTENSION_DEFAULT, $this->_name)) : $extensionContent->content_type_id;
     // Load Fields Helper
     $fieldsHelper = FieldsandfiltersFactory::getFields();
     if (is_null($fieldsID)) {
         // Load PluginTypes Helper
         if ($modes = FieldsandfiltersModes::getMode(FieldsandfiltersModes::MODE_FILTER)) {
         }
         // multi extensions array( $this->_name, 'allextensions' )
         // $fields = $fieldsHelper->getFieldsByModeIDPivot( 'type', $extensionsID, $modes, 1, true );
         // single extension $this->_name
         $fields = $fieldsHelper->getFieldsByModeIDPivot('type', $extensionsID, $modes, 1, FieldsandfiltersFields::VALUES_VALUES, true);
     } else {
         if (is_numeric($fieldsID) || is_array($fieldsID)) {
             // multi extensions array( $this->_name, 'allextensions' )
             // $fields = $fieldsHelper->getFieldsByIDPivot( 'type', $extensionContent->content_type_id, $fieldsID, 1, true );
             // single extension $this->_name
             $fields = $fieldsHelper->getFieldsByIDPivot('type', $extensionsID, $fieldsID, 1, FieldsandfiltersFields::VALUES_VALUES, true);
         } else {
             return;
         }
     }
     $templateFields = new JObject();
     JPluginHelper::importPlugin('fieldsandfilterstypes');
     // Trigger the onFieldsandfiltersPrepareFormField event.
     $app->triggerEvent('getFieldsandfiltersFiltersHTML', array($templateFields, $fields, $context, $params, $ordering));
     $templateFields = $templateFields->getProperties(true);
     if (empty($templateFields)) {
         return;
     }
     ksort($templateFields);
     $filters->set('layouts', $templateFields);
     $filters->set('request', array('extensionID' => (int) $extensionContent->content_type_id, 'id' => $app->input->get('id', 0, 'int')));
     $filters->set('counts', (array) $counts);
     $router = JFactory::getApplication()->getRouter();
     if ($router->getMode() == JROUTER_MODE_SEF) {
         $filters->set('pagination', array('start' => 0));
     } else {
         $filters->set('pagination', array('limitstart' => 0));
     }
     $selectors = $filters->get('selectors');
     if ($context == $this->_contexts['category'] && ($selector = trim($this->params->get('selector_other_category', '#adminForm')))) {
         $selectors['other'] = $selector;
     } elseif ($context == $this->_contexts['archive'] && ($selector = trim($this->params->get('selector_other_archive', '#adminForm')))) {
         $selectors['other'] = $selector;
     } elseif ($context == $this->_contexts['featured'] && ($selector = trim($this->params->get('selector_other_featured', '')))) {
         $selectors['other'] = $selector;
     }
     $filters->set('selectors', $selectors);
     return implode("\n", $templateFields);
 }
 /**
  * Method to get the available options field values item. value & text
  *
  * @return    array    array associate value and text
  * @since       1.1.0
  */
 public static function fieldValues($fieldID, $state = 1)
 {
     $options = array();
     $extenionsID = FieldsandfiltersFactory::getExtensions()->getExtensionsColumn('content_type_id');
     $field = FieldsandfiltersFactory::getFields()->getFieldsByID($extenionsID, $fieldID, $state, true);
     if (($field = $field->get($fieldID)) && ($values = $field->values->getProperties(true))) {
         while ($value = array_shift($values)) {
             $options[] = JHtml::_('select.option', $value->id, $value->value);
         }
     }
     return $options;
 }
 /**
  * @since       1.1.0
  */
 public function fields()
 {
     // Is ajax requerst
     KextensionsRequest::isAjax() or die;
     // Check for a valid token. If invalid, send a 403 with the error message.
     JSession::checkToken('request') or $this->sendResponse(new Exception(JText::_('JINVALID_TOKEN'), 403));
     $app = JFactory::getApplication();
     $requestID = $app->input->get('requestID', 0, 'alnum');
     $fieldID = $app->input->get('fieldID', 0, 'int');
     $extensionTypeID = $app->input->get('extensionID', 0, 'int');
     try {
         // Check if exist requestID and extention type id
         if (!$requestID) {
             throw new Exception(JText::_('COM_FILEDSANDFILTERS_FILTERS_ERROR_REQUEST_ID'), 403);
         } elseif (!$extensionTypeID) {
             throw new Exception(JText::_('COM_FILEDSANDFILTERS_FILTERS_ERROR_REQUEST_ID'), 403);
         } elseif (!$fieldID) {
             throw new Exception(JText::_('COM_FILEDSANDFILTERS_FILTERS_ERROR_FIELD_ID'), 403);
         }
         // Load PluginExtensions Helper
         if (!($extension = FieldsandfiltersFactory::getExtensions()->getExtensionsByTypeID($extensionTypeID)->get($extensionTypeID))) {
             throw new Exception(JText::_('COM_FILEDSANDFILTERS_FILTERS_ERROR_EXTENSION_NOT_EXISTS'), 403);
         } elseif (!($field = FieldsandfiltersFactory::getFields()->getFieldsByID($extension->extension_type_id, $fieldID)->get($fieldID))) {
             throw new Exception(JText::_('COM_FILEDSANDFILTERS_FILTERS_ERROR_FIELD_NOT_EXISTS'), 403);
         }
         self::_prepareDocument();
         JPluginHelper::importPlugin('fieldsandfilterstypes');
         // Trigger the onFieldsandfiltersPrepareFormField event.
         $context = 'com_fieldsandfilters.fields.' . $field->field_type;
         $data = new JObject();
         $app->triggerEvent('onFieldsandfiltersRequestJSON', array($context, $field, $data));
         // Send the response.
         $this->sendResponse($data);
     } catch (Exception $e) {
         $this->sendResponse($e);
     }
 }