Controller is the core file where all the logic and magic take place. This is also where the variables for values and language are set and passed to the view variables for display. A Controller in OpenCart is simply a class file that is named in a way that can be associated with a URL.
Consider this URL: http://example.com/index.php?route=module/helloworld.
In the above example, OpenCart would attempt to find a controller file helloworld. php in the module folder with class ControllerModuleHelloworld.
We can see the code at admin/controller/module/helloworld.php whose functionalities are described as follows:
In OpenCart, controller class names must start with the controller and the folder on which the module is located and the filename without extension. For example, in the Hello World module, the class name for the controller is ControllerModuleHelloworld as it is inside the module folder and the filename is helloworld.php. Also, always make sure your controller extends the parent controller class.
class ControllerModuleHelloworld extends Controller {
Whenever the controller is called, the index function (public function index()) is always loaded by default.
$this->language->load('module/helloworld');
The preceding line of code loads the language file variables of helloworld.php which is in the module folder at admin/language/*/module/helloworld.php (* represents the language folder) and now you are able to get the text or messages with reference to variables like $this->language->get('heading_title'). This means the Hello World text is ready to transfer to the template files.
$this->document->setTitle($this->language->get('heading_title')); The preceding line of code sets the title of the document Hello World.
The $this->load->model('setting/setting') variable loads the setting.php file of the setting folder which is in the model folder. As explained previously, it loadsadmin/model/setting/setting.php. Your module can load any model file in its controller file using the following code, if they are in the same admin or catalog folder as the controller. You will need to specify the path to the file you want to load from the admin folder within the parentheses. The preceding code will load the settings class so we have access to the functions within the ModelSettingSettingclass in our model's controller file. Use the following format in your code to call a function from a loaded model file:
$this->model_setting_setting->editSetting('helloworld',
$this->request->post);
if (($this->request->server['REQUEST_METHOD'] == 'POST') &&
$this->validate()) { $this->model_setting_setting->editSetting('helloworld',
$this->request->post);
$this->session->data['success'] = $this-> language->get('text_success');
$this->redirect($this->url->link('extension/module',
'token=' . $this->session->data['token'], 'SSL'));
}
When a form is saved in the module section, the preceding lines of code, which are at admin/controller/module/helloworld.php run. If the code is submitted through the POST method and validates function return true, all the settings are saved to the database at the setting table and a success message is assigned to the success variable and is redirected to the list of the module page.
protected function validate() {
if (!$this->user->hasPermission('modify', 'module/helloworld'))
{
$this->error['warning'] = $this->language
->get('error_permission');
}
if (!$this->request->post['helloworld_code']) {
$this->error['code'] = $this->language->get('error_code');
}
if (!$this->error) { return true;
} else { return false;
}
}
When a form is submitted, validation is checked for whether permission is provided or not. It is checked whether the Hello World Content consists of the text or not. If no access is provided or no content is entered, error is returned true, by which it shows Code Required or Permission Denied! and alerts the user to provide the access or insert the content.
$this->data['heading_title'] = $this->language
->get('heading_title');
$this->data['text_enabled'] = $this->language
->get('text_enabled');
The $this->language->get('heading_title') variable gets the value of the $_['heading_title'] variable from the language file helloworld.php, which is "Hello World" and is assigned to $this->data['heading_title']. Likewise, for $this->language->get('text_enabled'), "Enabled" is assigned to $this>data['text_enabled'] and the same for the other files.
if (isset($this->error['warning'])) {
$this->data['error_warning'] = $this->error['warning'];
} else {
$this->data['error_warning'] = '';
}
The Hello World module checks for access permission and gives a warning if the user has no access to the module.
if (isset($this->error['code'])) {
$this->data['error_code'] = $this->error['code'];
} else {
$this->data['error_code'] = '';
}
If no content is inserted in the Hello World Content field and the user tries to save the module, it validates whether the content is inserted or not; if content is not inserted, an error is activated by which it will show the error code as "Code Required".
$this->data['breadcrumbs'] = array();
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home', 'token=' .
$this->session->data['token'], 'SSL'),
'separator' => false
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_module'),
'href' => $this->url->link('extension/module', 'token=' .
$this->session->data['token'], 'SSL'),
'separator' =>' :: '
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('module/helloworld', 'token=' .
$this->session->data['token'], 'SSL'),
'separator' =>' :: '
);
Breadcrumbs are defined in an array, and contain elements such as text, href, and separator. Text elements hold the word to show in the template file, href holds the link for the word, and separator holds what to use to separate between words. This is shown in the preceding lines of code.
'text' => $this->language->get('text_home'),
The preceding line of code holds the "Home" word as per the language file.
'href' => $this->url->link('common/home', 'token=' .
$this->session->data['token'], 'SSL'),
The preceding line of code holds the link to the "Home" word.
'separator' => false
The preceding line of code holds the separator between the breadcrumbs; if no separator is needed, false is assigned.
$this->data['action'] = $this->url->link('module/helloworld',
'token=' . $this->session->data['token'], 'SSL');
The preceding line of code will create a link and store it into the action variable. If we have to create the link in the admin area, we have to use it as explained previously. A token is used to preserve the admin user state.
$this->data['modules'] = array();
$this->data['modules'] = $this->config->get('helloworld_module');
An empty array is defined and we assign $this->data['modules'] with all the settings of helloworld_module.
$this->load->model('design/layout');
It loads the layout.php file of the design folder which is in the model folder. As explained previously, it loads admin/model/design/layout.php. The preceding code will load the layout class, so we have access to the functions within the ModelDesignLayout class in our module's controller file.
$this->data['layouts'] = $this->model_design_layout->getLayouts();
The underscores (model_design_layout) refer to the file designations for model/ design/layout.php. The layouts variable now holds all the layouts that are created at System | Design | Layout at the admin sections.
$this->template = 'module/helloworld.tpl';
$this->children = array('common/header','common/footer');
In the controller, you will need to load your module's template file in view. To do so, set $this->template to $this->template = 'module/helloworld.tpl', and it loadsadmin/view/template/module/helloworld.tpl.
$this->response->setOutput($this->render());
The $this->response->setOutput() variable sends data to the browser whether it's HTML or JSON and $this->render constructs the output HTML from the templates/data.
Creating the template file at admin in the OpenCart module
This refers to the template or TPL files. All variables that are passed from the controller to the view can be used for displaying the output of calculations or functionality.
Open the admin/view/template/module/helloworld.tpl file; we are describing the code taking some snippets only.
<?php echo $header; ?>
<?php echo $footer; ?>
The $header and $footer variables are passed from the controller as the template's children.
$this->children = array('common/header','common/footer');
With this, the content of the header and footer are shown on the module section.
Breadcrumbs section for the module
For keeping track of navigation, breadcrumbs are used; in the template file, breadcrumbs are shown by the following lines of code:
<div class="breadcrumb">
<?phpforeach ($breadcrumbs as $breadcrumb) {
?>
<?php echo $breadcrumb['separator']; ?><a href="<?php echo
$breadcrumb['href']; ?>"><?php echo $breadcrumb['text'];
?></a>
<?php } ?>
</div>
The $breadcrumbs array has been passed by the controller files. The $breadcrumbs array consists of the separator, URL link, and the text to show. All elements of the $breadcrumbs array are managed in the controller.
<?php if ($error_warning) {
?>
<div class="warning"><?php echo $error_warning; ?></div>
<?php } ?>
A warning will show up if you have no permission to access or edit the module. As for the Hello World module, it checks for permission and shows a warning if the user has no access to the module. The following screenshot shows the Breadcrumbs, Header image and Title, and Header save and cancel button:
The following line of code shows the image icon near the heading title:
<h1><imgsrc="view/image/module.png" alt="" /><?php echo
$heading_title; ?></h1>
The following line of code shows the heading title that is passed from the controller:
$this->data['heading_title'] = $this->language
->get('heading_title');
The following lines of code show the buttons to save and cancel:
<div class="buttons">
<a onclick="$('#form').submit();" class="button"><?php echo
$button_save; ?></a>
<a href="<?php echo $cancel; ?>" class="button"><?php echo
$button_cancel; ?></a>
</div>
On clicking the Save button, the form with ID is submitted; on clicking the Cancel button, it calls the extension/module controller, which means it is redirected to the list of modules.
<form action="<?php echo $action; ?>" method="post"enctype="multipart/form-data" id="form">
When the form code is initiated, it has id=form, which is used in the Save button to submit the form. When we click on the Save button, an action to the module / Hello World controller processes the submitted data.
The <span class="required">*</span> shows the asterisk (*) in red color by the style class required.
<textarea name="helloworld_code" cols="40" rows="5"><?php echo
$helloworld_code; ?></textarea>
<?php if ($error_code) {
?>
<span class="error"><?php echo $error_code; ?></span>
<?php } ?>
This is the text area field which holds some data; if this text area is submitted empty, it shows as an error.
<tr>
<td class="left"><?php echo $entry_layout; ?></td>
<td class="left"><?php echo $entry_position; ?></td>
<td class="left"><?php echo $entry_status; ?></td>
<td class="right"><?php echo $entry_sort_order; ?></td>
<td></td>
</tr>
The table heading is shown by the preceding code and it will look as shown in the following screenshot:
In the following code snippet, the $module_row variable is defined. It is assigned to zero and is increased with the foreach loop, so it is the count of the module rows that increases on clicking on the Add Module button.
<?php $module_row = 0; ?>
<?phpforeach ($modules as $module) { ?>
The $modules array carries the setting of the module; if it is empty, only the Add Module button is shown.
<select name="helloworld_module[<?php echo $module_row;
?>][layout_id]">
<?php foreach ($layouts as $layout) {
?>
<?php if ($layout['layout_id'] == $module['layout_id']) {
?>
<option value="<?php echo $layout['layout_id']; ?>" selected="selected"><?php echo $layout['name'];
?></option>
<?php } else { ?>
<option value="<?php echo $layout['layout_id']; ?>"><?php echo
$layout['name']; ?></option>
<?php } ?>
<?php } ?>
</select>
The preceding code shows the Layout option. If the layout id matches the module layout id, which has been already saved, the selected layout is shown among other layouts, else layouts are shown as default. The layout arrays have been passed from the controller. Similarly, for the position, select fieldname as helloworld_module with its second element as position.
<select name="helloworld_module[<?php echo $module_row;
?>][position]">
As we already know, there are four positions described in OpenCart; they are content top, content bottom, column left, and column right. The position module code for the content top is as follows:
<?php if ($module['position'] == 'content_top') {
?>
<option value="content_top" selected="selected"><?php echo
$text_content_top; ?></option>
<?php } else {
?>
<option value="content_top"><?php echo $text_content_top;
?></option>
<?php } ?>
If module position is already defined and is equal to content_top, content top is selected, else others are selected as default. It works in a similar way for the content bottom, column left, and column right.
<select name="helloworld_module[<?php echo $module_row;
?>][status]">
<?php if ($module['status']) {
?>
<option value="1" selected="selected"><?php echo
$text_enabled; ?></option>
<option value="0"><?php echo $text_disabled; ?></option>
<?php } else { ?>
<option value="1"><?php echo $text_enabled; ?></option>
<option value="0" selected="selected"><?php echo
$text_disabled; ?></option>
<?php } ?>
</select>
The preceding code is to show the module status; if module is enabled, option value is equal to 1, else it is 0. If module status is defined or equal to 1, it shows that the module is already defined, soenabled is selected. If it is not defined, disabled is selected.
<input type="text" name="helloworld_module[<?php echo $module_row;
?>][sort_order]" value="<?php echo $module['sort_order']; ?>" size="3" />
The preceding code holds the sort order of the module.
<a onclick="$('#module-row<?php echo $module_row; ?>').remove();" class="button"><?php echo $button_remove; ?></a>
The preceding code line removes the rows when we click on the Remove button.
<a onclick="addModule();" class="button"><?php echo
$button_add_module; ?></a>
On clicking on the Add Module link, function addModule is called, which adds a row just below the previous row. function addModule() {}
The preceding function adds the rows for the modules setting. We can add as many modules as we like, just keep on clicking on the Add Module button. The following screenshot shows multiple rows for setting after clicking on the Add Module button:
No comments:
Post a Comment