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

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

https://github.com/projectfork/Projectfork
PHP | 377 lines | 224 code | 69 blank | 84 comment | 26 complexity | dfd8c4c80728e5105c8e8e2afe33e982 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 directories.
  15. *
  16. */
  17. class PFrepoModelDirectories extends JModelList
  18. {
  19. /**
  20. * Constructor
  21. *
  22. * @param array An optional associative array of configuration settings.
  23. *
  24. * @return void
  25. */
  26. public function __construct($config = array())
  27. {
  28. if (empty($config['filter_fields'])) {
  29. $config['filter_fields'] = array(
  30. 'a.id',
  31. 'a.project_id', 'project_title',
  32. 'a.title',
  33. 'a.created',
  34. 'a.created_by', 'author_name',
  35. 'a.modified',
  36. 'a.parent_id',
  37. 'a.modified_by', 'editor',
  38. 'a.checked_out',
  39. 'a.checked_out_time',
  40. 'a.access', 'access_level'
  41. );
  42. }
  43. parent::__construct($config);
  44. }
  45. /**
  46. * Method to get an array of data items.
  47. *
  48. * @return mixed An array of data items on success, false on failure.
  49. */
  50. public function getItems()
  51. {
  52. // Get a storage key.
  53. $store = $this->getStoreId();
  54. // Try to load the data from internal storage.
  55. if (isset($this->cache[$store])) {
  56. return $this->cache[$store];
  57. }
  58. // Load the list items.
  59. $query = $this->_getListQuery();
  60. try {
  61. $count = (int) $this->getState('list.count_elements');
  62. $items = $this->_getList($query, $this->getStart(), $this->getState('list.limit'));
  63. if ($count) {
  64. $pks = JArrayHelper::getColumn($items, 'id');
  65. $elements = $this->getElementCount($pks);
  66. }
  67. foreach ($items AS $item)
  68. {
  69. $item->element_count = ($count ? $elements[$item->id] : 0);
  70. $item->orphaned = empty($item->project_exists);
  71. }
  72. }
  73. catch (RuntimeException $e) {
  74. $this->setError($e->getMessage());
  75. return false;
  76. }
  77. // Add the items to the internal cache.
  78. $this->cache[$store] = $items;
  79. return $this->cache[$store];
  80. }
  81. /**
  82. * Counts the contents of the given folders
  83. *
  84. * @param array $pks The folders to count the contents of
  85. *
  86. * @retun array $count The element count
  87. */
  88. public function getElementCount($pks)
  89. {
  90. $query = $this->_db->getQuery(true);
  91. $user = JFactory::getUser();
  92. if (!is_array($pks) || !count($pks)) return array();
  93. // Count sub-folders
  94. $query->select('parent_id, COUNT(id) AS folder_count')
  95. ->from('#__pf_repo_dirs')
  96. ->where('parent_id IN(' . implode(',', $pks) . ')');
  97. if (!$user->authorise('core.admin')) {
  98. $levels = implode(',', $user->getAuthorisedViewLevels());
  99. $query->where('access IN (' . $levels . ')');
  100. }
  101. $query->group('parent_id');
  102. $this->_db->setQuery($query);
  103. try {
  104. $folder_count = $this->_db->loadAssocList('parent_id', 'folder_count');
  105. }
  106. catch (RuntimeException $e) {
  107. $this->setError($e->getMessage());
  108. return false;
  109. }
  110. // Count notes
  111. $query->clear()
  112. ->select('dir_id, COUNT(id) AS note_count')
  113. ->from('#__pf_repo_notes')
  114. ->where('dir_id IN(' . implode(',', $pks) . ')');
  115. if (!$user->authorise('core.admin')) {
  116. $levels = implode(',', $user->getAuthorisedViewLevels());
  117. $query->where('access IN (' . $levels . ')');
  118. }
  119. $query->group('dir_id');
  120. $this->_db->setQuery($query);
  121. try {
  122. $note_count = $this->_db->loadAssocList('dir_id', 'note_count');
  123. }
  124. catch (RuntimeException $e) {
  125. $this->setError($e->getMessage());
  126. return false;
  127. }
  128. // Count files
  129. $query->clear()
  130. ->select('dir_id, COUNT(id) AS file_count')
  131. ->from('#__pf_repo_files')
  132. ->where('dir_id IN(' . implode(',', $pks) . ')');
  133. if (!$user->authorise('core.admin')) {
  134. $levels = implode(',', $user->getAuthorisedViewLevels());
  135. $query->where('access IN (' . $levels . ')');
  136. }
  137. $query->group('dir_id');
  138. $this->_db->setQuery($query);
  139. try {
  140. $file_count = $this->_db->loadAssocList('dir_id', 'file_count');
  141. }
  142. catch (RuntimeException $e) {
  143. $this->setError($e->getMessage());
  144. return false;
  145. }
  146. // Put everything together
  147. $count = array();
  148. foreach ($pks as $pk)
  149. {
  150. $count[$pk] = 0;
  151. if (isset($folder_count[$pk])) $count[$pk] += $folder_count[$pk];
  152. if (isset($note_count[$pk])) $count[$pk] += $note_count[$pk];
  153. if (isset($file_count[$pk])) $count[$pk] += $file_count[$pk];
  154. }
  155. return $count;
  156. }
  157. /**
  158. * Build an SQL query to load the list data.
  159. *
  160. * @return jdatabasequery
  161. */
  162. protected function getListQuery()
  163. {
  164. $query = $this->_db->getQuery(true);
  165. $user = JFactory::getUser();
  166. // Get possible filters
  167. $filter_project = $this->getState('filter.project');
  168. $filter_access = $this->getState('filter.access');
  169. $filter_author = $this->getState('filter.author_id');
  170. $filter_search = $this->getState('filter.search');
  171. $filter_parent = $this->getState('filter.parent_id');
  172. // Select the required fields from the table.
  173. $query->select(
  174. $this->getState(
  175. 'list.select',
  176. 'a.id, a.project_id, a.title, a.alias, a.description, a.checked_out, '
  177. . 'a.checked_out_time, a.created, a.access, a.created_by, a.parent_id, '
  178. . 'a.protected, a.lft, a.rgt, a.level, a.path'
  179. )
  180. );
  181. $query->from('#__pf_repo_dirs AS a');
  182. // Join over the users for the checked out user.
  183. $query->select('uc.name AS editor')
  184. ->join('LEFT', '#__users AS uc ON uc.id = a.checked_out');
  185. // Join over the asset groups.
  186. $query->select('ag.title AS access_level')
  187. ->join('LEFT', '#__viewlevels AS ag ON ag.id = a.access');
  188. // Join over the users for the author.
  189. $query->select('ua.name AS author_name')
  190. ->join('LEFT', '#__users AS ua ON ua.id = a.created_by');
  191. // Join over the projects for the project title.
  192. $query->select('p.id AS project_exists, p.title AS project_title, p.alias AS project_alias')
  193. ->join('LEFT', '#__pf_projects AS p ON p.id = a.project_id');
  194. // Filter by access level.
  195. if ($filter_access) {
  196. $query->where('a.access = ' . (int) $filter_access);
  197. }
  198. // Implement View Level Access
  199. if (!$user->authorise('core.admin')) {
  200. $levels = implode(',', $user->getAuthorisedViewLevels());
  201. $query->where('a.access IN (' . $levels . ')');
  202. }
  203. // Filter by project
  204. if (is_numeric($filter_project) && $filter_project > 0) {
  205. $query->where('a.project_id = ' . (int) $filter_project);
  206. }
  207. // Filter by author
  208. if (is_numeric($filter_author)) {
  209. $type = $this->getState('filter.author_id.include', true) ? '= ' : '<>';
  210. $query->where('a.created_by ' . $type . (int) $filter_author);
  211. }
  212. // Filter by parent directory
  213. if (is_numeric($filter_parent)) {
  214. if (!empty($filter_search)) {
  215. $query2 = $this->_db->getQuery(true);
  216. $query2->select('lft, rgt')
  217. ->from('#__pf_repo_dirs')
  218. ->where('id = ' . (int) $filter_parent);
  219. $this->_db->setQuery($query2);
  220. $dir = $this->_db->loadObject();
  221. if (!empty($dir)) {
  222. $query->where('a.lft > ' . (int) $dir->lft)
  223. ->where('a.rgt < ' . (int) $dir->rgt);
  224. }
  225. }
  226. else {
  227. $query->where('a.parent_id = ' . (int) $filter_parent);
  228. }
  229. }
  230. // Filter by search in title.
  231. if (!empty($filter_search)) {
  232. if (stripos($filter_search, 'id:') === 0) {
  233. $query->where('a.id = '. (int) substr($filter_search, 3));
  234. }
  235. elseif (stripos($filter_search, 'author:') === 0) {
  236. $search = $this->_db->quote($this->_db->escape(substr($filter_search, 7), true) . '%');
  237. $query->where('(ua.name LIKE ' . $search . ' OR ua.username LIKE ' . $search . ')');
  238. }
  239. else {
  240. $search = $this->_db->quote('%' . $this->_db->escape($filter_search, true) . '%');
  241. $query->where('(a.title LIKE ' . $search . ' OR a.alias LIKE ' . $search . ')');
  242. }
  243. }
  244. // Add the list ordering clause.
  245. $order_col = $this->state->get('list.ordering', 'a.title');
  246. $order_dir = $this->state->get('list.direction', 'asc');
  247. if ($order_col != 'a.lft') {
  248. $order_col = $order_col . ' ' . $order_dir . ', a.lft';
  249. }
  250. $query->order($this->_db->escape($order_col . ' ' . $order_dir))
  251. ->group('a.id');
  252. return $query;
  253. }
  254. /**
  255. * Method to auto-populate the model state.
  256. * Note: Calling getState in this method will result in recursion.
  257. *
  258. * @return void
  259. */
  260. protected function populateState($ordering = 'a.title', $direction = 'asc')
  261. {
  262. // Initialise variables.
  263. $params = JComponentHelper::getParams('com_pfrepo');
  264. $app = JFactory::getApplication();
  265. // Adjust the context to support modal layouts.
  266. if ($layout = JRequest::getVar('layout')) $this->context .= '.' . $layout;
  267. // Config - Count elements
  268. $this->setState('list.count_elements', (int) $params->get('show_element_count'));
  269. // Filter - Search
  270. $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
  271. $this->setState('filter.search', $search);
  272. // Filter - Author
  273. $author_id = $app->getUserStateFromRequest($this->context . '.filter.author_id', 'filter_author_id');
  274. $this->setState('filter.author_id', $author_id);
  275. // Filter - Access
  276. $access = $this->getUserStateFromRequest($this->context . '.filter.access', 'filter_access', '');
  277. $this->setState('filter.access', $access);
  278. // Filter - Project
  279. $project = PFApplicationHelper::getActiveProjectId('filter_project');
  280. $this->setState('filter.project', $project);
  281. // Filter - Directory
  282. $parent_id = JRequest::getUint('filter_parent_id', 1);
  283. $this->setState('filter.parent_id', $parent_id);
  284. // List state information.
  285. parent::populateState($ordering, $direction);
  286. }
  287. /**
  288. * Method to get a store id based on model configuration state.
  289. *
  290. * This is necessary because the model is used by the component and
  291. * different modules that might need different sets of data or different
  292. * ordering requirements.
  293. *
  294. * @param string $id A prefix for the store id.
  295. *
  296. * @return string A store id.
  297. */
  298. protected function getStoreId($id = '')
  299. {
  300. // Compile the store id.
  301. $id .= ':' . $this->getState('filter.search');
  302. $id .= ':' . $this->getState('filter.access');
  303. $id .= ':' . $this->getState('filter.author_id');
  304. $id .= ':' . $this->getState('filter.project');
  305. $id .= ':' . $this->getState('filter.parent_id');
  306. return parent::getStoreId($id);
  307. }
  308. }