| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use DateTimeImmutable; |
| 6 |
use Exception; |
| 7 |
use FluentBoards\App\Models\Activity; |
| 8 |
use FluentBoards\App\Models\User; |
| 9 |
use FluentBoards\App\Services\Constant; |
| 10 |
use FluentCrm\App\Models\Subscriber; |
| 11 |
use FluentBoards\App\Models\Relation; |
| 12 |
use FluentCrm\App\Services\ContactsQuery; |
| 13 |
use FluentCrm\App\Services\PermissionManager as CRMPermissionManager; |
| 14 |
|
| 15 |
class TaskHandler |
| 16 |
{ |
| 17 |
private $fileHandler; |
| 18 |
|
| 19 |
public function __construct(FileHandler $fileHandler) |
| 20 |
{ |
| 21 |
$this->fileHandler = $fileHandler; |
| 22 |
} |
| 23 |
public function taskDeleted($task) |
| 24 |
{ |
| 25 |
// Delete all activities and comments related to this task |
| 26 |
Activity::where('object_id', $task->id)->where('object_type', Constant::ACTIVITY_TASK)->delete(); |
| 27 |
} |
| 28 |
|
| 29 |
public function searchAssignees($options, $search, $includedIds) |
| 30 |
{ |
| 31 |
$users = get_users([ |
| 32 |
'number' => 20, |
| 33 |
'search' => $search, |
| 34 |
]); |
| 35 |
|
| 36 |
$formattedUsers = []; |
| 37 |
|
| 38 |
$pushedIds = []; |
| 39 |
|
| 40 |
foreach ($users as $user) { |
| 41 |
$pushedIds[] = $user->ID; |
| 42 |
|
| 43 |
$formattedUsers[] = [ |
| 44 |
'id' => $user->ID, |
| 45 |
'title' => $user->display_name . ' (' . $user->user_email . ')', |
| 46 |
'photo' => get_avatar_url($user->user_email), |
| 47 |
'left_side_value' => $user->display_name, |
| 48 |
'right_side_value' => $user->user_email, |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
return $formattedUsers; |
| 53 |
|
| 54 |
if (!$includedIds) { |
| 55 |
return $formattedUsers; |
| 56 |
} |
| 57 |
if (!is_array($includedIds)) { |
| 58 |
// $includedIds = [$includedIds]; |
| 59 |
$includedIds = [$includedIds]; |
| 60 |
} |
| 61 |
|
| 62 |
// $includedIds = array_diff($includedIds, $pushedIds); |
| 63 |
// |
| 64 |
if ($includedIds) { |
| 65 |
$users = get_users([ |
| 66 |
'include' => $includedIds, |
| 67 |
]); |
| 68 |
|
| 69 |
foreach ($users as $user) { |
| 70 |
$formattedUsers[] = [ |
| 71 |
'id3' => $user->ID, |
| 72 |
'title' => $user->display_name . ' (' . $user->user_email . ')', |
| 73 |
'photo_' => /* get photo by gravitar or something */ get_avatar_url($user->user_email, ['size' => 50]), |
| 74 |
'left_side_value' => $user->display_name, |
| 75 |
'right_side_value' => $user->user_email, |
| 76 |
'right_side_value1' => $user->user_email, |
| 77 |
]; |
| 78 |
} |
| 79 |
} |
| 80 |
// dd($formattedUsers); |
| 81 |
|
| 82 |
return $formattedUsers; |
| 83 |
} |
| 84 |
|
| 85 |
public function searchNonBoardWordpressUsers() |
| 86 |
{ |
| 87 |
$superAdmin = Relation::select('user_id')->distinct()->pluck('user_id'); |
| 88 |
$users = User::whereDoesntHave('boards')->whereNotIn('ID', $superAdmin)->get(); |
| 89 |
|
| 90 |
$formattedUsers = []; |
| 91 |
|
| 92 |
foreach ($users as $user) { |
| 93 |
$formattedUsers[] = [ |
| 94 |
'id' => $user->ID, |
| 95 |
'photo' => $user->photo, |
| 96 |
'title' => $user->display_name . ' (' . $user->user_email . ')', |
| 97 |
'left_side_value' => $user->display_name, |
| 98 |
'right_side_value' => $user->user_email, |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
return $formattedUsers; |
| 103 |
} |
| 104 |
|
| 105 |
public function searchContact($options, $search, $includedIds) |
| 106 |
{ |
| 107 |
if (!defined('FLUENTCRM')) { |
| 108 |
return []; |
| 109 |
} |
| 110 |
|
| 111 |
// check if the use has permission to access contacts |
| 112 |
$contactPermission = CRMPermissionManager::currentUserCan('fcrm_read_contacts'); |
| 113 |
if(!$contactPermission) { |
| 114 |
return []; |
| 115 |
} |
| 116 |
|
| 117 |
$contactsQuery = new ContactsQuery([ |
| 118 |
'search' => $search, |
| 119 |
'with' => [], |
| 120 |
'limit' => 20, |
| 121 |
'offset' => 0, |
| 122 |
]); |
| 123 |
$subscribers = $contactsQuery->get(); |
| 124 |
|
| 125 |
$formattedSubscribers = []; |
| 126 |
|
| 127 |
$pushedIs = []; |
| 128 |
foreach ($subscribers as $subscriber) { |
| 129 |
$pushedIs[] = $subscriber->id; |
| 130 |
$formattedSubscribers[] = [ |
| 131 |
'id' => $subscriber->id, |
| 132 |
'title' => $subscriber->full_name . ' (' . $subscriber->email . ')', |
| 133 |
'photo' => $subscriber->photo, |
| 134 |
'name' => $subscriber->full_name, |
| 135 |
'email' => $subscriber->email, |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
if (!is_array($includedIds)) { |
| 140 |
$includedIds = [$includedIds]; |
| 141 |
} |
| 142 |
|
| 143 |
if (!$includedIds) { |
| 144 |
return $formattedSubscribers; |
| 145 |
} |
| 146 |
|
| 147 |
$remainingIds = array_diff($includedIds, $pushedIs); |
| 148 |
|
| 149 |
if ($remainingIds) { |
| 150 |
$remainingContacts = Subscriber::whereIn('id', $remainingIds)->get(); |
| 151 |
foreach ($remainingContacts as $subscriber) { |
| 152 |
$formattedSubscribers[] = [ |
| 153 |
'id' => $subscriber->id, |
| 154 |
'title' => $subscriber->full_name . ' (' . $subscriber->email . ')', |
| 155 |
'photo' => $subscriber->photo, |
| 156 |
'name' => $subscriber->full_name, |
| 157 |
'email' => $subscriber->email, |
| 158 |
]; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return $formattedSubscribers; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* Summary of taskAttachmentDeleted |
| 169 |
* @param mixed $task |
| 170 |
* @param mixed $deleteUrl |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
public function taskAttachmentDeleted($deletedAttachment) |
| 174 |
{ |
| 175 |
try { |
| 176 |
$deleteUrl = $deletedAttachment->full_url; |
| 177 |
$this->fileHandler->deleteFileByUrl($deleteUrl); |
| 178 |
} catch (Exception $e) { |
| 179 |
wp_send_json_error($e->getMessage()); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|