Plugin Developer Guide

Plugin directory structure

Example: Your plugin name is "yourplugin"
yourplugin_install.zip (For plugin install file)
yourplugin_upgrade.zip (For plugin upgrade file)

||- assets
    ||- plugin
        ||- yourplugin (For your plugin assets)
            ||- css
            ||- js
            ||- imgs

||- cszcms
    ||- config
        ||- plugin
            |o- yourplugin.php (For plugin config file)

    ||- controllers (For backend plugin. If you don't have backend please ignore this directory)
        ||- admin
            ||- plugin
                |o- Yourplugin.php (For your plugin controller on backend)

    ||- language (For language file to use on backend. In example has only english. If you don't have backend please ignore this directory)
        ||- english
            ||- plugin
                |o- yourplugin_lang.php

    ||- models
        ||- plugin
            |o- Yourplugin_model.php (For you plugin models function)

    ||- modules (For frontend plugin. If you don't have frontend please ignore this directory)
        ||- plugin
            ||- controllers
                |o- Yourplugin.php (For your plugin controller on frontend)

            ||- views
                ||- yourplugin (For your plugin views file on frontend)
                    |o- home.php (Example)

    ||- views (For backend plugin. If you don't have backend please ignore this directory)
        ||- admin
            ||- plugin
                ||- yourplugin (For your plugin views file on backend)
                    |o- home.php (Example)

||- photo
    ||- plugin
        ||- yourplugin (For your plugin photo storage)

||- plugin_sql (For plugin install file. Include in "yourplugin_install.zip" only)
    |o- install.sql (For your plugin sql install file. Please don't change this filename)

||- upgrade_sql (For plugin upgrade file. Include in "yourplugin_upgrade.zip" only)
    |o- upgrade.sql (For your plugin sql upgrade file. Please don't change this filename)

Install sql file (/plugin_sql/install.sql)

/* Important SQL for plugin */
INSERT INTO `plugin_manager` VALUES ('', 'yourplugin', 0, NOW(), NOW()); /* Your plugin config filename */
INSERT INTO `general_label` (`general_label_id`, `name`, `remark`, `lang_en`, `timestamp_update`) VALUES ('', 'YOUR_FRONTEND_LABEL_NAME', 'YOUR_LABEL_REMAK', 'YOUR_LABEL_TEXT', NOW()); /* Your plugin label for frontend */
INSERT INTO `user_perms` (`user_perms_id`, `name`, `definition`, `permstype`) VALUES
('', 'YOUR_BACKEND_PLUGIN_NAME', 'PERMISSION_REMARK', 'backend'), /* For your plugin permission on backend */
('', 'YOUR_FRONTEND_PLUGIN_NAME', 'PERMISSION_REMARK', 'frontend'); /* For your plugin permission on frontend */

/* Your custom SQL for plugin */
.... Your plugin sql (Please use your plugin name to table prefix. Example: yourplugin_table) ....


Plugin config file (/cszcms/config/plugin/yourplugin.php)

<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/* For your plugin config.
 * Importnt! Please Don't change the config item index name (for systems config)
 * 
 * For load plugin config:
 * $this->Csz_model->getPluginConfig('plugin_config_filename', 'item index name');
 * Ex. $this->Csz_model->getPluginConfig('article', 'plugin_name');
 * 
 */

/* Start System Config (Important) */
/* General Config */
$plugin_config['plugin_name']  = 'Yourplugin name';
$plugin_config['plugin_urlrewrite']  = 'yourplugin'; /* Please don't have any blank space */
$plugin_config['plugin_author']  = 'Your Name'; /* For your name */
$plugin_config['plugin_version']   = '1.0.1'; /* Your plugin version x.x.1 to x.9.9 */
$plugin_config['plugin_description']   = 'Your plugin description'; /* For your plugin description */

/* for menu inside member zone. If not have please blank. 
 * Example: $plugin_config['plugin_member_menu'] = 'link_name';
 * The link automatic to {base_url}/plugin/{your_plugin_urlrewrite}
 * plugin_menu_permission_name is permission name from user_perms table on DB
 */
$plugin_config['plugin_member_menu'] = '';
$plugin_config['plugin_menu_permission_name'] = '';

/* Database Config */
$plugin_config['plugin_db_table']   = array(
    'Yourplugin_table',
); /* Please input all your pludin db table name */

/* Sitemap Generater Config (for content view page only) 
 * If don't want to use sitemap for your plugin. Please blank.
 */
$plugin_config['plugin_sitemap_viewtable']   = '';
/* for sitemap sql extra condition for this view table. If not have please blank. */
$plugin_config['plugin_sqlextra_condition']   = "";

/* Sitemap Generater Config (for content category page only) 
 * If don't want to use sitemap for your plugin. Please blank.
 */
$plugin_config['plugin_sitemap_cattable']   = '';
/* for sitemap sql extra condition for this category table. If not have please blank. */
$plugin_config['plugin_sqlextra_catcondition']   = "";

/* All your plugin file path 
 * For directory please put / into the end of path.
 * Filename or Directory name is case sensitive.
 */
$plugin_config['plugin_file_path']   = array(
    FCPATH . '/photo/plugin/yourplugin/',
    FCPATH . '/cszcms/config/plugin/yourplugin.php',
    FCPATH . '/cszcms/controllers/admin/plugin/Yourplugin.php',
    FCPATH . '/cszcms/models/plugin/Yourplugin_model.php',
    FCPATH . '/cszcms/modules/plugin/controllers/Yourplugin.php',
    FCPATH . '/cszcms/modules/plugin/views/yourplugin/',
    FCPATH . '/cszcms/views/admin/plugin/yourplugin/',
);
/* End System Config (Important) */

/* Custom config (For your plugin config)
 * Please add your config after this section
 */

For plugin controller on backend (/cszcms/controllers/admin/plugin/Yourplugin.php)

This is Example for plugin controller on backend
<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Yourplugin extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('file');
        define('LANG', $this->Csz_admin_model->getLang());
        $this->lang->load('admin', LANG);
        $this->lang->load('plugin/shop', LANG);
        $this->template->set_template('admin');
        $this->load->model('plugin/Yourplugin_model');
        $this->_init();
        admin_helper::plugin_not_active('yourpluginconfig_filename');
    }

    public function _init() {
        $row = $this->Csz_admin_model->load_config();
        $pageURL = $this->Csz_admin_model->getCurPages();
        $this->template->set('core_css', $this->Csz_admin_model->coreCss());
        $this->template->set('core_js', $this->Csz_admin_model->coreJs());
        $this->template->set('title', 'Backend System | ' . $row->site_name);
        $this->template->set('meta_tags', $this->Csz_admin_model->coreMetatags('Backend System for CSZ Content Management'));
        $this->template->set('cur_page', $pageURL);
    }

    public function index() {
        admin_helper::is_logged_in($this->session->userdata('admin_email')); /* Check is logged in */
        admin_helper::is_allowchk('YOUR_PLUGIN_PERMISSION_NAME_BACKEND'); /* Check permission */
        $this->csz_referrer->setIndex('Your_plugin_name'); /* Set index page to redirect after save */
        $this->template->setSub('VARIABLE NAME', 'YOUR VALUE'); /* Send the value to view file  */
        $this->template->loadSub('admin/plugin/yourplugin/home'); /* Load view file. Please see in plugin directory structure */
    }

    public function save() {
        admin_helper::is_logged_in($this->session->userdata('admin_email')); /* Check is logged in */
        admin_helper::is_allowchk('YOUR_PLUGIN_PERMISSION_NAME_BACKEND'); /* Check permission */
        admin_helper::is_allowchk('save'); /* Check permission for save */
        /* Your code */
        $this->db->cache_delete_all(); /* clear db cache */
        $this->session->set_flashdata('error_message', '<div class="alert alert-success" role="alert">' . $this->lang->line('success_message_alert') . '</div>'); /* For success message alert */
        redirect($this->csz_referrer->getIndex('Your_plugin_name'), 'refresh'); /* Get index page to redirect after save */
    }

    public function delete() {
        admin_helper::is_logged_in($this->session->userdata('admin_email')); /* Check is logged in */
        admin_helper::is_allowchk('YOUR_PLUGIN_PERMISSION_NAME_BACKEND'); /* Check permission */
        admin_helper::is_allowchk('delete'); /* Check permission for delete */
        /* Your code */
        $this->db->cache_delete_all(); /* clear db cache */
        $this->session->set_flashdata('error_message', '<div class="alert alert-success" role="alert">' . $this->lang->line('success_message_alert') . '</div>'); /* For success message alert */
        redirect($this->csz_referrer->getIndex('Your_plugin_name'), 'refresh'); /* Get index page to redirect after delete */
    }

For plugin controller on frontend (/cszcms/modules/plugin/controllers/Yourplugin.php)

This is Example for plugin controller on frontend
<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Yourplugin extends CI_Controller {

    var $page_url;

    function __construct() {
        parent::__construct();
        $this->CI = & get_instance();
        $this->load->database();
        $row = $this->Csz_model->load_config();
        $this->load->model('plugin/Yourplugin_model');
        if ($row->themes_config) {
            $this->template->set_template($row->themes_config);
            define('THEME', $row->themes_config);
        }
        if (!$this->session->userdata('fronlang_iso')) {
            $this->Csz_model->setSiteLang();
        }
        if ($this->Csz_model->chkLangAlive($this->session->userdata('fronlang_iso')) == 0) {
            $this->session->unset_userdata('fronlang_iso');
            $this->Csz_model->setSiteLang();
        }
        $this->_init();
        member_helper::plugin_not_active('yourpluginconfig_filename');
    }

    public function _init() {
        $this->template->set('core_css', $this->Csz_model->coreCss());
        $this->template->set('core_js', $this->Csz_model->coreJs());
        $row = $this->Csz_model->load_config();
        $this->page_url = $this->Csz_model->getCurPages();
        $this->template->set('additional_js', $row->additional_js);
        $this->template->set('additional_metatag', $row->additional_metatag);
    }

    public function index() {
        Member_helper::is_logged_in($this->session->userdata('admin_email')); /* Check is logged in. If you want it */
        Member_helper::is_allowchk('YOUR_PLUGIN_PERMISSION_NAME_FRONTEND'); /* Check permission */
        $this->csz_referrer->setIndex('Your_plugin_name');
        $row = $this->Csz_model->load_config();
        $title = 'Your Plugin | ' . $row->site_name;
        $this->template->set('title', $title);
        $this->template->set('meta_tags', $this->Csz_model->coreMetatags($title, $row->keywords, $title));
        $this->template->set('cur_page', $this->page_url);
        $this->template->setSub('VARIABLE NAME', 'YOUR VALUE'); /* Send the value to view file  */
        $this->template->loadSub('yourplugin/home'); /* Load view file. Please see in plugin directory structure */
    }

Example: Shop plugin gitlab

This is Example for shop plugin on gitlab
https://gitlab.com/cszcms/cszcms-shop




Copyright © 2018 CSZ CMS Website by CSKAZA