add support for rules

fago - January 22, 2009 - 12:01
Project:Views Bulk Operations (VBO)
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:kratib
Status:needs work
Issue tags:rules integration
Description

http://drupal.org/project/rules

It would be really nice to have support to execute any "rule set" instead of an action or operation. Rule sets can be created by users, and may contain multiple rules. So this would allow users not only to execute any by rules supported action (e.g. populate a cck field) to be used with vbo but also to execute them only conditionally!

Executing a rule set is as simple as calling
<? php
rules_invoke_rule_set('set_name', arg1, arg2);
//e.g.
rules_invoke_rule_set('publish_node', $node);
?>

To get information about all available rule sets just call

<?php
rules_get_configured_items
('rule_sets')
?>
, which returns an array where the keys are the names of the sets. Probably you want to filter out rule sets with just one argument being of type node, user or something.

I also plan to add a mapping from rule data types to views base tables to rules - similar as your hook_object_info(). However it's not there yet. However if it's needed, I got give it priority and add it asap.

#1

kratib - January 22, 2009 - 17:13
Assigned to:Anonymous» kratib

Thanks for the interesting suggestion. I'll work on it in the near future.

#2

kratib - January 24, 2009 - 09:55

Here's a simple solution for this feature: export each ruleset as an action that VBO can readily consume. Attached is a file that does this, if you would like to incorporate it in your module that would be great. Otherwise, I can host it in my module, although it will be tightly coupled to your ruleset data structure.

Note that you'll need the latest 6.x-1.x-dev release (that I just checked in) for this to work properly.

AttachmentSize
rules.action.inc_.txt 806 bytes

#3

servicedevis - February 4, 2009 - 10:51
Title:add support for rules» It does not work !!!

I added this file in the VBO directory but any action created with rules appear in vbo style plugin ! and it create a callback warning error .
This issues is very interesting ! :-)

Merci !
Sorry for my english .

#4

kratib - February 4, 2009 - 16:49
Title:It does not work !!!» add support for rules

#5

kratib - February 4, 2009 - 23:03

@servicedevis: Please don't change the title of the issue :-) As for your comment:
* The point of this file is to show rulesets, not rules, as part of the available VBO operations.
* What's the callback message you get? Is it a warning , or an error?

#6

fago - February 6, 2009 - 10:22
Status:active» needs work

cool!

However you accidentally implement hook_rules_action_info() for vbo - so best we should rename the function a bit to avoid troubles.

#7

Amitaibu - February 6, 2009 - 13:20

Subscribe for later testing.

#8

kratib - February 6, 2009 - 19:22

@fago: please let me know if you'd like to integrate this code (after suitable modifications) to your module. Otherwise I'll just bundle it in VBO.

#9

Amitaibu - February 6, 2009 - 19:25

@Kratib,
It should be bundled with VBO (as CCK and OG did with their integration).
Having this feature in VBO is very cool :)

#10

fago - February 10, 2009 - 10:59

As amitaibu said, it should be bundled with vbo. Indeed this feature is really cool :)

#11

fago - February 10, 2009 - 15:15

Hm, the actions provided by vbo have troubles with rules: #372114: Crash with "Modifying node's taxonomy terms"

What about adding some code directly into .rules.inc that includes the action includes of vbo? So once .rules.inc gets included, it would automatically include the actions includes and everything should work.

#12

kratib - February 10, 2009 - 23:23

I don't understand the relationship between invoking an action from a rule, and including VBO actions inside the rules.inc file. Does the rules.inc file automatically get loaded when a rule fires?

#13

servicedevis - February 11, 2009 - 01:24

ok !

It's a warning message.

#14

kratib - February 19, 2009 - 18:44
Status:needs work» fixed

The latest VBO 6.x-1.x-dev contains support for rules as specified above.

@fago, I'm marking this issue as fixed and please feel free to open a new issue concerning the ability of calling VBO actions from within rules.

#15

fago - February 25, 2009 - 19:58

#16

System Message - March 11, 2009 - 20:00
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

#17

dimiduj - June 26, 2009 - 15:18

Hi everybody,

Is it complicate to add the $user as second argument ?

The benefit of this will be great ...

Allowing only one argument limit the power of the rules (more php rules, less built-in rules)...

#18

kratib - June 26, 2009 - 15:57

Second argument to which function?

#19

dimiduj - June 29, 2009 - 15:10

Sorry,
I mean a second parameter,

The user as second parameter for the ruleset...

#20

kratib - June 30, 2009 - 05:15

I still don't understand what you mean. Here's what happens on the VBO side:
* The user selects a few objects (nodes, users) that are shown in the view
* The user chooses to execute one of the rule_set actions
* For each selected object, VBO calls the rule_set action by calling rules_invoke_rule_set() and passing it the object

If the objects are nodes, where are the users that you are referring to?

If you mean that VBO should be able to invoke rule_sets for users, that is possible today by creating a VBO of type User. Then, any rule_set that is declared to act on users (i.e. where the rule_set has one argument whose type = 'User') will be available for that VBO.

