I am using rules so that when a content type group is created, I add a option to an ubercart attribute and enable that option on a product node, then, I set the adjustments for all the combinations that use that option to have a different sku. My next thing I want to automate is creating a feature that corresponds to the group created, and the sku. How should I do this?

How do I create a subscription feature in php code? By creating form_state and calling uc_og_subscribe_feature_form_submit?

What I have in my php code is the node structure (and thus the nid) for the og group, and the sku.

Assume I wanted to create a subscription feature for node 19 and sku "1001-oprf23".

... or should I ...

module_load_include('inc', 'uc_og_subscribe', 'uc_og_subscribe.ca.inc');
$form_state = array();
$form_state['values']['nid'] = 19;
$form_state['values']['uc_group_model'] = "1001-oprf23";
drupal_execute('uc-og-subscribe-feature-form', );

but I think what I really need is 'uc_group_gid'. is that the same as the nid? I think so looking at line 141 of uc_og_subscribe.module

well, I'll try

module_load_include('inc', 'uc_og_subscribe', 'uc_og_subscribe.ca.inc');
$form_state = array();
$form_state['values']['uc_group_gid'] = 19;
$form_state['values']['uc_group_model'] = "1001-oprf23";
drupal_execute('uc-og-subscribe-feature-form', );

Thanks for any help with this.

Comments

YesCT’s picture

opps forgot the form_state in the drupal execute:

<?php
module_load_include
('inc', 'uc_og_subscribe', 'uc_og_subscribe.ca.inc');
$form_state = array();
 
$form_state['values']['uc_group_gid'] = 19;
 
$form_state['values']['uc_group_model'] = "1001-oprf23";
drupal_execute('uc-og-subscribe-feature-form', $form_state);
?>
YesCT’s picture

had to change the -'s to _'s and send some extra arguments...

with

<?php
module_load_include
('inc', 'uc_og_subscribe', 'uc_og_subscribe.ca.inc');
$form_state = array();
$form_state['values']['uc_group_gid'] = 248;
$form_state['values']['uc_group_model'] = "1001-par1248";
$productnid=19;
drupal_execute('uc_og_subscribe_feature_form', $form_state, $productnid, NULL);
?>

now I get "An illegal choice has been detected. Please contact the site administrator."

I thought maybe I could send NULL for the feature/pfid since this would always be making a new feature, not editing one already existing...?

YesCT’s picture

I tracked down the error message as coming from (drupal/)includes/form.inc about line 2560
and saw there it was writing to the watchdog, so I checked the Recent Log Entries report and saw there:
Illegal choice 1001-par1248 in SKU element.
so I checked the source code on my form:
node/19/edit/features/group_subscribe/add
and see:

<?php
       
<div id="content-area">
          <
form action="/node/19/edit/features/group_subscribe/add"  accept-charset="UTF-8" method="post" id="uc-og-subscribe-feature-form">
<
div><div class="form-item" id="edit-uc-group-model-wrapper">
 <
label for="edit-uc-group-model">SKU: </label>
 <
select name="uc_group_model" class="form-select" id="edit-uc-group-model" ><option value="" selected="selected">- Any -</option><option value="1001">1001</option><option value="1001-NoGroup">1001-NoGroup</option><option value="1001-par1248">1001-par1248</option><option value="OakTre247">OakTre247</option></select>

 <
div class="description">This is the SKU that will need to be purchased to obtain the group subscription.</div>
</
div>
<
div class="form-item" id="edit-uc-group-gid-wrapper">
 <
label for="edit-uc-group-gid">Group name  : </label>
 <
select name="uc_group_gid" class="form-select" id="edit-uc-group-gid" ><option value="54">Taking helping situations</option><option value="55">AWOR LLLI</option><option value="56">AWOR</option><option value="57">Leader Reserve</option><option value="247">Oak Tree</option><option value="248">part 1</option></select>
 <
div class="description">The group that the user will be subscribed to.</div>

</
div>
?>

so.. it looks like I'm getting it right: value="1001-par1248"

Maybe I need to "fill out" the rest of the form? not just the two pull downs?

YesCT’s picture

more info that might mean something:

