Skip to content

Commit 594bb0f

Browse files
committed
Settings Plugin implementation
1 parent 43ded6f commit 594bb0f

File tree

6 files changed

+289
-2
lines changed

6 files changed

+289
-2
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"php": ">=5.4.16",
77
"cakephp/cakephp": "~3.0",
88
"cakemanager/cakephp-utils": "dev-master",
9-
"cakemanager/cakephp-notifier": "dev-master"
9+
"cakemanager/cakephp-notifier": "dev-master",
10+
"cakemanager/cakephp-settings": "dev-master"
1011
},
1112
"require-dev": {
1213
"phpunit/phpunit": "*"

config/bootstrap.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
*/
1515
use Cake\Core\Configure;
1616
use Cake\Core\Plugin;
17+
use Settings\Core\Setting;
1718

1819
Plugin::load('Utils', []);
20+
Plugin::load('Settings', ['bootstrap' => true, 'routes' => true]);
1921
Plugin::load('Notifier', ['bootstrap' => true, 'routes' => true]);
2022

2123
Configure::write('Session.timeout', 4320);
@@ -31,8 +33,11 @@
3133
'password' => 'password'
3234
]);
3335

36+
Configure::write('Settings.Prefixes.CA', 'CakeAdmin');
37+
3438
Configure::write('CA.PostTypes', []);
3539

3640
Configure::write('CA.Models.administrators', 'CakeAdmin.Administrators');
3741

38-
42+
//Settings
43+
Setting::register('App.Name', 'CakeAdmin Application');

src/Controller/AppController.php

+11
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ public function initMenuItems()
141141
'action' => 'index',
142142
]
143143
]);
144+
145+
$this->Menu->add('ca.settings', [
146+
'title' => __('Settings'),
147+
'url' => [
148+
'prefix' => false,
149+
'plugin' => 'CakeAdmin',
150+
'controller' => 'Settings',
151+
'action' => 'index',
152+
],
153+
'weight' => 50
154+
]);
144155
}
145156

146157
}

src/Controller/SettingsController.php

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}

src/Template/Settings/index.ctp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<h3><?= $prefix ?></h3>
2+
3+
<?= $this->Menu->menu('navbar', 'CakeAdmin.NavbarMenu') ?>
4+
5+
<?php
6+
echo $this->Form->create();
7+
8+
foreach ($settings as $id => $setting) {
9+
10+
echo $this->Form->input($id . '.id', [
11+
'type' => 'hidden',
12+
'value' => $setting->id,
13+
]);
14+
15+
$name = explode('.', $setting->name);
16+
17+
echo $this->Form->input($id . '.value', [
18+
'type' => (($setting->type) ? $setting->type : 'text'),
19+
'label' => ucfirst(end($name)) . (($setting->description) ? ' - ' . $setting->description : ''),
20+
'options' => (($setting->options) ? $setting->options : ''),
21+
'value' => $setting->value,
22+
]);
23+
}
24+
25+
echo $this->Form->button(__('Submit'));
26+
27+
echo $this->Form->end();

src/View/Helper/NavbarMenuHelper.php

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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\View\Helper;
16+
17+
use Cake\View\Helper;
18+
use Utils\View\Helper\MenuBuilderInterface;
19+
20+
/**
21+
* Menu helper
22+
*
23+
* This helper is a template to build up the main menu.
24+
* Thats the `main` area.
25+
*
26+
*/
27+
class NavBarMenuHelper extends Helper implements MenuBuilderInterface
28+
{
29+
/**
30+
* Used helpers
31+
*
32+
* @var array
33+
*/
34+
public $helpers = [
35+
'Html'
36+
];
37+
/**
38+
* Default configuration.
39+
*
40+
* @var array
41+
*/
42+
protected $_defaultConfig = [];
43+
/**
44+
* afterMenu
45+
*
46+
* Method after the menu has been build.
47+
*
48+
* @param array $menu The menu items.
49+
* @param array $options Options.
50+
* @return string
51+
*/
52+
public function afterMenu($menu = [], $options = [])
53+
{
54+
return '</ul>';
55+
}
56+
/**
57+
* afterSubItem
58+
*
59+
* Method after a submenu item has been build.
60+
*
61+
* @param array $item The menu items.
62+
* @param array $options Options.
63+
* @return string
64+
*/
65+
public function afterSubItem($item = [], $options = [])
66+
{
67+
return '';
68+
}
69+
/**
70+
* beforeMenu
71+
*
72+
* Method before the menu has been build.
73+
*
74+
* @param array $menu The menu items.
75+
* @param array $options Options.
76+
* @return string
77+
*/
78+
public function beforeMenu($menu = [], $options = [])
79+
{
80+
return '<ul style="margin-left: 0px">';
81+
}
82+
/**
83+
* afterSubItem
84+
*
85+
* Method before a submenu item has been build.
86+
*
87+
* @param array $item The menu items.
88+
* @param array $options Options.
89+
* @return string
90+
*/
91+
public function beforeSubItem($item = [], $options = [])
92+
{
93+
return '';
94+
}
95+
/**
96+
* item
97+
*
98+
* Method to build an menu item.
99+
*
100+
* @param array $item The menu item.
101+
* @param array $options Options.
102+
* @return string
103+
*/
104+
public function item($item = [], $options = [])
105+
{
106+
$html = '<li style="display: inline; list-style-type: none; padding-right: 20px;">';
107+
$html .= (key_exists('active', $item) && $item['active'] ? '<b>' : '');
108+
$html .= $this->Html->link(__($item['title']), $item['url']);
109+
$html .= (key_exists('active', $item) ? '</b>' : '');
110+
$html .= '</li>';
111+
return $html;
112+
}
113+
/**
114+
* item
115+
*
116+
* Method to build an submenu item.
117+
*
118+
* @param array $item The menu item.
119+
* @param array $options Options.
120+
* @return string
121+
*/
122+
public function subItem($item = [], $options = [])
123+
{
124+
return '';
125+
}
126+
}

0 commit comments

Comments
 (0)