You are here

function og_user in Organic groups 5

Same name and namespace in other branches
  1. 5.8 og.module \og_user()
  2. 5.2 og.module \og_user()
  3. 5.3 og.module \og_user()
  4. 5.7 og.module \og_user()
  5. 6.2 og.module \og_user()
  6. 6 og.module \og_user()

File

./og.module, line 2152

Code

function og_user($op, $edit, &$account, $category = NULL) {
  global $user;
  switch ($op) {
    case 'register':
      $options = array();
      list($types, $in) = og_get_sql_args();

      // if groups are passed on querystring, just use them.
      if ($gids = $_REQUEST['gids']) {
        $default_value = $gids;
        foreach ($gids as $gid) {
          $nids[] = (int) $gid;
        }
        $in_nids = implode(', ', array_fill(0, count($nids), "%d"));
        $sql = "SELECT n.nid, n.title, o.* FROM {node} n INNER JOIN {og} o ON n.nid = o.nid WHERE n.type {$in} AND n.status = 1 AND n.nid IN ({$in_nids}) ORDER BY n.title";
        $result = db_query(db_rewrite_sql($sql), array_merge($types, $nids));
      }
      else {
        $default_value = array();

        // perhaps this should be a View
        $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, o.* FROM {node} n INNER JOIN {og} o ON n.nid = o.nid WHERE n.type {$in} AND n.status = 1 AND o.register = 1 ORDER BY n.title"), $types);
      }
      while ($group = db_fetch_object($result)) {
        $options[$group->nid] = '<span class="og-registration-' . $group->nid . '">' . t('Join %name.', array(
          '%name' => $group->title,
        )) . "</span>\n";
        if ($group->selective) {
          $options[$group->nid] .= ' ' . t('(approval needed)');
        }
      }
      if (count($options)) {
        $form['og_register'] = array(
          '#type' => 'fieldset',
          '#title' => t('Groups'),
        );
        $form['og_register']['og_register'] = array(
          '#type' => 'checkboxes',
          '#options' => $options,
          '#default_value' => $default_value,
        );
      }
      return $form;
    case 'form':
      if ($category == 'account' && !empty($account->og_groups)) {
        $form['og_settings'] = array(
          '#type' => 'fieldset',
          '#title' => t('Organic groups settings'),
          '#collapsible' => TRUE,
          '#weight' => 4,
        );
        $options = array(
          OG_NOTIFICATION_NEVER => t('Never send email notifications. Useful when tracking activity via RSS feed instead.'),
          OG_NOTIFICATION_ALWAYS => t('Always send email notifications'),
          OG_NOTIFICATION_SELECTIVE => t("Selectively send email notification based on the checkbox for each of my group's <em>My Membership</em> page."),
        );
        $form['og_settings']['og_email'] = array(
          '#type' => 'radios',
          '#title' => t('Email notifications'),
          '#options' => $options,
          '#default_value' => isset($account->og_email) ? $account->og_email : variable_get('og_notification', 2),
          '#description' => t('When posts are submitted into your groups, you may be notified via email.'),
        );
        return $form;
      }
      break;
    case 'insert':
      if (is_array($edit['og_register'])) {
        foreach (array_keys(array_filter($edit['og_register'])) as $gid) {
          $return = og_subscribe_user($gid, $account);
          if (!empty($return['message'])) {
            drupal_set_message($return['message']);
          }
        }
      }
      $sql = 'INSERT INTO {og_uid_global} (uid, og_email) VALUES (%d, %d)';
      db_query($sql, $account->uid, variable_get('og_notification', OG_NOTIFICATION_ALWAYS));
      $account->og_email = NULL;
      break;
    case 'update':
      if (isset($edit['og_email'])) {
        $sql = 'UPDATE {og_uid_global} SET og_email=%d WHERE uid=%d';
        db_query($sql, $edit['og_email'], $account->uid);
        $account->og_email = NULL;
      }
      break;
    case 'delete':

      // user delete doesn't exist, but it should and will one day.
      $sql = 'DELETE FROM {og_uid_global} WHERE uid=%d';
      db_query($sql, $account->uid);
      $sql = 'DELETE FROM {og_uid} WHERE uid=%d';
      db_query($sql, $account->uid);
      break;
    case 'load':
      $account->og_groups = og_get_subscriptions($account->uid);
      $result = db_query("SELECT og_email FROM {og_uid_global} WHERE uid = %d", $account->uid);
      if (db_num_rows($result)) {
        $account->og_email = db_result($result);
      }
      break;
    case 'view':

      // only show list of groups to self and admins
      if ($account->uid == $user->uid || user_access('administer organic groups')) {
        if ($account->og_groups) {
          foreach ($account->og_groups as $key => $val) {
            $links[$key] = l($val['title'], "node/{$key}") . theme('og_format_subscriber_status', $val);
          }
          return array(
            t('Groups') => array(
              array(
                'value' => theme('item_list', $links),
                'title' => '',
                'class' => 'og_groups',
              ),
            ),
          );
        }
      }
      break;
  }
}