View source
<?php
module_load_include('inc', 'heartbeat_plugins', 'includes/heartbeat_plugin');
function heartbeat_plugins_ctools_plugin_api($owner, $api) {
if ($owner == 'heartbeat_plugins' && ($api == 'plugins' || $api == 'heartbeat_plugins')) {
return array(
'version' => 1,
);
}
}
function heartbeat_plugins_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && ($plugin == 'export_ui' || $plugin == 'heartbeat_plugins')) {
return 'plugins/' . $plugin;
}
}
function heartbeat_plugins_heartbeat_plugin_info() {
$plugin_names = array();
$plugin_name = new HeartbeatPluginWrapper();
$plugin_name->disabled = FALSE;
$plugin_name->api_version = 1;
$plugin_name->plugin_name = 'activitystatus';
$plugin_name->label = 'User activity status form above stream';
$plugin_name->module = 'heartbeat_plugins';
$plugin_name->settings = array();
$plugin_names['activitystatus'] = $plugin_name;
if (module_exists('flag')) {
$plugin_name = new HeartbeatPluginWrapper();
$plugin_name->disabled = FALSE;
$plugin_name->api_version = 1;
$plugin_name->plugin_name = 'flagattachment';
$plugin_name->label = 'Flag link attachments with a count';
$plugin_name->module = 'heartbeat_plugins';
$plugin_name->settings = array(
'attachment' => 1,
'count_enabled' => 1,
);
$plugin_names['flagattachment'] = $plugin_name;
}
if (module_exists('shoutbox')) {
$plugin_name = new HeartbeatPluginWrapper();
$plugin_name->disabled = FALSE;
$plugin_name->api_version = 1;
$plugin_name->plugin_name = 'shoutbox';
$plugin_name->label = 'Shoutbox form above stream';
$plugin_name->module = 'heartbeat_plugins';
$plugin_name->settings = array();
$plugin_names['shoutbox'] = $plugin_name;
}
if (module_exists('og')) {
$plugin_name = new HeartbeatPluginWrapper();
$plugin_name->disabled = FALSE;
$plugin_name->api_version = 1;
$plugin_name->plugin_name = 'og';
$plugin_name->label = 'Organic group streams and access restriction specific to groups';
$plugin_name->module = 'heartbeat_plugins';
$plugin_name->settings = array();
$plugin_names['og'] = $plugin_name;
}
return $plugin_names;
}
function heartbeat_plugins_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'ctools_export_ui_edit_item_form') {
if (isset($form['info']['message_id'])) {
_heartbeat_plugins_template_attachments_sort_form($form, $form_state);
}
if (isset($form['info']['class']) && isset($form['real_class'])) {
foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
$plugin = $pluginWrapper
->getPlugin();
if ($plugin && $plugin
->adaptsStream()) {
$plugin
->pluginStreamForm($form, $form_state);
}
}
}
}
}
function heartbeat_plugins_delete($plugin) {
db_delete('heartbeat_plugins')
->condition('plugin_name', $plugin->plugin_name)
->execute();
cache_clear_all('heartbeat_plugins', 'cache');
}
function heartbeat_plugins_permission() {
$permissions = array(
'admin heartbeat plugins' => array(
'title' => t('Administer heartbeat plugins'),
'description' => t('Manage the heartbeat plugins.'),
),
'post activity statuses' => array(
'title' => t('Post activity statuses'),
'description' => t('Post heartbeat activity statuses.'),
),
);
return $permissions;
}
function heartbeat_plugins_menu() {
$items = array();
foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
if ($plugin = $pluginWrapper
->getPlugin()) {
$items += $plugin
->getMenuItems();
}
}
return $items;
}
function heartbeat_plugins_theme() {
$registry = array(
'heartbeat_attachments' => array(
'variables' => array(
'heartbeat_activity' => NULL,
),
),
'heartbeat_plugins_attachments_form' => array(
'render element' => 'form',
),
'heartbeat_flagging' => array(
'variables' => array(
'heartbeat_activity' => NULL,
),
),
);
return $registry;
}
function heartbeat_plugins_init() {
heartbeat_plugins_get_active_plugins(TRUE);
}
function heartbeat_plugins_flag_definitions() {
$definitions = array();
foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
$plugin = $pluginWrapper
->getPlugin();
if ($plugin instanceof HeartbeatFlagPlugin && $plugin
->hasFlagDefinitions()) {
$definitions += $plugin
->flagDefinitions();
}
}
return $definitions;
}
function heartbeat_plugins_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(
'attachments' => array(
'label' => 'Attachments',
'description' => t('Heartbeat activity attachments'),
'weight' => 4,
),
),
);
}
return $return;
}
function heartbeat_plugins_flag_default_flags() {
$flags = array();
foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
$plugin = $pluginWrapper
->getPlugin();
if ($plugin instanceof HeartbeatFlagPlugin && $plugin
->hasDefaultFlags()) {
$flags += $plugin
->flagDefaults();
}
}
return $flags;
}
function heartbeat_plugins_token_info() {
$tokens = array();
if (module_exists('flag')) {
$flags = flag_get_flags('heartbeat_activity');
foreach ($flags as $flag) {
$tokens['heartbeat_activity']['flag-' . str_replace('_', '-', $flag->name) . '-count-linked'] = array(
'name' => t('@flag flag count', array(
'@flag' => $flag
->get_title(),
)),
'description' => t('Total flag count for flag @flag', array(
'@flag' => $flag
->get_title(),
)),
);
}
}
return array(
'tokens' => $tokens,
);
}
function heartbeat_plugins_tokens($type, $tokens, array $data = array(), array $options = array()) {
ctools_include('modal');
ctools_include('ajax');
$url_options = array(
'absolute' => TRUE,
);
$url_options['alias'] = TRUE;
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
}
else {
$language_code = NULL;
}
$sanitize = !empty($options['sanitize']);
$replacements = array();
if ($type == 'heartbeat_activity' && !empty($data['heartbeat_activity'])) {
$flags = flag_get_flags('heartbeat_activity');
$object = $data['heartbeat_activity'];
foreach ($flags as $flag) {
$flag_token = 'flag-' . str_replace('_', '-', $flag->name) . '-count-linked';
foreach ($tokens as $name => $original) {
if ($name == $flag_token) {
$count = $flag
->get_count($flag
->get_content_id($object));
$output = ctools_modal_text_button($count == 1 ? t('One person') : t('@count people', array(
'@count' => $count,
)), 'heartbeat/nojs/flagged/' . $object->uaid, t('Show people'), 'ctools-modal-ctools-heartbeat-style');
$replacements[$original] = $output;
}
}
}
}
return $replacements;
}
function heartbeat_plugins_heartbeat_stream_load(HeartbeatStream $heartbeatStream) {
foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
$plugin = $pluginWrapper
->getPlugin();
if ($plugin) {
$plugin
->streamLoaded($heartbeatStream);
}
}
}
function heartbeat_plugins_heartbeat_activity_delete($uaids, $all) {
foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
$plugin = $pluginWrapper
->getPlugin();
if ($plugin) {
$plugin
->activityRemoved($uaids, $all);
}
}
}
function heartbeat_plugins_heartbeat_activity_view(HeartbeatActivity $heartbeatActivity, $view_mode = 'full', $language = NULL) {
$heartbeatActivity->content['attachments'] = array(
'#markup' => '',
);
if ($heartbeatActivity->template
->hasPluginAttachments()) {
$heartbeatActivity->content['attachments']['#markup'] = theme('heartbeat_attachments', array(
'heartbeat_activity' => $heartbeatActivity,
));
}
}
function heartbeat_plugins_heartbeat_theme_alter(&$messages, HeartbeatStream $stream) {
foreach (array_keys($messages) as $key) {
$pluginLoaders = array();
if (isset($messages[$key]->template->attachments['buttons'])) {
foreach ($messages[$key]->template->attachments['buttons']['enabled'] as $id => $enabled) {
if ($enabled) {
$pluginLoaders[$id] = $id;
}
}
}
if (isset($messages[$key]->template->attachments['content'])) {
foreach ($messages[$key]->template->attachments['content']['enabled'] as $id => $enabled) {
if ($enabled) {
$pluginLoaders[$id] = $id;
}
}
}
foreach ($pluginLoaders as $pluginId => $info) {
$parts = explode(":", $pluginId);
$pluginName = $parts[0];
$name = isset($parts[1]) ? $parts[1] : NULL;
$pluginWrapper = heartbeat_plugins_get_plugin($pluginName);
if (!is_object($pluginWrapper)) {
continue;
}
$plugin = $pluginWrapper
->getPlugin();
if ($plugin instanceof HeartbeatBasePlugin) {
$plugin
->setStream($stream);
}
$plugin
->loadAttachments($messages[$key], $name);
}
}
}
function heartbeat_plugins_preprocess_heartbeat_attachments(&$variables) {
$pluginAttachments = isset($variables['heartbeat_activity']->additions->plugins) ? $variables['heartbeat_activity']->additions->plugins : array();
foreach ($pluginAttachments as $pluginId => $plugin) {
if ($plugin
->hasAttachmentsContent()) {
$plugin
->renderAttachmentsContent($variables['heartbeat_activity']);
}
}
$variables['attachments'] = $variables['heartbeat_activity']->attachments;
}
function heartbeat_plugins_preprocess_heartbeat_buttons(&$variables) {
$pluginAttachments = isset($variables['heartbeat_activity']->additions->plugins) ? $variables['heartbeat_activity']->additions->plugins : array();
foreach ($pluginAttachments as $pluginId => $plugin) {
if ($plugin
->hasAttachmentsButtons()) {
$plugin
->renderButtons($variables['heartbeat_activity']);
}
}
}
function theme_heartbeat_attachments($variables) {
$output = '<div class="heartbeat-attachments">';
$output .= implode(' ', $variables['attachments']);
$output .= '</div>';
return $output;
}
function heartbeat_plugins_get_plugin($plugin_name) {
$plugins =& drupal_static('heartbeat_plugins');
if (empty($plugins)) {
$plugins = heartbeat_plugins_get_active_plugins(TRUE);
}
foreach ($plugins as $plugin) {
if ($plugin->plugin_name == $plugin_name) {
return $plugin;
}
}
return NULL;
}
function heartbeat_plugins_get_active_plugins($include = FALSE) {
$plugins =& drupal_static('heartbeat_plugins');
if (!isset($plugins)) {
$plugins = array();
if ($object = cache_get('heartbeat_plugins')) {
$plugins = $object->data;
}
else {
ctools_include('export');
foreach (ctools_export_crud_load_all('heartbeat_plugins') as $plugin) {
if (empty($plugin->disabled)) {
$plugins[] = $plugin;
}
}
cache_set('heartbeat_plugins', $plugins);
}
}
if (!empty($plugins)) {
if ($include) {
foreach ($plugins as $plugin) {
module_load_include('inc', 'heartbeat_plugins', 'plugins/' . $plugin->plugin_name);
}
}
}
return $plugins;
}
function _heartbeat_plugins_template_attachments_sort_form(&$form, &$form_state) {
$attachments = array();
foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
$plugin = $pluginWrapper
->getPlugin();
if ($plugin) {
$item_attachments = $form_state['item']->attachments;
if ($plugin
->hasButtons()) {
$attachments['buttons'][$pluginWrapper->plugin_name] = array(
'plugin' => $plugin,
'values' => isset($item_attachments['buttons'][$pluginWrapper->plugin_name]) ? $item_attachments['buttons'][$pluginWrapper->plugin_name] : array(),
);
}
if ($plugin
->hasContent()) {
$attachments['content'][$pluginWrapper->plugin_name] = array(
'plugin' => $plugin,
'values' => isset($item_attachments['content'][$pluginWrapper->plugin_name]) ? $item_attachments['content'][$pluginWrapper->plugin_name] : array(),
);
}
}
}
$item = $form_state['item'];
$values = $item->attachments;
if (!empty($attachments['buttons'])) {
if (!isset($form_state['item']->attachments['buttons'])) {
$form_state['item']->attachments['buttons'] = array();
}
$form['attachments']['buttons'] = array(
'#tree' => TRUE,
'#type' => 'fieldset',
);
$form['attachments']['buttons']['title'] = array(
'#markup' => t('Attachment Buttons'),
);
$items = array();
foreach ($attachments['buttons'] as $class_name => $plugin_attachments) {
foreach ($plugin_attachments['plugin']
->getAttachments($item, 'buttons') as $attachment) {
$attachment['enabled'] = isset($values['buttons']['enabled'][$attachment['name']]) ? $values['buttons']['enabled'][$attachment['name']] : $attachment['enabled'];
$attachment['weight'] = isset($values['buttons']['weight'][$attachment['name']]) ? $values['buttons']['weight'][$attachment['name']] : $attachment['weight'];
$attachment['plugin'] = $plugin_attachments['plugin'];
$items[] = $attachment;
}
}
usort($items, '_heartbeat_attachment_sort_helper');
$options = array();
$enabled = array();
$form['attachments']['buttons']['weight'] = array(
'#tree' => TRUE,
);
foreach ($items as $attachment) {
$key = $attachment['name'];
$options[$key] = '';
if ($attachment['enabled']) {
$enabled[] = $key;
}
$form['attachments']['buttons']['weight'][$key] = array(
'#type' => 'weight',
'#title' => t('Weight for @title', array(
'@title' => $attachment['title'],
)),
'#title_display' => 'invisible',
'#default_value' => $attachment['weight'],
'#attributes' => array(
'class' => array(
'heartbeat-attachment-order-weight',
),
),
);
$form['attachments']['buttons']['settings'][$key] = array(
'#type' => 'container',
);
$form['attachments']['buttons']['name'][$key] = array(
'#markup' => check_plain($attachment['title']),
);
$attachment['plugin']
->pluginAttachmentForm($form['attachments']['buttons']['settings'][$key], $form_state['item']->attachments['buttons'], 'buttons');
}
$form['attachments']['buttons']['enabled'] = array(
'#type' => 'checkboxes',
'#title' => t('Enabled attachments'),
'#title_display' => 'invisible',
'#options' => $options,
'#default_value' => $enabled,
);
$form['attachments']['buttons']['#theme'] = 'heartbeat_plugins_attachments_form';
}
if (!empty($attachments['content'])) {
if (!isset($form_state['item']->attachments['content'])) {
$form_state['item']->attachments['content'] = array();
}
$form['attachments']['content'] = array(
'#tree' => TRUE,
'#type' => 'fieldset',
);
$form['attachments']['content']['title'] = array(
'#markup' => t('Attachments content'),
);
$items = array();
foreach ($attachments['content'] as $class_name => $plugin_attachments) {
foreach ($plugin_attachments['plugin']
->getAttachments($item, 'content') as $attachment) {
$attachment['enabled'] = isset($values['content']['enabled'][$attachment['name']]) ? $values['content']['enabled'][$attachment['name']] : $attachment['enabled'];
$attachment['weight'] = isset($values['content']['weight'][$attachment['name']]) ? $values['content']['weight'][$attachment['name']] : $attachment['weight'];
$attachment['plugin'] = $plugin_attachments['plugin'];
$items[] = $attachment;
}
}
usort($items, '_heartbeat_attachment_sort_helper');
$options = array();
$enabled = array();
$form['attachments']['content']['weight'] = array(
'#tree' => TRUE,
);
foreach ($items as $attachment) {
$key = $attachment['name'];
$options[$key] = '';
if ($attachment['enabled']) {
$enabled[] = $key;
}
$form['attachments']['content']['weight'][$key] = array(
'#type' => 'weight',
'#title' => t('Weight for @title', array(
'@title' => $attachment['title'],
)),
'#title_display' => 'invisible',
'#default_value' => $attachment['weight'],
'#attributes' => array(
'class' => array(
'heartbeat-attachment-order-weight',
),
),
);
$form['attachments']['content']['settings'][$key] = array(
'#type' => 'container',
);
$form['attachments']['content']['name'][$key] = array(
'#markup' => check_plain($attachment['title']),
);
$attachment['plugin']
->pluginAttachmentForm($form['attachments']['content']['settings'][$key], $form_state['item']->attachments['content'], 'content');
}
$form['attachments']['content']['enabled'] = array(
'#type' => 'checkboxes',
'#title' => t('Enabled attachments'),
'#title_display' => 'invisible',
'#options' => $options,
'#default_value' => $enabled,
);
$form['attachments']['content']['#theme'] = 'heartbeat_plugins_attachments_form';
}
}
function _heartbeat_attachment_sort_helper($a, $b) {
$a_weight = is_array($a) && isset($a['weight']) ? $a['weight'] : 0;
$b_weight = is_array($b) && isset($b['weight']) ? $b['weight'] : 0;
return $a_weight - $b_weight;
}
function theme_heartbeat_plugins_attachments_form($variables) {
$form = $variables['form'];
$weight_class = 'heartbeat-attachment-order-weight';
$table_id = 'heartbeat-attachments-table';
$headers = array();
$headers[] = drupal_render($form['title']);
$headers[] = t('Enabled');
$headers[] = t('Settings');
$headers[] = t('Weight');
foreach ($form['name'] as $key => $element) {
if (is_array($element) && element_child($key)) {
$title = drupal_render($form['name'][$key]);
$form['enabled'][$key]['#title'] = t('Enable !title', array(
'!title' => $title,
));
$form['enabled'][$key]['#title_display'] = 'invisible';
$rows[] = array(
'data' => array(
'<strong>' . $title . '</strong>',
array(
'data' => drupal_render($form['enabled'][$key]),
'align' => 'center',
),
drupal_render($form['settings_button'][$key]) . drupal_render($form['settings'][$key]),
drupal_render($form['weight'][$key]),
),
'class' => array(
'draggable',
),
);
}
}
$output = theme('table', array(
'header' => $headers,
'rows' => $rows,
'attributes' => array(
'id' => $table_id,
),
));
$output .= drupal_render_children($form);
drupal_add_tabledrag($table_id, 'order', 'sibling', $weight_class);
return $output;
}
function theme_heartbeat_flagging($variables) {
$newarray = theme('image_style', array(
'style_name' => 'activity_avatar',
'path' => $variables['uri'],
'title' => $variables['name'],
'attributes' => array(
'class' => 'avatar',
),
));
return '<div class="heartbeat-username">' . l($variables['name'], 'user/' . $variables['uid'], array(
'html' => TRUE,
)) . '</div><div class="heartbeat-avatar">' . l($newarray, 'user/' . $variables['uid'], array(
'html' => TRUE,
)) . '</div>';
}