PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 304 lines | 172 code | 51 blank | 81 comment | 20 complexity | 52787e69a53ab00228ff7d68bad4a04c MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: com_content.php 320 2010-10-18 01:03:30Z 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.model16');
  13. juimport('joomla.database.databasequery');
  14. /**
  15. * Model for extracting content from com_content.
  16. *
  17. * @package NewLifeInIT
  18. * @subpackage com_artofpdf
  19. * @since 1.0
  20. */
  21. class ArtofPdfModelCom_Content extends JModel16
  22. {
  23. /**
  24. * @var object The pdf object.
  25. */
  26. protected $content;
  27. /**
  28. * Get a pulbished article.
  29. *
  30. * @param int $id The id of the article.
  31. *
  32. * @return array
  33. * @since 1.6
  34. * @throws Exception on database error or invalid article id.
  35. */
  36. public function getArticle($id = 0)
  37. {
  38. // Initialiase variables.
  39. $db = $this->getDbo();
  40. $query = new JDatabaseQuery;
  41. $result = array();
  42. $id = (int) $id;
  43. if (empty($id)) {
  44. throw new Exception(JText::_('COM_ARTOFPDF_ERROR_INVALID_ARTICLE_ID'));
  45. }
  46. $query->select('a.id, a.catid, a.title, a.introtext, a.fulltext, a.metakey')
  47. ->from('#__content AS a')
  48. ->where('a.id = '.$id)
  49. ->where('a.state = 1');
  50. $db->setQuery((string) $query);
  51. $result = $db->loadObject();
  52. if ($error = $db->getErrorMsg()) {
  53. throw new Exception($error);
  54. }
  55. if (empty($result)) {
  56. throw new Exception(JText::_('COM_ARTOFPDF_ERROR_INVALID_ARTICLE'));
  57. }
  58. return $result;
  59. }
  60. /**
  61. * Get an array of articles grouped into their respective categories.
  62. *
  63. * @param array An array of section ids
  64. *
  65. * @return array
  66. * @since 1.6
  67. * @throws Exception on database error.
  68. */
  69. public function getArticles($categories = array())
  70. {
  71. // Initialiase variables.
  72. $db = $this->getDbo();
  73. $query = new JDatabaseQuery;
  74. $result = array();
  75. settype($categories, 'array');
  76. if (empty($categories)) {
  77. return $result;
  78. }
  79. JArrayHelper::toInteger($categories);
  80. $categoryIds = implode(',', $categories);
  81. $query->select('a.id, a.catid, a.title, a.introtext, a.fulltext')
  82. ->from('#__content AS a')
  83. ->where('a.catid IN ('.$categoryIds.')')
  84. ->where('a.state = 1')
  85. ->order('a.ordering');
  86. $db->setQuery((string) $query);
  87. $temp = $db->loadObjectList();
  88. if ($error = $db->getErrorMsg()) {
  89. throw new Exception($error);
  90. }
  91. // Group the articles into their respective categories.
  92. foreach ($temp as $article)
  93. {
  94. if (empty($result[$article->catid])) {
  95. $result[$article->catid] = array();
  96. }
  97. $result[$article->catid][] = $article;
  98. }
  99. return $result;
  100. }
  101. /**
  102. * Get an array of categories grouped into their respective sections.
  103. *
  104. * @param array An array of section ids
  105. *
  106. * @return array
  107. * @since 1.6
  108. * @throws Exception on database error.
  109. */
  110. public function getCategories($sections = array())
  111. {
  112. // Initialiase variables.
  113. $db = $this->getDbo();
  114. $query = new JDatabaseQuery;
  115. $result = array();
  116. if (empty($sections)) {
  117. return $result;
  118. }
  119. $values = array();
  120. foreach ($sections as $id) {
  121. $values[] = $db->quote(trim($id));
  122. }
  123. $query->select('a.id, a.title, a.description, a.section')
  124. ->from('#__categories AS a')
  125. ->where('a.section IN ('.implode(',', $values).')')
  126. ->where('a.published = 1')
  127. ->order('a.ordering');
  128. $db->setQuery((string) $query);
  129. $temp = $db->loadObjectList();
  130. if ($error = $db->getErrorMsg()) {
  131. throw new Exception($error);
  132. }
  133. $categoryIds = array();
  134. foreach ($temp as $category) {
  135. $categoryIds[] = $category->id;
  136. if (empty($result[$category->section])) {
  137. $result[$category->section] = array();
  138. }
  139. $result[$category->section][] = $category;
  140. }
  141. // Get the articles.
  142. $articles = $this->getArticles($categoryIds);
  143. // Loop through the categories and attach the articles array.
  144. foreach ($result as &$section)
  145. {
  146. foreach ($section as $category)
  147. {
  148. if (isset($articles[$category->id])) {
  149. $category->articles = $articles[$category->id];
  150. }
  151. else {
  152. $category->articles = array();
  153. }
  154. }
  155. }
  156. return $result;
  157. }
  158. /**
  159. * Get an array of categories grouped into their respective sections.
  160. *
  161. * @param int $id The id of the category.
  162. *
  163. * @return object
  164. * @since 1.6
  165. * @throws Exception on database error.
  166. */
  167. public function getCategory($id = 0)
  168. {
  169. // Initialiase variables.
  170. $db = $this->getDbo();
  171. $query = new JDatabaseQuery;
  172. $id = (int) $id;
  173. if (empty($id)) {
  174. throw new Exception(JText::_('COM_ARTOFPDF_ERROR_INVALID_CATEGORY_ID'));
  175. }
  176. $query->select('a.id, a.title, a.description, a.section')
  177. ->from('#__categories AS a')
  178. ->where('a.id = '.$id)
  179. ->where('a.published = 1')
  180. ->order('a.ordering');
  181. $db->setQuery((string) $query);
  182. $result = $db->loadObject();
  183. if ($error = $db->getErrorMsg()) {
  184. throw new Exception($error);
  185. }
  186. if (empty($result)) {
  187. throw new Exception(JText::_('COM_ARTOFPDF_ERROR_INVALID_ARTICLE'));
  188. }
  189. // Get the articles.
  190. $articles = $this->getArticles($id);
  191. if (isset($articles[$id])) {
  192. $result->articles = $articles[$id];
  193. }
  194. else {
  195. $result->articles = array();
  196. }
  197. return $result;
  198. }
  199. /**
  200. * Get a list of com_content sections.
  201. *
  202. * @return array
  203. * @since 1.0
  204. * @throws Exception on database error.
  205. */
  206. public function getSections()
  207. {
  208. // Initialiase variables.
  209. $result = array();
  210. if (empty($this->content['sections'])) {
  211. return $result;
  212. }
  213. JArrayHelper::toInteger($this->content['sections']);
  214. $sectionIds = implode(',', $this->content['sections']);
  215. $db = $this->getDbo();
  216. $query = new JDatabaseQuery;
  217. $query->select('a.id, a.title, a.description')
  218. ->from('#__sections AS a')
  219. ->where('a.id IN ('.$sectionIds.')')
  220. ->where('a.published = 1')
  221. ->order('a.ordering');
  222. $db->setQuery((string) $query);
  223. $result = $db->loadObjectList();
  224. if ($error = $db->getErrorMsg()) {
  225. throw new Exception($error);
  226. }
  227. // Get the categories.
  228. $categories = $this->getCategories($this->content['sections']);
  229. // Loop through the sections and attach the categories array.
  230. foreach ($result as $section)
  231. {
  232. if (isset($categories[$section->id])) {
  233. $section->categories = $categories[$section->id];
  234. }
  235. else {
  236. $section->categories = array();
  237. }
  238. }
  239. return $result;
  240. }
  241. /**
  242. * Sets the PDF object in the model.
  243. *
  244. * @param object $content The PDF content definition.
  245. *
  246. * @return void
  247. * @since 1.6
  248. */
  249. public function setContent($content)
  250. {
  251. $this->content = $content;
  252. }
  253. }