t('Produce a random list of users'), 'arguments' => array( 'max_length' => array( 'type' => 'number', 'label' => t('The size of the list to produce'), 'description' => t('No more than this number of items will be included in the list.'), ), ), 'new variables' => array( 'user_list_random' => array( 'type' => 'user_list', 'label' => t('Random list of users'), ), ), 'module' => 'User', ); $items['rules_produce_random_node_list'] = array( 'label' => t('Produce a random list of nodes'), 'arguments' => array( 'max_length' => array( 'type' => 'number', 'label' => t('The size of the list to produce'), 'description' => t('No more than this number of items will be included in the list.'), ), ), 'new variables' => array( 'node_list_random' => array( 'type' => 'node_list', 'label' => t('Random list of nodes'), ), ), 'module' => 'Node', ); if (module_exists('views')) { $items['rules_views_produce_node_list'] = array( 'label' => t('Produce a list of nodes from a view'), 'arguments' => array( 'view_name' => array( 'type' => 'rules_views_view_name', 'base_table' => 'node', 'label' => t('The view from which to produce a node list'), 'description' => t('(Only views of type node are shown.)'), 'required' => TRUE, ), 'view_display' => array( 'type' => 'string', 'label' => t("The view's display"), 'description' => t('(Optional.) A view may contain several displays, which are effectively "sub views". Enter the display to use; e.g. page_1, page_2, block_1. Leave blank to use the default display.'), 'required' => FALSE, ), 'view_args' => array( 'type' => 'string', 'label' => t('View arguments'), 'description' => t('(Optional.) Provide a comma separated list of arguments to pass to the view.'), 'required' => FALSE, ), 'limit' => array( 'type' => 'string', 'label' => t('Limit results to'), 'description' => t('(Optional.) Usually, all items answering the view\'s criteria will be included in the list, regardless of whether or not the "Use pager" or "Items per page" settings have been specified for the view. By typing a number here you can limit the view\'s results to a certain number of items.'), 'required' => FALSE, ), ), 'new variables' => array( 'node_list_from_view' => array( 'type' => 'node_list', 'label' => t('List of nodes produced from view'), ), ), 'module' => 'Node', ); $items['rules_views_produce_user_list'] = array( 'label' => t('Produce a list of users from a view'), 'arguments' => array( 'view_name' => array( 'type' => 'rules_views_view_name', 'base_table' => 'users', 'label' => t('The view from which to produce a user list'), 'description' => t('(Only views of type user are shown.)'), 'required' => TRUE, ), 'view_display' => array( 'type' => 'string', 'label' => t("The view's display"), 'description' => t('(Optional.) A view may contain several displays, which are effectively "sub views". Enter the display to use; e.g. page_1, page_2, block_1. Leave blank to use the default display.'), 'required' => FALSE, ), 'view_args' => array( 'type' => 'string', 'label' => t('View arguments'), 'description' => t('(Optional.) Provide a comma separated list of arguments to pass to the view.'), 'required' => FALSE, ), 'limit' => array( 'type' => 'string', 'label' => t('Limit results to'), 'description' => t('(Optional.) Usually, all items answering the view\'s criteria will be included in the list, regardless of whether or not the "Use pager" or "Items per page" settings have been specified for the view. By typing a number here you can limit the view\'s results to a certain number of items.'), 'required' => FALSE, ), ), 'new variables' => array( 'user_list_from_view' => array( 'type' => 'user_list', 'label' => t('List of users produced from view'), ), ), 'module' => 'User', ); } return $items; } /** * Produces a random list of nodes. Used for debugging. */ function rules_produce_random_node_list($max_length) { $list = array(); $res = db_query_range('SELECT nid FROM {node} ORDER BY RAND()', 0, $max_length); while ($row = db_fetch_object($res)) { $list[] = $row->nid; } return array('node_list_random' => $list); } /** * Produces a random list of users. Used for debugging. */ function rules_produce_random_user_list($max_length) { $list = array(); $res = db_query_range('SELECT uid FROM {users} WHERE uid <> 0 ORDER BY RAND()', 0, $max_length); while ($row = db_fetch_object($res)) { $list[] = $row->uid; } return array('user_list_random' => $list); } // --------------------------------------------------------------------------- // Views support // // (The names of te functions/actions/types here are such that it'd be easy to // move them to a 'rules_views' module, if so desired.) function rules_views_produce_node_list_label($settings, $argument_labels, $element) { return t('Produce a list of nodes from the "@view_name" view', array('@view_name' => $settings['view_name'])); } function rules_views_produce_user_list_label($settings, $argument_labels, $element) { return t('Produce a list of users from the "@view_name" view', array('@view_name' => $settings['view_name'])); } /** * Implementation of hook_rules_data_type_info() */ function list_generators_rules_data_type_info() { return array( 'rules_views_view_name' => array( 'label' => t('View name'), 'class' => 'rules_views_data_type_view_name', 'identifiable' => FALSE, ), ); } /** * Defines the view_name type */ class rules_views_data_type_view_name extends rules_data_type { /** * Gets the input form for the data */ function get_default_input_form($info, $value) { $info += array('base_table' => 'node', 'required' => TRUE, 'description' => ''); return array( '#type' => 'select', '#title' => $info['label'], '#description' => $info['description'], '#options' => $this->_get_views_options($info['base_table']), '#required' => $info['required'], '#default_value' => $value, ); } function _get_views_options($base_table) { $options = array(); $all_views = views_get_all_views(); foreach ($all_views as $view) { if ($view->base_table == $base_table) { if ($view->type == 'Default') { $options[t('Default views')][$view->name] = $view->name; } else { $options[t('Existing views')][$view->name] = $view->name; } } } return $options; } } function rules_views_produce_node_list($view_name, $view_display, $view_args, $limit) { if (module_exists('views')) { $ids = _rules_views_get_view_results($view_name, $view_display, $view_args, $limit, 'nid'); } else { $ids = array(); } return array('node_list_from_view' => $ids); } function rules_views_produce_user_list($view_name, $view_display, $view_args, $limit) { if (module_exists('views')) { $ids = _rules_views_get_view_results($view_name, $view_display, $view_args, $limit, 'uid'); } else { $ids = array(); } return array('user_list_from_view' => $ids); } function _rules_views_get_view_results($view_name, $view_display, $view_args, $limit, $id_column) { $ids = array(); $limit = (int)$limit; $view_args = !empty($view_args) ? array_map('trim', explode(',', $view_args)) : array(); if ($view = views_get_view($view_name)) { $view->set_use_pager(FALSE); $view->set_items_per_page($limit); $view->set_arguments($view_args); $view->is_cacheable = FALSE; $view->execute(!empty($view_display) ? $view_display : 'default'); foreach ($view->result as $row) { $ids[] = $row->{$id_column}; } } return $ids; }