Advertisement
Guest User

/com_contenthistory/models/history.php

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