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 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.

Comments

infojunkie’s picture

Assigned: Unassigned » infojunkie

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

infojunkie’s picture

StatusFileSize
new806 bytes

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.

uprojects’s picture

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 .

infojunkie’s picture

Title: It does not work !!! » add support for rules
infojunkie’s picture

@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?

fago’s picture

Status: Active » Needs work
Issue tags: +rules integration

cool!

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

amitaibu’s picture

Subscribe for later testing.

infojunkie’s picture

@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.

amitaibu’s picture

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

fago’s picture

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

fago’s picture

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.

infojunkie’s picture

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?

uprojects’s picture

ok !

It's a warning message.

infojunkie’s picture

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.

fago’s picture

Status: Fixed » Closed (fixed)

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

dimiduj’s picture

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)...

infojunkie’s picture

Second argument to which function?

dimiduj’s picture

Sorry,
I mean a second parameter,

The user as second parameter for the ruleset...

infojunkie’s picture

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?

dimiduj’s picture

Hi ,Kratib,

thanks for your fast ansers,

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

Im trying to do something like that:

>
<?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 ...

amitaibu’s picture

@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.

dimiduj’s picture

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

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;
}


dimiduj’s picture

StatusFileSize
new1.54 KB

Hi,

I provide a patch,

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

infojunkie’s picture

Status: Closed (fixed) » 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.

dimiduj’s picture

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

itangalo’s picture

Component: Code » Core

Me and some other guys (http://groups.drupal.org/node/41318#comment-150363) had been wondering why rule sets didn't appear in VBO, and I found the answer in this thread.

It would be really nice if rule sets with no arguments would be included in VBO, since rule sets with no arguments can still be really valuable.
I can see that rule sets with more than one argument could be a problem -- how would VBO know what to put into the second argument? But for no-argument sets this is not a problem.

I would have applied a patch, but don't really know how to handle the type parameter to make it fit with *all* types of views.

Thanks for an awsome module.
//Johan Falk, Sweden
http://nodeone.se/drupal-planet/make-rules-dance-with-views-bulk-operations

@dimiduj: You don't have to use an extra argument in the rule set to reach the global user. You can use the "Load a user account" action and reach for the global token [node:user-name] (which actually loads the global user and not the node author, despite its name).

infojunkie’s picture

@Itangalo: to make an action appear in all VBOs, its type should be 'system'. Does that help?

infojunkie’s picture

I just committed a fix to support zero-parameter rule sets as 'system' actions. Thanks for the suggestion.

robby.smith’s picture

subscribing

niklp’s picture

I really feel that being able to pass more than one parameter through the view would be useful. My current use case illustrates this.

I want to create a node, then update all referenced users (on that node) via email that the node has been created. I can certainly do that (now that I have a relevant patch for userref module) but because I can't pass another parameter (nid) to the rule set, I can't notify user x *that node y* has been created.

I don't see this as a major problem in most/some cases? I can easily get the right node id - and we're just setting views fields as the parameters, surely? So why can't we just say that rule set parameters are coming from views fields, in the order that they're displayed?

Hopefully I'm understanding this - I thought it was possible but it seems like I'm (or we are) missing a trick here - this would be very useful, because tokens can't cover these bases - due to triggering rule -> VBO view -> rule set not being properly "integrated".

infojunkie’s picture

Status: Needs work » Needs review

The latest dev contains some support for arbitrary ruleset arguments. To try it:
* Create a rule set with many arguments
* Enable the corresponding action in VBO
* When executing the action, you will be asked to write PHP code to return a value for each argument. Write the script without PHP enclosing tags.

You can also create an advanced action that saves this PHP code, so that you don't need to re-write the code every time you execute the VBO.

Someone please try it (12 hours from now) and let me know!

rburgundy’s picture

subscribing

itangalo’s picture

Awesome work! This brings even more power to the VBO + Rules combination.

There is a similar project going on at http://drupal.org/project/rules_views, "Rules and Views Integration".
I'm posting a comment in its issue queue to notify the maintainer (Steven Jones, http://drupal.org/user/99644).

You guys rock.

itangalo’s picture

When reviewing this update I was unable to access the $context or the $object to send any arguments to the rule set. This is probably linked to the issue at http://drupal.org/node/740754

I'll get back when I have more information – I'd like to try it again and see what I can find. (For some reason I couldn't get dpm() to print anything at all when called from the argument settings, which tells me that there might be something else faulting.)

Still: This module rocks.

itangalo’s picture

Ok, I'm back on track!

For information:
* There should be _no_ <?php and ?> tags in in the box where you provide the arguments for the rule set.
* The rule set expects an entire object, not just a nid or uid.

Sorry for implying that this function had a bug – it was just me. :-)

itangalo’s picture

StatusFileSize
new1.03 KB

...and here's a patch with an extended description text for the Rules arguments field(s).

Now says:
"Argument of type node. Enter PHP script that will return a value for this argument. Note that Rules expects a entire object (eg $node) rather than an id (eg nid). The variables $object and $context are available to this script. Do not include <?php and ?> tags."

Previously said:
"Argument of type node. Enter PHP script that will return a value for this argument. The variables $object and $context are available to this script."

itangalo’s picture

StatusFileSize
new2.17 KB

...and here's a patch with a few more suggestions for text changes:

* A description text for selecting an object for the 'Execute VBO programmatically' action: "The chosen object will be loaded as the $object variable, available below."
* A change of action name, from "Execute a VBO programmatically on !type" to "Use !type object to execute a VBO programmatically".
* The description texts from the patch above.

Cheers!

milos.kroulik’s picture

subscribe

infojunkie’s picture

Status: Needs review » Fixed

Thanks for the patch. I committed it to the latest dev. Closing this and we can focus on specific problems in new issues.

Status: Fixed » Closed (fixed)
Issue tags: -rules integration

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