/pdf/code/trunk/administrator/components/com_artofpdf/models/com_menus.php
https://bitbucket.org/eddieajau/the-art-of-joomla-archive · PHP · 374 lines · 185 code · 56 blank · 133 comment · 37 complexity · e3820daf6c3ec03cfb4e30ea74171513 MD5 · raw file
- <?php
- /**
- * @version $Id: com_menus.php 385 2010-11-03 09:01:13Z 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');
- juimport('artof.pdfcontent');
- if (!class_exists('ArtofPdfHelper')) {
- require JPATH_COMPONENT.'/helpers/artofpdf.php';
- }
- /**
- * Model for extracting content from com_content.
- *
- * @package NewLifeInIT
- * @subpackage com_artofpdf
- * @since 1.0
- */
- class ArtofPdfModelCom_Menus extends JModel16
- {
- /**
- * @var object The pdf object.
- * @since 1.0
- */
- protected $content;
- /**
- * @var array A list of all the menus.
- * @since 1.0
- */
- protected $items;
- protected $lookupType = array();
- protected $lookupParent = array();
- /**
- * @var JObject The component options.
- * @since 1.0.1
- */
- protected $options;
- /**
- * Constructor
- *
- * @since 1.5
- */
- public function __construct($config = array())
- {
- parent::__construct($config);
- // Get the component options.
- $this->options = JComponentHelper::getParams('com_artopdf');
- $db = $this->getDbo();
- $query = new JDatabaseQuery();
- $query->select('a.id, a.menutype, a.parent, a.name, a.link, a.type, a.params, 0 AS level');
- $query->from('#__menu AS a');
- $query->where('a.published = 1');
- $query->where('a.type IN ('.$db->quote('component').','.$db->quote('menulink').')');
- $query->order('a.parent, a.ordering');
- $db->setQuery((string) $query);
- $this->items = $db->loadObjectList('id');
- if ($error = $db->getErrorMsg()) {
- return JError::raiseError(500, $error);
- }
- // Convert params and create reverse lookup arrays.
- foreach ($this->items as &$item)
- {
- // Resolve the link query arguments.
- if ($q = parse_url($item->link, PHP_URL_QUERY)) {
- $vars = array();
- parse_str($q, $vars);
- $item->link = new JObject;
- $item->link->setProperties($vars);
- }
- else {
- $item->link = new JObject;
- }
- // Convert the params to a simple JObject.
- $temp = new JParameter($item->params);
- $item->params = new JObject;
- $item->params->setProperties($temp->toArray());
- unset($temp);
- // Group by menu type.
- if (empty($this->lookupType[$item->menutype])) {
- $this->lookupType[$item->menutype] = array();
- }
- $this->lookupType[$item->menutype][] = &$item;
- // Group by parent.
- if (empty($this->lookupParent[$item->parent])) {
- $this->lookupParent[$item->parent] = array();
- }
- $this->lookupParent[$item->parent][$item->id] = &$item;
- }
- // Debug
- // foreach ($this->lookupParent as $id => $items) {
- // echo "<br>Parent: $id, Children:".implode(',', array_keys($items));
- // }
- // Compute the level of each item.
- $this->setLevel();
- }
- /**
- * Get all the menu items in a menu.
- *
- * @param string $name The name of the menu.
- *
- * @return array
- * @since 1.0
- */
- protected function &getMenu($name, $recurse = true)
- {
- $result = array();
- if (isset($this->lookupType[$name])) {
- $result = &$this->lookupType[$name];
- }
- return $result;
- }
- /**
- * Get an array of menu items.
- *
- * @param int $parentId An optional parent id by which to get items.
- * @return array
- * @since 1.0
- */
- public function &getItems($parentId = null)
- {
- // Initialiase variables.
- $result = array();
- if (!empty($parentId)) {
- if (isset($this->lookupParent[$parentId])) {
- $result = &$this->lookupParent($parentId);
- }
- }
- else if (!empty($this->content['menu'])) {
- $result = &$this->getMenu($this->content['menu'][0]);
- }
- return $result;
- }
- /**
- * Resolves the content for the menu item.
- *
- * @param object $item The menu item.
- *
- * @return object The resolved menu item as an object with properties title, html.
- * @since 1.0
- */
- public function resolve($item)
- {
- // Work on a clone of the item.
- $temp = clone $item;
- // Resolve the alias if applicable.
- if ($item->type == 'menulink' && $id = $item->params->get('menu_item')) {
- if (isset($this->items[$id])) {
- $temp = $this->items[$id];
- }
- }
- // Interogate the link.
- $option = ucfirst(strtolower(str_replace('com_', '', $temp->link->get('option'))));
- $view = ucfirst(strtolower($temp->link->get('view')));
- if ($option && $view) {
- // There is an option and a view. Push into a ComponentView handler.
- $method = 'resolve'.$option.$view;
- if (method_exists($this, $method)) {
- $resolved = $this->$method($temp);
- }
- else {
- $resolved = $this->resolveGeneric($item, '<p>Unknown option '.$option.' and view '.$view.'</p>');
- }
- }
- else if ($option) {
- // There is just an option. Push into a component handler.
- $method = 'resolve'.$option;
- if (method_exists($this, $method)) {
- $resolved = $this->$method($temp);
- }
- else {
- $resolved = $this->resolveGeneric($item, '<p>Unknown option '.$option.'</p>');
- }
- }
- else {
- // No idea what this is.
- $resolved = $this->resolveGeneric($item, '<p>Unknown content.</p>');
- }
- return $resolved;
- }
- /**
- * Resolves a content article into HTML.
- *
- * @param object $item The menu item.
- *
- * @return string The HTML reprsentation of the content.
- * @throws Exception
- */
- protected function resolveContentArticle($item)
- {
- // Get the generic content and work from there.
- $resolved = $this->resolveGeneric($item);
- // Get the article and assign content.
- if ($id = $item->link->get('id')) {
- $model = JModel::getInstance('com_content', 'ArtofPdfModel');
- $article = $model->getArticle($id);
- // Override the page title with the content title (but leave the bookmark title).
- $resolved->title = $article->title;
- // TODO Examine menu params for appropriate switches.
- $resolved->html = $article->introtext.$article->fulltext;
- $directives = ArtofPdfHelper::getDirectives($article->metakey);
- if (!empty($directives)) {
- if (in_array('nonewpage', $directives)) {
- $resolved->newpage = false;
- }
- else if (in_array('newpage', $directives)) {
- $resolved->newpage = false;
- }
- }
- }
- else {
- $resolved->html = JText::sprintf('COM_ARTOFPDF_ERROR_INVALID_CONTENT_MENU_ID');
- }
- return $resolved;
- }
- /**
- * Resolves a content category into HTML.
- *
- * @param object $item The menu item.
- *
- * @return string The HTML reprsentation of the content.
- * @throws Exception
- */
- protected function resolveContentCategory($item)
- {
- // Get the generic content and work from there.
- $resolved = $this->resolveGeneric($item);
- // Get the article and assign content.
- if ($id = $item->link->get('id')) {
- $model = JModel::getInstance('Com_content', 'ArtofPdfModel');
- $category = $model->getCategory($id);
- // Override the page title with the content title (but leave the bookmark title).
- $resolved->title = $category->title;
- // TODO Examine menu params for appropriate switches.
- $resolved->html = $category->description;
- // HACK!
- if ($item->level > 0) {
- $resolved->newpage = false;
- }
- // Now, add the articles as children to this item.
- if (empty($resolved->children)) {
- // Increment the menu level for the level of the article.
- $level = $item->level + 1;
- // Add each of the articles.
- foreach ($category->articles as $article)
- {
- $child = new JPdfContent($article->title, $level, $article->introtext.$article->fulltext);
- $child->newpage = false;
- $resolved->children[] = $child;
- }
- }
- else {
- // TODO What to do if there are menu children?
- }
- }
- else {
- $resolved->html = JText::sprintf('COM_ARTOFPDF_ERROR_INVALID_CONTENT_MENU_ID');
- }
- return $resolved;
- }
- /**
- * Resolves a generic menu item.
- *
- * @param object $item The menu item.
- * @param string $html The HTML content to use of the item.
- *
- * @return string The HTML reprsentation of the content.
- * @throws Exception
- */
- protected function resolveGeneric($item, $html = '')
- {
- $resolved = new JPdfContent($item->name, $item->level, $html);
- if (!empty($this->lookupParent[$item->id])) {
- $resolved->children = &$this->lookupParent[$item->id];
- }
- // Debug
- // echo "<br>Resolving: $item->id, ".count($resolved->children);
- return $resolved;
- }
- /**
- * 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;
- }
- /**
- * Recursively set the level in the menu tree.
- *
- * @param object $id The id of the current parent item.
- * @param int $level The current level.
- *
- * @return void
- * @since 1.0
- */
- protected function setLevel($id = 0, $level = 0)
- {
- // Second pass, compute the level of each item.
- if (!empty($this->lookupParent[$id])) {
- foreach ($this->lookupParent[$id] as &$item)
- {
- $item->level = $level;
- $this->setLevel($item->id, $level + 1);
- //echo "<br>ID: {$item->id}, Level: $level, Parent: {$item->parent}";
- }
- }
- }
- }