Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
iglocska committed Dec 20, 2023
2 parents d3f0820 + 76b682e commit 26ec0b6
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 25 deletions.
32 changes: 26 additions & 6 deletions src/Controller/Component/CRUDComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public function index(array $options): void
$options['filters'][] = 'filteringTags';
}
$optionFilters = [];
$optionFilters += empty($options['filters']) ? [] : $options['filters'];
$optionFilters += array_map(function($filter) {
return is_array($filter) ? $filter['name'] : $filter;
}, empty($options['filters']) ? [] : $options['filters']);
foreach ($optionFilters as $i => $filter) {
$optionFilters[] = "{$filter} !=";
$optionFilters[] = "{$filter} >=";
Expand Down Expand Up @@ -90,14 +92,17 @@ public function index(array $options): void
if ($this->_validOrderFields($sort) && ($direction === 'asc' || $direction === 'desc')) {
$sort = explode('.', $sort);
if (count($sort) > 1) {
$sort[0] = Inflector::camelize(Inflector::pluralize($sort[0]));
if ($sort[0] != $this->Table->getAlias()) {
$sort[0] = Inflector::camelize(Inflector::pluralize($sort[0]));
}
}
$sort = implode('.', $sort);
$query->order($sort . ' ' . $direction);
}
}
$isRestOrCSV = $this->Controller->ParamHandler->isRest() || $this->request->is('csv');
if ($this->metaFieldsSupported()) {
$query = $this->includeRequestedMetaFields($query);
$query = $this->includeRequestedMetaFields($query, $isRestOrCSV);
}

