class LayoutMenuItem {
var $name = '';
var $title = '';
var $path = '';
public $weight = 0;
var $menu = array(
'title' => NULL,
'weight' => 0,
'type' => 'none',
'name' => 'main-menu',
'parent' => array(
'title' => NULL,
'weight' => 0,
'type' => 'none',
'name' => 'main-menu',
),
);
var $conditions = array();
var $arguments = array();
var $locked = FALSE;
var $is_new = FALSE;
var $module;
private $contexts = array();
function __construct($data) {
foreach ($data as $key => $value) {
$this->$key = $value;
}
if (empty($this->menu)) {
$this->menu = array();
}
$this->menu += array(
'type' => 'none',
'title' => '',
'weight' => 0,
'name' => 'main-menu',
);
if (!isset($this->menu['parent'])) {
$this->menu['parent'] = array();
}
$this->menu['parent'] += array(
'type' => 'none',
'title' => '',
'weight' => 0,
'name' => 'main-menu',
);
foreach ($this->conditions as $key => $condition) {
$this->conditions[$key] = layout_create_handler('layout_access', $condition['plugin'], $condition['data']);
}
if (is_null($this->contexts)) {
$this->contexts = array();
}
}
function save() {
$data = array(
'name' => $this->name,
'title' => $this->title,
'path' => $this->path,
'menu' => $this->menu,
'conditions' => array(),
'arguments' => $this->arguments,
);
foreach ($this->conditions as $condition) {
unset($condition->is_new);
$data['conditions'][] = array(
'plugin' => $condition->plugin,
'data' => $condition->toArray(),
);
}
config('layout.menu_item.' . $this->name)
->setData($data)
->save();
layout_reset_caches();
menu_rebuild();
}
function delete() {
config('layout.menu_item.' . $this->name)
->delete();
layout_reset_caches();
menu_rebuild();
}
function revert() {
if ($this->module) {
config_install_default_config($this->module, 'layout.menu_item.' . $this->name);
layout_reset_caches();
menu_rebuild();
}
}
function reassign() {
layout_reset_caches();
$layout_names = layout_get_path_layout_names($this->path);
if (count($layout_names)) {
$layout_name = array_pop($layout_names);
if ($this->name != $layout_name) {
$this->delete();
$this->name = $layout_name;
$this->save();
}
}
else {
$this->delete();
}
layout_reset_caches();
menu_rebuild();
}
function getContexts() {
if (is_null($this->contexts)) {
$this->contexts = array();
}
$this->resetContexts();
$contexts = layout_context_required_by_path($this->path);
$path_parts = explode('/', $this->path);
foreach ($this->arguments as $position => $argument_config) {
if (isset($path_parts[$position]) && $path_parts[$position] === '%') {
$context_config = $argument_config + array(
'required' => TRUE,
'usageType' => LayoutContext::USAGE_TYPE_MENU,
);
$contexts[$position] = layout_create_context($argument_config['plugin'], $context_config);
}
}
$contexts['current_user'] = layout_current_user_context();
$this->contexts += $contexts;
return $this->contexts;
}
function resetContexts() {
foreach ($this->contexts as $key => $context) {
if (!$context->storage) {
unset($this->contexts[$key]);
}
}
}
function hasContexts(array $required_contexts) {
$all_contexts = $this->getContexts();
foreach ($required_contexts as $required_context_name) {
$context_missing = TRUE;
foreach ($all_contexts as $context) {
if ($context->plugin === $required_context_name) {
$context_missing = FALSE;
break;
}
}
if ($context_missing) {
return FALSE;
}
}
return TRUE;
}
function checkAccess() {
$contexts = $this->getContexts();
foreach ($this->conditions as $condition) {
$condition->setContexts($contexts);
if (!$condition->checkAccess()) {
return FALSE;
}
}
return TRUE;
}
}