You are here

function flag_user in Flag 6.2

Same name and namespace in other branches
  1. 5 flag.module \flag_user()
  2. 6 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 493
The Flag module.

Code

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

      // Migrate anonymous flags to this user's account.
      if (module_exists('session_api') && ($sid = flag_get_sid(0))) {

        // The @ symbol suppresses errors if the user flags a piece of content
        // they have already flagged as a logged-in user.
        @db_query("UPDATE {flag_content} SET uid = %d, sid = 0 WHERE uid = 0 AND sid = %d", $account->uid, $sid);

        // Delete any remaining flags this user had as an anonymous user. We use the
        // proper unflag action here to make sure the count gets decremented again
        // and so that other modules can clean up their tables if needed.
        $result = db_query("SELECT fcid, fid, content_id FROM {flag_content} WHERE uid = 0 AND sid = %d", $sid);
        $anonymous_user = drupal_anonymous_user();
        while ($row = db_fetch_array($result)) {
          $flag = flag_get_flag(NULL, $row['fid']);
          $flag
            ->flag('unflag', $row['content_id'], $anonymous_user, TRUE);
        }

        // Clean up anonymous cookies.
        FlagCookieStorage::drop();
      }
      break;
    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);

      // Remove flags that have been done to this user.
      db_query("DELETE FROM {flag_counts} WHERE content_type = 'user' AND content_id = %d", $account->uid);
      db_query("DELETE FROM {flag_content} WHERE content_type = 'user' AND content_id = %d", $account->uid);
      break;
    case 'view':
      $flags = flag_get_flags('user');
      $flag_items = array();
      foreach ($flags as $flag) {
        if (!$flag
          ->access($account->uid)) {

          // User has no permission to use this flag.
          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;
  }
}