Projet

Général

Profil

Paste
Télécharger (7,53 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / flag / includes / flag.actions.inc @ 76e2e7c3

1
<?php
2

    
3
/**
4
 * @file
5
 * Hooks for flag actions.
6
 */
7

    
8
/**
9
 * Implements hook_trigger_info().
10
 */
11
function flag_trigger_info() {
12
  $hooks = array(
13
    'flag' => array(
14
      'flag_flag' => array(
15
        'label' => t('Object has been flagged with any flag'),
16
      ),
17
      'flag_unflag' => array(
18
        'label' => t('Object has been unflagged with any flag'),
19
      ),
20
    ),
21
  );
22

    
23
  foreach (flag_get_flags() as $flag) {
24
    $hooks['flag']['flag_flag_' . $flag->name]['label'] = t('A %type has been flagged with %name', array('%type' => $flag->entity_type, '%name' => $flag->name));
25
    $hooks['flag']['flag_unflag_' . $flag->name]['label'] = t('A %type has been unflagged with %name', array('%type' => $flag->entity_type, '%name' => $flag->name));
26
  }
27

    
28
  return $hooks;
29
}
30

    
31
/**
32
 * Implements hook_action_info().
33
 */
34
function flag_action_info() {
35
  return array(
36
    'flag_node_action' => array(
37
      'type' => 'node',
38
      'label' => t('Flag (or unflag) a node'),
39
      'configurable' => TRUE,
40
      'triggers' => array(
41
        'node_presave', 'node_insert', 'node_update', 'node_delete', 'node_view',
42
        'comment_insert', 'comment_update', 'comment_delete', 'comment_view',
43
      ),
44
    ),
45
    'flag_comment_action' => array(
46
      'type' => 'comment',
47
      'label' => t('Flag (or unflag) a comment'),
48
      'configurable' => TRUE,
49
      'triggers' => array(
50
        'comment_insert', 'comment_update', 'comment_delete', 'comment_view',
51
      ),
52
    ),
53
    'flag_user_action' => array(
54
      'type' => 'user',
55
      'label' => t('Flag (or unflag) a user'),
56
      'configurable' => TRUE,
57
      'triggers' => array(
58
        'user_insert', 'user_update', 'user_delete', 'user_login', 'user_logout', 'user_view',
59
      ),
60
    ),
61
  );
62
}
63

    
64
/**
65
 * Implements hook_action_info_alter().
66
 *
67
 * Enable Flag actions on Node, Comment, and User hooks without
68
 * the trigger_unlock.module.
69
 */
70
function flag_action_info_alter(&$actions) {
71
  $node_flags = flag_get_flags('node');
72
  $comment_flags = flag_get_flags('comment');
73
  $user_flags = flag_get_flags('user');
74

    
75
  foreach ($actions as $name => $action) {
76
    if (strpos($name, 'node') === 0) {
77
      $actions[$name]['triggers'][] = 'flag_flag';
78
      $actions[$name]['triggers'][] = 'flag_unflag';
79
      foreach ($node_flags as $flag) {
80
        $actions[$name]['triggers'][] = 'flag_flag_' . $flag->name;
81
        $actions[$name]['triggers'][] = 'flag_unflag_' . $flag->name;
82
      }
83
    }
84
    if (strpos($name, 'comment') === 0) {
85
      $actions[$name]['triggers'][] = 'flag_flag';
86
      $actions[$name]['triggers'][] = 'flag_unflag';
87
      foreach ($comment_flags as $flag) {
88
        $actions[$name]['triggers'][] = 'flag_flag_' . $flag->name;
89
        $actions[$name]['triggers'][] = 'flag_unflag_' . $flag->name;
90
      }
91
    }
92
    if (strpos($name, 'user') === 0) {
93
      $actions[$name]['triggers'][] = 'flag_flag';
94
      $actions[$name]['triggers'][] = 'flag_unflag';
95
      foreach ($user_flags as $flag) {
96
        $actions[$name]['triggers'][] = 'flag_flag_' . $flag->name;
97
        $actions[$name]['triggers'][] = 'flag_unflag_' . $flag->name;
98
      }
99
    }
100
  }
101
}
102

    
103
/**
104
 * Implements Drupal action. Flags a node.
105
 *
106
 * Note the first parameter is "object" because it may be a comment or a node.
107
 */
108
function flag_node_action(&$object, $context = array()) {
109
  if ($flag = flag_get_flag($context['flag_action']['flag'])) {
110
    $account = isset($context['account']) ? $context['account'] : $GLOBALS['user'];
111
    $flag->flag($context['flag_action']['op'], $object->nid, $account, TRUE);
112
  }
113
}
114

    
115
/**
116
 * Form for configuring the Flag node action.
117
 */
118
function flag_node_action_form($context = array()) {
119
  return flag_action_form($context, 'node');
120
}
121

    
122
/**
123
 * Submit function for the Flag node action form.
124
 */
125
function flag_node_action_submit($form, $form_state) {
126
  return flag_action_submit($form, $form_state);
127
}
128

    
129
/**
130
 * Implements Drupal action. Flags a comment.
131
 */
132
function flag_comment_action(&$comment, $context = array()) {
133
  if ($flag = flag_get_flag($context['flag_action']['flag'])) {
134
    $account = isset($context['account']) ? $context['account'] : $GLOBALS['user'];
135
    $flag->flag($context['flag_action']['op'], $comment->cid, $account, TRUE);
136
  }
137
}
138

    
139
/**
140
 * Form for configuring the Flag comment action.
141
 */
142
function flag_comment_action_form($context) {
143
  return flag_action_form($context, 'comment');
144
}
145

    
146
/**
147
 * Submit function for the Flag comment action form.
148
 */
149
function flag_comment_action_submit($form, $form_state) {
150
  return flag_action_submit($form, $form_state);
151
}
152

    
153
/**
154
 * Implements Drupal action. Flags a user.
155
 */
156
function flag_user_action(&$user, $context = array()) {
157
  if ($flag = flag_get_flag($context['flag_action']['flag'])) {
158
    $account = isset($context['account']) ? $context['account'] : $GLOBALS['user'];
159
    $flag->flag($context['flag_action']['op'], $user->uid, $account, TRUE);
160
  }
161
}
162

    
163
/**
164
 * Form for configuring the Flag user action.
165
 */
166
function flag_user_action_form($context) {
167
  return flag_action_form($context, 'user');
168
}
169

    
170
/**
171
 * Submit function for the Flag user action form.
172
 */
173
function flag_user_action_submit($form, $form_state) {
174
  return flag_action_submit($form, $form_state);
175
}
176

    
177
/**
178
 * Generic form for configuring Flag actions.
179
 *
180
 * @param $context
181
 *   The current action context.
182
 * @param $entity_type
183
 *   The entity type applicable to this action, such as "node" or "comment".
184
 */
185
function flag_action_form($context, $entity_type) {
186
  $form = array();
187

    
188
  $flags = flag_get_flags($entity_type);
189
  // If this is a flag_action action, do not allow the triggering flag.
190
  if (isset($context['actions_flag'])) {
191
    unset($flags[$context['actions_flag']]);
192
  }
193
  $options = drupal_map_assoc(array_keys($flags));
194

    
195
  $form['flag_action']['#tree'] = TRUE;
196
  $form['flag_action']['warning'] = array(
197
    '#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>',
198
  );
199
  $form['flag_action']['flag'] = array(
200
    '#title' => t('Flag to affect'),
201
    '#type' => 'radios',
202
    '#options' => $options,
203
    '#required' => TRUE,
204
    '#description' => t('When this action is fired, which flag should be flagged (or unflagged)?'),
205
    '#default_value' => isset($context['flag_action']['flag']) ? $context['flag_action']['flag'] : reset($options),
206
  );
207

    
208
  $form['flag_action']['op'] = array(
209
    '#title' => t('Flag operation'),
210
    '#type' => 'radios',
211
    '#options' => array('flag' => t('Flag'), 'unflag' => t('Unflag')),
212
    '#description' => t('When this action is fired, which operation should be performed on the flag?'),
213
    '#default_value' => isset($context['flag_action']['op']) ? $context['flag_action']['op'] : 'flag',
214
  );
215

    
216
  if (empty($options)) {
217
    $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')));
218
    $form['flag_action']['flag']['#type'] = 'item';
219
    $form['flag_action']['flag']['#markup'] = $error;
220
    $form['flag_action']['flag']['#element_validate'][] = 'flag_action_validate_flag';
221
    $form['flag_action']['flag']['#flag_error'] = $error;
222
  }
223

    
224
  return $form;
225
}
226

    
227
/**
228
 * Generic validation handler for validating Flag action configuration.
229
 */
230
function flag_action_validate_flag($element) {
231
  if (isset($element['#flag_error'])) {
232
    form_error($element, $element['#flag_error']);
233
  }
234
}
235

    
236
/**
237
 * Generic submission handler for saving Flag action configuration.
238
 */
239
function flag_action_submit($form, $form_state) {
240
  return array(
241
    'flag_action' => $form_state['values']['flag_action'],
242
  );
243
}