Blog

  •  

    Open Cart Coding Methods

    Create a Custom Controller:

    Today, we can implement a basic functionality demonstrating concepts of custom controllers. In the course of doing that, we can build the interface in front-end allowing certain guest users to submit their response by entering a name and message. Before proceeding, be assured that you have a working installation and Development in OpenCart. As it is pretty much the same, you can start applying through the Guestbook feature.

    For those who are not familiar with an OpenCart arrangement, you can always look at the front-end controller comprising of the catalog/controller. The directory manages all controllers group wise, based on certain provided functionality.

    In this particular case, creation and separation of the group called a guestbook is ideal. Go ahead and create your directory catalog/controller/guestbook. Within a directory, create the entry.php file with preceding content. Using controller file handling application logic as well as submission logic, use of Guestbook functionality carries out.

    Open Cart Code Practice

    <?php

    class ControllerGuestbookEntry extends Controller {

      private $error = array();

      public function index() {

        $this->load->language('guestbook/guestbook');

        $this->document->setTitle($this->language->get('heading_title'));

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {

          $this->load->model('guestbook/guestbook');

          $data['subject'] = sprintf('New guestbook entry submitted by %s', $this->request->post['guest_name']);

          $data['message'] = $this->request->post['guest_message'];

          $this->model_guestbook_guestbook->processGuestbookEntry($data);

          $this->session->data['success'] = $this->language->get('text_success');

          $this->response->redirect($this->url->link('guestbook/entry', '', true));

        }

        $data['success'] = '';

        if (isset($this->session->data['success'])) {

          $data['success'] = $this->session->data['success'];

          unset($this->session->data['success']);

        }

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(

          'text' => $this->language->get('text_home'),

          'href' => $this->url->link('common/home')

        );

        $data['breadcrumbs'][] = array(

          'text' => $this->language->get('heading_title'),

          'href' => $this->url->link('guestbook/entry', '', true)

        );

        $data['heading_title'] = $this->language->get('heading_title');

        $data['entry_guest_name'] = $this->language->get('entry_guest_name');

        $data['entry_guest_message'] = $this->language->get('entry_guest_message');

        $data['entry_submit'] = $this->language->get('entry_submit');

        if (isset($this->error['guest_name'])) {

          $data['error_guest_name'] = $this->error['guest_name'];

        } else {

          $data['error_guest_name'] = '';

        }

        if (isset($this->error['guest_message'])) {

          $data['error_guest_message'] = $this->error['guest_message'];

        } else {

          $data['error_guest_message'] = '';

        }

        $data['action'] = $this->url->link('guestbook/entry', '', true);

        if (isset($this->request->post['guest_name'])) {

          $data['guest_name'] = $this->request->post['guest_name'];

        } else {

          $data['guest_name'] = '';

        }

        if (isset($this->request->post['guest_message'])) {

          $data['guest_message'] = $this->request->post['guest_message'];

        } else {

          $data['guest_message'] = '';

        }

        $data['column_left'] = $this->load->controller('common/column_left');

        $data['column_right'] = $this->load->controller('common/column_right');

        $data['content_top'] = $this->load->controller('common/content_top');

        $data['content_bottom'] = $this->load->controller('common/content_bottom');

        $data['footer'] = $this->load->controller('common/footer');

        $data['header'] = $this->load->controller('common/header');

        $this->response->setOutput($this->load->view('guestbook/entry', $data));

      }

      protected function validate() {

        if (utf8_strlen(trim($this->request->post['guest_name'])) < 1) {

          $this->error['guest_name'] = $this->language->get('error_guest_name');

        }

        if (utf8_strlen(trim($this->request->post['guest_message'])) < 1) {

          $this->error['guest_message'] = $this->language->get('error_guest_message');

        }

        return !$this->error;

      }

    }

    Code for Open Cart

    Each controller class provides indexed method handling where most logic of the controller is used. Next, you can search through certain codes in your indexing method, and create specific files. Most of the time, we can begin by inculcating your language file for a specific group. It is the way OpenCart manages static language labels through your application. Of course, it makes usage of multiple language-operated sites beneficial and one you can manage.

    $this->load->language('guestbook/guestbook');

    Before proceeding, let creation of the corresponding language file be so that the controller recovers it. Create a catalog/language/en-gb/guestbook/guestbook.php file with following contents.

    <?php

    // Heading

    $_['heading_title']  = 'Guestbook';

    // Entry

    $_['entry_guest_name']   = 'Your Name';

    $_['entry_guest_message']   = 'Message';

    $_['entry_submit']   = 'Submit';

    $_['text_success']   = 'Success: Your entry has been successfully submitted.';

    // Error

    $_['error_guest_name'] = 'Please enter Your Name!';

    $_['error_guest_message'] = 'Please enter Message!';

    As you realize, we just need assign labels with certain values in a language array. Coming to a controller, the following aspect is to set up an HTML title tag for your page.

    $this->document->setTitle($this->language->get('heading_title'));

    User conception has notification to use with a heading_title language variable defined in the language file created previously. In order to understand the following piece of code, you can create a stereotypical file. Therefore, for the moment, you can come down to the model file at catalog/model/guestbook/guestbook.php with these contents.

    <?php

    class ModelGuestbookGuestbook extends Model {

      public function processGuestbookEntry($data) {

        // send email notification to store admin

        $mail = new Mail();

        $mail->protocol = $this->config->get('config_mail_protocol');

        $mail->parameter = $this->config->get('config_mail_parameter');

        $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');

        $mail->smtp_username = $this->config->get('config_mail_smtp_username');

        $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');

        $mail->smtp_port = $this->config->get('config_mail_smtp_port');

        $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');

        $mail->setTo($this->config->get('config_email'));

        $mail->setFrom($this->config->get('config_email'));

        $mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));

        $mail->setSubject($data['subject']);

        $mail->setText($data['message']);

        $mail->send();

      }

    }

Comments

  • (no comments)

This free website is created and hosted by Website.com's Site Builder.