PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/source/components/com_pfrepo/admin/models/filerevisions.php

https://github.com/projectfork/Projectfork
PHP | 257 lines | 132 code | 51 blank | 74 comment | 14 complexity | 721c9ade4c76e975b8e1a910282575fe MD5 | raw file
  1. <?php
  2. /**
  3. * @package pkg_projectfork
  4. * @subpackage com_pfrepo
  5. *
  6. * @author Tobias Kuhn (eaxs)
  7. * @copyright Copyright (C) 2006-2013 Tobias Kuhn. All rights reserved.
  8. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL, see LICENSE.txt
  9. */
  10. defined('_JEXEC') or die();
  11. jimport('joomla.application.component.modellist');
  12. jimport('joomla.application.component.helper');
  13. /**
  14. * Methods supporting a list of file revisions.
  15. *
  16. */
  17. class PFrepoModelFileRevisions extends JModelList
  18. {
  19. /**
  20. * Constructor
  21. *
  22. * @param array An optional associative array of configuration settings.
  23. */
  24. public function __construct($config = array())
  25. {
  26. if (empty($config['filter_fields'])) {
  27. $config['filter_fields'] = array(
  28. 'a.id',
  29. 'a.ordering'
  30. );
  31. }
  32. parent::__construct($config);
  33. }
  34. /**
  35. * Method to load the file head revision
  36. *
  37. * @param integer $pk The file id
  38. *
  39. * @return mixed Record object on success, False on error
  40. */
  41. public function getItem($pk = 0)
  42. {
  43. $pk = (int) (empty($pk) ? $this->getState('filter.id') : $pk);
  44. $cfg = array('ignore_request' => true);
  45. $model = $this->getInstance('File', 'PFrepoModel', $cfg);
  46. if (empty($pk)) return false;
  47. $item = $model->getItem($pk);
  48. if ($item) {
  49. $query = $this->_db->getQuery(true);
  50. $uid = ($item->modified_by > 0 ? $item->modified_by : $item->created_by);
  51. $query->select('name')
  52. ->from('#__users')
  53. ->where('id = ' . (int) $uid);
  54. $this->_db->setQuery($query);
  55. $item->author_name = $this->_db->loadResult();
  56. if ($item->modified != $this->_db->getNullDate()) {
  57. $item->created = $item->modified;
  58. }
  59. }
  60. return $item;
  61. }
  62. /**
  63. * Method to get an array of data items.
  64. *
  65. * @return mixed An array of data items on success, false on failure.
  66. */
  67. public function getItems()
  68. {
  69. if ((int) $this->getState('filter.id') <= 0) {
  70. return array();
  71. }
  72. // Get a storage key.
  73. $store = $this->getStoreId();
  74. // Try to load the data from internal storage.
  75. if (isset($this->cache[$store])) {
  76. return $this->cache[$store];
  77. }
  78. // Load the list items.
  79. $query = $this->_getListQuery();
  80. $items = $this->_getList($query, 0, 0);
  81. // Check for a database error.
  82. if ($this->_db->getErrorNum()) {
  83. $this->setError($this->_db->getErrorMsg());
  84. return false;
  85. }
  86. // Add the items to the internal cache.
  87. $this->cache[$store] = $items;
  88. return $this->cache[$store];
  89. }
  90. /**
  91. * Build a list of project authors
  92. *
  93. * @return array
  94. */
  95. public function getAuthors()
  96. {
  97. // Load only if we have a file id
  98. $id = (int) $this->getState('filter.id');
  99. if ($id <= 0) return array();
  100. $query = $this->_db->getQuery(true);
  101. // Construct the query
  102. $query->select('u.id AS value, u.name AS text')
  103. ->from('#__users AS u')
  104. ->join('INNER', '#__pf_repo_file_revs AS a ON a.created_by = u.id')
  105. ->where('a.parent_id = ' . $id)
  106. ->group('u.id')
  107. ->order('u.name');
  108. // Return the result
  109. $this->_db->setQuery($query, 0, 50);
  110. return $this->_db->loadObjectList();
  111. }
  112. /**
  113. * Build an SQL query to load the list data.
  114. *
  115. * @return object
  116. */
  117. protected function getListQuery()
  118. {
  119. $query = $this->_db->getQuery(true);
  120. $user = JFactory::getUser();
  121. // Get possible filters
  122. $filter_author = $this->getState('filter.author_id');
  123. $filter_search = $this->getState('filter.search');
  124. $filter_id = $this->getState('filter.id');
  125. // Select the required fields from the table.
  126. $query->select(
  127. $this->getState(
  128. 'list.select',
  129. 'a.id, a.project_id, a.parent_id, a.title, a.alias, a.description, '
  130. . 'a.file_name, a.file_extension, a.file_size, a.created, a.created_by, '
  131. . 'a.attribs, a.ordering'
  132. )
  133. );
  134. $query->from('#__pf_repo_file_revs AS a');
  135. // Join over the users for the author.
  136. $query->select('ua.name AS author_name')
  137. ->join('LEFT', '#__users AS ua ON ua.id = a.created_by');
  138. // Filter by file id
  139. $query->where('parent_id = ' . (int) $filter_id);
  140. // Filter by author
  141. if (is_numeric($filter_author)) {
  142. $type = $this->getState('filter.author_id.include', true) ? '= ' : '<>';
  143. $query->where('a.created_by ' . $type . (int) $filter_author);
  144. }
  145. // Filter by search in title.
  146. if (!empty($filter_search)) {
  147. if (stripos($filter_search, 'id:') === 0) {
  148. $query->where('a.id = '. (int) substr($filter_search, 3));
  149. }
  150. elseif (stripos($filter_search, 'author:') === 0) {
  151. $search = $this->_db->quote($this->_db->escape(substr($filter_search, 7), true) . '%');
  152. $query->where('(ua.name LIKE ' . $search . ' OR ua.username LIKE ' . $search . ')');
  153. }
  154. else {
  155. $search = $this->_db->quote('%' . $this->_db->escape($filter_search, true) . '%');
  156. $query->where('(a.title LIKE ' . $search . ' OR a.alias LIKE ' . $search . ')');
  157. }
  158. }
  159. // Add the list ordering clause.
  160. $order_col = $this->state->get('list.ordering', 'a.ordering');
  161. $order_dir = $this->state->get('list.direction', 'desc');
  162. $query->order($this->_db->escape($order_col . ' ' . $order_dir))
  163. ->group('a.id');
  164. return $query;
  165. }
  166. /**
  167. * Method to auto-populate the model state.
  168. * Note: Calling getState in this method will result in recursion.
  169. *
  170. * @return void
  171. */
  172. protected function populateState($ordering = 'a.ordering', $direction = 'desc')
  173. {
  174. // Initialise variables.
  175. $params = JComponentHelper::getParams('com_pfrepo');
  176. $app = JFactory::getApplication();
  177. // Adjust the context to support modal layouts.
  178. if ($layout = JRequest::getVar('layout')) $this->context .= '.' . $layout;
  179. // Filter - Search
  180. $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
  181. $this->setState('filter.search', $search);
  182. // Filter - Author
  183. $author_id = $app->getUserStateFromRequest($this->context . '.filter.author_id', 'filter_author_id');
  184. $this->setState('filter.author_id', $author_id);
  185. // Filter - File id
  186. $id = JRequest::getUint('id');
  187. $this->setState('filter.id', $id);
  188. // List state information.
  189. parent::populateState($ordering, $direction);
  190. }
  191. /**
  192. * Method to get a store id based on model configuration state.
  193. *
  194. * @param string $id A prefix for the store id.
  195. *
  196. * @return string A store id.
  197. */
  198. protected function getStoreId($id = '')
  199. {
  200. // Compile the store id.
  201. $id .= ':' . $this->getState('filter.search');
  202. $id .= ':' . $this->getState('filter.author_id');
  203. $id .= ':' . $this->getState('filter.id');
  204. return parent::getStoreId($id);
  205. }
  206. }