2021-03-15 22:47:13 +01:00
< ? php
namespace App\Model\Table ;
use App\Model\Table\AppTable ;
use Cake\Database\Schema\TableSchemaInterface ;
use Cake\Database\Type ;
use Cake\ORM\Table ;
use Cake\ORM\RulesChecker ;
use Cake\Validation\Validator ;
2021-06-14 16:46:53 +02:00
use Cake\Http\Exception\NotFoundException ;
2022-10-25 14:38:16 +02:00
use Cake\ORM\ResultSet ;
2021-03-15 22:47:13 +01:00
2022-01-24 15:13:28 +01:00
use App\Utility\UI\Notification ;
2021-03-15 22:47:13 +01:00
Type :: map ( 'json' , 'Cake\Database\Type\JsonType' );
class InboxTable extends AppTable
{
2022-10-25 14:38:16 +02:00
public const SEVERITY_PRIMARY = 0 ,
SEVERITY_INFO = 1 ,
SEVERITY_WARNING = 2 ,
SEVERITY_DANGER = 3 ;
public $severityVariant = [
self :: SEVERITY_PRIMARY => 'primary' ,
self :: SEVERITY_INFO => 'info' ,
self :: SEVERITY_WARNING => 'warning' ,
self :: SEVERITY_DANGER => 'danger' ,
];
2021-03-15 22:47:13 +01:00
public function initialize ( array $config ) : void
{
parent :: initialize ( $config );
$this -> addBehavior ( 'UUID' );
2021-10-21 11:27:02 +02:00
$this -> addBehavior ( 'Timestamp' );
2021-11-17 15:43:52 +01:00
$this -> addBehavior ( 'AuditLog' );
2021-03-15 22:47:13 +01:00
$this -> belongsTo ( 'Users' );
$this -> setDisplayField ( 'title' );
}
protected function _initializeSchema ( TableSchemaInterface $schema ) : TableSchemaInterface
{
$schema -> setColumnType ( 'data' , 'json' );
return $schema ;
}
public function validationDefault ( Validator $validator ) : Validator
{
$validator
-> notEmptyString ( 'scope' )
-> notEmptyString ( 'action' )
-> notEmptyString ( 'title' )
-> notEmptyString ( 'origin' )
-> datetime ( 'created' )
2021-06-14 16:46:53 +02:00
2021-03-15 22:47:13 +01:00
-> requirePresence ([
'scope' => [ 'message' => __ ( 'The field `scope` is required' )],
'action' => [ 'message' => __ ( 'The field `action` is required' )],
'title' => [ 'message' => __ ( 'The field `title` is required' )],
'origin' => [ 'message' => __ ( 'The field `origin` is required' )],
], 'create' );
return $validator ;
}
public function buildRules ( RulesChecker $rules ) : RulesChecker
{
$rules -> add ( $rules -> existsIn ( 'user_id' , 'Users' ), [
'message' => 'The provided `user_id` does not exist'
]);
return $rules ;
}
2021-06-12 15:29:16 +02:00
2021-06-14 16:46:53 +02:00
public function checkUserBelongsToBroodOwnerOrg ( $user , $entryData ) {
$this -> Broods = \Cake\ORM\TableRegistry :: getTableLocator () -> get ( 'Broods' );
$this -> Individuals = \Cake\ORM\TableRegistry :: getTableLocator () -> get ( 'Individuals' );
$errors = [];
2022-01-24 17:37:32 +01:00
$originUrl = trim ( $entryData [ 'origin' ], '/' );
2021-06-14 16:46:53 +02:00
$brood = $this -> Broods -> find ()
2022-01-24 17:37:32 +01:00
-> where ([
'url IN' => [ $originUrl , " { $originUrl } / " ]
])
2021-06-14 16:46:53 +02:00
-> first ();
if ( empty ( $brood )) {
$errors [] = __ ( 'Unkown brood `{0}`' , $entryData [ 'data' ][ 'cerebrateURL' ]);
}
2021-11-17 15:43:52 +01:00
2021-06-29 11:37:49 +02:00
// $found = false;
// foreach ($user->individual->organisations as $organisations) {
// if ($organisations->id == $brood->organisation_id) {
// $found = true;
// }
// }
// if (!$found) {
// $errors[] = __('User `{0}` is not part of the brood\'s organisation. Make sure `{0}` is aligned with the organisation owning the brood.', $user->individual->email);
// }
2021-06-14 16:46:53 +02:00
return $errors ;
}
2021-06-19 13:16:25 +02:00
public function createEntry ( $entryData )
{
$savedEntry = $this -> save ( $entryData );
return $savedEntry ;
}
2022-01-24 15:13:28 +01:00
2022-01-25 11:32:09 +01:00
public function collectNotifications ( \App\Model\Entity\User $user ) : array
2022-01-24 15:13:28 +01:00
{
$allNotifications = [];
2022-01-25 11:32:09 +01:00
$inboxNotifications = $this -> getNotificationsForUser ( $user );
2022-01-24 15:13:28 +01:00
foreach ( $inboxNotifications as $notification ) {
2022-10-25 14:38:16 +02:00
$title = $notification -> title ;
$details = $notification -> message ;
2022-01-24 15:13:28 +01:00
$router = [
'controller' => 'inbox' ,
2022-01-25 09:32:16 +01:00
'action' => 'process' ,
2022-01-24 15:13:28 +01:00
'plugin' => null ,
2022-01-25 09:32:16 +01:00
$notification -> id
2022-01-24 15:13:28 +01:00
];
$allNotifications [] = ( new Notification ( $title , $router , [
'icon' => 'envelope' ,
'details' => $details ,
'datetime' => $notification -> created ,
2022-10-25 14:38:16 +02:00
'variant' => $notification -> severity_variant ,
2022-01-25 09:32:16 +01:00
'_useModal' => true ,
2022-02-08 17:58:30 +01:00
'_sidebarId' => 'inbox' ,
2022-01-24 15:13:28 +01:00
])) -> get ();
}
return $allNotifications ;
}
2022-01-25 11:32:09 +01:00
2022-10-25 14:38:16 +02:00
public function getNotificationsForUser ( \App\Model\Entity\User $user ) : ResultSet
2022-01-25 11:32:09 +01:00
{
$query = $this -> find ();
2022-10-25 14:38:16 +02:00
$conditions = [
'Inbox.user_id' => $user -> id
];
2022-01-25 11:32:09 +01:00
$query -> where ( $conditions );
2022-10-25 14:38:16 +02:00
$notifications = $query -> all ();
2022-01-25 11:32:09 +01:00
return $notifications ;
}
2021-03-15 22:47:13 +01:00
}