PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1. <?php
  2. /**
  3. * @version $Id: com_menus.php 385 2010-11-03 09:01:13Z 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. juimport('artof.pdfcontent');
  15. if (!class_exists('ArtofPdfHelper')) {
  16. require JPATH_COMPONENT.'/helpers/artofpdf.php';
  17. }
  18. /**
  19. * Model for extracting content from com_content.
  20. *
  21. * @package NewLifeInIT
  22. * @subpackage com_artofpdf
  23. * @since 1.0
  24. */
  25. class ArtofPdfModelCom_Menus extends JModel16
  26. {
  27. /**
  28. * @var object The pdf object.
  29. * @since 1.0
  30. */
  31. protected $content;
  32. /**
  33. * @var array A list of all the menus.
  34. * @since 1.0
  35. */
  36. protected $items;
  37. protected $lookupType = array();
  38. protected $lookupParent = array();
  39. /**
  40. * @var JObject The component options.
  41. * @since 1.0.1
  42. */
  43. protected $options;
  44. /**
  45. * Constructor
  46. *
  47. * @since 1.5
  48. */
  49. public function __construct($config = array())
  50. {
  51. parent::__construct($config);
  52. // Get the component options.
  53. $this->options = JComponentHelper::getParams('com_artopdf');
  54. $db = $this->getDbo();
  55. $query = new JDatabaseQuery();
  56. $query->select('a.id, a.menutype, a.parent, a.name, a.link, a.type, a.params, 0 AS level');
  57. $query->from('#__menu AS a');
  58. $query->where('a.published = 1');
  59. $query->where('a.type IN ('.$db->quote('component').','.$db->quote('menulink').')');
  60. $query->order('a.parent, a.ordering');
  61. $db->setQuery((string) $query);
  62. $this->items = $db->loadObjectList('id');
  63. if ($error = $db->getErrorMsg()) {
  64. return JError::raiseError(500, $error);
  65. }
  66. // Convert params and create reverse lookup arrays.
  67. foreach ($this->items as &$item)
  68. {
  69. // Resolve the link query arguments.
  70. if ($q = parse_url($item->link, PHP_URL_QUERY)) {
  71. $vars = array();
  72. parse_str($q, $vars);
  73. $item->link = new JObject;
  74. $item->link->setProperties($vars);
  75. }
  76. else {
  77. $item->link = new JObject;
  78. }
  79. // Convert the params to a simple JObject.
  80. $temp = new JParameter($item->params);
  81. $item->params = new JObject;
  82. $item->params->setProperties($temp->toArray());
  83. unset($temp);
  84. // Group by menu type.
  85. if (empty($this->lookupType[$item->menutype])) {
  86. $this->lookupType[$item->menutype] = array();
  87. }
  88. $this->lookupType[$item->menutype][] = &$item;
  89. // Group by parent.
  90. if (empty($this->lookupParent[$item->parent])) {
  91. $this->lookupParent[$item->parent] = array();
  92. }
  93. $this->lookupParent[$item->parent][$item->id] = &$item;
  94. }
  95. // Debug
  96. // foreach ($this->lookupParent as $id => $items) {
  97. // echo "<br>Parent: $id, Children:".implode(',', array_keys($items));
  98. // }
  99. // Compute the level of each item.
  100. $this->setLevel();
  101. }
  102. /**
  103. * Get all the menu items in a menu.
  104. *
  105. * @param string $name The name of the menu.
  106. *
  107. * @return array
  108. * @since 1.0
  109. */
  110. protected function &getMenu($name, $recurse = true)
  111. {
  112. $result = array();
  113. if (isset($this->lookupType[$name])) {
  114. $result = &$this->lookupType[$name];
  115. }
  116. return $result;
  117. }
  118. /**
  119. * Get an array of menu items.
  120. *
  121. * @param int $parentId An optional parent id by which to get items.
  122. * @return array
  123. * @since 1.0
  124. */
  125. public function &getItems($parentId = null)
  126. {
  127. // Initialiase variables.
  128. $result = array();
  129. if (!empty($parentId)) {
  130. if (isset($this->lookupParent[$parentId])) {
  131. $result = &$this->lookupParent($parentId);
  132. }
  133. }
  134. else if (!empty($this->content['menu'])) {
  135. $result = &$this->getMenu($this->content['menu'][0]);
  136. }
  137. return $result;
  138. }
  139. /**
  140. * Resolves the content for the menu item.
  141. *
  142. * @param object $item The menu item.
  143. *
  144. * @return object The resolved menu item as an object with properties title, html.
  145. * @since 1.0
  146. */
  147. public function resolve($item)
  148. {
  149. // Work on a clone of the item.
  150. $temp = clone $item;
  151. // Resolve the alias if applicable.
  152. if ($item->type == 'menulink' && $id = $item->params->get('menu_item')) {
  153. if (isset($this->items[$id])) {
  154. $temp = $this->items[$id];
  155. }
  156. }
  157. // Interogate the link.
  158. $option = ucfirst(strtolower(str_replace('com_', '', $temp->link->get('option'))));
  159. $view = ucfirst(strtolower($temp->link->get('view')));
  160. if ($option && $view) {
  161. // There is an option and a view. Push into a ComponentView handler.
  162. $method = 'resolve'.$option.$view;
  163. if (method_exists($this, $method)) {
  164. $resolved = $this->$method($temp);
  165. }
  166. else {
  167. $resolved = $this->resolveGeneric($item, '<p>Unknown option '.$option.' and view '.$view.'</p>');
  168. }
  169. }
  170. else if ($option) {
  171. // There is just an option. Push into a component handler.
  172. $method = 'resolve'.$option;
  173. if (method_exists($this, $method)) {
  174. $resolved = $this->$method($temp);
  175. }
  176. else {
  177. $resolved = $this->resolveGeneric($item, '<p>Unknown option '.$option.'</p>');
  178. }
  179. }
  180. else {
  181. // No idea what this is.
  182. $resolved = $this->resolveGeneric($item, '<p>Unknown content.</p>');
  183. }
  184. return $resolved;
  185. }
  186. /**
  187. * Resolves a content article into HTML.
  188. *
  189. * @param object $item The menu item.
  190. *
  191. * @return string The HTML reprsentation of the content.
  192. * @throws Exception
  193. */
  194. protected function resolveContentArticle($item)
  195. {
  196. // Get the generic content and work from there.
  197. $resolved = $this->resolveGeneric($item);
  198. // Get the article and assign content.
  199. if ($id = $item->link->get('id')) {
  200. $model = JModel::getInstance('com_content', 'ArtofPdfModel');
  201. $article = $model->getArticle($id);
  202. // Override the page title with the content title (but leave the bookmark title).
  203. $resolved->title = $article->title;
  204. // TODO Examine menu params for appropriate switches.
  205. $resolved->html = $article->introtext.$article->fulltext;
  206. $directives = ArtofPdfHelper::getDirectives($article->metakey);
  207. if (!empty($directives)) {
  208. if (in_array('nonewpage', $directives)) {
  209. $resolved->newpage = false;
  210. }
  211. else if (in_array('newpage', $directives)) {
  212. $resolved->newpage = false;
  213. }
  214. }
  215. }
  216. else {
  217. $resolved->html = JText::sprintf('COM_ARTOFPDF_ERROR_INVALID_CONTENT_MENU_ID');
  218. }
  219. return $resolved;
  220. }
  221. /**
  222. * Resolves a content category into HTML.
  223. *
  224. * @param object $item The menu item.
  225. *
  226. * @return string The HTML reprsentation of the content.
  227. * @throws Exception
  228. */
  229. protected function resolveContentCategory($item)
  230. {
  231. // Get the generic content and work from there.
  232. $resolved = $this->resolveGeneric($item);
  233. // Get the article and assign content.
  234. if ($id = $item->link->get('id')) {
  235. $model = JModel::getInstance('Com_content', 'ArtofPdfModel');
  236. $category = $model->getCategory($id);
  237. // Override the page title with the content title (but leave the bookmark title).
  238. $resolved->title = $category->title;
  239. // TODO Examine menu params for appropriate switches.
  240. $resolved->html = $category->description;
  241. // HACK!
  242. if ($item->level > 0) {
  243. $resolved->newpage = false;
  244. }
  245. // Now, add the articles as children to this item.
  246. if (empty($resolved->children)) {
  247. // Increment the menu level for the level of the article.
  248. $level = $item->level + 1;
  249. // Add each of the articles.
  250. foreach ($category->articles as $article)
  251. {
  252. $child = new JPdfContent($article->title, $level, $article->introtext.$article->fulltext);
  253. $child->newpage = false;
  254. $resolved->children[] = $child;
  255. }
  256. }
  257. else {
  258. // TODO What to do if there are menu children?
  259. }
  260. }
  261. else {
  262. $resolved->html = JText::sprintf('COM_ARTOFPDF_ERROR_INVALID_CONTENT_MENU_ID');
  263. }
  264. return $resolved;
  265. }
  266. /**
  267. * Resolves a generic menu item.
  268. *
  269. * @param object $item The menu item.
  270. * @param string $html The HTML content to use of the item.
  271. *
  272. * @return string The HTML reprsentation of the content.
  273. * @throws Exception
  274. */
  275. protected function resolveGeneric($item, $html = '')
  276. {
  277. $resolved = new JPdfContent($item->name, $item->level, $html);
  278. if (!empty($this->lookupParent[$item->id])) {
  279. $resolved->children = &$this->lookupParent[$item->id];
  280. }
  281. // Debug
  282. // echo "<br>Resolving: $item->id, ".count($resolved->children);
  283. return $resolved;
  284. }
  285. /**
  286. * Sets the PDF object in the model.
  287. *
  288. * @param object $content The PDF content definition.
  289. *
  290. * @return void
  291. * @since 1.6
  292. */
  293. public function setContent($content)
  294. {
  295. $this->content = $content;
  296. }
  297. /**
  298. * Recursively set the level in the menu tree.
  299. *
  300. * @param object $id The id of the current parent item.
  301. * @param int $level The current level.
  302. *
  303. * @return void
  304. * @since 1.0
  305. */
  306. protected function setLevel($id = 0, $level = 0)
  307. {
  308. // Second pass, compute the level of each item.
  309. if (!empty($this->lookupParent[$id])) {
  310. foreach ($this->lookupParent[$id] as &$item)
  311. {
  312. $item->level = $level;
  313. $this->setLevel($item->id, $level + 1);
  314. //echo "<br>ID: {$item->id}, Level: $level, Parent: {$item->parent}";
  315. }
  316. }
  317. }
  318. }