Skip to content

Commit

Permalink
Add a Settings part to manage the website.
Browse files Browse the repository at this point in the history
  • Loading branch information
Xety committed Nov 4, 2016
1 parent bf8c2d5 commit 0765e7e
Show file tree
Hide file tree
Showing 38 changed files with 931 additions and 28 deletions.
56 changes: 56 additions & 0 deletions config/Migrations/20161104074025_CreateSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
use Migrations\AbstractMigration;

class CreateSettings extends AbstractMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-change-method
* @return void
*/
public function change()
{
$table = $this->table('settings');
$table->addColumn('name', 'string', [
'default' => null,
'limit' => 255,
'null' => false,
]);
$table->addColumn('value_int', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
]);
$table->addColumn('value_str', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
]);
$table->addColumn('value_bool', 'boolean', [
'default' => null,
'null' => true,
]);
$table->addColumn('last_updated_user_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
]);
$table->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
]);
$table->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
]);
$table->addIndex([
'name',
], [
'name' => 'UNIQUE_NAME',
'unique' => true,
]);
$table->create();
}
}
Binary file added config/Migrations/schema-dump-default.lock
Binary file not shown.
46 changes: 46 additions & 0 deletions config/Routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,51 @@
]
);

//Settings Routes.
$routes->connect(
'/settings/edit/:id',
[
'controller' => 'settings',
'action' => 'edit'
],
[
'_name' => 'settings-edit',
'pass' => [
'id'
],
'id' => '[0-9]+'
]
);

$routes->connect(
'/settings/view/:id',
[
'controller' => 'settings',
'action' => 'view'
],
[
'_name' => 'settings-view',
'pass' => [
'id'
],
'id' => '[0-9]+'
]
);

$routes->connect(
'/settings/delete/:id',
[
'controller' => 'settings',
'action' => 'delete'
],
[
'_name' => 'settings-delete',
'pass' => [
'id'
],
'id' => '[0-9]+'
]
);

