Tuesday, April 8, 2014

Configuring the module [Getting Started with OpenCart Modules]

After clicking on [Install] of the module, [Edit] [Uninstall] gets activated; after clicking on [Edit], you will see the configuration section for the module. As per the Hello World module, the following screenshot shows the configuration section on clicking on [Edit]:


The Hello World Content field is saved in the setting table (oc_setting or as per the prefixes used during installation of OpenCart) of the database as per the name of the input box with group column of "helloworld". For this module, navigate to the file admin/view/template/module/helloworld.tpl, where you will find the following code:
<textarea name="helloworld_code" cols="40" rows="5"><?php echo
$helloworld_code; ?></textarea>
Thus, the message or text you typed in the text area is passed to the  admin/controller/module/helloworld.php controller and the following  lines of code is processed:
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'));
}
It checks if the form is submitted through the POST method and checks whether the Hello World Content field is empty or not with the validate function. If the content is not empty and the form is submitted through the POST method, it calls the editSetting function which is in admin/model/setting/setting.php.
public function editSetting($group, $data, $store_id = 0) {
  $this->db->query("DELETE FROM " . DB_PREFIX . "setting WHERE      store_id = '" . (int)$store_id . "' AND `group` = '" . $this-
      >db->escape($group) . "'");   foreach ($data as $key => $value) {     if (!is_array($value)) {
      $this->db->query("INSERT INTO " . DB_PREFIX . "setting SET          store_id = '" . (int)$store_id . "', `group` = '" . $this-
          >db->escape($group) . "', `key` = '" . $this->db-
            >escape($key) . "', `value` = '" . $this->db-
              >escape($value) . "'");
    } else {
    $this->db->query("INSERT INTO " . DB_PREFIX . "setting SET        store_id = '" . (int)$store_id . "', `group` = '" .         $this->db->escape($group) . "', `key` = '" . $this->           db->escape($key) . "', `value` = '" . $this->             db->escape(serialize($value)) . "', serialized = 
              '1'");
    }
  }
}
 
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at  http://www.packtpub.com . If you purchased this book elsewhere, you can visit  http://www.packtpub. com/support and register to have the files e-mailed directly to you.
As given at the controller, $group=helloworld$data is the $_POST, and $store_id is 0. First it deletes all the Hello World settings and then starts to insert the new values. Following are the rows inserted in the setting table of the database:


If the value of the input field of the form is in array, the value is saved with serialized. Thus serialized becomes 1, or else the value of serialized is 0.
The serialize($value), serialize function of PHP generates a storable representation of a value for an array.
http://php.net/manual/en/function.serialize.php

Layouts for the module


OpenCart has default page layouts that are based on the route of the page. Some of the layouts can be found at admin | System | Design | Layouts, and they are as follows:
        Account
        Affiliate
        Category
        Checkout
        Contact
        Default
        Home
        Information
        Manufacturer
        Product
        Sitemap
Now edit one of them, let's consider Account, as shown in the following screenshot:


The value of Route is account; this means that the module will be seen where the route value contains account. If your URL is http://example.com/index. php?route=account/login, the module is shown as route=account. If you want to show the module in the account section, you have to change the layout to Account.
If you like to show the module in affiliate section, you have to choose the Affiliate layout as the route of Affiliate, that is, route=affiliate in the URL.
Similarly, for other layouts, check the route at admin | System | Setting | Design | Layouts | Edit, see the route, and check the URL route; you will find where the module will show on choosing the layout name.

No comments:

Post a Comment