Source for file maps.php

Documentation is available at maps.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_finder
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('_JEXEC'or die();
  11.  
  12. /**
  13.  * Maps model for the Finder package.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_finder
  17.  * @since       2.5
  18.  */
  19. class FinderModelMaps extends JModelList
  20. {
  21.     /**
  22.      * Constructor.
  23.      *
  24.      * @param   array  $config  An associative array of configuration settings. [optional]
  25.      *
  26.      * @since   2.5
  27.      * @see     JController
  28.      */
  29.     public function __construct($config array())
  30.     {
  31.         if (empty($config['filter_fields']))
  32.         {
  33.             $config['filter_fields'array(
  34.                 'state''a.state',
  35.                 'title''a.title'
  36.             );
  37.         }
  38.  
  39.         parent::__construct($config);
  40.     }
  41.  
  42.     /**
  43.      * Method to test whether a record can be deleted.
  44.      *
  45.      * @param   object  $record  A record object.
  46.      *
  47.      * @return  boolean  True if allowed to delete the record. Defaults to the permission for the component.
  48.      *
  49.      * @since   2.5
  50.      */
  51.     protected function canDelete($record)
  52.     {
  53.         $user JFactory::getUser();
  54.         return $user->authorise('core.delete'$this->option);
  55.     }
  56.  
  57.     /**
  58.      * Method to test whether a record can be deleted.
  59.      *
  60.      * @param   object  $record  A record object.
  61.      *
  62.      * @return  boolean  True if allowed to change the state of the record. Defaults to the permission for the component.
  63.      *
  64.      * @since   2.5
  65.      */
  66.     protected function canEditState($record)
  67.     {
  68.         $user JFactory::getUser();
  69.         return $user->authorise('core.edit.state'$this->option);
  70.     }
  71.  
  72.     /**
  73.      * Method to delete one or more records.
  74.      *
  75.      * @param   array  &$pks  An array of record primary keys.
  76.      *
  77.      * @return  boolean  True if successful, false if an error occurs.
  78.      *
  79.      * @since   2.5
  80.      */
  81.     public function delete(&$pks)
  82.     {
  83.         $dispatcher JEventDispatcher::getInstance();
  84.         $pks = (array) $pks;
  85.         $table $this->getTable();
  86.  
  87.         // Include the content plugins for the on delete events.
  88.         JPluginHelper::importPlugin('content');
  89.  
  90.         // Iterate the items to delete each one.
  91.         foreach ($pks as $i => $pk)
  92.         {
  93.             if ($table->load($pk))
  94.             {
  95.                 if ($this->canDelete($table))
  96.                 {
  97.                     $context $this->option . '.' $this->name;
  98.  
  99.                     // Trigger the onContentBeforeDelete event.
  100.                     $result $dispatcher->trigger('onContentBeforeDelete'array($context$table));
  101.                     if (in_array(false$resulttrue))
  102.                     {
  103.                         $this->setError($table->getError());
  104.                         return false;
  105.                     }
  106.  
  107.                     if (!$table->delete($pk))
  108.                     {
  109.                         $this->setError($table->getError());
  110.                         return false;
  111.                     }
  112.  
  113.                     // Trigger the onContentAfterDelete event.
  114.                     $dispatcher->trigger('onContentAfterDelete'array($context$table));
  115.                 }
  116.                 else
  117.                 {
  118.                     // Prune items that you can't change.
  119.                     unset($pks[$i]);
  120.                     $error $this->getError();
  121.                     if ($error)
  122.                     {
  123.                         $this->setError($error);
  124.                     }
  125.                     else
  126.                     {
  127.                         $this->setError(JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'));
  128.                     }
  129.                 }
  130.             }
  131.             else
  132.             {
  133.                 $this->setError($table->getError());
  134.                 return false;
  135.             }
  136.         }
  137.  
  138.         // Clear the component's cache
  139.         $this->cleanCache();
  140.  
  141.         return true;
  142.     }
  143.  
  144.     /**
  145.      * Build an SQL query to load the list data.
  146.      *
  147.      * @return  JDatabaseQuery  A JDatabaseQuery object
  148.      *
  149.      * @since   2.5
  150.      */
  151.     protected function getListQuery()
  152.     {
  153.         $db $this->getDbo();
  154.         $query $db->getQuery(true);
  155.  
  156.         // Select all fields from the table.
  157.         $query->select('a.*')
  158.             ->from($db->quoteName('#__finder_taxonomy'' AS a');
  159.  
  160.         // Self-join to get children.
  161.         $query->select('COUNT(b.id) AS num_children')
  162.             ->join('LEFT'$db->quoteName('#__finder_taxonomy'' AS b ON b.parent_id=a.id');
  163.  
  164.         // Join to get the map links
  165.         $query->select('COUNT(c.node_id) AS num_nodes')
  166.             ->join('LEFT'$db->quoteName('#__finder_taxonomy_map'' AS c ON c.node_id=a.id')
  167.  
  168.             ->group('a.id, a.parent_id, a.title, a.state, a.access, a.ordering');
  169.  
  170.         // If the model is set to check item state, add to the query.
  171.         if (is_numeric($this->getState('filter.state')))
  172.         {
  173.             $query->where('a.state = ' . (int) $this->getState('filter.state'));
  174.         }
  175.  
  176.         // Filter the maps over the branch if set.
  177.         $branch_id $this->getState('filter.branch');
  178.         if (!empty($branch_id))
  179.         {
  180.             $query->where('a.parent_id = ' . (int) $branch_id);
  181.         }
  182.  
  183.         // Filter the maps over the search string if set.
  184.         $search $this->getState('filter.search');
  185.         if (!empty($search))
  186.         {
  187.             $query->where('a.title LIKE ' $db->quote('%' $search '%'));
  188.         }
  189.  
  190.         // Handle the list ordering.
  191.         $ordering $this->getState('list.ordering');
  192.         $direction $this->getState('list.direction');
  193.         if (!empty($ordering))
  194.         {
  195.             $query->order($db->escape($ordering' ' $db->escape($direction));
  196.         }
  197.  
  198.         return $query;
  199.     }
  200.  
  201.     /**
  202.      * Method to get a store id based on model configuration state.
  203.      *
  204.      * This is necessary because the model is used by the component and
  205.      * different modules that might need different sets of data or different
  206.      * ordering requirements.
  207.      *
  208.      * @param   string  $id  A prefix for the store id. [optional]
  209.      *
  210.      * @return  string  A store id.
  211.      *
  212.      * @since   2.5
  213.      */
  214.     protected function getStoreId($id '')
  215.     {
  216.         // Compile the store id.
  217.         $id .= ':' $this->getState('filter.state');
  218.         $id .= ':' $this->getState('filter.search');
  219.         $id .= ':' $this->getState('filter.branch');
  220.  
  221.         return parent::getStoreId($id);
  222.     }
  223.  
  224.     /**
  225.      * Returns a JTable object, always creating it.
  226.      *
  227.      * @param   string  $type    The table type to instantiate. [optional]
  228.      * @param   string  $prefix  A prefix for the table class name. [optional]
  229.      * @param   array   $config  Configuration array for model. [optional]
  230.      *
  231.      * @return  JTable  A database object
  232.      *
  233.      * @since   2.5
  234.      */
  235.     public function getTable($type 'Map'$prefix 'FinderTable'$config array())
  236.     {
  237.         return JTable::getInstance($type$prefix$config);
  238.     }
  239.  
  240.     /**
  241.      * Method to auto-populate the model state.  Calling getState in this method will result in recursion.
  242.      *
  243.      * @param   string  $ordering   An optional ordering field. [optional]
  244.      * @param   string  $direction  An optional direction. [optional]
  245.      *
  246.      * @return  void 
  247.      *
  248.      * @since   2.5
  249.      */
  250.     protected function populateState($ordering null$direction null)
  251.     {
  252.         // Load the filter state.
  253.         $search $this->getUserStateFromRequest($this->context . '.filter.search''filter_search');
  254.         $this->setState('filter.search'$search);
  255.  
  256.         $state $this->getUserStateFromRequest($this->context . '.filter.state''filter_state''''string');
  257.         $this->setState('filter.state'$state);
  258.  
  259.         $branch $this->getUserStateFromRequest($this->context . '.filter.branch''filter_branch''1''string');
  260.         $this->setState('filter.branch'$branch);
  261.  
  262.         // Load the parameters.
  263.         $params JComponentHelper::getParams('com_finder');
  264.         $this->setState('params'$params);
  265.  
  266.         // List state information.
  267.         parent::populateState('a.title''asc');
  268.     }
  269.  
  270.     /**
  271.      * Method to change the published state of one or more records.
  272.      *
  273.      * @param   array    &$pks   A list of the primary keys to change.
  274.      * @param   integer  $value  The value of the published state. [optional]
  275.      *
  276.      * @return  boolean  True on success.
  277.      *
  278.      * @since   2.5
  279.      */
  280.     public function publish(&$pks$value 1)
  281.     {
  282.         $dispatcher JEventDispatcher::getInstance();
  283.         $user JFactory::getUser();
  284.         $table $this->getTable();
  285.         $pks = (array) $pks;
  286.  
  287.         // Include the content plugins for the change of state event.
  288.         JPluginHelper::importPlugin('content');
  289.  
  290.         // Access checks.
  291.         foreach ($pks as $i => $pk)
  292.         {
  293.             $table->reset();
  294.  
  295.             if ($table->load($pk))
  296.             {
  297.                 if (!$this->canEditState($table))
  298.                 {
  299.                     // Prune items that you can't change.
  300.                     unset($pks[$i]);
  301.                     $this->setError(JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'));
  302.                     return false;
  303.                 }
  304.             }
  305.         }
  306.  
  307.         // Attempt to change the state of the records.
  308.         if (!$table->publish($pks$value$user->get('id')))
  309.         {
  310.             $this->setError($table->getError());
  311.             return false;
  312.         }
  313.  
  314.         $context $this->option . '.' $this->name;
  315.  
  316.         // Trigger the onContentChangeState event.
  317.         $result $dispatcher->trigger('onContentChangeState'array($context$pks$value));
  318.  
  319.         if (in_array(false$resulttrue))
  320.         {
  321.             $this->setError($table->getError());
  322.             return false;
  323.         }
  324.  
  325.         // Clear the component's cache
  326.         $this->cleanCache();
  327.  
  328.         return true;
  329.     }
  330.  
  331.     /**
  332.      * Method to purge all maps from the taxonomy.
  333.      *
  334.      * @return  boolean  Returns true on success, false on failure.
  335.      *
  336.      * @since   2.5
  337.      */
  338.     public function purge()
  339.     {
  340.         $db $this->getDbo();
  341.         $query $db->getQuery(true)
  342.             ->delete($db->quoteName('#__finder_taxonomy'))
  343.             ->where($db->quoteName('parent_id'' > 1');
  344.         $db->setQuery($query);
  345.         $db->execute();
  346.  
  347.         $query->clear()
  348.             ->delete($db->quoteName('#__finder_taxonomy_map'))
  349.             ->where('1');
  350.         $db->setQuery($query);
  351.         $db->execute();
  352.  
  353.         return true;
  354.     }
  355. }

Documentation generated on Tue, 19 Nov 2013 15:07:39 +0100 by phpDocumentor 1.4.3