<?php

$lpo_menu = array();

foreach($this->list as $k1 => $v1) {
  $a = array(
    'uri'      => '/index/lpo/id/' . $v1->id,
    'text'     => '<img src="/image/lpo.png" />&nbsp;'.$v1->name,
    'attr' => array(),
    'ul'   => array(),
  );
  $a['attr']['class'][] = $v1->status;
  if(
    (
      $this->action == 'lpo'                  || 
      $this->action == 'lpo-update'           ||
      $this->action == 'lpo-update-submit'    ||
      $this->action == 'lpo-delete'           ||
      $this->action == 'lpo-condition-create'
    ) && $this->lpo->id == $v1->id
  ) {
    $a['attr']['class'][] = 'selected';
  }

  if($this->lpo && ($v1->id == $this->lpo->id)) {
    foreach(
      $v1->findDependentRowset(
        'Condition',
        null,
        $v1->getTable()->select()->where('status != ?', 'deleted')->order('priority')
      ) as $k2 => $v2
    ) {
      $b = array(
        'uri'  => '/index/lpo-condition/id/' . $v2->id,
        'text' => '<img src="/image/condition.png" />&nbsp;'.$v2->getName(),
        'attr' => array(),
        'ul'   => array(),
      );
      $b['attr']['class'][] = $v2->status;
      if(
        (
          $this->action == 'lpo-condition'               ||
          $this->action == 'lpo-condition-create-submit' ||
          $this->action == 'lpo-condition-update'        ||
          $this->action == 'lpo-condition-update-submit' ||
          $this->action == 'lpo-condition-delete'        ||
          $this->action == 'lpo-content-create'          ||
          $this->action == 'lpo-content-create-submit'
        ) &&
        $this->condition->id == $v2->id
      ) {
        $b['attr']['class'][] = 'selected';
      }

      if($this->condition && ($v2->id == $this->condition->id)) {
        foreach(
          $v2->findDependentRowset(
            'Content',
            null,
            $v2->getTable()->select()->where('status != ?', 'deleted')->order('name')
          ) as $k3 => $v3
        ) {
          $c = array(
            'uri'  => '/index/lpo-content/id/' . $v3->id,
            'text' => '<img src="/image/content.png" />&nbsp;'.$v3->name,
            'attr' => array(),
            'ul'   => array(),
          );
          $c['attr']['class'][] = $v3->status;
          if(
            (
              $this->action == 'lpo-content'               ||
              $this->action == 'lpo-content-update'        ||
              $this->action == 'lpo-content-update-submit' ||
              $this->action == 'lpo-content-delete'
            ) &&
            $this->content->id == $v3->id
          ) {
            $c['attr']['class'][] = 'selected';
          }

          $b['ul'][] = $c;
        }
      }

      $a['ul'][] = $b;
    }
  }

  $lpo_menu[] = $a;
}

$ul = array();

$li = array(
  'uri'      => '/index/lpo-create',
  'text'     => '<img src="/image/create.png" />&nbsp;新規 LPO ページ登録',
  'ul'       => array(),
  'attr'     => array(),
);
$li['attr']['class'][] = 'create';
if($this->action == 'lpo-create') {
  $li['attr']['class'][] = 'selected';
}
$ul[] = $li;

$li = array(
  'uri'      => '/index/index',
  'text'     => '<img src="/image/graph.png" />&nbsp;LPO グラフ一覧',
  'ul'       => $lpo_menu,
  'attr'     => array(),
);
$li['attr']['class'][] = 'index';
if($this->action == 'index') {
  $li['attr']['class'][] = 'selected';
}
$ul[] = $li;

#$ul = array_merge($ul, $lpo_menu);

$li = array(
  'uri'      => '/index/account',
  'text'     => '<img src="/image/account.png" />&nbsp;登録情報',
  'selected' => array(),
  'ul'       => array(),
  'attr'     => array(),
);
$li['attr']['class'][] = 'account';
if(strncmp('account', $this->action, 7) == 0) {
  $li['attr']['class'][] = 'selected';
}
$ul[] = $li;

$li = array(
  'uri'      => '/account/logout',
  'text'     => '<img src="/image/logout.png" />&nbsp;ログアウト',
  'ul'       => array(),
  'attr'     => array(),
);
$li['attr']['class'][] = 'logout';
$ul[] = $li;

echo $this->partialLoop('index/menu-li.phtml', $ul);
