You are here

function menu_patterns_validate in Patterns 7

Same name and namespace in other branches
  1. 7.2 patterns_components/components/menu.inc \menu_patterns_validate()

File

patterns_components/components/menu.inc, line 191

Code

function menu_patterns_validate($action, $tag, &$data) {
  $status = PATTERNS_SUCCESS;
  $msg = '';
  if ($tag == "menu") {
    if (empty($data['menu_name'])) {
      $msg = t('The menu name is required.');
    }
    elseif (!empty($data['menu_name']) && preg_match('/[^a-z0-9\\-]/', $data['menu_name'])) {
      $status = PATTERNS_ERR;
      $msg = t('The menu name may only consist of lowercase letters, numbers, and hyphens: %menu', array(
        '%menu' => $data['menu_name'],
      ));
    }
    else {
      $count = db_query("SELECT COUNT(*) FROM {menu_custom} WHERE menu_name = :menu", array(
        'menu' => $data['menu_name'],
      ))
        ->fetchField();

      // TODO: count()
      if ($action == PATTERNS_DELETE && !$count) {
        $status = PATTERNS_ERR;
        $msg = t('Attempt to delete a nonexistent menu');
      }
      elseif ($action == PATTERNS_MODIFY && !$count) {
        $status = PATTERNS_ERR;
        $msg = t('Attempt to update a nonexistent menu');
      }
      elseif ($action == PATTERNS_CREATE) {
        $count = db_query("SELECT COUNT(*) FROM {menu_custom} WHERE menu_name = :menu", array(
          'menu' => 'menu-' . $data['menu_name'],
        ))
          ->fetchField();

        // TODO: count()
        if ($count) {
          $status = PATTERNS_ERR;
          $msg = t('The machine-readable menu name is already in use. It must be unique.');
        }
      }
    }
  }
  elseif ($tag == "menu_item") {
    if ($action == PATTERNS_MODIFY && !isset($data['mlid'])) {
      if (empty($data['menu_name_old'])) {
        $status = PATTERNS_ERR;
        $msg = t('"menu_name_old" or "mlid" is required.');
      }
      elseif (empty($data['link_path_old'])) {
        $status = PATTERNS_ERR;
        $msg = t('"link_path_old" or "mlid" is required.');
      }
      elseif (empty($data['link_title_old'])) {
        $status = PATTERNS_ERR;
        $msg = t('"link_title_old" or "mlid" is required.');
      }
    }
    if ($action !== PATTERNS_DELETE || $action == PATTERNS_DELETE && !isset($data['mlid'])) {
      if (empty($data['menu_name'])) {
        $status = PATTERNS_ERR;
        $msg = t('"menu_name" is required.');
      }
      elseif (empty($data['link_path'])) {
        $status = PATTERNS_ERR;
        $msg = t('"path" is required.');
      }
      elseif (empty($data['link_title'])) {
        $status = PATTERNS_ERR;
        $msg = t('"title" is required.');
      }
    }
    if (!empty($data['parent']) && $action !== PATTERNS_DELETE) {
      list($menu_name, $mlid) = explode(':', $data['parent']);
      if ($mlid != 0 && !db_query("SELECT count(*) FROM {menu_links} WHERE menu_name = :menu AND mlid = :mlid", array(
        'menu' => $menu_name,
        'mlid' => $mlid,
      ))
        ->fetchField()) {

        // TODO: count()
        $status = PATTERNS_ERR;
        $msg = t('"parent" is not valid.');
      }
    }
  }
  return patterns_results($status, $msg);
}