Login   Register  
Icontem

File: sidemenu.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Vallo Reima  >  JS side menu handler  >  sidemenu.php  >  Download  
File: sidemenu.php
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: JS side menu handler
Manage hierarchical menus of HTML content sections
Author: By
Last change:
Date: 2013-08-25 05:33
Size: 2,922 bytes
 

Contents

Class file image Download
<?php

/*
 * sidemanu generator class
 *
 * @package     System
 * @author      Vallo Reima
 * @copyright   (C)2012
 */

class SideMenu {

  private $dom;     /* DOM object */
  private $xpt;     /* XPath object */

  public function __construct($cfg, Texts $txt)
  /*
   * load configuration
   * in:  cfg -- config filename
   *      txt -- text's object
   *      
   */ {
    $this->dom = new DOMDocument();
    $this->dom->preserveWhiteSpace = false;
    $this->dom->encoding = mb_strtolower(mb_internal_encoding());
    if (@$this->dom->load("$cfg.xml")) {
      $this->xpt = new DomXPath($this->dom);
      $this->Names('sct', $txt->sct);
    } else {
      $txt->_err = $txt->ecfg;
    }
  }

  private function Names($pth, $nms)
  /*
   * save section names in the name attribute
   * in:  pth - section path
   *      nms - names array
   */ {
    $nodes = $this->xpt->query($pth)->item(0);
    foreach ($nodes->childNodes as $node) {
      $sct = $pth . '/' . $node->nodeName;
      if ($node->nodeType == XML_ELEMENT_NODE) {
        $attr = $this->dom->createAttribute('name');
        $node->appendChild($attr);
        $txt = $this->dom->createTextNode($nms[$node->nodeName]);
        $attr->appendChild($txt);
      }
      if ($node->hasChildNodes()) {
        $this->Names($sct, $nms);
      }
    }
  }

  public function Menu($ops)
  /*
   * form the menu htm
   * in:  ops -- forming options
   * out: htm
   */ {
    $htm = '';
    $this->Create('sct', 'sct', $htm, $ops);
    return $htm;
  }

  private function Create($pth, $id, &$htm, $ops)
  /*
   * create menu recursively
   * in:  pth - current section path
   *      id - ul tag id
   *      htm - formed htm string
   *      ops -- options - url, icon source
   */ {
    $htm .= '<ul id="' . $id . '">' . PHP_EOL;
    $sct = $this->xpt->query($pth)->item(0);
    foreach ($sct->childNodes as $node) {
      $key = $node->nodeName;
      $val = $this->mb_str_replace(' ', '&nbsp;', $node->getAttribute('name'));
      $ref = $ops['url'] . $this->mb_str_replace('sct', '', $pth) . '/' . $key;
      if ($node->nodeType == XML_ELEMENT_NODE &&
              $node->hasChildNodes() && $node->childNodes->item(0)->nodeType == XML_ELEMENT_NODE) {
        $nme = $this->mb_str_replace('/', '_', $pth) . '_' . $key;
        $htm .= '<li><img alt="" src="' . $ops['img'] . '" id="' . $nme . '_mrk"/>';
        $htm .= '<a href="' . $ref . '" name="' . $nme . '">' . $val . '</a>' . PHP_EOL;
        $this->Create($pth . '/' . $key, $nme, $htm, $ops);
      } else if ($node->nodeType == XML_ELEMENT_NODE) {
        $htm .= '<li><a href="' . $ref . '">' . $val . '</a></li>' . PHP_EOL;
      }
    }
    $htm .= '</ul>' . PHP_EOL;
    if (mb_strpos($pth, '/')) {
      $htm .= '</li>' . PHP_EOL;
    }
  }

  private function mb_str_replace($needle, $replacement, $haystack) {
    return implode($replacement, mb_split($needle, $haystack));
  }

}

?>