<div dir="ltr">Ok.<div><br></div><div style>Till now I have created a pair of actions; one which I call at the start of my rule to impersonate "node changing user", and one at last to change back. Using this I can call scheduled rules with the user who triggered them. It is all I need to the moment, so I will stop looking further into batching actions (limited time).</div>
<div style><br></div><div style>Thanks.<br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/1/21 Earnie Boyd <span dir="ltr"><<a href="mailto:earnie@users.sourceforge.net" target="_blank">earnie@users.sourceforge.net</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">drupal_static() is not equivalent to PHP static. The example you've<br>
given is using PHP static and yes, it will not be available in another<br>
function.<br>
<br>
<a href="http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_static/7" target="_blank">http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_static/7</a><br>
<div class="HOEnZb"><div class="h5"><br>
On Mon, Jan 21, 2013 at 11:21 AM, Lluís Forns <<a href="mailto:enboig@gmail.com">enboig@gmail.com</a>> wrote:<br>
> I know we are going OT, but I also want to understand this;<br>
><br>
> what about:<br>
><br>
><br>
> function mymodule_complex_calculation() {<br>
> static $cache;<br>
> if (isset($cache)) {<br>
> return $cache;<br>
> }<br>
> // Otherwise we need to perform our calculations<br>
> // ...some complex calculation that returns<br>
> $cache = complex_calculation();<br>
> return $cache;<br>
> }<br>
><br>
><br>
> <a href="http://techblog.zabuchy.net/2011/enhancing-drupal-performance-with-drupal_static/" target="_blank">http://techblog.zabuchy.net/2011/enhancing-drupal-performance-with-drupal_static/</a><br>
><br>
> You are saying that if I instantiate a variable in "mymodule_impersonate()"<br>
> it will not be available in "mymodule_unimpersonate()"?<br>
><br>
><br>
> 2013/1/21 Earnie Boyd <<a href="mailto:earnie@users.sourceforge.net">earnie@users.sourceforge.net</a>><br>
>><br>
>> You "think" wrongly. A static variable has local scope to the<br>
>> function declaring the static variable.<br>
>><br>
>> On Mon, Jan 21, 2013 at 11:03 AM, Lluís Forns <<a href="mailto:enboig@gmail.com">enboig@gmail.com</a>> wrote:<br>
>> > I think static are also available to other functions, use them to store<br>
>> > cached information across my code and they work.<br>
>> ><br>
>> ><br>
>> > 2013/1/21 Darren Oh <<a href="mailto:darrenoh@sidepotsinternational.com">darrenoh@sidepotsinternational.com</a>><br>
>> >><br>
>> >> Static variables are only saved for a single function. Global variables<br>
>> >> are available to all functions during a single page request. Session<br>
>> >> variables are available to all page requests during a session.<br>
>> >><br>
>> >> On Jan 21, 2013, at 10:08 AM, Lluís Forns wrote:<br>
>> >><br>
>> >> Darren, thanks for the link. What is the difference between using<br>
>> >> global<br>
>> >> or static to save the data?<br>
>> >><br>
>> >><br>
>> >><br>
>> >> 2013/1/21 Lluís Forns <<a href="mailto:enboig@gmail.com">enboig@gmail.com</a>><br>
>> >>><br>
>> >>> My code was from a module where impersonation recursively could<br>
>> >>> happen,<br>
>> >>> this is why I use a stack to save user state. I am also saving it to<br>
>> >>> session<br>
>> >>> and not using a static variable because I may uses batch functions.<br>
>> >>><br>
>> >>> But I see some problems right now:<br>
>> >>> - To be safe impersonation should be saved into static variables (and<br>
>> >>> "using session_save_session(FALSE);" ), so if anything goes wrong,<br>
>> >>> $user is<br>
>> >>> not afected.<br>
>> >>> - If I want to be able to execute every Rules action in a batch, I<br>
>> >>> need<br>
>> >>> to change user "per action"<br>
>> >>><br>
>> >>> Any idea of how to "inject" code before and after a rule action?<br>
>> >>><br>
>> >>><br>
>> >>> 2013/1/21 Walt Daniels <<a href="mailto:wdlists@gmail.com">wdlists@gmail.com</a>><br>
>> >>>><br>
>> >>>> We do the latter simple trick in our store code so we can show people<br>
>> >>>> the member prices. We have a dummy id that is a member so we switch<br>
>> >>>> to it,<br>
>> >>>> pick up the price and use it so we can show both member and<br>
>> >>>> non-member<br>
>> >>>> prices on the screen.<br>
>> >>>><br>
>> >>>><br>
>> >>>> On Mon, Jan 21, 2013 at 9:18 AM, Christian López Espínola<br>
>> >>>> <<a href="mailto:penyaskito@computer.org">penyaskito@computer.org</a>> wrote:<br>
>> >>>>><br>
>> >>>>> Haven't tested your code, but maybe you want to take a look at<br>
>> >>>>> masquerade_switch_user and masquerade_switch_back:<br>
>> >>>>><br>
>> >>>>> <a href="http://drupalcode.org/project/masquerade.git/blob/HEAD:/masquerade.module" target="_blank">http://drupalcode.org/project/masquerade.git/blob/HEAD:/masquerade.module</a>.<br>
>> >>>>><br>
>> >>>>> And if the impersonation is only for the given request, maybe just<br>
>> >>>>><br>
>> >>>>> global $user;<br>
>> >>>>> $previous = $user;<br>
>> >>>>> $user = user_load($uid);<br>
>> >>>>> // Do your stuff.<br>
>> >>>>> $user = $previous;<br>
>> >>>>><br>
>> >>>>> would work.<br>
>> >>>>><br>
>> >>>>><br>
>> >>>>><br>
>> >>>>> On Mon, Jan 21, 2013 at 10:37 AM, Lluís Forns <<a href="mailto:enboig@gmail.com">enboig@gmail.com</a>><br>
>> >>>>> wrote:<br>
>> >>>>>><br>
>> >>>>>> Some of my scheduled task needs to run as the user who triggered<br>
>> >>>>>> them<br>
>> >>>>>> (not anonymous neither admin); so I thought of writing two actions:<br>
>> >>>>>> - One to impersonate a user, which will receive uid as an argument.<br>
>> >>>>>> - Another to undo the impersonate.<br>
>> >>>>>> All the called actions should be between them.<br>
>> >>>>>><br>
>> >>>>>> Anybody see any fault in this idea or the actions code?<br>
>> >>>>>><br>
>> >>>>>> Thanks.<br>
>> >>>>>><br>
>> >>>>>> function wu_action_impersonate($uid) {<br>
>> >>>>>> global $user;<br>
>> >>>>>> if (!array_key_exists('wu_action_impersonate', $_SESSION)) {<br>
>> >>>>>> $_SESSION['wu_action_impersonate'] = array();<br>
>> >>>>>> }<br>
>> >>>>>> array_push($_SESSION['wu_action_impersonate'], $user);<br>
>> >>>>>> session_save_session(FALSE);<br>
>> >>>>>> $user = user_load($uid);<br>
>> >>>>>> user_load($uid);<br>
>> >>>>>> }<br>
>> >>>>>><br>
>> >>>>>> function wu_action_unimpersonate() {<br>
>> >>>>>> global $user;<br>
>> >>>>>> if (array_key_exists('wu_action_impersonate', $_SESSION)) {<br>
>> >>>>>> if (!empty($_SESSION['wu_action_impersonate'])) {<br>
>> >>>>>> $user = array_pop($_SESSION['wu_action_impersonate']);<br>
>> >>>>>> }<br>
>> >>>>>> if (empty($_SESSION['wu_action_impersonate'])) {<br>
>> >>>>>> unset($_SESSION['wu_action_impersonate']);<br>
>> >>>>>> }<br>
>> >>>>>> }<br>
>> >>>>>> session_save_session(TRUE);<br>
>> >>>>>> }<br>
>> >>>>>><br>
>> >>>>>> --<br>
>> >>>>>> *Ser freak no és imprescindible per ser informàtic, però ajuda.<br>
>> >>>>>> *La vida no ha de ser feliç, ha de ser plena.<br>
>> >>>>>> *Abans d'imprimir aquest missatge, pensa en el medi ambient.<br>
>> >>>>><br>
>> >>>>><br>
>> >>>>><br>
>> >>>>><br>
>> >>>>> --<br>
>> >>>>> Cheers,<br>
>> >>>>><br>
>> >>>>> Christian López Espínola <penyaskito AT computer DOT org><br>
>> >>>>> <a href="http://twitter.com/penyaskito" target="_blank">http://twitter.com/penyaskito</a> | <a href="http://penyaskito.com" target="_blank">http://penyaskito.com</a><br>
>> >>>><br>
>> >>>><br>
>> >>><br>
>> >>><br>
>> >>><br>
>> >>> --<br>
>> >>> *Ser freak no és imprescindible per ser informàtic, però ajuda.<br>
>> >>> *La vida no ha de ser feliç, ha de ser plena.<br>
>> >>> *Abans d'imprimir aquest missatge, pensa en el medi ambient.<br>
>> >><br>
>> >><br>
>> >><br>
>> >><br>
>> >> --<br>
>> >> *Ser freak no és imprescindible per ser informàtic, però ajuda.<br>
>> >> *La vida no ha de ser feliç, ha de ser plena.<br>
>> >> *Abans d'imprimir aquest missatge, pensa en el medi ambient.<br>
>> >><br>
>> >><br>
>> ><br>
>> ><br>
>> ><br>
>> > --<br>
>> > *Ser freak no és imprescindible per ser informàtic, però ajuda.<br>
>> > *La vida no ha de ser feliç, ha de ser plena.<br>
>> > *Abans d'imprimir aquest missatge, pensa en el medi ambient.<br>
>><br>
>><br>
>><br>
>> --<br>
>> Earnie<br>
>> -- <a href="https://sites.google.com/site/earnieboyd" target="_blank">https://sites.google.com/site/earnieboyd</a><br>
><br>
><br>
><br>
><br>
> --<br>
> *Ser freak no és imprescindible per ser informàtic, però ajuda.<br>
> *La vida no ha de ser feliç, ha de ser plena.<br>
> *Abans d'imprimir aquest missatge, pensa en el medi ambient.<br>
<br>
<br>
<br>
--<br>
Earnie<br>
-- <a href="https://sites.google.com/site/earnieboyd" target="_blank">https://sites.google.com/site/earnieboyd</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>*Ser freak no és imprescindible per ser informàtic, però ajuda.<br>*La vida no ha de ser feliç, ha de ser plena.<br>*Abans d'imprimir aquest missatge, pensa en el medi ambient.
</div>