You are here

spaces_contacts.module in Spaces 5

Same filename and directory in other branches
  1. 5.2 spaces_contacts/spaces_contacts.module

File

spaces_contacts/spaces_contacts.module
View source
<?php

/**
 * Implementation of hook_context_define()
 */
function spaces_contacts_context_define() {
  $items = array();
  $items[] = array(
    'namespace' => 'spaces',
    'attribute' => 'feature',
    'value' => 'contacts',
    'block' => array(
      array(
        'module' => 'spaces_contacts',
        'delta' => 'contact_list',
        'region' => 'right',
        'weight' => -11,
      ),
    ),
    'menu' => array(
      'contacts',
    ),
    'spaces' => array(
      'label' => t('Contacts'),
      'description' => t('A contacts section with member listings.'),
      'options' => array(
        0 => t('Disabled'),
        2 => t('Enabled'),
      ),
    ),
  );
  return $items;
}

/**
 * Implementation of hook_block()
 */
function spaces_contacts_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks['contact_list']['info'] = t('Spaces Contacts: Contact List');
    return $blocks;
  }
  else {
    if ($op == 'view') {
      switch ($delta) {
        case 'contact_list':
          $users = _spaces_contacts_users();
          $items = array();
          if ($users) {
            foreach ($users as $account) {
              $item = new stdClass();
              $item->title = theme('username', $account);
              $items[] = theme('datetime_view_style_item', $item);
            }
            $block['content'] = theme('item_list', $items);
          }
          else {
            $block['content'] = "<p>" . t('No contacts found.') . "</p>";
          }
          $block['subject'] = t('Contacts');
          return $block;
      }
    }
  }
}

/**
 * Implementation of hook_menu()
 */
function spaces_contacts_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'contacts',
      'title' => t('Contacts'),
      'description' => t('Provides a group-aware contact listing.'),
      'callback' => 'spaces_contacts_pageview',
      'access' => user_access('access content'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * Implementation of hook_help()
 */
function spaces_contacts_help($page) {
  if (context_get('spaces', 'feature') == 'contacts') {
    return "<p>" . t('Contacts shows the team members that are part of this group and any additional contact information if they have provided it.') . "</p>";
  }
}

/**
 * Implementation of hook_user()
 */
function spaces_contacts_user($op, &$edit, &$account, $category = NULL) {
  if (in_array($op, array(
    'view',
    'form',
  )) && spaces_gid()) {
    context_set('spaces', 'feature', 'contacts');
  }
}

/**
 * Page callback that displays a user contact listing
 */
function spaces_contacts_pageview() {
  drupal_set_title(t('Contacts'));
  context_set('spaces', 'feature', 'contacts');
  context_set('theme', 'layout', 'wide');
  $rows = array();
  $users = _spaces_contacts_users();
  foreach ($users as $account) {
    $row = array(
      theme('user_picture', $account),
      theme('username', $account),
      l($account->mail, 'mailto:' . $account->mail),
    );
    if (module_exists('profile')) {
      $row[] = $account->profile_organization ? $account->profile_organization : null;
    }
    $rows[] = $row;
  }
  $labels = array(
    null,
    t('Name'),
    t('Email'),
  );
  if (module_exists('profile')) {
    $labels[] = t('Organization');
  }

  // wrap the table as if it were produced by a view
  return "<div class='view-content'>" . theme('table', $labels, $rows, array(
    'class' => 'userlist',
  )) . "</div>";
}

/**
 * Wrapper around spaces_get_users() that provides additional support for vcard name fields
 */
function _spaces_contacts_users() {
  $users = spaces_get_users();
  if (module_exists('profile')) {
    foreach ($users as $key => $account) {
      $account = user_load($account);
      profile_load_profile($account);

      // Replace username listing with real name if possible
      if ($account->profile_givenname && $account->profile_familyname) {
        $account->name = $account->profile_givenname . ' ' . $account->profile_familyname;
      }
      else {
        if ($account->profile_givenname) {
          $account->name = $account->profile_givenname;
        }
      }
      $users[$key] = $account;
    }
  }
  return $users;
}

Functions

Namesort descending Description
spaces_contacts_block Implementation of hook_block()
spaces_contacts_context_define Implementation of hook_context_define()
spaces_contacts_help Implementation of hook_help()
spaces_contacts_menu Implementation of hook_menu()
spaces_contacts_pageview Page callback that displays a user contact listing
spaces_contacts_user Implementation of hook_user()
_spaces_contacts_users Wrapper around spaces_get_users() that provides additional support for vcard name fields