/administrator/components/com_templates/models/templates.php

https://bitbucket.org/kraymitchell/fcd · PHP · 157 lines · 79 code · 20 blank · 58 comment · 5 complexity · 2fa9040b588cc96a3e97f0286f5332f7 MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  4. * @license GNU General Public License version 2 or later; see LICENSE.txt
  5. */
  6. defined('_JEXEC') or die;
  7. jimport('joomla.application.component.modellist');
  8. /**
  9. * Methods supporting a list of template extension records.
  10. *
  11. * @package Joomla.Administrator
  12. * @subpackage com_templates
  13. * @since 1.6
  14. */
  15. class TemplatesModelTemplates extends JModelList
  16. {
  17. /**
  18. * Constructor.
  19. *
  20. * @param array An optional associative array of configuration settings.
  21. * @see JController
  22. * @since 1.6
  23. */
  24. public function __construct($config = array())
  25. {
  26. if (empty($config['filter_fields'])) {
  27. $config['filter_fields'] = array(
  28. 'id', 'a.id',
  29. 'name', 'a.name',
  30. 'folder', 'a.folder',
  31. 'element', 'a.element',
  32. 'checked_out', 'a.checked_out',
  33. 'checked_out_time', 'a.checked_out_time',
  34. 'state', 'a.state',
  35. 'enabled', 'a.enabled',
  36. 'access', 'a.access', 'access_level',
  37. 'ordering', 'a.ordering',
  38. 'client_id', 'a.client_id',
  39. );
  40. }
  41. parent::__construct($config);
  42. }
  43. /**
  44. * Override parent getItems to add extra XML metadata.
  45. *
  46. * @since 1.6
  47. */
  48. public function getItems()
  49. {
  50. $items = parent::getItems();
  51. foreach ($items as &$item) {
  52. $client = JApplicationHelper::getClientInfo($item->client_id);
  53. $item->xmldata = TemplatesHelper::parseXMLTemplateFile($client->path, $item->element);
  54. }
  55. return $items;
  56. }
  57. /**
  58. * Build an SQL query to load the list data.
  59. *
  60. * @return JDatabaseQuery
  61. * @since 1.6
  62. */
  63. protected function getListQuery()
  64. {
  65. // Create a new query object.
  66. $db = $this->getDbo();
  67. $query = $db->getQuery(true);
  68. // Select the required fields from the table.
  69. $query->select(
  70. $this->getState(
  71. 'list.select',
  72. 'a.extension_id, a.name, a.element, a.client_id'
  73. )
  74. );
  75. $query->from($db->quoteName('#__extensions').' AS a');
  76. // Filter by extension type.
  77. $query->where($db->quoteName('type').' = '.$db->quote('template'));
  78. // Filter by client.
  79. $clientId = $this->getState('filter.client_id');
  80. if (is_numeric($clientId)) {
  81. $query->where('a.client_id = '.(int) $clientId);
  82. }
  83. // Filter by search in title
  84. $search = $this->getState('filter.search');
  85. if (!empty($search)) {
  86. if (stripos($search, 'id:') === 0) {
  87. $query->where('a.id = '.(int) substr($search, 3));
  88. } else {
  89. $search = $db->Quote('%'.$db->escape($search, true).'%');
  90. $query->where('a.element LIKE '.$search.' OR a.name LIKE '.$search);
  91. }
  92. }
  93. // Add the list ordering clause.
  94. $query->order($db->escape($this->getState('list.ordering', 'a.folder')).' '.$db->escape($this->getState('list.direction', 'ASC')));
  95. return $query;
  96. }
  97. /**
  98. * Method to get a store id based on model configuration state.
  99. *
  100. * This is necessary because the model is used by the component and
  101. * different modules that might need different sets of data or different
  102. * ordering requirements.
  103. *
  104. * @param string $id A prefix for the store id.
  105. * @return string A store id.
  106. * @since 1.6
  107. */
  108. protected function getStoreId($id = '')
  109. {
  110. // Compile the store id.
  111. $id .= ':'.$this->getState('filter.search');
  112. $id .= ':'.$this->getState('filter.client_id');
  113. return parent::getStoreId($id);
  114. }
  115. /**
  116. * Method to auto-populate the model state.
  117. *
  118. * Note. Calling getState in this method will result in recursion.
  119. *
  120. * @since 1.6
  121. */
  122. protected function populateState($ordering = null, $direction = null)
  123. {
  124. // Initialise variables.
  125. $app = JFactory::getApplication('administrator');
  126. // Load the filter state.
  127. $search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
  128. $this->setState('filter.search', $search);
  129. $clientId = $this->getUserStateFromRequest($this->context.'.filter.client_id', 'filter_client_id', null);
  130. $this->setState('filter.client_id', $clientId);
  131. // Load the parameters.
  132. $params = JComponentHelper::getParams('com_templates');
  133. $this->setState('params', $params);
  134. // List state information.
  135. parent::populateState('a.element', 'asc');
  136. }
  137. }