2020-06-21 21:29:25 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Model\Table;
|
|
|
|
|
|
|
|
use App\Model\Table\AppTable;
|
|
|
|
use Cake\ORM\Table;
|
2021-09-06 11:17:25 +02:00
|
|
|
use Cake\ORM\TableRegistry;
|
2020-06-21 21:29:25 +02:00
|
|
|
use Cake\Validation\Validator;
|
2021-02-24 11:05:23 +01:00
|
|
|
use Migrations\Migrations;
|
2021-09-18 11:21:50 +02:00
|
|
|
use Cake\Filesystem\Folder;
|
2021-09-10 11:55:54 +02:00
|
|
|
use Cake\Http\Exception\MethodNotAllowedException;
|
2020-06-21 21:29:25 +02:00
|
|
|
|
|
|
|
class InstanceTable extends AppTable
|
|
|
|
{
|
2021-10-04 11:01:20 +02:00
|
|
|
protected $activePlugins = ['Tags', 'ADmad/SocialAuth'];
|
2021-09-10 11:55:54 +02:00
|
|
|
public $seachAllTables = ['Broods', 'Individuals', 'Organisations', 'SharingGroups', 'Users', 'EncryptionKeys', ];
|
|
|
|
|
2020-06-21 21:29:25 +02:00
|
|
|
public function initialize(array $config): void
|
|
|
|
{
|
|
|
|
parent::initialize($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validationDefault(Validator $validator): Validator
|
|
|
|
{
|
|
|
|
return $validator;
|
|
|
|
}
|
2021-02-24 11:05:23 +01:00
|
|
|
|
2021-11-12 15:40:03 +01:00
|
|
|
public function getStatistics(int $days=30): array
|
2021-09-06 11:17:25 +02:00
|
|
|
{
|
2021-09-28 13:32:51 +02:00
|
|
|
$models = ['Individuals', 'Organisations', 'Alignments', 'EncryptionKeys', 'SharingGroups', 'Users', 'Broods', 'Tags.Tags'];
|
2021-09-06 11:17:25 +02:00
|
|
|
foreach ($models as $model) {
|
|
|
|
$table = TableRegistry::getTableLocator()->get($model);
|
2021-11-12 15:40:03 +01:00
|
|
|
$statistics[$model]['created'] = $this->getActivityStatistic($table, $days, 'created');
|
|
|
|
$statistics[$model]['modified'] = $this->getActivityStatistic($table, $days, 'modified');
|
2021-09-06 11:17:25 +02:00
|
|
|
}
|
|
|
|
return $statistics;
|
|
|
|
}
|
|
|
|
|
2021-11-12 15:40:03 +01:00
|
|
|
public function getActivityStatistic(Object $table, int $days=30, string $field='modified', bool $includeTimeline=true): array
|
|
|
|
{
|
|
|
|
$statistics = [];
|
|
|
|
$statistics['amount'] = $table->find()->all()->count();
|
|
|
|
if ($table->behaviors()->has('Timestamp') && $includeTimeline) {
|
|
|
|
$statistics['timeline'] = $this->buildTimeline($table, $days, $field);
|
|
|
|
$statistics['variation'] = $table->find()->where(["{$field} >" => new \DateTime("-{$days} days")])->all()->count(); - $statistics['amount'];
|
|
|
|
} else {
|
|
|
|
$statistics['timeline'] = [];
|
|
|
|
$statistics['variation'] = 0;
|
|
|
|
}
|
|
|
|
return $statistics;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildTimeline(Object $table, int $days=30, string $field='modified'): array
|
|
|
|
{
|
|
|
|
$timeline = [];
|
|
|
|
$authorizedFields = ['modified', 'created'];
|
|
|
|
if ($table->behaviors()->has('Timestamp')) {
|
|
|
|
if (!in_array($field, $authorizedFields)) {
|
|
|
|
throw new MethodNotAllowedException(__('Cannot construct timeline for field `{0}`', $field));
|
|
|
|
}
|
|
|
|
$query = $table->find();
|
|
|
|
$query->select([
|
|
|
|
'count' => $query->func()->count('id'),
|
|
|
|
'date' => "DATE({$field})",
|
|
|
|
])
|
|
|
|
->where(["{$field} >" => new \DateTime("-{$days} days")])
|
|
|
|
->group(['date'])
|
|
|
|
->order(['date']);
|
|
|
|
$data = $query->toArray();
|
|
|
|
$interval = new \DateInterval('P1D');
|
|
|
|
$period = new \DatePeriod(new \DateTime("-{$days} days"), $interval, new \DateTime());
|
|
|
|
foreach ($period as $date) {
|
|
|
|
$timeline[$date->format("Y-m-d")] = [
|
|
|
|
'time' => $date->format("Y-m-d"),
|
|
|
|
'count' => 0
|
|
|
|
];
|
|
|
|
}
|
|
|
|
foreach ($data as $entry) {
|
|
|
|
$timeline[$entry->date]['count'] = $entry->count;
|
|
|
|
}
|
|
|
|
$timeline = array_values($timeline);
|
|
|
|
}
|
|
|
|
return $timeline;
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:56:51 +02:00
|
|
|
public function searchAll($value, $limit=5, $model=null)
|
2021-09-10 11:55:54 +02:00
|
|
|
{
|
|
|
|
$results = [];
|
2021-09-13 15:56:51 +02:00
|
|
|
$models = $this->seachAllTables;
|
|
|
|
if (!is_null($model)) {
|
|
|
|
if (in_array($model, $this->seachAllTables)) {
|
|
|
|
$models = [$model];
|
|
|
|
} else {
|
|
|
|
return $results; // Cannot search in this model
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($models as $tableName) {
|
2021-09-10 11:55:54 +02:00
|
|
|
$controller = $this->getController($tableName);
|
|
|
|
$table = TableRegistry::get($tableName);
|
|
|
|
$query = $table->find();
|
|
|
|
$quickFilterOptions = $this->getQuickFiltersFieldsFromController($controller);
|
|
|
|
$containFields = $this->getContainFieldsFromController($controller);
|
|
|
|
if (empty($quickFilterOptions)) {
|
|
|
|
continue; // make sure we are filtering on something
|
|
|
|
}
|
|
|
|
$params = ['quickFilter' => $value];
|
|
|
|
$query = $controller->CRUD->setQuickFilters($params, $query, $quickFilterOptions);
|
|
|
|
if (!empty($containFields)) {
|
|
|
|
$query->contain($containFields);
|
|
|
|
}
|
2021-09-10 14:46:56 +02:00
|
|
|
$results[$tableName]['amount'] = $query->count();
|
|
|
|
$result = $query->limit($limit)->all()->toList();
|
2021-09-10 11:55:54 +02:00
|
|
|
if (!empty($result)) {
|
2021-09-10 14:46:56 +02:00
|
|
|
$results[$tableName]['entries'] = $result;
|
2021-09-10 11:55:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getController($name)
|
|
|
|
{
|
|
|
|
$controllerName = "\\App\\Controller\\{$name}Controller";
|
|
|
|
if (!class_exists($controllerName)) {
|
|
|
|
throw new MethodNotAllowedException(__('Model `{0}` does not exists', $model));
|
|
|
|
}
|
|
|
|
$controller = new $controllerName;
|
|
|
|
return $controller;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getQuickFiltersFieldsFromController($controller)
|
|
|
|
{
|
|
|
|
return !empty($controller->quickFilterFields) ? $controller->quickFilterFields : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getContainFieldsFromController($controller)
|
|
|
|
{
|
|
|
|
return !empty($controller->containFields) ? $controller->containFields : [];
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:05:23 +01:00
|
|
|
public function getMigrationStatus()
|
|
|
|
{
|
|
|
|
$migrations = new Migrations();
|
|
|
|
$status = $migrations->status();
|
2021-09-03 09:47:13 +02:00
|
|
|
foreach ($this->activePlugins as $pluginName) {
|
|
|
|
$pluginStatus = $migrations->status([
|
|
|
|
'plugin' => $pluginName
|
|
|
|
]);
|
|
|
|
$pluginStatus = array_map(function ($entry) use ($pluginName) {
|
|
|
|
$entry['plugin'] = $pluginName;
|
|
|
|
return $entry;
|
|
|
|
}, $pluginStatus);
|
|
|
|
$status = array_merge($status, $pluginStatus);
|
|
|
|
}
|
2021-02-24 11:05:23 +01:00
|
|
|
$status = array_reverse($status);
|
|
|
|
|
|
|
|
$updateAvailables = array_filter($status, function ($update) {
|
|
|
|
return $update['status'] != 'up';
|
|
|
|
});
|
|
|
|
return [
|
|
|
|
'status' => $status,
|
|
|
|
'updateAvailables' => $updateAvailables,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function migrate($version=null) {
|
|
|
|
$migrations = new Migrations();
|
|
|
|
if (is_null($version)) {
|
|
|
|
$migrationResult = $migrations->migrate();
|
|
|
|
} else {
|
|
|
|
$migrationResult = $migrations->migrate(['target' => $version]);
|
|
|
|
}
|
2021-10-04 11:01:20 +02:00
|
|
|
$command = ROOT . '/bin/cake schema_cache clear';
|
|
|
|
$output = shell_exec($command);
|
2021-02-24 11:05:23 +01:00
|
|
|
return [
|
|
|
|
'success' => true
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rollback($version=null) {
|
|
|
|
$migrations = new Migrations();
|
|
|
|
if (is_null($version)) {
|
|
|
|
$migrationResult = $migrations->rollback();
|
|
|
|
} else {
|
|
|
|
$migrationResult = $migrations->rollback(['target' => $version]);
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
'success' => true
|
|
|
|
];
|
|
|
|
}
|
2021-09-18 11:21:50 +02:00
|
|
|
|
|
|
|
public function getAvailableThemes()
|
|
|
|
{
|
|
|
|
$themesPath = ROOT . '/webroot/css/themes';
|
|
|
|
$dir = new Folder($themesPath);
|
|
|
|
$filesRegex = 'bootstrap-(?P<themename>\w+)\.css';
|
|
|
|
$themeRegex = '/' . 'bootstrap-(?P<themename>\w+)\.css' . '/';
|
|
|
|
$files = $dir->find($filesRegex);
|
|
|
|
$themes = [];
|
|
|
|
foreach ($files as $filename) {
|
|
|
|
$matches = [];
|
|
|
|
$themeName = preg_match($themeRegex, $filename, $matches);
|
|
|
|
if (!empty($matches['themename'])) {
|
|
|
|
$themes[] = $matches['themename'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $themes;
|
|
|
|
}
|
2020-06-21 21:29:25 +02:00
|
|
|
}
|