Closed mahotilo closed 4 years ago
Sorry, the question was a little naive and lazy. I have spended some time looking into CustomSections.php and have understood the reason of such behaviour. But I still see no possibility to write current (edited, not initial) section parameters from section.php into _addondata, like in made in run_custom_scripts/script.php.
2 solutions for sharing config values…
A) simple: Write your values to Typesetter's $config, which is already available at any stage we deal with custom sections
// set the currency value
global $config;
$config['cs_currency'] = 'EUR';
// save $config
\gp\admin\Tools::SaveConfig(true);
// get the currency value
global $config;
$currency = $config['cs_currency'];
B) Use a shared config/file for both types
// load config
$shared_config = array(
// default value(s)
'currency' => 'EUR',
'some_other' => 'foo',
'and_another' => 'bar',
);
global $addonPathData;
$config_file = $addonPathData . '/shared_config.php';
if( file_exists($config_file) ){
include $config_file;
}
$currency = $shared_config['currency'],
// save config
global $addonPathData;
$config_file = $addonPathData . '/shared_config.php';
// $shared_config array must exist at this point
\gp\tool\Files::SaveData($config_file, 'shared_config', $shared_config);
For setting global config values from a section while editing, we would have to add some hook into the SaveSection() method in CustomSections.php. But is this wise? Any section editing instance could (over)write the global config values.
Thank you, Juergen Your answer is as always detailed. I tried variant B) but still have not what I want.
Let me show part of my code
$section = array();
$section['values'] = array_merge(array(
'currency' => 'EUR',
), $sectionCurrentValues );
$CartCurrency = $section['values']['currency'];
global $addonPathData;
$config_file = $addonPathData . '/shared_config.php';
$shared_config = array(
currency => 'USD',
);
$shared_config['currency'] = $CartCurrency;
\gp\tool\Files::SaveData($config_file, 'shared_config', $shared_config);
$section['content'] = '
<div class="my-icon-box">
<span class="my-cart-icon fa fa-3x '.$icon_class.'"
data-currency="'.$CartCurrency.'"></span>
</div>
';
In Section edit I set 'currency' to UAH So I have
In .my-icon-box data-currency I have UAH and see it in JS alert() But in shared_config.php I have EUR! Sections default.
<?php defined('is_running') or die('Not an entry point...'); $fileVersion = '5.1'; $fileModTime = '1504350044'; $file_stats = array ( 'created' => 1504350043, 'gpversion' => '5.1', 'modified' => 1504350044, 'username' => 'admin', );
$shared_config = array ( 'currency' => 'EUR', );
$meta_data = array ( );
I hope I could describe this puzzle.
But is this wise?
I don't like global var myself. It is like GOTO. But if you have a great need:)
Could you please zip and attach your cart section type folder, so I can check what to do best? Maybe we need to implement a save hook in order to get a clean solution.
Here both car and item section types _types.zip
P.S. Sorry for delay. I was offline
Wow, awesome work!
So if I understand it correctly, you want to set a global currency when editing the cart section? Are you also planning to re-calculate the prices in the item sections based on exchange rates or sth.? Sorry, I don't yet get it.
Thx. Yes, I want to let admin changes separator and currency code in items from one place. Exchange rate is in plans but not now.
Ok. I actually believe in better making the cart a Gadget (which could be used site/layout wide) instead of a content section – and the currency setting (besides other config values like exchange rates etc.) should go to an Admin page.
But I'll implement config saving for sections. Be back...
Ok. I actually believe making the cart a Gadget (which could be used site/layout wide) instead of a content section – and the currency setting (besides other config values like exchange rates etc.) should go to an Admin page.
Oh, it is old-fashioned. If you don't know, now we have powerful CustomSections:) And it can be used in layout too! Really it so cool. It give feeling of RAD or Visual programming. I have made for myself freemailform fork with gadget. It was hard. Now with CustomSections it is hard too:) but with much more fun.
But I'll implement config saving for sections. Be back... But I'll try to implement type+global config saving. Could come in handy for other things.
Thx, I'm glad that I can get your attention!
After fiddling a bit with a config saving mechanism, it turns out that it would be cumbersome and hardly understandable, because we would have to 'route' all config values through section editors - even those which are not supposed to be changed there. Current CS editor isn't made to carry more information than the actually needed one. So adding it this way would bloat the code and dilute the whole concept.
I'll try a different approach - sort of 'config snap in' for the Admin page. Makes much more sense there.
So will it be a part of TS $config?
Hi, Juergen!
Maybe simplest solution is to introduce something like EditCallback.php
?
This file must be included only ones after section is saved.
Now we do include $section_file
in logged in mode 3-times and 1-times in logged out mode. Therefore, we have a mess when trying to write $section['values']
to $addonPathData/config.php
in section.php
. Section writes to config.php
user defined values of $section['values']
when admin is logged in, and default values when logged out. Callback after editing allow us to write to $addonPathData/config.php
actual $section['values']
, not default.
So we obtain possibility to share data with another section types or addons.
What do you think about this?
P.S.
Just now I managed to solve my problem via POST from cart to items. In item/script.php I use AJAX replace
to setup posted currency and separator. It works for me, but it is not the best solution and maybe is worst one for perfomance. So I still hope to got better tool from you.
In any case, without your kindly help all this would be impossible
Sorry for the delay. I moved recently and was too busy to keep everything up. I still have to get back into the code.
Just an idea: Have you tried to use
$section['always_process_values'] = true;
in section.php? This might make your initial globals work (as they do when you're logged-in).
Glad to hear from you. I hope all changes was for the best.
$section['always_process_values'] = true;
Yes, I have tried to set it in "true" and "false" before write to you. Don't helps.
Now, I see that the interaction between the two types of sections is not a common task, so this does not have to be supported by CS.
As I said
Just now I managed to solve my problem via POST
This solution works for me reliable enough, so I close the issue.
Hi, Juergen
I use
global $CartCurrency
to share data between two section types (cart and item). All works fine, but when I am logged in.I have such code structure:
cart/section.php
In editor I have set Currency to UAH.
item/section.php
What I see in message box:
In admin mode
I am interesting, are that different ways to process section.php predefined or accidental?
Now I have choose another way to share data between sections, but using global vars is much easer.
Thx in advance