You are here

function _menu_per_role_form_submit in Menu Per Role 6

1 call to _menu_per_role_form_submit()
menu_per_role_nodeapi in ./menu_per_role.module
Implementation of hook_nodeapi().
1 string reference to '_menu_per_role_form_submit'
_menu_per_role_form_alter in ./menu_per_role.admin.inc

File

./menu_per_role.admin.inc, line 120
Administration form for menu items.

Code

function _menu_per_role_form_submit($form, &$form_state) {
  global $db_type;
  if ($form_state['submitted'] && user_access('administer menu_per_role')) {
    $mlid = $form_state['values']['menu']['mlid'];
    if ($mlid) {

      // hide but to those roles
      $rids = array();
      $hide_show = variable_get('menu_per_role_hide_show', 0);
      if ($hide_show == 0 || $hide_show == 2) {
        $roles = isset($form_state['values']['menu_per_role_roles']) ? $form_state['values']['menu_per_role_roles'] : $form_state['values']['menu']['menu_per_role']['menu_per_role_roles'];
        foreach ($roles as $rid => $checked) {
          if ($checked) {
            $rids[] = $rid;
          }
        }
      }
      $rids_str = implode(',', $rids);

      // show but to those roles
      $hrids = array();
      if ($hide_show == 0 || $hide_show == 1) {
        $roles = isset($form_state['values']['menu_per_role_hide_from_roles']) ? $form_state['values']['menu_per_role_hide_from_roles'] : $form_state['values']['menu']['menu_per_role']['menu_per_role_hide_from_roles'];
        foreach ($roles as $rid => $checked) {
          if ($checked) {
            $hrids[] = $rid;
          }
        }
      }
      $hrids_str = implode(',', $hrids);

      // save in our table

      //db_lock_table('menu_per_role');
      if ($rids_str || $hrids_str) {
        db_query("UPDATE {menu_per_role} SET rids = '%s', hrids = '%s' WHERE mlid = %d", $rids_str, $hrids_str, $mlid);
        if (db_affected_rows() == 0) {

          // if nothing was affected, the row did not exist yet
          // (although with MySQL this may fail because db_affected_rows() may only return
          // rows that have been changed instead of the # of rows that match the WHERE clause.)
          if ($db_type != 'pgsql') {

            // MySQL hack
            $insert = !db_result(db_query("SELECT 1 FROM {menu_per_role} WHERE mlid = %d", $mlid));
          }
          else {
            $insert = TRUE;
          }
          if ($insert) {
            db_query("INSERT INTO {menu_per_role} (mlid, rids, hrids) VALUES (%d, '%s', '%s')", $mlid, $rids_str, $hrids_str);
          }
        }
      }
      else {

        // we don't need to save it when empty, instead, remove that completely
        db_query("DELETE FROM {menu_per_role} WHERE mlid = %d", $mlid);
      }

      //db_unlock_tables();

      // reset the menus
      menu_cache_clear_all();
    }
    elseif (isset($form_state['values']['menu_per_role_roles'])) {
      drupal_set_message(t('The menu link identifier was not defined on Submit in <b>Menu per Role</b>. You are most certainly adding a new menu item. For this feature to work when adding a menu item, you must apply the patch defined in <a href="http://drupal.org/node/326210" target="_blank">node #326210</a>. That patch is included in this module for that purpose.'), 'error');
    }
  }
}