Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

handler on setting value change Rate Topic   - - - - -

 
  • RagingRaven
  • Member
  • Trial users
  • Join Date: 26-Mar 13
  • 44 posts

Posted 26 June 2015 - 11:41 AM #1

Hello everybody,

I've followed this tutorial to create an addon (and it works):
http://docs.cs-cart....s/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.

 

Posted 26 June 2015 - 04:41 PM #2

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 app/addons/advanced_addon/controllers/frontend/mypage.php with content:
<?php

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

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

2. create file design/themes/THEME/templates/addons/advanced_addon/views/mypage/view.tpl and add necessary content into the file

3*. if you need a custom layout, you may create a new custom tab in Design -> Layouts with dispatch = mypage.view
4*. if you use SEO module, you may also specify unique SEO name for the new dispatch

best regards,
WSA team

 
  • RagingRaven
  • Member
  • Trial users
  • Join Date: 26-Mar 13
  • 44 posts

Posted 27 June 2015 - 12:25 PM #3

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

 

Posted 27 June 2015 - 06:05 PM #4

great, keep us updated!

best regards,
WSA team

 
  • RagingRaven
  • Member
  • Trial users
  • Join Date: 26-Mar 13
  • 44 posts

Posted 29 June 2015 - 03:04 PM #5

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 'index.php?dispatch=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 specify a unique SEO name for the new dispatch.

 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 19851 posts

Posted 29 June 2015 - 03:20 PM #6

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

http://demo.cs-cart....eo_rules.manage

GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 345     Multi-Vendor              USD 1250    CS-Cart RU                         24500 руб.
CS-Cart Ultimate  USD 775     CS-Cart + YOUPI      USD 545      CS-Cart RU + UniTheme    36000 руб.


 

Posted 29 June 2015 - 06:05 PM #7

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


well done, you are correct about these points.

best regards,
WSA team

 
  • RagingRaven
  • Member
  • Trial users
  • Join Date: 26-Mar 13
  • 44 posts

Posted 30 June 2015 - 09:12 AM #8

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

 
  • RagingRaven
  • Member
  • Trial users
  • Join Date: 26-Mar 13
  • 44 posts

Posted 30 June 2015 - 03:54 PM #9

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_<my_addon_name>_check_emailaddress($emailaddress) where of course <my_addon_name> 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.

 
  • RagingRaven
  • Member
  • Trial users
  • Join Date: 26-Mar 13
  • 44 posts

Posted 06 July 2015 - 12:31 PM #10

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?

 
  • rionyamato
  • Newbie
  • Trial users
  • Join Date: 02-Jul 15
  • 6 posts

Posted 07 July 2015 - 02:35 AM #11

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

fn_register_hooks(
'get_category_data_pre'
);

is just for you to use the PHP hooks available in Cscart which can be found on their hooks base.
https://www.cs-cart....oks_base.manage
(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:

http://docs.cs-cart..../php_hooks.html
(change the version for the one you are using)

and the naming convention of the function would be
fn_<your addon name>_<hook name you called in fn_register hooks>

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

 
  • RagingRaven
  • Member
  • Trial users
  • Join Date: 26-Mar 13
  • 44 posts

Posted 07 July 2015 - 02:29 PM #12

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.

 
  • imac
  • Head of Product
  • CS-Cart Architects
  • Join Date: 22-Nov 05
  • 2074 posts

Posted 08 July 2015 - 12:38 PM #13

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.


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 "get_category_data_pre" these are:
$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.
Ilya Makarov,
CS-Cart Architect Team
Suggest and vote for new features | Report a bug

 
  • RagingRaven
  • Member
  • Trial users
  • Join Date: 26-Mar 13
  • 44 posts

Posted 13 July 2015 - 12:38 PM #14

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).

 
  • imac
  • Head of Product
  • CS-Cart Architects
  • Join Date: 22-Nov 05
  • 2074 posts

Posted 15 July 2015 - 08:42 AM #15

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).


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.
Ilya Makarov,
CS-Cart Architect Team
Suggest and vote for new features | Report a bug

 
  • RagingRaven
  • Member
  • Trial users
  • Join Date: 26-Mar 13
  • 44 posts

Posted 15 July 2015 - 09:15 AM #16

Ok, I guess I misunderstood you.

I though I needed to add a function to app/schemas/settings/actions.functions.php, but those were just examples.
If I create a function 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.


I can then execute my code and for example show a notification on error:
fn_set_notification('W', '<my notification title>', '<my notification message content>', 'S');
Where the notification then would stay up untill closed according to the 'S' state.

Am I correct?

 
  • RagingRaven
  • Member
  • Trial users
  • Join Date: 26-Mar 13
  • 44 posts

Posted 15 July 2015 - 02:17 PM #17

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/<myaddon>/addon.xml I have the following setting:
<settings>
<sections>
  <section id="algemeen">
   <items>
    <item id="emailaddress">
	 <name>E-mailadres</name>
	 <type>input</type>
	 <default_value></default_value>
    </item>
   </items>
  </section>
</sections>
</settings>

In /app/addons/<myaddon>/init.php I have:
if ( !defined('AREA') ) { die('Access denied'); }

fn_register_hooks(
'fn_settings_actions'
);

In I have:
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 YOUR_SETTING_NAME), I get an internal server error.
What am I missing / doing wrong?

 

Posted 15 July 2015 - 04:46 PM #18

hi,

the "app/addons/<myaddon>/schemas/settings/actions.functions.post.php" should be:
<?php


function fn_settings_actions_addons_<myaddon>_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.

 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 19851 posts

Posted 16 July 2015 - 08:43 AM #19

hi,

the "app/addons/<myaddon>/schemas/settings/actions.functions.post.php" should be:

<?php


function fn_settings_actions_<myaddon>_emailaddress(&$new_value, $old_value)
{
....
}
....


The correct function name should be:

fn_settings_actions_addons_<myaddon>_emailaddress

GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 345     Multi-Vendor              USD 1250    CS-Cart RU                         24500 руб.
CS-Cart Ultimate  USD 775     CS-Cart + YOUPI      USD 545      CS-Cart RU + UniTheme    36000 руб.


 
  • imac
  • Head of Product
  • CS-Cart Architects
  • Join Date: 22-Nov 05
  • 2074 posts

Posted 16 July 2015 - 01:03 PM #20

Solution from WSA team should work correctly. To see more example search the CS-Cart app/addons folder for "fn_settings_actions_addons_" there are several add-ons that add setting handlers.
Ilya Makarov,
CS-Cart Architect Team
Suggest and vote for new features | Report a bug