Example #1
0
 /**
  * Method to get a list of tags
  *
  * @return  array  The field option objects.
  *
  * @since   3.1
  */
 protected function getOptions()
 {
     $options = array();
     $published = $this->element['published'] ? $this->element['published'] : array(0, 1);
     $name = (string) $this->element['name'];
     $db = FOFPlatform::getInstance()->getDbo();
     $query = $db->getQuery(true)->select('a.id AS value, a.path, a.title AS text, a.level, a.published')->from('#__tags AS a')->join('LEFT', $db->quoteName('#__tags') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt');
     $item = $this->form->getModel()->getItem();
     if ($item instanceof FOFTable) {
         // Fake value for selected tags
         $keyfield = $item->getKeyName();
         $content_id = $item->{$keyfield};
         $type = $item->getContentType();
         $selected_query = $db->getQuery(true);
         $selected_query->select('tag_id')->from('#__contentitem_tag_map')->where('content_item_id = ' . (int) $content_id)->where('type_alias = ' . $db->quote($type));
         $db->setQuery($selected_query);
         $this->value = $db->loadColumn();
     }
     // Ajax tag only loads assigned values
     if (!$this->isNested()) {
         // Only item assigned values
         $values = (array) $this->value;
         FOFUtilsArray::toInteger($values);
         $query->where('a.id IN (' . implode(',', $values) . ')');
     }
     // Filter language
     if (!empty($this->element['language'])) {
         $query->where('a.language = ' . $db->quote($this->element['language']));
     }
     $query->where($db->quoteName('a.alias') . ' <> ' . $db->quote('root'));
     // Filter to only load active items
     // Filter on the published state
     if (is_numeric($published)) {
         $query->where('a.published = ' . (int) $published);
     } elseif (is_array($published)) {
         FOFUtilsArray::toInteger($published);
         $query->where('a.published IN (' . implode(',', $published) . ')');
     }
     $query->group('a.id, a.title, a.level, a.lft, a.rgt, a.parent_id, a.published, a.path')->order('a.lft ASC');
     // Get the options.
     $db->setQuery($query);
     try {
         $options = $db->loadObjectList();
     } catch (RuntimeException $e) {
         return false;
     }
     // Prepare nested data
     if ($this->isNested()) {
         $this->prepareOptionsNested($options);
     } else {
         $options = JHelperTags::convertPathsToNames($options);
     }
     return $options;
 }
Example #2
0
 /**
  * Utility function to sort an array of objects on a given field
  *
  * @param   array  &$a             An array of objects
  * @param   mixed  $k              The key (string) or a array of key to sort on
  * @param   mixed  $direction      Direction (integer) or an array of direction to sort in [1 = Ascending] [-1 = Descending]
  * @param   mixed  $caseSensitive  Boolean or array of booleans to let sort occur case sensitive or insensitive
  * @param   mixed  $locale         Boolean or array of booleans to let sort occur using the locale language or not
  *
  * @return  array  The sorted array of objects
  */
 public static function sortObjects(&$a, $k, $direction = 1, $caseSensitive = true, $locale = false)
 {
     if (!is_array($locale) || !is_array($locale[0])) {
         $locale = array($locale);
     }
     self::$sortCase = (array) $caseSensitive;
     self::$sortDirection = (array) $direction;
     self::$sortKey = (array) $k;
     self::$sortLocale = $locale;
     usort($a, array(__CLASS__, '_sortObjects'));
     self::$sortCase = null;
     self::$sortDirection = null;
     self::$sortKey = null;
     self::$sortLocale = null;
     return $a;
 }
Example #3
0
 /**
  * Publish or unpublish records
  *
  * @param   integer|array  $cid      The primary key value(s) of the item(s) to publish/unpublish
  * @param   integer        $publish  1 to publish an item, 0 to unpublish
  * @param   integer        $user_id  The user ID of the user (un)publishing the item.
  *
  * @return  boolean  True on success, false on failure (e.g. record is locked)
  */
 public function publish($cid = null, $publish = 1, $user_id = 0)
 {
     $enabledName = $this->getColumnAlias('enabled');
     $locked_byName = $this->getColumnAlias('locked_by');
     // Mhm... you called the publish method on a table without publish support...
     if (!in_array($enabledName, $this->getKnownFields())) {
         return false;
     }
     //We have to cast the id as array, or the helper function will return an empty set
     if ($cid) {
         $cid = (array) $cid;
     }
     FOFUtilsArray::toInteger($cid);
     $user_id = (int) $user_id;
     $publish = (int) $publish;
     $k = $this->_tbl_key;
     if (count($cid) < 1) {
         if ($this->{$k}) {
             $cid = array($this->{$k});
         } else {
             $this->setError("No items selected.");
             return false;
         }
     }
     if (!$this->onBeforePublish($cid, $publish)) {
         return false;
     }
     $query = $this->_db->getQuery(true)->update($this->_db->qn($this->_tbl))->set($this->_db->qn($enabledName) . ' = ' . (int) $publish);
     $checkin = in_array($locked_byName, $this->getKnownFields());
     if ($checkin) {
         $query->where(' (' . $this->_db->qn($locked_byName) . ' = 0 OR ' . $this->_db->qn($locked_byName) . ' = ' . (int) $user_id . ')', 'AND');
     }
     // TODO Rewrite this statment using IN. Check if it work in SQLServer and PostgreSQL
     $cids = $this->_db->qn($k) . ' = ' . implode(' OR ' . $this->_db->qn($k) . ' = ', $cid);
     $query->where('(' . $cids . ')');
     $this->_db->setQuery((string) $query);
     if (version_compare(JVERSION, '3.0', 'ge')) {
         try {
             $this->_db->execute();
         } catch (JDatabaseException $e) {
             $this->setError($e->getMessage());
         }
     } else {
         if (!$this->_db->execute()) {
             $this->setError($this->_db->getErrorMsg());
             return false;
         }
     }
     if (count($cid) == 1 && $checkin) {
         if ($this->_db->getAffectedRows() == 1) {
             $this->checkin($cid[0]);
             if ($this->{$k} == $cid[0]) {
                 $this->{$enabledName} = $publish;
             }
         }
     }
     $this->setError('');
     return true;
 }
 /**
  * Makes a copy of the record, inserting it as the last child of the given node's parent.
  *
  * @param   integer|array  $cid  The primary key value (or values) or the record(s) to copy. 
  *                               If null, the current record will be copied
  * 
  * @return self|FOFTableNested	 The last copied node
  */
 public function copy($cid = null)
 {
     //We have to cast the id as array, or the helper function will return an empty set
     if ($cid) {
         $cid = (array) $cid;
     }
     FOFUtilsArray::toInteger($cid);
     $k = $this->_tbl_key;
     if (count($cid) < 1) {
         if ($this->{$k}) {
             $cid = array($this->{$k});
         } else {
             // Even if it's null, let's still create the record
             $this->create($this->getData());
             return $this;
         }
     }
     foreach ($cid as $item) {
         // Prevent load with id = 0
         if (!$item) {
             continue;
         }
         $this->load($item);
         $this->create($this->getData());
     }
     return $this;
 }
Example #5
0
 /**
  * Automatically detects all views of the component
  *
  * @return  array  A list of all views, in the order to be displayed in the toolbar submenu
  */
 protected function getMyViews()
 {
     $views = array();
     $t_views = array();
     $using_meta = false;
     $componentPaths = FOFPlatform::getInstance()->getComponentBaseDirs($this->component);
     $searchPath = $componentPaths['main'] . '/views';
     $filesystem = FOFPlatform::getInstance()->getIntegrationObject('filesystem');
     $allFolders = $filesystem->folderFolders($searchPath);
     if (!empty($allFolders)) {
         foreach ($allFolders as $folder) {
             $view = $folder;
             // View already added
             if (in_array(FOFInflector::pluralize($view), $t_views)) {
                 continue;
             }
             // Do we have a 'skip.xml' file in there?
             $files = $filesystem->folderFiles($searchPath . '/' . $view, '^skip\\.xml$');
             if (!empty($files)) {
                 continue;
             }
             // Do we have extra information about this view? (ie. ordering)
             $meta = $filesystem->folderFiles($searchPath . '/' . $view, '^metadata\\.xml$');
             // Not found, do we have it inside the plural one?
             if (!$meta) {
                 $plural = FOFInflector::pluralize($view);
                 if (in_array($plural, $allFolders)) {
                     $view = $plural;
                     $meta = $filesystem->folderFiles($searchPath . '/' . $view, '^metadata\\.xml$');
                 }
             }
             if (!empty($meta)) {
                 $using_meta = true;
                 $xml = simplexml_load_file($searchPath . '/' . $view . '/' . $meta[0]);
                 $order = (int) $xml->foflib->ordering;
             } else {
                 // Next place. It's ok since the index are 0-based and count is 1-based
                 if (!isset($to_order)) {
                     $to_order = array();
                 }
                 $order = count($to_order);
             }
             $view = FOFInflector::pluralize($view);
             $t_view = new stdClass();
             $t_view->ordering = $order;
             $t_view->view = $view;
             $to_order[] = $t_view;
             $t_views[] = $view;
         }
     }
     FOFUtilsArray::sortObjects($to_order, 'ordering');
     $views = FOFUtilsArray::getColumn($to_order, 'view');
     // If not using the metadata file, let's put the cpanel view on top
     if (!$using_meta) {
         $cpanel = array_search('cpanels', $views);
         if ($cpanel !== false) {
             unset($views[$cpanel]);
             array_unshift($views, 'cpanels');
         }
     }
     return $views;
 }
 /**
  * Method to get the field options.
  *
  * Ordering is disabled by default. You can enable ordering by setting the
  * 'order' element in your form field. The other order values are optional.
  *
  * - order					What to order.			Possible values: 'name' or 'value' (default = false)
  * - order_dir				Order direction.		Possible values: 'asc' = Ascending or 'desc' = Descending (default = 'asc')
  * - order_case_sensitive	Order case sensitive.	Possible values: 'true' or 'false' (default = false)
  *
  * @return  array  The field option objects.
  *
  * @since	Ordering is available since FOF 2.1.b2.
  */
 protected function getOptions()
 {
     // Ordering is disabled by default for backward compatibility
     $order = false;
     // Set default order direction
     $order_dir = 'asc';
     // Set default value for case sensitive sorting
     $order_case_sensitive = false;
     if ($this->element['order'] && $this->element['order'] !== 'false') {
         $order = $this->element['order'];
     }
     if ($this->element['order_dir']) {
         $order_dir = $this->element['order_dir'];
     }
     if ($this->element['order_case_sensitive']) {
         // Override default setting when the form element value is 'true'
         if ($this->element['order_case_sensitive'] == 'true') {
             $order_case_sensitive = true;
         }
     }
     // Create a $sortOptions array in order to apply sorting
     $i = 0;
     $sortOptions = array();
     foreach ($this->element->children() as $option) {
         $name = JText::alt(trim((string) $option), preg_replace('/[^a-zA-Z0-9_\\-]/', '_', $this->fieldname));
         $sortOptions[$i] = new stdClass();
         $sortOptions[$i]->option = $option;
         $sortOptions[$i]->value = $option['value'];
         $sortOptions[$i]->name = $name;
         $i++;
     }
     // Only order if it's set
     if ($order) {
         jimport('joomla.utilities.arrayhelper');
         FOFUtilsArray::sortObjects($sortOptions, $order, $order_dir == 'asc' ? 1 : -1, $order_case_sensitive, false);
     }
     // Initialise the options
     $options = array();
     // Get the field $options
     foreach ($sortOptions as $sortOption) {
         $option = $sortOption->option;
         $name = $sortOption->name;
         // Only add <option /> elements.
         if ($option->getName() != 'option') {
             continue;
         }
         $tmp = JHtml::_('select.option', (string) $option['value'], $name, 'value', 'text', (string) $option['disabled'] == 'true');
         // Set some option attributes.
         $tmp->class = (string) $option['class'];
         // Set some JavaScript option attributes.
         $tmp->onclick = (string) $option['onclick'];
         // Add the option object to the result set.
         $options[] = $tmp;
     }
     // Do we have a class and method source for our options?
     $source_file = empty($this->element['source_file']) ? '' : (string) $this->element['source_file'];
     $source_class = empty($this->element['source_class']) ? '' : (string) $this->element['source_class'];
     $source_method = empty($this->element['source_method']) ? '' : (string) $this->element['source_method'];
     $source_key = empty($this->element['source_key']) ? '*' : (string) $this->element['source_key'];
     $source_value = empty($this->element['source_value']) ? '*' : (string) $this->element['source_value'];
     $source_translate = empty($this->element['source_translate']) ? 'true' : (string) $this->element['source_translate'];
     $source_translate = in_array(strtolower($source_translate), array('true', 'yes', '1', 'on')) ? true : false;
     $source_format = empty($this->element['source_format']) ? '' : (string) $this->element['source_format'];
     if ($source_class && $source_method) {
         // Maybe we have to load a file?
         if (!empty($source_file)) {
             $source_file = FOFTemplateUtils::parsePath($source_file, true);
             if (FOFPlatform::getInstance()->getIntegrationObject('filesystem')->fileExists($source_file)) {
                 include_once $source_file;
             }
         }
         // Make sure the class exists
         if (class_exists($source_class, true)) {
             // ...and so does the option
             if (in_array($source_method, get_class_methods($source_class))) {
                 // Get the data from the class
                 if ($source_format == 'optionsobject') {
                     $options = array_merge($options, $source_class::$source_method());
                 } else {
                     // Get the data from the class
                     $source_data = $source_class::$source_method();
                     // Loop through the data and prime the $options array
                     foreach ($source_data as $k => $v) {
                         $key = empty($source_key) || $source_key == '*' ? $k : $v[$source_key];
                         $value = empty($source_value) || $source_value == '*' ? $v : $v[$source_value];
                         if ($source_translate) {
                             $value = JText::_($value);
                         }
                         $options[] = JHtml::_('select.option', $key, $value, 'value', 'text');
                     }
                 }
             }
         }
     }
     reset($options);
     return $options;
 }