Index: flag.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/flag/Attic/flag.inc,v retrieving revision 1.1.2.28 diff -u -r1.1.2.28 flag.inc --- flag.inc 15 Feb 2009 21:14:21 -0000 1.1.2.28 +++ flag.inc 24 Feb 2009 19:32:23 -0000 @@ -623,6 +623,30 @@ */ /** + * @defgroup rules Rules integration + * @{ + * Methods that can be overridden to support the Rules module. + */ + + /** + * Defines the Rules arguments involved in a flag event. + */ + function rules_get_event_arguments_definition() { + return array(); + } + + /** + * Defines the Rules argument for flag actions or conditions + */ + function rules_get_element_argument_definition() { + return array(); + } + + /** + * @} End of "defgroup rules". + */ + + /** * @defgroup views Views 2 integration * @{ * Methods that can be overridden to support the Views module. @@ -908,6 +932,20 @@ ); } + function rules_get_event_arguments_definition() { + return array( + 'node' => array( + 'type' => 'node', + 'label' => t('flagged content'), + 'handler' => 'flag_rules_get_event_argument', + ), + ); + } + + function rules_get_element_argument_definition() { + return array('type' => 'node', 'label' => t('Content to flag')); + } + function get_views_info() { return array( 'views table' => 'node', @@ -1004,6 +1042,25 @@ ); } + function rules_get_event_arguments_definition() { + return array( + 'comment' => array( + 'type' => 'comment', + 'label' => t('flagged comment'), + 'handler' => 'flag_rules_get_event_argument', + ), + 'node' => array( + 'type' => 'node', + 'label' => t("the flagged comment's content"), + 'handler' => 'flag_rules_get_comment_content', + ), + ); + } + + function rules_get_element_argument_definition() { + return array('type' => 'comment', 'label' => t('Comment to flag')); + } + function get_views_info() { return array( 'views table' => 'comments', @@ -1101,6 +1158,20 @@ ); } + function rules_get_event_arguments_definition() { + return array( + 'account' => array( + 'type' => 'user', + 'label' => t('flagged user'), + 'handler' => 'flag_rules_get_event_argument', + ), + ); + } + + function rules_get_element_argument_definition() { + return array('type' => 'user', 'label' => t('User to flag')); + } + function get_views_info() { return array( 'views table' => 'users', Index: flag.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/flag/Attic/flag.module,v retrieving revision 1.11.2.65 diff -u -r1.11.2.65 flag.module --- flag.module 22 Feb 2009 03:15:52 -0000 1.11.2.65 +++ flag.module 24 Feb 2009 19:32:23 -0000 @@ -955,6 +955,12 @@ } } + + if (module_exists('rules')) { + $event_name = ($action == 'flag' ? 'flag_flagged_' : 'flag_unflagged_') . $flag->name; + $arguments = array('flag' => $flag, 'flag_content_id' => $content_id, 'flagging_user' => $account); + rules_invoke_event($event_name, $arguments); + } } /** @@ -1028,6 +1034,9 @@ 'flag_admin_page' => array( 'arguments' => array('flags' => NULL, 'default_flags' => NULL), ), + 'flag_rules_radios' => array( + 'arguments' => array(), + ), ); } Index: includes/flag.rules_forms.inc =================================================================== RCS file: includes/flag.rules_forms.inc diff -N includes/flag.rules_forms.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/flag.rules_forms.inc 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,109 @@ + $flag->get_title())); +} + +function flag_rules_action_unflag_form($settings, &$form, &$form_state) { +} + +function flag_rules_action_unflag_label($settings, $argument_labels, $element) { + $flag = rules_get_element_variable($element, 'flag'); + return t('Unflag @object, under "@flag-title"', $argument_labels + array('@flag-title' => $flag->get_title())); +} + +function flag_rules_action_trim_form($settings, &$form, &$form_state) { + // Move argument to center of form. + $form['settings']['cutoff_size']['#weight'] = 2; +} + +function flag_rules_action_trim_label($settings, $argument_labels, $element) { + $flag = rules_get_element_variable($element, 'flag'); + return t('Trim "@flag-title" at @size', array('@flag-title' => $flag->get_title(), '@size' => $settings['cutoff_size'])); +} + +function flag_rules_action_trim_help() { + return t('The trim action is used to restrict the number of objects that may be flagged. For example, you may wish your "Editor picks" queue (that is, flag) to contain a maximum of 3 nodes. The trim action is best understood when we think of a flag as a queue. The action works by discarding old flaggings; So newly flagged objects push older ones out of the queue.'); +} + + +// ------------------------------------------------------------------------- +// Conditions forms + +function flag_rules_condition_threshold_form($settings, &$form, &$form_state) { + + $form['settings']['operator'] = array( + '#title' => t('Comparison operator'), + '#type' => 'select', + '#options' => array( + '>' => t('Greater than'), + '>=' => t('Greater than or equal'), + '=' => t('Equal to'), + '<=' => t('Less than or equal'), + '<' => t('Less than'), + ), + '#default_value' => isset($settings['operator']) ? $settings['operator'] : '>=', + ); + + // Move argument to bottom of form. + $form['settings']['number']['#weight'] = 2; + + // Move tokens help to bottom of form. + $form['settings']['input_help'] = $form['input_help']; + $form['settings']['input_help']['#weight'] = 3; + unset($form['input_help']); +} + +function flag_rules_condition_threshold_label($settings, $argument_labels, $element) { + $flag = rules_get_element_variable($element, 'flag'); + return t('Flagging count !op @number, for @object, under "@flag-title"', array('@flag-title' => $flag->get_title(), '!op' => $settings['operator'], '@number' => $settings['number']) + $argument_labels); +} + + +// ------------------------------------------------------------------------- +// Form utilities for the default input form of flags. +// + +function theme_flag_rules_radios($element) { + + if (!empty($element['#disabled'])) { + drupal_set_message($element['#description'], 'error'); + } + + $headers = array(t('Flag:'), t('The flag type'), t('Is the flag global?')); + $rows = array(); + foreach (element_children($element) as $flag_name) { + $flag = flag_get_flag($flag_name); + $rows[] = array( + '
' . drupal_render($element[$flag_name]) . '
', + $flag->content_type, + $flag->global ? t('Yes') : t('No'), + ); + } + return theme('table', $headers, $rows); +} + +function _flag_rules_flags_options($flag_type = NULL) { + $flags = flag_get_flags(); + $options = array(); + foreach ($flags as $flag) { + if (!isset($flag_type) || $flag->content_type == $flag_type) { + $options[$flag->name] = $flag->get_title(); + } + } + return $options; +} + Index: includes/flag.rules.inc =================================================================== RCS file: includes/flag.rules.inc diff -N includes/flag.rules.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/flag.rules.inc 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,276 @@ + array( + 'label' => t('Flag'), + 'class' => 'flag_rules_data_type', + 'savable' => TRUE, + 'identifiable' => TRUE, + 'use_input_form' => TRUE, + 'module' => 'Node', + ), + ); +} + +/** + * Defines the flag rule data type + * @ingroup rules + */ +class flag_rules_data_type extends rules_data_type { + + function save() { + $flag = &$this->get(); + $flag->save; + return TRUE; + } + + function load($name) { + return flag_get_flag($name); + } + + function get_identifier() { + $flag = &$this->get(); + return $flag->name; + } + + /** + * Returns radios for selecting a flag of the type given in $info['flag_type'] + */ + function get_default_input_form($info, $value) { + $info += array('description' => ''); + + $options = _flag_rules_flags_options(isset($info['flag_type']) ? $info['flag_type'] : NULL); + $form = array( + '#type' => 'radios', + '#title' => $info['label'], + '#options' => $options, + '#required' => TRUE, + '#disabled' => !$options, + '#default_value' => isset($value) ? $value : NULL, + '#theme' => 'flag_rules_radios', + '#printed' => TRUE, //don't use the elements type theme function + ); + if (!$options) { + $form['#description'] = t('Error: There is no suiting flag available.'); + } + return $form; + } + + function check_value($info, $value) { + if ($flag = flag_get_flag($value)) { + return $flag; + } + rules_log(t('The flag %name does not exist any more.', array('%name' => $value))); + } +} + +/** + * Implementation of hook_rules_event_info(). + */ +function flag_rules_event_info() { + $items = array(); + + $flags = flag_get_flags(); + foreach ($flags as $flag) { + + $arguments = array( + // First, define ubiquitous arguments. + 'flag' => array( + 'type' => 'flag', + 'label' => t('flag'), + ), + 'flag_content_id' => array( + 'type' => 'number', + 'hidden' => TRUE, + ), + 'flagging_user' => array( + 'type' => 'user', + 'label' => t('flagging user'), + ), + ); + // Then, define flag-specific arguments. + $arguments += $flag->rules_get_event_arguments_definition(); + + // For each flag we define two events. + $items['flag_flagged_' . $flag->name] = array( + 'module' => 'Flag', + 'label' => t('A @flag-type has been flagged, under "@flag-title"', array('@flag-title' => $flag->get_title(), '@flag-type' => t($flag->content_type))), + 'arguments' => $arguments, + ); + $items['flag_unflagged_' . $flag->name] = array( + 'module' => 'Flag', + 'label' => t('A @flag-type has been unflagged, under "@flag-title"', array('@flag-title' => $flag->get_title(), '@flag-type' => t($flag->content_type))), + 'arguments' => $arguments, + ); + } + return $items; +} + +/** + * Argument handler: Fetches the flags content for use with rules. + */ +function flag_rules_get_event_argument($flag, $content_id, $account) { + return $flag->fetch_content($content_id); +} + +function flag_rules_get_comment_content($flag, $content_id, $account) { + $comment = $flag->fetch_content($content_id); + return node_load($comment->nid); +} + +/** + * Implementation of hook_rules_action_info(). + */ +function flag_rules_action_info() { + $items = array( + 'flag_rules_action_trim' => array( + 'label' => t('Trim a flag'), + 'arguments' => array( + 'flag' => array( + 'type' => 'flag', + 'label' => t('Flag'), + ), + 'flagging_user' => array( + 'type' => 'user', + 'label' => t('User whose flag to trim'), + 'description' => t('For non-global flags, this is the user whose flag to trim. (For global flags, this argument is ignored.)'), + ), + 'cutoff_size' => array( + 'type' => 'number', + 'label' => t('Cutoff size'), + '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.'), + ), + ), + 'module' => 'Flag', + ), + ); + foreach (flag_get_types() as $type) { + $args = array( + 'flag' => array( + 'type' => 'flag', + 'label' => t('Flag'), + 'flag_type' => $type, + ), + ); + $flag = flag_create_handler($type); + $args += array('object' => $flag->rules_get_element_argument_definition()); + + $items += array( + 'flag_rules_action_flag_'. $type => array( + 'label' => t('Flag a @type', array('@type' => $type)), + 'base' => 'flag_rules_action_flag', + 'label callback' => 'flag_rules_action_flag_label', + 'arguments' => $args + array( + 'flagging_user' => array( + 'type' => 'user', + 'label' => t('User on whose behalf to flag'), + 'description' => t('For non-global flags, this is the user on whose behalf to flag the object. In addition, the access permissions to the flag are checked against this user.'), + ), + ), + 'module' => 'Flag', + ), + 'flag_rules_action_unflag_'. $type => array( + 'label' => t('Unflag a @type', array('@type' => $type)), + 'base' => 'flag_rules_action_unflag', + 'label callback' => 'flag_rules_action_unflag_label', + 'arguments' => $args + array( + 'flagging_user' => array( + 'type' => 'user', + 'label' => t('User on whose behalf to unflag'), + 'description' => t('For non-global flags, this is the user on whose behalf to unflag the object. In addition, the access permissions to the flag are checked against this user.'), + ), + ), + 'module' => 'Flag', + ), + ); + } + return $items; +} + +/** + * Base action implementation: Flag + */ +function flag_rules_action_flag($flag, $object, $flagging_user, $settings) { + flag('flag', $flag->name, $flag->get_content_id($object), $flagging_user); +} + +/** + * Base action implementation: Unflag + */ +function flag_rules_action_unflag($flag, $object, $flagging_user, $settings) { + flag('unflag', $flag->name, $flag->get_content_id($object), $flagging_user); +} + +/** + * Base action implementation + */ +function flag_rules_action_trim($flag, $flagging_user, $cutoff_size, $settings) { + // We can't use db_query_range(), because we can't specify 'infinity'. + $result = db_query("SELECT * FROM {flag_content} WHERE fid = %d AND (uid = %d OR uid = 0) ORDER BY timestamp DESC", $flag->fid, $flagging_user->uid); + $i = 1; + while ($row = db_fetch_object($result)) { + if ($i++ > $cutoff_size) { + flag('unflag', $flag->name, $row->content_id, $flagging_user); + } + } +} + +/** + * Implementation of hook_rules_condition_info(). + */ +function flag_rules_condition_info() { + $items = array(); + foreach (flag_get_types() as $type) { + $args = array( + 'flag' => array( + 'type' => 'flag', + 'label' => t('Flag'), + 'flag_type' => $type, + ), + ); + $flag = flag_create_handler($type); + $args += array('object' => $flag->rules_get_element_argument_definition()); + $args += array( + 'number' => array( + 'type' => 'number', + 'label' => t('Number'), + '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.'), + ), + ); + + $items += array( + 'flag_rules_condition_threshold_'. $type => array( + 'label' => drupal_ucfirst(t('@type has flagging count', array('@type' => $type))), + 'base' => 'flag_rules_condition_threshold', + 'label callback' => 'flag_rules_condition_threshold_label', + 'arguments' => $args, + 'module' => 'Flag', + ), + ); + } + return $items; +} + +function flag_rules_condition_threshold($flag, $object, $number, $settings) { + + $count = $flag->get_count($flag->get_content_id($object)); + + switch ($settings['operator']) { + case '>' : return $count > $number; + case '>=': return $count >= $number; + case '=' : return $count == $number; + case '<' : return $count < $number; + case '<=': return $count <= $number; + } +}