[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/administrator/components/com_finder/models/ -> index.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   * Index model class for Finder.
  16   *
  17   * @package     Joomla.Administrator
  18   * @subpackage  com_finder
  19   * @since       2.5
  20   */
  21  class FinderModelIndex extends JModelList
  22  {
  23      /**
  24       * The event to trigger after deleting the data.
  25       *
  26       * @var    string
  27       * @since  2.5
  28       */
  29      protected $event_after_delete = 'onContentAfterDelete';
  30  
  31      /**
  32       * The event to trigger before deleting the data.
  33       *
  34       * @var    string
  35       * @since  2.5
  36       */
  37      protected $event_before_delete = 'onContentBeforeDelete';
  38  
  39      /**
  40       * Constructor.
  41       *
  42       * @param   array  $config  An associative array of configuration settings. [optional]
  43       *
  44       * @since   2.5
  45       * @see     JController
  46       */
  47  	public function __construct($config = array())
  48      {
  49          if (empty($config['filter_fields']))
  50          {
  51              $config['filter_fields'] = array(
  52                  'published', 'l.published',
  53                  'title', 'l.title',
  54                  'type_id', 'l.type_id',
  55                  'url', 'l.url',
  56                  'indexdate', 'l.indexdate'
  57              );
  58          }
  59  
  60          parent::__construct($config);
  61      }
  62  
  63      /**
  64       * Method to test whether a record can be deleted.
  65       *
  66       * @param   object  $record  A record object.
  67       *
  68       * @return  boolean  True if allowed to delete the record. Defaults to the permission for the component.
  69       *
  70       * @since   2.5
  71       */
  72  	protected function canDelete($record)
  73      {
  74          $user = JFactory::getUser();
  75          return $user->authorise('core.delete', $this->option);
  76      }
  77  
  78      /**
  79       * Method to test whether a record can be deleted.
  80       *
  81       * @param   object  $record  A record object.
  82       *
  83       * @return  boolean  True if allowed to change the state of the record. Defaults to the permission for the component.
  84       *
  85       * @since   2.5
  86       */
  87  	protected function canEditState($record)
  88      {
  89          $user = JFactory::getUser();
  90          return $user->authorise('core.edit.state', $this->option);
  91      }
  92  
  93      /**
  94       * Method to delete one or more records.
  95       *
  96       * @param   array  &$pks  An array of record primary keys.
  97       *
  98       * @return  boolean  True if successful, false if an error occurs.
  99       *
 100       * @since   2.5
 101       */
 102  	public function delete(&$pks)
 103      {
 104          // Initialise variables.
 105          $dispatcher = JDispatcher::getInstance();
 106          $user = JFactory::getUser();
 107          $pks = (array) $pks;
 108          $table = $this->getTable();
 109  
 110          // Include the content and finder plugins for the on delete events.
 111          JPluginHelper::importPlugin('content');
 112          JPluginHelper::importPlugin('finder');
 113  
 114          // Iterate the items to delete each one.
 115          foreach ($pks as $i => $pk)
 116          {
 117              if ($table->load($pk))
 118              {
 119                  if ($this->canDelete($table))
 120                  {
 121                      $context = $this->option . '.' . $this->name;
 122  
 123                      // Trigger the onContentBeforeDelete event.
 124                      $result = $dispatcher->trigger($this->event_before_delete, array($context, $table));
 125                      if (in_array(false, $result, true))
 126                      {
 127                          $this->setError($table->getError());
 128                          return false;
 129                      }
 130  
 131                      if (!$table->delete($pk))
 132                      {
 133                          $this->setError($table->getError());
 134                          return false;
 135                      }
 136  
 137                      // Trigger the onContentAfterDelete event.
 138                      $dispatcher->trigger($this->event_after_delete, array($context, $table));
 139                  }
 140                  else
 141                  {
 142                      // Prune items that you can't change.
 143                      unset($pks[$i]);
 144                      $error = $this->getError();
 145                      if ($error)
 146                      {
 147                          $this->setError($error);
 148                      }
 149                      else
 150                      {
 151                          $this->setError(JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'));
 152                      }
 153                  }
 154              }
 155              else
 156              {
 157                  $this->setError($table->getError());
 158                  return false;
 159              }
 160          }
 161  
 162          // Clear the component's cache
 163          $this->cleanCache();
 164  
 165          return true;
 166      }
 167  
 168      /**
 169       * Build an SQL query to load the list data.
 170       *
 171       * @return  JDatabaseQuery  A JDatabaseQuery object
 172       *
 173       * @since   2.5
 174       */
 175  	protected function getListQuery()
 176      {
 177          $db = $this->getDbo();
 178          $query = $db->getQuery(true);
 179  
 180          $query->select('l.*');
 181          $query->select('t.title AS t_title');
 182          $query->from($db->quoteName('#__finder_links') . ' AS l');
 183          $query->join('INNER', $db->quoteName('#__finder_types') . ' AS t ON t.id = l.type_id');
 184  
 185          // Check the type filter.
 186          if ($this->getState('filter.type'))
 187          {
 188              $query->where($db->quoteName('l.type_id') . ' = ' . (int) $this->getState('filter.type'));
 189          }
 190  
 191          // Check for state filter.
 192          if (is_numeric($this->getState('filter.state')))
 193          {
 194              $query->where($db->quoteName('l.published') . ' = ' . (int) $this->getState('filter.state'));
 195          }
 196  
 197          // Check the search phrase.
 198          if ($this->getState('filter.search') != '')
 199          {
 200              $search = $db->escape($this->getState('filter.search'));
 201              $query->where($db->quoteName('l.title') . ' LIKE "%' . $db->escape($search) . '%"' . ' OR ' . $db->quoteName('l.url') . ' LIKE "%' . $db->escape($search) . '%"' . ' OR ' . $db->quoteName('l.indexdate') . ' LIKE "%' . $db->escape($search) . '%"');
 202          }
 203  
 204          // Handle the list ordering.
 205          $ordering = $this->getState('list.ordering');
 206          $direction = $this->getState('list.direction');
 207          if (!empty($ordering))
 208          {
 209              $query->order($db->escape($ordering) . ' ' . $db->escape($direction));
 210          }
 211  
 212          return $query;
 213      }
 214  
 215       /**
 216       * Method to get the state of the Smart Search plug-ins.
 217       *
 218       * @return  array   Array of relevant plug-ins and whether they are enabled or not.
 219       *
 220       * @since   2.5
 221       */
 222  	public function getPluginState()
 223      {
 224          $db = $this->getDbo();
 225          $query = $db->getQuery(true);
 226  
 227          $query->select('name, enabled');
 228          $query->from($db->quoteName('#__extensions'));
 229          $query->where($db->quoteName('type') . ' = ' .  $db->quote('plugin'));
 230          $query->where($db->quoteName('folder') . ' IN(' .  $db->quote('system') . ',' . $db->quote('content') . ')');
 231          $query->where($db->quoteName('element') . ' = ' .  $db->quote('finder'));
 232          $db->setQuery($query);
 233          $db->query();
 234          $plugins = $db->loadObjectList('name');
 235  
 236          return $plugins;
 237      }
 238  
 239      /**
 240       * Method to get a store id based on model configuration state.
 241       *
 242       * This is necessary because the model is used by the component and
 243       * different modules that might need different sets of data or different
 244       * ordering requirements.
 245       *
 246       * @param   string  $id  A prefix for the store id. [optional]
 247       *
 248       * @return  string  A store id.
 249       *
 250       * @since   2.5
 251       */
 252  	protected function getStoreId($id = '')
 253      {
 254          // Compile the store id.
 255          $id .= ':' . $this->getState('filter.search');
 256          $id .= ':' . $this->getState('filter.state');
 257          $id .= ':' . $this->getState('filter.type');
 258  
 259          return parent::getStoreId($id);
 260      }
 261  
 262      /**
 263       * Returns a JTable object, always creating it.
 264       *
 265       * @param   string  $type    The table type to instantiate. [optional]
 266       * @param   string  $prefix  A prefix for the table class name. [optional]
 267       * @param   array   $config  Configuration array for model. [optional]
 268       *
 269       * @return  JTable  A database object
 270       *
 271       * @since   2.5
 272       */
 273  	public function getTable($type = 'Link', $prefix = 'FinderTable', $config = array())
 274      {
 275          return JTable::getInstance($type, $prefix, $config);
 276      }
 277  
 278      /**
 279       * Method to purge the index, deleting all links.
 280       *
 281       * @return  boolean  True on success, false on failure.
 282       *
 283       * @since   2.5
 284       */
 285  	public function purge()
 286      {
 287          $db = $this->getDbo();
 288  
 289          // Truncate the links table.
 290          $db->truncateTable('#__finder_links');
 291  
 292          // Check for a database error.
 293          if ($db->getErrorNum())
 294          {
 295              // Throw database error exception.
 296              throw new Exception($db->getErrorMsg(), 500);
 297          }
 298  
 299          // Truncate the links terms tables.
 300          for ($i = 0; $i <= 15; $i++)
 301          {
 302              // Get the mapping table suffix.
 303              $suffix = dechex($i);
 304  
 305              $db->truncateTable('#__finder_links_terms' . $suffix);
 306  
 307              // Check for a database error.
 308              if ($db->getErrorNum())
 309              {
 310                  // Throw database error exception.
 311                  throw new Exception($db->getErrorMsg(), 500);
 312              }
 313          }
 314  
 315          // Truncate the terms table.
 316          $db->truncateTable('#__finder_terms');
 317  
 318          // Check for a database error.
 319          if ($db->getErrorNum())
 320          {
 321              // Throw database error exception.
 322              throw new Exception($db->getErrorMsg(), 500);
 323          }
 324  
 325          // Truncate the taxonomy map table.
 326          $db->truncateTable('#__finder_taxonomy_map');
 327  
 328          // Check for a database error.
 329          if ($db->getErrorNum())
 330          {
 331              // Throw database error exception.
 332              throw new Exception($db->getErrorMsg(), 500);
 333          }
 334  
 335          // Delete all the taxonomy nodes except the root.
 336          $query = $db->getQuery(true);
 337          $query->delete();
 338          $query->from($db->quoteName('#__finder_taxonomy'));
 339          $query->where($db->quoteName('id') . ' > 1');
 340          $db->setQuery($query);
 341          $db->query();
 342  
 343          // Check for a database error.
 344          if ($db->getErrorNum())
 345          {
 346              // Throw database error exception.
 347              throw new Exception($db->getErrorMsg(), 500);
 348          }
 349  
 350          // Truncate the tokens tables.
 351          $db->truncateTable('#__finder_tokens');
 352  
 353          // Check for a database error.
 354          if ($db->getErrorNum())
 355          {
 356              // Throw database error exception.
 357              throw new Exception($db->getErrorMsg(), 500);
 358          }
 359  
 360          // Truncate the tokens aggregate table.
 361          $db->truncateTable('#__finder_tokens_aggregate');
 362  
 363          // Check for a database error.
 364          if ($db->getErrorNum())
 365          {
 366              // Throw database error exception.
 367              throw new Exception($db->getErrorMsg(), 500);
 368          }
 369  
 370          return true;
 371      }
 372  
 373      /**
 374       * Method to auto-populate the model state.  Calling getState in this method will result in recursion.
 375       *
 376       * @param   string  $ordering   An optional ordering field. [optional]
 377       * @param   string  $direction  An optional direction. [optional]
 378       *
 379       * @return  void
 380       *
 381       * @since   2.5
 382       */
 383  	protected function populateState($ordering = null, $direction = null)
 384      {
 385          // Load the filter state.
 386          $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
 387          $this->setState('filter.search', $search);
 388  
 389          $state = $this->getUserStateFromRequest($this->context . '.filter.state', 'filter_state', '', 'string');
 390          $this->setState('filter.state', $state);
 391  
 392          $type = $this->getUserStateFromRequest($this->context . '.filter.type', 'filter_type', '', 'string');
 393          $this->setState('filter.type', $type);
 394  
 395          // Load the parameters.
 396          $params = JComponentHelper::getParams('com_finder');
 397          $this->setState('params', $params);
 398  
 399          // List state information.
 400          parent::populateState('l.title', 'asc');
 401      }
 402  
 403      /**
 404       * Method to change the published state of one or more records.
 405       *
 406       * @param   array    &$pks   A list of the primary keys to change.
 407       * @param   integer  $value  The value of the published state. [optional]
 408       *
 409       * @return  boolean  True on success.
 410       *
 411       * @since   2.5
 412       */
 413  	public function publish(&$pks, $value = 1)
 414      {
 415          // Initialise variables.
 416          $dispatcher = JDispatcher::getInstance();
 417          $user = JFactory::getUser();
 418          $table = $this->getTable();
 419          $pks = (array) $pks;
 420  
 421          // Include the content plugins for the change of state event.
 422          JPluginHelper::importPlugin('content');
 423  
 424          // Access checks.
 425          foreach ($pks as $i => $pk)
 426          {
 427              $table->reset();
 428  
 429              if ($table->load($pk))
 430              {
 431                  if (!$this->canEditState($table))
 432                  {
 433                      // Prune items that you can't change.
 434                      unset($pks[$i]);
 435                      $this->setError(JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'));
 436                      return false;
 437                  }
 438              }
 439          }
 440  
 441          // Attempt to change the state of the records.
 442          if (!$table->publish($pks, $value, $user->get('id')))
 443          {
 444              $this->setError($table->getError());
 445              return false;
 446          }
 447  
 448          $context = $this->option . '.' . $this->name;
 449  
 450          // Trigger the onContentChangeState event.
 451          $result = $dispatcher->trigger('onContentChangeState', array($context, $pks, $value));
 452  
 453          if (in_array(false, $result, true))
 454          {
 455              $this->setError($table->getError());
 456              return false;
 457          }
 458  
 459          // Clear the component's cache
 460          $this->cleanCache();
 461  
 462          return true;
 463      }
 464  }


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