|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * CakeManager (http://cakemanager.org) |
| 4 | + * Copyright (c) http://cakemanager.org |
| 5 | + * |
| 6 | + * Licensed under The MIT License |
| 7 | + * For full copyright and license information, please see the LICENSE.txt |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright (c) http://cakemanager.org |
| 11 | + * @link http://cakemanager.org CakeManager Project |
| 12 | + * @since 1.0 |
| 13 | + * @license http://www.opensource.org/licenses/mit-license.php MIT License |
| 14 | + */ |
| 15 | +namespace CakeAdmin\Controller; |
| 16 | + |
| 17 | +use Cake\Core\Configure; |
| 18 | +use Cake\Network\Exception\NotFoundException; |
| 19 | +use Cake\Utility\Hash; |
| 20 | +use CakeAdmin\Controller\AppController; |
| 21 | +use Settings\Core\Setting; |
| 22 | + |
| 23 | +/** |
| 24 | + * Settings Controller |
| 25 | + * |
| 26 | + * @property \Settings\Model\Table\SettingsTable $Settings |
| 27 | + */ |
| 28 | +class SettingsController extends AppController |
| 29 | +{ |
| 30 | + |
| 31 | + /** |
| 32 | + * beforeFilter |
| 33 | + * |
| 34 | + * beforeFilter event. |
| 35 | + * |
| 36 | + * @param \Cake\Event\Event $event Event. |
| 37 | + * @return void |
| 38 | + */ |
| 39 | + public function beforeFilter(\Cake\Event\Event $event) |
| 40 | + { |
| 41 | + parent::beforeFilter($event); |
| 42 | + |
| 43 | + $this->loadModel('Settings.Configurations'); |
| 44 | + |
| 45 | + $this->prefixes = Configure::read('Settings.Prefixes'); |
| 46 | + |
| 47 | + $this->Menu->active('ca.settings'); |
| 48 | + $this->Menu->area('navbar'); |
| 49 | + |
| 50 | + foreach ($this->prefixes as $prefix => $alias) { |
| 51 | + $this->Menu->add($alias, [ |
| 52 | + 'url' => [ |
| 53 | + 'action' => 'index', $prefix, |
| 54 | + ] |
| 55 | + ]); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * index action |
| 61 | + * |
| 62 | + * Shows all settings with the specific prefix. |
| 63 | + * |
| 64 | + * @param string $key The prefix. |
| 65 | + * @return void|\Cake\Network\Respose |
| 66 | + * @throws NotFoundException |
| 67 | + */ |
| 68 | + public function index($key = null) |
| 69 | + { |
| 70 | + if (!$key) { |
| 71 | + $key = 'App'; |
| 72 | + } |
| 73 | + |
| 74 | + if (!$this->__prefixExists($key)) { |
| 75 | + throw new NotFoundException("The prefix-setting " . $key . " could not be found"); |
| 76 | + } |
| 77 | + |
| 78 | + $prefix = Hash::get($this->prefixes, ucfirst($key)); |
| 79 | + |
| 80 | + $settings = $this->Configurations->find('all')->where([ |
| 81 | + 'name LIKE' => $key . '%', |
| 82 | + 'editable' => 1, |
| 83 | + ])->order(['weight', 'id']); |
| 84 | + |
| 85 | + if ($this->request->is(['patch', 'post', 'put'])) { |
| 86 | + $settings = $this->Configurations->patchEntities($settings, $this->request->data); |
| 87 | + foreach ($settings as $setting) { |
| 88 | + $this->Flash->success('The settings has been saved.'); |
| 89 | + if (!$this->Configurations->save($setting)) { |
| 90 | + $this->Flash->error('The settings could not be saved. Please, try again.'); |
| 91 | + } |
| 92 | + } |
| 93 | + Setting::clear(true); |
| 94 | + Setting::autoLoad(); |
| 95 | + return $this->redirect([]); |
| 96 | + } |
| 97 | + |
| 98 | + $this->set(compact('prefix', 'settings')); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * _prefixExists |
| 103 | + * |
| 104 | + * Checks if a prefix exists. |
| 105 | + * |
| 106 | + * @param string $prefix The prefix. |
| 107 | + * @return bool |
| 108 | + */ |
| 109 | + private function __prefixExists($prefix) |
| 110 | + { |
| 111 | + if (Hash::get($this->prefixes, ucfirst($prefix)) == null) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + return true; |
| 116 | + } |
| 117 | +} |
0 commit comments