PageRenderTime 217ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_roksprocket/models/contentbuilderitems.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 324 lines | 202 code | 56 blank | 66 comment | 24 complexity | a9be17119eed25e8b76bc3d728d1c52b MD5 | raw file
  1. <?php
  2. // no direct access
  3. defined('_JEXEC') or die;
  4. jimport('joomla.application.component.modellist');
  5. /**
  6. *
  7. */
  8. class RokSprocketModelContentBuilderItems extends JModelList
  9. {
  10. /**
  11. * @param array $config
  12. */
  13. public function __construct($config = array())
  14. {
  15. if (empty($config['filter_fields'])) {
  16. $config['filter_fields'] = array(
  17. 'id', 'a.id',
  18. 'title', 'a.title',
  19. 'alias', 'a.alias',
  20. 'checked_out', 'a.checked_out',
  21. 'checked_out_time', 'a.checked_out_time',
  22. 'catid', 'a.catid', 'category_title',
  23. 'state', 'a.published',
  24. 'access', 'a.access', 'access_level',
  25. 'created', 'a.created',
  26. 'created_by', 'a.created_by',
  27. 'ordering', 'a.ordering',
  28. 'featured', 'a.featured',
  29. 'language', 'a.language',
  30. 'hits', 'a.hits',
  31. 'publish_up', 'a.publish_up',
  32. 'publish_down', 'a.publish_down',
  33. 'storage_id', 's.id',
  34. );
  35. }
  36. parent::__construct($config);
  37. }
  38. /**
  39. * @param null $ordering
  40. * @param null $direction
  41. */
  42. protected function populateState($ordering = null, $direction = null)
  43. {
  44. // Initialise variables.
  45. $app = JFactory::getApplication();
  46. $session = JFactory::getSession();
  47. // Adjust the context to support modal layouts.
  48. if ($layout = JFactory::getApplication()->input->getCmd('layout')) {
  49. $this->context .= '.' . $layout;
  50. }
  51. $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
  52. $this->setState('filter.search', $search);
  53. $access = $this->getUserStateFromRequest($this->context . '.filter.access', 'filter_access', 0, 'int');
  54. $this->setState('filter.access', $access);
  55. $authorId = $app->getUserStateFromRequest($this->context . '.filter.author_id', 'filter_author_id');
  56. $this->setState('filter.author_id', $authorId);
  57. $published = $this->getUserStateFromRequest($this->context . '.filter.published', 'filter_published', '');
  58. $this->setState('filter.published', $published);
  59. $categoryId = $this->getUserStateFromRequest($this->context . '.filter.category_id', 'filter_category_id');
  60. $this->setState('filter.category_id', $categoryId);
  61. $storage_id = $this->getUserStateFromRequest($this->context . '.filter.storage_id', 'filter_storage_id', '');
  62. $this->setState('filter.storage_id', $storage_id);
  63. $level = $this->getUserStateFromRequest($this->context . '.filter.featured', 'filter_featured', 0, 'int');
  64. $this->setState('filter.level', $level);
  65. $language = $this->getUserStateFromRequest($this->context . '.filter.language', 'filter_language', '');
  66. $this->setState('filter.language', $language);
  67. // List state information.
  68. parent::populateState('a.title', 'asc');
  69. }
  70. /**
  71. * @param string $id
  72. * @return string
  73. */
  74. protected function getStoreId($id = '')
  75. {
  76. // Compile the store id.
  77. $id .= ':' . $this->getState('filter.search');
  78. $id .= ':' . $this->getState('filter.access');
  79. $id .= ':' . $this->getState('filter.published');
  80. $id .= ':' . $this->getState('filter.category_id');
  81. $id .= ':' . $this->getState('filter.storage_id');
  82. $id .= ':' . $this->getState('filter.author_id');
  83. $id .= ':' . $this->getState('filter.language');
  84. return parent::getStoreId($id);
  85. }
  86. /**
  87. * @return JDatabaseQuery
  88. */
  89. protected function getListQuery()
  90. {
  91. // Create a new query object.
  92. $db = $this->getDbo();
  93. $query = $db->getQuery(true);
  94. $user = JFactory::getUser();
  95. // Select the required fields from the table.
  96. $query->select(
  97. $this->getState(
  98. 'list.select',
  99. 'a.id, a.title, a.alias, a.checked_out, a.checked_out_time, a.catid' .
  100. ', a.state, a.access, a.created, a.created_by, a.ordering, a.featured, a.language, a.hits' .
  101. ', a.publish_up, a.publish_down'
  102. )
  103. );
  104. $query->from('#__contentbuilder_articles as ca');
  105. $query->join('LEFT', '#__content AS a ON a.id = ca.article_id');
  106. $query->join('LEFT', '#__contentbuilder_records AS cr ON (cr.record_id = ca.record_id AND cr.reference_id = ca.reference_id)');
  107. $query->select('s.id as storage_id, s.name as storage_name');
  108. $query->join('LEFT', '#__contentbuilder_storages AS s ON s.id = cr.reference_id');
  109. // Join over the language
  110. $query->select('l.title AS language_title');
  111. $query->join('LEFT', $db->quoteName('#__languages').' AS l ON l.lang_code = a.language');
  112. // Join over the users for the checked out user.
  113. $query->select('uc.name AS editor');
  114. $query->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');
  115. // Join over the asset groups.
  116. $query->select('ag.title AS access_level');
  117. $query->join('LEFT', '#__viewlevels AS ag ON ag.id = a.access');
  118. // Join over the categories.
  119. $query->select('c.title AS category_title');
  120. $query->join('LEFT', '#__categories AS c ON c.id = a.catid');
  121. // Join over the users for the author.
  122. $query->select('ua.name AS author_name');
  123. $query->join('LEFT', '#__users AS ua ON ua.id = a.created_by');
  124. // Filter by storage_id location.
  125. if ($storage_id = $this->getState('filter.storage_id')) {
  126. $query->where('s.id = ' . (int) $storage_id);
  127. }
  128. // Filter by access level.
  129. if ($access = $this->getState('filter.access')) {
  130. $query->where('a.access = ' . (int) $access);
  131. }
  132. // Implement View Level Access
  133. if (!$user->authorise('core.admin'))
  134. {
  135. $groups = implode(',', $user->getAuthorisedViewLevels());
  136. $query->where('a.access IN ('.$groups.')');
  137. }
  138. // Filter by published state
  139. $published = $this->getState('filter.published');
  140. if (is_numeric($published)) {
  141. $query->where('a.state = ' . (int) $published);
  142. }
  143. elseif ($published === '') {
  144. $query->where('(a.state = 0 OR a.state = 1)');
  145. }
  146. // Filter by a single or group of categories.
  147. $baselevel = 1;
  148. $categoryId = $this->getState('filter.category_id');
  149. if (is_numeric($categoryId)) {
  150. $cat_tbl = JTable::getInstance('Category', 'JTable');
  151. $cat_tbl->load($categoryId);
  152. $rgt = $cat_tbl->rgt;
  153. $lft = $cat_tbl->lft;
  154. $baselevel = (int) $cat_tbl->level;
  155. $query->where('c.lft >= '.(int) $lft);
  156. $query->where('c.rgt <= '.(int) $rgt);
  157. }
  158. elseif (is_array($categoryId)) {
  159. JArrayHelper::toInteger($categoryId);
  160. $categoryId = implode(',', $categoryId);
  161. $query->where('a.catid IN ('.$categoryId.')');
  162. }
  163. // Filter on the level.
  164. if ($level = $this->getState('filter.level')) {
  165. $query->where('c.level <= '.((int) $level + (int) $baselevel - 1));
  166. }
  167. // Filter by author
  168. $authorId = $this->getState('filter.author_id');
  169. if (is_numeric($authorId)) {
  170. $type = $this->getState('filter.author_id.include', true) ? '= ' : '<>';
  171. $query->where('a.created_by '.$type.(int) $authorId);
  172. }
  173. // Filter by search in title.
  174. $search = $this->getState('filter.search');
  175. if (!empty($search)) {
  176. if (stripos($search, 'id:') === 0) {
  177. $query->where('a.id = '.(int) substr($search, 3));
  178. }
  179. elseif (stripos($search, 'author:') === 0) {
  180. $search = $db->Quote('%'.$db->escape(substr($search, 7), true).'%');
  181. $query->where('(ua.name LIKE '.$search.' OR ua.username LIKE '.$search.')');
  182. }
  183. else {
  184. $search = $db->Quote('%'.$db->escape($search, true).'%');
  185. $query->where('(a.title LIKE '.$search.' OR a.alias LIKE '.$search.')');
  186. }
  187. }
  188. // Filter on the language.
  189. if ($language = $this->getState('filter.language')) {
  190. $query->where('a.language = '.$db->quote($language));
  191. }
  192. // Add the list ordering clause.
  193. $orderCol = $this->state->get('list.ordering', 'a.title');
  194. $orderDirn = $this->state->get('list.direction', 'asc');
  195. if ($orderCol == 'a.ordering' || $orderCol == 'category_title') {
  196. $orderCol = 'c.title '.$orderDirn.', a.ordering';
  197. }
  198. //sqlsrv change
  199. if($orderCol == 'language')
  200. $orderCol = 'l.title';
  201. if($orderCol == 'access_level')
  202. $orderCol = 'ag.title';
  203. $query->order($db->escape($orderCol.' '.$orderDirn));
  204. // echo nl2br(str_replace('#__','jos_',$query));
  205. return $query;
  206. }
  207. /**
  208. * @return mixed
  209. */
  210. public function getStorages()
  211. {
  212. // Create a new query object.
  213. $db = $this->getDbo();
  214. $query = $db->getQuery(true);
  215. // Construct the query
  216. $query->select('id AS value, title AS text');
  217. $query->from('#__contentbuilder_storages');
  218. $query->order('name ASC');
  219. // Setup the query
  220. $db->setQuery($query->__toString());
  221. // Return the result
  222. return $db->loadObjectList();
  223. }
  224. /**
  225. * Build a list of authors
  226. *
  227. * @return JDatabaseQuery
  228. * @since 1.6
  229. */
  230. public function getAuthors() {
  231. // Create a new query object.
  232. $db = $this->getDbo();
  233. $query = $db->getQuery(true);
  234. // Construct the query
  235. $query->select('u.id AS value, u.name AS text');
  236. $query->from('#__users AS u');
  237. $query->join('INNER', '#__content AS c ON c.created_by = u.id');
  238. $query->group('u.id, u.name');
  239. $query->order('u.name');
  240. // Setup the query
  241. $db->setQuery($query->__toString());
  242. // Return the result
  243. return $db->loadObjectList();
  244. }
  245. /**
  246. * Method to get a list of articles.
  247. * Overridden to add a check for access levels.
  248. *
  249. * @return mixed An array of data items on success, false on failure.
  250. * @since 1.6.1
  251. */
  252. public function getItems()
  253. {
  254. $items = parent::getItems();
  255. $app = JFactory::getApplication();
  256. if ($app->isSite()) {
  257. $user = JFactory::getUser();
  258. $groups = $user->getAuthorisedViewLevels();
  259. for ($x = 0, $count = count($items); $x < $count; $x++) {
  260. //Check the access level. Remove articles the user shouldn't see
  261. if (!in_array($items[$x]->access, $groups)) {
  262. unset($items[$x]);
  263. }
  264. }
  265. }
  266. return $items;
  267. }
  268. }