index > technolog > php > project - opencart


2017-10-07

2018-02-06

用户组权限设置

从控制器目录读取所有模块
foreach ($files as $file) {
    $controller = substr($file, strlen(DIR_APPLICATION . 'controller/'));
    $permission = substr($controller, 0, strrpos($controller, '.'));
    if (!in_array($permission, $ignore)) {
        $data['permissions'][] = $permission;
    }
}

保存json数据到permission字段
$name = $this->db->escape($data['name']);
$permission = (isset($data['permission']) ? $this->db->escape(json_encode($data['permission'])) : ''; 
$sql = "UPDATE oc_user_group .
        SET name = '$name', permission = '$permission'" . 
        WHERE user_group_id = $user_group_id";
$this->db->query($sql);

从数据库中直接以数组的形式读取
function getUserGroup($user_group_id) {
    $query = $this->db->query("SELECT DISTINCT * FROM oc_user_group WHERE user_group_id = $user_group_id");
    $user_group = array(
        'name'       => $query->row['name'],
        'permission' => json_decode($query->row['permission'], true)
    );
    return $user_group;
}
$user_group_info = $this->model_user_user_group->getUserGroup($this->request->get['user_group_id']);
$data['access'] = $user_group_info['permission']['access'];

tpl设置
<?php foreach($permissions as $permission) { ?>
<input type="checkbox" name="permission[modify][]" value="<?php echo $permission; ?>" />
<?php } ?>

2017-09-20

保存扩展模块配置

admin/module/controller/newsblog_articles.php:
    if (!isset($this->request->get['module_id'])) {
        $this->model_extension_module->addModule('newsblog_articles', $this->request->post);
    } else {
        $this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post);
    }

admin/model/extension/module.php:
	public function addModule($code, $data) {
		$this->db->query("INSERT INTO `" . DB_PREFIX . "module` SET `name` = '" . $this->db->escape($data['name']) . "', `code` = '" . $this->db->escape($code) . "', `setting` = '" . $this->db->escape(json_encode($data)) . "'");
	}
在数据表oc_module.setting中保存扩展模块中的参数数据,数据来自表单中的输入值
+-----------+------------+----------+----------------------------------------------------------------------------------+
| module_id | name       | code     | setting                                                                          |
+-----------+------------+----------+----------------------------------------------------------------------------------+
|        29 | Home Page  | carousel | {"name":"Home Page","banner_id":"8","width":"130","height":"100","status":"1"}   |
|        41 | store show | carousel | {"name":"store show","banner_id":"13","width":"230","height":"150","status":"1"} |
+-----------+------------+----------+----------------------------------------------------------------------------------+

判断用户是否登录的方法

if (isset($this->request->get['token']) && 
	isset($this->session->data['token']) && 
	($this->request->get['token'] == $this->session->data['token'])) {}

if ($this->user->isLogged() && 
	isset($this->request->get['token']) && 
	($this->request->get['token'] == $this->session->data['token'])) {}

多语言显示信息

$this->load->language('common/login');	加载一个语言目录
$this->language->get('error_login');	检索语言对应的文字
多语言的文字检索是先在控制器中完成,加载到模板中显示

MVC机制

index.php?route=common/login
系统加载控制器:/catelog/controller/ControllerCommonLogin,并调用index()方法
在控制器中设置数据$data, 加载view模板, 并显示网页
$this->response->setOutput($this->load->view('common/login.tpl', $data));
模板目录在:/catelog/view/template/common/

主页是如何检索布局layout_id

依路由$route来判断:
1.如果是'product/category', 则layout_id = getID('product/category');
2.如果是'product/product', 则layout_id = getID('category/product');
3.如果是'information/information', 则layout_id = getID('catalog/information');
4.其它layout_id = getID($route)
  select layout_id from layout_route where route like $route and store_id = $store_id;
5.取出layout_id对应的模组(有多个模组)
  select * from layout_module where layout_id = $layout_id and position = 'left'
6.加载相应的模组(模组定义高度和宽度,显示条目的数量)
  加载相应的模板文件:template\module下
  \catalog\view\theme\default\template\module\special.tpl
  layout_module.code: 如special.34, 34是module.module_id, special.tpl可能会有多个module的配置
7.模组的信息是来自\catalog\controller\module\special.php
  名称来自:catalog\language\chinese\module\special.php
  $_['heading_title'] = '优惠商品';

select * from oc_layout_route where 'common/home' like route;
select * from oc_layout where layout_id = 1;
select * from oc_layout_module where layout_id = 1;		
select * from oc_moduele where code = 'special';

/system/engine/			基本类
/system/library/		购物功能类,由/system/startup.php中spl_autoload_register('autoload')加载

主页的加载过程:
/catalog/controller/common/home.php		取数据
/catalog/view/theme/default/template/common/home.tpl  调用主页模板并显示

/system/logs/error.log	错误信息文件

index.php加载过程

1.加载config.php:
  加载路径和数据库设置
2.加载startup.php
  解析服务请求,
  自动加载模块spl_autoload_register.(\system\library)
  加载基本模块(\system\engine目录下)

// Front Controller
$controller = new Front($registry);

// Maintenance Mode
$controller->addPreAction(new Action('common/maintenance'));

// SEO URL's
$controller->addPreAction(new Action('common/seo_url'));

// Router
if (isset($request->get['route'])) {
	$action = new Action($request->get['route']);
} else {
	$action = new Action('common/home');
}

// Dispatch
$controller->dispatch($action, new Action('error/not_found'));

// Output
$response->output();