context_condition_menu.inc in Context 7.3
File
plugins/context_condition_menu.inc
View source
<?php
class context_condition_menu extends context_condition {
function condition_values() {
if (module_exists('menu')) {
$menus = menu_parent_options(menu_get_menus(), array(
'mlid' => 0,
));
$root_menus = array();
foreach ($menus as $key => $name) {
$id = explode(':', $key);
if ($id[1] == '0') {
$root_menus[$id[0]] = $name;
}
else {
$link = menu_link_load($id[1]);
$identifier = $link['link_path'];
$root_menu = $root_menus[$id[0]];
while (isset($menus[$root_menu][$identifier])) {
$identifier .= "'";
}
$menus[$root_menu][$identifier] = $name;
}
unset($menus[$key]);
}
array_unshift($menus, "-- " . t('None') . " --");
}
else {
$menus = array();
}
return $menus;
}
function condition_form($context) {
$form = parent::condition_form($context);
$menu_count = count($form['#options'], COUNT_RECURSIVE);
$form['#type'] = 'select';
$form['#multiple'] = TRUE;
$form['#attributes'] = array(
'size' => $menu_count > 20 ? 20 : $menu_count,
);
return $form;
}
function condition_form_submit($values) {
$values = parent::condition_form_submit($values);
$trimmed = array();
foreach ($values as $key => $value) {
$trimmed[trim($key, "'")] = trim($value, "'");
}
return $trimmed;
}
function execute() {
if ($this
->condition_used()) {
$trail = menu_get_active_trail();
foreach ($trail as $item) {
if (!empty($item['href'])) {
foreach ($this
->get_contexts($item['href']) as $context) {
$this
->condition_met($context, $item['href']);
}
}
}
}
}
}