2011-03-23 10:51:00 +01:00
|
|
|
<?php
|
2016-08-11 01:14:02 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Admin;
|
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2016-08-11 01:14:02 +02:00
|
|
|
use Object;
|
|
|
|
use Convert;
|
2016-08-11 01:40:23 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
2016-08-11 01:14:02 +02:00
|
|
|
|
2013-10-02 08:06:54 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
2013-10-02 08:06:54 +02:00
|
|
|
* A simple CMS menu item.
|
|
|
|
*
|
|
|
|
* Items can be added to the menu through custom {@link LeftAndMainExtension}
|
|
|
|
* classes and {@link CMSMenu}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-10-02 08:06:54 +02:00
|
|
|
* @see CMSMenu
|
|
|
|
*
|
|
|
|
* @package framework
|
|
|
|
* @subpackage admin
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2013-10-02 08:06:54 +02:00
|
|
|
class CMSMenuItem extends Object {
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* The (translated) menu title
|
|
|
|
* @var string $title
|
|
|
|
*/
|
|
|
|
public $title;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Relative URL
|
|
|
|
* @var string $url
|
|
|
|
*/
|
|
|
|
public $url;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Parent controller class name
|
|
|
|
* @var string $controller
|
|
|
|
*/
|
|
|
|
public $controller;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Menu priority (sort order)
|
|
|
|
* @var integer $priority
|
|
|
|
*/
|
|
|
|
public $priority;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-02 08:06:54 +02:00
|
|
|
/**
|
|
|
|
* Attributes for the link. For instance, custom data attributes or standard
|
|
|
|
* HTML anchor properties.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $attributes = array();
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Create a new CMS Menu Item
|
2013-10-02 08:06:54 +02:00
|
|
|
*
|
2011-03-23 10:51:00 +01:00
|
|
|
* @param string $title
|
|
|
|
* @param string $url
|
|
|
|
* @param string $controller Controller class name
|
|
|
|
* @param integer $priority The sort priority of the item
|
|
|
|
*/
|
|
|
|
public function __construct($title, $url, $controller = null, $priority = -1) {
|
|
|
|
$this->title = $title;
|
|
|
|
$this->url = $url;
|
|
|
|
$this->controller = $controller;
|
|
|
|
$this->priority = $priority;
|
2013-10-02 08:06:54 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2013-10-02 08:06:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $attributes
|
|
|
|
*/
|
|
|
|
public function setAttributes($attributes) {
|
|
|
|
$this->attributes = $attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 01:40:23 +02:00
|
|
|
* @param array $attrs
|
|
|
|
* @return DBHTMLText
|
2013-10-02 08:06:54 +02:00
|
|
|
*/
|
|
|
|
public function getAttributesHTML($attrs = null) {
|
2016-08-11 01:40:23 +02:00
|
|
|
$excludeKeys = (is_string($attrs)) ? func_get_args() : null;
|
2013-10-02 08:06:54 +02:00
|
|
|
|
|
|
|
if(!$attrs || is_string($attrs)) {
|
|
|
|
$attrs = $this->attributes;
|
|
|
|
}
|
|
|
|
|
2016-08-11 01:40:23 +02:00
|
|
|
// Remove empty or excluded values
|
|
|
|
foreach ($attrs as $key => $value) {
|
|
|
|
if (
|
|
|
|
($excludeKeys && in_array($key, $excludeKeys))
|
|
|
|
|| (!$value && $value !== 0 && $value !== '0')
|
|
|
|
) {
|
|
|
|
unset($attrs[$key]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2013-10-02 08:06:54 +02:00
|
|
|
|
|
|
|
// Create markkup
|
|
|
|
$parts = array();
|
|
|
|
|
|
|
|
foreach($attrs as $name => $value) {
|
|
|
|
$parts[] = ($value === true) ? "{$name}=\"{$name}\"" : "{$name}=\"" . Convert::raw2att($value) . "\"";
|
|
|
|
}
|
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
return DBField::create_field('HTMLFragment', implode(' ', $parts));
|
2013-10-02 08:06:54 +02:00
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|