rb_misc.module in Rules Bonus Pack 6
Same filename and directory in other branches
Miscellaneous conditions and actions for Rules.
File
rb_misc.moduleView source
<?php
/**
* @file
* Miscellaneous conditions and actions for Rules.
*/
/**
* Implementation of hook_rules_condition_info().
* @ingroup rules
*/
function rb_misc_rules_condition_info() {
$conditions = array(
'rb_misc_condition_arg' => array(
'label' => t('Verify path argument'),
'module' => 'Rules Bonus: Misc',
'eval input' => array(
'arg_value',
),
),
);
// Add conditions that depend on non-required modules.
if (module_exists('views')) {
$conditions['rb_misc_action_views_result_count'] = array(
'label' => t('Check number of results from a view'),
'eval input' => array(
'args',
),
'module' => 'Rules Bonus: Misc',
);
}
return $conditions;
}
/**
* Implementation of hook_rules_action_info().
* @ingroup rules
*/
function rb_misc_rules_action_info() {
// Add the actions relevant for required core modules.
$actions = array(
'rb_misc_action_arg' => array(
'label' => t('Load path argument'),
'new variables' => array(
'arg_part' => array(
'type' => 'string',
'label' => t('Path argument'),
'save' => TRUE,
),
),
'module' => 'Rules Bonus: Misc',
),
'rb_misc_action_get_time' => array(
'label' => t('Get a string with current time'),
'new variables' => array(
'current_time' => array(
'type' => 'string',
'label' => t('Current time'),
'save' => TRUE,
),
),
'module' => 'Rules Bonus: Misc',
),
'rb_misc_action_variable_get' => array(
'label' => t('Load a system variable'),
'new variables' => array(
'variable' => array(
'type' => 'string',
'label' => t('System variable'),
'save' => TRUE,
),
),
'module' => 'Rules Bonus: Misc',
),
'rb_misc_action_set_node_creation_time' => array(
'label' => t('Set node creation time'),
'arguments' => array(
'node' => array(
'type' => 'node',
'label' => t('Node to give new post time'),
),
),
'eval input' => array(
'time',
),
'module' => 'Rules Bonus: Misc',
),
);
// Add actions that depend on non-required modules.
if (module_exists('path')) {
$actions['rb_misc_action_get_url_alias'] = array(
'label' => t('Get URL alias for an internal path'),
'new variables' => array(
'alias' => array(
'type' => 'string',
'label' => t('URL alias'),
'save' => TRUE,
),
),
'eval input' => array(
'path',
),
'module' => 'Rules Bonus: Misc',
);
}
if (module_exists('views')) {
$actions['rb_misc_action_views_load_node'] = array(
'label' => t('Use Views to load a node'),
'new variables' => array(
'node' => array(
'type' => 'node',
'label' => t('Views-provided node'),
'save' => TRUE,
),
),
'eval input' => array(
'args',
),
'module' => 'Rules Bonus: Misc',
);
}
if (module_exists('menu') && module_exists('menu_position')) {
$actions['rb_misc_action_set_active_menu_item'] = array(
'label' => t('Set the active menu item'),
'module' => 'Rules Bonus: Misc',
);
}
if (module_exists('feeds')) {
$actions['rb_misc_action_get_import_nid'] = array(
'label' => t('Get import nid for content'),
'arguments' => array(
'node' => array(
'type' => 'node',
'label' => t('Imported content'),
),
),
'new variables' => array(
'import_nid' => array(
'type' => 'string',
'label' => t('nid for import node'),
'save' => TRUE,
),
),
'module' => 'Course web',
);
}
return $actions;
}
/**
* Configuration form for 'rb_misc_condition_arg'.
*/
function rb_misc_condition_arg_form($settings, &$form) {
$form['settings']['arg_part'] = array(
'#type' => 'select',
'#options' => range(0, 9),
'#default_value' => $settings['arg_part'],
'#title' => t('Part of path argument'),
'#description' => t('The part of the path argument to verify. Note that
numbering starts with zero.'),
);
$form['settings']['arg_value'] = array(
'#type' => 'textfield',
'#default_value' => $settings['arg_value'],
'#title' => t('Path argument value'),
'#description' => t('The value the path argument should have.'),
);
}
/**
* The 'rb_misc_condition_arg' condition.
*/
function rb_misc_condition_arg($settings) {
return arg($settings['arg_part']) == $settings['arg_value'];
}
/**
* Configuration form for 'rb_misc_action_views_result_count'.
*/
function rb_misc_action_views_result_count_form($settings, &$form) {
$selectable_displays = array();
foreach (views_get_all_views() as $view_name => $view) {
if ($view->base_table == 'node') {
foreach ($view->display as $display_name => $display) {
$selectable_displays[$view_name . '|' . $display_name] = $view_name . '|' . check_plain($display->display_title);
}
}
}
$form['settings']['view'] = array(
'#type' => 'select',
'#options' => $selectable_displays,
'#default_value' => $settings['view'],
'#title' => t('Select view and display'),
'#description' => t('Select the view and the view display you want to use to
load a node. The first node listed in the view will be loaded.'),
);
$form['settings']['args'] = array(
'#type' => 'textarea',
'#default_value' => $settings['args'],
'#title' => t('View arguments'),
'#description' => t('Add any arguments you want to send to the view, one per
line. You may use tokens.'),
);
$form['settings']['minimum'] = array(
'#type' => 'textfield',
'#title' => t('Minimum number of results'),
'#default_value' => $settings['minimum'],
'#description' => t('This condition returns TRUE if the view has at least
the given number of results.'),
);
}
/**
* The 'rb_misc_action_views_result_count' condition.
*/
function rb_misc_action_views_result_count($settings) {
$views_settings = explode('|', $settings['view']);
$view_name = $views_settings[0];
$display_name = $views_settings[1];
$view_arguments = explode("\r", $settings['args']);
$view = views_get_view($view_name);
$view
->set_display($display_name);
$view
->set_arguments($view_arguments);
$view
->set_items_per_page(0);
$view
->execute();
$results = $view->result;
return count($results) >= $settings['minimum'];
}
/**
* Configuration form for 'rb_misc_action_arg'.
*/
function rb_misc_action_arg_form($settings, &$form) {
$form['settings']['arg_part'] = array(
'#type' => 'select',
'#options' => range(0, 9),
'#default_value' => $settings['arg_part'],
'#title' => t('Part of path argument'),
'#description' => t('The part of the path argument to load. Note that
numbering starts with zero.'),
);
}
/**
* The 'rb_misc_action_arg' action.
*/
function rb_misc_action_arg($settings) {
return array(
'arg_part' => arg($settings['arg_part']),
);
}
/**
* Configuration form for 'rb_misc_action_get_time'.
*/
function rb_misc_action_get_time_form($settings, &$form) {
$form['settings']['format'] = array(
'#type' => 'textfield',
'#default_value' => $settings['format'],
'#title' => t('Time format'),
'#description' => t('A format for the time, as defined by the !PHP-date
function. Could be \'Y-m-d\'.', array(
'!PHP-date' => l('PHP date', 'http://php.net/manual/en/function.date.php'),
)),
);
$form['settings']['utc'] = array(
'#type' => 'checkbox',
'#default_value' => $settings['utc'],
'#title' => t('Get UTC time'),
'#description' => t('Setting this will give you Coordinated Universal Time,
ignoring any timezone settings.'),
);
}
/**
* The 'rb_misc_action_get_time' action.
*/
function rb_misc_action_get_time($settings) {
// Get UTC time or time with user's time zone.
if ($settings['utc']) {
$time = format_date(time(), 'custom', $settings['format'], '');
}
else {
$time = format_date(time(), 'custom', $settings['format']);
}
return array(
'current_time' => $time,
);
}
/**
* Configuration form for 'rb_misc_action_views_load_node'.
*/
function rb_misc_action_views_load_node_form($settings, &$form) {
$selectable_displays = array();
foreach (views_get_all_views() as $view_name => $view) {
if ($view->base_table == 'node') {
foreach ($view->display as $display_name => $display) {
$selectable_displays[$view_name . '|' . $display_name] = $view_name . '|' . check_plain($display->display_title);
}
}
}
$form['settings']['view'] = array(
'#type' => 'select',
'#options' => $selectable_displays,
'#default_value' => $settings['view'],
'#title' => t('Select view and display'),
'#description' => t('Select the view and the view display you want to use to
load a node. The first node listed in the view will be loaded.'),
);
$form['settings']['args'] = array(
'#type' => 'textarea',
'#default_value' => $settings['args'],
'#title' => t('View arguments'),
'#description' => t('Add any arguments you want to send to the view, one per
line. You may use tokens.'),
);
}
/**
* The 'rb_misc_action_views_load_node' action.
*/
function rb_misc_action_views_load_node($settings) {
$views_settings = explode('|', $settings['view']);
$view_name = $views_settings[0];
$display_name = $views_settings[1];
$view_arguments = explode("\r", $settings['args']);
$view = views_get_view($view_name);
$view
->set_display($display_name);
$view
->set_arguments($view_arguments);
$view
->set_items_per_page(1);
$view
->execute();
$results = $view->result;
$node = node_load($results[0]->nid);
return array(
'node' => $node,
);
}
/**
* Configuration form for 'rb_misc_action_variable_get'.
*/
function rb_misc_action_variable_get_form($settings, &$form) {
// Get a list of all defined system variables, formatted as a select array.
global $conf;
$selectable_variables = array();
foreach ($conf as $variable => $value) {
if (is_string($value) || is_integer($value) || is_bool($variable)) {
$selectable_variables[$variable] = $variable;
}
}
$form['settings']['variable'] = array(
'#type' => 'select',
'#options' => $selectable_variables,
'#default_value' => $settings['variable'],
'#title' => t('System variable'),
'#description' => t('Select the system variable you want to load. Note that
only string, number and boolean variables are available.'),
);
}
/**
* The 'rb_misc_action_variable_get' action.
*/
function rb_misc_action_variable_get($settings) {
return array(
'variable' => variable_get($settings['variable'], NULL),
);
}
/**
* Configuration form for 'rb_misc_action_get_url_alias'.
*/
function rb_misc_action_get_url_alias_form($settings, &$form) {
$form['settings']['path'] = array(
'#type' => 'textfield',
'#default_value' => $settings['path'],
'#title' => t('Internal path'),
'#description' => t('The internal path to get an alias for. If no alias is
set, the internal path itself will be returned.'),
);
}
/**
* The 'rb_misc_action_get_url_alias' action.
*/
function rb_misc_action_get_url_alias($settings) {
$alias = url($settings['path']);
// Trim any starting base path in the alias.
global $base_path;
if (strpos($alias, $base_path) === 0) {
$alias = substr($alias, strlen($base_path));
}
// Trim any starting '/' in the alias.
if (strpos($alias, '/') === 0) {
$alias = substr($alias, 1);
}
// URL aliases are URL encoded, so we decode it before returning it. (If we
// use it as a new URL alias it would be URL encoded anyway. No worries.)
return array(
'alias' => urldecode($alias),
);
}
/**
* Configuration form for 'rb_misc_action_set_active_menu_item'.
*/
function rb_misc_action_set_active_menu_item_form($settings, &$form) {
$form['settings']['menu_item'] = array(
'#type' => 'select',
'#default_value' => $settings['menu_item'],
'#title' => t('Menu item'),
'#options' => menu_parent_options(menu_get_menus(), array(
'mlid' => 0,
)),
'#description' => t('Select the menu item that should be marked active.'),
);
}
/**
* The 'rb_misc_action_set_active_menu_item' action.
*/
function rb_misc_action_set_active_menu_item($settings) {
// 90 percent of the code below is shamelessly stolen from the menu_position
// module, by JohnAlbin (http://drupal.org/user/32095). Cred to him!
$configuration = explode(':', $settings['menu_item']);
$menu_name = $configuration[0];
$mlid = $configuration[1];
// Retrieve menu item specified in the settings.
$menu_item = menu_link_load($mlid);
// Clone the original router item, but insert our menu_position path.
$original_router_item = menu_get_item();
$router_item = $original_router_item;
$router_item['href'] = $menu_item['link_path'];
$router_item['menu_name'] = $menu_name;
// Even if we are denied access to the page, we still want to show the
// navigational paths to the page.
$router_item['access'] = TRUE;
// Temporarily override the original router item.
menu_set_item(NULL, $router_item);
// The magic goes here. Cred to JohnAlbin.
menu_position_precache_tree($router_item, $original_router_item, $menu_item['menu_name']);
// Restore the original router item.
menu_set_item(NULL, $original_router_item);
// TODO: Find a way to add the 'active' CCS class to the menu item. I have
// studied context_reaction_menu.inc in the Context module, but it has so far
// not made me any wiser. //Itangalo.
}
/**
* Action "Get import nid for content"
*/
function rb_misc_action_get_import_nid($node, $settings) {
// Get any import node for this node.
$result = db_fetch_object(db_query("SELECT feed_nid FROM {feeds_node_item} WHERE nid = %d", $node->nid));
// If we got any match on import node, return it.
if ($result) {
return array(
'import_nid' => $result->feed_nid,
);
}
// Otherwise, just return. Nothing to save.
return;
}
/**
* Configuration form for 'rb_misc_action_set_node_creation_time'.
*/
function rb_misc_action_set_node_creation_time_form($settings, &$form) {
$form['settings']['time'] = array(
'#type' => 'textfield',
'#title' => t('Time value'),
'#default_value' => $settings['time'],
'#description' => t('Enter any time expression parsable for PHP strtotime().
Leave this field empty to use the current time.'),
);
}
/**
* The 'rb_misc_action_set_node_creation_time' action.
*/
function rb_misc_action_set_node_creation_time($node, $settings) {
// Load the configured time as a timestamp, or default to current time.
$time = empty($settings['time']) ? time() : strtotime($settings['time']);
// Set the post time and return the node for saving.
$node->created = $time;
return array(
'node' => $node,
);
}
Functions
Name | Description |
---|---|
rb_misc_action_arg | The 'rb_misc_action_arg' action. |
rb_misc_action_arg_form | Configuration form for 'rb_misc_action_arg'. |
rb_misc_action_get_import_nid | Action "Get import nid for content" |
rb_misc_action_get_time | The 'rb_misc_action_get_time' action. |
rb_misc_action_get_time_form | Configuration form for 'rb_misc_action_get_time'. |
rb_misc_action_get_url_alias | The 'rb_misc_action_get_url_alias' action. |
rb_misc_action_get_url_alias_form | Configuration form for 'rb_misc_action_get_url_alias'. |
rb_misc_action_set_active_menu_item | The 'rb_misc_action_set_active_menu_item' action. |
rb_misc_action_set_active_menu_item_form | Configuration form for 'rb_misc_action_set_active_menu_item'. |
rb_misc_action_set_node_creation_time | The 'rb_misc_action_set_node_creation_time' action. |
rb_misc_action_set_node_creation_time_form | Configuration form for 'rb_misc_action_set_node_creation_time'. |
rb_misc_action_variable_get | The 'rb_misc_action_variable_get' action. |
rb_misc_action_variable_get_form | Configuration form for 'rb_misc_action_variable_get'. |
rb_misc_action_views_load_node | The 'rb_misc_action_views_load_node' action. |
rb_misc_action_views_load_node_form | Configuration form for 'rb_misc_action_views_load_node'. |
rb_misc_action_views_result_count | The 'rb_misc_action_views_result_count' condition. |
rb_misc_action_views_result_count_form | Configuration form for 'rb_misc_action_views_result_count'. |
rb_misc_condition_arg | The 'rb_misc_condition_arg' condition. |
rb_misc_condition_arg_form | Configuration form for 'rb_misc_condition_arg'. |
rb_misc_rules_action_info | Implementation of hook_rules_action_info(). |
rb_misc_rules_condition_info | Implementation of hook_rules_condition_info(). |