Jump to content

Plugin Module admin - how to initialize an array in module configuration


ploughguy

Recommended Posts

I am building an own-use module that offers a choice from a largish table of options that may change over time.

It makes sense to:

  • Initialise this table during module installation
  • Offer a maintenance dialog to add new options down the track

The benefit of initialisation is that 

  • all sandpit, development, test and production systems will have the same initial set of data;
  • I won't have to initialise it manually;
  • Eliminates opportunities for error when setting up a new system

I studiously cribbed the example provided by SWFS Product Dimensions  - viz:

if(!$module_config = $GLOBALS['config']->get('sfws_product_dimensions')) {
    $module_config = array (
        'dimensions_unit' => 'cm',
        'display_length' => true,
        'display_width' => true,
        'display_height' => true,
        'display_package_count' => true,
        'product_page_tab' => true,
        'product_page_specifications' => true,
        'product_page_other' => false,
        'status' => true
    );
    $GLOBALS['config']->set('sfws_product_dimensions','', $module_config);
    $record = array (
        'module'    => 'plugins',
        'folder'    => 'SFWS_Product_Dimensions',
        'status'    => '1',
    );
    $GLOBALS['db']->insert('CubeCart_modules', $record);
    $GLOBALS['main']->setACPNotify("The 'Product Dimensions CC6 Plugin' configuration has been installed.");
    $redirect_status = 1;
}

To initialize the table, I cooked up this monster:

if(!$module_config = $GLOBALS['config']->get('testModule')) {
    $module_config = array (
        'products' => array (
          array ( 
               'id'              => 1,
               'active'          => true,
               'colour'          => 'Red',
               'flavour'         => 'Chilli, avocado and broccoli',
               'priority'        => 1
          ),
           array ( 
               'id'              => 2,
               'active'          => true,
               'colour'          => 'Grey',
               'flavour'         => 'Pineapple, turnip and liver',
               'priority'        => 2
          )
        )
    );
	$GLOBALS['config']->set('testModule','', $module_config);
}

if ($redirect_status == '1') {
	httpredir(currentPage());
}

$module	= new Module(__FILE__, $_GET['module'], 'admin/index.php', true);
$page_content = $module->display();

The module class should deliver an array named MODULE_DINNER to the Smarty template, but it never arrives.

If I remove the bang from the if statement to get if ( $module_config = $GLOBALS['config']->get('testModule') ) then the table is displayed when I enter the form (for example by clicking the module name in the breadcrumb trail at the top of the page) but it is not  when I click Save.  I believe the template is working perfectly because it displays correctly when MODULE_DINNER is available, so the problem is in the logic of the PHP module.  

Refining the code above to:

if($module_config = $GLOBALS['config']->get('testModule') && !isset($module_config['products])) {
    $module_config['products'] = array (
          array ( 
               'id'              => 1,

provides an amusing variation on the existing disfunction, but makes no real difference.

The theory of what is happening above is that the module config is initialised before the module class is instantiated. Therefore the module reads the updated config, builds $MODULE, which contains the scalar config values, and creates MODULE_<KEY> for all module config entries that are arrays.

The effect that I am seeing is:

  • if the config does not exist already, it is not saved by $GLOBALS['config']->set(. 
  • The Save action 

Clearly I am doing something wrong, and given an infinite amount of time, I may well be able to work it out, but I have spent more hours than I can spare on it so far, so expedience over-rules pride and I am <sob!> asking for help, hoping that someone has managed to get this to work already.

My heart is in your hands...  There must be a pattern for this already, right?  I can find lots of examples that manage scalars, but I have not found a module that initializes a table that has visible code.

Thank you for your help.

Russ

Link to comment
Share on other sites

  • 3 months later...

just stumbled across this. I would actually suggest putting your data into their own DB tables, it's easier to manage and scale. the "config" is ideal for basic settings for a module, but not really intended for large scale data storage. Build a table for each data type, index it correctly and gather/update/insert your data as needed.

In your example, I didn't see any code assigning MODULE_DINNER to smarty. Search for ->assign in random files within the classes directory to see how that works

For DB usage (once your tables exist) You'll want to use the $GLOBALS['db']->insert(), $GLOBALS['db']->select(), $GLOBALS['db']->update() and $GLOBALS['db']->query() functions. 

select and query output arrays which you can send directly to smarty and loop using a foreach 

 

Link to comment
Share on other sites

  • 5 months later...

Just stumbled across you response - thank you for taking the time, Noodles.

The largish table of options is about 20 rows - probably a couple of hundred bytes in the global area.   I appreciate your comments about putting the data into a database table, and that may well be what I finally choose to do, if only because I can maintain the data with PhpMyAdmin.

I was exploring the possibilities with the global area and my main interest in the posting above was to determine why it does not work.  It may be that the code was never finished and this aspect of it actually does not work, but it is more likely that I missed a nuance (or an explicit and obvious instruction somewhere, which is quite likely.)

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...