I am trying to configure a workflow-ng workflow based on whether a user who just registered has entered a promotion code or not. So I need some variable or argument accessible from a PHP Code box that I can base a conditional on to determine the direction of the workflow. Something like:

if ($promotion_code) return TRUE;
else return FALSE;

I imagine this would be useful for many tasks in addition to workflow-ng.

Comments

dafeder’s picture

I've been trying this code, which seems like it should work but doesn't. I'm suspecting the php just isn't being executed, is that possible?

$result = db_query("SELECT uid FROM {promotion_codes_users} WHERE uid = %d", $user->uid);

if (db_num_rows($result) > 0) return TRUE; 
else return FALSE;
zostay’s picture

I'm not sure why the code snippet you've suggested there isn't working. I would have written it like this though:

return db_result(db_query("SELECT COUNT(*) FROM {promotion_codes_user} WHERE uid = %d", $user->uid)) > 0;

I come from Perl and naturally prefer one-liners. ;)

I'm familiar with workflow-ng, but I haven't written conditions in PHP before, so I don't know how they are supposed to fail or succeed. However, that snippet above should return true if the user has a valid promotion code and false if he does not.

I also do not know when you are running this. If you are running it during User Registration, it might be possible that the workflow-ng hook is running before the promotion module hook, which would cause the snippet to be false anyway.

If you do find a way to make it work or a patch for the module, please let me know and I'll consider incorporating it.

dafeder’s picture

I've got this working - I'm not sure if it was switching "true" and "false" (i had them backwards with a "negate" before, maybe that's not correct?) or using $account instead of $user. Anyway, here's the workflow export:

array (
  'cfg_3' => 
  array (
    '#type' => 'configuration',
    '#altered' => false,
    '#event' => 'user_insert',
    '#label' => 'Request payment on registration',
    '#active' => 1,
    '#module' => 'workflow-ng',
    0 => 
    array (
      '#type' => 'condition',
      '#name' => 'workflow_ng_condition_custom_php',
      '#settings' => 
      array (
        'php' => '$result = db_query("SELECT uid FROM {promotion_codes_users} WHERE uid = %d", $account->uid);

if (db_num_rows($result) > 0) return FALSE;
else return TRUE;',
        'used_arguments' => 
        array (
        ),
        'used_php_arguments' => 
        array (
          0 => 'account',
        ),
      ),
      '#label' => 'Check for promotion code',
    ),
    1 => 
    array (
      '#type' => 'action',
      '#name' => 'workflow_ng_action_drupal_goto',
      '#settings' => 
      array (
        'path' => 'membership-payment',
        'path_args' => 
        array (
        ),
        'query' => '',
        'query_args' => 
        array (
        ),
        'fragment' => '',
        'fragment_args' => 
        array (
        ),
        'force' => 1,
        'override' => 1,
      ),
    ),
    '#name' => 'cfg_3',
  ),
)
zostay’s picture

Status: Active » Fixed

I'm going to assume this is resolved, then, unless you need something else.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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