$routes->fallbacks();
});
11 changes: 8 additions & 3 deletions config/Seeds/AcosSeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AcosSeed extends AbstractSeed
public function run()
{
$data = [
['id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'app', 'lft' => '1', 'rght' => '168'],
['id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'app', 'lft' => '1', 'rght' => '178'],
['id' => '2', 'parent_id' => '1', 'model' => null, 'foreign_key' => null, 'alias' => 'Conversations', 'lft' => '2', 'rght' => '35'],
['id' => '3', 'parent_id' => '2', 'model' => null, 'foreign_key' => null, 'alias' => 'index', 'lft' => '3', 'rght' => '4'],
['id' => '4', 'parent_id' => '2', 'model' => null, 'foreign_key' => null, 'alias' => 'action', 'lft' => '5', 'rght' => '6'],
Expand Down Expand Up @@ -70,7 +70,7 @@ public function run()
['id' => '54', 'parent_id' => '44', 'model' => null, 'foreign_key' => null, 'alias' => 'deleteComment', 'lft' => '103', 'rght' => '104'],
['id' => '55', 'parent_id' => '44', 'model' => null, 'foreign_key' => null, 'alias' => 'getEditComment', 'lft' => '105', 'rght' => '106'],
['id' => '56', 'parent_id' => '44', 'model' => null, 'foreign_key' => null, 'alias' => 'editComment', 'lft' => '107', 'rght' => '108'],
['id' => '58', 'parent_id' => '1', 'model' => null, 'foreign_key' => null, 'alias' => 'admin', 'lft' => '110', 'rght' => '167'],
['id' => '58', 'parent_id' => '1', 'model' => null, 'foreign_key' => null, 'alias' => 'admin', 'lft' => '110', 'rght' => '177'],
['id' => '59', 'parent_id' => '58', 'model' => null, 'foreign_key' => null, 'alias' => 'Categories', 'lft' => '111', 'rght' => '120'],
['id' => '60', 'parent_id' => '59', 'model' => null, 'foreign_key' => null, 'alias' => 'index', 'lft' => '112', 'rght' => '113'],
['id' => '61', 'parent_id' => '59', 'model' => null, 'foreign_key' => null, 'alias' => 'add', 'lft' => '114', 'rght' => '115'],
Expand Down Expand Up @@ -99,7 +99,12 @@ public function run()
['id' => '84', 'parent_id' => '82', 'model' => null, 'foreign_key' => null, 'alias' => 'add', 'lft' => '160', 'rght' => '161'],
['id' => '85', 'parent_id' => '82', 'model' => null, 'foreign_key' => null, 'alias' => 'edit', 'lft' => '162', 'rght' => '163'],
['id' => '86', 'parent_id' => '82', 'model' => null, 'foreign_key' => null, 'alias' => 'delete', 'lft' => '164', 'rght' => '165'],
['id' => '87', 'parent_id' => '19', 'model' => '', 'foreign_key' => null, 'alias' => 'terms', 'lft' => '43', 'rght' => '44']
['id' => '87', 'parent_id' => '19', 'model' => '', 'foreign_key' => null, 'alias' => 'terms', 'lft' => '43', 'rght' => '44'],
['id' => '88', 'parent_id' => '58', 'model' => '', 'foreign_key' => null, 'alias' => 'Settings', 'lft' => '167', 'rght' => '176'],
['id' => '89', 'parent_id' => '88', 'model' => '', 'foreign_key' => null, 'alias' => 'index', 'lft' => '168', 'rght' => '169'],
['id' => '90', 'parent_id' => '88', 'model' => '', 'foreign_key' => null, 'alias' => 'create', 'lft' => '170', 'rght' => '171'],
['id' => '91', 'parent_id' => '88', 'model' => '', 'foreign_key' => null, 'alias' => 'edit', 'lft' => '172', 'rght' => '173'],
['id' => '92', 'parent_id' => '88', 'model' => '', 'foreign_key' => null, 'alias' => 'delete', 'lft' => '174', 'rght' => '175']
];

$table = $this->table('acos');
Expand Down
53 changes: 53 additions & 0 deletions config/Seeds/SettingsSeed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

use Phinx\Seed\AbstractSeed;

class SettingsSeed extends AbstractSeed
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*
* @return void
*/
public function run()
{
$data = [
[
'name' => 'User.Login.enabled',
'value_int' => null,
'value_str' => null,
'value_bool' => '1',
'last_updated_user_id' => '1',
'created' => '2015-12-08 01:00:00',
'modified' => '2015-12-08 13:30:48'
],
[
'name' => 'Site.version',
'value_int' => null,
'value_str' => '3.0.1',
'value_bool' => null,
'last_updated_user_id' => '1',
'created' => '2015-12-08 01:00:00',
'modified' => '2015-12-08 10:52:48'
],
[
'name' => 'Conversations.enabled',
'value_int' => null,
'value_str' => null,
'value_bool' => '1',
'last_updated_user_id' => '1',
'created' => '2015-12-08 10:38:44',
'modified' => '2015-12-08 15:34:40'
]
];

$settings = $this->table('settings');
$settings->insert($data)
->save();
}
}
10 changes: 10 additions & 0 deletions config/app.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@
'duration' => '+1 hours',
],

/**
* Configure the cache used for the database query caching.
*/
'database' => [
'className' => 'File',
'prefix' => 'Xeta_database_',
'path' => CACHE . 'database/',
'duration' => '+1 days',
],

/**
* Configure the cache used for general framework caching. Path information,
* object listings, and translation cache files are stored with this
Expand Down
127 changes: 127 additions & 0 deletions src/Controller/Admin/SettingsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
namespace App\Controller\Admin;

use App\Controller\AppController;
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\Network\Exception\NotFoundException;

class SettingsController extends AppController
{

/**
* Display all settings.
*
* @return void
*/
public function index()
{
$this->paginate = [
'maxLimit' => 20
];

$settings = $this->Settings
->find()
->contain([
'LastUpdatedUser' => function ($q) {
return $q->find('medium');
}
])
->order([
'Settings.name' => 'ASC'
]);

$settings = $this->paginate($settings);

$this->set(compact('settings'));
}

/**
* Create a setting.
*
* @return \Cake\Network\Response|void
*/
public function create()
{
$setting = $this->Settings->newEntity($this->request->data);

if ($this->request->is('post')) {
$setting->last_updated_user_id = $this->Auth->user('id');

if ($this->Settings->save($setting)) {
Cache::delete('settings', 'database');
$this->Flash->success(__d('admin', 'This setting has been created successfully !'));

return $this->redirect(['action' => 'index']);
}
}

$this->set(compact('setting'));
}

/**
* Edit a setting.
*
* @return \Cake\Network\Response|void
*/
public function edit()
{
$setting = $this->Settings
->find()
->where([
'Settings.id' => $this->request->id
])
->first();

if (is_null($setting)) {
$this->Flash->error(__d('admin', 'This setting doesn\'t exist or has been deleted.'));

return $this->redirect(['action' => 'index']);
}

if ($this->request->is(['post', 'put'])) {
$this->Settings->patchEntity($setting, $this->request->data);

$setting->last_updated_user_id = $this->Auth->user('id');

if ($this->Settings->save($setting)) {
Cache::delete('settings', 'database');
$this->Flash->success(__d('admin', 'This setting has been edited successfully !'));

return $this->redirect(['action' => 'index']);
}
}

$this->set(compact('setting'));
}

/**
* Delete a setting.
*
* @return \Cake\Network\Response|void
*/
public function delete()
{
$setting = $this->Settings
->find()
->where([
'Settings.id' => $this->request->id
])
->first();

if (is_null($setting)) {
$this->Flash->error(__d('admin', 'This setting doesn\'t exist or has been deleted.'));

return $this->redirect(['action' => 'index']);
}

if ($this->Settings->delete($setting)) {
$this->Flash->success(__d('admin', 'This setting has been deleted successfully !'));

return $this->redirect(['action' => 'index']);
}

return $this->redirect(['action' => 'index']);
}
}
8 changes: 6 additions & 2 deletions src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public function initialize()
'logoutRedirect' => [
'controller' => 'pages',
'action' => 'home'
],
'authError' => 'You are not authorized to access that location !'
]
]);

if (env('HTTPS')) {
Expand All @@ -86,6 +85,11 @@ public function initialize()
*/
public function beforeFilter(Event $event)
{
$this->loadModel('Settings');
$this->Settings->setSettings();

$this->Auth->config('authError', __('You need to be logged in or you are not authorized to access that location !'));

//Define the language.
$language = new Language($this);
$language->setLanguage();
Expand Down

0 comments on commit 0765e7e

Please sign in to comment.