/pdf/code/trunk/administrator/components/com_artofpdf/models/com_content.php
PHP | 304 lines | 172 code | 51 blank | 81 comment | 20 complexity | 52787e69a53ab00228ff7d68bad4a04c MD5 | raw file
- <?php
- /**
- * @version $Id: com_content.php 320 2010-10-18 01:03:30Z eddieajau $
- * @package NewLifeInIT
- * @subpackage com_artofpdf
- * @copyright Copyright 2010 New Life in IT Pty Ltd. All rights reserved.
- * @license GNU General Public License version 2 or later.
- * @link http://www.theartofjoomla.com
- */
- // No direct access
- defined('_JEXEC') or die;
- juimport('joomla.application.component.model16');
- juimport('joomla.database.databasequery');
- /**
- * Model for extracting content from com_content.
- *
- * @package NewLifeInIT
- * @subpackage com_artofpdf
- * @since 1.0
- */
- class ArtofPdfModelCom_Content extends JModel16
- {
- /**
- * @var object The pdf object.
- */
- protected $content;
- /**
- * Get a pulbished article.
- *
- * @param int $id The id of the article.
- *
- * @return array
- * @since 1.6
- * @throws Exception on database error or invalid article id.
- */
- public function getArticle($id = 0)
- {
- // Initialiase variables.
- $db = $this->getDbo();
- $query = new JDatabaseQuery;
- $result = array();
- $id = (int) $id;
- if (empty($id)) {
- throw new Exception(JText::_('COM_ARTOFPDF_ERROR_INVALID_ARTICLE_ID'));
- }
- $query->select('a.id, a.catid, a.title, a.introtext, a.fulltext, a.metakey')
- ->from('#__content AS a')
- ->where('a.id = '.$id)
- ->where('a.state = 1');
- $db->setQuery((string) $query);
- $result = $db->loadObject();
- if ($error = $db->getErrorMsg()) {
- throw new Exception($error);
- }
- if (empty($result)) {
- throw new Exception(JText::_('COM_ARTOFPDF_ERROR_INVALID_ARTICLE'));
- }
- return $result;
- }
- /**
- * Get an array of articles grouped into their respective categories.
- *
- * @param array An array of section ids
- *
- * @return array
- * @since 1.6
- * @throws Exception on database error.
- */
- public function getArticles($categories = array())
- {
- // Initialiase variables.
- $db = $this->getDbo();
- $query = new JDatabaseQuery;
- $result = array();
- settype($categories, 'array');
- if (empty($categories)) {
- return $result;
- }
- JArrayHelper::toInteger($categories);
- $categoryIds = implode(',', $categories);
- $query->select('a.id, a.catid, a.title, a.introtext, a.fulltext')
- ->from('#__content AS a')
- ->where('a.catid IN ('.$categoryIds.')')
- ->where('a.state = 1')
- ->order('a.ordering');
- $db->setQuery((string) $query);
- $temp = $db->loadObjectList();
- if ($error = $db->getErrorMsg()) {
- throw new Exception($error);
- }
- // Group the articles into their respective categories.
- foreach ($temp as $article)
- {
- if (empty($result[$article->catid])) {
- $result[$article->catid] = array();
- }
- $result[$article->catid][] = $article;
- }
- return $result;
- }
- /**
- * Get an array of categories grouped into their respective sections.
- *
- * @param array An array of section ids
- *
- * @return array
- * @since 1.6
- * @throws Exception on database error.
- */
- public function getCategories($sections = array())
- {
- // Initialiase variables.
- $db = $this->getDbo();
- $query = new JDatabaseQuery;
- $result = array();
- if (empty($sections)) {
- return $result;
- }
- $values = array();
- foreach ($sections as $id) {
- $values[] = $db->quote(trim($id));
- }
- $query->select('a.id, a.title, a.description, a.section')
- ->from('#__categories AS a')
- ->where('a.section IN ('.implode(',', $values).')')
- ->where('a.published = 1')
- ->order('a.ordering');
- $db->setQuery((string) $query);
- $temp = $db->loadObjectList();
- if ($error = $db->getErrorMsg()) {
- throw new Exception($error);
- }
- $categoryIds = array();
- foreach ($temp as $category) {
- $categoryIds[] = $category->id;
- if (empty($result[$category->section])) {
- $result[$category->section] = array();
- }
- $result[$category->section][] = $category;
- }
- // Get the articles.
- $articles = $this->getArticles($categoryIds);
- // Loop through the categories and attach the articles array.
- foreach ($result as &$section)
- {
- foreach ($section as $category)
- {
- if (isset($articles[$category->id])) {
- $category->articles = $articles[$category->id];
- }
- else {
- $category->articles = array();
- }
- }
- }
- return $result;
- }
- /**
- * Get an array of categories grouped into their respective sections.
- *
- * @param int $id The id of the category.
- *
- * @return object
- * @since 1.6
- * @throws Exception on database error.
- */
- public function getCategory($id = 0)
- {
- // Initialiase variables.
- $db = $this->getDbo();
- $query = new JDatabaseQuery;
- $id = (int) $id;
- if (empty($id)) {
- throw new Exception(JText::_('COM_ARTOFPDF_ERROR_INVALID_CATEGORY_ID'));
- }
- $query->select('a.id, a.title, a.description, a.section')
- ->from('#__categories AS a')
- ->where('a.id = '.$id)
- ->where('a.published = 1')
- ->order('a.ordering');
- $db->setQuery((string) $query);
- $result = $db->loadObject();
- if ($error = $db->getErrorMsg()) {
- throw new Exception($error);
- }
- if (empty($result)) {
- throw new Exception(JText::_('COM_ARTOFPDF_ERROR_INVALID_ARTICLE'));
- }
- // Get the articles.
- $articles = $this->getArticles($id);
- if (isset($articles[$id])) {
- $result->articles = $articles[$id];
- }
- else {
- $result->articles = array();
- }
- return $result;
- }
- /**
- * Get a list of com_content sections.
- *
- * @return array
- * @since 1.0
- * @throws Exception on database error.
- */
- public function getSections()
- {
- // Initialiase variables.
- $result = array();
- if (empty($this->content['sections'])) {
- return $result;
- }
- JArrayHelper::toInteger($this->content['sections']);
- $sectionIds = implode(',', $this->content['sections']);
- $db = $this->getDbo();
- $query = new JDatabaseQuery;
- $query->select('a.id, a.title, a.description')
- ->from('#__sections AS a')
- ->where('a.id IN ('.$sectionIds.')')
- ->where('a.published = 1')
- ->order('a.ordering');
- $db->setQuery((string) $query);
- $result = $db->loadObjectList();
- if ($error = $db->getErrorMsg()) {
- throw new Exception($error);
- }
- // Get the categories.
- $categories = $this->getCategories($this->content['sections']);
- // Loop through the sections and attach the categories array.
- foreach ($result as $section)
- {
- if (isset($categories[$section->id])) {
- $section->categories = $categories[$section->id];
- }
- else {
- $section->categories = array();
- }
- }
- return $result;
- }
- /**
- * Sets the PDF object in the model.
- *
- * @param object $content The PDF content definition.
- *
- * @return void
- * @since 1.6
- */
- public function setContent($content)
- {
- $this->content = $content;
- }
- }