Skip to content

Commit 81331ec

Browse files
committed
Documentation for new module support infrastructure
1 parent 6f72d44 commit 81331ec

File tree

28 files changed

+1053
-57
lines changed

28 files changed

+1053
-57
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Modules Infrastructure
3+
description: New modules infrastructure
4+
weight: 100
5+
layout: docs
6+
---
7+
8+
{{% alert title="Warning" color="warning" %}}
9+
The documentation in this section is Work in progress and relates to features that might not be available yet.
10+
{{% /alert %}}
11+
12+
In order to make it easier and quicker to write modules(Payment/Shipping/Order Totals) a new infrastructure for these modules has been created.
13+
14+
This new infrastructure will reduce the amount of boilerplate code required, minimize copy/paste coding and adds helpers for common coding requirements.
15+
16+
{{% alert title="Note" color="primary" %}}
17+
Previous Payment/Shipping/Order total modules will continue to work without modification.
18+
{{% /alert %}}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: Base Module Support
3+
description:
4+
weight: 10
5+
layout: docs
6+
---
7+
8+
{{% alert title="Warning" color="warning" %}}
9+
The documentation in this section is Work in progress and relates to features that might not be available yet.
10+
{{% /alert %}}
11+
12+
# Introduction
13+
14+
The base module support classes contain common code that is used across all module types e.g Payments, Shipping and Order Total modules.
15+
16+
It also provides some common helper methods that you can use in you modules.
17+
18+
## Accessing Define Values
19+
20+
All module types store information about themselves in the database `configuration` table.
21+
22+
The `keys` for this table will look something like
23+
24+
- MODULE_PAYMENT_COD_STATUS
25+
- MODULE_SHIPPING_FLAT_COST
26+
- MODULE_ORDERTOTAL_TOTAL_SORT_ORDER
27+
28+
The structure of these defines is
29+
30+
MODULE_[module type]\_[module name]\_[define name]
31+
32+
where module type is one of `PAYMENT` or `SHIPPING` or `ORDERTOTAL`
33+
34+
The module name is taken from the $code class variable of the module, and the define name describes the purpose of the define.
35+
36+
In previous version of Zen Cart,these configuration values were accessed directly using the key that was stored in the database, however using
37+
the new module support classes we access them through a helper method.
38+
39+
e.g In legacy code we might have done
40+
41+
`$this->sort_order = defined('MODULE_PAYMENT_COD_SORT_ORDER') ? MODULE_PAYMENT_COD_SORT_ORDER : null;`
42+
43+
With the new infratructure we can simply do
44+
45+
`$this->sort_order = $this->getDefine('SORT_ORDER);`
46+
47+
The `getDefine` method is context aware, so knows which type of module you are using(PAYMENT/SHIPPING/ORDERTOTAL) and the current define name (e.g. COD) for
48+
the module you are using.
49+
50+
Furthermore, you can also set a value that will be returned from the method if the configuration value does not exist.
51+
52+
In the example above, the method will return `null` if the `MODULE_PAYMENT_COD_SORT_ORDER` does not exist.
53+
54+
However you could also do `$this->sort_order = $this->getDefine('SORT_ORDER, 0);`
55+
56+
and the method will return `0`, if the configuration value does not exist.
57+
58+
There are also a number of helper methods to get some of the most common module configuration keys.
59+
60+
### getTitle
61+
62+
This gets the title for the Module.
63+
64+
It looks for defines with suffixes in an array of ['TITLE', 'TEXT_TITLE', 'CATALOG_TITLE']
65+
66+
If your module uses a different define for the title then you should override the getTitle method.
67+
68+
### getAdminTitle
69+
70+
This returns a title to display on the Admin interface. In general this would be the same as the getTitle as above, but sometimes you may want it to be different.
71+
It expects to be passed a default title, which would normally be what the getTitle function returns, ans will use this if it does not find a define related to the admin title.
72+
73+
This function looks for defines with a suffix in an array ['TITLE_ADMIN', 'TEXT_TITLE_ADMIN', 'ADMIN_TITLE']
74+
75+
If your admin title is defined through other means, them you will need to override this method in your module.
76+
77+
### getDescription
78+
79+
returns a module define with the suffix `DESCRIPTION`
80+
81+
### getSortOrder
82+
83+
returns a module define with the suffix `SORT_ORDER`
84+
85+
### getZone
86+
87+
returns a module define with the suffix `ZONE`
88+
89+
### getDebugMode
90+
91+
returns a module define with the suffix `DEBUG_MODE`
92+
93+
### isEnabled
94+
95+
This function checks to see if the module is enabled.
96+
97+
it first checks to see if the module has any missing configuraitions and then check the module define suffix `STATUS`
98+
99+
### moduleAutoloadSupportClasses
100+
101+
This function is only called if it exists.
102+
103+
It allows a module to add autoload classes using the psr4autoload Aura autoload.
104+
105+
e.g.
106+
107+
``` php
108+
protected function moduleAutoloadSupportClasses(Loader $psr4Autoloader): Loader
109+
{
110+
$psr4Autoloader->addPrefix('Stripe', DIR_FS_CATALOG . DIR_WS_MODULES . 'payment/stripe_pay/stripe-php-13.15.0/lib/');
111+
$psr4Autoloader->addPrefix('Monolog', DIR_FS_CATALOG . DIR_WS_MODULES . 'payment/stripe_pay/monolog/src/Monolog/');
112+
$psr4Autoloader->addPrefix('Zencart\Logger', DIR_FS_CATALOG . DIR_WS_MODULES . 'payment/stripe_pay/Logger/');
113+
return $psr4Autoloader;
114+
}
115+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: Class Variables
3+
description:
4+
weight: 140
5+
layout: docs
6+
---
7+
8+
{{% alert title="Warning" color="warning" %}}
9+
The documentation in this section is Work in progress and relates to features that might not be available yet.
10+
{{% /alert %}}
11+
12+
The base module provides the following class variables for all other module types.
13+
14+
## public int $_check
15+
16+
## public string $description
17+
18+
## public bool $enabled
19+
20+
## public ?int $sort_order
21+
22+
## public string $code = ''
23+
24+
## protected string $defineName = ''
25+
26+
## protected string $version = ''
27+
28+
## public string $title = ''
29+
30+
## protected array $configurationKeys
31+
32+
## protected int $zone
33+
34+
## protected array $configureErrors = []
35+
36+
## protected Logger $logger
37+
38+
## protected $storeLanguageSuffixes = ['TITLE', 'TEXT_TITLE', 'CATALOG_TITLE']
39+
40+
## protected $adminLanguageSuffixes = ['TITLE_ADMIN', 'TEXT_TITLE_ADMIN', 'ADMIN_TITLE']
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
title: Configuration Settings
3+
description:
4+
weight: 80
5+
layout: docs
6+
---
7+
8+
{{% alert title="Warning" color="warning" %}}
9+
The documentation in this section is Work in progress and relates to features that might not be available yet.
10+
{{% /alert %}}
11+
12+
# Introduction
13+
14+
In Zen Cart modules written before this module infrastructure code, configuration settings used SQL statements for those settings.
15+
16+
This was usually done in a module method called `install` and would look something like.
17+
18+
``` php
19+
function install() {
20+
global $db, $messageStack;
21+
if (defined('MODULE_PAYMENT_FREECHARGER_STATUS')) {
22+
$messageStack->add_session('FreeCharger module already installed.', 'error');
23+
zen_redirect(zen_href_link(FILENAME_MODULES, 'set=payment&module=freecharger', 'SSL'));
24+
return 'failed';
25+
}
26+
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Free Charge Module', 'MODULE_PAYMENT_FREECHARGER_STATUS', 'True', 'Do you want to accept Free Charge payments?', '6', '1', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now());");
27+
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort order of display.', 'MODULE_PAYMENT_FREECHARGER_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
28+
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Payment Zone', 'MODULE_PAYMENT_FREECHARGER_ZONE', '0', 'If a zone is selected, only enable this payment method for that zone.', '6', '2', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_classes(', now())");
29+
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('Set Order Status', 'MODULE_PAYMENT_FREECHARGER_ORDER_STATUS_ID', '0', 'Set the status of orders made with this payment module to this value', '6', '0', 'zen_cfg_pull_down_order_statuses(', 'zen_get_order_status_name', now())");
30+
}
31+
32+
```
33+
34+
With the new module infrastructure code this is simplified.
35+
36+
The base module classes define a default set of configurations settings for payment/shipping/order total classes, and 3rd party modules can add to those settings.
37+
38+
Rather than use SQL statements, we now use an array based method of defining the settings.
39+
40+
If a module needs to add extra settings, it should define a method called `addCustomConfigurationKeys`
41+
42+
An example, taken from a rewritten moneyorder payment module.
43+
44+
```php
45+
protected function addCustomConfigurationKeys(): array
46+
{
47+
$configKeys = [];
48+
$key = $this->buildDefine('PAYTO');
49+
$configKeys[$key] = [
50+
'configuration_value' => 'the Store Owner/Website Name',
51+
'configuration_title' => 'Make Payable to:',
52+
'configuration_description' => 'Who should payments be made payable to?',
53+
'configuration_group_id' => 6,
54+
'sort_order' => 1,
55+
];
56+
return $configKeys;
57+
}
58+
```
59+
60+
## Module Settings Keys
61+
62+
The following is a list of module settings keys and their usage as per the `configuration` database table.
63+
64+
### configuration_title
65+
66+
### configuration_key
67+
68+
### configuration_value
69+
70+
### configuration_description
71+
72+
### sort_order
73+
74+
### last_modified
75+
76+
### date_added
77+
78+
### use_function
79+
80+
### set_function
81+
82+
### val_function
83+
84+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: Configuration Errors
3+
description:
4+
weight: 100
5+
layout: docs
6+
---
7+
8+
{{% alert title="Warning" color="warning" %}}
9+
The documentation in this section is Work in progress and relates to features that might not be available yet.
10+
{{% /alert %}}
11+
12+
# Introduction
13+
14+
The new module support infrastructure allows you to define methods to capture errors in the configuration of a module
15+
and display these errors in the admin title for the module.
16+
17+
If the errors are considered `fatal` the system will also ensure that the module cannot be enabled until the errors are fixed.
18+
19+
Two methods are used which you can add to your module.
20+
21+
`checkNonFatalConfigureStatus` and `checkFatalConfigureStatus`
22+
23+
some examples.
24+
25+
The moneyorder payment module defines a `checkNonFatalConfigureStatus` method
26+
27+
``` php
28+
protected function checkNonFatalConfigureStatus(): void
29+
{
30+
if ($this->getDefine('PAYTO') == 'the Store Owner/Website Name' || $this->getDefine('PAYTO') == '') {
31+
$this->configureErrors[] = '(not configured - needs pay-to)';
32+
}
33+
}
34+
```
35+
36+
The Stripe Pay module defines a `checkFatalConfigureStatus` method
37+
38+
39+
```php
40+
protected function checkFatalConfigureStatus(): bool
41+
{
42+
$configureStatus = true;
43+
$toCheck = 'LIVE';
44+
if ($this->getDefine('MODE') == 'Test') {
45+
$toCheck = 'TEST';
46+
}
47+
if ($this->getDefine($toCheck . 'PUB_KEY') == '' || $this->getDefine($toCheck . '_SECRET_KEY') == '') {
48+
$this->configureErrors[] = sprintf('(not configured - needs %s publishable and secret key)', $toCheck);
49+
$configureStatus = false;
50+
}
51+
return $configureStatus;
52+
}
53+
```
54+
55+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: Language defines
3+
description:
4+
weight: 120
5+
layout: docs
6+
---
7+
8+
{{% alert title="Warning" color="warning" %}}
9+
The documentation in this section is Work in progress and relates to features that might not be available yet.
10+
{{% /alert %}}
11+
12+
# Introduction
13+
14+
Language define files for modules follow the same format as normal array based language defines.
15+
16+
They are stored in `includes/languages/{language}/modules/` and at the very least should have an english translation.
17+
18+
e.g.
19+
20+
`includes/languages/english/modules/payment/lang.cod.php`
21+
22+
`includes/languages/english/modules/shipping/lang.flat.php`
23+
24+
`includes/languages/english/modules/order_total/lang.ot_subtotal.php`
25+
26+
27+
Like module configuration keys, naming of module language defines are the same
28+
e.g.
29+
30+
`MODULE_[module type]\_[module name]\_[define name]`
31+
32+
There are some other rules.
33+
34+
language define names should be unique and not match any other module configuration defines.
35+
36+
At a minimum you should provide the following
37+
38+
`'TEXT_TITLE' => 'Stripe Checkout',`
39+
40+
`'TEXT_DESCRIPTION' => '<strong>Stripe Pay</strong>',`
41+
42+
43+
e.g
44+
45+
``` php
46+
$define = [
47+
'MODULE_PAYMENT_STRIPE_PAY_TEXT_TITLE' => 'Stripe Checkout',
48+
'MODULE_PAYMENT_STRIPE_PAY_TEXT_DESCRIPTION' => '<strong>Stripe Pay</strong>',
49+
];
50+
return $define;
51+
```
52+
53+
Note: also if you want to provide a different tiile in the admin interface you can add something like
54+
55+
`MODULE_PAYMENT_STRIPE_PAY_TEXT_TITLE_ADMIN' => 'Stripe Checkout(ADMIN)`
56+

0 commit comments

Comments
 (0)