Source for file history.php

Documentation is available at history.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_contenthistory
  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.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Methods supporting a list of contenthistory records.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_contenthistory
  17.  * @since       3.2
  18.  */
  19. {
  20.     /**
  21.      * Constructor.
  22.      *
  23.      * @param   array  $config  An optional associative array of configuration settings.
  24.      *
  25.      * @see     JControllerLegacy
  26.      * @since   3.2
  27.      */
  28.     public function __construct($config array())
  29.     {
  30.         if (empty($config['filter_fields']))
  31.         {
  32.             $config['filter_fields'array(
  33.                     'version_id''h.version_id',
  34.                     'version_note''h.version_note',
  35.                     'save_date''h.save_date',
  36.                     'editor_user_id''h.editor_user_id',
  37.             );
  38.         }
  39.  
  40.         parent::__construct($config);
  41.     }
  42.  
  43.     /**
  44.      * Method to test whether a history record can be deleted. Note that we check whether we have edit permissions
  45.      * for the content item row.
  46.      *
  47.      * @param   object  $record  A JTable object.
  48.      *
  49.      * @return  boolean  True if allowed to delete the record. Defaults to the permission set in the component.
  50.      *
  51.      * @since   3.2
  52.      */
  53.     protected function canEdit($record)
  54.     {
  55.         if (!empty($record->ucm_type_id))
  56.         {
  57.             $result false;
  58.  
  59.             // Check that the type id matches the type alias
  60.             $typeAlias JFactory::getApplication()->input->get('type_alias');
  61.             $contentTypeTable JTable::getInstance('Contenttype''JTable');
  62.  
  63.             if ($contentTypeTable->getTypeId($typeAlias== $record->ucm_type_id)
  64.             {
  65.                 /**
  66.                  * Make sure user has edit privileges for this content item. Note that we use edit permissions
  67.                  * for the content item, not delete permissions for the content history row.
  68.                  */
  69.                 $user JFactory::getUser();
  70.                 $result $user->authorise('core.edit'$typeAlias . (int) $record->version_id);
  71.             }
  72.         }
  73.  
  74.         return $result;
  75.     }
  76.  
  77.     /**
  78.      * Method to delete one or more records from content history table.
  79.      *
  80.      * @param   array  &$pks  An array of record primary keys.
  81.      *
  82.      * @return  boolean  True if successful, false if an error occurs.
  83.      *
  84.      * @since   3.2
  85.      */
  86.     public function delete(&$pks)
  87.     {
  88.         $pks = (array) $pks;
  89.         $table $this->getTable();
  90.  
  91.         // Iterate the items to delete each one.
  92.         foreach ($pks as $i => $pk)
  93.         {
  94.             if ($table->load($pk))
  95.             {
  96.                 if ($this->canEdit($table))
  97.                 {
  98.                     if (!$table->delete($pk))
  99.                     {
  100.                         $this->setError($table->getError());
  101.  
  102.                         return false;
  103.                     }
  104.                 }
  105.                 else
  106.                 {
  107.                     // Prune items that you can't change.
  108.                     unset($pks[$i]);
  109.                     $error $this->getError();
  110.  
  111.                     if ($error)
  112.                     {
  113.                         JLog::add($errorJLog::WARNING'jerror');
  114.  
  115.                         return false;
  116.                     }
  117.                     else
  118.                     {
  119.                         JLog::add(JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED')JLog::WARNING'jerror');
  120.  
  121.                         return false;
  122.                     }
  123.                 }
  124.             }
  125.             else
  126.             {
  127.                 $this->setError($table->getError());
  128.  
  129.                 return false;
  130.             }
  131.         }
  132.  
  133.         // Clear the component's cache
  134.         $this->cleanCache();
  135.  
  136.         return true;
  137.     }
  138.  
  139.     /**
  140.      * Method to get a table object, load it if necessary.
  141.      *
  142.      * @param   string  $type    The table name. Optional.
  143.      * @param   string  $prefix  The class prefix. Optional.
  144.      * @param   array   $config  Configuration array for model. Optional.
  145.      *
  146.      * @return  JTable  A JTable object
  147.      *
  148.      * @since   3.2
  149.      */
  150.     public function getTable($type 'Contenthistory'$prefix 'JTable'$config array())
  151.     {
  152.         return JTable::getInstance($type$prefix$config);
  153.     }
  154.  
  155.     /**
  156.      * Method to toggle on and off the keep forever value for one or more records from content history table.
  157.      *
  158.      * @param   array  &$pks  An array of record primary keys.
  159.      *
  160.      * @return  boolean  True if successful, false if an error occurs.
  161.      *
  162.      * @since   3.2
  163.      */
  164.     public function keep(&$pks)
  165.     {
  166.         $pks = (array) $pks;
  167.         $table $this->getTable();
  168.  
  169.         // Iterate the items to delete each one.
  170.         foreach ($pks as $i => $pk)
  171.         {
  172.             if ($table->load($pk))
  173.             {
  174.                 if ($this->canEdit($table))
  175.                 {
  176.                     $table->keep_forever $table->keep_forever 1;
  177.  
  178.                     if (!$table->store())
  179.                     {
  180.                         $this->setError($table->getError());
  181.  
  182.                         return false;
  183.                     }
  184.                 }
  185.                 else
  186.                 {
  187.                     // Prune items that you can't change.
  188.                     unset($pks[$i]);
  189.                     $error $this->getError();
  190.  
  191.                     if ($error)
  192.                     {
  193.                         JLog::add($errorJLog::WARNING'jerror');
  194.  
  195.                         return false;
  196.                     }
  197.                     else
  198.                     {
  199.                         JLog::add(JText::_('COM_CONTENTHISTORY_ERROR_KEEP_NOT_PERMITTED')JLog::WARNING'jerror');
  200.  
  201.                         return false;
  202.                     }
  203.                 }
  204.             }
  205.             else
  206.             {
  207.                 $this->setError($table->getError());
  208.  
  209.                 return false;
  210.             }
  211.         }
  212.  
  213.         // Clear the component's cache
  214.         $this->cleanCache();
  215.  
  216.         return true;
  217.     }
  218.  
  219.     /**
  220.      * Method to auto-populate the model state.
  221.      *
  222.      * Note. Calling getState in this method will result in recursion.
  223.      *
  224.      * @param   string  $ordering   An optional ordering field.
  225.      * @param   string  $direction  An optional direction (asc|desc).
  226.      *
  227.      * @return  void 
  228.      *
  229.      * @since   3.2
  230.      */
  231.     protected function populateState($ordering null$direction null)
  232.     {
  233.         $input JFactory::getApplication()->input;
  234.         $itemId $input->get('item_id'0'integer');
  235.         $typeId $input->get('type_id'0'integer');
  236.         $typeAlias $input->get('type_alias''''string');
  237.  
  238.         $this->setState('item_id'$itemId);
  239.         $this->setState('type_id'$typeId);
  240.         $this->setState('type_alias'$typeAlias);
  241.         $this->setState('sha1_hash'$this->getSha1Hash());
  242.  
  243.         // Load the parameters.
  244.         $params JComponentHelper::getParams('com_contenthistory');
  245.         $this->setState('params'$params);
  246.  
  247.         // List state information.
  248.         parent::populateState('h.save_date''DESC');
  249.     }
  250.  
  251.     /**
  252.      * Build an SQL query to load the list data.
  253.      *
  254.      * @return  JDatabaseQuery 
  255.      *
  256.      * @since   3.2
  257.      */
  258.     protected function getListQuery()
  259.     {
  260.         // Create a new query object.
  261.         $db $this->getDbo();
  262.         $query $db->getQuery(true);
  263.  
  264.         // Select the required fields from the table.
  265.         $query->select(
  266.             $this->getState(
  267.                 'list.select',
  268.                 'h.version_id, h.ucm_item_id, h.ucm_type_id, h.version_note, h.save_date, h.editor_user_id,' .
  269.                 'h.character_count, h.sha1_hash, h.version_data, h.keep_forever'
  270.             )
  271.         )
  272.         ->from($db->quoteName('#__ucm_history'' AS h')
  273.         ->where($db->quoteName('h.ucm_item_id'' = ' $this->getState('item_id'))
  274.         ->where($db->quoteName('h.ucm_type_id'' = ' $this->getState('type_id'))
  275.  
  276.         // Join over the users for the editor
  277.         ->select('uc.name AS editor')
  278.         ->join('LEFT''#__users AS uc ON uc.id = h.editor_user_id');
  279.  
  280.         // Add the list ordering clause.
  281.         $orderCol $this->state->get('list.ordering');
  282.         $orderDirn $this->state->get('list.direction');
  283.         $query->order($db->quoteName($orderCol$orderDirn);
  284.  
  285.         return $query;
  286.     }
  287.  
  288.     /**
  289.      * Get the sha1 hash value for the current item being edited.
  290.      *
  291.      * @return  string  sha1 hash of row data
  292.      *
  293.      * @since   3.2
  294.      */
  295.     protected function getSha1Hash()
  296.     {
  297.         $result false;
  298.         $typeTable JTable::getInstance('Contenttype''JTable');
  299.         $typeId JFactory::getApplication()->input->getInteger('type_id'0);
  300.         $typeTable->load($typeId);
  301.         $typeAliasArray explode('.'$typeTable->type_alias);
  302.         JTable::addIncludePath(JPATH_ROOT '/administrator/components/' $typeAliasArray[0'/tables');
  303.         $contentTable $typeTable->getContentTable();
  304.         $keyValue JFactory::getApplication()->input->getInteger('item_id'0);
  305.  
  306.         if ($contentTable && $contentTable->load($keyValue))
  307.         {
  308.             $helper new JHelper;
  309.  
  310.             $dataObject $helper->getDataObject($contentTable);
  311.             $result $this->getTable('Contenthistory''JTable')->getSha1(json_encode($dataObject)$typeTable);
  312.         }
  313.  
  314.         return $result;
  315.     }
  316. }

Documentation generated on Tue, 19 Nov 2013 15:04:43 +0100 by phpDocumentor 1.4.3