View source
<?php
function heartbeat_entity_info() {
$return = array(
'heartbeat_activity' => array(
'label' => t('Heartbeat Activity Entity'),
'controller class' => 'HeartbeatActivityController',
'base table' => 'heartbeat_activity',
'uri callback' => 'heartbeat_activity_uri',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'uaid',
'bundle' => 'message_id',
'label' => 'message_id',
),
'bundle keys' => array(
'bundle' => 'message_id',
),
'bundles' => array(),
'view modes' => array(
'full' => array(
'label' => t('Full activity message'),
'custom settings' => FALSE,
),
),
'module' => 'heartbeat',
),
);
foreach (heartbeat_templates_names() as $name => $description) {
$return['heartbeat_activity']['bundles'][$name] = array(
'label' => $description,
);
if (module_exists('heartbeat_ui')) {
$return['heartbeat_activity']['bundles'][$name]['admin'] = array(
'path' => 'admin/structure/heartbeat/templates/list/%heartbeat_message_template',
'real path' => 'admin/structure/heartbeat/templates/list/' . $name,
'bundle argument' => 5,
'access arguments' => array(
'admin heartbeat templates',
),
);
}
}
$return['heartbeat_activity_template'] = array(
'label' => t('Heartbeat activity entity type'),
'controller class' => 'HeartbeatTemplateController',
'base table' => 'heartbeat_messages',
'entity keys' => array(
'id' => 'hid',
'label' => 'message_id',
),
'fieldable' => FALSE,
);
return $return;
}
function heartbeat_entity_info_alter(&$existing) {
$existing['user']['view modes']['heartbeat_user'] = array(
'label' => t('Social user mode'),
'custom settings' => FALSE,
);
}
function heartbeat_activity_uri($heartbeatActivity) {
return array(
'path' => 'heartbeat/message/' . $heartbeatActivity->uaid,
);
}
function heartbeat_field_extra_fields() {
$return = array();
$info = entity_get_info('heartbeat_activity');
foreach (array_keys($info['bundles']) as $bundle) {
$return['heartbeat_activity'][$bundle] = array(
'display' => array(
'avatar' => array(
'label' => 'Avatar',
'description' => t('Heartbeat activity avatar element'),
'weight' => 0,
),
'username' => array(
'label' => 'Username',
'description' => t('Heartbeat activity username'),
'weight' => 1,
),
'message' => array(
'label' => 'Message',
'description' => t('Heartbeat activity message element'),
'weight' => 2,
),
'time' => array(
'label' => 'Time',
'description' => t('Heartbeat activity time element'),
'weight' => 3,
),
'buttons' => array(
'label' => 'Buttons',
'description' => t('Heartbeat activity buttons'),
'weight' => 4,
),
),
);
}
$return['user']['user'] = array(
'form' => array(
'heartbeat' => array(
'label' => 'Heartbeat activity templates',
'description' => t('Heartbeat activity template form element.'),
'weight' => 6,
),
),
);
return $return;
}
class HeartbeatActivityController extends DrupalDefaultEntityController {
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = parent::buildQuery($ids, $conditions, $revision_id);
$query
->addTag('translatable');
$query
->addTag('heartbeat_activity_access');
$query
->addField('base', 'variables', 'variables');
$query
->addField('base', 'access', 'access');
$query
->join('users', 'u', 'u.uid = base.uid');
$query
->fields('u');
$query
->leftJoin('heartbeat_user_templates', 'hut', ' base.uid = hut.uid AND base.message_id = hut.message_id ');
$query
->addField('hut', 'status', 'access_status');
$query
->addField('base', 'access', 'access');
$query
->addField('u', 'access', 'users_access');
return $query;
}
protected function cacheGet($ids, $conditions = array()) {
$activities = parent::cacheGet($ids, $conditions);
return $activities;
}
}
class HeartbeatTemplateController extends DrupalDefaultEntityController {
public function load($ids = array(), $conditions = array()) {
ctools_include('export');
return ctools_export_load_object('heartbeat_templates', 'conditions', $conditions);
}
}
function heartbeat_activity_form($form, &$form_state, HeartbeatActivity $heartbeatActivity, HeartbeatStream $heartbeatStream) {
if (!isset($form_state['heartbeat_activity'])) {
if (!isset($heartbeatActivity->title)) {
$heartbeatActivity->title = NULL;
}
$form_state['heartbeat_activity'] = $heartbeatActivity;
}
else {
$heartbeatActivity = $form_state['heartbeat_activity'];
}
$form['#attributes']['class'][] = 'heartbeat-activity-form';
if (!empty($heartbeatActivity->type)) {
$form['#attributes']['class'][] = 'heartbeat-activity-' . $heartbeatActivity->type . '-form';
}
foreach (array(
'uaid',
'uid',
'uid_target',
'nid',
'nid_target',
'timestamp',
'message_id',
'language',
) as $key) {
$form[$key] = array(
'#type' => 'value',
'#value' => isset($heartbeatActivity->{$key}) ? $heartbeatActivity->{$key} : 0,
);
}
$form['#heartbeat_activity'] = $heartbeatActivity;
$form['#heartbeat_stream'] = $heartbeatStream;
$form['name'] = array(
'#type' => 'value',
'#value' => !empty($heartbeatActivity->actor->name) ? $heartbeatActivity->actor->name : '',
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 5,
'#ajax' => array(
'callback' => 'heartbeat_activity_form_callback',
'wrapper' => 'heartbeat-status-wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['#prefix'] = '<div id="heartbeat-status-wrapper">';
$form['#suffix'] = '</div>';
$form['#validate'][] = 'heartbeat_activity_form_validate';
$form['#submit'][] = 'heartbeat_activity_form_submit';
field_attach_form('heartbeat_activity', $heartbeatActivity, $form, $form_state, $heartbeatActivity->language);
return $form;
}
function heartbeat_activity_form_callback(&$form, &$form_state) {
$commands = array();
$args = $form_state['values']['uid'] == $GLOBALS['user']->uid ? array(
0,
) : array(
1,
);
$template_id = $form['#heartbeat_activity']->template->message_id;
$form_id = 'heartbeat_activity_form_' . $template_id;
$heartbeatStream = $form['#heartbeat_stream'];
$template = heartbeat_message_template_load($heartbeatStream->config->settings['show_status_form']);
$heartbeatActivity = new HeartbeatActivity(array(
'actor' => $GLOBALS['user'],
), $template);
$heartbeatStream
->modifyActivityMessage($heartbeatActivity);
$new_form_state = array();
$new_form_state['input'] = array();
$new_form_state['rebuild'] = TRUE;
$new_form = drupal_get_form($form_id . '_' . $_SERVER['REQUEST_TIME'], $heartbeatActivity, $heartbeatStream);
$commands[] = ajax_command_html(NULL, drupal_render($new_form));
$commands[] = ajax_command_invoke(NULL, 'heartbeatNotifyStreams', $args);
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
function heartbeat_activity_form_validate($form, &$form_state) {
}
function heartbeat_activity_form_submit($form, &$form_state) {
$heartbeatActivity = $form_state['heartbeat_activity'];
entity_form_submit_build_entity('heartbeat_activity', $heartbeatActivity, $form, $form_state);
$heartbeatActivity->timestamp = $_SERVER['REQUEST_TIME'];
field_attach_presave('heartbeat_activity', $heartbeatActivity);
module_invoke_all('heartbeat_activity_presave', $heartbeatActivity);
$saved = $heartbeatActivity
->save();
field_attach_insert("heartbeat_activity", $heartbeatActivity);
module_invoke_all("heartbeat_activity_insert", $heartbeatActivity);
module_invoke_all("entity_insert", $heartbeatActivity, 'heartbeat_activity');
entity_get_controller('heartbeat_activity')
->resetCache(array(
$heartbeatActivity->uaid,
));
}