heartbeat_plugins.module

Tracking 7.x-1.x branch
  1. drupal
    1. 7 contributions/heartbeat/modules/heartbeat_plugins/heartbeat_plugins.module

This file includes plugins for heartbeat by configuration. The plugins are ctools exportables.

Functions & methods

NameDescription
heartbeat_plugins_ctools_plugin_apiImplements hook_ctools_plugin_api().
heartbeat_plugins_ctools_plugin_directoryImplements hook_ctools_plugin_directory().
heartbeat_plugins_deleteCTools delete callback for plugins.
heartbeat_plugins_field_extra_fieldsImplements hook_field_extra_fields().
heartbeat_plugins_flag_default_flagsImplements hook_flag_default_flags().
heartbeat_plugins_flag_definitionsImplements of hook_flag_definitions().
heartbeat_plugins_form_alterImplements hook_form_alter().
heartbeat_plugins_get_active_pluginsHelper function to retrieve all active plugins.
heartbeat_plugins_get_pluginHelper function to retrieve an active plugin.
heartbeat_plugins_heartbeat_activity_deleteImplements of hook_heartbeat_activity_delete().
heartbeat_plugins_heartbeat_activity_viewImplements hook_heartbeat_activity_view().
heartbeat_plugins_heartbeat_plugin_infoImplementation of hook_heartbeat_plugin_info(). Built-in default plugins that can be used with heartbeat.
heartbeat_plugins_heartbeat_stream_loadImplements hook_heartbeat_stream_load().
heartbeat_plugins_heartbeat_theme_alterImplementation of hook_heartbeat_theme_alter().
heartbeat_plugins_initImplements hook_init().
heartbeat_plugins_menuImplements hook_menu().
heartbeat_plugins_permissionImplements hook_permission().
heartbeat_plugins_preprocess_heartbeat_attachmentsPreprocess function for the heartbeat activity attachments.
heartbeat_plugins_preprocess_heartbeat_buttonsPreprocess function for the heartbeat activity buttons.
heartbeat_plugins_themeImplements hook_theme().
heartbeat_plugins_tokensImplements hook_tokens().
heartbeat_plugins_token_infoImplements hook_token_info().
theme_heartbeat_attachmentsTheme function for messages attachments.
theme_heartbeat_flaggingTheme function for flagged users (default is clickable avatar).
theme_heartbeat_plugins_attachments_formTheme function for the draggable attachments form.
_heartbeat_attachment_sort_helperHelper function to sort an array for the Plugin Attachments.
_heartbeat_plugins_template_attachments_sort_formHelper function to create draggable interface for heartbeat plugins.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * This file includes plugins for heartbeat by configuration.
  5. * The plugins are ctools exportables.
  6. */
  7. module_load_include('inc', 'heartbeat_plugins', 'includes/heartbeat_plugin');
  8. /**
  9. * Implements hook_ctools_plugin_api().
  10. */
  11. function heartbeat_plugins_ctools_plugin_api($owner, $api) {
  12. if ($owner == 'heartbeat_plugins' && ($api == 'plugins' || $api == 'heartbeat_plugins')) {
  13. return array('version' => 1);
  14. }
  15. }
  16. /**
  17. * Implements hook_ctools_plugin_directory().
  18. */
  19. function heartbeat_plugins_ctools_plugin_directory($module, $plugin) {
  20. if ($module == 'ctools' && ($plugin == 'export_ui' || $plugin == 'heartbeat_plugins')) {
  21. return 'plugins/' . $plugin;
  22. }
  23. }
  24. /**
  25. * Implementation of hook_heartbeat_plugin_info().
  26. * Built-in default plugins that can be used with heartbeat.
  27. */
  28. function heartbeat_plugins_heartbeat_plugin_info() {
  29. $plugin_names = array();
  30. $plugin_name = new HeartbeatPluginWrapper;
  31. $plugin_name->disabled = FALSE; /* Edit this to true to make a default plugin_name disabled initially */
  32. $plugin_name->api_version = 1;
  33. $plugin_name->plugin_name = 'activitystatus';
  34. $plugin_name->label = 'User activity status form above stream';
  35. $plugin_name->module = 'heartbeat_plugins';
  36. $plugin_name->settings = array();
  37. $plugin_names['activitystatus'] = $plugin_name;
  38. if (module_exists('flag')) {
  39. $plugin_name = new HeartbeatPluginWrapper;
  40. $plugin_name->disabled = FALSE; /* Edit this to true to make a default plugin_name disabled initially */
  41. $plugin_name->api_version = 1;
  42. $plugin_name->plugin_name = 'flagattachment';
  43. $plugin_name->label = 'Flag link attachments with a count';
  44. $plugin_name->module = 'heartbeat_plugins';
  45. $plugin_name->settings = array(
  46. 'attachment' => 1,
  47. 'count_enabled' => 1,
  48. );
  49. $plugin_names['flagattachment'] = $plugin_name;
  50. }
  51. if (module_exists('shoutbox')) {
  52. $plugin_name = new HeartbeatPluginWrapper;
  53. $plugin_name->disabled = FALSE; /* Edit this to true to make a default plugin_name disabled initially */
  54. $plugin_name->api_version = 1;
  55. $plugin_name->plugin_name = 'shoutbox';
  56. $plugin_name->label = 'Shoutbox form above stream';
  57. $plugin_name->module = 'heartbeat_plugins';
  58. $plugin_name->settings = array();
  59. $plugin_names['shoutbox'] = $plugin_name;
  60. }
  61. if (module_exists('og')) {
  62. $plugin_name = new HeartbeatPluginWrapper;
  63. $plugin_name->disabled = FALSE; /* Edit this to true to make a default plugin_name disabled initially */
  64. $plugin_name->api_version = 1;
  65. $plugin_name->plugin_name = 'og';
  66. $plugin_name->label = 'Organic group streams and access restriction specific to groups';
  67. $plugin_name->module = 'heartbeat_plugins';
  68. $plugin_name->settings = array();
  69. $plugin_names['og'] = $plugin_name;
  70. }
  71. return $plugin_names;
  72. }
  73. /**
  74. * Implements hook_form_alter().
  75. *
  76. * @param array $form
  77. * @param array $form_state
  78. */
  79. function heartbeat_plugins_form_alter(&$form, &$form_state, $form_id) {
  80. // Hook into the ctools edit form for heartbeat message templates.
  81. if ($form_id == 'ctools_export_ui_edit_item_form') {
  82. // Hook into the message template form.
  83. if (isset($form['info']['message_id'])) {
  84. _heartbeat_plugins_template_attachments_sort_form($form, $form_state);
  85. }
  86. // Hook into the stream configuration form.
  87. if (isset($form['info']['class']) && isset($form['real_class'])) {
  88. foreach(heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
  89. $plugin = $pluginWrapper->getPlugin();
  90. if ($plugin && $plugin->adaptsStream()) {
  91. $plugin->pluginStreamForm($form, $form_state);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * CTools delete callback for plugins.
  99. */
  100. function heartbeat_plugins_delete($plugin) {
  101. db_delete('heartbeat_plugins')
  102. ->condition('plugin_name', $plugin->plugin_name)
  103. ->execute();
  104. // Clear the cache for heartbeat plugins.
  105. cache_clear_all('heartbeat_plugins', 'cache');
  106. }
  107. /**
  108. * Implements hook_permission().
  109. */
  110. function heartbeat_plugins_permission() {
  111. $permissions = array(
  112. 'admin heartbeat plugins' => array(
  113. 'title' => t('Administer heartbeat plugins'),
  114. 'description' => t('Manage the heartbeat plugins.')
  115. ),
  116. 'post activity statuses' => array(
  117. 'title' => t('Post activity statuses'),
  118. 'description' => t('Post heartbeat activity statuses.')
  119. ),
  120. );
  121. return $permissions;
  122. }
  123. /**
  124. * Implements hook_menu().
  125. */
  126. function heartbeat_plugins_menu() {
  127. $items = array();
  128. foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
  129. if ($plugin = $pluginWrapper->getPlugin()) {
  130. $items += $plugin->getMenuItems();
  131. }
  132. }
  133. return $items;
  134. }
  135. /**
  136. * Implements hook_theme().
  137. */
  138. function heartbeat_plugins_theme() {
  139. $registry = array(
  140. 'heartbeat_attachments' => array(
  141. 'variables' => array('heartbeat_activity' => NULL),
  142. ),
  143. 'heartbeat_plugins_attachments_form' => array(
  144. 'render element' => 'form',
  145. ),
  146. 'heartbeat_flagging' => array(
  147. 'variables' => array('heartbeat_activity' => NULL),
  148. ),
  149. );
  150. return $registry;
  151. }
  152. /**
  153. * Implements hook_init().
  154. */
  155. function heartbeat_plugins_init() {
  156. heartbeat_plugins_get_active_plugins(TRUE);
  157. }
  158. /**
  159. * Implements of hook_flag_definitions().
  160. */
  161. function heartbeat_plugins_flag_definitions() {
  162. $definitions = array();
  163. foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
  164. $plugin = $pluginWrapper->getPlugin();
  165. if ($plugin instanceof HeartbeatFlagPlugin && $plugin->hasFlagDefinitions()) {
  166. $definitions += $plugin->flagDefinitions();
  167. }
  168. }
  169. return $definitions;
  170. }
  171. /**
  172. * Implements hook_field_extra_fields().
  173. */
  174. function heartbeat_plugins_field_extra_fields() {
  175. $return = array();
  176. $info = entity_get_info('heartbeat_activity');
  177. foreach (array_keys($info['bundles']) as $bundle) {
  178. $return['heartbeat_activity'][$bundle] = array(
  179. 'display' => array(
  180. 'attachments' => array(
  181. 'label' => 'Attachments',
  182. 'description' => t('Heartbeat activity attachments'),
  183. 'weight' => 4,
  184. ),
  185. ),
  186. );
  187. }
  188. return $return;
  189. }
  190. /**
  191. * Implements hook_flag_default_flags().
  192. */
  193. function heartbeat_plugins_flag_default_flags() {
  194. $flags = array();
  195. foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
  196. $plugin = $pluginWrapper->getPlugin();
  197. if ($plugin instanceof HeartbeatFlagPlugin && $plugin->hasDefaultFlags()) {
  198. $flags += $plugin->flagDefaults();
  199. }
  200. }
  201. return $flags;
  202. }
  203. /**
  204. * Implements hook_token_info().
  205. */
  206. function heartbeat_plugins_token_info() {
  207. // Handy tokens for heartbeat_activity.
  208. $tokens = array();
  209. if (module_exists('flag')) {
  210. $flags = flag_get_flags('heartbeat_activity');
  211. foreach ($flags as $flag) {
  212. $tokens['heartbeat_activity']['flag-' . str_replace('_', '-', $flag->name) . '-count-linked'] = array(
  213. 'name' => t('@flag flag count', array('@flag' => $flag->get_title())),
  214. 'description' => t('Total flag count for flag @flag', array('@flag' => $flag->get_title())),
  215. );
  216. }
  217. }
  218. return array(
  219. 'tokens' => $tokens,
  220. );
  221. }
  222. /**
  223. * Implements hook_tokens().
  224. */
  225. function heartbeat_plugins_tokens($type, $tokens, array $data = array(), array $options = array()) {
  226. ctools_include('modal');
  227. ctools_include('ajax');
  228. $url_options = array('absolute' => TRUE);
  229. $url_options['alias'] = TRUE;
  230. if (isset($options['language'])) {
  231. $url_options['language'] = $options['language'];
  232. $language_code = $options['language']->language;
  233. }
  234. else {
  235. $language_code = NULL;
  236. }
  237. $sanitize = !empty($options['sanitize']);
  238. $replacements = array();
  239. if ($type == 'heartbeat_activity' && !empty($data['heartbeat_activity'])) {
  240. $flags = flag_get_flags('heartbeat_activity');
  241. $object = $data['heartbeat_activity'];
  242. foreach ($flags as $flag) {
  243. $flag_token = 'flag-'. str_replace('_', '-', $flag->name) .'-count-linked';
  244. foreach ($tokens as $name => $original) {
  245. if ($name == $flag_token) {
  246. $count = $flag->get_count($flag->get_content_id($object));
  247. $output = ctools_modal_text_button($count == 1 ? t('One person') : t('@count people', array('@count' => $count)),
  248. 'heartbeat/nojs/flagged/' . $object->uaid,
  249. t('Show people'),
  250. 'ctools-modal-ctools-heartbeat-style');
  251. $replacements[$original] = $output;
  252. }
  253. }
  254. }
  255. }
  256. return $replacements;
  257. }
  258. /**
  259. * Implements hook_heartbeat_stream_load().
  260. *
  261. * TODO Refactor this for two reasons.
  262. * First is the plugins should be attached to the stream if needed (cfr heartbeatActivity)
  263. * Second is that the plugin loaded here loads too often and it is invisible to any other
  264. * point in the process where we could have picked in.
  265. */
  266. function heartbeat_plugins_heartbeat_stream_load(HeartbeatStream $heartbeatStream) {
  267. // Always invoke the streamLoaded on every plugin.
  268. foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
  269. $plugin = $pluginWrapper->getPlugin();
  270. // Notify the plugin that there is a stream loaded.
  271. if ($plugin) {
  272. $plugin->streamLoaded($heartbeatStream);
  273. // This is the method that will stay, replacing the one above.
  274. //$heartbeatStream->executePlugin($plugin);
  275. }
  276. }
  277. }
  278. /**
  279. * Implements of hook_heartbeat_activity_delete().
  280. *
  281. * Delete the attachments from a heartbeat activity object.
  282. * @param $message
  283. * HeartbeatActivity ID
  284. */
  285. function heartbeat_plugins_heartbeat_activity_delete($uaids, $all) {
  286. foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
  287. $plugin = $pluginWrapper->getPlugin();
  288. if ($plugin) {
  289. $plugin->activityRemoved($uaids, $all);
  290. }
  291. }
  292. }
  293. /**
  294. * Implements hook_heartbeat_activity_view().
  295. *
  296. * @param HeartbeatActivity $heartbeatActivity
  297. * The activity message object.
  298. */
  299. function heartbeat_plugins_heartbeat_activity_view(HeartbeatActivity $heartbeatActivity, $view_mode = 'full', $language = NULL) {
  300. $heartbeatActivity->content['attachments'] = array('#markup' => '');
  301. if ($heartbeatActivity->template->hasPluginAttachments()) {
  302. $heartbeatActivity->content['attachments']['#markup'] = theme('heartbeat_attachments', array('heartbeat_activity' => $heartbeatActivity));
  303. }
  304. }
  305. /**
  306. * Implementation of hook_heartbeat_theme_alter().
  307. *
  308. * In this phase we want to tell all plugins attached to a message
  309. * they can perform any loading tasks or any other data that needs to
  310. * be available to later display (E.g. buttons and content).
  311. */
  312. function heartbeat_plugins_heartbeat_theme_alter(&$messages, HeartbeatStream $stream) {
  313. // Evaluate each message to attach plugin content or buttons.
  314. foreach (array_keys($messages) as $key) {
  315. $pluginLoaders = array();
  316. if (isset($messages[$key]->template->attachments['buttons'])) {
  317. foreach ($messages[$key]->template->attachments['buttons']['enabled'] as $id => $enabled) if ($enabled) {
  318. $pluginLoaders[$id] = $id;
  319. }
  320. }
  321. if (isset($messages[$key]->template->attachments['content'])) {
  322. foreach ($messages[$key]->template->attachments['content']['enabled'] as $id => $enabled) if ($enabled) {
  323. $pluginLoaders[$id] = $id;
  324. }
  325. }
  326. // Alter the messages by attaching buttons.
  327. foreach ($pluginLoaders as $pluginId => $info) {
  328. $parts = explode(":", $pluginId);
  329. $pluginName = $parts[0];
  330. $name = isset($parts[1]) ? $parts[1] : NULL;
  331. // Prepare a PluginWrapper.
  332. $pluginWrapper = heartbeat_plugins_get_plugin($pluginName);
  333. if (!is_object($pluginWrapper)) {
  334. continue;
  335. }
  336. // Get a plugin to work with.
  337. $plugin = $pluginWrapper->getPlugin();
  338. // TODO This needs a check if the plugin is enabled (in this context).
  339. if ($plugin instanceof HeartbeatBasePlugin) {
  340. $plugin->setStream($stream);
  341. }
  342. // Load attachments.
  343. $plugin->loadAttachments($messages[$key], $name);
  344. }
  345. }
  346. }
  347. /**
  348. * Preprocess function for the heartbeat activity attachments.
  349. */
  350. function heartbeat_plugins_preprocess_heartbeat_attachments(&$variables) {
  351. $pluginAttachments = isset($variables['heartbeat_activity']->additions->plugins) ? $variables['heartbeat_activity']->additions->plugins : array();
  352. foreach ($pluginAttachments as $pluginId => $plugin) {
  353. if ($plugin->hasAttachmentsContent()) {
  354. $plugin->renderAttachmentsContent($variables['heartbeat_activity']);
  355. }
  356. }
  357. $variables['attachments'] = $variables['heartbeat_activity']->attachments;
  358. }
  359. /**
  360. * Preprocess function for the heartbeat activity buttons.
  361. */
  362. function heartbeat_plugins_preprocess_heartbeat_buttons(&$variables) {
  363. $pluginAttachments = isset($variables['heartbeat_activity']->additions->plugins) ? $variables['heartbeat_activity']->additions->plugins : array();
  364. foreach ($pluginAttachments as $pluginId => $plugin) {
  365. if ($plugin->hasAttachmentsButtons()) {
  366. $plugin->renderButtons($variables['heartbeat_activity']);
  367. }
  368. }
  369. }
  370. /**
  371. * Theme function for messages attachments.
  372. *
  373. * @param $variables
  374. * Array of variables available for output.
  375. */
  376. function theme_heartbeat_attachments($variables) {
  377. $output = '<div class="heartbeat-attachments">';
  378. $output .= implode(' ', $variables['attachments']);
  379. $output .= '</div>';
  380. return $output;
  381. }
  382. /**
  383. * Helper function to retrieve an active plugin.
  384. *
  385. * @param Boolean $include
  386. * Indicates whether the plugins needs to be loaded/included.
  387. */
  388. function heartbeat_plugins_get_plugin($plugin_name) {
  389. $plugins = &drupal_static('heartbeat_plugins');
  390. if (empty($plugins)) {
  391. $plugins = heartbeat_plugins_get_active_plugins(TRUE);
  392. }
  393. foreach ($plugins as $plugin) {
  394. if ($plugin->plugin_name == $plugin_name) {
  395. return $plugin;
  396. }
  397. }
  398. return NULL;
  399. }
  400. /**
  401. * Helper function to retrieve all active plugins.
  402. *
  403. * @param Boolean $include
  404. * Indicates whether the plugins needs to be loaded/included.
  405. */
  406. function heartbeat_plugins_get_active_plugins($include = FALSE) {
  407. $plugins = &drupal_static('heartbeat_plugins');
  408. if (!isset($plugins)) {
  409. $plugins = array();
  410. // Load from cache if possible.
  411. if ($object = cache_get('heartbeat_plugins')) {
  412. $plugins = $object->data;
  413. }
  414. // Ctools loads the plugins by default, we'll save it in cache.
  415. else {
  416. ctools_include('export');
  417. foreach(ctools_export_crud_load_all('heartbeat_plugins') as $plugin) {
  418. if (empty($plugin->disabled)) {
  419. $plugins[] = $plugin;
  420. }
  421. }
  422. cache_set('heartbeat_plugins', $plugins);
  423. }
  424. }
  425. if (!empty($plugins)) {
  426. if ($include) {
  427. foreach ($plugins as $plugin) {
  428. // Load our plugin file.
  429. module_load_include('inc', 'heartbeat_plugins', 'plugins/' . $plugin->plugin_name);
  430. }
  431. }
  432. }
  433. return $plugins;
  434. }
  435. /**
  436. * Helper function to create draggable interface for heartbeat plugins.
  437. */
  438. function _heartbeat_plugins_template_attachments_sort_form(& $form, & $form_state) {
  439. // Prepare the attachments for all active plugins willing to participate.
  440. $attachments = array();
  441. foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
  442. $plugin = $pluginWrapper->getPlugin();
  443. if ($plugin) {
  444. $item_attachments = $form_state['item']->attachments;
  445. // Create a list of attachment buttons provided by the plugins.
  446. if ($plugin->hasButtons()) {
  447. $attachments['buttons'][$pluginWrapper->plugin_name] = array(
  448. 'plugin' => $plugin,
  449. 'values' => isset($item_attachments['buttons'][$pluginWrapper->plugin_name]) ? $item_attachments['buttons'][$pluginWrapper->plugin_name] : array()
  450. );
  451. }
  452. // Create a list of attachments (content) provided by the plugins.
  453. if ($plugin->hasContent()) {
  454. $attachments['content'][$pluginWrapper->plugin_name] = array(
  455. 'plugin' => $plugin,
  456. 'values' => isset($item_attachments['content'][$pluginWrapper->plugin_name]) ? $item_attachments['content'][$pluginWrapper->plugin_name] : array()
  457. );
  458. }
  459. }
  460. }
  461. $item = $form_state['item'];
  462. $values = $item->attachments;
  463. /**
  464. * Create wrapper for Button Attachments.
  465. */
  466. if (!empty($attachments['buttons'])) {
  467. if (!isset($form_state['item']->attachments['buttons'])) {
  468. $form_state['item']->attachments['buttons'] = array();
  469. }
  470. $form['attachments']['buttons'] = array('#tree' => TRUE, '#type' => 'fieldset');
  471. $form['attachments']['buttons']['title'] = array('#markup' => t('Attachment Buttons'));
  472. // Prepare a sorted list of Attachment Buttons.
  473. $items = array();
  474. foreach ($attachments['buttons'] as $class_name => $plugin_attachments) {
  475. foreach ($plugin_attachments['plugin']->getAttachments($item, 'buttons') as $attachment) {
  476. $attachment['enabled'] = isset($values['buttons']['enabled'][$attachment['name']]) ? $values['buttons']['enabled'][$attachment['name']] : $attachment['enabled'];
  477. $attachment['weight'] = isset($values['buttons']['weight'][$attachment['name']]) ? $values['buttons']['weight'][$attachment['name']] : $attachment['weight'];
  478. $attachment['plugin'] = $plugin_attachments['plugin'];
  479. $items[] = $attachment;
  480. }
  481. }
  482. usort($items, '_heartbeat_attachment_sort_helper');
  483. $options = array();
  484. $enabled = array();
  485. $form['attachments']['buttons']['weight'] = array('#tree' => TRUE);
  486. foreach ($items as $attachment) {
  487. $key = $attachment['name'];
  488. $options[$key] = '';
  489. if ($attachment['enabled']) {
  490. $enabled[] = $key;
  491. }
  492. $form['attachments']['buttons']['weight'][$key] = array(
  493. '#type' => 'weight',
  494. '#title' => t('Weight for @title', array('@title' => $attachment['title'])),
  495. '#title_display' => 'invisible',
  496. '#default_value' => $attachment['weight'],
  497. '#attributes' => array('class' => array('heartbeat-attachment-order-weight')),
  498. );
  499. $form['attachments']['buttons']['settings'][$key] = array(
  500. '#type' => 'container',
  501. );
  502. $form['attachments']['buttons']['name'][$key] = array('#markup' => check_plain($attachment['title']));
  503. $attachment['plugin']->pluginAttachmentForm($form['attachments']['buttons']['settings'][$key], $form_state['item']->attachments['buttons'], 'buttons');
  504. }
  505. $form['attachments']['buttons']['enabled'] = array(
  506. '#type' => 'checkboxes',
  507. '#title' => t('Enabled attachments'),
  508. '#title_display' => 'invisible',
  509. '#options' => $options,
  510. '#default_value' => $enabled,
  511. );
  512. $form['attachments']['buttons']['#theme'] = 'heartbeat_plugins_attachments_form';
  513. }
  514. /**
  515. * Create wrapper for Attachments.
  516. */
  517. if (!empty($attachments['content'])) {
  518. if (!isset($form_state['item']->attachments['content'])) {
  519. $form_state['item']->attachments['content'] = array();
  520. }
  521. $form['attachments']['content'] = array('#tree' => TRUE, '#type' => 'fieldset');
  522. $form['attachments']['content']['title'] = array('#markup' => t('Attachments content'));
  523. // Prepare a sorted list of Attachment Content.
  524. $items = array();
  525. foreach ($attachments['content'] as $class_name => $plugin_attachments) {
  526. foreach ($plugin_attachments['plugin']->getAttachments($item, 'content') as $attachment) {
  527. $attachment['enabled'] = isset($values['content']['enabled'][$attachment['name']]) ? $values['content']['enabled'][$attachment['name']] : $attachment['enabled'];
  528. $attachment['weight'] = isset($values['content']['weight'][$attachment['name']]) ? $values['content']['weight'][$attachment['name']] : $attachment['weight'];
  529. $attachment['plugin'] = $plugin_attachments['plugin'];
  530. $items[] = $attachment;
  531. }
  532. }
  533. usort($items, '_heartbeat_attachment_sort_helper');
  534. $options = array();
  535. $enabled = array();
  536. $form['attachments']['content']['weight'] = array('#tree' => TRUE);
  537. foreach ($items as $attachment) {
  538. $key = $attachment['name'];
  539. $options[$key] = '';
  540. if ($attachment['enabled']) {
  541. $enabled[] = $key;
  542. }
  543. $form['attachments']['content']['weight'][$key] = array(
  544. '#type' => 'weight',
  545. '#title' => t('Weight for @title', array('@title' => $attachment['title'])),
  546. '#title_display' => 'invisible',
  547. '#default_value' => $attachment['weight'],
  548. '#attributes' => array('class' => array('heartbeat-attachment-order-weight')),
  549. );
  550. $form['attachments']['content']['settings'][$key] = array(
  551. '#type' => 'container',
  552. );
  553. $form['attachments']['content']['name'][$key] = array('#markup' => check_plain($attachment['title']));
  554. $attachment['plugin']->pluginAttachmentForm($form['attachments']['content']['settings'][$key], $form_state['item']->attachments['content'], 'content');
  555. }
  556. $form['attachments']['content']['enabled'] = array(
  557. '#type' => 'checkboxes',
  558. '#title' => t('Enabled attachments'),
  559. '#title_display' => 'invisible',
  560. '#options' => $options,
  561. '#default_value' => $enabled,
  562. );
  563. $form['attachments']['content']['#theme'] = 'heartbeat_plugins_attachments_form';
  564. }
  565. }
  566. /**
  567. * Helper function to sort an array for the Plugin Attachments.
  568. */
  569. function _heartbeat_attachment_sort_helper($a, $b) {
  570. $a_weight = (is_array($a) && isset($a['weight']) ? $a['weight'] : 0);
  571. $b_weight = (is_array($b) && isset($b['weight']) ? $b['weight'] : 0);
  572. return $a_weight - $b_weight;
  573. }
  574. /**
  575. * Theme function for the draggable attachments form.
  576. */
  577. function theme_heartbeat_plugins_attachments_form($variables) {
  578. $form = $variables['form'];
  579. // Special ID and classes for draggable tables.
  580. $weight_class = 'heartbeat-attachment-order-weight';
  581. $table_id = 'heartbeat-attachments-table';
  582. // Build up a table of applicable fields.
  583. $headers = array();
  584. $headers[] = drupal_render($form['title']);
  585. $headers[] = t('Enabled');
  586. $headers[] = t('Settings');
  587. $headers[] = t('Weight');
  588. foreach ($form['name'] as $key => $element) {
  589. // Do not take form control structures.
  590. if (is_array($element) && element_child($key)) {
  591. $title = drupal_render($form['name'][$key]);
  592. $form['enabled'][$key]['#title'] = t('Enable !title', array('!title' => $title));
  593. $form['enabled'][$key]['#title_display'] = 'invisible';
  594. $rows[] = array(
  595. 'data' => array(
  596. '<strong>' . $title . '</strong>',
  597. array('data' => drupal_render($form['enabled'][$key]), 'align' => 'center'),
  598. drupal_render($form['settings_button'][$key]) . drupal_render($form['settings'][$key]),
  599. drupal_render($form['weight'][$key]),
  600. ),
  601. 'class' => array('draggable'),
  602. );
  603. }
  604. }
  605. $output = theme('table', array('header' => $headers, 'rows' => $rows, 'attributes' => array('id' => $table_id)));
  606. $output .= drupal_render_children($form);
  607. drupal_add_tabledrag($table_id, 'order', 'sibling', $weight_class);
  608. return $output;
  609. }
  610. /**
  611. * Theme function for flagged users (default is clickable avatar).
  612. */
  613. function theme_heartbeat_flagging($variables) {
  614. $newarray = theme('image_style', array(
  615. 'style_name' => 'activity_avatar',
  616. 'path' => $variables['uri'],
  617. 'title' => $variables['name'],
  618. 'attributes' => array('class' => 'avatar')));
  619. 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>';
  620. }