Skip to content

Commit 3cae21f

Browse files
committed
feat. impl. new system of maintenance -> StanByes
1 parent 8bb6375 commit 3cae21f

12 files changed

Lines changed: 339 additions & 103 deletions

File tree

app/Config/Schema/schema.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class AppSchema extends CakeSchema
5858
'lang' => ['type' => 'string', 'null' => false, 'default' => 'fr', 'length' => 5, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'],
5959
'theme' => ['type' => 'string', 'null' => false, 'default' => 'default', 'length' => 50, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'],
6060
'layout' => ['type' => 'string', 'null' => false, 'default' => null, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'],
61-
'maintenance' => ['type' => 'text', 'null' => false, 'default' => null, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'],
6261
'money_name_singular' => ['type' => 'string', 'null' => false, 'default' => null, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'],
6362
'money_name_plural' => ['type' => 'string', 'null' => false, 'default' => null, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'],
6463
'server_state' => ['type' => 'integer', 'null' => false, 'default' => null, 'length' => 1, 'unsigned' => false],
@@ -136,6 +135,16 @@ class AppSchema extends CakeSchema
136135
],
137136
'tableParameters' => ['charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'InnoDB']
138137
];
138+
public $maintenances = [
139+
'id' => ['type' => 'integer', 'null' => false, 'default' => null, 'length' => 20, 'unsigned' => false, 'key' => 'primary'],
140+
'url' => ['type' => 'string', 'null' => false, 'default' => null, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'],
141+
'reason' => ['type' => 'string', 'null' => false, 'default' => null, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'],
142+
'active' => ['type' => 'integer', 'null' => false, 'default' => 1, 'length' => 1, 'unsigned' => false],
143+
'indexes' => [
144+
'PRIMARY' => ['column' => 'id', 'unique' => 1]
145+
],
146+
'tableParameters' => ['charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'InnoDB']
147+
];
139148
public $navbars = [
140149
'id' => ['type' => 'integer', 'null' => false, 'default' => null, 'length' => 20, 'unsigned' => false, 'key' => 'primary'],
141150
'order' => ['type' => 'integer', 'null' => false, 'default' => null, 'length' => 2, 'unsigned' => false],
@@ -387,7 +396,6 @@ public function after($event = [], $install = false, $updateContent = [])
387396
'lang' => 'fr_FR',
388397
'theme' => 'default',
389398
'layout' => 'default',
390-
'maintenance' => '0',
391399
'money_name_singular' => 'point',
392400
'money_name_plural' => 'points',
393401
'server_state' => 0,

app/Controller/AppController.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ public function beforeFilter()
5050
// lowercase to avoid errors when the controller is called with uppercase
5151
$this->params['controller'] = strtolower($this->params['controller']);
5252
$this->params['action'] = strtolower($this->params['action']);
53+
54+
$LoginCondition = $this->here != "/login" || !$this->EyPlugin->isInstalled('phpierre.signinup');
55+
56+
$this->loadModel("Maintenance");
57+
if ($this->params['controller'] != "user" and $this->params['controller'] != "maintenance" and !$this->Permissions->can("BYPASS_MAINTENANCE") and $this->Maintenance->checkMaintenance($this->here) and $LoginCondition) {
58+
$this->redirect([
59+
'controller' => 'maintenance',
60+
'action' => 'index',
61+
'plugin' => false,
62+
'admin' => false,
63+
]);
64+
}
65+
5366
// Plugin disabled
5467
if ($this->request->params['plugin']) {
5568
$plugin = $this->EyPlugin->findPlugin('slugLower', $this->request->params['plugin']);
@@ -93,16 +106,6 @@ public function beforeFilter()
93106
if ($event->isStopped())
94107
return $event->result;
95108
}
96-
$LoginCondition = ($this->here != "/login") || !$this->EyPlugin->isInstalled('phpierre.signinup');
97-
// Maintenance
98-
if ($this->params['controller'] != "user" && $this->params['controller'] != "maintenance" && $this->Configuration->getKey('maintenance') != '0' && !$this->Permissions->can('BYPASS_MAINTENANCE') && $LoginCondition) {
99-
$this->redirect([
100-
'controller' => 'maintenance',
101-
'action' => 'index',
102-
'plugin' => false,
103-
'admin' => false
104-
]);
105-
}
106109

107110

108111
}

app/Controller/MaintenanceController.php

Lines changed: 124 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,137 @@ class MaintenanceController extends AppController
55

66
public $components = ['Session'];
77

8-
public function index($banned = false)
8+
public function index($url = "")
99
{
10-
if ($this->Configuration->getKey('maintenance') != '0') {
11-
$msg = $this->Configuration->getKey('maintenance');
12-
} else if ($banned) {
13-
$msg = $this->Lang->get('USER__BANNED_MSG');
14-
} else {
15-
$this->redirect('/');
16-
}
10+
$this->loadModel("Maintenance");
11+
12+
if ($this->Permissions->can("BYPASS_MAINTENANCE") || !$this->Maintenance->checkMaintenance("/" . $url))
13+
$this->redirect("/");
14+
15+
$msg = $this->Maintenance->checkMaintenance("/" . $url)["Maintenance"]["reason"];
1716
$this->set(compact('msg'));
1817
}
1918

2019
public function admin_index()
2120
{
22-
if ($this->isConnected and $this->Permissions->can('MANAGE_MAINTENANCE')) {
23-
$this->layout = "admin";
24-
25-
$this->set('title_for_layout', $this->Lang->get('MAINTENANCE__TITLE'));
26-
if ($this->request->is('post')) {
27-
if ($this->request->data['state'] == 'enabled') {
28-
$maintenance = $this->request->data['reason'];
29-
$this->History->set('ADD_MAINTENANCE', 'maintenance');
30-
} else if ($this->request->data['state'] == 'disabled') {
31-
$maintenance = '0';
32-
$this->History->set('DELETE_MAINTENANCE', 'maintenance');
33-
}
34-
$this->Configuration->setKey('maintenance', $maintenance);
35-
$this->Session->setFlash($this->Lang->get('MAINTENANCE__EDIT_SUCCESS'), 'default.success');
36-
}
37-
} else {
38-
$this->redirect('/');
21+
if (!$this->isConnected and !$this->Permissions->can('MANAGE_MAINTENANCE'))
22+
throw new ForbiddenException();
23+
24+
$this->layout = "admin";
25+
$this->set('title_for_layout', $this->Lang->get('MAINTENANCE__TITLE'));
26+
27+
$this->loadModel("Maintenance");
28+
$pagesInMaintenance = $this->Maintenance->find("all");
29+
30+
$this->set("pages", $pagesInMaintenance);
31+
}
32+
33+
public function admin_add()
34+
{
35+
if (!$this->isConnected and !$this->Permissions->can('MANAGE_MAINTENANCE'))
36+
throw new ForbiddenException();
37+
38+
$this->layout = "admin";
39+
$this->set('title_for_layout', $this->Lang->get('MAINTENANCE__TITLE'));
40+
41+
if ($this->request->is("post")) {
42+
$this->autoRender = false;
43+
$this->response->type('json');
44+
45+
if (empty($this->request->data["reason"]))
46+
return $this->response->body(json_encode(['statut' => false, 'msg' => $this->Lang->get('MAINTENANCE__ADD_REASON_EMPTY')]));
47+
48+
$this->loadModel("Maintenance");
49+
50+
$this->Maintenance->create();
51+
$this->Maintenance->set($this->request->data);
52+
$this->Maintenance->save();
53+
54+
return $this->response->body(json_encode(['statut' => true, 'msg' => $this->Lang->get('MAINTENANCE__ADD_SUCCESS')]));
3955
}
4056
}
4157

58+
public function admin_edit($id = false)
59+
{
60+
if (!$this->isConnected and !$this->Permissions->can('MANAGE_MAINTENANCE') | !$id)
61+
throw new ForbiddenException();
62+
63+
$this->layout = "admin";
64+
$this->set('title_for_layout', $this->Lang->get('MAINTENANCE__TITLE'));
65+
66+
$this->loadModel("Maintenance");
67+
$page = $this->Maintenance->find("first", ["conditions" => ["id" => $id]])["Maintenance"];
68+
69+
$this->set("page", $page);
70+
71+
if ($this->request->is("post")) {
72+
$this->autoRender = false;
73+
$this->response->type('json');
74+
75+
if (empty($this->request->data["reason"]))
76+
return $this->response->body(json_encode(['statut' => false, 'msg' => $this->Lang->get('MAINTENANCE__ADD_REASON_EMPTY')]));
77+
78+
$this->Maintenance->read(null, $id);
79+
$this->Maintenance->set([
80+
"url" => $this->request->data["url"],
81+
"reason" => $this->request->data["reason"]
82+
]);
83+
$this->Maintenance->save();
84+
85+
return $this->response->body(json_encode(['statut' => true, 'msg' => $this->Lang->get('MAINTENANCE__EDIT_SUCCESS')]));
86+
}
87+
}
88+
89+
public function admin_disable($id = false)
90+
{
91+
$this->autoRender = false;
92+
if (!$this->isConnected || !$this->Permissions->can('MANAGE_MAINTENANCE') || !$id)
93+
throw new ForbiddenException();
94+
95+
$this->loadModel('Maintenance');
96+
$this->Maintenance->read(null, $id);
97+
$this->Maintenance->set(["active" => "0"]);
98+
$this->Maintenance->save();
99+
100+
$pageUrl = $this->Maintenance->find('first', ["conditions" => ['id' => $id]])["Maintenance"]["url"];
101+
$this->Session->setFlash($this->Lang->get('MAINTENANCE__DISABLED_PAGE', [
102+
'{PAGE}' => $pageUrl,
103+
]), 'default.success');
104+
$this->redirect(['controller' => 'maintenance', 'action' => 'index', 'admin' => true]);
105+
}
106+
107+
public function admin_enable($id = false)
108+
{
109+
$this->autoRender = false;
110+
if (!$this->isConnected || !$this->Permissions->can('MANAGE_MAINTENANCE') || !$id)
111+
throw new ForbiddenException();
112+
113+
$this->loadModel('Maintenance');
114+
$this->Maintenance->read(null, $id);
115+
$this->Maintenance->set(["active" => "1"]);
116+
$this->Maintenance->save();
117+
118+
$pageUrl = $this->Maintenance->find('first', ["conditions" => ['id' => $id]])["Maintenance"]["url"];
119+
$this->Session->setFlash($this->Lang->get('MAINTENANCE__ENABLED_PAGE', [
120+
'{PAGE}' => $pageUrl,
121+
]), 'default.success');
122+
$this->redirect(['controller' => 'maintenance', 'action' => 'index', 'admin' => true]);
123+
}
124+
125+
public function admin_delete($id = false)
126+
{
127+
$this->autoRender = false;
128+
if (!$this->isConnected || !$this->Permissions->can('MANAGE_MAINTENANCE') || !$id)
129+
throw new ForbiddenException();
130+
131+
$this->loadModel('Maintenance');
132+
$pageUrl = $this->Maintenance->find('first', ["conditions" => ['id' => $id]])["Maintenance"]["url"];
133+
134+
$this->Maintenance->delete($id);
135+
136+
$this->Session->setFlash($this->Lang->get('MAINTENANCE__DELETED_PAGE', [
137+
'{PAGE}' => $pageUrl,
138+
]), 'default.success');
139+
$this->redirect(['controller' => 'maintenance', 'action' => 'index', 'admin' => true]);
140+
}
42141
}

app/Model/Maintenance.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
class Maintenance extends AppModel
4+
{
5+
function checkMaintenance($url = "")
6+
{
7+
$check = $this->find("first", ["conditions" => ["url" => $url, "active" => 1]]);
8+
if ($check)
9+
return $check;
10+
$is_full = $this->isFullMaintenance();
11+
if ($is_full)
12+
return $is_full;
13+
return false;
14+
}
15+
16+
function isFullMaintenance()
17+
{
18+
return $this->find("first", ["conditions" => ["url" => "", "active" => 1]]);
19+
}
20+
}

app/View/Maintenance/admin_add.ctp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<section class="content">
2+
<div class="row">
3+
<div class="col-md-12">
4+
<div class="card">
5+
<div class="card-header with-border">
6+
<h3 class="card-title"><?= $Lang->get('MAINTENANCE__TITLE') ?></h3>
7+
</div>
8+
<div class="card-body">
9+
<form method="post" data-ajax="true" data-redirect-url="<?= $this->Html->url(['controller' => 'maintenance', 'action' => 'index', 'admin' => 'true']) ?>">
10+
<div class="form-group">
11+
<label><?= $Lang->get("MAINTENANCE__PAGE") ?></label><br>
12+
<i><?= $Lang->get("MAINTENANCE__ADD_EXAMPLE") ?></i><br>
13+
<i><?= $Lang->get("MAINTENANCE__ADD_EMPTY_URL") ?></i>
14+
15+
<input type="text" id="url" name="url" class="form-control">
16+
</div>
17+
<div class="form-group">
18+
<label><?= $Lang->get('MAINTENANCE__REASON') ?></label>
19+
<?= $this->Html->script('admin/tinymce/tinymce.min.js') ?>
20+
<script type="text/javascript">
21+
tinymce.init({
22+
selector: "textarea",
23+
height: 300,
24+
width: '100%',
25+
language: 'fr_FR',
26+
plugins: "textcolor code image link",
27+
toolbar: "fontselect fontsizeselect bold italic underline strikethrough link image forecolor backcolor alignleft aligncenter alignright alignjustify cut copy paste bullist numlist outdent indent blockquote code"
28+
});
29+
</script>
30+
<textarea id="editor" name="reason" cols="30"
31+
rows="10"></textarea>
32+
</div>
33+
34+
<input type="hidden" name="data[_Token][key]" value="<?= $csrfToken ?>">
35+
36+
<div class="float-right">
37+
<a href="<?= $this->Html->url(['controller' => 'maintenance', 'action' => 'index', 'admin' => true]) ?>"
38+
class="btn btn-default"><?= $Lang->get('GLOBAL__CANCEL') ?></a>
39+
<button class="btn btn-primary" type="submit"><?= $Lang->get('GLOBAL__SUBMIT') ?></button>
40+
</div>
41+
</form>
42+
43+
</div>
44+
</div>
45+
</div>
46+
</div>
47+
</section>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<section class="content">
2+
<div class="row">
3+
<div class="col-md-12">
4+
<div class="card">
5+
<div class="card-header with-border">
6+
<h3 class="card-title"><?= $Lang->get('MAINTENANCE__TITLE') ?></h3>
7+
</div>
8+
<div class="card-body">
9+
<form method="post" data-ajax="true" data-redirect-url="<?= $this->Html->url(['controller' => 'maintenance', 'action' => 'index', 'admin' => 'true']) ?>">
10+
11+
<div class="form-group">
12+
<label><?= $Lang->get("MAINTENANCE__PAGE") ?></label><br>
13+
<i><?= $Lang->get("MAINTENANCE__ADD_EXAMPLE") ?></i><br>
14+
<i><?= $Lang->get("MAINTENANCE__ADD_EMPTY_URL") ?></i>
15+
16+
<input type="text" id="url" name="url" class="form-control" value="<?= $page["url"] ?>">
17+
</div>
18+
<div class="form-group">
19+
<label><?= $Lang->get('MAINTENANCE__REASON') ?></label>
20+
<?= $this->Html->script('admin/tinymce/tinymce.min.js') ?>
21+
<script type="text/javascript">
22+
tinymce.init({
23+
selector: "textarea",
24+
height: 300,
25+
width: '100%',
26+
language: 'fr_FR',
27+
plugins: "textcolor code image link",
28+
toolbar: "fontselect fontsizeselect bold italic underline strikethrough link image forecolor backcolor alignleft aligncenter alignright alignjustify cut copy paste bullist numlist outdent indent blockquote code"
29+
});
30+
</script>
31+
<textarea id="editor" name="reason" cols="30" rows="10"><?= $page["reason"] ?></textarea>
32+
</div>
33+
34+
<input type="hidden" name="data[_Token][key]" value="<?= $csrfToken ?>">
35+
36+
<div class="float-right">
37+
<a href="<?= $this->Html->url(['controller' => 'maintenance', 'action' => 'index', 'admin' => true, $page["id"]]) ?>"
38+
class="btn btn-default"><?= $Lang->get('GLOBAL__CANCEL') ?></a>
39+
<button class="btn btn-primary" type="submit"><?= $Lang->get('GLOBAL__SUBMIT') ?></button>
40+
</div>
41+
</form>
42+
43+
</div>
44+
</div>
45+
</div>
46+
</div>
47+
</section>

0 commit comments

Comments
 (0)