/**
  * @since       1.2.0
  *
  */
 public static function getParams($key, JObject $extensions, $default = null)
 {
     $value = $default;
     $isEmptyValue = true;
     // get module param
     if (!property_exists($extensions, 'module.off')) {
         $valueName = 'module.value';
         if (property_exists($extensions, $valueName)) {
             $value = $extensions->{$valueName};
             if ($value !== null && $value !== '') {
                 $isEmptyValue = false;
             }
         } else {
             if ($moduleID = (int) $extensions->get('module.id')) {
                 if (!is_null($value = KextensionsModule::getParams($moduleID)->get($key))) {
                     $isEmptyValue = false;
                 }
             }
         }
     }
     // get plugin param
     if ($isEmptyValue && !property_exists($extensions, 'plugin.off')) {
         $valueName = 'plugin.value';
         if (property_exists($extensions, $valueName)) {
             $value = $extensions->{$valueName};
             if ($value !== null && $value !== '') {
                 $isEmptyValue = false;
             }
         } else {
             if (($type = $extensions->get('plugin.type', 'fieldsandfiltersExtensions')) && ($name = $extensions->get('plugin.name'))) {
                 if (!is_null($value = KextensionsPlugin::getParams($type, $name)->get($key))) {
                     $isEmptyValue = false;
                 }
             }
         }
     }
     // get component param
     if ($isEmptyValue && !property_exists($extensions, 'component.off')) {
         $valueName = 'component.value';
         if (property_exists($extensions, $valueName)) {
             $value = $extensions->{$valueName};
             if ($value !== null && $value !== '') {
                 $isEmptyValue = false;
             }
         } else {
             if ($option = $extensions->get('component.option', 'com_fieldsandfilters')) {
                 if (!is_null($value = JComponentHelper::getParams($option)->get($key))) {
                     $isEmptyValue = false;
                 }
             }
         }
     }
     return !$isEmptyValue ? $value : $default;
 }
 /**
  * @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);
 }
 /**
  * @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 onFieldsandfiltersRequestJSON($context, JObject $data)
 {
     if ($context != 'com_fieldsandfilters.filters.content') {
         return;
     }
     $app = JFactory::getApplication();
     $jinput = $app->input;
     $document = JFactory::getDocument();
     $basePath = sprintf('%s/%s/%s/overrides', JPATH_PLUGINS, $this->_type, $this->_name);
     $contentPath = JPATH_SITE . '/components/com_content';
     $id = $jinput->get('id', 0, 'int');
     $context = $jinput->get('context');
     if (!in_array($context, $this->_contexts)) {
         return false;
     }
     // Load PluginExtensions Helper
     $extension = FieldsandfiltersFactory::getExtensions()->getExtensionsByName($this->_name)->get($this->_name);
     if ($context == $this->_contexts['category'] && !$id || !$extension) {
         return false;
     }
     list($option, $class) = explode('.', $context);
     // set new jinput values
     $jinput->set('option', $option);
     $jinput->set('view', $class);
     $jinput->set('id', $jinput->get('id', 0, 'int'));
     // Include dependancies
     JLoader::import('com_content.helpers.route', JPATH_SITE . '/components');
     JLoader::import('com_content.helpers.query', JPATH_SITE . '/components');
     // Get controller Instance
     if (!($controller = KextensionsController::getInstance(null, 'contentController', array('base_path' => $contentPath, 'view_path' => $basePath . '/views')))) {
         return false;
     }
     // add model path
     $prefix = get_class($this);
     $controller->addModelPath($basePath . '/models', $prefix . 'Model');
     if (!($model = $controller->getModel($class, $prefix . 'Model', array('ignore_request' => false, 'table_path' => JPATH_ADMINISTRATOR . '/components/' . $option . '/tables')))) {
         return false;
     }
     $fieldsandfilters = $jinput->get('fieldsandfilters', array(), 'array');
     if (!empty($fieldsandfilters)) {
         $extensionsParams = new JObject(array('plugin.value' => $this->params->get('comparison_between_filters')));
         if ($moduleID = $jinput->get('module', 0, 'int')) {
             $extensionsParams->set('module.id', $moduleID);
         }
         $betweenFilters = FieldsandfiltersExtensionsHelper::getParams('comparison_between_filters', $extensionsParams, 'OR');
         $extensionsParams->set('plugin.value', $this->params->get('comparison_between_values_filters'));
         $betweenValues = FieldsandfiltersExtensionsHelper::getParams('comparison_between_values_filters', $extensionsParams, 'OR');
         $itemsID = FieldsandfiltersFiltersHelper::getItemsIDByFilters($extension->content_type_id, $fieldsandfilters, $model->getState('filter.published'), $betweenFilters, $betweenValues);
     } else {
         $itemsID = FieldsandfiltersFiltersHelper::getSimpleItemsID(false);
     }
     // load view
     if (!($view = $controller->getView($class, 'html', $prefix . 'View', array('base_path' => $contentPath, 'layout' => $jinput->get('layout', 'default'))))) {
         return false;
     }
     // For joomla 2.5 && Key Reference
     if (!FieldsandfiltersFactory::isVersion()) {
         $view->addTemplatePath(JPATH_THEMES . '/' . $app->getTemplate() . '/html/com_content/' . $class);
     }
     // set module to view
     $view->setModel($model, true);
     $view->document = $document;
     // add helpers
     JHtml::addIncludePath(JPATH_SITE . '/components/com_content/helpers');
     JHtml::addIncludePath(JPATH_SITE . '/components/com_content/helpers/html');
     KextensionsLanguage::load($option);
     $emptyItemsID = $itemsID->get('empty', false);
     $model->setState('fieldsandfilters.itemsID', (array) $itemsID->get('itemsID'));
     $model->setState('fieldsandfilters.emptyItemsID', $emptyItemsID);
     if ($jinput->get('random', false) && KextensionsModule::getParams($jinput->get('module'))->get('show_random', 0) && JComponentHelper::getParams('com_fieldsandfilters')->get('random_type_filters') == 'selected') {
         $model->setState('fieldsandfilters.random.selected', true);
         $model->setState('fieldsandfilters.random.limit', JComponentHelper::getParams('com_fieldsandfilters')->get('random_items_limit_filters', 0));
     }
     if (!$emptyItemsID) {
         ob_start();
         $view->display();
         $body = ob_get_contents();
         ob_end_clean();
     } else {
         $this->loadLanguage();
         $variables = new JObject();
         $variables->type = $this->_type;
         $variables->name = $this->_name;
         $variables->params = $this->params;
         $variables->extension = $extension;
         $body = KextensionsPlugin::renderLayout($variables, FieldsandfiltersPlugin::getLayout($this->params, 'empty_layout', 'empty'));
     }
     $itemsID = $model->getState('fieldsandfilters.itemsID', array());
     $fieldsID = $jinput->get('fields', array(), 'array');
     if (!empty($itemsID) && !empty($fieldsID) && !$emptyItemsID) {
         // Load Filters Helper
         $counts = (array) FieldsandfiltersFiltersHelper::getFiltersValuesCount($extension->content_type_id, $fieldsID, $itemsID, $model->getState('filter.published'));
         $data->set('counts', $counts);
     } else {
         if ($emptyItemsID) {
             // [TODO] when is empty display all fields with 0 counts or display special buttons to reset filters
             $data->set('empty', $emptyItemsID);
         }
     }
     $document->setBuffer($body, array('type' => 'component', 'name' => 'fieldsandfilters', 'title' => null));
     if (!$emptyItemsID) {
         // [TODO] move to another place because we need this only once
         $script[] = 'jQuery(document).ready(function($) {';
         $script[] = '	$("' . $this->params->get('selector_pagination_filters', '.pagination') . '").fieldsandfilters("pagination"' . ($app->get('sef', 0) ? ',{pagination: "start"}' : '') . ');';
         $script[] = '});';
         $document->addScriptDeclaration(implode("\n", $script));
     }
 }
 /**
  * @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);
 }