PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/aamenu/code/trunk/administrator/components/com_aamenu/models/aamenu.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 194 lines | 98 code | 30 blank | 66 comment | 5 complexity | dca5d2584fedab6aee1b8ca07b2a03d9 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: aamenu.php 255 2010-08-20 09:43:07Z eddieajau $
  4. * @copyright Copyright (C) 2009 New Life in IT Pty Ltd. All rights reserved.
  5. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  6. * @link http://www.theartofjoomla.com
  7. */
  8. // no direct access
  9. defined('_JEXEC') or die;
  10. jximport('jxtended.application.component.modellist');
  11. jximport('jxtended.database.query');
  12. /**
  13. * @package Artof.AAMenu
  14. * @subpackage com_aamenu
  15. */
  16. class AAMenuModelAAMenu extends JxModelList
  17. {
  18. /**
  19. * Model context string.
  20. *
  21. * @var string
  22. */
  23. public $_context = 'com_aamenu.tags';
  24. /**
  25. * Get a list of the tags defined in the component parameters
  26. *
  27. * @return array
  28. */
  29. function getTags()
  30. {
  31. // Get the tags field from the parameters, explode on new line
  32. $config = JComponentHelper::getParams('com_aamenu');
  33. $tags = explode("\n", $config->get('tags'));
  34. // Build an options list
  35. $options = array();
  36. $options[] = JHtml::_('select.option', '', '');
  37. foreach ($tags as $tag) {
  38. $options[] = JHtml::_('select.option', trim($tag));
  39. }
  40. return $options;
  41. }
  42. /**
  43. * Assemble the query for the list based on the model state.
  44. *
  45. * @return object
  46. */
  47. function _getListQuery()
  48. {
  49. $query = new JXQuery;
  50. $db = &$this->getDBO();
  51. // Only get the fields we want the layout to know about
  52. $query->select($this->getState('list.select', 'a.id, a.name, a.option'));
  53. $query->from('#__components AS a');
  54. // We need to exclude some core components
  55. $exclude = array(
  56. 'com_content',
  57. 'com_frontpage',
  58. 'com_media',
  59. );
  60. $query->where($db->NameQuote('option').' NOT IN ('.implode(',', array_map(array($db, 'Quote'), $exclude)).')');
  61. // We need to exclude the sub-menu items
  62. $query->where('parent = 0');
  63. $query->where('enabled = 1');
  64. $query->where('admin_menu_link <> '.$db->quote(''));
  65. // Join on the tags mapped to the component
  66. $query->select('t.tag, t.ordering');
  67. $query->join('LEFT', '#__taoj_aamenu_tags AS t ON t.component_id = a.id');
  68. // Filter by search in title
  69. $search = $this->getState('filter.search');
  70. if (!empty($search)) {
  71. $search = $this->_db->Quote('%'.$this->_db->getEscaped( $search, true ).'%', false);
  72. $query->where('(a.title LIKE '.$search.')');
  73. }
  74. // Add the list ordering clause.
  75. $query->order($this->_db->getEscaped($this->getState('list.ordering', 'a.ordering')).' '.$this->_db->getEscaped($this->getState('list.direction', 'ASC')));
  76. //echo nl2br(str_replace('#__','jos_',$query->toString()));
  77. return $query;
  78. }
  79. /**
  80. * Method to get a store id based on model configuration state.
  81. *
  82. * This is necessary because the model is used by the component and
  83. * different modules that might need different sets of data or different
  84. * ordering requirements.
  85. *
  86. * @param string $id A prefix for the store id.
  87. *
  88. * @return string A store id.
  89. */
  90. public function _getStoreId($id = '')
  91. {
  92. // Compile the store id.
  93. $id .= ':'.$this->getState('list.start');
  94. $id .= ':'.$this->getState('list.limit');
  95. $id .= ':'.$this->getState('list.ordering');
  96. $id .= ':'.$this->getState('list.direction');
  97. $id .= ':'.$this->getState('filter.search');
  98. return md5($id);
  99. }
  100. /**
  101. * Method to auto-populate the model state.
  102. *
  103. * This method should only be called once per instantiation and is designed
  104. * to be called on the first call to the getState() method unless the model
  105. * configuration flag to ignore the request is set.
  106. *
  107. * @return void
  108. */
  109. public function _populateState()
  110. {
  111. // Initialize variables.
  112. $app = &JFactory::getApplication('administrator');
  113. $params = JComponentHelper::getParams('com_aamenu');
  114. $context = $this->_context.'.';
  115. // Load the filter state.
  116. $this->setState('filter.search', $app->getUserStateFromRequest($context.'filter.search', 'filter_search', ''));
  117. // Load the list state.
  118. $this->setState('list.start', $app->getUserStateFromRequest($context.'list.start', 'limitstart', 0, 'int'));
  119. $this->setState('list.limit', $app->getUserStateFromRequest($context.'list.limit', 'limit', $app->getCfg('list_limit', 25), 'int'));
  120. $this->setState('list.ordering', $app->getUserStateFromRequest($context.'list.ordering', 'filter_order', 'a.name', 'cmd'));
  121. $this->setState('list.direction', $app->getUserStateFromRequest($context.'list.direction', 'filter_order_Dir', 'ASC', 'word'));
  122. // Load the check parameters.
  123. if ($this->_state->get('filter.state') === '*') {
  124. $this->setState('check.state', false);
  125. } else {
  126. $this->setState('check.state', true);
  127. }
  128. // Load the parameters.
  129. $this->setState('params', $params);
  130. }
  131. /**
  132. * Save the tags assigned to the components in the list
  133. *
  134. * @return boolean True if successful, false otherwise and internal error set
  135. */
  136. function save()
  137. {
  138. $request = $this->getState('request');
  139. $db = &$this->getDBO();
  140. $tags = JArrayHelper::getValue($request, 'tags', array(), 'array');
  141. $ordering = JArrayHelper::getValue($request, 'ordering', array(), 'array');
  142. $ids = array_keys($tags);
  143. JArrayHelper::toInteger($ids);
  144. $query = 'DELETE FROM #__taoj_aamenu_tags' .
  145. ' WHERE component_id IN ('.implode(',', $ids).')';
  146. $db->setQuery($query);
  147. if (!$db->query()) {
  148. $this->setError($db->getErrorMsg());
  149. return false;
  150. }
  151. $tuples = array();
  152. foreach ($ids as $id) {
  153. $tuples[] = '('.(int) $id.','.$db->Quote($tags[$id]).','.(int) $ordering[$id].')';
  154. }
  155. $query = 'INSERT INTO #__taoj_aamenu_tags (component_id,tag,ordering) VALUES '.
  156. implode(',', $tuples);
  157. $db->setQuery($query);
  158. if (!$db->query()) {
  159. $this->setError($db->getErrorMsg());
  160. return false;
  161. }
  162. return true;
  163. }
  164. }