Jump to content

 

tbirnseth

Member Since 08 Nov 2008
Offline Last Active May 02 2022 02:11 AM
*****

#102879 PHP Controllers - 1

Posted by tbirnseth on 04 February 2011 - 05:42 AM

Okay, so what's a controller and why would I want one?

In cs-cart, a controller is the basis for navigation and collection of data and presentation of information... Whew, that was a mouthful.

The home page of your store is the 'index' controller. it is special and not handled like most others. But it can be modified in the same way as other controllers.

Anything related to a product is the 'products' controller and then there are 'categories', 'checkout', etc. You can identify which controllers are associated with each AREA (customer or admin) by looking in the controllers/<area or common> directories.

A controller has several standard properties to it and are part of the URL or "dispatch" of the controller. A controller is invoked by the parameter "dispatch=<controller_name>" in the URL. The standard properties of a controller are separated by dots '.' after the controller. These are:
MODE ($mode)
ACTION ($action)
DISPATCH_EXTRA ($dispatch_extra)

In the above properties there is a PHP define for the property (like MODE) and a variable that is defined when a controller is executed (like $mode). These are used within the controller to tell it how to behave.

So a dispatch of "dispatch=checkout.add..1234" would come to the 'checkout' controller with
$mode = 'add'
$action = '' (empty)
$dispatch_extra = '1234'

This is generally how single products are added to the cart via an ajax request where 1234 is the product_id. Just wanted to give an example of using all the properties....

Other times the key info is sent as an http REQUEST. I.e. "&order_id=123". So these are not really consistent. But for the most part things like product_id, category_id, order_id, etc. are passed as REQUEST parameters either via http POST or GET methods.

For the basics about what a controller does, let's look at a simple one. We'll walk through controllers/customer/pages.php.

You'll see if( $mode == 'view') This means that we're only going to do something for the 'view' mode. There are no other modes available to customers. But if you look in controllers/admin/pages.php you'll see other modes avaiable.

The first line:
$_REQUEST['page_id'] = empty($_REQUEST['page_id']) ? 0 : $_REQUEST['page_id'];
simply makes sure there is a page_id set and that it's an integer if not set.

The next line
$preview = ($auth['area'] == 'A' && !empty($_REQUEST['action']) && $_REQUEST['action'] == 'preview') ? true : false;
will always have $preview == false since this is the customer controller. So not sure why it's even in there other than it's a bug!

The next line
$page = fn_get_page_data($_REQUEST['page_id'], CART_LANGUAGE, $preview);
Loads the page data from the database for page_id 1.

The next line checks to see if the page is active or not. If not, it returns a value so that a 404 Page Not Found header will be generated. (we'll talk about returning from a controller later. But it is critical that this be done correctly).

The next lines set the template variables for the page's meta-description and for the meta-keywords. Note the $view variable which always points to the current template page that will be constructed. However, I've found that $view is NOT always set so it is safer to use Registry::get('view')-> instead of $view-> (just an observation).

The next line assigns the page title and the next one extracts the page heirarchy into the $parent_ids array from the id_path comma separated value (csc calls this a hash). It then uses the parent_ids to build the breadcrumbs.

And finally, the $page template variable is set with all the page data from the database.

For consistency, this controller should end with
return array(CONTROLLER_STATUS_OK) for consistency.

So what happens next. The system will look for a template based on the 'mode' of the controller. So by default, it will look for:
skins/<your_skin>/customer/views/pages/view.tpl

If it can't find that file it will return a page not found. Otherwise, all the template variables assigned so far will be available for use in the template.

So in summmary, a controller is what's specified to tell the system what to do. The mode, action and dispatch_extra properties tell the controller what to do. The mode is special in that it also identifies the template file to use.

I'm tired of typing so we'll end this one here. In the next post I'll talk about addons and how they behave just like general controllers....


#102876 The PHP side of the equation

Posted by tbirnseth on 04 February 2011 - 05:01 AM

I'm starting this thread with the goal of addressing specific methods in cs-cart for modifying the behavior of the system through the use of controllers hooks and php hooks.

Hopefully someone will make this sticky so it will remain visible.

My thoughts are I would write a posts that try to cover the why's and how's for the following things:
1) Pre and post controllers (starting with an overview of controllers and addons)
2) PHP hooks
3) Using controllers and hooks to add fields to a product and have those fields used in an order calcuation and visible on the orders page.

Will probably create a separate thread for each so conversation can be kept together. I'm happy to answer questions and would encourage cs-cart developers to engage along the way.


#93502 How to Set Flat Rate Shipping for unlimited products in pkg to any country

Posted by tbirnseth on 02 November 2010 - 05:25 AM

Shipping/Taxes
Select the shipping method
Click the Shipping charges tab of the method
Set your $15.00 (Aboslute) for Products cost greater than $0.00


#93333 Creating a Custom Skin/Template

Posted by tbirnseth on 31 October 2010 - 07:49 AM

Most skins utilize a core set of functionality from the cart including ajax methods, javascript, jquery stuff and functional (not display) templates.

Hence it's generally easiest (and safest for the future) to take a standard template that has a layout as close to your needs as possible and then to modify it to suit. The majoritiy of templates are really more functional than UI oriented. You want to use there wherever possible. Pay close attention to common_templates/product_data.tpl. You might want to enclose that whole template in your own hook..... Just a hint.

I personally would use hooks for everything so my changes are upgrade independent. If hooks don't exist for what I want to do, I would add them. Much easier to add a hook back after an upgrade than to integrate a bunch of template code.

Your css should also be specific to your changes, not modifying the standard files but to hook your own stylesheet in after the standard ones.

Just my opinion...


#80328 New tax calculation method

Posted by tbirnseth on 18 May 2010 - 01:09 AM

Aren't taxes fun?


#56250 Can someone explain me the "My changes" addon?

Posted by tbirnseth on 07 September 2009 - 05:29 AM

developer-supported


Falling out of my chair laughing...

When have you ever seen a cs-cart developer discuss (or document) any of the internals about how something is intended to work? Hope you're not holding your breath! :-)