【CakePHP2.x】パンくずリスト

CakePHP1系でもgetCrumbList()が使えるように、必要なものをCakePHP2から移植。

<?php
class BreadCrumbHelper extends Helper {

  protected $_tags = array(
    'link' => '<a href="%s"%s>%s</a>',
    'tag' => '<%s%s>%s</%s>',
    'ul' => '<ul%s>%s</ul>',
    'ol' => '<ol%s>%s</ol>',
    'li' => '<li%s>%s</li>',
  );


  protected $_crumbs = array();

  public function addCrumb($name, $link = null, $options = null) {
    $this->_crumbs[] = array($name, $link, $options);
  }


  protected function _prepareCrumbs($startText) {
    $crumbs = $this->_crumbs;
    if ($startText) {
      if (!is_array($startText)) {
        $startText = array(
          'url' => '/',
          'text' => $startText
        );
      }
      $startText += array('url' => '/', 'text' => __('Home'));
      list($url, $text) = array($startText['url'], $startText['text']);
      unset($startText['url'], $startText['text']);
      array_unshift($crumbs, array($text, $url, $startText));
    }
    return $crumbs;
  }

  public function getCrumbList($options = array(), $startText = false) {
    $crumbs = $this->_prepareCrumbs($startText);
    if (!empty($crumbs)) {
        $result = '';
        $crumbCount = count($crumbs);
        $ulOptions = $options;
        foreach ($crumbs as $which => $crumb) {
            $options = array();
            if (empty($crumb[1])) {
                $elementContent = $crumb[0];
            } else {
                $elementContent = $this->link($crumb[0], $crumb[1], $crumb[2]);
            }
            if ($which == 0) {
                $options['class'] = 'first';
            } elseif ($which == $crumbCount - 1) {
                $options['class'] = 'last';
            }
            $result .= $this->tag('li', $elementContent, $options);
        }
        return $this->tag('ul', $result, $ulOptions);
    } else {
        return null;
    }
  }


  function link($title, $url = null, $options = array(), $confirmMessage = false) {
     $escapeTitle = true;
     if ($url !== null) {
         $url = $this->url($url);
     } else {
         $url = $this->url($title);
         $title = htmlspecialchars_decode($url, ENT_QUOTES);
         $title = h(urldecode($title));
         $escapeTitle = false;
     }

     if (isset($options['escape'])) {
         $escapeTitle = $options['escape'];
     }

     if ($escapeTitle === true) {
         $title = h($title);
     } elseif (is_string($escapeTitle)) {
         $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
     }

     if (!empty($options['confirm'])) {
         $confirmMessage = $options['confirm'];
         unset($options['confirm']);
     }
     if ($confirmMessage) {
         $confirmMessage = str_replace("'", "\'", $confirmMessage);
         $confirmMessage = str_replace('"', '\"', $confirmMessage);
         $options['onclick'] = "return confirm('{$confirmMessage}');";
     } elseif (isset($options['default']) && $options['default'] == false) {
         if (isset($options['onclick'])) {
             $options['onclick'] .= ' event.returnValue = false; return false;';
         } else {
             $options['onclick'] = 'event.returnValue = false; return false;';
         }
         unset($options['default']);
     }
     return sprintf($this->_tags['link'], $url, $this->_parseAttributes($options), $title);
  }

  public function tag($name, $text = null, $options = array()) {
    if (is_array($options) && isset($options['escape']) && $options['escape']) {
       $text = h($text);
       unset($options['escape']);
    }
    if (!is_array($options)) {
       $options = array('class' => $options);
    }
    if ($text === null) {
       $tag = 'tagstart';
    } else {
       $tag = 'tag';
    }
    return sprintf($this->_tags[$tag], $name, $this->_parseAttributes($options, null, ' ', ''), $text, $name);
  }


}