添加新页面到opencart管理员

时间:2015-11-22 16:31:56

标签: php opencart

我正在尝试在opencart的admin部分添加一个新页面,并设法创建所有必要的文件和权限以显示它,但它不会显示任何结果。我正在尝试创建一个页面,我可以添加/编辑/删除我的世界食谱。但是,不是在数据库中显示每个配方的结果,而是显示“没有结果!”请帮助我,我被卡住了。

数据库表:

CREATE TABLE IF NOT EXISTS `oc_recipes` (
  `recipe_id` int(255) NOT NULL AUTO_INCREMENT,
  `item1` int(255) NOT NULL,
  `item2` int(255) NOT NULL,
  `item3` int(255) NOT NULL,
  `item4` int(255) NOT NULL,
  `item5` int(255) NOT NULL,
  `item6` int(255) NOT NULL,
  `item7` int(255) NOT NULL,
  `item8` int(255) NOT NULL,
  `item9` int(255) NOT NULL,
  `product_id` int(11) NOT NULL,
  `sort_order` int(255) NOT NULL,
  PRIMARY KEY (`recipe_id`),
  UNIQUE KEY `product_id` (`product_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

INSERT INTO `oc_recipes` (`recipe_id`, `item1`, `item2`, `item3`, `item4`, `item5`, `item6`, `item7`, `item8`, `item9`, `product_id`, `sort_order`) VALUES (1, 89, 0, 0, 0, 0, 0, 0, 0, 0, 90, 0);

管理员/控制器/目录/ recipe.php:

class ControllerCatalogRecipe extends Controller {
    private $error = array();

    public function index() {
        $this->language->load('catalog/recipe');

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

        $this->load->model('catalog/recipe');

        $this->getList();
    }

    protected function getList() {
    if (isset($this->request->get['sort'])) {
        $sort = $this->request->get['sort'];
    } else {
        $sort = 'name';
    }

    if (isset($this->request->get['order'])) {
        $order = $this->request->get['order'];
    } else {
        $order = 'ASC';
    }

    if (isset($this->request->get['page'])) {
        $page = $this->request->get['page'];
    } else {
        $page = 1;
    }

    $url = '';

    if (isset($this->request->get['sort'])) {
        $url .= '&sort=' . $this->request->get['sort'];
    }

    if (isset($this->request->get['order'])) {
        $url .= '&order=' . $this->request->get['order'];
    }

    if (isset($this->request->get['page'])) {
        $url .= '&page=' . $this->request->get['page'];
    }

    $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('heading_title'),
        'href' => $this->url->link('catalog/recipe', 'token=' . $this->session->data['token'] . $url, 'SSL')
    );

    $data['add'] = $this->url->link('catalog/recipe/add', 'token=' . $this->session->data['token'] . $url, 'SSL');
    $data['delete'] = $this->url->link('catalog/recipe/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');

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

    $filter_data = array(
        'sort'  => $sort,
        'order' => $order,
        'start' => ($page - 1) * $this->config->get('config_limit_admin'),
        'limit' => $this->config->get('config_limit_admin')
    );

    $recipe_total = $this->model_catalog_recipe->getTotalRecipes();

    $results = $this->model_catalog_recipe->getRecipes($filter_data);

    foreach ($results as $result) {
        $data['recipes'][] = array(
            'recipe_id' => $result['recipe_id'],
            'name'            => $this->model_catalog_recipe->getRecipeName($result['product_id']),
            'sort_order'      => $result['sort_order'],
            'edit'            => $this->url->link('catalog/recipe/edit', 'token=' . $this->session->data['token'] . '&recipe_id=' . $result['recipe_id'] . $url, 'SSL')
        );
    }

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

    $data['text_list'] = $this->language->get('text_list');
    $data['text_no_results'] = $this->language->get('text_no_results');
    $data['text_confirm'] = $this->language->get('text_confirm');

    $data['column_name'] = $this->language->get('column_name');
    $data['column_sort_order'] = $this->language->get('column_sort_order');
    $data['column_action'] = $this->language->get('column_action');

    $data['button_add'] = $this->language->get('button_add');
    $data['button_edit'] = $this->language->get('button_edit');
    $data['button_delete'] = $this->language->get('button_delete');

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

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

        unset($this->session->data['success']);
    } else {
        $data['success'] = '';
    }

    if (isset($this->request->post['selected'])) {
        $data['selected'] = (array)$this->request->post['selected'];
    } else {
        $data['selected'] = array();
    }

    $url = '';

    if ($order == 'ASC') {
        $url .= '&order=DESC';
    } else {
        $url .= '&order=ASC';
    }

    if (isset($this->request->get['page'])) {
        $url .= '&page=' . $this->request->get['page'];
    }

    $data['sort_name'] = $this->url->link('catalog/recipe', 'token=' . $this->session->data['token'] . '&sort=name' . $url, 'SSL');
    $data['sort_sort_order'] = $this->url->link('catalog/recipe', 'token=' . $this->session->data['token'] . '&sort=sort_order' . $url, 'SSL');

    $url = '';

    if (isset($this->request->get['sort'])) {
        $url .= '&sort=' . $this->request->get['sort'];
    }

    if (isset($this->request->get['order'])) {
        $url .= '&order=' . $this->request->get['order'];
    }

    $pagination = new Pagination();
    $pagination->total = $recipe_total;
    $pagination->page = $page;
    $pagination->limit = $this->config->get('config_limit_admin');
    $pagination->url = $this->url->link('catalog/recipe', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

    $data['pagination'] = $pagination->render();

    $data['results'] = sprintf($this->language->get('text_pagination'), ($recipe_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($recipe_total - $this->config->get('config_limit_admin'))) ? $recipe_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $recipe_total, ceil($recipe_total / $this->config->get('config_limit_admin')));

    $data['sort'] = $sort;
    $data['order'] = $order;

    $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('catalog/recipe_list.tpl', $data));
    }

}

管理员/语言/英语/目录/ recipe.php:

    // Heading
    $_['heading_title']      = 'Recipes';

    // Text
    $_['text_success']       = 'Success: You have modified a recipe!';
    $_['text_list']          = 'Recipe List';
    $_['text_add']           = 'Add Recipe';
    $_['text_edit']          = 'Edit Recipe';
    $_['text_default']       = 'Default';
    $_['text_percent']       = 'Percentage';
    $_['text_amount']        = 'Fixed Amount';

    // Column
    $_['column_name']        = 'Recipe Name';
    $_['column_sort_order']  = 'Sort Order';
    $_['column_action']      = 'Action';

    // Entry
    $_['entry_name']         = 'Recipe Name';
    $_['entry_store']        = 'Stores';
    $_['entry_keyword']      = 'SEO Keyword';
    $_['entry_image']        = 'Image';
    $_['entry_sort_order']   = 'Sort Order';
    $_['entry_type']         = 'Type';

    // Help
    $_['help_keyword']       = 'Do not use spaces, instead replace spaces with - and make sure the keyword is globally unique.';

    // Error
    $_['error_permission']   = 'Warning: You do not have permission to modify recipes!';
    $_['error_name']         = 'Recipe Name must be between 2 and 64 characters!';
    $_['error_keyword']      = 'SEO keyword already in use!';
    $_['error_product']      = 'Warning: This recipe cannot be deleted as it is currently assigned to %s products!';

管理员/模型/目录/ recipe.php:

class ModelCatalogRecipe extends Model {

    public function getRecipe($recipe_id) {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "recipes");

        return $query->row;
    }

    public function getRecipes($data = array()) {
        $sql = "SELECT * FROM " . DB_PREFIX . "recipes";

        if (!empty($data['filter_name'])) {
            $sql .= " WHERE name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
        }

        $sort_data = array(
            'name',
            'sort_order'
        );

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY name";
        }

        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        return $query->rows;
    }

    public function getRecipeName($product_id) {
        $sql = "SELECT name FROM " . DB_PREFIX . "product_description WHERE product_id = ".$product_id;

        $query = $this->db->query($sql);

        return $query->row['name'];
    }

    public function getTotalRecipes() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "recipes");

        return $query->row['total'];
    }

}

管理员/视图/模板/目录/ recipe_list.tpl:

<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="pull-right"><a href="<?php echo $add; ?>" data-toggle="tooltip" title="<?php echo $button_add; ?>" class="btn btn-primary"><i class="fa fa-plus"></i></a>
    <button type="button" data-toggle="tooltip" title="<?php echo $button_delete; ?>" class="btn btn-danger" onclick="confirm('<?php echo $text_confirm; ?>') ? $('#form-recipe').submit() : false;"><i class="fa fa-trash-o"></i></button>
  </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 } ?>
<?php if ($success) { ?>
<div class="alert alert-success"><i class="fa fa-check-circle"></i> <?php echo $success; ?>
  <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-list"></i> <?php echo $text_list; ?></h3>
  </div>
  <div class="panel-body">
    <form action="<?php echo $delete; ?>" method="post" enctype="multipart/form-data" id="form-manufacturer">
      <div class="table-responsive">
        <table class="table table-bordered table-hover">
          <thead>
            <tr>
              <td style="width: 1px;" class="text-center"><input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /></td>
              <td class="text-left"><?php if ($sort == 'name') { ?>
                <a href="<?php echo $sort_name; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_name; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_name; ?>"><?php echo $column_name; ?></a>
                <?php } ?></td>
              <td class="text-right"><?php if ($sort == 'sort_order') { ?>
                <a href="<?php echo $sort_sort_order; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_sort_order; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_sort_order; ?>"><?php echo $column_sort_order; ?></a>
                <?php } ?></td>
              <td class="text-right"><?php echo $column_action; ?></td>
            </tr>
          </thead>
          <tbody>
            <?php if ($recipes) { ?>
            <?php foreach ($recipes as $recipe) { ?>
            <tr>
              <td class="text-center"><?php if (in_array($recipe['recipe_id'], $selected)) { ?>
                <input type="checkbox" name="selected[]" value="<?php echo $recipe['recipe_id']; ?>" checked="checked" />
                <?php } else { ?>
                <input type="checkbox" name="selected[]" value="<?php echo $recipe['recipe_id']; ?>" />
                <?php } ?></td>
              <td class="text-left"><?php echo $recipe['name']; ?></td>
              <td class="text-right"><?php echo $recipe['sort_order']; ?></td>
              <td class="text-right"><a href="<?php echo $recipe['edit']; ?>" data-toggle="tooltip" title="<?php echo $button_edit; ?>" class="btn btn-primary"><i class="fa fa-pencil"></i></a></td>
            </tr>
            <?php } ?>
            <?php } else { ?>
            <tr>
              <td class="text-center" colspan="4"><?php echo $text_no_results; ?></td>
            </tr>
            <?php } ?>
          </tbody>
        </table>
      </div>
    </form>
    <div class="row">
      <div class="col-sm-6 text-left"><?php echo $pagination; ?></div>
      <div class="col-sm-6 text-right"><?php echo $results; ?></div>
    </div>
  </div>
</div>

- 编辑 -

在View页面上,我打印出$recipes,它只显示array(),所以我猜测问题出现在Controller内,$recipes将数据插入其中。但据我所知,看起来它的设置正确!我很困惑大声笑。

1 个答案:

答案 0 :(得分:1)

您需要在管理面板中启用控制器以显示您的控制器

  

管理员菜单&gt;设置&GT;用户组&gt; 启用您的控制器

之后,您可以按此模式打开新页面

[admin url] /index.php?route=catalog/recipe