You are here

function flag_user in Flag 6

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

Implementation of hook_user().

1 string reference to 'flag_user'
flag_flag_definitions in ./flag.inc
Implementation of hook_flag_definitions().

File

./flag.module, line 376

Code

function flag_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'delete':

      // Remove flags by this user.
      $result = db_query("SELECT fc.fid, fc.content_id, c.count FROM {flag_content} fc LEFT JOIN {flag_counts} c ON fc.content_id = c.content_id AND fc.content_type = c.content_type WHERE fc.uid = %d", $account->uid);
      while ($flag_data = db_fetch_object($result)) {
        $flag_data->count--;

        // Only decrement the flag count table if it's greater than 1.
        if ($flag_data->count > 0) {
          db_query("UPDATE {flag_counts} SET count = %d WHERE fid = %d AND content_id = %d", $flag_data->count, $flag_data->fid, $flag_data->content_id);
        }
        elseif ($flag_data->count == 0) {
          db_query("DELETE FROM {flag_counts} WHERE fid = %d AND content_id = %d", $flag_data->fid, $flag_data->content_id);
        }
      }
      db_query("DELETE FROM {flag_content} WHERE uid = %d", $account->uid);
      break;
    case 'view':
      $flags = flag_get_flags('user');
      $flag_items = array();
      foreach ($flags as $flag) {
        if (!$flag
          ->user_access()) {

          // User has no permission to use this flag.
          continue;
        }
        if (!$flag
          ->applies_to_content_object($account)) {

          // Flag does not apply to this content.
          continue;
        }
        if (!$flag
          ->uses_hook_link(array())) {

          // Flag not set to appear on profile.
          continue;
        }
        $flag_items[$flag->name] = array(
          '#type' => 'user_profile_item',
          '#title' => $flag
            ->get_title($account->uid),
          '#value' => $flag
            ->theme($flag
            ->is_flagged($account->uid) ? 'unflag' : 'flag', $account->uid),
          '#attributes' => array(
            'class' => 'flag-profile-' . $flag->name,
          ),
        );
      }
      if (!empty($flag_items)) {
        $account->content['flags'] = $flag_items;
        $account->content['flags'] += array(
          '#type' => 'user_profile_category',
          '#title' => t('Actions'),
          '#attributes' => array(
            'class' => 'flag-profile',
          ),
        );
      }
      break;
  }
}