PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_categories/models/categories.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 305 lines | 186 code | 42 blank | 77 comment | 18 complexity | b9d07169a72a22cac8680b235be372aa MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_categories
  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. defined('_JEXEC') or die;
  10. /**
  11. * Categories Component Categories Model
  12. *
  13. * @package Joomla.Administrator
  14. * @subpackage com_categories
  15. * @since 1.6
  16. */
  17. class CategoriesModelCategories extends JModelList
  18. {
  19. /**
  20. * Constructor.
  21. *
  22. * @param array An optional associative array of configuration settings.
  23. * @see JController
  24. * @since 1.6
  25. */
  26. public function __construct($config = array())
  27. {
  28. if (empty($config['filter_fields']))
  29. {
  30. $config['filter_fields'] = array(
  31. 'id', 'a.id',
  32. 'title', 'a.title',
  33. 'alias', 'a.alias',
  34. 'published', 'a.published',
  35. 'access', 'a.access', 'access_level',
  36. 'language', 'a.language',
  37. 'checked_out', 'a.checked_out',
  38. 'checked_out_time', 'a.checked_out_time',
  39. 'created_time', 'a.created_time',
  40. 'created_user_id', 'a.created_user_id',
  41. 'lft', 'a.lft',
  42. 'rgt', 'a.rgt',
  43. 'level', 'a.level',
  44. 'path', 'a.path',
  45. );
  46. }
  47. parent::__construct($config);
  48. }
  49. /**
  50. * Method to auto-populate the model state.
  51. *
  52. * Note. Calling getState in this method will result in recursion.
  53. *
  54. * @param string An optional ordering field.
  55. * @param string An optional direction (asc|desc).
  56. *
  57. * @return void
  58. * @since 1.6
  59. */
  60. protected function populateState($ordering = null, $direction = null)
  61. {
  62. $app = JFactory::getApplication();
  63. $context = $this->context;
  64. $extension = $app->getUserStateFromRequest('com_categories.categories.filter.extension', 'extension', 'com_content', 'cmd');
  65. $this->setState('filter.extension', $extension);
  66. $parts = explode('.', $extension);
  67. // Extract the component name
  68. $this->setState('filter.component', $parts[0]);
  69. // Extract the optional section name
  70. $this->setState('filter.section', (count($parts) > 1) ? $parts[1] : null);
  71. $search = $this->getUserStateFromRequest($context . '.search', 'filter_search');
  72. $this->setState('filter.search', $search);
  73. $level = $this->getUserStateFromRequest($context . '.filter.level', 'filter_level', 0, 'int');
  74. $this->setState('filter.level', $level);
  75. $access = $this->getUserStateFromRequest($context . '.filter.access', 'filter_access', 0, 'int');
  76. $this->setState('filter.access', $access);
  77. $published = $this->getUserStateFromRequest($context . '.filter.published', 'filter_published', '');
  78. $this->setState('filter.published', $published);
  79. $language = $this->getUserStateFromRequest($context . '.filter.language', 'filter_language', '');
  80. $this->setState('filter.language', $language);
  81. $tag = $this->getUserStateFromRequest($this->context . '.filter.tag', 'filter_tag', '');
  82. $this->setState('filter.tag', $tag);
  83. // List state information.
  84. parent::populateState('a.lft', 'asc');
  85. }
  86. /**
  87. * Method to get a store id based on model configuration state.
  88. *
  89. * This is necessary because the model is used by the component and
  90. * different modules that might need different sets of data or different
  91. * ordering requirements.
  92. *
  93. * @param string $id A prefix for the store id.
  94. *
  95. * @return string A store id.
  96. * @since 1.6
  97. */
  98. protected function getStoreId($id = '')
  99. {
  100. // Compile the store id.
  101. $id .= ':' . $this->getState('filter.search');
  102. $id .= ':' . $this->getState('filter.extension');
  103. $id .= ':' . $this->getState('filter.published');
  104. $id .= ':' . $this->getState('filter.language');
  105. return parent::getStoreId($id);
  106. }
  107. /**
  108. * @return string
  109. *
  110. * @since 1.6
  111. */
  112. protected function getListQuery()
  113. {
  114. // Create a new query object.
  115. $db = $this->getDbo();
  116. $query = $db->getQuery(true);
  117. $user = JFactory::getUser();
  118. $app = JFactory::getApplication();
  119. // Select the required fields from the table.
  120. $query->select(
  121. $this->getState(
  122. 'list.select',
  123. 'a.id, a.title, a.alias, a.note, a.published, a.access' .
  124. ', a.checked_out, a.checked_out_time, a.created_user_id' .
  125. ', a.path, a.parent_id, a.level, a.lft, a.rgt' .
  126. ', a.language'
  127. )
  128. );
  129. $query->from('#__categories AS a');
  130. // Join over the language
  131. $query->select('l.title AS language_title')
  132. ->join('LEFT', $db->quoteName('#__languages') . ' AS l ON l.lang_code = a.language');
  133. // Join over the users for the checked out user.
  134. $query->select('uc.name AS editor')
  135. ->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');
  136. // Join over the asset groups.
  137. $query->select('ag.title AS access_level')
  138. ->join('LEFT', '#__viewlevels AS ag ON ag.id = a.access');
  139. // Join over the users for the author.
  140. $query->select('ua.name AS author_name')
  141. ->join('LEFT', '#__users AS ua ON ua.id = a.created_user_id');
  142. // Join over the associations.
  143. $assoc = $this->getAssoc();
  144. if ($assoc)
  145. {
  146. $query->select('COUNT(asso2.id)>1 as association')
  147. ->join('LEFT', '#__associations AS asso ON asso.id = a.id AND asso.context=' . $db->quote('com_categories.item'))
  148. ->join('LEFT', '#__associations AS asso2 ON asso2.key = asso.key')
  149. ->group('a.id');
  150. }
  151. // Filter by extension
  152. if ($extension = $this->getState('filter.extension'))
  153. {
  154. $query->where('a.extension = ' . $db->quote($extension));
  155. }
  156. // Filter on the level.
  157. if ($level = $this->getState('filter.level'))
  158. {
  159. $query->where('a.level <= ' . (int) $level);
  160. }
  161. // Filter by access level.
  162. if ($access = $this->getState('filter.access'))
  163. {
  164. $query->where('a.access = ' . (int) $access);
  165. }
  166. // Implement View Level Access
  167. if (!$user->authorise('core.admin'))
  168. {
  169. $groups = implode(',', $user->getAuthorisedViewLevels());
  170. $query->where('a.access IN (' . $groups . ')');
  171. }
  172. // Filter by published state
  173. $published = $this->getState('filter.published');
  174. if (is_numeric($published))
  175. {
  176. $query->where('a.published = ' . (int) $published);
  177. }
  178. elseif ($published === '')
  179. {
  180. $query->where('(a.published IN (0, 1))');
  181. }
  182. // Filter by search in title
  183. $search = $this->getState('filter.search');
  184. if (!empty($search))
  185. {
  186. if (stripos($search, 'id:') === 0)
  187. {
  188. $query->where('a.id = ' . (int) substr($search, 3));
  189. }
  190. elseif (stripos($search, 'author:') === 0)
  191. {
  192. $search = $db->quote('%' . $db->escape(substr($search, 7), true) . '%');
  193. $query->where('(ua.name LIKE ' . $search . ' OR ua.username LIKE ' . $search . ')');
  194. }
  195. else
  196. {
  197. $search = $db->quote('%' . $db->escape($search, true) . '%');
  198. $query->where('(a.title LIKE ' . $search . ' OR a.alias LIKE ' . $search . ' OR a.note LIKE ' . $search . ')');
  199. }
  200. }
  201. // Filter on the language.
  202. if ($language = $this->getState('filter.language'))
  203. {
  204. $query->where('a.language = ' . $db->quote($language));
  205. }
  206. // Filter by a single tag.
  207. $tagId = $this->getState('filter.tag');
  208. if (is_numeric($tagId))
  209. {
  210. $query->where($db->quoteName('tagmap.tag_id') . ' = ' . (int) $tagId)
  211. ->join(
  212. 'LEFT', $db->quoteName('#__contentitem_tag_map', 'tagmap')
  213. . ' ON ' . $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
  214. . ' AND ' . $db->quoteName('tagmap.type_alias') . ' = ' . $db->quote($extension . '.category')
  215. );
  216. }
  217. // Add the list ordering clause
  218. $listOrdering = $this->getState('list.ordering', 'a.lft');
  219. $listDirn = $db->escape($this->getState('list.direction', 'ASC'));
  220. if ($listOrdering == 'a.access')
  221. {
  222. $query->order('a.access ' . $listDirn . ', a.lft ' . $listDirn);
  223. }
  224. else
  225. {
  226. $query->order($db->escape($listOrdering) . ' ' . $listDirn);
  227. }
  228. //echo nl2br(str_replace('#__','jos_',$query));
  229. return $query;
  230. }
  231. /**
  232. * Method to determine if an association exists
  233. *
  234. * @return boolean True if the association exists
  235. *
  236. * @since 3.0
  237. */
  238. public function getAssoc()
  239. {
  240. static $assoc = null;
  241. if (!is_null($assoc))
  242. {
  243. return $assoc;
  244. }
  245. $app = JFactory::getApplication();
  246. $extension = $this->getState('filter.extension');
  247. $assoc = isset($app->item_associations) ? $app->item_associations : 0;
  248. $extension = explode('.', $extension);
  249. $component = array_shift($extension);
  250. $cname = str_replace('com_', '', $component);
  251. if (!$assoc || !$component || !$cname)
  252. {
  253. $assoc = false;
  254. }
  255. else
  256. {
  257. $hname = $cname . 'HelperAssociation';
  258. JLoader::register($hname, JPATH_SITE . '/components/' . $component . '/helpers/association.php');
  259. $assoc = class_exists($hname) && !empty($hname::$category_association);
  260. }
  261. return $assoc;
  262. }
  263. }