123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\admin\controller\manage;
- use app\common\model\ManageBlock;
- use app\common\controller\Backend;
- use app\common\model\Attachment;
- use app\common\model\Book;
- use app\main\service\SpecialService;
- use think\Controller;
- use think\Exception;
- use think\exception\ErrorException;
- use think\exception\PDOException;
- use think\Request;
- /**
- * 书城管理-资源管理
- *
- * @icon fa fa-circle-o
- */
- class Blockresource extends Backend
- {
- /**
- * ManageBlockResource模型对象
- */
- protected $model = null;
- protected $blockData = [];
- // protected $searchFields = 'block_id';
- protected $relationSearch = true;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('ManageBlockResource');
- $typeList = $this->model->getTypeList();
- $block = model('ManageBlock')->where('id', $this->request->get('block_id'))->find();
- if ($block['type'] != '1') {
- unset($typeList[3]);
- }
- $this->view->assign("typeList", $typeList);
- $this->view->assign("specialList", SpecialService::instance()->getSpecialList()->data);
- $page_id = model('ManageBlock')->where('id','in',$this->request->get('block_id'))->find();
- $blockList = collection(model('ManageBlock')->where('page_id', 'in', $page_id['page_id'])->select())->toArray();
- foreach ($blockList as $k => $v) {
- $this->blockData[$v['id']] = $v['name'];
- }
- $this->view->assign('block_id', $this->request->get('block_id'));
- $this->assignconfig('block_id', $this->request->get('block_id'));
- $this->assignconfig('page_id', $this->request->get('page_id'));
- $this->view->assign('blockData', $this->blockData);
- }
- /**
- * 查看
- */
- public function index()
- {
- if ($this->request->isAjax()) {
- list($where, $sort, $order, $offset, $limit) = $this->buildparams(NULL);
- $total = $this->model
- ->where($where)
- ->with('Book')
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where($where)
- ->with('Book')
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list, "extend" => ['id' => 1]);
- return json($result);
- }
- return $this->view->fetch();
- }
- /*
- * 新增块资源
- */
- public function add()
- {
- if($this->request->isPost()){
- $row = $this->request->post('row/a');
- if($row){
- try{
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
- $this->model->validate($validate);
- }
- $this->model->startTrans();
- //保存资源信息
- if($this->model->allowField(true)->save($row) == false){
- throw new Exception($this->model->getError());
- }
- $row['free_stime'] = 0;
- $row['free_etime'] = 0;
- $this->model->commit();
- $this->success();
- }catch (Exception $e){
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $this->view->assign('page_id', $this->request->get('page_id'));
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = NULL)
- {
- $row = $this->model->get($ids);
- if (!$row)
- $this->error(__('No Results were found'));
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- if (!in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- }
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
- $row->validate($validate);
- }
- $row->startTrans();
- $result = $row->allowField(true)->save($params);
- if($result === false){
- throw new Exception($row->getError());
- }
- $params['free_stime'] = 0;
- $params['free_etime'] = 0;
- $row->commit();
- $this->success();
- }catch (Exception $e){
- $row->rollback();
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- }
|