OpenCart's Controller is simply a class file that is named in a way that can be associated with a URI. The class name should start with the word Controller followed by the folder name and the file name. For example: class ControllerModuleFeatured extends Controller {
The preceding code line creates the Controller class of the Featured module. The class name starts with Controller, followed by the module folder, and then the featured file. The Featured module'sController file is named as featured.php and is in the module folder. As always, it has extended the Controller parent.
If the file name consists of an underscore (_), there will be no problems with respect to the class name. Everything except the underscore needs to be the same. If your Controller file is named with an underscore, you have to make the language file with an underscore as well. Never use an underscore for the class name.
Most of the related code is already described in Chapter 1, Getting Started with OpenCart Modules, so you have the description of the code and how it works functionally. By default, the index() method is called unless the second segment is passed in the URI. While clicking on [Edit], no second segment is passed, so it runs the index() method, which loads the language files named featured.php in the module folder in the language section, and sets the title of the document as follows:
$this->document->setTitle($this->language->get('heading_title'));
It then loads the model file, setting.php, and when the module is saved, it validates the data by checking the permission and checking whether the image size is inserted or not. Check the validate()method, find out how it returns true when validation is successful and assigns the error message; it returns false if there is some error, such as permission denied and/or the image's height and width are not entered.
if (isset($this->request->post['featured_module'])) { foreach ($this->request->post['featured_module'] as
$key => $value) {
if (!$value['image_width'] || !$value['image_height']) {
$this->error['image'][$key] = $this->language->get(
'error_image');
}
}
}
The preceding code shows how the error message gets activated if height and/or width are not inserted on saving the module.
$this->data['heading_title'] = $this->language->get(
'heading_title');
$this->data['text_enabled'] = $this->language->get(
'text_enabled');
$this->data['text_disabled'] = $this->language->get(
'text_disabled');
The text and messages to be shown at view are assigned from the language files to data variables; you can see similar lines of code which perform this function:
if (isset($this->error['image'])) {
$this->data['error_image'] = $this->error['image'];
} else {
$this->data['error_image'] = array();
}
If someone forgets to insert the height and/or width of the image, the error messages to be shown are assigned. The breadcrumbs are defined in an array as follows: $this->data['breadcrumbs']
And the action links are defined as follows:
$this->data['action'] = $this->url->link('module/featured', 'token=' . $this->session->data['token'], 'SSL');
The list of products that you have inserted is submitted in $_POST['featured_ product'] and all the product IDs are separated by a comma. Similarly, the products stored in the database for the Featuredmodule are also saved with their product IDs separated by commas:
The following code checks whether the product is submitted and then takes the product IDs from the POST method; if not, it takes from the database value:
if (isset($this->request->post['featured_product'])) {
$products = explode(',', $this->request->post[
'featured_product']);
} else {
$products = explode(',', $this->config->get(
'featured_product'));
}
$products is run through a loop to make an array of products' names and IDs and is passed to the template view.
$this->data['modules'] = $this->config->get('featured_module');
The preceding line of code retrieves the settings of the Featured module from the database. Other parts of the code are similar to those defined in the Hello World module, in Chapter 1, Getting Started with OpenCart Modules, and the same logics are applied to the language file, so we don't need to describe these here.
Exploring the featured.tpl file under admin folder
We will be describing only the extra code snippets, as most of them are already described in the Hello World module.
The most distinguishing section in the Featured module is the autocomplete input box. For that, let's create an input box as follows:
<input type="text" name="product" value="" />
Whenever a user starts to type in the text box, the following code starts to work:
$('input[name=\'product\']').autocomplete({
It searches for similar-named products as follows:
admin/index.php?route=catalog/product/autocomplete
If it finds products, the product, on clicking, gets appended to the featured product ID's <div> element and a product list is generated. The code is shown in the admin/ view/template/module/featured.tpl file as follows:
$('#featured-product').append('<div id="featured-product' + ui.item.value + '">' + ui.item.label + '<imgsrc=
"view/image/delete.png" alt="" /><input type="hidden" value=
"' + ui.item.value + '"/></div>')
When the red minus sign, to the right of the product, is clicked, the following code snippet gets activated that deletes the rows of product:
$('#featured-product div img').live('click', function() {
$(this).parent().remove();
$('#featured-product div:odd').attr('class', 'odd');
$('#featured-product div:even').attr('class', 'even'); data = $.map($('#featured-product input'), function(element){ return $(element).attr('value');
});
$('input[name=\'featured_product\']').attr('value', data.join()); });
Exploring the featured.php file under the catalog folder
Only the extra code snippets are described, as most of them are discussed in Chapter
1, Getting Started with OpenCart Modules, and most of them are similar to the Hello World module.
$products = explode(',', $this->config->get('featured_product'));
If we echo $this->config->get('featured_product'), we will get the product IDs that are separated by commas. Thus, the $products array is assigned by separating the product IDs by commas.
if (empty($setting['limit'])) {
$setting['limit'] = 5;
}
If there is no limit inserted while setting the Featured module, it will show only five products.
$products = array_slice($products, 0, (int)$setting['limit']);
An iteration is performed using foreach to the $products array, and with the help of $this->model_catalog_product->getProduct($product_id);, all details of the product are retrieved and only the required elements are assigned to the $products array to be passed to the template file as follows:
$this->data['products'][] = array(
'product_id' => $product_info['product_id'],
'thumb' => $image,
'name' => $product_info['name'],
'price' => $price,
'special' => $special,
'rating' => $rating,
'reviews' =>sprintf($this->language->get('text_reviews'),
(int)$product_info['reviews']),
'href' => $this->url->link('product/product',
'product_id=' . $product_info['product_id'])
);
With the preceding code, only the required data such as product_id, thumb, and name are assigned to the array that will be shown in the template file.
The code of catalog/view/theme/default/template/module/featured.tpl are similar to the Hello World module template file. Here, products that are added on the backend are shown. The$products array is received from the controller file, which consists of the product ID, thumb of image, name, price, special price, rating, reviews, and link to the product details. The same data are shown in the Featured module's frontend.
No comments:
Post a Comment