|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @file |
| 4 | + * Action callbacks for flag module. |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Implements Drupal action. Flags any entity. |
| 9 | + */ |
| 10 | +function flag_entity_action(&$entity, $context = array()) { |
| 11 | + $entity_flags = flag_get_flags($context['action_info']['type']); |
| 12 | + if (FALSE && count($entity_flags) === 1) { |
| 13 | + $flag = reset($entity_flags); |
| 14 | + $account = isset($context['account']) ? $context['account'] : $GLOBALS['user']; |
| 15 | + $op = ($flag->is_flagged($entity->id()))? 'unflag' : 'flag'; |
| 16 | + $flag->flag($op, $entity->id(), $account, TRUE); |
| 17 | + } |
| 18 | + else { |
| 19 | + $entity_info = entity_get_info($context['action_info']['type']); |
| 20 | + $message = 'This action can only flag or unflag items from this interface if there is only a single flag for @label'; |
| 21 | + $substitutions = array('@label' => $entity_info['label']); |
| 22 | + backdrop_set_message(t($message, $substitutions), 'warning'); |
| 23 | + return FALSE; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Form for configuring the Flag node action. |
| 29 | + */ |
| 30 | +function flag_node_action_form($context = array()) { |
| 31 | + return flag_action_form($context, 'node'); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Submit function for the Flag node action form. |
| 36 | + */ |
| 37 | +function flag_node_action_submit($form, $form_state) { |
| 38 | + return flag_action_submit($form, $form_state); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Form for configuring the Flag comment action. |
| 43 | + */ |
| 44 | +function flag_comment_action_form($context) { |
| 45 | + return flag_action_form($context, 'comment'); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Submit function for the Flag comment action form. |
| 50 | + */ |
| 51 | +function flag_comment_action_submit($form, $form_state) { |
| 52 | + return flag_action_submit($form, $form_state); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Form for configuring the Flag user action. |
| 57 | + */ |
| 58 | +function flag_user_action_form($context) { |
| 59 | + return flag_action_form($context, 'user'); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Submit function for the Flag user action form. |
| 64 | + */ |
| 65 | +function flag_user_action_submit($form, $form_state) { |
| 66 | + return flag_action_submit($form, $form_state); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Generic form for configuring Flag actions. |
| 71 | + * |
| 72 | + * @param $context |
| 73 | + * The current action context. |
| 74 | + * @param $entity_type |
| 75 | + * The entity type applicable to this action, such as "node" or "comment". |
| 76 | + */ |
| 77 | +function flag_action_form($context, $entity_type) { |
| 78 | + $form = array(); |
| 79 | + |
| 80 | + $flags = flag_get_flags($entity_type); |
| 81 | + // If this is a flag_action action, do not allow the triggering flag. |
| 82 | + if (isset($context['actions_flag'])) { |
| 83 | + unset($flags[$context['actions_flag']]); |
| 84 | + } |
| 85 | + $options = backdrop_map_assoc(array_keys($flags)); |
| 86 | + |
| 87 | + $form['flag_action']['#tree'] = TRUE; |
| 88 | + $form['flag_action']['warning'] = array( |
| 89 | + '#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>', |
| 90 | + ); |
| 91 | + $form['flag_action']['flag'] = array( |
| 92 | + '#title' => t('Flag to affect'), |
| 93 | + '#type' => 'radios', |
| 94 | + '#options' => $options, |
| 95 | + '#required' => TRUE, |
| 96 | + '#description' => t('When this action is fired, which flag should be flagged (or unflagged)?'), |
| 97 | + '#default_value' => isset($context['flag_action']['flag']) ? $context['flag_action']['flag'] : reset($options), |
| 98 | + ); |
| 99 | + |
| 100 | + $form['flag_action']['op'] = array( |
| 101 | + '#title' => t('Flag operation'), |
| 102 | + '#type' => 'radios', |
| 103 | + '#options' => array('flag' => t('Flag'), 'unflag' => t('Unflag')), |
| 104 | + '#description' => t('When this action is fired, which operation should be performed on the flag?'), |
| 105 | + '#default_value' => isset($context['flag_action']['op']) ? $context['flag_action']['op'] : 'flag', |
| 106 | + ); |
| 107 | + |
| 108 | + if (empty($options)) { |
| 109 | + $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'))); |
| 110 | + $form['flag_action']['flag']['#type'] = 'item'; |
| 111 | + $form['flag_action']['flag']['#markup'] = $error; |
| 112 | + $form['flag_action']['flag']['#element_validate'][] = 'flag_action_validate_flag'; |
| 113 | + $form['flag_action']['flag']['#flag_error'] = $error; |
| 114 | + } |
| 115 | + |
| 116 | + return $form; |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Generic validation handler for validating Flag action configuration. |
| 121 | + */ |
| 122 | +function flag_action_validate_flag($element) { |
| 123 | + if (isset($element['#flag_error'])) { |
| 124 | + form_error($element, $element['#flag_error']); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Generic submission handler for saving Flag action configuration. |
| 130 | + */ |
| 131 | +function flag_action_submit($form, $form_state) { |
| 132 | + return array( |
| 133 | + 'flag_action' => $form_state['values']['flag_action'], |
| 134 | + ); |
| 135 | +} |
0 commit comments