Beispiel #1
0
 protected function initData()
 {
     $menus = \Cake\Cache\Cache::read('admin_menus');
     if ($menus === false) {
         $Menu = \Cake\ORM\TableRegistry::get('cwp_menu');
         $menus = $Menu->find()->hydrate(false)->where('status = 1')->orderDesc('rank')->toArray();
         $menus = \Admin\Utils\Util::getMenu($menus);
         \Cake\Cache\Cache::write('admin_menus', $menus);
     }
     $this->_menus = $menus;
     $controller = strtolower($this->request->param('controller'));
     $action = strtolower($this->request->param('action'));
     $url = '/admin/' . $controller . '/' . $action;
     $this->_url = $url;
     $active = null;
     foreach ($menus as $value) {
         if (isset($value['child'])) {
             foreach ($value['child'] as $sub_menu) {
                 if ($sub_menu['node'] == $url) {
                     $this->_pageTitle = $sub_menu['name'];
                     $this->_breadFirst = ['name' => $value['name'], 'node' => $value['node']];
                     $this->_breadSecond = ['name' => $sub_menu['name'], 'node' => $sub_menu['node']];
                     break;
                 }
                 if (isset($sub_menu['child'])) {
                     foreach ($sub_menu['child'] as $v) {
                         if ($v['node'] == $url) {
                             $active = $sub_menu['node'];
                             $this->_active = $active;
                             $this->_pageTitle = $v['name'];
                             $this->_breadFirst = ['name' => $value['name'], 'node' => $value['node']];
                             $this->_breadSecond = ['name' => $sub_menu['name'], 'node' => $sub_menu['node']];
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * Edit method
  *
  * @param string|null $id Menu id.
  * @return void Redirects on successful edit, renders view otherwise.
  * @throws \Cake\Network\Exception\NotFoundException When record not found.
  */
 public function edit($id = null)
 {
     $menu = $this->Menu->get($id);
     if ($this->request->is(['post', 'put'])) {
         $this->autoRender = false;
         $this->response->type('json');
         $menu = $this->Menu->patchEntity($menu, $this->request->data);
         if ($this->Menu->save($menu)) {
             echo json_encode(array('status' => true, 'msg' => '修改成功'));
         } else {
             $errors = $menu->errors();
             echo json_encode(array('status' => false, 'msg' => getMessage($errors)));
         }
         return;
     }
     $menus = $this->Menu->find()->hydrate(false)->all()->toArray();
     $menus = \Admin\Utils\Util::tree($menus, 0, 'id', 'pid');
     $this->set(array('menu' => $menu, 'menus' => $menus));
 }