flag.actions.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * @file
  4. * Hooks for flag actions.
  5. */
  6. /**
  7. * Implements hook_trigger_info().
  8. */
  9. function flag_trigger_info() {
  10. $hooks = array(
  11. 'flag' => array(
  12. 'flag_flag' => array(
  13. 'label' => t('Object has been flagged with any flag'),
  14. ),
  15. 'flag_unflag' => array(
  16. 'label' => t('Object has been unflagged with any flag'),
  17. ),
  18. ),
  19. );
  20. foreach (flag_get_flags() as $flag) {
  21. $hooks['flag']['flag_flag_' . $flag->name]['label'] = t('A %type has been flagged with %name', array('%type' => $flag->entity_type, '%name' => $flag->name));
  22. $hooks['flag']['flag_unflag_' . $flag->name]['label'] = t('A %type has been unflagged with %name', array('%type' => $flag->entity_type, '%name' => $flag->name));
  23. }
  24. return $hooks;
  25. }
  26. /**
  27. * Implements hook_action_info().
  28. */
  29. function flag_action_info() {
  30. return array(
  31. 'flag_node_action' => array(
  32. 'type' => 'node',
  33. 'label' => t('Flag (or unflag) a node'),
  34. 'configurable' => TRUE,
  35. 'triggers' => array(
  36. 'node_presave', 'node_insert', 'node_update', 'node_delete', 'node_view',
  37. 'comment_insert', 'comment_update', 'comment_delete', 'comment_view',
  38. ),
  39. ),
  40. 'flag_comment_action' => array(
  41. 'type' => 'comment',
  42. 'label' => t('Flag (or unflag) a comment'),
  43. 'configurable' => TRUE,
  44. 'triggers' => array(
  45. 'comment_insert', 'comment_update', 'comment_delete', 'comment_view',
  46. ),
  47. ),
  48. 'flag_user_action' => array(
  49. 'type' => 'user',
  50. 'label' => t('Flag (or unflag) a user'),
  51. 'configurable' => TRUE,
  52. 'triggers' => array(
  53. 'user_insert', 'user_update', 'user_delete', 'user_login', 'user_logout', 'user_view',
  54. ),
  55. ),
  56. );
  57. }
  58. /**
  59. * Implements hook_action_info_alter().
  60. *
  61. * Enable Flag actions on Node, Comment, and User hooks without
  62. * the trigger_unlock.module.
  63. */
  64. function flag_action_info_alter(&$actions) {
  65. $node_flags = flag_get_flags('node');
  66. $comment_flags = flag_get_flags('comment');
  67. $user_flags = flag_get_flags('user');
  68. foreach ($actions as $name => $action) {
  69. if (strpos($name, 'node') === 0) {
  70. $actions[$name]['triggers'][] = 'flag_flag';
  71. $actions[$name]['triggers'][] = 'flag_unflag';
  72. foreach ($node_flags as $flag) {
  73. $actions[$name]['triggers'][] = 'flag_flag_' . $flag->name;
  74. $actions[$name]['triggers'][] = 'flag_unflag_' . $flag->name;
  75. }
  76. }
  77. if (strpos($name, 'comment') === 0) {
  78. $actions[$name]['triggers'][] = 'flag_flag';
  79. $actions[$name]['triggers'][] = 'flag_unflag';
  80. foreach ($comment_flags as $flag) {
  81. $actions[$name]['triggers'][] = 'flag_flag_' . $flag->name;
  82. $actions[$name]['triggers'][] = 'flag_unflag_' . $flag->name;
  83. }
  84. }
  85. if (strpos($name, 'user') === 0) {
  86. $actions[$name]['triggers'][] = 'flag_flag';
  87. $actions[$name]['triggers'][] = 'flag_unflag';
  88. foreach ($user_flags as $flag) {
  89. $actions[$name]['triggers'][] = 'flag_flag_' . $flag->name;
  90. $actions[$name]['triggers'][] = 'flag_unflag_' . $flag->name;
  91. }
  92. }
  93. }
  94. }
  95. /**
  96. * Implements Drupal action. Flags a node.
  97. *
  98. * Note the first parameter is "object" because it may be a comment or a node.
  99. */
  100. function flag_node_action(&$object, $context = array()) {
  101. if ($flag = flag_get_flag($context['flag_action']['flag'])) {
  102. $account = isset($context['account']) ? $context['account'] : $GLOBALS['user'];
  103. $flag->flag($context['flag_action']['op'], $object->nid, $account, TRUE);
  104. }
  105. }
  106. /**
  107. * Form for configuring the Flag node action.
  108. */
  109. function flag_node_action_form($context = array()) {
  110. return flag_action_form($context, 'node');
  111. }
  112. /**
  113. * Submit function for the Flag node action form.
  114. */
  115. function flag_node_action_submit($form, $form_state) {
  116. return flag_action_submit($form, $form_state);
  117. }
  118. /**
  119. * Implements Drupal action. Flags a comment.
  120. */
  121. function flag_comment_action(&$comment, $context = array()) {
  122. if ($flag = flag_get_flag($context['flag_action']['flag'])) {
  123. $account = isset($context['account']) ? $context['account'] : $GLOBALS['user'];
  124. $flag->flag($context['flag_action']['op'], $comment->cid, $account, TRUE);
  125. }
  126. }
  127. /**
  128. * Form for configuring the Flag comment action.
  129. */
  130. function flag_comment_action_form($context) {
  131. return flag_action_form($context, 'comment');
  132. }
  133. /**
  134. * Submit function for the Flag comment action form.
  135. */
  136. function flag_comment_action_submit($form, $form_state) {
  137. return flag_action_submit($form, $form_state);
  138. }
  139. /**
  140. * Implements Drupal action. Flags a user.
  141. */
  142. function flag_user_action(&$user, $context = array()) {
  143. if ($flag = flag_get_flag($context['flag_action']['flag'])) {
  144. $account = isset($context['account']) ? $context['account'] : $GLOBALS['user'];
  145. $flag->flag($context['flag_action']['op'], $user->uid, $account, TRUE);
  146. }
  147. }
  148. /**
  149. * Form for configuring the Flag user action.
  150. */
  151. function flag_user_action_form($context) {
  152. return flag_action_form($context, 'user');
  153. }
  154. /**
  155. * Submit function for the Flag user action form.
  156. */
  157. function flag_user_action_submit($form, $form_state) {
  158. return flag_action_submit($form, $form_state);
  159. }
  160. /**
  161. * Generic form for configuring Flag actions.
  162. *
  163. * @param $context
  164. * The current action context.
  165. * @param $entity_type
  166. * The entity type applicable to this action, such as "node" or "comment".
  167. */
  168. function flag_action_form($context, $entity_type) {
  169. $form = array();
  170. $flags = flag_get_flags($entity_type);
  171. // If this is a flag_action action, do not allow the triggering flag.
  172. if (isset($context['actions_flag'])) {
  173. unset($flags[$context['actions_flag']]);
  174. }
  175. $options = drupal_map_assoc(array_keys($flags));
  176. $form['flag_action']['#tree'] = TRUE;
  177. $form['flag_action']['warning'] = array(
  178. '#markup' => '<div class="messages status">' . t("Note when setting a flag through actions, the selected flag will be flagged regardless of the user's permissions.") . '</div>',
  179. );
  180. $form['flag_action']['flag'] = array(
  181. '#title' => t('Flag to affect'),
  182. '#type' => 'radios',
  183. '#options' => $options,
  184. '#required' => TRUE,
  185. '#description' => t('When this action is fired, which flag should be flagged (or unflagged)?'),
  186. '#default_value' => isset($context['flag_action']['flag']) ? $context['flag_action']['flag'] : reset($options),
  187. );
  188. $form['flag_action']['op'] = array(
  189. '#title' => t('Flag operation'),
  190. '#type' => 'radios',
  191. '#options' => array('flag' => t('Flag'), 'unflag' => t('Unflag')),
  192. '#description' => t('When this action is fired, which operation should be performed on the flag?'),
  193. '#default_value' => isset($context['flag_action']['op']) ? $context['flag_action']['op'] : 'flag',
  194. );
  195. if (empty($options)) {
  196. $error = t('There are no available %type flags. Before you can create an action of this type, you need to <a href="!url">create a %type flag</a>.', array('%type' => $entity_type, '!url' => url(FLAG_ADMIN_PATH . '/add')));
  197. $form['flag_action']['flag']['#type'] = 'item';
  198. $form['flag_action']['flag']['#markup'] = $error;
  199. $form['flag_action']['flag']['#element_validate'][] = 'flag_action_validate_flag';
  200. $form['flag_action']['flag']['#flag_error'] = $error;
  201. }
  202. return $form;
  203. }
  204. /**
  205. * Generic validation handler for validating Flag action configuration.
  206. */
  207. function flag_action_validate_flag($element) {
  208. if (isset($element['#flag_error'])) {
  209. form_error($element, $element['#flag_error']);
  210. }
  211. }
  212. /**
  213. * Generic submission handler for saving Flag action configuration.
  214. */
  215. function flag_action_submit($form, $form_state) {
  216. return array(
  217. 'flag_action' => $form_state['values']['flag_action'],
  218. );
  219. }