2020-05-29 13:41:58 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Model\Table;
|
|
|
|
|
|
|
|
use Cake\ORM\Table;
|
|
|
|
use Cake\Validation\Validator;
|
2020-06-21 23:53:38 +02:00
|
|
|
use Cake\Core\Configure;
|
|
|
|
use Cake\Core\Configure\Engine\PhpConfig;
|
2021-01-13 14:20:29 +01:00
|
|
|
use Cake\ORM\TableRegistry;
|
2021-11-17 17:04:39 +01:00
|
|
|
use Cake\Utility\Hash;
|
|
|
|
use Cake\Database\Expression\QueryExpression;
|
|
|
|
use Cake\ORM\Query;
|
2022-03-08 17:13:19 +01:00
|
|
|
use Cake\I18n\FrozenTime;
|
2020-05-29 13:41:58 +02:00
|
|
|
|
|
|
|
class AppTable extends Table
|
|
|
|
{
|
|
|
|
public function initialize(array $config): void
|
|
|
|
{
|
|
|
|
}
|
2021-01-13 14:20:29 +01:00
|
|
|
|
2021-11-17 17:04:39 +01:00
|
|
|
public function getStatisticsUsageForModel(Object $table, array $scopes, array $options=[]): array
|
|
|
|
{
|
|
|
|
$defaultOptions = [
|
|
|
|
'limit' => 5,
|
|
|
|
'includeOthers' => true,
|
|
|
|
'ignoreNull' => true,
|
|
|
|
];
|
|
|
|
$options = $this->getOptions($defaultOptions, $options);
|
|
|
|
$stats = [];
|
|
|
|
foreach ($scopes as $scope) {
|
|
|
|
$queryTopUsage = $table->find();
|
|
|
|
$queryTopUsage
|
|
|
|
->select([
|
|
|
|
$scope,
|
|
|
|
'count' => $queryTopUsage->func()->count('id'),
|
|
|
|
]);
|
|
|
|
if ($queryTopUsage->getDefaultTypes()[$scope] != 'boolean') {
|
|
|
|
$queryTopUsage->where(function (QueryExpression $exp) use ($scope) {
|
|
|
|
return $exp
|
|
|
|
->isNotNull($scope)
|
|
|
|
->notEq($scope, '');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
$queryTopUsage
|
|
|
|
->group($scope)
|
|
|
|
->order(['count' => 'DESC'])
|
|
|
|
->limit($options['limit'])
|
|
|
|
->page(1)
|
|
|
|
->enableHydration(false);
|
2022-08-17 13:49:11 +02:00
|
|
|
$topUsage = $queryTopUsage->all()->toList();
|
2021-11-17 17:04:39 +01:00
|
|
|
$stats[$scope] = $topUsage;
|
|
|
|
if (
|
|
|
|
!empty($options['includeOthers']) && !empty($topUsage) &&
|
|
|
|
$queryTopUsage->getDefaultTypes()[$scope] != 'boolean' // No need to get others as we only have 2 possibilities already considered
|
|
|
|
) {
|
|
|
|
$queryOthersUsage = $table->find();
|
|
|
|
$queryOthersUsage
|
|
|
|
->select([
|
|
|
|
'count' => $queryOthersUsage->func()->count('id'),
|
|
|
|
])
|
|
|
|
->where(function (QueryExpression $exp, Query $query) use ($topUsage, $scope, $options) {
|
|
|
|
if (!empty($options['ignoreNull'])) {
|
|
|
|
return $exp
|
|
|
|
->isNotNull($scope)
|
|
|
|
->notEq($scope, '')
|
|
|
|
->notIn($scope, Hash::extract($topUsage, "{n}.{$scope}"));
|
|
|
|
} else {
|
|
|
|
return $exp->or([
|
|
|
|
$query->newExpr()->isNull($scope),
|
|
|
|
$query->newExpr()->eq($scope, ''),
|
|
|
|
$query->newExpr()->notIn($scope, Hash::extract($topUsage, "{n}.{$scope}")),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
->enableHydration(false);
|
2022-08-17 13:49:52 +02:00
|
|
|
$othersUsage = $queryOthersUsage->all()->toList();
|
2021-11-17 17:04:39 +01:00
|
|
|
if (!empty($othersUsage)) {
|
|
|
|
$stats[$scope][] = [
|
|
|
|
$scope => __('Others'),
|
|
|
|
'count' => $othersUsage[0]['count'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getOptions($defaults=[], $options=[]): array
|
|
|
|
{
|
|
|
|
return array_merge($defaults, $options);
|
|
|
|
}
|
|
|
|
|
2021-11-15 11:48:23 +01:00
|
|
|
// Move this into a tool
|
2021-11-17 17:04:39 +01:00
|
|
|
public function getActivityStatisticsForModel(Object $table, int $days = 30): array
|
2021-11-15 11:48:23 +01:00
|
|
|
{
|
|
|
|
$statistics = [];
|
|
|
|
if ($table->hasBehavior('Timestamp')) {
|
2022-01-20 13:44:19 +01:00
|
|
|
if ($table->getSchema()->getColumnType('created') == 'datetime') {
|
|
|
|
$statistics['created'] = $this->getActivityStatistic($table, $days, 'created');
|
|
|
|
}
|
|
|
|
if ($table->getSchema()->getColumnType('modified') == 'datetime') {
|
|
|
|
$statistics['modified'] = $this->getActivityStatistic($table, $days, 'modified');
|
|
|
|
}
|
2021-11-15 11:48:23 +01:00
|
|
|
}
|
|
|
|
return $statistics;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getActivityStatistic(Object $table, int $days = 30, string $field = 'modified', bool $includeTimeline = true): array
|
|
|
|
{
|
|
|
|
$statistics = [];
|
|
|
|
$statistics['days'] = $days;
|
|
|
|
$statistics['amount'] = $table->find()->all()->count();
|
|
|
|
if ($table->behaviors()->has('Timestamp') && $includeTimeline) {
|
|
|
|
$statistics['timeline'] = $this->buildTimeline($table, $days, $field);
|
2022-03-08 17:13:19 +01:00
|
|
|
$statistics['variation'] = $table->find()->where(["{$field} >" => FrozenTime::now()->subDays($days)])->all()->count();
|
2021-11-15 11:48:23 +01:00
|
|
|
} 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));
|
|
|
|
}
|
|
|
|
$days = $days - 1;
|
|
|
|
$query = $table->find();
|
|
|
|
$query->select([
|
|
|
|
'count' => $query->func()->count('id'),
|
|
|
|
'date' => "DATE({$field})",
|
|
|
|
])
|
2022-03-08 17:13:19 +01:00
|
|
|
->where(["{$field} >" => FrozenTime::now()->subDays($days)])
|
2021-11-15 11:48:23 +01:00
|
|
|
->group(['date'])
|
|
|
|
->order(['date']);
|
2022-01-20 13:44:19 +01:00
|
|
|
$data = $query->all()->toArray();
|
2021-11-15 11:48:23 +01:00
|
|
|
$interval = new \DateInterval('P1D');
|
2022-03-08 17:13:19 +01:00
|
|
|
$period = new \DatePeriod(FrozenTime::now()->subDays($days), $interval, FrozenTime::now()->addDays(1));
|
2021-11-15 11:48:23 +01:00
|
|
|
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-01-13 14:20:29 +01:00
|
|
|
public function saveMetaFields($id, $input)
|
|
|
|
{
|
|
|
|
$this->MetaFields = TableRegistry::getTableLocator()->get('MetaFields');
|
2021-01-14 09:15:37 +01:00
|
|
|
$this->MetaTemplates = TableRegistry::getTableLocator()->get('MetaTemplates');
|
|
|
|
foreach ($input['metaFields'] as $templateID => $metaFields) {
|
|
|
|
$metaTemplates = $this->MetaTemplates->find()->where([
|
|
|
|
'id' => $templateID,
|
|
|
|
'enabled' => 1
|
|
|
|
])->contain(['MetaTemplateFields'])->first();
|
|
|
|
$fieldNameToId = [];
|
|
|
|
foreach ($metaTemplates->meta_template_fields as $i => $metaTemplateField) {
|
|
|
|
$fieldNameToId[$metaTemplateField->field] = $metaTemplateField->id;
|
2021-01-13 14:20:29 +01:00
|
|
|
}
|
2021-01-14 09:15:37 +01:00
|
|
|
foreach ($metaFields as $metaField => $values) {
|
|
|
|
if (!is_array($values)) {
|
|
|
|
$values = [$values];
|
|
|
|
}
|
|
|
|
foreach ($values as $value) {
|
|
|
|
if ($value !== '') {
|
|
|
|
$temp = $this->MetaFields->newEmptyEntity();
|
|
|
|
$temp->field = $metaField;
|
|
|
|
$temp->value = $value;
|
2021-01-15 14:40:03 +01:00
|
|
|
$temp->scope = $this->metaFields;
|
2021-01-14 09:15:37 +01:00
|
|
|
$temp->parent_id = $id;
|
|
|
|
$temp->meta_template_id = $templateID;
|
|
|
|
$temp->meta_template_field_id = $fieldNameToId[$metaField];
|
2021-01-15 14:40:03 +01:00
|
|
|
$res = $this->MetaFields->save($temp);
|
2021-01-14 09:15:37 +01:00
|
|
|
}
|
2021-01-13 14:20:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 14:57:43 +01:00
|
|
|
|
|
|
|
public function isValidUrl($value, array $context): bool
|
|
|
|
{
|
|
|
|
return filter_var($value, FILTER_VALIDATE_URL);
|
|
|
|
}
|
2020-05-29 13:41:58 +02:00
|
|
|
}
|