You are here

function flexiaccess_user in Flexi Access 7

Build form to handle ACLs for user.

See http://passingcuriosity.com/2011/drupal-7-forms-tables/.

1 string reference to 'flexiaccess_user'
flexiaccess_menu in ./flexiaccess.module
Implements hook_menu().

File

./flexiaccess.users.inc, line 13
Form handling for per user ACL.

Code

function flexiaccess_user($form, &$form_state, $user) {
  if (module_exists('acl')) {

    /**
     * First, a utility function to help code re-use.
     */
    function addrow($row, &$form, $user) {
      $cell_node = array(
        '#type' => 'markup',
        '#markup' => '<strong>' . t('#!nid: !title', array(
          '!nid' => $row->nid,
          '!title' => $row->title,
        )) . '</strong>',
      );
      $cell_view = array(
        '#type' => 'checkbox',
        '#title' => t('View'),
        '#default_value' => $row->grant_view,
      );
      $cell_update = array(
        '#type' => 'checkbox',
        '#title' => t('Update'),
        '#default_value' => $row->grant_update,
      );
      $cell_delete = array(
        '#type' => 'checkbox',
        '#title' => t('Delete'),
        '#default_value' => $row->grant_delete,
      );
      $form['acl'][] = array(
        'node' => &$cell_node,
        'nid' => array(
          '#type' => 'value',
          '#value' => $row->nid,
        ),
        'view' => &$cell_view,
        'update' => &$cell_update,
        'delete' => &$cell_delete,
      );
    }

    /* end function addrow */
    $query = db_select('acl_user', 'u');
    $query
      ->join('acl', 'a', 'a.acl_id = u.acl_id');
    $query
      ->join('acl_node', 'n', 'a.acl_id = n.acl_id');
    $query
      ->join('node', 'node', 'n.nid = node.nid');
    $query
      ->fields('a', array(
      'acl_id',
    ))
      ->fields('n', array(
      'nid',
    ))
      ->fields('node', array(
      'title',
    ))
      ->fields('node', array(
      'uid',
    ))
      ->fields('node', array(
      'type',
    ));
    $query
      ->addExpression('SUM(n.grant_view)', 'grant_view');
    $query
      ->addExpression('SUM(n.grant_update)', 'grant_update');
    $query
      ->addExpression('SUM(n.grant_delete)', 'grant_delete');
    $query
      ->condition('a.module', 'flexiaccess')
      ->condition('u.uid', $user->uid)
      ->groupBy('n.nid');
    $result = $query
      ->execute();
    $form_state['user'] = $user;

    // The permissions table:
    $form['acl'] = array(
      '#title' => t('Node based ACL'),
      '#prefix' => '<div id="flexiaccess-user-acl-table"><p><em>' . t('Manage the nodes which this user has access to.  Remember to press &#8220;Commit updates&#8221; when done (otherwise, your changes will not be saved).') . '</em></p>',
      '#suffix' => '</div>',
      '#tree' => TRUE,
      // @todo: Check this out.
      // See: http://drupal.stackexchange.com/questions/90282/d7-fapi-unexpected-bahviour-when-combining-ajax-checkbox-and-a-table-theme
      // '#theme' => 'table',
      '#header' => array(
        t("Node"),
        t("View"),
        t("Update"),
        t("Delete"),
      ),
      '#rows' => array(),
    );
    $form['add'] = array(
      '#type' => 'textfield',
      '#title' => t('Add node'),
      '#size' => 60,
      '#autocomplete_path' => 'flexiaccess/node_autocomplete',
    );
    $form['add_button'] = array(
      '#type' => 'button',
      '#name' => 'acl_user_' . $user->uid,
      '#value' => t('Add Node'),
      '#ajax' => array(
        'callback' => 'flexiaccess_user_ajax_callback',
        'wrapper' => 'flexiaccess-user',
        'method' => 'replace',
        'effect' => 'fade',
      ),
    );
    foreach ($result as $row) {
      addrow($row, $form, $user);
    }
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Commit updates'),
      '#weight' => 10,
      '#submit' => array(
        'flexiaccess_user_submit',
      ),
    );

    // Determine whether we are rebuilding the form.
    // This is 1 when a row is being added via ajax.
    if ($form_state['rebuild']) {

      // Check if there are previously added, but uncommitted rows.
      if (empty($form_state['uncommitted'])) {
        $form_state['uncommitted'] = array();
      }

      // Find the correct node titles.
      if (!empty($form_state['uncommitted'])) {
        $result = db_query('SELECT nid,title, 0 AS grant_view, 0 AS grant_update, 0 AS grant_delete FROM {node} WHERE nid IN (:nids)', array(
          ':nids' => $form_state['uncommitted'],
        ));

        // Add the rows.
        if ($result
          ->rowCount()) {
          foreach ($result as $row) {
            addrow($row, $form, $user);
          }
        }
      }
    }
    $form['#tree'] = TRUE;
    return $form;
  }
}