/administrator/components/com_templates/models/styles.php

https://github.com/dextercowley/joomla-cms · PHP · 167 lines · 82 code · 22 blank · 63 comment · 5 complexity · bca83a966d65de84eb2a4b1b3e38ac37 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_templates
  5. *
  6. * @copyright Copyright (C) 2005 - 2014 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. * Methods supporting a list of template style records.
  12. *
  13. * @package Joomla.Administrator
  14. * @subpackage com_templates
  15. * @since 1.6
  16. */
  17. class TemplatesModelStyles extends JModelList
  18. {
  19. /**
  20. * Constructor.
  21. *
  22. * @param array $config An optional associative array of configuration settings.
  23. *
  24. * @see JControllerLegacy
  25. * @since 1.6
  26. */
  27. public function __construct($config = array())
  28. {
  29. if (empty($config['filter_fields']))
  30. {
  31. $config['filter_fields'] = array(
  32. 'id', 'a.id',
  33. 'title', 'a.title',
  34. 'client_id', 'a.client_id',
  35. 'template', 'a.template',
  36. 'home', 'a.home',
  37. );
  38. }
  39. parent::__construct($config);
  40. }
  41. /**
  42. * Method to auto-populate the model state.
  43. *
  44. * Note. Calling getState in this method will result in recursion.
  45. *
  46. * @param string $ordering An optional ordering field.
  47. * @param string $direction An optional direction (asc|desc).
  48. *
  49. * @return void
  50. *
  51. * @since 1.6
  52. */
  53. protected function populateState($ordering = null, $direction = null)
  54. {
  55. // Load the filter state.
  56. $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
  57. $this->setState('filter.search', $search);
  58. $template = $this->getUserStateFromRequest($this->context . '.filter.template', 'filter_template');
  59. $this->setState('filter.template', $template);
  60. $clientId = $this->getUserStateFromRequest($this->context . '.filter.client_id', 'filter_client_id', null);
  61. $this->setState('filter.client_id', $clientId);
  62. // Load the parameters.
  63. $params = JComponentHelper::getParams('com_templates');
  64. $this->setState('params', $params);
  65. // List state information.
  66. parent::populateState('a.template', 'asc');
  67. }
  68. /**
  69. * Method to get a store id based on model configuration state.
  70. *
  71. * This is necessary because the model is used by the component and
  72. * different modules that might need different sets of data or different
  73. * ordering requirements.
  74. *
  75. * @param string $id A prefix for the store id.
  76. *
  77. * @return string A store id.
  78. */
  79. protected function getStoreId($id = '')
  80. {
  81. // Compile the store id.
  82. $id .= ':' . $this->getState('filter.search');
  83. $id .= ':' . $this->getState('filter.template');
  84. $id .= ':' . $this->getState('filter.client_id');
  85. return parent::getStoreId($id);
  86. }
  87. /**
  88. * Build an SQL query to load the list data.
  89. *
  90. * @return JDatabaseQuery
  91. */
  92. protected function getListQuery()
  93. {
  94. // Create a new query object.
  95. $db = $this->getDbo();
  96. $query = $db->getQuery(true);
  97. // Select the required fields from the table.
  98. $query->select(
  99. $this->getState(
  100. 'list.select',
  101. 'a.id, a.template, a.title, a.home, a.client_id, l.title AS language_title, l.image as image'
  102. )
  103. );
  104. $query->from($db->quoteName('#__template_styles') . ' AS a');
  105. // Join on menus.
  106. $query->select('COUNT(m.template_style_id) AS assigned')
  107. ->join('LEFT', '#__menu AS m ON m.template_style_id = a.id')
  108. ->group('a.id, a.template, a.title, a.home, a.client_id, l.title, l.image, e.extension_id');
  109. // Join over the language
  110. $query->join('LEFT', '#__languages AS l ON l.lang_code = a.home');
  111. // Filter by extension enabled
  112. $query->select('extension_id AS e_id')
  113. ->join('LEFT', '#__extensions AS e ON e.element = a.template')
  114. ->where('e.enabled = 1')
  115. ->where('e.type=' . $db->quote('template'));
  116. // Filter by template.
  117. if ($template = $this->getState('filter.template'))
  118. {
  119. $query->where('a.template = ' . $db->quote($template));
  120. }
  121. // Filter by client.
  122. $clientId = $this->getState('filter.client_id');
  123. if (is_numeric($clientId))
  124. {
  125. $query->where('a.client_id = ' . (int) $clientId);
  126. }
  127. // Filter by search in title
  128. $search = $this->getState('filter.search');
  129. if (!empty($search))
  130. {
  131. if (stripos($search, 'id:') === 0)
  132. {
  133. $query->where('a.id = ' . (int) substr($search, 3));
  134. }
  135. else
  136. {
  137. $search = $db->quote('%' . $db->escape($search, true) . '%');
  138. $query->where('a.template LIKE ' . $search . ' OR a.title LIKE ' . $search);
  139. }
  140. }
  141. // Add the list ordering clause.
  142. $query->order($db->escape($this->getState('list.ordering', 'a.title')) . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
  143. return $query;
  144. }
  145. }