Currently, the rule_set action does not handle rule_sets with more than one argument. This is because the action would not know what to pass as values for the remaining arguments. Do you have a suggestion on how this situation could be handled?

#21

dimiduj - June 30, 2009 - 07:25

Hi ,Kratib,

thanks for your fast ansers,

I was talking about the "global $user"...

Im trying to do something like that:

<?php
>
<?
php


function views_bulk_operations_ruleset_action_info() {
  if (!
module_exists('rules')) return array();
 
$actions = array();
  foreach (
rules_get_configured_items('rule_sets') as $ruleset_key => $ruleset) {
   
    if (
count($ruleset['arguments']) == 2) { // For now, we only accept rulesets with one parameter (taken to be the 'type')
     
$arg = key(current($ruleset['arguments']));
     
$arg1 = key(current($ruleset['arguments']));
     
$arg2 = key(next($ruleset['arguments']));
     
     
$actions["views_bulk_operations_ruleset_action_{$ruleset_key}"] = array(
       
'type' => $ruleset['arguments'][$arg]['type'],
       
'parameters' => array('ruleset' => $ruleset_key),
       
'description' => $ruleset['label'],
       
'configurable' => FALSE,
       
'rules_ignore' => TRUE,
      );
      eval(<<<EOS
if (!function_exists('views_bulk_operations_ruleset_action_{$ruleset_key}')) {
  function views_bulk_operations_ruleset_action_
{$ruleset_key}(&\$object, \$context) {
      global \$user;
    rules_invoke_rule_set(\$context['ruleset'], \$object, \$user);
  }
}
EOS
      );
    }
  }
  return
$actions;
}
?>

This should allow to make more condition in the rules such as "if the current user has a certain role..." etc ...

#22

Amitaibu - June 30, 2009 - 07:25

@kratib,

Rules sets accept pre-defined arguments (node, user, number, etc'). If you pass more arguments chances are you are able trigger more rule sets. So even if you pass a node, passing a user argument will allow you for example to flag (using flag module) that node in the name of the user.

@dimiduj,
THe question is which user should be passed - The acting user? The content author? I would answer the acting user - simply because it's a simple global $user.

#23

dimiduj - July 1, 2009 - 07:48

I'm working on it with the global user...

Actually the code looks like
If there are two parameter it test if the second is of type user...

And it seems to work fine ...

<?php
<?php

function views_bulk_operations_ruleset_action_info() {
  if (!
module_exists('rules')) return array();
 
$actions = array();
  foreach (
rules_get_configured_items('rule_sets') as $ruleset_key => $ruleset) {
   
   
   
   
    
    if (
count($ruleset['arguments']) <= 1) { // For now, we only accept rulesets with one parameter (taken to be the 'type')
     
$arg = key($ruleset['arguments']);
    
// $arg1 = key(current($ruleset['arguments']));
    //  $arg2 = key(next($ruleset['arguments']));
     
     
$actions["views_bulk_operations_ruleset_action_{$ruleset_key}"] = array(
       
'type' => $ruleset['arguments'][$arg]['type'],
       
'parameters' => array('ruleset' => $ruleset_key),
       
'description' => $ruleset['label'],
       
'configurable' => FALSE,
       
'rules_ignore' => TRUE,
      );
      eval(<<<EOS
if (!function_exists('views_bulk_operations_ruleset_action_{$ruleset_key}')) {
  function views_bulk_operations_ruleset_action_
{$ruleset_key}(&\$object, \$context) {
    rules_invoke_rule_set(\$context['ruleset'], \$object);
  }
}
EOS
      );
    }
   
    if (
count($ruleset['arguments']) == 2) {

     
$arg = key($ruleset['arguments']);
     
$arg2=next($ruleset["arguments"]);
      if(
$arg2['type']=='user'){
    
       
$actions["views_bulk_operations_ruleset_action_{$ruleset_key}"] = array(
         
'type' => $ruleset['arguments'][$arg]['type'],
         
'parameters' => array('ruleset' => $ruleset_key),
         
'description' => $ruleset['label'],
         
'configurable' => FALSE,
         
'rules_ignore' => TRUE,
        );
        eval(<<<EOS
        if (!function_exists('views_bulk_operations_ruleset_action_{$ruleset_key}')) {
          function views_bulk_operations_ruleset_action_
{$ruleset_key}(&\$object, \$context) {
            global \$user;
            rules_invoke_rule_set(\$context['ruleset'], \$object,\$user);
          }
        }
EOS
        );
      }
    }
  }
  return
$actions;
}
?>

#24

dimiduj - July 1, 2009 - 07:49

Hi,

I provide a patch,

I don't know if i can/should change the status of the post ...?

AttachmentSize
ruleset.action.patch 1.54 KB

#25

kratib - July 1, 2009 - 16:47
Status:closed» needs work

Thanks for the patch. I'm still thinking about the generality of this solution so don't expect the patch to be committed very soon.

#26

dimiduj - July 18, 2009 - 02:06

No matter...
I'm agree with you that it 's a little tricky...

 
 

Drupal is a registered trademark of Dries Buytaert.