1 <?php
2 // PHP Layers Menu 3.2.0-rc (C) 2001-2004 Marco Pratesi - http://www.marcopratesi.it/
4 /**
5 * This file contains the code of the LayersMenuCommon class.
6 * @package PHPLayersMenu
7 */
9 /**
10 * This is the "common" class of the PHP Layers Menu library.
11 *
12 * You need to include PEAR.php and DB.php if (and only if) you want to use the DB support provided by ths class.
13 *
14 * @version 3.2.0-rc
15 * @package PHPLayersMenu
16 */
17 class LayersMenuCommon
18 {
20 /**
21 * The name of the package
22 * @access private
23 * @var string
24 */
25 var $_packageName;
26 /**
27 * The version of the package
28 * @access private
29 * @var string
30 */
31 var $version;
32 /**
33 * The copyright of the package
34 * @access private
35 * @var string
36 */
37 var $copyright;
38 /**
39 * The author of the package
40 * @access private
41 * @var string
42 */
43 var $author;
45 /**
46 * URL to be prepended to the menu hrefs
47 * @access private
48 * @var string
49 */
50 var $prependedUrl = '';
51 /**
52 * Do you want that code execution halts on error?
53 * @access private
54 * @var string
55 */
56 var $haltOnError = 'yes';
58 /**
59 * The base directory where the package is installed
60 * @access private
61 * @var string
62 */
63 var $dirroot;
64 /**
65 * The "libjs" directory of the package
66 * @access private
67 * @var string
68 */
69 var $libjsdir;
70 /**
71 * The directory where images related to the menu can be found
72 * @access private
73 * @var string
74 */
75 var $imgdir;
76 /**
77 * The http path corresponding to imgdir
78 * @access private
79 * @var string
80 */
81 var $imgwww;
82 /**
83 * The directory where icons of menu items can be found
84 * @access private
85 * @var string
86 */
87 var $icondir;
88 /**
89 * The http path corresponding to icondir
90 * @access private
91 * @var string
92 */
93 var $iconwww;
94 /**
95 * This array may contain width and height of all icons
96 * @access private
97 * @var integer
98 */
99 var $iconsize = array();
100 /**
101 * If this var is false, width and height of icons have to be detected; if this var is true, width and height of icons are not detected and are retrieved from the iconsize array
102 * @access private
103 * @var boolean
104 */
105 var $issetIconsize = false;
106 /**
107 * The directory where templates can be found
108 * @access private
109 * @var string
110 */
111 var $tpldir;
112 /**
113 * The string containing the menu structure
114 * @access private
115 * @var string
116 */
117 var $menuStructure;
119 /**
120 * It counts nodes for all menus
121 * @access private
122 * @var integer
123 */
124 var $_nodesCount;
125 /**
126 * A multi-dimensional array to store informations for each menu entry
127 * @access private
128 * @var array
129 */
130 var $tree;
131 /**
132 * A multi-dimensional array used only with the DB support; for each $menu_name, it stores the $cnt associated to each item id
133 *
134 * This array is needed for selection of the current item
135 * through the corresponding id (see the DB table structure)
136 * as, internally, items are stored, sorted and addressed in terms of $cnt
137 *
138 * @access private
139 * @var array
140 */
141 var $treecnt;
142 /**
143 * The maximum hierarchical level of menu items
144 * @access private
145 * @var integer
146 */
147 var $_maxLevel;
148 /**
149 * An array that counts the number of first level items for each menu
150 * @access private
151 * @var array
152 */
153 var $_firstLevelCnt;
154 /**
155 * An array containing the number identifying the first item of each menu
156 * @access private
157 * @var array
158 */
159 var $_firstItem;
160 /**
161 * An array containing the number identifying the last item of each menu
162 * @access private
163 * @var array
164 */
165 var $_lastItem;
167 /**
168 * Data Source Name: the connection string for PEAR DB
169 * @access private
170 * @var string
171 */
172 var $dsn = 'pgsql://dbuser:dbpass@dbhost/dbname';
173 /**
174 * DB connections are either persistent or not persistent
175 * @access private
176 * @var boolean
177 */
178 var $persistent = false;
179 /**
180 * Name of the table storing data describing the menu
181 * @access private
182 * @var string
183 */
184 var $tableName = 'phplayersmenu';
185 /**
186 * Name of the i18n table corresponding to $tableName
187 * @access private
188 * @var string
189 */
190 var $tableName_i18n = 'phplayersmenu_i18n';
191 /**
192 * Names of fields of the table storing data describing the menu
193 *
194 * default field names correspond to the same field names foreseen
195 * by the menu structure format
196 *
197 * @access private
198 * @var array
199 */
200 var $tableFields = array(
201 'id' => 'id',
202 'parent_id' => 'parent_id',
203 'text' => 'text',
204 'href' => 'href',
205 'title' => 'title',
206 'icon' => 'icon',
207 'target' => 'target',
208 'orderfield' => 'orderfield',
209 'expanded' => 'expanded'
210 );
211 /**
212 * Names of fields of the i18n table corresponding to $tableName
213 * @access private
214 * @var array
215 */
216 var $tableFields_i18n = array(
217 'language' => 'language',
218 'id' => 'id',
219 'text' => 'text',
220 'title' => 'title'
221 );
222 /**
223 * A temporary array to store data retrieved from the DB and to perform the depth-first search
224 * @access private
225 * @var array
226 */
227 var $_tmpArray = array();
229 /**
230 * The constructor method; it initializates the menu system
231 * @return void
232 */
233 function LayersMenuCommon()
234 {
235 $this->_packageName = 'PHP Layers Menu';
236 $this->version = '3.2.0-rc';
237 $this->copyright = '(C) 2001-2004';
238 $this->author = 'Marco Pratesi - http://www.marcopratesi.it/';
240 $this->prependedUrl = '';
242 $this->dirroot = './';
243 $this->libjsdir = './libjs/';
244 $this->imgdir = './menuimages/';
245 $this->imgwww = 'menuimages/';
246 $this->icondir = './menuicons/';
247 $this->iconwww = 'menuicons/';
248 $this->tpldir = './templates/';
249 $this->menuStructure = '';
250 $this->separator = '|';
252 $this->_nodesCount = 0;
253 $this->tree = array();
254 $this->treecnt = array();
255 $this->_maxLevel = array();
256 $this->_firstLevelCnt = array();
257 $this->_firstItem = array();
258 $this->_lastItem = array();
259 }
261 /**
262 * The method to set the prepended URL
263 * @access public
264 * @return boolean
265 */
266 function setPrependedUrl($prependedUrl)
267 {
268 // We do not perform any check
269 $this->prependedUrl = $prependedUrl;
270 return true;
271 }
273 /**
274 * The method to set the dirroot directory
275 * @access public
276 * @return boolean
277 */
278 function setDirrootCommon($dirroot)
279 {
280 if (!is_dir($dirroot)) {
281 $this->error("setDirroot: $dirroot is not a directory.");
282 return false;
283 }
284 if (substr($dirroot, -1) != '/') {
285 $dirroot .= '/';
286 }
287 $oldlength = strlen($this->dirroot);
288 $foobar = strpos($this->libjsdir, $this->dirroot);
289 if (!($foobar === false || $foobar != 0)) {
290 $this->libjsdir = $dirroot . substr($this->libjsdir, $oldlength);
291 }
292 $foobar = strpos($this->imgdir, $this->dirroot);
293 if (!($foobar === false || $foobar != 0)) {
294 $this->imgdir = $dirroot . substr($this->imgdir, $oldlength);
295 }
296 $foobar = strpos($this->icondir, $this->dirroot);
297 if (!($foobar === false || $foobar != 0)) {
298 $this->icondir = $dirroot . substr($this->icondir, $oldlength);
299 }
300 $foobar = strpos($this->tpldir, $this->dirroot);
301 if (!($foobar === false || $foobar != 0)) {
302 $this->tpldir = $dirroot . substr($this->tpldir, $oldlength);
303 }
304 $this->dirroot = $dirroot;
305 return true;
306 }
308 /**
309 * The method to set the libjsdir directory
310 * @access public
311 * @return boolean
312 */
313 function setLibjsdir($libjsdir)
314 {
315 if ($libjsdir != '' && substr($libjsdir, -1) != '/') {
316 $libjsdir .= '/';
317 }
318 if ($libjsdir == '' || substr($libjsdir, 0, 1) != '/') {
319 $foobar = strpos($libjsdir, $this->dirroot);
320 if ($foobar === false || $foobar != 0) {
321 $libjsdir = $this->dirroot . $libjsdir;
322 }
323 }
324 if (!is_dir($libjsdir)) {
325 $this->error("setLibjsdir: $libjsdir is not a directory.");
326 return false;
327 }
328 $this->libjsdir = $libjsdir;
329 return true;
330 }
332 /**
333 * The method to set the imgdir directory
334 * @access public
335 * @return boolean
336 */
337 function setImgdir($imgdir)
338 {
339 if ($imgdir != '' && substr($imgdir, -1) != '/') {
340 $imgdir .= '/';
341 }
342 if ($imgdir == '' || substr($imgdir, 0, 1) != '/') {
343 $foobar = strpos($imgdir, $this->dirroot);
344 if ($foobar === false || $foobar != 0) {
345 $imgdir = $this->dirroot . $imgdir;
346 }
347 }
348 if (!is_dir($imgdir)) {
349 $this->error("setImgdir: $imgdir is not a directory.");
350 return false;
351 }
352 $this->imgdir = $imgdir;
353 return true;
354 }
356 /**
357 * The method to set imgwww
358 * @access public
359 * @return void
360 */
361 function setImgwww($imgwww)
362 {
363 if ($imgwww != '' && substr($imgwww, -1) != '/') {
364 $imgwww .= '/';
365 }
366 $this->imgwww = $imgwww;
367 }
369 /**
370 * The method to set the icondir directory
371 * @access public
372 * @return boolean
373 */
374 function setIcondir($icondir)
375 {
376 if ($icondir != '' && substr($icondir, -1) != '/') {
377 $icondir .= '/';
378 }
379 if ($icondir == '' || substr($icondir, 0, 1) != '/') {
380 $foobar = strpos($icondir, $this->dirroot);
381 if ($foobar === false || $foobar != 0) {
382 $icondir = $this->dirroot . $icondir;
383 }
384 }
385 if (!is_dir($icondir)) {
386 $this->error("setIcondir: $icondir is not a directory.");
387 return false;
388 }
389 $this->icondir = $icondir;
390 return true;
391 }
393 /**
394 * The method to set iconwww
395 * @access public
396 * @return void
397 */
398 function setIconwww($iconwww)
399 {
400 if ($iconwww != '' && substr($iconwww, -1) != '/') {
401 $iconwww .= '/';
402 }
403 $this->iconwww = $iconwww;
404 }
406 /**
407 * The method to set the iconsize array
408 * @access public
409 * @return void
410 */
411 function setIconsize($width, $height)
412 {
413 $this->iconsize['width'] = ($width == (int) $width) ? $width : 0;
414 $this->iconsize['height'] = ($height == (int) $height) ? $height : 0;
415 $this->issetIconsize = true;
416 }
418 /**
419 * The method to unset the iconsize array
420 * @access public
421 * @return void
422 */
423 function unsetIconsize()
424 {
425 unset($this->iconsize['width']);
426 unset($this->iconsize['height']);
427 $this->issetIconsize = false;
428 }
430 /**
431 * The method to set the tpldir directory
432 * @access public
433 * @return boolean
434 */
435 function setTpldirCommon($tpldir)
436 {
437 if ($tpldir != '' && substr($tpldir, -1) != '/') {
438 $tpldir .= '/';
439 }
440 if ($tpldir == '' || substr($tpldir, 0, 1) != '/') {
441 $foobar = strpos($tpldir, $this->dirroot);
442 if ($foobar === false || $foobar != 0) {
443 $tpldir = $this->dirroot . $tpldir;
444 }
445 }
446 if (!is_dir($tpldir)) {
447 $this->error("setTpldir: $tpldir is not a directory.");
448 return false;
449 }
450 $this->tpldir = $tpldir;
451 return true;
452 }
454 /**
455 * The method to read the menu structure from a file
456 * @access public
457 * @param string $tree_file the menu structure file
458 * @return boolean
459 */
460 function setMenuStructureFile($tree_file)
461 {
462 if (!($fd = fopen($tree_file, 'r'))) {
463 $this->error("setMenuStructureFile: unable to open file $tree_file.");
464 return false;
465 }
466 $this->menuStructure = '';
467 while ($buffer = fgets($fd, 4096)) {
468 $buffer = ereg_replace(chr(13), '', $buffer); // Microsoft Stupidity Suppression
469 $this->menuStructure .= $buffer;
470 }
471 fclose($fd);
472 if ($this->menuStructure == '') {
473 $this->error("setMenuStructureFile: $tree_file is empty.");
474 return false;
475 }
476 return true;
477 }
479 /**
480 * The method to set the menu structure passing it through a string
481 * @access public
482 * @param string $tree_string the menu structure string
483 * @return boolean
484 */
485 function setMenuStructureString($tree_string)
486 {
487 $this->menuStructure = ereg_replace(chr(13), '', $tree_string); // Microsoft Stupidity Suppression
488 if ($this->menuStructure == '') {
489 $this->error('setMenuStructureString: empty string.');
490 return false;
491 }
492 return true;
493 }
495 /**
496 * The method to set the value of separator
497 * @access public
498 * @return void
499 */
500 function setSeparator($separator)
501 {
502 $this->separator = $separator;
503 }
505 /**
506 * The method to set parameters for the DB connection
507 * @access public
508 * @param string $dns Data Source Name: the connection string for PEAR DB
509 * @param bool $persistent DB connections are either persistent or not persistent
510 * @return boolean
511 */
512 function setDBConnParms($dsn, $persistent=false)
513 {
514 if (!is_string($dsn)) {
515 $this->error('initdb: $dsn is not an string.');
516 return false;
517 }
518 if (!is_bool($persistent)) {
519 $this->error('initdb: $persistent is not a boolean.');
520 return false;
521 }
522 $this->dsn = $dsn;
523 $this->persistent = $persistent;
524 return true;
525 }
527 /**
528 * The method to set the name of the table storing data describing the menu
529 * @access public
530 * @param string
531 * @return boolean
532 */
533 function setTableName($tableName)
534 {
535 if (!is_string($tableName)) {
536 $this->error('setTableName: $tableName is not a string.');
537 return false;
538 }
539 $this->tableName = $tableName;
540 return true;
541 }
543 /**
544 * The method to set the name of the i18n table corresponding to $tableName
545 * @access public
546 * @param string
547 * @return boolean
548 */
549 function setTableName_i18n($tableName_i18n)
550 {
551 if (!is_string($tableName_i18n)) {
552 $this->error('setTableName_i18n: $tableName_i18n is not a string.');
553 return false;
554 }
555 $this->tableName_i18n = $tableName_i18n;
556 return true;
557 }
559 /**
560 * The method to set names of fields of the table storing data describing the menu
561 * @access public
562 * @param array
563 * @return boolean
564 */
565 function setTableFields($tableFields)
566 {
567 if (!is_array($tableFields)) {
568 $this->error('setTableFields: $tableFields is not an array.');
569 return false;
570 }
571 if (count($tableFields) == 0) {
572 $this->error('setTableFields: $tableFields is a zero-length array.');
573 return false;
574 }
575 reset ($tableFields);
576 while (list($key, $value) = each($tableFields)) {
577 $this->tableFields[$key] = ($value == '') ? "''" : $value;
578 }
579 return true;
580 }
582 /**
583 * The method to set names of fields of the i18n table corresponding to $tableName
584 * @access public
585 * @param array
586 * @return boolean
587 */
588 function setTableFields_i18n($tableFields_i18n)
589 {
590 if (!is_array($tableFields_i18n)) {
591 $this->error('setTableFields_i18n: $tableFields_i18n is not an array.');
592 return false;
593 }
594 if (count($tableFields_i18n) == 0) {
595 $this->error('setTableFields_i18n: $tableFields_i18n is a zero-length array.');
596 return false;
597 }
598 reset ($tableFields_i18n);
599 while (list($key, $value) = each($tableFields_i18n)) {
600 $this->tableFields_i18n[$key] = ($value == '') ? "''" : $value;
601 }
602 return true;
603 }
605 /**
606 * The method to parse the current menu structure and correspondingly update related variables
607 * @access public
608 * @param string $menu_name the name to be attributed to the menu
609 * whose structure has to be parsed
610 * @return void
611 */
612 function parseStructureForMenu(
613 $menu_name = '' // non consistent default...
614 )
615 {
616 $this->_maxLevel[$menu_name] = 0;
617 $this->_firstLevelCnt[$menu_name] = 0;
618 $this->_firstItem[$menu_name] = $this->_nodesCount + 1;
619 $cnt = $this->_firstItem[$menu_name];
620 $menuStructure = $this->menuStructure;
622 /* *********************************************** */
623 /* Partially based on a piece of code taken from */
624 /* TreeMenu 1.1 - Bjorge Dijkstra (bjorge@gmx.net) */
625 /* *********************************************** */
627 while ($menuStructure != '') {
628 $before_cr = strcspn($menuStructure, "\n");
629 $buffer = substr($menuStructure, 0, $before_cr);
630 $menuStructure = substr($menuStructure, $before_cr+1);
631 if (substr($buffer, 0, 1) == '#') {
632 continue; // commented item line...
633 }
634 $tmp = rtrim($buffer);
635 $node = explode($this->separator, $tmp);
636 for ($i=count($node); $i<=6; $i++) {
637 $node[$i] = '';
638 }
639 $this->tree[$cnt]['level'] = strlen($node[0]);
640 $this->tree[$cnt]['text'] = $node[1];
641 $this->tree[$cnt]['href'] = $node[2];
642 $this->tree[$cnt]['title'] = $node[3];
643 $this->tree[$cnt]['icon'] = $node[4];
644 $this->tree[$cnt]['target'] = $node[5];
645 $this->tree[$cnt]['expanded'] = $node[6];
646 $cnt++;
647 }
649 /* *********************************************** */
651 $this->_lastItem[$menu_name] = count($this->tree);
652 $this->_nodesCount = $this->_lastItem[$menu_name];
653 $this->tree[$this->_lastItem[$menu_name]+1]['level'] = 0;
654 $this->_postParse($menu_name);
655 }
657 /**
658 * The method to parse the current menu table and correspondingly update related variables
659 * @access public
660 * @param string $menu_name the name to be attributed to the menu
661 * whose structure has to be parsed
662 * @param string $language i18n language; either omit it or pass
663 * an empty string ('') if you do not want to use any i18n table
664 * @return void
665 */
666 function scanTableForMenu(
667 $menu_name = '', // non consistent default...
668 $language = ''
669 )
670 {
671 $this->_maxLevel[$menu_name] = 0;
672 $this->_firstLevelCnt[$menu_name] = 0;
673 unset($this->tree[$this->_nodesCount+1]);
674 $this->_firstItem[$menu_name] = $this->_nodesCount + 1;
675 /* BEGIN BENCHMARK CODE
676 $time_start = $this->_getmicrotime();
677 /* END BENCHMARK CODE */
678 $db = DB::connect($this->dsn, $this->persistent);
679 if (DB::isError($db)) {
680 $this->error('scanTableForMenu: ' . $db->getMessage());
681 }
682 $dbresult = $db->query('
683 SELECT ' .
684 $this->tableFields['id'] . ' AS id, ' .
685 $this->tableFields['parent_id'] . ' AS parent_id, ' .
686 $this->tableFields['text'] . ' AS text, ' .
687 $this->tableFields['href'] . ' AS href, ' .
688 $this->tableFields['title'] . ' AS title, ' .
689 $this->tableFields['icon'] . ' AS icon, ' .
690 $this->tableFields['target'] . ' AS target, ' .
691 $this->tableFields['expanded'] . ' AS expanded
692 FROM ' . $this->tableName . '
693 WHERE ' . $this->tableFields['id'] . ' <> 1
694 ORDER BY ' . $this->tableFields['orderfield'] . ', ' . $this->tableFields['text'] . ' ASC
695 ');
696 $this->_tmpArray = array();
697 while ($dbresult->fetchInto($row, DB_FETCHMODE_ASSOC)) {
698 $this->_tmpArray[$row['id']]['parent_id'] = $row['parent_id'];
699 $this->_tmpArray[$row['id']]['text'] = $row['text'];
700 $this->_tmpArray[$row['id']]['href'] = $row['href'];
701 $this->_tmpArray[$row['id']]['title'] = $row['title'];
702 $this->_tmpArray[$row['id']]['icon'] = $row['icon'];
703 $this->_tmpArray[$row['id']]['target'] = $row['target'];
704 $this->_tmpArray[$row['id']]['expanded'] = $row['expanded'];
705 }
706 if ($language != '') {
707 $dbresult = $db->query('
708 SELECT ' .
709 $this->tableFields_i18n['id'] . ' AS id, ' .
710 $this->tableFields_i18n['text'] . ' AS text, ' .
711 $this->tableFields_i18n['title'] . ' AS title
712 FROM ' . $this->tableName_i18n . '
713 WHERE ' . $this->tableFields_i18n['id'] . ' <> 1
714 AND ' . $this->tableFields_i18n['language'] . ' = ' . "'$language'" . '
715 ');
716 while ($dbresult->fetchInto($row, DB_FETCHMODE_ASSOC)) {
717 if (isset($this->_tmpArray[$row['id']])) {
718 $this->_tmpArray[$row['id']]['text'] = $row['text'];
719 $this->_tmpArray[$row['id']]['title'] = $row['title'];
720 }
721 }
722 }
723 unset($dbresult);
724 unset($row);
725 $this->_depthFirstSearch($menu_name, $this->_tmpArray, 1, 1);
726 /* BEGIN BENCHMARK CODE
727 $time_end = $this->_getmicrotime();
728 $time = $time_end - $time_start;
729 print "TIME ELAPSED = $time\n<br>";
730 /* END BENCHMARK CODE */
731 $this->_lastItem[$menu_name] = count($this->tree);
732 $this->_nodesCount = $this->_lastItem[$menu_name];
733 $this->tree[$this->_lastItem[$menu_name]+1]['level'] = 0;
734 $this->_postParse($menu_name);
735 }
737 function _getmicrotime()
738 {
739 list($usec, $sec) = explode(' ', microtime());
740 return ((float) $usec + (float) $sec);
741 }
743 /**
744 * Recursive method to perform the depth-first search of the tree data taken from the current menu table
745 * @access private
746 * @param string $menu_name the name to be attributed to the menu
747 * whose structure has to be parsed
748 * @param array $tmpArray the temporary array that stores data to perform
749 * the depth-first search
750 * @param integer $parent_id id of the item whose children have
751 * to be searched for
752 * @param integer $level the hierarchical level of children to be searched for
753 * @return void
754 */
755 function _depthFirstSearch($menu_name, $tmpArray, $parent_id=1, $level=1)
756 {
757 reset ($tmpArray);
758 while (list($id, $foobar) = each($tmpArray)) {
759 if ($foobar['parent_id'] == $parent_id) {
760 unset($tmpArray[$id]);
761 unset($this->_tmpArray[$id]);
762 $cnt = count($this->tree) + 1;
763 $this->tree[$cnt]['level'] = $level;
764 $this->tree[$cnt]['text'] = $foobar['text'];
765 $this->tree[$cnt]['href'] = $foobar['href'];
766 $this->tree[$cnt]['title'] = $foobar['title'];
767 $this->tree[$cnt]['icon'] = $foobar['icon'];
768 $this->tree[$cnt]['target'] = $foobar['target'];
769 $this->tree[$cnt]['expanded'] = $foobar['expanded'];
770 $this->treecnt[$menu_name][$id] = $cnt;
771 unset($foobar);
772 if ($id != $parent_id) {
773 $this->_depthFirstSearch($menu_name, $this->_tmpArray, $id, $level+1);
774 }
775 }
776 }
777 }
779 /**
780 * A method providing parsing needed after both file/string parsing and DB table parsing
781 * @access private
782 * @param string $menu_name the name of the menu for which the parsing
783 * has to be performed
784 * @return void
785 */
786 function _postParse(
787 $menu_name = '' // non consistent default...
788 )
789 {
790 for ($cnt=$this->_firstItem[$menu_name]; $cnt<=$this->_lastItem[$menu_name]; $cnt++) { // this counter scans all nodes of the new menu
791 $this->tree[$cnt]['child_of_root_node'] = ($this->tree[$cnt]['level'] == 1);
792 $this->tree[$cnt]['parsed_text'] = stripslashes($this->tree[$cnt]['text']);
793 $this->tree[$cnt]['parsed_href'] = (ereg_replace(' ', '', $this->tree[$cnt]['href']) == '') ? '#' : $this->prependedUrl . $this->tree[$cnt]['href'];
794 $this->tree[$cnt]['parsed_title'] = ($this->tree[$cnt]['title'] == '') ? '' : ' title="' . stripslashes($this->tree[$cnt]['title']) . '"';
795 $fooimg = $this->icondir . $this->tree[$cnt]['icon'];
796 if ($this->tree[$cnt]['icon'] != '' && (substr($this->tree[$cnt]['icon'], 0, 7) == 'http://' || substr($this->tree[$cnt]['icon'], 0, 8) == 'https://')) {
797 $this->tree[$cnt]['parsed_icon'] = $this->tree[$cnt]['icon'];
798 if ($this->issetIconsize) {
799 $this->tree[$cnt]['iconwidth'] = $this->iconsize['width'];
800 $this->tree[$cnt]['iconheight'] = $this->iconsize['height'];
801 } else {
802 $foobar = getimagesize($this->tree[$cnt]['icon']);
803 $this->tree[$cnt]['iconwidth'] = $foobar[0];
804 $this->tree[$cnt]['iconheight'] = $foobar[1];
805 }
806 } elseif ($this->tree[$cnt]['icon'] != '' && file_exists($fooimg)) {
807 $this->tree[$cnt]['parsed_icon'] = $this->iconwww . $this->tree[$cnt]['icon'];
808 if ($this->issetIconsize) {
809 $this->tree[$cnt]['iconwidth'] = $this->iconsize['width'];
810 $this->tree[$cnt]['iconheight'] = $this->iconsize['height'];
811 } else {
812 $foobar = getimagesize($fooimg);
813 $this->tree[$cnt]['iconwidth'] = $foobar[0];
814 $this->tree[$cnt]['iconheight'] = $foobar[1];
815 }
816 } else {
817 $this->tree[$cnt]['parsed_icon'] = '';
818 }
819 $this->tree[$cnt]['parsed_target'] = ($this->tree[$cnt]['target'] == '') ? '' : ' target="' . $this->tree[$cnt]['target'] . '"';
820 // $this->tree[$cnt]['expanded'] = ($this->tree[$cnt]['expanded'] == '') ? 0 : $this->tree[$cnt]['expanded'];
821 $this->_maxLevel[$menu_name] = max($this->_maxLevel[$menu_name], $this->tree[$cnt]['level']);
822 if ($this->tree[$cnt]['level'] == 1) {
823 $this->_firstLevelCnt[$menu_name]++;
824 }
825 }
826 }
828 /**
829 * A method to replace strings in all URLs (hrefs) of a menu
830 * @access public
831 * @param string $menu_name the name of the menu for which the replacement
832 * has to be performed
833 * @param string $string the string to be replaced
834 * @param string $value the replacement string
835 * @return void
836 */
837 function replaceStringInUrls($menu_name, $string, $value)
838 {
839 for ($cnt=$this->_firstItem[$menu_name]; $cnt<=$this->_lastItem[$menu_name]; $cnt++) { // this counter scans all nodes of the new menu
840 $this->tree[$cnt]['parsed_href'] = str_replace($string, $value, $this->tree[$cnt]['parsed_href']);
841 }
842 }
844 /**
845 * A method to set the same target for all links of a menu
846 * @access public
847 * @param string $menu_name the name of the menu for which the targets
848 * have to be set
849 * @param string $target the target to be set for all links
850 * of the $menu_name menu
851 * @return void
852 */
853 function setLinksTargets($menu_name, $target)
854 {
855 for ($cnt=$this->_firstItem[$menu_name]; $cnt<=$this->_lastItem[$menu_name]; $cnt++) { // this counter scans all nodes of the new menu
856 $this->tree[$cnt]['parsed_target'] = ' target="' . $target . '"';
857 }
858 }
860 /**
861 * A method to select the current item of $menu_name in terms of $cnt, i.e., very likely, in terms of its line number in the corresponding menu structure file (excluding from the count commented out lines, if any)
862 * @access public
863 * @param string $menu_name the name of the menu for which the current item
864 * has to be selected
865 * @param integer $count the line number of the current item
866 * in the corresponding menu structure file
867 * (excluding from the count commented out lines, if any)
868 * @return void
869 */
870 function setSelectedItemByCount($menu_name, $count)
871 {
872 if ($count < 1) {
873 $this->error("setSelectedItemByCount: the \$count argument is $count, but \$count can not be lower than 1");
874 return;
875 }
876 if ($count > $this->_lastItem[$menu_name] - $this->_firstItem[$menu_name] + 1) {
877 $this->error("setSelectedItemByCount: the \$count argument is $count and is larger than the number of items of the '$menu_name' menu");
878 return;
879 }
880 $cnt = $this->_firstItem[$menu_name] + $count - 1;
881 $this->tree[$cnt]['selected'] = true;
882 }
884 /**
885 * A method to select the current item of $menu_name in terms of the corresponding id (see the DB table structure); obviously, this method can be used only together with the DB support
886 * @access public
887 * @param string $menu_name the name of the menu for which the current item
888 * has to be selected
889 * @param integer $id the id of the current item in the corresponding DB table
890 * @return void
891 */
892 function setSelectedItemById($menu_name, $id)
893 {
894 if (!isset($this->treecnt[$menu_name][$id])) {
895 $this->error("setSelectedItemById: there is not any item with \$id = $id in the '$menu_name' menu");
896 return;
897 }
898 $cnt = $this->treecnt[$menu_name][$id];
899 $this->tree[$cnt]['selected'] = true;
900 }
902 /**
903 * A method to select the current item of $menu_name specifying a string that occurs in the current URL
904 * @access public
905 * @param string $menu_name the name of the menu for which the current item
906 * has to be selected
907 * @param string $url a string that occurs in the current URL
908 * @return void
909 */
910 function setSelectedItemByUrl($menu_name, $url)
911 {
912 for ($cnt=$this->_firstItem[$menu_name]; $cnt<=$this->_lastItem[$menu_name]; $cnt++) { // this counter scans all nodes of the new menu
913 if (!(strpos($this->tree[$cnt]['parsed_href'], $url) === false)) {
914 $this->tree[$cnt]['selected'] = true;
915 break;
916 }
917 }
918 }
920 /**
921 * A method to select the current item of $menu_name specifying a regular expression that matches (a substring of) the current URL; just the same as the setSelectedItemByUrl() method, but using eregi() instead of strpos()
922 * @access public
923 * @param string $menu_name the name of the menu for which the current item
924 * has to be selected
925 * @param string $url_eregi the regular expression that matches
926 * (a substring of) the current URL
927 * @return void
928 */
929 function setSelectedItemByUrlEregi($menu_name, $url_eregi)
930 {
931 for ($cnt=$this->_firstItem[$menu_name]; $cnt<=$this->_lastItem[$menu_name]; $cnt++) { // this counter scans all nodes of the new menu
932 if (eregi($url_eregi, $this->tree[$cnt]['parsed_href'])) {
933 $this->tree[$cnt]['selected'] = true;
934 break;
935 }
936 }
937 }
939 /**
940 * Method to handle errors
941 * @access private
942 * @param string $errormsg the error message
943 * @return void
944 */
945 function error($errormsg)
946 {
947 print "<b>LayersMenu Error:</b> $errormsg<br />\n";
948 if ($this->haltOnError == 'yes') {
949 die("<b>Halted.</b><br />\n");
950 }
951 }
953 } /* END OF CLASS */
955 ?>