Guest kmdev Posted November 7, 2011 Share Posted November 7, 2011 I'm building an interface to a new gateway, which I have working, but I would like to be able to set default configuration values when the module is first enabled (or the first time the config screen is loaded) Any pointers as to how I could do this? Quote Link to comment Share on other sites More sharing options...
Dirty Butter Posted November 7, 2011 Share Posted November 7, 2011 I'm building an interface to a new gateway, which I have working, but I would like to be able to set default configuration values when the module is first enabled (or the first time the config screen is loaded) Any pointers as to how I could do this? Can you set the defaults in the Structure of the Mod table in the database?? Quote Link to comment Share on other sites More sharing options...
Guest kmdev Posted November 8, 2011 Share Posted November 8, 2011 I managed to get this working by using the following code for index.inc.php inside the module admin directory: <?php // Create the module $module = new Module( __FILE__, // Path $_GET['module'], // Local name 'admin/index.tpl', // Template true, // Zones true); // Fetch // Check if default settings have been done if (!$module->__get('initdone')) { // Set the default values for this module $defaults = Array ( 'status' => $module->__get('status'), 'default' => $module->__get('default'), 'param1' => 'value1', 'param2' => 'value2', // ... [etc, just keep adding params and values as needed].... 'initdone' => '1' ); $module->module_settings_save($defaults); // Perform a redirect back to this URL in order to reload the template with the default values. httpredir( '', // Empty destination means use current page '', // No anchor false); // Use Location: redirect, not meta refresh exit; } // Get the page content for display $page_content = $module->display(); What this does is check if a module setting called 'intidone' has been defined or not after the module has been loaded. If not, then the settings that you want to have default values for are added to an array and applied to the module. This includes the 'initdone' setting so as to show that the defaults have been loaded. It also includes the current values for 'status' and 'default' to ensure these are kept to the correct setting. After applying these default values, a redirect is issued to reload the page with the default values set. This redirect only happens once. You also need to include the following hidden variable in the form that is in the settings template file (index.tpl in this case) <input type="hidden" name="module[initdone]" value="1"> to ensure that the module keeps the information about the default data having been loaded, otherwise it could be loaded again over-writing any user changes. Don't know if this is the best/easiest approach to do this, but it has worked for me... Hope this is of use to someone else. Quote Link to comment Share on other sites More sharing options...
Dirty Butter Posted November 8, 2011 Share Posted November 8, 2011 I managed to get this working by using the following code for index.inc.php inside the module admin directory: <?php // Create the module $module = new Module( __FILE__, // Path $_GET['module'], // Local name 'admin/index.tpl', // Template true, // Zones true); // Fetch // Check if default settings have been done if (!$module->__get('initdone')) { // Set the default values for this module $defaults = Array ( 'status' => $module->__get('status'), 'default' => $module->__get('default'), 'param1' => 'value1', 'param2' => 'value2', // ... [etc, just keep adding params and values as needed].... 'initdone' => '1' ); $module->module_settings_save($defaults); // Perform a redirect back to this URL in order to reload the template with the default values. httpredir( '', // Empty destination means use current page '', // No anchor false); // Use Location: redirect, not meta refresh exit; } // Get the page content for display $page_content = $module->display(); What this does is check if a module setting called 'intidone' has been defined or not after the module has been loaded. If not, then the settings that you want to have default values for are added to an array and applied to the module. This includes the 'initdone' setting so as to show that the defaults have been loaded. It also includes the current values for 'status' and 'default' to ensure these are kept to the correct setting. After applying these default values, a redirect is issued to reload the page with the default values set. This redirect only happens once. You also need to include the following hidden variable in the form that is in the settings template file (index.tpl in this case) <input type="hidden" name="module[initdone]" value="1"> to ensure that the module keeps the information about the default data having been loaded, otherwise it could be loaded again over-writing any user changes. Don't know if this is the best/easiest approach to do this, but it has worked for me... Hope this is of use to someone else. I'm glad you were able to get it to work. I don't know enough php to do anything like that. Since you seem so knowledgeable, maybe you can help me. We tend to get customers who are not used to using online carts. They make all kinds of mistakes, one of which is to NOT choose their own state from the drop down list as they add their address. I keep getting incorrect addresses set to the correct city and ZIP, but supposedly in Alabama (because that shows by default in the list). Since we happen to be in Alabama, they end up paying Sales Tax. I added to the geo-zone table a new row for *CHOOSE STATE and would like to have THAT show in the box, rather than Alabama. I thought I could put that id number as a default defined field in the Structure and get it to work that way, but I get a SQL error message and it won't accept it as defined. Can you suggest how to write the Structure to accomplish this or another way to solve this little problem? Quote Link to comment Share on other sites More sharing options...
Guest kmdev Posted November 8, 2011 Share Posted November 8, 2011 I'm glad you were able to get it to work. I don't know enough php to do anything like that. Since you seem so knowledgeable, maybe you can help me. We tend to get customers who are not used to using online carts. They make all kinds of mistakes, one of which is to NOT choose their own state from the drop down list as they add their address. I keep getting incorrect addresses set to the correct city and ZIP, but supposedly in Alabama (because that shows by default in the list). Since we happen to be in Alabama, they end up paying Sales Tax. I added to the geo-zone table a new row for *CHOOSE STATE and would like to have THAT show in the box, rather than Alabama. I thought I could put that id number as a default defined field in the Structure and get it to work that way, but I get a SQL error message and it won't accept it as defined. Can you suggest how to write the Structure to accomplish this or another way to solve this little problem? I'm far from knowledgeable on these things!! I've only been using PHP for about two weeks, and CubeCart for a bit less than that. I've had a quick look round the code to see what I could find that might help with your Alabama problem... I did managed to get something working on my system that does force the user to select a state, hopefully you can either use it as is or at least use it as a basis for a solution. I've put the details on what changes I made into a seperate topic with a title that may help others find it as well - Forcing user to select a state Quote Link to comment Share on other sites More sharing options...
Dirty Butter Posted November 8, 2011 Share Posted November 8, 2011 I'm glad you were able to get it to work. I don't know enough php to do anything like that. Since you seem so knowledgeable, maybe you can help me. We tend to get customers who are not used to using online carts. They make all kinds of mistakes, one of which is to NOT choose their own state from the drop down list as they add their address. I keep getting incorrect addresses set to the correct city and ZIP, but supposedly in Alabama (because that shows by default in the list). Since we happen to be in Alabama, they end up paying Sales Tax. I added to the geo-zone table a new row for *CHOOSE STATE and would like to have THAT show in the box, rather than Alabama. I thought I could put that id number as a default defined field in the Structure and get it to work that way, but I get a SQL error message and it won't accept it as defined. Can you suggest how to write the Structure to accomplish this or another way to solve this little problem? I'm far from knowledgeable on these things!! I've only been using PHP for about two weeks, and CubeCart for a bit less than that. I've had a quick look round the code to see what I could find that might help with your Alabama problem... I did managed to get something working on my system that does force the user to select a state, hopefully you can either use it as is or at least use it as a basis for a solution. I've put the details on what changes I made into a seperate topic with a title that may help others find it as well - Forcing user to select a state Thanks for sharing! I've commented on your explanation of how you made it work for you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.