2020-03-11 12:05:05 +01:00
< ? php
/**
2020-09-06 11:38:10 +02:00
* Invoice Ninja ( https :// invoiceninja . com ) .
2020-03-11 12:05:05 +01:00
*
* @ link https :// github . com / invoiceninja / invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @ copyright Copyright ( c ) 2023. Invoice Ninja LLC ( https :// invoiceninja . com )
2020-03-11 12:05:05 +01:00
*
2021-06-16 08:58:16 +02:00
* @ license https :// www . elastic . co / licensing / elastic - license
2020-03-11 12:05:05 +01:00
*/
namespace App\Utils\Traits\Notifications ;
2021-11-27 09:59:55 +01:00
use App\Models\Client ;
2021-11-28 05:05:59 +01:00
use App\Models\Credit ;
2021-11-27 09:59:55 +01:00
use App\Models\Invoice ;
use App\Models\Payment ;
2023-01-19 07:20:31 +01:00
use App\Models\Product ;
2022-06-15 08:27:21 +02:00
use App\Models\PurchaseOrder ;
2021-11-27 09:59:55 +01:00
use App\Models\Quote ;
2020-03-11 12:05:05 +01:00
/**
2020-09-06 11:38:10 +02:00
* Class UserNotifies .
2021-02-14 22:32:59 +01:00
*
* I think the term $required_permissions is confusing here , what
2022-06-21 11:57:17 +02:00
* we are actually defining is the notifications available on the
2021-02-14 22:32:59 +01:00
* user itself .
2020-03-11 12:05:05 +01:00
*/
trait UserNotifies
{
public function findUserNotificationTypes ( $invitation , $company_user , $entity_name , $required_permissions ) : array
{
$notifiable_methods = [];
$notifications = $company_user -> notifications ;
2022-06-21 11:57:17 +02:00
if ( $invitation -> company -> is_disabled &&
is_array ( $notifications -> email ) ||
2022-05-17 03:18:34 +02:00
$company_user -> trashed () ||
2022-06-21 11:57:17 +02:00
! $company_user -> user ||
2022-05-17 03:18:34 +02:00
$company_user -> user -> trashed ()) {
2021-11-28 05:05:59 +01:00
return [];
}
2022-06-21 11:57:17 +02:00
2021-02-01 06:30:28 +01:00
//if a user owns this record or is assigned to it, they are attached the permission for notification.
2023-01-19 07:20:31 +01:00
if ( $invitation -> { $entity_name } -> user_id == $company_user -> user_id || $invitation -> { $entity_name } -> assigned_user_id == $company_user -> user_id ) {
2023-01-24 22:26:32 +01:00
// $required_permissions = $this->addSpecialUserPermissionForEntity($invitation->{$entity_name}, $required_permissions);
2022-06-21 11:57:17 +02:00
} else {
2021-11-27 10:24:31 +01:00
$required_permissions = $this -> removeSpecialUserPermissionForEntity ( $invitation -> { $entity_name }, $required_permissions );
}
2020-03-11 12:05:05 +01:00
2021-11-27 10:24:31 +01:00
if ( count ( array_intersect ( $required_permissions , $notifications -> email )) >= 1 ) {
2020-03-11 12:05:05 +01:00
array_push ( $notifiable_methods , 'mail' );
2020-03-21 06:37:30 +01:00
}
2020-03-11 12:05:05 +01:00
return $notifiable_methods ;
}
2020-04-01 10:54:22 +02:00
2021-11-27 10:24:31 +01:00
public function findUserEntityNotificationType ( $entity , $company_user , array $required_permissions ) : array
2020-04-01 10:54:22 +02:00
{
$notifiable_methods = [];
$notifications = $company_user -> notifications ;
2022-06-21 11:57:17 +02:00
if ( $entity -> company -> is_disabled ||
! $notifications ||
2022-05-17 03:18:34 +02:00
$company_user -> trashed () ||
2022-06-21 11:57:17 +02:00
! $company_user -> user ||
2022-05-17 03:18:34 +02:00
$company_user -> user -> trashed ()) {
2020-06-05 02:24:02 +02:00
return [];
2020-09-06 11:38:10 +02:00
}
2020-06-05 02:24:02 +02:00
2023-01-19 07:20:31 +01:00
if ( $entity -> user_id == $company_user -> user_id || $entity -> assigned_user_id == $company_user -> user_id ) {
2021-11-27 09:59:55 +01:00
$required_permissions = $this -> addSpecialUserPermissionForEntity ( $entity , $required_permissions );
2022-06-21 11:57:17 +02:00
} else {
2021-11-27 09:59:55 +01:00
$required_permissions = $this -> removeSpecialUserPermissionForEntity ( $entity , $required_permissions );
2020-04-01 10:54:22 +02:00
}
2021-11-27 10:24:31 +01:00
if ( count ( array_intersect ( $required_permissions , $notifications -> email )) >= 1 ) {
2020-04-01 10:54:22 +02:00
array_push ( $notifiable_methods , 'mail' );
}
return $notifiable_methods ;
}
2021-11-27 10:24:31 +01:00
private function addSpecialUserPermissionForEntity ( $entity , array $required_permissions ) : array
2021-11-27 09:59:55 +01:00
{
2023-01-24 22:26:32 +01:00
return array_merge ( $required_permissions , [ 'all_notifications' , 'all_user_notifications' ]);
// switch ($entity) {
// case $entity instanceof Payment || $entity instanceof Client: //we pass client also as this is the proxy for Payment Failures (ie, there is no payment)
// return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'payment_failure_user', 'payment_success_user']);
// case $entity instanceof Invoice:
// return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'invoice_created_user', 'invoice_sent_user', 'invoice_viewed_user', 'invoice_late_user']);
// case $entity instanceof Quote:
// return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'quote_created_user', 'quote_sent_user', 'quote_viewed_user', 'quote_approved_user', 'quote_expired_user']);
// case $entity instanceof Credit:
// return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'credit_created_user', 'credit_sent_user', 'credit_viewed_user']);
// case $entity instanceof PurchaseOrder:
// return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'purchase_order_created_user', 'purchase_order_sent_user', 'purchase_order_viewed_user']);
// case $entity instanceof Product:
// return array_merge($required_permissions, ['all_notifications', 'all_user_notifications', 'inventory_user', 'inventory_all']);
// default:
// return [];
// }
2021-11-27 09:59:55 +01:00
}
private function removeSpecialUserPermissionForEntity ( $entity , $required_permissions )
{
2022-06-21 11:57:17 +02:00
array_merge ( $required_permissions , [ 'all_notifications' ]);
2021-11-27 10:24:31 +01:00
2021-11-27 09:59:55 +01:00
switch ( $entity ) {
2022-06-21 11:57:17 +02:00
case $entity instanceof Payment || $entity instanceof Client : //we pass client also as this is the proxy for Payment Failures (ie, there is no payment)
return array_diff ( $required_permissions , [ 'all_user_notifications' , 'payment_failure_user' , 'payment_success_user' ]);
case $entity instanceof Invoice :
return array_diff ( $required_permissions , [ 'all_user_notifications' , 'invoice_created_user' , 'invoice_sent_user' , 'invoice_viewed_user' , 'invoice_late_user' ]);
case $entity instanceof Quote :
return array_diff ( $required_permissions , [ 'all_user_notifications' , 'quote_created_user' , 'quote_sent_user' , 'quote_viewed_user' , 'quote_approved_user' , 'quote_expired_user' ]);
case $entity instanceof Credit :
return array_diff ( $required_permissions , [ 'all_user_notifications' , 'credit_created_user' , 'credit_sent_user' , 'credit_viewed_user' ]);
case $entity instanceof PurchaseOrder :
return array_diff ( $required_permissions , [ 'all_user_notifications' , 'purchase_order_created_user' , 'purchase_order_sent_user' , 'purchase_order_viewed_user' ]);
2023-01-19 07:20:31 +01:00
case $entity instanceof Product :
return array_diff ( $required_permissions , [ 'all_user_notifications' , 'inventory_user' ]);
2021-11-27 09:59:55 +01:00
default :
// code...
break ;
}
}
2020-05-26 12:22:50 +02:00
public function findCompanyUserNotificationType ( $company_user , $required_permissions ) : array
{
2022-06-21 11:57:17 +02:00
if ( $company_user -> company -> is_disabled ||
$company_user -> trashed () ||
! $company_user -> user ||
2022-05-17 03:18:34 +02:00
$company_user -> user -> trashed ()) {
2020-05-26 12:22:50 +02:00
return [];
}
$notifiable_methods = [];
$notifications = $company_user -> notifications ;
2021-02-14 22:32:59 +01:00
//conditional to define whether the company user has the required notification for the MAIL notification TYPE
2020-09-06 11:38:10 +02:00
if ( count ( array_intersect ( $required_permissions , $notifications -> email )) >= 1 || count ( array_intersect ( $required_permissions , [ 'all_user_notifications' ])) >= 1 || count ( array_intersect ( $required_permissions , [ 'all_notifications' ])) >= 1 ) {
2020-05-26 12:22:50 +02:00
array_push ( $notifiable_methods , 'mail' );
}
return $notifiable_methods ;
}
2021-02-14 22:32:59 +01:00
/*
* Returns a filtered collection of users with the
2022-06-21 11:57:17 +02:00
* required notification - NOTE this is only implemented for
2021-02-14 22:32:59 +01:00
* EMAIL notification types - we ' ll need to chain
* additional types at a later stage .
*/
public function filterUsersByPermissions ( $company_users , $entity , array $required_notification )
{
2022-06-21 11:57:17 +02:00
return $company_users -> filter ( function ( $company_user ) use ( $required_notification , $entity ) {
2021-02-14 23:12:14 +01:00
return $this -> checkNotificationExists ( $company_user , $entity , $required_notification );
2021-02-14 22:32:59 +01:00
});
}
2023-01-19 07:20:31 +01:00
/**
* Underrated method right here , last ones
* are always the best
2023-02-16 02:36:09 +01:00
*
* @ param CompanyUser $company_user
2023-01-19 07:20:31 +01:00
* @ param Invoice | Quote | Credit | PurchaseOrder | Product $entity
2023-02-16 02:36:09 +01:00
* @ param array $required_notification
*
* @ return bool
2023-01-19 07:20:31 +01:00
*/
private function checkNotificationExists ( $company_user , $entity , $required_notification ) : bool
2021-02-14 22:32:59 +01:00
{
/* Always make sure we push the `all_notificaitons` into the mix */
array_push ( $required_notification , 'all_notifications' );
/* Selectively add the all_user if the user is associated with the entity */
2023-01-19 07:20:31 +01:00
if ( $entity -> user_id == $company_user -> user_id || $entity -> assigned_user_id == $company_user -> user_id ) {
2021-02-14 22:32:59 +01:00
array_push ( $required_notification , 'all_user_notifications' );
2022-06-21 11:57:17 +02:00
}
2021-02-14 22:32:59 +01:00
return count ( array_intersect ( $required_notification , $company_user -> notifications -> email )) >= 1 ;
}
2020-03-21 06:37:30 +01:00
}