You are here

function flag_flag_access_multiple in Flag 7.2

Same name and namespace in other branches
  1. 6.2 flag.module \flag_flag_access_multiple()
  2. 7.3 flag.module \flag_flag_access_multiple()

Implements hook_flag_access_multiple().

File

./flag.module, line 1176
The Flag module.

Code

function flag_flag_access_multiple($flag, $content_ids, $account) {
  $access = array();

  // Do nothing if there is no restriction by authorship.
  if (empty($flag->access_author)) {
    return $access;
  }
  if ($flag->content_type == 'node') {

    // Restrict access by authorship. This is similar to flag_flag_access()
    // above, but returns an array of 'nid' => $access values. Similarly, we
    // should never return TRUE in any of these access values, only FALSE if we
    // want to deny access, or use the current access value provided by Flag.
    $result = db_select('node', 'n')
      ->fields('n', array(
      'nid',
      'uid',
    ))
      ->condition('nid', array_keys($content_ids), 'IN')
      ->condition('type', $flag->types, 'IN')
      ->execute();
    foreach ($result as $row) {
      if ($flag->access_author == 'own') {
        $access[$row->nid] = $row->uid != $account->uid ? FALSE : NULL;
      }
      elseif ($flag->access_author == 'others') {
        $access[$row->nid] = $row->uid == $account->uid ? FALSE : NULL;
      }
    }
  }
  if ($flag->content_type == 'comment') {

    // Restrict access by comment ownership.
    $query = db_select('comment', 'c');
    $query
      ->leftJoin('node', 'n', 'c.nid = n.nid');
    $query
      ->fields('c', array(
      'cid',
      'nid',
      'uid',
    ))
      ->condition('c.cid', $content_ids, 'IN');
    $query
      ->addField('c', 'uid', 'comment_uid');
    $result = $query
      ->execute();
    foreach ($result as $row) {
      if ($flag->access_author == 'node_own') {
        $access[$row->cid] = $row->node_uid != $account->uid ? FALSE : NULL;
      }
      elseif ($flag->access_author == 'node_others') {
        $access[$row->cid] = $row->node_uid == $account->uid ? FALSE : NULL;
      }
      elseif ($flag->access_author == 'comment_own') {
        $access[$row->cid] = $row->comment_uid != $account->uid ? FALSE : NULL;
      }
      elseif ($flag->access_author == 'comment_others') {
        $access[$row->cid] = $row->comment_uid == $account->uid ? FALSE : NULL;
      }
    }
  }

  // Always return an array (even if empty) of accesses.
  return $access;
}