YesCT: I added some words to figure out which "illegal choice" was getting triggered in form.inc and it was the one around line 711
[2:39pm] YesCT: the last else if part of _form_validate http://api.drupal.org/api/function/_form_validate/6
[2:40pm] YesCT: sooo, I commented out that else if, to see what would happen,
[2:41pm] YesCT: and it made the feature, put the right info in the uc_og_subscribe table
[2:42pm] YesCT: but put the wrong info in the uc_product_features table. the pfid , fid and description were ok. but the nid was set to 0.
[2:43pm] YesCT: I went in and edited the row in the database to have the right nid in uc_product_features and then it looks like it worked...
[2:43pm] YesCT: so I'm hoping that not setting (or putting the wrong nid in uc_product_features) will help me figure out what I need to do to get it to actually work (without editing core!)

     if (is_array($elements['#value'])) {
          $value = $elements['#type'] == 'checkboxes' ? array_keys(array_filter($elements['#value'])) : $elements['#value'];
          foreach ($value as $v) {
            if (!isset($options[$v])) {
              form_error($elements, $t('An illegal choice (option not set) has been detected. Please contact the site administrator.'));
              watchdog('form', 'Illegal choice %choice (option not set) in !name element.', array('%choice' => $v, '!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR);
            }
          }
        }
/*        elseif (!isset($options[$elements['#value']])) {          form_error($elements, $t('An illegal choice (option element value) has been detected. Please contact the site administrator.'));          watchdog('form', 'Illegal choice %choice (option element value) in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR);
        }
*/
      }
YesCT’s picture

I added a dsm to the uc_og_subscribe_feature_form_submit function

and when I execute the php code,

<?php
module_load_include
('inc', 'uc_og_subscribe', 'uc_og_subscribe.ca.inc');
$form_state = array();
$form_state['values']['uc_group_gid'] = "248";
$form_state['values']['uc_group_model'] = "1001-par1248";
$form_state['values']['nid']="19";
$productnid=19;
$myemptyfeature=array();
drupal_execute('uc_og_subscribe_feature_form', $form_state, $productnid, $myemptyfeature);
?>

I get

#
... (Array, 4 elements)

    *
      values (Array, 9 elements)
          o
            nid (NULL)
          o
            pfid (NULL)
          o
            uc_group_model (String, 12 characters ) 1001-par1248
          o
            uc_group_gid (String, 3 characters ) 248
          o
            uc_group_description (String, 0 characters )
          o
            uc_group_active (Integer) 1
          o
            uc_group_admin (Integer) 0
          o
            submit (String, 12 characters ) Save feature
          o
            form_id (String, 28 characters ) uc_og_subscribe_feature_form | (Callback) uc_og_subscribe_feature_form();
    *
      submitted (Boolean) TRUE
    *
      process_input (Boolean) TRUE
    *
      redirect (NULL)

when I use the gui to submit the form I get:

...  (Array, 6 elements)

    *
      storage (NULL)
    *
      submitted (Boolean) TRUE
    *
      values (Array, 12 elements)
          o
            nid (String, 2 characters ) 19
          o
            pfid (NULL)
          o
            uc_group_model (String, 12 characters ) 1001-par1248
          o
            uc_group_gid (String, 3 characters ) 248
          o
            uc_group_description (String, 0 characters )
          o
            uc_group_active (Integer) 1
          o
            uc_group_admin (Integer) 0
          o
            op (String, 12 characters ) Save feature
          o
            submit (String, 12 characters ) Save feature
          o
            form_build_id (String, 37 characters ) form-93bfd42179b5fda2f4b8e02f1af1602e
          o
            form_token (String, 32 characters ) ce7f82c695ebf5b195a6145d3bb6c4b1
          o
            form_id (String, 28 characters ) uc_og_subscribe_feature_form | (Callback) uc_og_subscribe_feature_form();
    *

so I can see the nid is not getting set ... or maybe it is getting set, and then unset?

YesCT’s picture

I tried changing the nid form element to be hidden and a default value instead of value:

<?php
  $form
['nid'] = array(
     
'#type' => 'hidden',
     
'#default_value' => $node->nid,
  );
?>

which got me this reported by dsm

...  (Array, 4 elements)

    *
      values (Array, 9 elements)
          o
            nid (String, 2 characters ) 19
          o
            pfid (NULL)
          o
            uc_group_model (String, 12 characters ) 1001-par1248
          o
            uc_group_gid (String, 3 characters ) 248
          o
            uc_group_description (String, 0 characters )
          o
            uc_group_active (Integer) 1
          o
            uc_group_admin (Integer) 0
          o
            submit (String, 12 characters ) Save feature
          o
            form_id (String, 28 characters ) uc_og_subscribe_feature_form | (Callback) uc_og_subscribe_feature_form();
    *

And I like that the nid is now being set... but the uc_product_features row still has the nid as 0! (this is with the else if commented out still to get it to actually go through)

Still dont know what I'm doing wrong, but thought it might be worth condidering changing the value statements to default_value statements

YesCT’s picture

I added a dsm to the _form_validate in includes/form.inc and there were 17 calls to dsm... from the form inc.

new idea... looking at a dsm from inside the else if

looks like maybe the list of options is not being build correctly. I think it should be all the options that could be valid?

<?php
       
elseif (!isset($options[$elements['#value']])) {
dsm($elements);
dsm($options);
/*          form_error($elements, $t('An illegal choice (option element value) has
 been detected. Please contact the site administrator.'));          watchdog('form', 'Illegal choice %choice (option element value) in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR);
*/
       
}
?>
#

    *
      ... (Array, 24 elements)
          o
            #type (String, 6 characters ) select
          o
            #title (String, 3 characters ) SKU
          o
            #default_value (NULL)
          o
            #description (String, 80 characters ) This is the SKU that will need to be purchased ...
                +
                  This is the SKU that will need to be purchased to obtain the group subscription.
          o
LOOK ->            #options (Array, 1 element)
                +
                  (String, 7 characters ) - Any -
          o
            #post (Array, 3 elements)
                +
                  uc_group_gid (String, 3 characters ) 252
                +
                  uc_group_model (String, 13 characters ) 1001-cgrat252
                +
                  nid (Integer) 19
          o
            #programmed (Boolean) TRUE
          o
            #tree (Boolean) FALSE
          o
            #parents (Array, 1 element)
                +
                  0 (String, 14 characters ) uc_group_model
          o
            #array_parents (Array, 1 element)
                +
                  0 (String, 14 characters ) uc_group_model
          o
            #weight (Float) 0.002
          o
            #processed (Boolean) TRUE
          o
            #attributes (Array, 0 elements)
          o
            #required (Boolean) FALSE
          o
            #input (Boolean) TRUE
          o
            #size (Integer) 0
          o
            #multiple (Boolean) FALSE
          o
            #process (Array, 1 element)
                +
                  0 (String, 16 characters ) form_expand_ahah | (Callback) form_expand_ahah();
          o
            #name (String, 14 characters ) uc_group_model
          o
            #id (String, 19 characters ) edit-uc-group-model
          o
            #value (String, 13 characters ) 1001-cgrat252
          o
            #needs_validation (Boolean) TRUE
          o
            #defaults_loaded (Boolean) TRUE
          o
            #sorted (Boolean) TRUE
    *
      Krumo version 0.2a
      | http://krumo.sourceforge.net
      Called from /.../includes/form.inc, line 711 

LOOK -> this is the dsm($options); and look: this cant be right...
#

    *
      ... (Array, 1 element)
          o
            (Integer) 1
    *
      Krumo version 0.2a
      | http://krumo.sourceforge.net
      Called from /.../includes/form.inc, line 712 

#

    *
      ... (Array, 4 elements)
          o
            values (Array, 9 elements)
                +
                  nid (Integer) 19
                +
                  pfid (NULL)
                +
                  uc_group_model (String, 13 characters ) 1001-cgrat252
                +
                  uc_group_gid (String, 3 characters ) 252
                +
                  uc_group_description (String, 0 characters )
                +
                  uc_group_active (Integer) 1
                +
                  uc_group_admin (Integer) 0
                +
                  submit (String, 12 characters ) Save feature
                +
                  form_id (String, 28 characters ) uc_og_subscribe_feature_form | (Callback) uc_og_subscribe_feature_form();
          o
            submitted (Boolean) TRUE
          o
            process_input (Boolean) TRUE
          o
            redirect (NULL)
    *
      Krumo version 0.2a
      | http://krumo.sourceforge.net
      Called from /.../sites/all/modules/uc_og_subscribe/uc_og_subscribe.module, line 128

YesCT’s picture

line 52 of uc_og_subscribe.module

... around line 84

<?php
 
  $form
['uc_group_model'] = array(
   
'#type' => 'select',
   
'#title' => t('SKU'),
   
'#default_value' => $default_model,
   
'#description' => t('This is the SKU that will need to be purchased to obtain the group subscription.'),
   
'#options' => $models,
  );
?>

I'm thinking when I call the drupal execute, the $node is not loaded, and so the $modules is not getting filled out...

ah, when I call it:
drupal_execute('uc_og_subscribe_feature_form', $form_state, $productnid, $myemptyfeature);

maybe the 3rd arg is not a nid, but the node structure! and it's not my "node" which is the group I'm creating, it's the product node structure

yep. OK, so I dont have the form.inc error anymore!

which now gives me this code i'm trying in my rule to automate all the stuff I need to do on creation of a new group. the bit to add the person paying for the product to the og is the last few lines.

Added the group name <? echo $node->field_groupname[0]['value'] ?> for nid <? echo $node->nid ?> to the list of groups in the join product drop down (attribute 2) option list.  <?
$myaid->aid=2;
$myproductid=19; 
$myoptionname = $node->field_groupname[0]['value'];
$result = db_query("SELECT myoptions.oid FROM {uc_attribute_options} myoptions WHERE myoptions.name = '%s' AND myoptions.aid = %d", $myoptionname, $myaid->aid);
if ($row = db_fetch_object($result))
{
  $myfirstmatchingoid=$row->oid;
  echo "Option id matching that name is: ";
  echo $row->oid;
  echo ". ";
  $productresult = db_query("SELECT myprodoptions.oid FROM {uc_product_options} myprodoptions WHERE myprodoptions.nid = %d AND myprodoptions.oid = %d", $myproductid, $row->oid);
  if (db_fetch_object($productresult)) {
    echo "The option was already enabled. Contact developer. ";
  } else {
    db_query("INSERT INTO {uc_product_options} (nid, oid) VALUES (%d, %d)", $myproductid, $row->oid);
    echo "Enabled option $row->oid on product (node) $myproductid";
    echo ". ";
  }
  if ($row = db_fetch_object($result))
  {
    echo "Oops and there were other options matching that name: ";
    echo $row->oid;
    while ($row = db_fetch_object($result))
    {
      echo " ";
      echo $row->oid;
    }
    echo ". ";
    echo "Having more than one option with the same name is going to cause problems. Contact a developer and delete the group you just made. Create a new group with a slightly different name.";
  }
  $node->field_storeoptionid[0]['value']=$row->oid;
  node_save($node);
}
else {
echo "no options found with that name. something is really wrong. contact developer.";
}
//now set the adjustments skus so later the feature of automatically joining can be done
$myadjleveloidarray = array ( 1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', 7 => '7', 8 => '8', 9 => '9', 10 => '10',);
$myadjoid = $myfirstmatchingoid;
foreach ($myadjleveloidarray as $aleveloid)
{
  $myadjarray = array(
    1 => $aleveloid,
    2 => $myadjoid,
  );
  $mycomb='';
  $mycomb=serialize($myadjarray);
  $mymodel="1001-" . $node->field_groupname_codeunique[0]['value'];
  db_query("INSERT INTO {uc_product_adjustments} (nid, combination, model) VALUES (%d, '%s', '%s')", $myproductid, $mycomb, $mymodel);
}
echo " Made the adjustments to the sku. ";

module_load_include('inc', 'uc_og_subscribe', 'uc_og_subscribe.ca.inc');
$form_state = array();
$form_state['values']['uc_group_gid'] = $node->nid;
$form_state['values']['uc_group_model'] = $mymodel;
$form_state['values']['nid']="19";
$myemptyfeature=array();
$productnode=node_load($myproductid);
drupal_execute('uc_og_subscribe_feature_form', $form_state, $productnode, $myemptyfeature);

echo "added the feature to product $productnode->nid";

but the product nid is still missing from the uc_product_features table...

in uc_product.module line 1939:

<?php
    db_query
("INSERT INTO {uc_product_features} (nid, fid, description) VALUES (%d, '%s', '%s')",
            
$data['nid'], $data['fid'], $data['description']);
?>

it is using data['nid']

and in uc_og_subscribe.module line 151 it sets the nid part of data as:

<?php
   
'nid' => $subscription['values']['nid'],
?>

for more context:

<?php
  $data
= array(
   
'pfid' => $subscription['pfid'],
   
'nid' => $subscription['values']['nid'],
   
'fid' => 'group_subscribe',
   
'type' => t('group'),
   
'description' => $description,
  );
?>

so... I think it should be:

<?php
  $data
= array(
   
'pfid' => $subscription['pfid'],
   
'nid' => $subscription['nid'],
   
'fid' => 'group_subscribe',
   
'type' => t('group'),
   
'description' => $description,
  );
?>

it still did not work, (nid 0 in the features table)

so I looked and in uc_og_subscribe.module on line 131 there was no nid entry, so I added one:

<?php
  $subscription
= array(
   
'nid' => $form_state['values']['nid'],
   
'pfid' => $form_state['values']['pfid'],
   
'model' => $form_state['values']['uc_group_model'],
   
'gid' => $form_state['values']['uc_group_gid'],
   
'description' => $form_state['values']['uc_group_description'],
   
'is_admin' => $form_state['values']['uc_group_admin'],
   
'is_active' => $form_state['values']['uc_group_active'],
  );
?>

in uc_product.module on line 1925 it tries to get the nid if it is empty... and maybe that was working for the gui form? but now it works! (both the gui form, and my php code drupal_execute)

YesCT’s picture

Category:support» bug
Status:Active» Needs review
StatusFileSize
new774 bytes
new1.02 KB

here are two patches, one with the change of making the nid a hidden default_value (which I'm thinking now did not really effect things) and one without the hidden default_value change.

YesCT’s picture

just adding a note that this is related to a general quest to automate some stuff dealing with groups (and ubercart)
http://www.ubercart.org/forum/support/15829/practical_advice_setting_lar...