root/drupal-6/sites/all/modules/flag/includes/flag.rules.inc @ 867

Revision 867, 9.1 KB (checked in by frans, 6 months ago)

update: flag to 6.x-2.x-dev version

Line 
1<?php
2// $Id: flag.rules.inc,v 1.1.2.2.2.2 2009/11/03 01:53:36 quicksketch Exp $
3
4/**
5 * @file flag.rules.inc
6 * Rules integration for the Flag module.
7 */
8
9/**
10 * Implementation of hook_rules_data_type_info().
11 * @ingroup rules
12 */
13function flag_rules_data_type_info() {
14  return array(
15    'flag' => array(
16      'label' => t('Flag'),
17      'class' => 'flag_rules_data_type',
18      'savable' => TRUE,
19      'identifiable' => TRUE,
20      'use_input_form' => TRUE,
21      'module' => 'Flag',
22    ),
23  );
24}
25
26/**
27 * Defines the flag rule data type.
28 * @ingroup rules
29 */
30class flag_rules_data_type extends rules_data_type {
31
32  function save() {
33    $flag = &$this->get();
34    $flag->save;
35    return TRUE;
36  }
37
38  function load($name) {
39    return flag_get_flag($name);
40  }
41
42  function get_identifier() {
43    $flag = &$this->get();
44    return $flag->name;
45  }
46
47  /**
48   * Returns radios for selecting a flag of the type given in
49   * $info['flag_type'].
50   */
51  function get_default_input_form($info, $value) {
52    $options = _flag_rules_flags_options(isset($info['flag_type']) ? $info['flag_type'] : NULL);
53    $form = array(
54      '#type' => 'radios',
55      '#title' => $info['label'],
56      '#options' => $options,
57      '#required' => TRUE,
58      '#disabled' => !$options,
59      '#default_value' =>  isset($value) ? $value : NULL,
60      '#theme' => 'flag_rules_radios',
61      '#printed' => TRUE, //don't use the elements type theme function
62    );
63    if (!$options) {
64      $form['#description'] = t('Error: There is no suiting flag available.');
65    }
66    return $form;
67  }
68
69  function check_value($info, $value) {
70    if ($flag = flag_get_flag($value)) {
71      return $flag;
72    }
73    rules_log(t('The flag %name does not exist any more.', array('%name' => $value)));
74  }
75}
76
77/**
78 * Implementation of hook_rules_event_info().
79 */
80function flag_rules_event_info() {
81  $items = array();
82
83  $flags = flag_get_flags();
84  foreach ($flags as $flag) {
85
86    $arguments = array(
87      // First, define ubiquitous arguments.
88      'flag' => array(
89        'type' => 'flag',
90        'label' => t('flag'),
91      ),
92      'flag_content_id' => array(
93        'type' => 'number',
94        'hidden' => TRUE,
95      ),
96      'flagging_user' => array(
97        'type' => 'user',
98        'label' => t('flagging user'),
99      ),
100    );
101    // Then, define flag-specific arguments.
102    $arguments += $flag->rules_get_event_arguments_definition();
103
104    // For each flag we define two events.
105    $items['flag_flagged_' . $flag->name] = array(
106      'module' => 'Flag',
107      'label' => t('A @flag-type has been flagged, under "@flag-title"', array('@flag-title' => $flag->get_title(), '@flag-type' => t($flag->content_type))),
108      'arguments' => $arguments,
109    );
110    $items['flag_unflagged_' . $flag->name] = array(
111      'module' => 'Flag',
112      'label' => t('A @flag-type has been unflagged, under "@flag-title"', array('@flag-title' => $flag->get_title(), '@flag-type' => t($flag->content_type))),
113      'arguments' => $arguments,
114    );
115  }
116  return $items;
117}
118
119/**
120 * Argument handler: Fetches the flags content for use with rules.
121 */
122function flag_rules_get_event_argument($flag, $content_id, $account) {
123  return $flag->fetch_content($content_id);
124}
125
126function flag_rules_get_comment_content($flag, $content_id, $account) {
127  $comment = $flag->fetch_content($content_id);
128  return node_load($comment->nid);
129}
130
131/**
132 * Implementation of hook_rules_action_info().
133 */
134function flag_rules_action_info() {
135  $items = array(
136    'flag_rules_action_trim' => array(
137      'label' => t('Trim a flag'),
138      'arguments' => array(
139        'flag' => array(
140          'type' => 'flag',
141          'label' => t('Flag'),
142        ),
143        'flagging_user' => array(
144          'type' => 'user',
145          'label' => t('User whose flag to trim'),
146          'description' => t('For non-global flags, this is the user whose flag to trim. (For global flags, this argument is ignored.)'),
147        ),
148        'cutoff_size' => array(
149          'type' => 'number',
150          'label' => t('Flag queue size'),
151          'description' => t('The maximum number of objects to keep in the queue. Newly flagged objects will be kept; older ones will be removed. Tip: by typing "1" here you implement a singleton.'),
152        ),
153      ),
154      'module' => 'Flag',
155    ),
156  );
157  foreach (flag_get_types() as $type) {
158    $args = array(
159      'flag' => array(
160        'type' => 'flag',
161        'label' => t('Flag'),
162        'flag_type' => $type,
163      ),
164    );
165    $flag = flag_create_handler($type);
166    if ($flag->rules_get_element_argument_definition()) {
167      $args += array('object' => $flag->rules_get_element_argument_definition());
168
169      $items += array(
170        'flag_rules_action_flag_'. $type => array(
171          'label' => t('Flag a @type', array('@type' => $type)),
172          'base' => 'flag_rules_action_flag',
173          'label callback' => 'flag_rules_action_flag_label',
174          'arguments' => $args + array(
175            'flagging_user' => array(
176              'type' => 'user',
177              'label' => t('User on whose behalf to flag'),
178              'description' => t('For non-global flags, this is the user on whose behalf to flag the object. In addition, if checked below, the access permissions to the flag are checked against this user.'),
179            ),
180          ),
181          'module' => 'Flag',
182        ),
183        'flag_rules_action_unflag_'. $type => array(
184          'label' => t('Unflag a @type', array('@type' => $type)),
185          'base' => 'flag_rules_action_unflag',
186          'label callback' => 'flag_rules_action_unflag_label',
187          'arguments' => $args + array(
188            'flagging_user' => array(
189              'type' => 'user',
190              'label' => t('User on whose behalf to unflag'),
191              'description' => t('For non-global flags, this is the user on whose behalf to unflag the object. In addition, if checked below, the access permissions to the flag are checked against this user.'),
192            ),
193          ),
194          'module' => 'Flag',
195        ),
196      );
197    }
198  }
199  return $items;
200}
201
202/**
203 * Base action implementation: Flag.
204 */
205function flag_rules_action_flag($flag, $object, $flagging_user, $settings) {
206  $flag->flag('flag', $flag->get_content_id($object), $flagging_user, !$settings['permission_check']);
207}
208
209/**
210 * Base action implementation: Unflag.
211 */
212function flag_rules_action_unflag($flag, $object, $flagging_user, $settings) {
213  $flag->flag('unflag', $flag->get_content_id($object), $flagging_user, !$settings['permission_check']);
214}
215
216/**
217 * Base action implementation: Trim flag.
218 */
219function flag_rules_action_trim($flag, $flagging_user, $cutoff_size, $settings) {
220  flag_trim_flag($flag, $flagging_user, $cutoff_size);
221}
222
223/**
224 * Implementation of hook_rules_condition_info().
225 */
226function flag_rules_condition_info() {
227  $items = array();
228  foreach (flag_get_types() as $type) {
229    $args = array(
230      'flag' => array(
231        'type' => 'flag',
232        'label' => t('Flag'),
233        'flag_type' => $type,
234      ),
235    );
236    $flag = flag_create_handler($type);
237    if ($flag->rules_get_element_argument_definition()) {
238      $args += array('object' => $flag->rules_get_element_argument_definition());
239
240      $items += array(
241        'flag_rules_condition_threshold_'. $type => array(
242          'label' => drupal_ucfirst(t('@type has flagging count', array('@type' => $type))),
243          'base' => 'flag_rules_condition_threshold',
244          'label callback' => 'flag_rules_condition_threshold_label',
245          'arguments' => $args + array(
246            'number' => array(
247              'type' => 'number',
248              'label' => t('Number'),
249              'description' => t('The number against which to test the number of times the object is flagged. For example, if you type "3" here, and choose "Greater than" for the operator, then this condition will return TRUE if the object is flagged more than three times.'),
250            ),
251          ),
252          'module' => 'Flag',
253        ),
254        'flag_rules_condition_flagged_'. $type => array(
255          'label' => drupal_ucfirst(t('@type is flagged', array('@type' => $type))),
256          'base' => 'flag_rules_condition_flagged',
257          'label callback' => 'flag_rules_condition_flagged_label',
258          'arguments' => $args + array(
259            'user' => array(
260              'type' => 'user',
261              'label' => t('User on whose behalf to check'),
262              'description' => t('For non-global flags, this is the user on whose behalf the flag is checked.'),
263            ),
264          ),
265          'module' => 'Flag',
266        ),
267      );
268    }
269  }
270  return $items;
271}
272
273/**
274 * Condition: Check flagging count.
275 */
276function flag_rules_condition_threshold($flag, $object, $number, $settings) {
277
278  $count = $flag->get_count($flag->get_content_id($object));
279
280  switch ($settings['operator']) {
281    case '>' : return $count >  $number;
282    case '>=': return $count >= $number;
283    case '=' : return $count == $number;
284    case '<' : return $count <  $number;
285    case '<=': return $count <= $number;
286  }
287}
288
289/**
290 * Condition: Flag is flagged.
291 */
292function flag_rules_condition_flagged($flag, $object, $account, $settings) {
293  return $flag->is_flagged($flag->get_content_id($object), $account->uid);
294}
Note: See TracBrowser for help on using the browser.