You are here

function userreference_user in Content Construction Kit (CCK) 6

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

Implementation of hook_user().

File

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

Code

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

      // 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();
      $search_links = array();
      foreach ($types as $type_name => $type) {
        foreach ($type['fields'] as $field) {
          if ($field['type'] == 'userreference') {
            $db_info = content_database_info($field);
            $search_tables[$db_info['table']] = $db_info['columns']['uid']['column'];
            $search_links[$db_info['table']] = $field['widget']['reverse_link'];
          }
        }
      }
      foreach ($search_tables as $table => $column) {
        $ids = db_query(db_rewrite_sql("SELECT DISTINCT(n.nid) 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)) {

          // TODO, do we really want a complete node_load() here? We only need the title to create a link.
          $node = node_load($data->nid);
          $node->reverse_link = $search_links[$table];
          $additions[$node->type][] = $node;
        }
      }
      $account->userreference = $additions;
      return;
      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 $node) {
            if ($node->reverse_link) {
              $values[$node_type][] = l($node->title, 'node/' . $node->nid);
            }
          }
          if (isset($values[$node_type])) {
            $additions[] = array(
              '#type' => 'user_profile_item',
              '#title' => $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;
  }
}