You are here

function redhen_engagements_page in RedHen CRM 7

Page callback for showing a contacts engagements.

Parameters

RedhenContact $contact: Contact to show engagement for.

Return value

array List of engagements.

1 string reference to 'redhen_engagements_page'
redhen_engagement_menu in modules/redhen_engagement/redhen_engagement.module
Implements hook_menu().

File

modules/redhen_engagement/redhen_engagement.module, line 107
RedhenEngagement hook implementations and API

Code

function redhen_engagements_page(RedhenContact $contact) {
  module_load_include('inc', 'redhen', 'includes/redhen.forms');

  // Engagement totals.
  $engagement_totals_header = array(
    t('Score'),
    t('Type'),
  );
  $engagement_totals_rows = array();
  $query = db_select('redhen_engagement', 're')
    ->fields('re', array(
    'engagement_score',
  ))
    ->condition('type', 'engagement', '=')
    ->condition('contact_id', $contact->contact_id, '=')
    ->groupBy('re.engagement_score');
  $query
    ->addExpression('COUNT (engagement_score)', 'count');
  $result = $query
    ->execute();
  if ($result) {
    $engagement_scores = redhen_engagement_get_scores();
    foreach ($result as $record) {
      if (isset($engagement_scores[$record->engagement_score])) {
        $engagement_score = $engagement_scores[$record->engagement_score];
        $engagement_totals_rows[] = array(
          'data' => array(
            'score' => $record->count * $engagement_score->score,
            'type' => check_plain($engagement_score
              ->label()),
          ),
        );
      }
    }
  }

  // Engagements.
  $engagements_header = array(
    'score' => array(
      'data' => t('Score'),
      'field' => 'score',
      'type' => 'property',
      'specifier' => 'score',
    ),
    'type' => array(
      'data' => t('Type'),
      'field' => 'type',
      'type' => 'property',
      'specifier' => 'type',
    ),
    'created' => array(
      'field' => 'created',
      'type' => 'property',
      'data' => t('Created'),
      'sort' => 'desc',
      'specifier' => 'created',
    ),
    'description' => array(
      'data' => t('Description'),
    ),
    'source' => array(
      'data' => t('Source'),
    ),
    'author' => array(
      'data' => t('Author'),
    ),
  );
  $properties = isset($_GET['properties']) ? $_GET['properties'] : array();
  $properties += array(
    'contact_id' => $contact->contact_id,
  );
  $result = redhen_filter_query('redhen_engagement', $engagements_header, '', $properties);
  $engagements_rows = array();
  if ($result) {
    $engagements = entity_load('redhen_engagement', array_keys($result['redhen_engagement']));
    foreach ($engagements as $engagement) {
      $engagement_wrapper = entity_metadata_wrapper('redhen_engagement', $engagement);
      $author = $engagement_wrapper->author;
      if ($author
        ->getIdentifier()) {
        $author_uri = entity_uri('user', $author
          ->value());
        $author_link = l($author->name
          ->value(), $author_uri['path']);
      }
      else {
        $author_link = check_plain($author->name
          ->value());
      }
      $source_link = t('N/A');
      if ($engagement->entity_id) {
        $source_entity = $engagement_wrapper->entity
          ->value();
        $source_uri = entity_uri($engagement->entity_type, $source_entity);
        $source_link = array(
          'data' => array(
            '#type' => 'link',
            '#title' => check_plain($engagement_wrapper->entity
              ->label()),
            '#href' => $source_uri['path'],
          ),
        );
      }
      $engagements_rows[] = array(
        'data' => array(
          'score' => $engagement_wrapper->engagement_score->score
            ->value(),
          'type' => check_plain(entity_label('redhen_engagement_score', $engagement_wrapper->engagement_score
            ->value())),
          'created' => redhen_format_date($engagement->created, 'short'),
          'description' => $engagement->description,
          'source' => $source_link,
          'author' => $author_link,
        ),
      );
    }
  }
  $render = array(
    'form' => drupal_get_form('redhen_filter_form', 'redhen_engagement'),
    'engagement_totals_table' => array(
      '#theme' => 'table',
      '#caption' => 'Engagement score totals',
      '#header' => $engagement_totals_header,
      '#rows' => $engagement_totals_rows,
      '#empty' => t('Sorry, there are no engagements that match your criteria.'),
    ),
    'engagements_table' => array(
      '#theme' => 'table',
      '#caption' => 'Engagement scores',
      '#header' => $engagements_header,
      '#rows' => $engagements_rows,
      '#empty' => t('Sorry, there are no engagements that match your criteria.'),
    ),
    'pager' => array(
      '#theme' => 'pager',
    ),
  );
  return $render;
}