<?php
class blogMenuItem {
  /**
   * Text of menu item
   * @var string
   */
  public $text;

  /**
   * url of menu item
   * @var string;
   */
  public $link;

  /**
   * Array of module.action pattern
   *
   * @var array
   */
  public $allow = array();

  /**
   * Array of module.action pattern
   *
   * @var array
   */
  public $deny = array();

  /**
   *
   * @var string
   */
  public $show = '';

  /**
   * Enter description here...
   *
   * @var string
   */
  public $hide = '';

  /**
   * Enter description here...
   *
   * @var string
   */
  public $key = '';

  /**
   * Child item of items
   *
   * @var unknown_type
   */
  public $child = array();

  public $config = array();


  public static $default_cridentials = array(
    'user_only' => array('allow'=>array('user')),
    'admin_only' => array('allow'=>array('blogadmin')),
  );

  public static $default_show_options = array(
    'home_only' => array('hide'=>'*.*', 'show'=>'home.*'),
    'entry_only' => array('hide'=>'*.*', 'show'=>'entry.*'),
  );

  /**
   * add param to item
   *
   * @param unknown_type $config
   * @param unknown_type $param
   * @param unknown_type $var
   * @param bool $clean Clean string (\r, \n character)
   */
  function addParam(&$config, $param, &$var, $clean=false) {
    if ($val = @$config[$param]) {
      if ($clean) {
        $val = str_replace(array("\r","\n"), ' ', $val);
      }
      $var = $val;
      unset($config[$param]);
    }
  }

  /**
   * Enter description here...
   *
   * @param unknown_type $config
   */
  public function __construct($key, $config) {
    $this->key = $key;
    $this->addParam($config, 'text', $this->text, true);
    $this->addParam($config, 'link', $this->link, true);
    $this->addParam($config, 'allow', $this->allow, true);
    $this->addParam($config, 'deny', $this->deny, true);
    $this->addParam($config, 'show', $this->show, true);
    $this->addParam($config, 'hide', $this->hide, true);

    if ($val = @$config['child']) {
      foreach ($val as $key=>$item) {

        // Dam bao key la xau ky tu (sua loi key trong)
        if (is_numeric($key)) {
          for ($key = 0; ; $key++) {
            if (!key_exists("user$key", $val)) {
              $key = "user$key";
              break;
            }
          }
        }

        $this->child[$key] = new blogMenuItem($key, $item);
      }
      unset($config['child']);
    }
    $this->config = $config;
  }

  /**
   * Convert
   *
   */
  public function __toArray() {
    $ret = array();
    $ret['text'] = $this->text;
    $ret['link'] = $this->link;
    if (count($this->allow)) $ret['allow'] = $this->allow;
    if (count($this->deny)) $ret['deny'] = $this->deny;
    if ($this->show) $ret['show'] = $this->show;
    if ($this->hide) $ret['hide'] = $this->hide;

    $ret = array_merge($ret, $this->config);
    return $ret;
  }

  public function addAllowCridential($cridential) {
    if (false===array_search($cridential, $this->allow)) return;
    $this->allow = array_merge($this->allow, $cridential);
  }

  public function removeAllowCridential($cridential) {
    $i = array_search($cridential, $this->allow);
    if (false!==$i) {
      array_splice($this->allow, $i, 1);
    }
  }

  public function setShowOption($show) {
    $this->show = $show;
  }

  public function setHideOption($hide) {
    $this->hide = $hide;
  }

  public function getXML() {
    $xml = "<item id='$this->key' text='".htmlentities($this->text)."'>";
    foreach ($this->child as $key=>$item) {
      $xml .= $item->getXML();
    }
    $xml .= '</item>';
    return $xml;
  }

  protected static function buildJavascriptArray($ar) {
    if (is_array($ar)) {
      return '[' . join(',', $ar) . ']';
    } else {
      return $ar;
    }
  }

  public function getJavascriptObject() {
    $obj = '{';
    $obj .= "key:'$this->key'";
    $obj .= ",text:'" . htmlspecialchars($this->text, ENT_QUOTES) . "'";
    $obj .= ",link:'" . htmlspecialchars($this->link) . "'";

    if ($this->allow) $obj .= ",allow:'" . htmlspecialchars(self::buildJavascriptArray($this->allow)) . "'";
    if ($this->deny) $obj .= ",deny:'" . htmlspecialchars(self::buildJavascriptArray($this->hide)) . "'";
    if ($this->show) $obj .= ",show:'" . htmlspecialchars($this->show) . "'";
    if ($this->hide) $obj .= ",hide:'" . htmlspecialchars($this->hide) . "'";

    if (count($this->child)>0) {
      $obj .= ',child: {';
      $isFirst = true;
      foreach ($this->child as $key=>$item) {
        $obj .= ($isFirst?'':',') . $key . ':' . $item->getJavascriptObject();
        $isFirst = false;
      }
      $obj .= '}';
    }
    $obj .= '}';
    return $obj;
  }
}
