the-art-of-joomla-archive /pdf/code/trunk/administrator/components/com_artofpdf/models/com_menus.php

Language PHP Lines 374
MD5 Hash e3820daf6c3ec03cfb4e30ea74171513 Estimated Cost $4,542 (why?)
Repository https://bitbucket.org/eddieajau/the-art-of-joomla-archive View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?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}";
			}
		}
	}
}
Back to Top