handler on setting value change

Hello everybody,



I've followed this tutorial to create an addon (and it works):

http://docs.cs-cart.com/4.3.x/addons/tutorials/advanced.html



(the tutorial shows how to use hooks to store category views and show them on the dashboard)



But now I'm trying to show the data on a specific page in the frontend, rather than on the index of the backend.

I don't seem to be able to find a tutorial or example of how to do this and the tutorial above isn't detailed enought to understand fully how cs-cart addons work.



For example it uses:

design/backend/templates/addons/advanced_addon/hooks/index/index.post.tpl (visual template)

and

app/addons/advanced_addon/controllers/backend/index.post.php (controller for getting database data)



I understand that filenames/foldernames are important for it to actually pickup the addon, so where to place the files when you want to use it on a specific page on the frontend.

I'm guessing you no longer work with hooks, but create a new template file, but then where do I put this and what would the controller be called.



Maybe there is a tutorial that explains this, but I haven't found it yet.

If somebody could explain it for me/write a turorial/point me to a tutorial, I'd be grateful.

hi,



let's say you need a new page available by URL - mysite.com/index.php?dispatch=mypage.view

note: in this example controller name is mypage and mode name is view (in summary - mypage.view)


  1. create file [color=#282828][font=arial, verdana, tahoma, sans-serif]app/addons/advanced_addon/controllers/frontend/mypage.php with content:[/font][/color]

```php


if (!defined('BOOTSTRAP')) { die('Access denied'); }

if ($mode == 'view') { // this is mode name, see above
// my code
}
```

2. create file [color=#282828][font=arial, verdana, tahoma, sans-serif]design/themes/THEME/templates/addons/advanced_addon/views/[b]mypage/view.tpl [/b]and add necessary content into the file[/font][/color]

[color=#282828][font=arial, verdana, tahoma, sans-serif]3*. if you need a custom layout, you may create a new custom tab in Design -> Layouts with dispatch = mypage.view[/font][/color]
[color=#282828][font=arial, verdana, tahoma, sans-serif]4*. if you use SEO module, you may also specify unique SEO name for the new dispatch[/font][/color]

[color=#282828][font=arial, verdana, tahoma, sans-serif]best regards,[/font][/color]
[color=#282828][font=arial, verdana, tahoma, sans-serif]WSA team[/font][/color]

Ok thanks for the explanation, I'm gonna try and see if I can get it to work!

great, keep us updated!



best regards,

WSA team

Ok I've tried a few things and I came to the following conclusions, please correct me if I'm wrong.


  1. The mypage.php is used to do work in php like creating variables etc. and can handle different 'modes' so for example can create some variables in the mode 'view' to use in a form and also read $_POST varialbles in another mode (ex. 'handling') to handle the submitted form (where the form action would then have to be '[color=#282828][font=arial, verdana, tahoma, sans-serif]index.php?dispatch=[/font][/color]mypage.handling'.


  2. The view.tpl is loaded in the 'main content' area of the 'default' layout when not creating a custom layout.

    This template is used to show the actual html content like a form.


  3. when creating a custom layout with dispatch = mypage.view, you have to create a block with 'main content' where the view.tpl is then loaded.



    The only thing I have yet to find is how/where to s[color=#282828][font=arial, verdana, tahoma, sans-serif]pecify a unique SEO name for the new dispatch.[/font][/color]

Your 3 points are correct. To specify unique SEO name for the custom dispatch please use the Website → SEO → SEO rules page:

[quote]Ok I've tried a few things and I came to the following conclusions, please correct me if I'm wrong.[/quote]



well done, you are correct about these points.



best regards,

WSA team

Thanks to the both of you, I think I know enough (for now) to complete my first project.

So I've been playing around some more and I'm almost done with my form.



I'm just having some problems figuring out how to use functions in the init file (if that is the correct place).



What I'm trying to do is basically this:



I have an input field in the settings part of the plugin, where somebody can enter an e-mailaddress.

I'm guessing that I can't actually add checks (javascript/php) to the settings part of an addon so my guess was that you have to do this during the init part of the loading process (init.php).



To do this I made a check function (check if e-mailaddress is a correctly entered, so no 2x@ etc.) in the func.php file and named it according to the naming convention I red about: function fn__check_emailaddress($emailaddress) where of course is my actual addon name.



Now the problem is that the advanced tutorial only shows how to call a function with a hook and no parameters are used:

fn_register_hooks(

'get_category_data_pre'

);



But I would like to call my function with the e-mailaddress in the settings and either set a variable to show in the template or use something like die(); to show a message that the e-mailaddress isn't correct.



I know how to read the settings by using:



$vars = Registry::get('addons.my_addon');

$emailaddress = $vars['emailaddress'];



And assign a variable for use in a template with:

Registry::get('view')->assign('emailaddress', $emailaddress);



It's just I don't yet understand how to call functions the correct way and if init.php/func.php are the correct files to use in this situation.

I still haven't figured it out, it's not really that big of an issue, but it would be nice if I could use the settings part of a plugin.

Nobody has any idea?

I'm not really an expert in cscart yet but im sure that this:



[color=#282828][font=arial, verdana, tahoma, sans-serif]fn_register_hooks([/font][/color]

[color=#282828][font=arial, verdana, tahoma, sans-serif]'get_category_data_pre'[/font][/color]

[color=#282828][font=arial, verdana, tahoma, sans-serif]);[/font][/color]



[color=#282828][font=arial, verdana, tahoma, sans-serif]is just for you to use the PHP hooks available in Cscart which can be found on their hooks base. [/font][/color]

Hooks base

(don't forget to choose the version you are using)



using the site and if you search for get_category_data_pre you will see where the hook is located by mousing over the “…” icon

and you will see the parameters when you press the “+” button.



the parameters are used in this tutorial:



PHP Hooks — CS-Cart 4.2.x documentation

(change the version for the one you are using)



and the naming convention of the function would be

fn__



You only want to use PHP hooks when you have something you want to use or manipulate on already existing hook functions.

Thanks for your reply rionyamato, but as I allready said in my previous post, the problem with the tutiorial and the default existing hooks is that they don't show how to work with parameters.



Like I said, I would like to make a check function to make sure the user correctly entered the information in the plugin settings.

The information in the tutorial doesn't provide me with enough info to actually be able to do this.

[quote][color=#282828][font=arial, verdana, tahoma, sans-serif]Now the problem is that the advanced tutorial only shows how to call a function with a hook and no parameters are used:[/font][/color]

[color=#282828][font=arial, verdana, tahoma, sans-serif]fn_register_hooks([/font][/color]

[color=#282828][font=arial, verdana, tahoma, sans-serif]'get_category_data_pre'[/font][/color]

[color=#282828][font=arial, verdana, tahoma, sans-serif]);[/font][/color]



[color=#282828][font=arial, verdana, tahoma, sans-serif]But I would like to call my function with the e-mailaddress in the settings and either set a variable to show in the template or use something like die(); to show a message that the e-mailaddress isn't correct.[/font][/color]



[/quote]



The hook is when you interrupt the CORE function with you code in other words with your function that assigned to this hook.

You can add some extra parameter to such function because the get all the parameters that sent to the hook. For example in “[color=#282828][font=arial, verdana, tahoma, sans-serif]get_category[/font][/color][color=#282828][font=arial, verdana, tahoma, sans-serif]_data_pre” these are:[/font][/color]

$category_id, $field_list, $get_main_pair, $skip_company_condition, $lang_code





But as I understood from you post you would like to add some preprocessing of your add-on settings update, this could be done easily that hook init.php. In this case you can just extend the settings action scheme located at

app/schemas/settings/actions.functions.php



The function name should be

fn_settings_actions_YOUR_SETTING_NAME($old_value, $new_value)



Here you can do whatever you want, take a look to the app/schemas/settings/actions.functions.php - you will find several examples there.

Sorry for the delay in my reply, I was a bit busy the last couple of days.



Thanks for your reply, I think I get it.

But wouldn't adding my own function there be an issue when there's an update?



I'm trying to design my addon in such a way that updating shouldn't break my addon (unless something big changes to how addons function).

[quote name='RagingRaven' timestamp='1436791107' post='222704']

Sorry for the delay in my reply, I was a bit busy the last couple of days.



Thanks for your reply, I think I get it.

But wouldn't adding my own function there be an issue when there's an update?



I'm trying to design my addon in such a way that updating shouldn't break my addon (unless something big changes to how addons function).

[/quote]



There won't be any problems if you add all your code to the add-on.

So in this case you should add this new function to the func.php of your add-on.

Ok, I guess I misunderstood you.



I though I needed to add a function to [color=#282828][font=arial, verdana, tahoma, sans-serif]app/schemas/settings/actions.functions.php, but those were just examples.[/font][/color]

[color=#282828][font=arial, verdana, tahoma, sans-serif]If I create a function [/font][/color][color=#282828][font=arial, verdana, tahoma, sans-serif]fn_settings_actions_YOUR_SETTING_NAME($old_value, $new_value) in my func.php it would be executed once I save/update my addon settings.



In this function $old_value is the value the setting had before changing it and $new_value is the value that got filled in / set.[/font][/color]



[color=#282828][font=arial, verdana, tahoma, sans-serif]I can then execute my code and for example show a notification on error:[/font][/color]

fn_set_notification('W', '', '', 'S');

[color=#282828][font=arial, verdana, tahoma, sans-serif]Where the notification then would stay up untill closed according to the 'S' state.[/font][/color]



[color=#282828][font=arial, verdana, tahoma, sans-serif]Am I correct?[/font][/color]

Ok I've been trying to get it to work, but I have yet to get an error notification.

I now have the following:



in /app/addons//addon.xml I have the following setting:

```php







E-mailadres
input






```

In [i]/app/addons//init.php[/i] I have:
```php
if ( !defined('AREA') ) { die('Access denied'); }

fn_register_hooks(
'fn_settings_actions'
);
```

In I have:
```php
if ( !defined('AREA') ) { die('Access denied'); }
function fn_settings_actions_emailaddress($old_value, $new_value)
{
if (empty($new_value)) {
$new_value = $old_value;
fn_set_notification('W', 'Check input', 'E-mailaddress cannot be empty', 'S');
return false;
}
}
```

Note: I used the item id from the addon settings instead of name, because when using the name (you said [color=#282828][font=arial, verdana, tahoma, sans-serif]YOUR_SETTING_NAME), I get an internal server error.[/font][/color]
[color=#282828][font=arial, verdana, tahoma, sans-serif]What am I missing / doing wrong?[/font][/color]

hi,



the “app/addons//schemas/settings/actions.functions.post.php” should be:

```php



function fn_settings_actions_addons__emailaddress(&$new_value, $old_value)
{
if (empty($new_value)) {
$new_value = $old_value;
fn_set_notification('W', 'Check input', 'E-mailaddress cannot be empty', 'S');
return false;
}

return true; // try to remove this line if update doesn't work
}
```

comments:
1. the function name should have addon ID in its name
2. possibly "return true;" line is needed.

p.s. this code has not been tested.

best regards,
WSA team

P.S. updated by imac so the code is correct now.

[quote name='Damir (WSA-team)' timestamp='1436978764' post='223088']

hi,



the “app/addons//schemas/settings/actions.functions.post.php” should be:

```php



function fn_settings_actions__emailaddress(&$new_value, $old_value)
{
....
}
```
....
[/quote]

The correct function name should be:

```php fn_settings_actions_addons__emailaddress ```

Solution from [font=arial, verdana, tahoma, sans-serif][color=#282828]WSA team should work correctly. To see more example search the CS-Cart app/addons folder for “[/color][/font]fn_settings_actions_addons_” there are several add-ons that add setting handlers.