function menu_patterns_validate in Patterns 7.2
Same name and namespace in other branches
- 7 patterns_components/components/menu.inc \menu_patterns_validate()
File
- patterns_components/
components/ menu.inc, line 176
Code
function menu_patterns_validate($action, $tag, &$data) {
$result = array();
$status = PATTERNS_SUCCESS;
$msg = '';
/*
* Syntactic validation
*
*/
switch ($tag) {
case 'menu':
switch ($action) {
case PATTERNS_CREATE:
$mandatory_attributes = array(
'menu_name',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
break;
case PATTERNS_MODIFY:
//Check mandatory fields.
$mandatory_attributes = array(
'menu_name',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
//In this case we will need to define as well the attributes generated by the hook_prepare: menu_name, title
$interpretable_attributes = array(
'menu_name',
'title',
'description',
);
if (_patterns_has_uninterpretable_attributes($data, $interpretable_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
case PATTERNS_DELETE:
//Check mandatory fields, in this case is only one.
$mandatory_attributes = array(
'menu_name',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
break;
}
break;
case 'menu_item':
switch ($action) {
case PATTERNS_CREATE:
$mandatory_attributes = array(
'menu_name',
'link_path',
'link_title',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
break;
case PATTERNS_MODIFY:
//Check mandatory fields.
$mandatory_attributes = array(
'menu_name',
'menu_name_old',
'link_path',
'link_path_old',
'link_title',
'link_title_old',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
//In this case we will need to define as well the attributes generated by the hook_prepare
$interpretable_attributes = array(
'menu_name',
'menu_name_old',
'link_path',
'link_path_old',
'link_title',
'link_title_old',
'disable',
'parent',
'customized',
'module',
'enabled',
);
if (_patterns_has_uninterpretable_attributes($data, $interpretable_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
case PATTERNS_DELETE:
//Check mandatory fields
$mandatory_attributes = array(
'menu_name',
'link_path',
'link_title',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
}
break;
}
/*
* Semantic validation
*
*/
$exist_menu = menu_load_all();
//
$menus = menu_get_active_menu_names();
$menu_items = menu_load_links($menus);
switch ($tag) {
case 'menu':
switch ($action) {
case PATTERNS_CREATE:
$menu_name = $data['menu_name'];
//Create semantic warning if the role name has already been defined
if (in_array($menu_name, $exist_menu)) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The menu name %menu_name already exists in the system.', array(
'%menu_name' => $menu_name,
)),
);
}
break;
case PATTERNS_MODIFY:
//Create semantic warning if the menu does not exist
if (!$exist_menu) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The menu name %menu_name does not exists in the system.', array(
'%menu_name' => $data['menu_name'],
)),
);
}
break;
}
break;
case 'menu_item':
switch ($action) {
case PATTERNS_CREATE:
//Create semantic warning if the menu_item name has already been defined
$menu_item_name = $data['menu_name'];
if (in_array($menu_item_name, $menu_items)) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The menu item name %menu_item_name already exists in the system.', array(
'%menu_item_name' => $menu_item_name,
)),
);
}
break;
case PATTERNS_MODIFY:
//Create semantic warning if the rid does not exist
if (!$menu_items) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The menu item with %menu_item_name, %link_path,and %link_title do not exist in the system.', array(
'%menu_item_name' => $data['menu_name'],
'%link_path' => $data['link_path'],
'%link_title' => $data['link_title'],
)),
);
}
break;
case PATTERNS_DELETE:
if (!$menu_items) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The menu item with %menu_item_name, %link_path,and %link_title do not exist in the system.', array(
'%menu_item_name' => $data['menu_name'],
'%link_path' => $data['link_path'],
'%link_title' => $data['link_title'],
)),
);
}
break;
}
break;
}
return patterns_results($status, $msg, $result);
}