如何在 oc 2.0 的自定义模块中自动初始化变量

How to auto initialize variable in custom module in oc 2.0

最近我开始为 opencart 2 开发自定义模块,在一个模块中我需要用户可以增加(添加)和减少(minus/hide)文本字段。我必须使用 jQuery 来执行此操作。在这里您可以找到演示:

http://demo.themextension.com/admin


在 extensions/modules 菜单标题旋转器内容下

所以问题是当我保存它并再次打开时它没有显示使用 jQuery 创建的其他字段。


.tpl 文件

http://pastebin.com/Fs6t2T8j

控制器文件

http://pastebin.com/KF5DG4Ay

所以我不知道如何管理 tpl 文件中的自定义字段

如有任何帮助,我们将不胜感激。谢谢

检查修改后的代码

admin/controller/modules/headlinerotator.php

 <?php
    class ControllerModuleHeadlineRotator extends Controller {
        private $error = array();

        public function index() {
            $this->load->language('module/headlinerotator');

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

            $this->load->model('extension/module');

            if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
                if (!isset($this->request->get['module_id'])) {
                    $this->model_extension_module->addModule('headlinerotator', $this->request->post);
                } else {
                    $this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post);
                }

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

                $this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
            }

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

            $data['text_edit'] = $this->language->get('text_edit');
            $data['text_enabled'] = $this->language->get('text_enabled');
            $data['text_disabled'] = $this->language->get('text_disabled');

            $data['entry_name'] = $this->language->get('entry_name');
            $data['entry_title'] = $this->language->get('entry_title');
            $data['entry_description'] = $this->language->get('entry_description');
            $data['entry_description1'] = $this->language->get('entry_description1');
            $data['entry_status'] = $this->language->get('entry_status');

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

            if (isset($this->error['warning'])) {
                $data['error_warning'] = $this->error['warning'];
            } else {
                $data['error_warning'] = '';
            }

            if (isset($this->error['name'])) {
                $data['error_name'] = $this->error['name'];
            } else {
                $data['error_name'] = '';
            }

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

            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
            );

            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('text_module'),
                'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')
            );

            if (!isset($this->request->get['module_id'])) {
                $data['breadcrumbs'][] = array(
                    'text' => $this->language->get('heading_title'),
                    'href' => $this->url->link('module/headlinerotator', 'token=' . $this->session->data['token'], 'SSL')
                );
            } else {
                $data['breadcrumbs'][] = array(
                    'text' => $this->language->get('heading_title'),
                    'href' => $this->url->link('module/headlinerotator', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], 'SSL')
                );         
            }

            if (!isset($this->request->get['module_id'])) {
                $data['action'] = $this->url->link('module/headlinerotator', 'token=' . $this->session->data['token'], 'SSL');
            } else {
                $data['action'] = $this->url->link('module/headlinerotator', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], 'SSL');
            }

            $data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');

            if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
                $module_info = $this->model_extension_module->getModule($this->request->get['module_id']);
            }

            if (isset($this->request->post['name'])) {
                $data['name'] = $this->request->post['name'];
            } elseif (!empty($module_info)) {
                $data['name'] = $module_info['name'];
            } else {
                $data['name'] = '';
            }
           // echo "<pre>"; print_r($module_info); exit;
            if (isset($this->request->post['module_description'])) {
                $data['module_description'] = $this->request->post['module_description'];
                $data['headline_row']= count($this->request->post['module_description']);
            } elseif (!empty($module_info)) {
                $data['module_description'] = $module_info['module_description'];
                $data['headline_row']= count($module_info['module_description']);
            } else {
                $data['module_description'] = '';
                $data['headline_row']= 0;
            }

            $this->load->model('localisation/language');

            $data['languages'] = $this->model_localisation_language->getLanguages();

            if (isset($this->request->post['status'])) {
                $data['status'] = $this->request->post['status'];
            } elseif (!empty($module_info)) {
                $data['status'] = $module_info['status'];
            } else {
                $data['status'] = '';
            }

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

            $this->response->setOutput($this->load->view('module/headlinerotator.tpl', $data));
        }

        protected function validate() {
            if (!$this->user->hasPermission('modify', 'module/html')) {
                $this->error['warning'] = $this->language->get('error_permission');
            }

            if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {
                $this->error['name'] = $this->language->get('error_name');
            }

            return !$this->error;
        }
    }

