You are here

public function AckMenuMap::objectFormAlter in Access Control Kit 7

Overrides AccessControlKitHandler::objectFormAlter().

Overrides AccessControlKitHandler::objectFormAlter

File

ack_menu/handlers/ack_menu_map.inc, line 129
Contains the handler class for mapping menu links to access realms.

Class

AckMenuMap
Controls access to menu links based on realm mapping.

Code

public function objectFormAlter($object_type, $menu_link, &$form, &$form_state, $form_id, $realms = NULL) {
  $admin = !empty($form_state['ack_menu']['admin']);
  $global = !empty($form_state['ack_menu']['global admin']);

  // If the user isn't a global admin, we need to filter the parent options.
  if (!$global) {

    // If a realm list wasn't provided, get the link's current realm.
    if (!isset($realms) && !empty($menu_link['mlid'])) {
      $realms = $this
        ->objectRealms($object_type, $menu_link);
    }

    // Only filter if we were able to identify a realm. Links without a realm
    // are skipped, so as not to remove options needed by other schemes.
    if (!empty($realms)) {
      $menus = $this
        ->managedMenus();

      // Realm menu admins can place the link anywhere in the managed menus.
      if ($admin) {
        $allowed = empty($menus) ? array() : menu_parent_options($menus, $menu_link);
      }
      else {

        // For non-admins, get the form option keys for all eligible parents.
        $allowed = array();
        foreach ($realms as $realm) {
          foreach (array_keys($menus) as $menu_name) {
            foreach ($this
              ->realmLinks($realm, $menu_name) as $link) {

              // Exclude links that are mapped to other realms.
              $link_realms = $this
                ->objectRealms('menu_link', $link);
              if (in_array($realm, $link_realms)) {
                $allowed[$link['menu_name'] . ':' . $link['mlid']] = TRUE;
              }
            }
          }
        }
      }

      // For an existing link, we need to keep the default value as an option.
      if (!empty($menu_link['mlid'])) {
        $allowed[$form['parent']['#default_value']] = TRUE;
      }

      // Remove options that aren't in our list.
      foreach (array_keys($form['parent']['#options']) as $option) {
        if (empty($allowed[$option])) {
          unset($form['parent']['#options'][$option]);
        }
      }

      // For new links, set the first available mapped link as the default;
      // if none is found, use the first allowed parent option.
      if (empty($menu_link['mlid'])) {
        $default = NULL;
        reset($realms);
        while (empty($default) && ($realm = next($realms))) {
          $mapped_links = $this
            ->mappedLinks($realm);
          while (empty($default) && ($mlid = array_pop($mapped_links))) {
            $default = menu_link_load($mlid);
          }
        }
        $form['parent']['#default_value'] = empty($default) ? key($allowed) : $default['menu_name'] . ':' . $default['mlid'];
      }
    }
  }

  // Realm menu admins can map the link to a realm from the menu item form.
  if ($admin && $form_id == 'menu_edit_item') {
    if (!isset($form['AckMenuMap'])) {
      $form['AckMenuMap'] = array(
        '#type' => 'fieldset',
        '#title' => t('Access control kit'),
        '#description' => t('This link may be associated with a realm in each of the following access schemes. Doing so will allow users with permission to manage menu links in that realm to edit this link and any of its child links.'),
        '#tree' => TRUE,
      );
    }

    // For a new link, if we know what realm it should be in, set the default
    // mapping to that realm.
    if (empty($menu_link['mlid']) && !empty($realms) && count($realms) == 1) {
      $mapped = reset($realms);
      $locked = TRUE;
    }
    else {

      // Otherwise, check to see if the link is already mapped.
      $mapped = empty($menu_link['mlid']) ? NULL : $this
        ->mappedRealm($menu_link['mlid']);
      $locked = FALSE;
    }
    $form['AckMenuMap'][$this->schemeMachineName] = array(
      '#type' => 'select',
      '#title' => check_plain($this->schemeName),
      '#options' => $this->schemeRealms,
      '#empty_option' => t('- Not mapped -'),
      '#default_value' => $mapped,
      '#disabled' => $locked,
    );
  }
}