PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/pdf/code/trunk/administrator/components/com_artofpdf/models/pdfs.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 170 lines | 87 code | 21 blank | 62 comment | 13 complexity | 55bdd92fc46e1454e0828b78280d531d MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: pdfs.php 288 2010-09-27 21:45:36Z eddieajau $
  4. * @package NewLifeInIT
  5. * @subpackage com_artofpdf
  6. * @copyright Copyright 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License version 2 or later.
  8. * @link http://www.theartofjoomla.com
  9. */
  10. // No direct access
  11. defined('_JEXEC') or die;
  12. juimport('joomla.application.component.modellist');
  13. juimport('joomla.database.databasequery');
  14. /**
  15. * Pdfs model.
  16. *
  17. * @package NewLifeInIT
  18. * @subpackage com_artofpdf
  19. * @since 1.0
  20. */
  21. class ArtofPdfModelPdfs extends JModelList
  22. {
  23. /**
  24. * Method to get an array of data items.
  25. *
  26. * @return mixed An array of data items on success, false on failure.
  27. * @since 1.6
  28. */
  29. public function getItems()
  30. {
  31. $items = parent::getItems();
  32. // Add path and PDF information.
  33. foreach ($items as $item)
  34. {
  35. // TODO configure PDF name.
  36. $path = JPATH_SITE.'/tmp/'.$item->title.'.pdf';
  37. if (file_exists($path)) {
  38. $item->file_path = $path;
  39. $item->file_url = JUri::root().'tmp/'.$item->title.'.pdf';
  40. $item->file_size = filesize($path);
  41. $item->file_name = basename($path);
  42. }
  43. else {
  44. $item->file_path = null;
  45. $item->file_url = null;
  46. $item->file_size = null;
  47. }
  48. }
  49. return $items;
  50. }
  51. /**
  52. * Build an SQL query to load the list data.
  53. *
  54. * @return JDatabaseQuery
  55. * @since 1.0
  56. */
  57. protected function getListQuery()
  58. {
  59. // Initialise variables.
  60. $db = $this->getDbo();
  61. $query = new JDatabaseQuery;
  62. // Select the required fields from the table.
  63. $query->select(
  64. $this->getState(
  65. 'list.select',
  66. 'a.id, a.title, a.published, a.build_time'
  67. )
  68. );
  69. $query->from('#__artof_pdfs AS a');
  70. // Join on the access name.
  71. $query->select('g.name AS access_name');
  72. $query->innerJoin('#__groups AS g ON g.id = a.access');
  73. // Filter by access level.
  74. $access = $this->getState('filter.access');
  75. if (is_numeric($access)) {
  76. $query->where('a.access = '.(int) $access);
  77. }
  78. // Filter by published state.
  79. $published = $this->getState('filter.state');
  80. if (is_numeric($published)) {
  81. $query->where('a.published = '.(int) $published);
  82. }
  83. else if (empty($published)) {
  84. $query->where('(a.published IN (0, 1))');
  85. }
  86. // Filter by search in title
  87. $search = $this->getState('filter.search');
  88. if (!empty($search)) {
  89. if (stripos($search, 'id:') === 0) {
  90. $query->where('a.id = '.(int) substr($search, 3));
  91. }
  92. else {
  93. $search = $db->Quote('%'.$db->getEscaped($search, true).'%');
  94. $query->where('(a.title LIKE '.$search.')');
  95. }
  96. }
  97. // Add the list ordering clause.
  98. $orderCol = $this->state->get('list.ordering');
  99. $orderDirn = $this->state->get('list.direction');
  100. if ($orderCol == 'a.ordering' || $orderCol == 'category_title') {
  101. $orderCol = 'category_title '.$orderDirn.', a.ordering';
  102. }
  103. $query->order($db->getEscaped($orderCol.' '.$orderDirn));
  104. //echo nl2br(str_replace('#__','jos_',$query));
  105. return $query;
  106. }
  107. /**
  108. * Method to get a store id based on model configuration state.
  109. *
  110. * This is necessary because the model is used by the component and
  111. * different modules that might need different sets of data or different
  112. * ordering requirements.
  113. *
  114. * @param string A prefix for the store id.
  115. *
  116. * @return string A store id.
  117. * @since 1.0
  118. */
  119. protected function getStoreId($id = '')
  120. {
  121. // Compile the store id.
  122. $id .= ':'.$this->getState('filter.search');
  123. $id .= ':'.$this->getState('filter.access');
  124. $id .= ':'.$this->getState('filter.state');
  125. return parent::getStoreId($id);
  126. }
  127. /**
  128. * Method to auto-populate the model state.
  129. *
  130. * Note. Calling getState in this method will result in recursion.
  131. *
  132. * @return void
  133. * @since 1.0
  134. */
  135. protected function populateState()
  136. {
  137. // Initialise variables.
  138. $app = JFactory::getApplication('administrator');
  139. // Load the filter state.
  140. $value = $app->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
  141. $this->setState('filter.search', $value);
  142. $value = $app->getUserStateFromRequest($this->context.'.filter.access', 'filter_access');
  143. $this->setState('filter.access', $value);
  144. $state = $app->getUserStateFromRequest($this->context.'.filter.state', 'filter_published');
  145. $this->setState('filter.state', $state);
  146. // Set list state ordering defaults.
  147. parent::populateState('a.title', 'asc');
  148. }
  149. }