2020-06-21 21:29:25 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use App\Controller\AppController;
|
2021-09-28 13:32:51 +02:00
|
|
|
use Cake\Utility\Inflector;
|
2020-06-21 21:29:25 +02:00
|
|
|
use Cake\Utility\Hash;
|
|
|
|
use Cake\Utility\Text;
|
|
|
|
use \Cake\Database\Expression\QueryExpression;
|
2021-09-10 11:55:54 +02:00
|
|
|
use Cake\ORM\TableRegistry;
|
2021-02-24 11:05:23 +01:00
|
|
|
use Cake\Event\EventInterface;
|
2021-07-27 10:40:58 +02:00
|
|
|
use Cake\Core\Configure;
|
2020-06-21 21:29:25 +02:00
|
|
|
|
|
|
|
class InstanceController extends AppController
|
|
|
|
{
|
2021-02-24 11:05:23 +01:00
|
|
|
public function beforeFilter(EventInterface $event)
|
|
|
|
{
|
|
|
|
parent::beforeFilter($event);
|
|
|
|
$this->set('metaGroup', !empty($this->isAdmin) ? 'Cerebrate' : 'Administration');
|
|
|
|
}
|
|
|
|
|
2020-06-21 21:29:25 +02:00
|
|
|
public function home()
|
|
|
|
{
|
2021-09-06 11:17:25 +02:00
|
|
|
$statistics = $this->Instance->getStatistics();
|
|
|
|
$this->set('statistics', $statistics);
|
2020-06-21 21:29:25 +02:00
|
|
|
}
|
2020-09-29 13:20:33 +02:00
|
|
|
|
|
|
|
public function status()
|
|
|
|
{
|
|
|
|
$data = file_get_contents(APP . 'VERSION.json');
|
|
|
|
$data = json_decode($data, true);
|
|
|
|
$data['user'] = $this->ACL->getUser();
|
|
|
|
return $this->RestResponse->viewData($data, 'json');
|
|
|
|
}
|
2021-02-24 11:05:23 +01:00
|
|
|
|
2021-09-13 15:56:51 +02:00
|
|
|
public function searchAll()
|
2021-09-10 11:55:54 +02:00
|
|
|
{
|
|
|
|
$searchValue = $this->request->getQuery('search');
|
2021-09-13 15:56:51 +02:00
|
|
|
$model = $this->request->getQuery('model', null);
|
|
|
|
$limit = $this->request->getQuery('limit', 5);
|
2023-03-25 09:23:45 +01:00
|
|
|
$limit = is_numeric($limit) ? $limit : 5;
|
2021-09-13 15:56:51 +02:00
|
|
|
if (!empty($this->request->getQuery('show_all', false))) {
|
|
|
|
$limit = null;
|
|
|
|
}
|
2021-09-10 11:55:54 +02:00
|
|
|
$data = [];
|
|
|
|
if (!empty($searchValue)) {
|
2022-02-28 14:16:12 +01:00
|
|
|
$data = $this->Instance->searchAll($searchValue, $this->ACL->getUser(), $limit, $model);
|
2021-09-10 11:55:54 +02:00
|
|
|
}
|
|
|
|
if ($this->ParamHandler->isRest()) {
|
|
|
|
return $this->RestResponse->viewData($data, 'json');
|
|
|
|
}
|
|
|
|
$this->set('data', $data);
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:05:23 +01:00
|
|
|
public function migrationIndex()
|
|
|
|
{
|
|
|
|
$migrationStatus = $this->Instance->getMigrationStatus();
|
|
|
|
|
|
|
|
$this->loadModel('Phinxlog');
|
|
|
|
$status = $this->Phinxlog->mergeMigrationLogIntoStatus($migrationStatus['status']);
|
|
|
|
|
2021-09-28 13:32:51 +02:00
|
|
|
foreach ($status as $i => $entry) {
|
|
|
|
if (!empty($entry['plugin'])) {
|
|
|
|
$pluginTablename = sprintf('%s_phinxlog', Inflector::underscore($entry['plugin']));
|
2021-10-04 11:01:20 +02:00
|
|
|
$pluginTablename = str_replace(['\\', '/', '.'], '_', $pluginTablename);
|
2021-09-28 13:32:51 +02:00
|
|
|
$status[$i] = $this->Phinxlog->mergeMigrationLogIntoStatus([$entry], $pluginTablename)[0];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usort($status, function($a, $b) {
|
|
|
|
return strcmp($b['id'], $a['id']);
|
|
|
|
});
|
2022-01-17 15:55:55 +01:00
|
|
|
if ($this->ParamHandler->isRest()) {
|
|
|
|
return $this->RestResponse->viewData([
|
|
|
|
'status' => $status,
|
|
|
|
'updateAvailables' => $migrationStatus['updateAvailables'],
|
|
|
|
], 'json');
|
|
|
|
}
|
2021-02-24 11:05:23 +01:00
|
|
|
$this->set('status', $status);
|
|
|
|
$this->set('updateAvailables', $migrationStatus['updateAvailables']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function migrate($version=null) {
|
|
|
|
if ($this->request->is('post')) {
|
|
|
|
if (is_null($version)) {
|
|
|
|
$migrateResult = $this->Instance->migrate();
|
|
|
|
} else {
|
|
|
|
$migrateResult = $this->Instance->migrate(['target' => $version]);
|
|
|
|
}
|
|
|
|
if ($this->ParamHandler->isRest() || $this->ParamHandler->isAjax()) {
|
|
|
|
if ($migrateResult['success']) {
|
|
|
|
return $this->RestResponse->saveSuccessResponse('instance', 'migrate', false, false, __('Migration sucessful'));
|
|
|
|
} else {
|
|
|
|
return $this->RestResponse->saveFailResponse('instance', 'migrate', false, $migrateResult['error']);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($migrateResult['success']) {
|
|
|
|
$this->Flash->success(__('Migration sucessful'));
|
|
|
|
$this->redirect(['action' => 'migrationIndex']);
|
|
|
|
} else {
|
|
|
|
$this->Flash->error(__('Migration fail'));
|
|
|
|
$this->redirect(['action' => 'migrationIndex']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$migrationStatus = $this->Instance->getMigrationStatus();
|
|
|
|
$this->set('title', __n('Run database update?', 'Run all database updates?', count($migrationStatus['updateAvailables'])));
|
|
|
|
$this->set('question', __('The process might take some time.'));
|
|
|
|
$this->set('actionName', __n('Run update', 'Run all updates', count($migrationStatus['updateAvailables'])));
|
|
|
|
$this->set('path', ['controller' => 'instance', 'action' => 'migrate']);
|
|
|
|
$this->render('/genericTemplates/confirm');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rollback($version=null) {
|
|
|
|
if ($this->request->is('post')) {
|
|
|
|
if (is_null($version)) {
|
|
|
|
$migrateResult = $this->Instance->rollback();
|
|
|
|
} else {
|
|
|
|
$migrateResult = $this->Instance->rollback(['target' => $version]);
|
|
|
|
}
|
|
|
|
if ($this->ParamHandler->isRest() || $this->ParamHandler->isAjax()) {
|
|
|
|
if ($migrateResult['success']) {
|
|
|
|
return $this->RestResponse->saveSuccessResponse('instance', 'rollback', false, false, __('Rollback sucessful'));
|
|
|
|
} else {
|
|
|
|
return $this->RestResponse->saveFailResponse('instance', 'rollback', false, $migrateResult['error']);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($migrateResult['success']) {
|
|
|
|
$this->Flash->success(__('Rollback sucessful'));
|
|
|
|
$this->redirect(['action' => 'migrationIndex']);
|
|
|
|
} else {
|
|
|
|
$this->Flash->error(__('Rollback fail'));
|
|
|
|
$this->redirect(['action' => 'migrationIndex']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$migrationStatus = $this->Instance->getMigrationStatus();
|
|
|
|
$this->set('title', __('Run database rollback?'));
|
|
|
|
$this->set('question', __('The process might take some time.'));
|
|
|
|
$this->set('actionName', __('Run rollback'));
|
|
|
|
$this->set('path', ['controller' => 'instance', 'action' => 'rollback']);
|
|
|
|
$this->render('/genericTemplates/confirm');
|
|
|
|
}
|
2021-07-19 15:00:09 +02:00
|
|
|
|
|
|
|
public function settings()
|
|
|
|
{
|
|
|
|
$this->Settings = $this->getTableLocator()->get('Settings');
|
2021-07-20 11:54:55 +02:00
|
|
|
$all = $this->Settings->getSettings(true);
|
2022-01-17 15:55:55 +01:00
|
|
|
if ($this->ParamHandler->isRest()) {
|
|
|
|
return $this->RestResponse->viewData([
|
|
|
|
'settingsProvider' => $all['settingsProvider'],
|
|
|
|
'settings' => $all['settings'],
|
|
|
|
'settingsFlattened' => $all['settingsFlattened'],
|
|
|
|
'notices' => $all['notices'],
|
|
|
|
], 'json');
|
|
|
|
}
|
2021-07-19 15:00:09 +02:00
|
|
|
$this->set('settingsProvider', $all['settingsProvider']);
|
|
|
|
$this->set('settings', $all['settings']);
|
2021-07-21 11:18:06 +02:00
|
|
|
$this->set('settingsFlattened', $all['settingsFlattened']);
|
2021-07-20 11:54:55 +02:00
|
|
|
$this->set('notices', $all['notices']);
|
2021-07-19 15:00:09 +02:00
|
|
|
}
|
2021-07-21 16:07:30 +02:00
|
|
|
|
|
|
|
public function saveSetting()
|
|
|
|
{
|
|
|
|
if ($this->request->is('post')) {
|
|
|
|
$data = $this->ParamHandler->harvestParams([
|
|
|
|
'name',
|
|
|
|
'value'
|
|
|
|
]);
|
|
|
|
$this->Settings = $this->getTableLocator()->get('Settings');
|
|
|
|
$errors = $this->Settings->saveSetting($data['name'], $data['value']);
|
|
|
|
$message = __('Could not save setting `{0}`', $data['name']);
|
|
|
|
if (empty($errors)) {
|
|
|
|
$message = __('Setting `{0}` saved', $data['name']);
|
|
|
|
$data = $this->Settings->getSetting($data['name']);
|
|
|
|
}
|
|
|
|
$this->CRUD->setResponseForController('saveSetting', empty($errors), $message, $data, $errors);
|
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-21 21:29:25 +02:00
|
|
|
}
|