how to create one config by template with v1.7

Discussions and requests related to new CMSimple features, plugins, templates etc. and how to develop.
Please don't ask for support at this forums!
Post Reply
ludovicam
Posts: 6
Joined: Tue May 29, 2018 8:23 pm
Contact:

how to create one config by template with v1.7

Post by ludovicam » Tue May 29, 2018 8:30 pm

Hi all,
I had created a plugin that apparently did not interest anyone.
But it seems that a few rare users remain interested by Glossaire_XH

Could I ask a question to the developers of version 1.7 of CMSimple_XH ?

Could you tell me how to properly use config.php of my plugin to allow one configuration by template.

Before version 1.7 (1.6.x):
$plugin_cf was an array variable.
The config.php file of my plugin contained the values corresponding to the default configuration.
I gave the user the possibility to save a configuration for each template used.
A configuration file dedicated to this template was then created (there was potentially one for each template).
Then, for a template used, if the dedicated configuration file existed, I realized an include and thus updated the $config_cf variables.

Since version 1.7:
$plugin_cf has become an object.
I can not do what I did before.

Question:
You who created this object $plugin_cf,
how can a plugin use it to allow configuration by template.
It is necessary that when a template is used -and that the configuration has already been created for this template-, its dedicated configuration can be loaded as I did before with an include.

Currently, I can not update the variables anymore. Whatever I do, these remain as they are in config.php.
Even if an include of a different configuration is loaded, it does not change these variables.

I hope I have been clear enough. I speak only a little English and I have no notion of German.


Thanks for reading me,
hoping to get an answer and that I can understand it,

best regards,
Ludovic

[Glossaire_XH]

Tata
Posts: 2691
Joined: Tue May 20, 2008 5:34 am
Location: Slovakia
Contact:

Re: how to create one config by template with v1.7

Post by Tata » Wed May 30, 2018 5:43 am

Maybe this scenario can work:
1. let the plugin reading what is the actual template
2. let the plugin create new copy of glossaire/config/config.php config for the active template renamed to e.g.: config_<active_template_name>.php
3. let the glossaire/index.php to test the active template and load the respective config_<active_template_name>.php
CMSimple.sk
It's no shame to ask for an answer if all efforts failed.
But it's awful to ask without any effort to find the answer yourself.

ludovicam
Posts: 6
Joined: Tue May 29, 2018 8:23 pm
Contact:

Re: how to create one config by template with v1.7

Post by ludovicam » Wed May 30, 2018 6:32 am

that's exactly what I did.
But it's always the configuration from config.php that takes precedence.

I have already done the following test:
In index.php, test the variables from $plugin_cf just after loading the configuration for the template used.
Well, these variables still retain the values from config.php.

Apparently, it is not expected that the object $plugin_cf allows the existence of several possible configurations.

Unless there is a trick to "force" the refresh of $plugin_cf values ...
But I do not know object-oriented programming ...
Thank you for your answer.

Holger
Site Admin
Posts: 2813
Joined: Mon May 19, 2008 7:10 pm
Location: Hessen, Germany
Contact:

Re: how to create one config by template with v1.7

Post by Holger » Wed May 30, 2018 8:21 am

Hi Ludovic,

JFTR: $plugin_cf is treated as read-only since XH 1.6 if I'm right.

Anyway, here's my workaround to load additional settings based on config.php :

1st step:
create a copy of your plugins configuration array at the very beginning of your index.php

Code: Select all

$pcf = $plugin_cf['glossaire'];
From now use only the copy of the array in your code.


2nd step:
create a little function to handle a copy of config.php in the template folder and to load a changed configuration.
We'll replace the original $plugin_cf... with our copy and include the array at the end.
(Just a draft, untested, only to show the idea behind):

Code: Select all

function readConfig() {
    global $pth;
    $config = $pth['folder']['template'] . 'config.php';
    if (file_exists($config) {
        $t = file_get_contents($config);
        if (strpos('$plugin_cf['glossaire']', $t)) {
            str_replace('$plugin_cf['glossaire']', '$pcf', $t);
            file_put_contents($config, $t);
        }
        include $config;   
    }
}
Now always call readConfig() instead of include config.php.

ludovicam
Posts: 6
Joined: Tue May 29, 2018 8:23 pm
Contact:

Re: how to create one config by template with v1.7

Post by ludovicam » Wed May 30, 2018 8:42 am

Hi Holger,
and thank you very much for that detailed answer.
I am going to try it as soon as possible ;-)

cmb
Posts: 12601
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

Re: how to create one config by template with v1.7

Post by cmb » Wed May 30, 2018 11:31 am

First, let me quickly explain why this has changed. The main reason was to avoid loading configuration files which are not required for the request (see viewtopic.php?f=29&t=12012#p59038). More of a side effect, but still a Good Thing in my opinon, has been to abstract over the concrete format of the configuration files.
ludovicam wrote:
Tue May 29, 2018 8:30 pm
Could you tell me how to properly use config.php of my plugin to allow one configuration by template.
A slight variation of Holger's suggestion:

Code: Select all

<?php

function glossaire_readConfig() {
    global $pth;
    $config = $pth['folder']['template'] . 'config.php';
    if (file_exists($config)) {
        return XH_includeVar($config, 'plugin_cf')['glossaire'];
    }
}

$glossaire_config = glossaire_readConfig();

After this code has been executed, $glossaire_config has the same content as $plugin_cf['glossaire']. The main benefit of using XH_includeVar() is that you don't have to assume a particular config file format.
Christoph M. Becker –Plugins for CMSimple_XH, but not for CMSimple 4+

Holger
Site Admin
Posts: 2813
Joined: Mon May 19, 2008 7:10 pm
Location: Hessen, Germany
Contact:

Re: how to create one config by template with v1.7

Post by Holger » Wed May 30, 2018 11:42 am

cmb wrote:
Wed May 30, 2018 11:31 am
A slight variation of Holger's suggestion:
Ah, so easy. Nice! Thank you. Will change this in my plugins too.

ludovicam
Posts: 6
Joined: Tue May 29, 2018 8:23 pm
Contact:

Re: how to create one config by template with v1.7

Post by ludovicam » Wed May 30, 2018 8:55 pm

thanks cmb, it works perfectly. Indeed, it's very simple.

I did not have to modify much and I should be able to offer a compatible version with CMSimple_XH 1.7 shortly.
(It remain a little problem with newsboxes...)

thanks again holger and cmb.

ludovicam
Posts: 6
Joined: Tue May 29, 2018 8:23 pm
Contact:

Re: how to create one config by template with v1.7

Post by ludovicam » Sun Jun 03, 2018 9:21 pm

New Glossaire_XH version 2.0 is now available and compatible with CMSimple_XH V1.7.x

https://www.f5swn.fr/en/?Plugins/Glossaire

As it seems that a few rare users appreciate it, it is back ;-)

Post Reply