Source for file index.php

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

Documentation generated on Tue, 19 Nov 2013 15:05:22 +0100 by phpDocumentor 1.4.3