[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/administrator/components/com_finder/models/ -> maps.php (source)

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


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1