123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
- namespace dokuwiki\Menu\Item;
- abstract class AbstractItem {
-
- const CTX_DESKTOP = 1;
-
- const CTX_MOBILE = 2;
-
- const CTX_ALL = 3;
-
- protected $type = '';
-
- protected $accesskey = '';
-
- protected $id = '';
-
- protected $method = 'get';
-
- protected $params = array();
-
- protected $nofollow = true;
-
- protected $replacement = '';
-
- protected $svg = DOKU_INC . 'lib/images/menu/00-default_checkbox-blank-circle-outline.svg';
-
- protected $label = '';
-
- protected $title = '';
-
- protected $context = self::CTX_ALL;
-
- public function __construct() {
- global $ID;
- $this->id = $ID;
- $this->type = $this->getType();
- $this->params['do'] = $this->type;
- if(!actionOK($this->type)) throw new \RuntimeException("action disabled: {$this->type}");
- }
-
- public function getLabel() {
- if($this->label !== '') return $this->label;
-
- global $lang;
- $label = $lang['btn_' . $this->type];
- if(strpos($label, '%s')) {
- $label = sprintf($label, $this->replacement);
- }
- if($label === '') $label = '[' . $this->type . ']';
- return $label;
- }
-
- public function getTitle() {
- if($this->title === '') return $this->getLabel();
- return $this->title;
- }
-
- public function getLink() {
- if($this->id && $this->id[0] == '#') {
- return $this->id;
- } else {
- return wl($this->id, $this->params, false, '&');
- }
- }
-
- public function getLinkAttributes($classprefix = 'menuitem ') {
- $attr = array(
- 'href' => $this->getLink(),
- 'title' => $this->getTitle(),
- );
- if($this->isNofollow()) $attr['rel'] = 'nofollow';
- if($this->getAccesskey()) {
- $attr['accesskey'] = $this->getAccesskey();
- $attr['title'] .= ' [' . $this->getAccesskey() . ']';
- }
- if($classprefix !== false) $attr['class'] = $classprefix . $this->getType();
- return $attr;
- }
-
- public function asHtmlLink($classprefix = 'menuitem ', $svg = true) {
- $attr = buildAttributes($this->getLinkAttributes($classprefix));
- $html = "<a $attr>";
- if($svg) {
- $html .= '<span>' . hsc($this->getLabel()) . '</span>';
- $html .= inlineSVG($this->getSvg());
- } else {
- $html .= hsc($this->getLabel());
- }
- $html .= "</a>";
- return $html;
- }
-
- public function asHtmlButton() {
- return html_btn(
- $this->getType(),
- $this->id,
- $this->getAccesskey(),
- $this->getParams(),
- $this->method,
- $this->getTitle(),
- $this->getLabel(),
- $this->getSvg()
- );
- }
-
- public function visibleInContext($ctx) {
- return (bool) ($ctx & $this->context);
- }
-
- public function getType() {
- if($this->type === '') {
- $this->type = strtolower(substr(strrchr(get_class($this), '\\'), 1));
- }
- return $this->type;
- }
-
- public function getAccesskey() {
- return $this->accesskey;
- }
-
- public function getParams() {
- return $this->params;
- }
-
- public function isNofollow() {
- return $this->nofollow;
- }
-
- public function getSvg() {
- return $this->svg;
- }
-
- public function getLegacyData() {
- return array(
- 'accesskey' => $this->accesskey ?: null,
- 'type' => $this->type,
- 'id' => $this->id,
- 'method' => $this->method,
- 'params' => $this->params,
- 'nofollow' => $this->nofollow,
- 'replacement' => $this->replacement
- );
- }
- }
|