privatemsg_rules.rules.inc

  1. 7-1 privatemsg_rules/privatemsg_rules.rules.inc
  2. 7-2 privatemsg_rules/privatemsg_rules.rules.inc
  3. 6-2 privatemsg_rules/privatemsg_rules.rules.inc

Hooks and callback functions for rules.module integration.

File

privatemsg_rules/privatemsg_rules.rules.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Hooks and callback functions for rules.module integration.
  5. */
  6. /**
  7. * Implements hook_rules_action_info().
  8. *
  9. * @ingroup rules
  10. */
  11. function privatemsg_rules_rules_action_info() {
  12. // Re-use existing information and also get information about fields added
  13. // to private messages.
  14. $fields = entity_get_all_property_info('privatemsg_message');
  15. // Remove properties that can't be used here.
  16. unset($fields['mid']);
  17. unset($fields['thread_id']);
  18. unset($fields['timestamp']);
  19. // Remove fields, they are not supported yet.
  20. foreach (array_keys($fields) as $key) {
  21. if (strpos($key, 'field_') === 0) {
  22. unset($fields[$key]);
  23. }
  24. }
  25. // Update some descriptions and other definitions.
  26. $fields['subject']['description'] = t('Subject of the message. To use the tokens provided by Privatemsg, use {token}, for example: {privatemsg_message:recipient:name}.');
  27. $fields['body']['description'] = t('Body of the message. To use the tokens provided by Privatemsg, use {token}, for example: {privatemsg_message:recipient:name}.');
  28. $fields['body']['long'] = TRUE;
  29. // Make subject and body translatable.
  30. $fields['subject']['translatable'] = TRUE;
  31. $fields['body']['translatable'] = TRUE;
  32. // Reply doesn't have subject either.
  33. $reply_fields = $fields;
  34. unset($reply_fields['subject']);
  35. $actions = array(
  36. 'privatemsg_rules_new' => array(
  37. 'label' => t('Send a message'),
  38. 'named parameter' => TRUE,
  39. 'parameter' => array(
  40. 'recipient' => array(
  41. 'type' => 'user',
  42. 'label' => t('Recipient'),
  43. 'description' => t('Recipient of the message.'),
  44. ),
  45. ) + $fields,
  46. 'new variables' => array(
  47. 'thread_id' => array(
  48. 'type' => 'integer',
  49. 'label' => t('ID of new thread'),
  50. ),
  51. ),
  52. 'group' => t('Private messages'),
  53. ),
  54. 'privatemsg_rules_reply' => array(
  55. 'label' => t('Reply to a message'),
  56. 'named parameter' => TRUE,
  57. 'parameter' => array(
  58. 'thread_id' => array(
  59. 'type' => 'integer',
  60. 'label' => t('Privatemsg thread id'),
  61. 'description' => t('Thread ID of the thread that should be responded to.')
  62. ),
  63. ) + $reply_fields,
  64. 'group' => t('Private messages'),
  65. ),
  66. 'privatemsg_rules_unread_count' => array(
  67. 'label' => t('Load number of unread messages'),
  68. 'parameter' => array(
  69. 'account' => array(
  70. 'type' => 'user',
  71. 'label' => t('User account'),
  72. 'description' => t('Specify the user for which the number of unread messages should be loaded.'),
  73. ),
  74. ),
  75. 'new variables' => array(
  76. 'unread_count' => array(
  77. 'type' => 'integer',
  78. 'label' => t('Number of unread messages'),
  79. ),
  80. ),
  81. 'group' => t('Private messages'),
  82. ),
  83. );
  84. if (module_exists('privatemsg_filter')) {
  85. $actions['privatemsg_rules_tag_thread'] = array(
  86. 'label' => t('Tag a privatemsg thread'),
  87. 'parameter' => array(
  88. 'thread_id' => array(
  89. 'type' => 'integer',
  90. 'label' => t('Specify the thread ID that should be tagged.'),
  91. ),
  92. 'account' => array(
  93. 'type' => 'user',
  94. 'label' => t('Specify for which user the message should tagged.'),
  95. ),
  96. 'privatemsg_tag' => array(
  97. 'type' => 'text',
  98. 'label' => t('Name of the tag that should be added.'),
  99. 'description' => t('Multiple tags can be specified, separated by a comma.'),
  100. ),
  101. ),
  102. 'group' => t('Private messages'),
  103. );
  104. }
  105. if (module_exists('privatemsg_roles')) {
  106. $actions['privatemsg_rules_new_role'] = array(
  107. 'label' => t('Send a message to a role'),
  108. 'named parameter' => TRUE,
  109. 'parameter' => array(
  110. 'roles' => array(
  111. 'type' => 'list<integer>',
  112. 'label' => t('Roles'),
  113. 'options list' => 'entity_metadata_user_roles',
  114. 'description' => t('Select the roles whose users should receive the message.'),
  115. ),
  116. ) + $fields,
  117. 'new variables' => array(
  118. 'thread_id' => array(
  119. 'type' => 'integer',
  120. 'label' => t('ID of new thread'),
  121. ),
  122. ),
  123. 'group' => t('Private messages'),
  124. );
  125. }
  126. return $actions;
  127. }
  128. /**
  129. * Implements hook_rules_event_info().
  130. *
  131. * @ingroup rules
  132. */
  133. function privatemsg_rules_rules_event_info() {
  134. return array(
  135. 'privatemsg_insert' => array(
  136. 'label' => t('A message is sent'),
  137. 'group' => t('Private messages'),
  138. 'variables' => array(
  139. 'privatemsg_message' => array(
  140. 'type' => 'privatemsg_message',
  141. 'bundle' => 'privatemsg_message',
  142. 'label' => t('Private message'),
  143. ),
  144. 'recipient' => array(
  145. 'type' => 'user',
  146. 'label' => t('Recipient of the message.'),
  147. ),
  148. ),
  149. ),
  150. );
  151. }
  152. /**
  153. * Implements hook_rules_condition_info().
  154. *
  155. * @ingroup rules
  156. */
  157. function privatemsg_rules_rules_condition_info() {
  158. if (module_exists('privatemsg_filter')) {
  159. return array(
  160. 'privatemsg_rules_thread_has_tag' => array(
  161. 'label' => t('Thread has tag'),
  162. 'parameter' => array(
  163. 'thread_id' => array(
  164. 'type' => 'integer',
  165. 'label' => t('Specify the thread ID that should be checked.'),
  166. ),
  167. 'account' => array(
  168. 'type' => 'user',
  169. 'label' => t('Specify the user that should be checked.'),
  170. ),
  171. 'privatemsg_tag' => array(
  172. 'type' => 'text',
  173. 'label' => t('Name of the tag for which should be checked.'),
  174. ),
  175. 'check_all' => array(
  176. 'type' => 'boolean',
  177. 'label' => t('Only return TRUE if all tags are found.'),
  178. ),
  179. ),
  180. 'group' => t('Private messages'),
  181. ),
  182. );
  183. }
  184. }
  185. /**
  186. * Converts temporary token format ({token}) to real tokens.
  187. */
  188. function privatemsg_rules_replace_tokens($text) {
  189. return preg_replace('/{([a-z0-9_:]+)}/', '[$1]', $text);
  190. }
  191. /**
  192. * Helper function for sending a new message.
  193. */
  194. function privatemsg_rules_new($args, $element) {
  195. // Recipient could be a wrapped entity. They do not play nice
  196. // together with the Privatemsg API because that one assumes recipient
  197. // objects which have additional properties like type.
  198. $recipient = $args['recipient'];
  199. if ($recipient instanceof EntityDrupalWrapper) {
  200. $recipient = user_load($recipient->getIdentifier());
  201. }
  202. // Enforce recipient type values.
  203. $recipient->recipient = $recipient->uid;
  204. $recipient->type = 'user';
  205. // Replace token placeholders.
  206. $args['subject'] = privatemsg_rules_replace_tokens($args['subject']);
  207. $args['body'] = privatemsg_rules_replace_tokens($args['body']);
  208. rules_log('Writing new message with subject %subject to %user from %author', array('%subject' => $args['subject'], '%user' => $recipient->name, '%author' => $args['author']->name));
  209. $result = privatemsg_new_thread(array($recipient), $args['subject'], $args['body'], $args);
  210. if ($result['success']) {
  211. rules_log('New message successfully sent, !link', array('!link' => l($args['subject'], 'messages/view/' . $result['message']->thread_id)));
  212. }
  213. else {
  214. foreach ($result['messages']['error'] as $args) {
  215. rules_log('Message not sent, reported error: !error', array('!error' => $args), RulesLog::ERROR);
  216. }
  217. }
  218. foreach ($result['messages']['warning'] as $args) {
  219. rules_log('Warning message reported when trying to send message: !warning', array('!warning' => $args), RulesLog::WARN);
  220. }
  221. // Return thread ID of new thread (or NULL if failed).
  222. if ($result['success']) {
  223. return array(
  224. 'thread_id' => $result['message']->thread_id,
  225. );
  226. }
  227. else {
  228. return array(
  229. 'thread_id' => NULL,
  230. );
  231. }
  232. }
  233. /**
  234. * Implements ACTIONBASE_form_alter().
  235. */
  236. function privatemsg_rules_new_form_alter(&$form, &$form_state) {
  237. $form['parameter']['subject']['settings']['subject']['#type'] = 'textfield';
  238. }
  239. /**
  240. * Helper function for sending a new message to a role.
  241. */
  242. function privatemsg_rules_new_role($args, $element) {
  243. $args['subject'] = privatemsg_rules_replace_tokens($args['subject']);
  244. $args['body'] = privatemsg_rules_replace_tokens($args['body']);
  245. $recipient_array = array();
  246. $recipient_names = array();
  247. foreach ($args['roles'] as $rid) {
  248. $recipient_array[$rid] = user_role_load($rid);
  249. $recipient_array[$rid]->type = 'role';
  250. $recipient_array[$rid]->recipient = $recipient_array[$rid]->rid;
  251. $recipient_names[] = $recipient_array[$rid]->name;
  252. }
  253. $recipient_names = implode(', ', $recipient_names);
  254. rules_log('Writing new message with subject %subject to roles %roles from %author.', array('%subject' => $args['subject'], '%author' => $args['author']->name, '%roles' => $recipient_names));
  255. $result = privatemsg_new_thread($recipient_array, $args['subject'], $args['body'], $args);
  256. if ($result['success']) {
  257. rules_log('New message successfully sent, !link.', array('!link' => l($args['subject'], 'messages/view/' . $result['message']->thread_id)));
  258. }
  259. else {
  260. foreach ($result['messages']['error'] as $message) {
  261. rules_log('Message not sent, reported error: !error.', array('!error' => $message), RulesLog::ERROR);
  262. }
  263. }
  264. foreach ($result['messages']['warning'] as $message) {
  265. rules_log('Warning message reported when trying to send message: !warning.', array('!warning' => $message), RulesLog::WARN);
  266. }
  267. // Return thread ID of new thread (or NULL if failed).
  268. if ($result['success']) {
  269. return array(
  270. 'thread_id' => $result['message']->thread_id,
  271. );
  272. }
  273. else {
  274. return array(
  275. 'thread_id' => NULL,
  276. );
  277. }
  278. }
  279. /**
  280. * Implements ACTIONBASE_form_alter().
  281. */
  282. function privatemsg_rules_new_role_form_alter(&$form, &$form_state) {
  283. $form['parameter']['subject']['settings']['subject']['#type'] = 'textfield';
  284. }
  285. /**
  286. * Helper function for replying to a thread.
  287. */
  288. function privatemsg_rules_reply($args, $element) {
  289. $args['body'] = privatemsg_rules_replace_tokens($args['body']);
  290. rules_log('Writing reply to thread %thread_id from %author', array('%thread_id' => $args['thread_id'], '%author' => $args['author']->name));
  291. $result = privatemsg_reply($args['thread_id'], $args['body'], $args);
  292. if ($result['success']) {
  293. rules_log('New message successfully sent, !link', array('!link' => l(drupal_substr($args['body'], 0, 15), 'messages/view/' . $result['message']->thread_id)));
  294. }
  295. else {
  296. foreach ($result['messages']['error'] as $message) {
  297. rules_log('Message not sent, reported error: !error', array('!error' => $message), RulesLog::ERROR);
  298. }
  299. }
  300. foreach ($result['messages']['warning'] as $message) {
  301. rules_log('Warning message reported when trying to send message: !warning', array('!warning' => $message), RulesLog::WARN);
  302. }
  303. }
  304. /**
  305. * Implements ACTIONBASE_form_alter().
  306. */
  307. function privatemsg_rules_thread_has_tag_form_alter(&$form, &$form_state) {
  308. $form['parameter']['privatemsg_tag']['settings']['privatemsg_tag']['#type'] = 'textfield';
  309. $form['parameter']['privatemsg_tag']['settings']['privatemsg_tag']['#autocomplete_path'] = 'messages/filter/tag-autocomplete';
  310. }
  311. /**
  312. * Check if a thread has a specific tag.
  313. *
  314. * @param $thread_id
  315. * Which thread to check.
  316. * @param $account
  317. * Which user should be checked.
  318. * @param $tag_settings
  319. * Rules settings, includes the tag that should be checked.
  320. *
  321. * @return
  322. * TRUE if the thread has such a tag, FALSe otherwise.
  323. */
  324. function privatemsg_rules_thread_has_tag($thread_id, $account, $tag_string, $check_all = FALSE) {
  325. $tags = explode(',', $tag_string);
  326. $tag_ids = privatemsg_filter_create_tags($tags);
  327. if (empty($tag_ids)) {
  328. rules_log('No valid tag could be loaded or created.', array(), RulesLog::ERROR);
  329. return;
  330. }
  331. $query = _privatemsg_assemble_query(array('tags', 'privatemsg_filter'), $account, array($thread_id));
  332. $tag_ids_on_thread = $query->execute()->fetchCol();
  333. if ($check_all) {
  334. // If check_all is set, only return TRUE if all tags exist.
  335. if (count(array_diff($tag_ids, $tag_ids_on_thread)) == 0) {
  336. return TRUE;
  337. }
  338. }
  339. else {
  340. // If not, then it is enough it at least one of the submitted tags is used
  341. // on the thread.
  342. if (count(array_diff($tag_ids, $tag_ids_on_thread)) < count($tag_ids)) {
  343. return TRUE;
  344. }
  345. }
  346. return FALSE;
  347. }
  348. /**
  349. * Implements ACTIONBASE_form_alter().
  350. */
  351. function privatemsg_rules_tag_thread_form_alter(&$form, &$form_state) {
  352. $form['parameter']['privatemsg_tag']['settings']['privatemsg_tag']['#type'] = 'textfield';
  353. $form['parameter']['privatemsg_tag']['settings']['privatemsg_tag']['#autocomplete_path'] = 'messages/filter/tag-autocomplete';
  354. }
  355. function privatemsg_rules_tag_thread($thread_id, $account, $tag_string) {
  356. $tags = explode(',', $tag_string);
  357. $tag_ids = privatemsg_filter_create_tags($tags);
  358. if (empty($tag_ids)) {
  359. rules_log('No valid tag could be loaded or created.', array(), RulesLog::ERROR);
  360. return;
  361. }
  362. privatemsg_filter_add_tags(array($thread_id), $tag_ids, $account);
  363. }
  364. function privatemsg_rules_unread_count($account) {
  365. return array(
  366. 'unread_count' => privatemsg_unread_count($account),
  367. );
  368. }

Functions

Namesort descending Description
privatemsg_rules_new Helper function for sending a new message.
privatemsg_rules_new_form_alter Implements ACTIONBASE_form_alter().
privatemsg_rules_new_role Helper function for sending a new message to a role.
privatemsg_rules_new_role_form_alter Implements ACTIONBASE_form_alter().
privatemsg_rules_replace_tokens Converts temporary token format ({token}) to real tokens.
privatemsg_rules_reply Helper function for replying to a thread.
privatemsg_rules_rules_action_info Implements hook_rules_action_info().
privatemsg_rules_rules_condition_info Implements hook_rules_condition_info().
privatemsg_rules_rules_event_info Implements hook_rules_event_info().
privatemsg_rules_tag_thread
privatemsg_rules_tag_thread_form_alter Implements ACTIONBASE_form_alter().
privatemsg_rules_thread_has_tag Check if a thread has a specific tag.
privatemsg_rules_thread_has_tag_form_alter Implements ACTIONBASE_form_alter().
privatemsg_rules_unread_count