headlinerotator.tpl

    <?php echo $header; ?><?php echo $column_left; ?>
    <div id="content">
      <div class="page-header">
        <div class="container-fluid">
          <div class="pull-right">
            <button type="submit" form="form-html" data-toggle="tooltip" title="<?php echo $button_save; ?>" class="btn btn-primary"><i class="fa fa-save"></i></button>
            <a href="<?php echo $cancel; ?>" data-toggle="tooltip" title="<?php echo $button_cancel; ?>" class="btn btn-default"><i class="fa fa-reply"></i></a></div>
          <h1><?php echo $heading_title; ?></h1>
          <ul class="breadcrumb">
            <?php foreach ($breadcrumbs as $breadcrumb) { ?>
            <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
            <?php } ?>
          </ul>
        </div>
      </div>
      <div class="container-fluid">
        <?php if ($error_warning) { ?>
        <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
          <button type="button" class="close" data-dismiss="alert">&times;</button>
        </div>
        <?php } ?>
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-pencil"></i> <?php echo $text_edit; ?></h3>
          </div>
          <div class="panel-body">
            <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-html" class="form-horizontal">
              <div class="form-group">
                <label class="col-sm-2 control-label" for="input-name"><?php echo $entry_name; ?></label>
                <div class="col-sm-10">
                  <input type="text" name="name" value="<?php echo $name; ?>" placeholder="<?php echo $entry_name; ?>" id="input-name" class="form-control" />
                  <?php if ($error_name) { ?>
                  <div class="text-danger"><?php echo $error_name; ?></div>
                  <?php } ?>
                </div>
              </div>        
              <div class="tab-pane">
                <ul class="nav nav-tabs" id="language">
                  <?php foreach ($languages as $language) { ?>
                  <li><a href="#language<?php echo $language['language_id']; ?>" data-toggle="tab"><img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" /> <?php echo $language['name']; ?></a></li>
                  <?php } ?>
                </ul>
                <div class="tab-content">
                  <?php foreach ($languages as $language) { ?>
                  <div class="tab-pane" id="language<?php echo $language['language_id']; ?>">
                    <div class="form-group">
                      <label class="col-sm-2 control-label" for="input-title<?php echo $language['language_id']; ?>"><?php echo $entry_title; ?></label>
                      <div class="col-sm-10">
                        <input type="text" name="module_description[0][<?php echo $language['language_id']; ?>][title]" placeholder="<?php echo $entry_title; ?>" id="input-heading<?php echo $language['language_id']; ?>" value="<?php echo isset($module_description[0][$language['language_id']]['title']) ? $module_description[0][$language['language_id']]['title'] : ''; ?>" class="form-control" />
                      </div>
                    </div>
                    <div class="form-group">
                      <label class="col-sm-2 control-label" for="input-description<?php echo $language['language_id']; ?>"><?php echo $entry_description; ?></label>
                      <div class="col-sm-8">
                        <textarea name="module_description[0][<?php echo $language['language_id']; ?>][description]" placeholder="<?php echo $entry_description; ?>" id="input-description<?php echo $language['language_id']; ?>" class="form-control summernote"><?php echo isset($module_description[0][$language['language_id']]['description']) ? $module_description[0][$language['language_id']]['description'] : ''; ?></textarea>
                      </div>
                      <div class="col-sm-2">
                        <a id="plusbtn" class="btn btn-danger" ><i class="fa fa-plus"></i></a>
                      </div>
                    </div>
                    <?php foreach($module_description as $key=>$details): ?> 
                      <?php if($key > 0) : ?>
                      <div class="form-group">
                      <label class="col-sm-2 control-label" for="input-description<?php echo $language['language_id']; ?>"><?php echo $entry_description; ?></label>
                      <div class="col-sm-8">
                        <textarea name="module_description[<?php echo $key; ?>][<?php echo $language['language_id']; ?>][description]" placeholder="<?php echo $entry_description; ?>" id="input-description<?php echo $language['language_id']; ?>" class="form-control summernote"><?php echo isset($details[$language['language_id']]['description']) ? $details[$language['language_id']]['description'] : ''; ?></textarea>
                      </div>
                      <div class="col-sm-2">
                        <a class='btn btn-danger minusbtn' onclick='minusdis()'><i class='fa fa-minus'></i></a>
                      </div>
                    </div>
                      <?php endif; ?>
                    <?php endforeach; ?>
                  </div>
                  <?php } ?>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label" for="input-status"><?php echo $entry_status; ?></label>
                <div class="col-sm-10">
                  <select name="status" id="input-status" class="form-control">
                    <?php if ($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>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
      <script type="text/javascript"><!--
    <?php foreach ($languages as $language) { ?>
    $('.summernote').summernote({height: 100});
    <?php } ?>

     //function addHeadline(){ alert('asdf');
     var headline_row = <?php echo $key; ?>;

    $('#plusbtn').click(function(){ 
      headline_row++;
        // var token = Math.random().toString(36).substr(2);
        html = "<div class='form-group'>";
        html += " <label class='col-sm-2 control-label' for='input-description"+headline_row+"<?php echo $language['language_id']; ?>'><?php echo $entry_description; ?></label>";
        html += "   <div class='col-sm-8'>";
        html += "       <textarea name='module_description["+headline_row+"][<?php echo $language['language_id']; ?>][description]' placeholder='<?php echo $entry_description; ?>' id='input-description"+headline_row+"<?php echo $language['language_id']; ?>' class='form-control summernote'></textarea>";
        html += "            </div>";
        html += "         <div class='col-sm-2'>";
        html += "       <a class='btn btn-danger minusbtn' onclick='minusdis()'><i class='fa fa-minus'></i></a>";
        html += "         </div>";
        html += "           </div>";


        $('#input-status').parents('.form-group').before(html);
    });
  // }

    function minusdis() {
        $('.minusbtn').click(function(){
            $(this).parents('.form-group').remove();
        });
    }
    //--></script>

      <script type="text/javascript"><!--
    $('#language a:first').tab('show');
    //--></script></div>
    <?php echo $footer; ?>