You are here

function userreference_user in Content Construction Kit (CCK) 6.2

Same name and namespace in other branches
  1. 5 userreference.module \userreference_user()
  2. 6.3 modules/userreference/userreference.module \userreference_user()
  3. 6 modules/userreference/userreference.module \userreference_user()

Implementation of hook_user().

File

modules/userreference/userreference.module, line 857
Defines a field type for referencing a user from a node.

Code

function userreference_user($type, &$edit, &$account) {
  switch ($type) {
    case 'load':

      // Only add links if we are on the user 'view' page.
      if (arg(0) != 'user' || arg(2)) {
        return;
      }

      // find CCK userreference field tables
      // search through them for matching user ids and load those nodes
      $additions = array();
      $types = content_types();

      // Find the table and columns to search through, if the same
      // table comes up in more than one content type, we only need
      // to search it once.
      $search_tables = array();
      foreach ($types as $type_name => $type) {
        foreach ($type['fields'] as $field) {

          // Only add tables when reverse link has been selected.
          if ($field['type'] == 'userreference' && !empty($field['widget']['reverse_link'])) {
            $db_info = content_database_info($field);
            $search_tables[$db_info['table']][] = $db_info['columns']['uid']['column'];
          }
        }
      }
      foreach ($search_tables as $table => $columns) {
        foreach ($columns as $column) {
          $ids = db_query(db_rewrite_sql("SELECT DISTINCT(n.nid), n.title, n.type FROM {node} n LEFT JOIN {" . $table . "} f ON n.vid = f.vid WHERE f." . $column . "=" . $account->uid . " AND n.status = 1"));
          while ($data = db_fetch_object($ids)) {
            $additions[$data->type][$data->nid] = $data->title;
          }
        }
      }
      $account->userreference = $additions;
      break;
    case 'view':
      if (!empty($account->userreference)) {
        $node_types = content_types();
        $additions = array();
        $values = array();
        foreach ($account->userreference as $node_type => $nodes) {
          foreach ($nodes as $nid => $title) {
            $values[$node_type][] = l($title, 'node/' . $nid);
          }
          if (isset($values[$node_type])) {
            $additions[] = array(
              '#type' => 'user_profile_item',
              '#title' => check_plain($node_types[$node_type]['name']),
              '#value' => theme('item_list', $values[$node_type]),
            );
          }
        }
        if ($additions) {
          $account->content['userreference'] = $additions + array(
            '#type' => 'user_profile_category',
            '#attributes' => array(
              'class' => 'user-member',
            ),
            '#title' => t('Related content'),
            '#weight' => 10,
          );
        }
      }
      break;
  }
}