if (!$this->Controller->ParamHandler->isRest()) {
Expand All @@ -107,7 +112,7 @@ public function index(array $options): void
}
$data = $this->Controller->paginate($query, $this->Controller->paginate ?? []);
$totalCount = $this->Controller->getRequest()->getAttribute('paging')[$this->TableAlias]['count'];
if ($this->Controller->ParamHandler->isRest() || $this->request->is('csv')) {
if ($isRestOrCSV) {
if (isset($options['hidden'])) {
$data->each(function($value, $key) use ($options) {
$hidden = is_array($options['hidden']) ? $options['hidden'] : [$options['hidden']];
Expand Down Expand Up @@ -256,6 +261,13 @@ public function filtering(): void
foreach ($filtersConfigRaw as $fieldConfig) {
if (is_array($fieldConfig)) {
$filtersConfig[$fieldConfig['name']] = $fieldConfig;
if (!empty($fieldConfig['options'])) {
if (is_string($fieldConfig['options'])) {
$filtersConfig[$fieldConfig['name']]['options'] = $this->Table->{$fieldConfig['options']}($this->Controller->ACL->getUser());
} else {
$filtersConfig[$fieldConfig['name']]['options'] = $fieldConfig['options'];
}
}
} else {
$filtersConfig[$fieldConfig] = ['name' => $fieldConfig];
}
Expand Down Expand Up @@ -777,8 +789,11 @@ public function attachMetaTemplates($data, $metaTemplates, $pruneEmptyDisabled=t
return $data;
}

protected function includeRequestedMetaFields($query)
protected function includeRequestedMetaFields($query, $isREST=false)
{
if (!empty($isREST)) {
return $query->contain(['MetaFields']);
}
$user = $this->Controller->ACL->getUser();
$tableSettings = IndexSetting::getTableSetting($user, $this->Table);
if (empty($tableSettings['visible_meta_column'])) {
Expand Down Expand Up @@ -1362,7 +1377,12 @@ protected function setNestedRelatedCondition($query, $filterParts, $filterValue)
protected function setRelatedCondition($query, $modelName, $fieldName, $filterValue)
{
return $query->matching($modelName, function (\Cake\ORM\Query $q) use ($fieldName, $filterValue) {
return $this->setValueCondition($q, $fieldName, $filterValue);
if (is_array($filterValue)) {
$query = $this->setInCondition($q, $fieldName, $filterValue);
} else {
$query = $this->setValueCondition($q, $fieldName, $filterValue);
}
return $query;
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/Controller/InboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

class InboxController extends AppController
{
public $filterFields = ['scope', 'action', 'Inbox.created', 'severity', 'title', 'origin', 'message', 'Users.id', 'Users.username',];
public $filterFields = ['scope', 'action', 'Inbox.created', 'severity', 'title', 'origin', 'message', 'Users.id', ['name' => 'Users.username', 'multiple' => true, 'options' => 'getAllUsername', 'select2' => true],];
public $quickFilterFields = ['scope', 'action', ['title' => true], ['message' => true], 'origin'];
public $containFields = ['Users'];

public $paginate = [
'order' => [
'Inbox.created' => 'desc'
]
],
];

public function beforeFilter(EventInterface $event)
Expand Down
7 changes: 5 additions & 2 deletions src/Model/Behavior/NotifyAdminsBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public function afterDelete(EventInterface $event, EntityInterface $entity, Arra
public function isNotificationAllowed(EventInterface $event, EntityInterface $entity, ArrayObject $options): bool
{
$loggedUser = Configure::read('loggedUser');
if (empty($loggedUser) || !empty($loggedUser['role']['perm_admin']) || !empty($loggedUser['role']['perm_sync'])) {
if (
empty(Configure::read('inbox.data_change_notify_for_all', false)) &&
(empty($loggedUser) || !empty($loggedUser['role']['perm_admin']) || !empty($loggedUser['role']['perm_sync']))
) {
return false;
}
return true;
Expand Down Expand Up @@ -322,7 +325,7 @@ protected function _serializeFields($fields): array
} else if (is_object($fieldValue)) {
switch (get_class($fieldValue)) {
case 'Cake\I18n\FrozenTime':
return $fieldValue->i18nFormat('yyyy-mm-dd HH:mm:ss');
return $fieldValue->i18nFormat('yyyy-MM-dd HH:mm:ss');
}
} else {
return strval($fieldValue);
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Entity/AppModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function rearrangeMetaFields(array $options = []): void
$this->meta_fields[$i]['template_namespace'] = $templates[$templateDirectoryId]['namespace'];
}
}
if (!empty($options['smartFlattenMetafields'])) {
if (!empty($this->meta_fields) && !empty($options['smartFlattenMetafields'])) {
$smartFlatten = [];
foreach ($this->meta_fields as $metafield) {
$key = "{$metafield['template_name']}_v{$metafield['template_version']}:{$metafield['field']}";
Expand Down
12 changes: 12 additions & 0 deletions src/Model/Table/InboxTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace App\Model\Table;

use App\Model\Table\AppTable;
use Cake\Utility\Hash;
use Cake\Database\Schema\TableSchemaInterface;
use Cake\Database\Type;
use Cake\ORM\Table;
Expand Down Expand Up @@ -72,6 +73,17 @@ public function buildRules(RulesChecker $rules): RulesChecker
return $rules;
}

public function getAllUsername($currentUser): array
{
$this->Users = \Cake\ORM\TableRegistry::getTableLocator()->get('Users');
$conditions = [];
if (empty($currentUser['role']['perm_admin'])) {
$conditions['organisation_id IN'] = [$currentUser['organisation_id']];
}
$users = $this->Users->find()->where($conditions)->all()->extract('username')->toList();
return Hash::combine($users, '{n}', '{n}');
}

public function checkUserBelongsToBroodOwnerOrg($user, $entryData) {
$this->Broods = \Cake\ORM\TableRegistry::getTableLocator()->get('Broods');
$this->Individuals = \Cake\ORM\TableRegistry::getTableLocator()->get('Individuals');
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Table/IndividualsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function buildRules(RulesChecker $rules): RulesChecker
public function validationDefault(Validator $validator): Validator
{
$validator
->notEmptyString('email')
->email('email')
->requirePresence(['email'], 'create');
return $validator;
}
Expand Down
15 changes: 14 additions & 1 deletion src/Model/Table/SettingProviders/CerebrateSettingsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,20 @@ protected function generateSettingsConfiguration()
],
]
]
]
],
'Inbox' => [
'Data change notification' => [
'Data change notification' => [
'inbox.data_change_notify_for_all' => [
'name' => __('Notify data modification for all Users'),
'type' => 'boolean',
'description' => __('Turning this option ON will alert administrators whenever data is modified, irrespective of the user\'s role responsible for the modification.'),
'default' => false,
'severity' => 'warning',
],
],
],
],
/*
'Features' => [
'Demo Settings' => [
Expand Down
2 changes: 1 addition & 1 deletion src/VERSION.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "1.17",
"version": "1.18",
"application": "Cerebrate"
}
6 changes: 3 additions & 3 deletions templates/Inbox/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
'fields' => [
[
'name' => '#',
'sort' => 'id',
'sort' => 'Inbox.id',
'data_path' => 'id',
],
[
'name' => 'created',
'sort' => 'created',
'sort' => 'Inbox.created',
'data_path' => 'created',
'element' => 'datetime'
],
Expand Down Expand Up @@ -87,7 +87,7 @@
],
[
'name' => 'user',
'sort' => 'user_id',
'sort' => 'Inbox.user_id',
'data_path' => 'user',
'element' => 'user'
],
Expand Down
29 changes: 21 additions & 8 deletions templates/genericTemplates/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,22 @@
'label' => '',
'class' => 'fieldValue form-control-sm'
];
if (!empty($filtersConfig[$fieldName]['multiple'])) {
$fieldData['type'] = 'dropdown';
$fieldData['multiple'] = true;
$fieldData['select2'] = [
'tags' => true,
'tokenSeparators' => [',', ' '],
];
if (!empty($filtersConfig[$fieldName])) {
if (!empty($filtersConfig[$fieldName]['options'])) {
$fieldData['options'] = $filtersConfig[$fieldName]['options'];
}
if (!empty($filtersConfig[$fieldName]['select2'])) {
$fieldData['type'] = 'dropdown';
$fieldData['select2'] = true;
}
if (!empty($filtersConfig[$fieldName]['multiple'])) {
$fieldData['type'] = 'dropdown';
$fieldData['multiple'] = true;
$fieldData['select2'] = [
'tags' => true,
'tokenSeparators' => [',', ' '],
];
}
}
$this->Form->setTemplates([
'formGroup' => '<div class="col-sm-10">{{input}}</div>',
Expand Down Expand Up @@ -187,6 +196,7 @@ function setFilteringValues($filteringTable, field, value, operator) {
$row = $filteringTable.find('td > span.fieldName').filter(function() {
return $(this).data('fieldname') == field
}).closest('tr')
$row.addClass('table-warning')
$row.find('.fieldOperator').val(operator)
const $formElement = $row.find('.fieldValue');
if ($formElement.attr('type') === 'datetime-local') {
Expand All @@ -195,7 +205,10 @@ function setFilteringValues($filteringTable, field, value, operator) {
let newOptions = [];
value.forEach(aValue => {
const existingOption = $formElement.find('option').filter(function() {
return $(this).val() === aValue
if ($(this).val() === aValue) {
$(this).prop('selected', true)
return true
}
})
if (existingOption.length == 0) {
newOptions.push(new Option(aValue, aValue, true, true))
Expand Down

0 comments on commit 26ec0b6

Please sign in to comment.