You are here

function userreference_user in Content Construction Kit (CCK) 5

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

Implementation of hook_user().

File

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

Code

function userreference_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    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();
      $search_links = 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' && $field['widget']['reverse_link']) {
            $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->content = $additions;
      return;
    case 'view':
      if (!empty($account->content)) {
        $node_types = content_types();
        $links = array();
        $values = array();
        foreach ($account->content 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])) {
            $links[$node_type] = array(
              'title' => check_plain($node_types[$node_type]['name']),
              'value' => theme('item_list', $values[$node_type]),
            );
          }
        }
        if ($links) {
          return array(
            t('Related Content') => $links,
          );
        }
      }
      break;
  }
}