PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/site/models/category.php

https://github.com/Joomla-Bible-Study/joomla_churchdirectory
PHP | 479 lines | 250 code | 72 blank | 157 comment | 25 complexity | b396195e3bd5d62d9c944c08b13d866e MD5 | raw file
  1. <?php
  2. /**
  3. * @package ChurchDirectory.Site
  4. * @copyright 2007 - 2016 (C) Joomla Bible Study Team All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. * */
  7. defined('_JEXEC') or die;
  8. use Joomla\Registry\Registry;
  9. /**
  10. * Class list category
  11. *
  12. * @package ChurchDirectory.Site
  13. * @since 1.7.0
  14. */
  15. class ChurchDirectoryModelCategory extends JModelList
  16. {
  17. /**
  18. * Category items data
  19. *
  20. * @access protected
  21. * @var array
  22. * @since 1.7.2
  23. */
  24. protected $item = null;
  25. /**
  26. * Protect _articles
  27. *
  28. * @access protected
  29. * @var array
  30. * @since 1.7.2
  31. */
  32. protected $articles = null;
  33. /**
  34. * Protect _siblings
  35. *
  36. * @access protected
  37. * @var array
  38. * @since 1.7.2
  39. */
  40. protected $siblings = null;
  41. /**
  42. * Protect _children
  43. *
  44. * @access protected
  45. * @var array
  46. * @since 1.7.2
  47. */
  48. protected $children = null;
  49. /**
  50. * Protect _parent
  51. *
  52. * @access protected
  53. * @var object
  54. * @since 1.7.2
  55. */
  56. protected $parent = null;
  57. /**
  58. * The category that applies.
  59. *
  60. * @access protected
  61. * @var object
  62. * @since 1.7.2
  63. */
  64. protected $category = null;
  65. /**
  66. * The list of other newfeed categories.
  67. *
  68. * @access protected
  69. * @var array
  70. * @since 1.7.2
  71. */
  72. protected $categories = null;
  73. protected $rightsibling;
  74. protected $leftsibling;
  75. /**
  76. * Constructor.
  77. *
  78. * @param array $config An optional associative array of configuration settings.
  79. *
  80. * @since 1.7.2
  81. */
  82. public function __construct($config = [])
  83. {
  84. if (empty($config['filter_fields']))
  85. {
  86. $config['filter_fields'] = [
  87. 'id', 'a.id',
  88. 'name', 'a.name',
  89. 'con_position', 'a.con_position',
  90. 'suburb', 'a.suburb',
  91. 'state', 'a.state',
  92. 'country', 'a.country',
  93. 'ordering', 'a.ordering',
  94. 'sortname',
  95. 'sortname1', 'a.sortname1',
  96. 'sortname2', 'a.sortname2',
  97. 'sortname3', 'a.sortname3'
  98. ];
  99. }
  100. parent::__construct($config);
  101. }
  102. /**
  103. * Method to get a list of items.
  104. *
  105. * @return mixed An array of objects on success, false on failure.
  106. *
  107. * @since 1.7.2
  108. */
  109. public function getItems()
  110. {
  111. // Invoke the parent getItems method to get the main list
  112. $items = parent::getItems();
  113. // Convert the params field into an object, saving original in params
  114. foreach ($items as $i => $iValue)
  115. {
  116. $item = & $items[$i];
  117. if (!isset($this->params))
  118. {
  119. $item->params = new Registry($item->params);
  120. }
  121. $this->tags = new JHelperTags;
  122. $this->tags->getItemTags('com_churchdirectory.member', $item->id);
  123. }
  124. return $items;
  125. }
  126. /**
  127. * Method to build an SQL query to load the list data.
  128. *
  129. * @return string An SQL query
  130. *
  131. * @since 1.6
  132. */
  133. protected function getListQuery()
  134. {
  135. $user = JFactory::getUser();
  136. $groups = implode(',', $user->getAuthorisedViewLevels());
  137. // Create a new query object.
  138. $db = $this->getDbo();
  139. $query = $db->getQuery(true);
  140. // Select required fields from the categories.
  141. // -- sqlsrv changes
  142. $case_when = ' CASE WHEN ';
  143. $case_when .= $query->charLength('a.alias', '!=', '0');
  144. $case_when .= ' THEN ';
  145. $a_id = $query->castAsChar('a.id');
  146. $case_when .= $query->concatenate([$a_id, 'a.alias'], ':');
  147. $case_when .= ' ELSE ';
  148. $case_when .= $a_id . ' END as slug';
  149. $case_when1 = ' CASE WHEN ';
  150. $case_when1 .= $query->charLength('c.alias', '!=', '0');
  151. $case_when1 .= ' THEN ';
  152. $c_id = $query->castAsChar('c.id');
  153. $case_when1 .= $query->concatenate([$c_id, 'c.alias'], ':');
  154. $case_when1 .= ' ELSE ';
  155. $case_when1 .= $c_id . ' END as catslug';
  156. $query->select($this->getState('list.select', 'a.*') . ',' . $case_when . ',' . $case_when1);
  157. $query->from('`#__churchdirectory_details` AS a');
  158. $query->join('LEFT', '#__categories AS c ON c.id = a.catid');
  159. $query->where('a.access IN (' . $groups . ')');
  160. // Filter by category.
  161. if ($categoryId = $this->getState('category.id'))
  162. {
  163. $query->where('a.catid = ' . (int) $categoryId);
  164. $query->where('c.access IN (' . $groups . ')');
  165. }
  166. // Join over the users for the author and modified_by names.
  167. $query->select("CASE WHEN a.created_by_alias > ' ' THEN a.created_by_alias ELSE ua.name END AS author");
  168. $query->select("ua.email AS author_email");
  169. $query->join('LEFT', '#__users AS ua ON ua.id = a.created_by');
  170. $query->join('LEFT', '#__users AS uam ON uam.id = a.modified_by');
  171. // Filter by state
  172. $state = $this->getState('filter.published');
  173. if (is_numeric($state))
  174. {
  175. $query->where('a.published = ' . (int) $state);
  176. }
  177. // Filter by start and end dates.
  178. $nullDate = $db->q($db->getNullDate());
  179. $nowDate = $db->q(JFactory::getDate()->toSql());
  180. if ($this->getState('filter.publish_date'))
  181. {
  182. $query->where('(a.publish_up = ' . $nullDate . ' OR a.publish_up <= ' . $nowDate . ')');
  183. $query->where('(a.publish_down = ' . $nullDate . ' OR a.publish_down >= ' . $nowDate . ')');
  184. }
  185. // Filter by search in title
  186. $search = $this->getState('list.filter');
  187. if (!empty($search))
  188. {
  189. $search = $db->q('%' . $db->escape($search, true) . '%');
  190. $query->where('(a.name LIKE ' . $search . ')');
  191. }
  192. // Join over the categories.
  193. $query->select('ca.title AS category_title, ca.params AS category_params, ca.description AS category_description,' .
  194. ' ca.alias AS category_alias, ca.access AS category_access');
  195. $query->join('INNER', '#__categories AS ca ON ca.id = a.catid');
  196. // Filter by language
  197. if ($this->getState('filter.language'))
  198. {
  199. $query->where('a.language in (' . $db->q(JFactory::getLanguage()->getTag()) . ',' . $db->q('*') . ')');
  200. }
  201. // Set sortname ordering if selected
  202. if ($this->getState('list.ordering') === 'sortname')
  203. {
  204. $query->order($db->escape('a.sortname1') . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
  205. $query->order($db->escape('a.sortname2') . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
  206. $query->order($db->escape('a.sortname3') . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
  207. }
  208. else
  209. {
  210. $query->order($db->escape($this->getState('list.ordering', 'a.ordering')) . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
  211. }
  212. return $query;
  213. }
  214. /**
  215. * Method to auto-populate the model state.
  216. *
  217. * Note. Calling getState in this method will result in recursion.
  218. *
  219. * @param string $ordering An optional ordering field.
  220. * @param string $direction An optional direction (asc|desc).
  221. *
  222. * @return void
  223. * @throws \Exception
  224. * @since 1.6
  225. */
  226. protected function populateState($ordering = null, $direction = null)
  227. {
  228. // Initialise variables.
  229. $app = JFactory::getApplication();
  230. $params = JComponentHelper::getParams('com_churchdirectory');
  231. // List state information
  232. $format = $app->input->getWord('format');
  233. if ($format === 'feed')
  234. {
  235. $limit = $app->get('feed_limit');
  236. }
  237. else
  238. {
  239. $limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->get('list_limit'));
  240. }
  241. if (!$app->input->getInt('print'))
  242. {
  243. $this->setState('list.limit', $limit);
  244. $limitstart = $app->input->get('limitstart', 0, '', 'int');
  245. $this->setState('list.start', $limitstart);
  246. }
  247. // Optional filter text
  248. $this->setState('list.filter', $app->input->getString('filter-search', ''));
  249. // Get list ordering default from the parameters
  250. $menuParams = new Registry;
  251. if ($menu = $app->getMenu()->getActive())
  252. {
  253. $menuParams->loadString($menu->params);
  254. }
  255. $mergedParams = clone $params;
  256. $mergedParams->merge($menuParams);
  257. $orderCol = $app->input->get('filter_order', $mergedParams->get('initial_sort', 'ordering'));
  258. if (!in_array($orderCol, $this->filter_fields))
  259. {
  260. $orderCol = 'ordering';
  261. }
  262. $this->setState('list.ordering', $orderCol);
  263. $listOrder = $app->input->get('filter_order_Dir', 'ASC');
  264. if (!in_array(strtoupper($listOrder), ['ASC', 'DESC', '']))
  265. {
  266. $listOrder = 'ASC';
  267. }
  268. $this->setState('list.direction', $listOrder);
  269. $id = $app->input->get('id', 0, 'int');
  270. $this->setState('category.id', $id);
  271. $user = JFactory::getUser();
  272. if ((!$user->authorise('core.edit.state', 'com_churchdirectory')) && (!$user->authorise('core.edit', 'com_churchdirectory')))
  273. {
  274. // Limit to published for people who can't edit or edit.state.
  275. $this->setState('filter.published', 1);
  276. // Filter by start and end dates.
  277. $this->setState('filter.publish_date', true);
  278. }
  279. $this->setState('filter.language', $app->getLanguageFilter());
  280. // Load the parameters.
  281. $this->setState('params', $params);
  282. }
  283. /**
  284. * Method to get category data for the current category
  285. *
  286. * @return object
  287. *
  288. * @since 1.5
  289. */
  290. public function getCategory()
  291. {
  292. if (!is_object($this->item))
  293. {
  294. $app = JFactory::getApplication();
  295. $menu = $app->getMenu();
  296. $active = $menu->getActive();
  297. $params = new Registry;
  298. if ($active)
  299. {
  300. $params->loadString($active->params);
  301. }
  302. $options = [];
  303. $options['countItems'] = $params->get('show_cat_items', 1) || $params->get('show_empty_categories', 0);
  304. $categories = JCategories::getInstance('ChurchDirectory', $options);
  305. $this->item = $categories->get($this->getState('category.id', 'root'));
  306. if (is_object($this->item))
  307. {
  308. $this->children = $this->item->getChildren();
  309. $this->parent = false;
  310. if ($this->item->getParent())
  311. {
  312. $this->parent = $this->item->getParent();
  313. }
  314. $this->rightsibling = $this->item->getSibling();
  315. $this->leftsibling = $this->item->getSibling(false);
  316. }
  317. else
  318. {
  319. $this->children = false;
  320. $this->parent = false;
  321. }
  322. }
  323. return $this->item;
  324. }
  325. /**
  326. * Get the parent category.
  327. *
  328. * @return mixed An array of categories or false if an error occurs.
  329. *
  330. * @since 1.7.2
  331. */
  332. public function getParent()
  333. {
  334. if (!is_object($this->item))
  335. {
  336. $this->getCategory();
  337. }
  338. return $this->parent;
  339. }
  340. /**
  341. * Get the sibling (adjacent) categories.
  342. *
  343. * @return mixed An array of categories or false if an error occurs.
  344. *
  345. * @since 1.7.2
  346. */
  347. public function &getLeftSibling()
  348. {
  349. if (!is_object($this->item))
  350. {
  351. $this->getCategory();
  352. }
  353. return $this->leftsibling;
  354. }
  355. /**
  356. * Get right Sibling
  357. *
  358. * @return string
  359. *
  360. * @since 1.7.2
  361. */
  362. public function &getRightSibling()
  363. {
  364. if (!is_object($this->item))
  365. {
  366. $this->getCategory();
  367. }
  368. return $this->rightsibling;
  369. }
  370. /**
  371. * Get the child categories.
  372. *
  373. * @return mixed An array of categories or false if an error occurs.
  374. *
  375. * @since 1.7.2
  376. */
  377. public function &getChildren()
  378. {
  379. if (!is_object($this->item))
  380. {
  381. $this->getCategory();
  382. }
  383. return $this->children;
  384. }
  385. /**
  386. * Increment the hit counter for the category.
  387. *
  388. * @param integer $pk Optional primary key of the category to increment.
  389. *
  390. * @return boolean True if successful; false otherwise and internal error set.
  391. *
  392. * @since 3.2
  393. */
  394. public function hit($pk = 0)
  395. {
  396. $input = JFactory::getApplication()->input;
  397. $hitcount = $input->getInt('hitcount', 1);
  398. if ($hitcount)
  399. {
  400. $pk = (!empty($pk)) ? $pk : (int) $this->getState('category.id');
  401. $table = JTable::getInstance('Category', 'JTable');
  402. $table->load($pk);
  403. $table->hit($pk);
  404. }
  405. return true;
  406. }
  407. }