fluent-boards
/
app
/
Services
/
Intergrations
/
FluentCRM
/
Automations
/
ContactAddedTaskTrigger.php
ContactAddedTaskTrigger.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 2.0.4, at app/Services/Intergrations/FluentCRM/Automations/ContactAddedTaskTrigger.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentBoards\App\Services\Intergrations\FluentCRM\Automations; |
| 4 | |
| 5 | use FluentCrm\App\Services\Funnel\BaseTrigger; |
| 6 | use FluentCrm\App\Services\Funnel\FunnelHelper; |
| 7 | use FluentCrm\App\Services\Funnel\FunnelProcessor; |
| 8 | use FluentCrm\Framework\Support\Arr; |
| 9 | use FluentBoards\App\Services\Helper; |
| 10 | |
| 11 | |
| 12 | class ContactAddedTaskTrigger extends BaseTrigger |
| 13 | { |
| 14 | public function __construct() |
| 15 | { |
| 16 | $this->triggerName = 'fluent_boards/contact_added_to_task'; |
| 17 | $this->actionArgNum = 2; |
| 18 | $this->priority = 20; |
| 19 | |
| 20 | parent::__construct(); |
| 21 | } |
| 22 | |
| 23 | public function getTrigger() |
| 24 | { |
| 25 | return [ |
| 26 | 'category' => __('FluentBoards', 'fluent-boards'), |
| 27 | 'label' => __('Contact Added to Task', 'fluent-boards'), |
| 28 | 'description' => __('This will run when a contact will be added to a task', 'fluent-boards'), |
| 29 | 'icon' => 'fc-icon-list_applied_2' |
| 30 | ]; |
| 31 | } |
| 32 | |
| 33 | public function getFunnelSettingsDefaults() |
| 34 | { |
| 35 | return [ |
| 36 | 'boards' => [], |
| 37 | 'select_type' => 'any' |
| 38 | ]; |
| 39 | } |
| 40 | |
| 41 | public function getSettingsFields($funnel) |
| 42 | { |
| 43 | return [ |
| 44 | 'title' => __('Contact Added to Task', 'fluent-boards'), |
| 45 | 'sub_title' => __('This will run when a contact will be added to a task', 'fluent-boards'), |
| 46 | 'fields' => [ |
| 47 | 'boards' => [ |
| 48 | 'type' => 'rest_selector', |
| 49 | 'option_key' => 'boards', |
| 50 | 'is_multiple' => true, |
| 51 | 'label' => __('Select Board', 'fluent-boards'), |
| 52 | 'placeholder' => __('Select Board', 'fluent-boards'), |
| 53 | ], |
| 54 | 'select_type' => [ |
| 55 | 'label' => __('Run When', 'fluent-boards'), |
| 56 | 'type' => 'radio', |
| 57 | 'options' => [ |
| 58 | [ |
| 59 | 'id' => 'any', |
| 60 | 'title' => __('Contact added to task in any of the selected boards', 'fluent-boards') |
| 61 | ] |
| 62 | ], |
| 63 | 'dependency' => [ |
| 64 | 'depends_on' => 'boards', |
| 65 | 'operator' => '!=', |
| 66 | 'value' => [] |
| 67 | ] |
| 68 | ] |
| 69 | ] |
| 70 | ]; |
| 71 | } |
| 72 | |
| 73 | public function getFunnelConditionDefaults($funnel) |
| 74 | { |
| 75 | return [ |
| 76 | 'run_multiple' => 'no' |
| 77 | ]; |
| 78 | } |
| 79 | |
| 80 | public function getConditionFields($funnel) |
| 81 | { |
| 82 | return [ |
| 83 | 'run_multiple' => [ |
| 84 | 'type' => 'yes_no_check', |
| 85 | 'label' => '', |
| 86 | 'check_label' => __('Restart the Automation Multiple times for a contact for this event. (Only enable if you want to restart automation for the same contact)', 'fluent-boards'), |
| 87 | 'inline_help' => __('If you enable, then it will restart the automation for a contact if the contact already in the automation. Otherwise, It will just skip if already exist', 'fluent-boards') |
| 88 | ] |
| 89 | ]; |
| 90 | } |
| 91 | |
| 92 | public function handle($funnel, $originalArgs) |
| 93 | { |
| 94 | $task = $originalArgs[0]; |
| 95 | |
| 96 | $subscriber = Helper::crm_contact($task->crm_contact_id); |
| 97 | |
| 98 | $willProcess = $this->isProcessable($funnel, $task, $subscriber); |
| 99 | |
| 100 | if (!$willProcess) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | (new FunnelProcessor())->startFunnelSequence($funnel, $subscriber, [ |
| 105 | 'source_trigger_name' => $this->triggerName |
| 106 | ]); |
| 107 | } |
| 108 | |
| 109 | private function isProcessable($funnel, $task, $subscriber) |
| 110 | { |
| 111 | $boards = $funnel->settings['boards']; |
| 112 | |
| 113 | if($boards){ |
| 114 | $attachIntersection = array_intersect($boards, [$task->board_id]); |
| 115 | if(!$attachIntersection) { |
| 116 | return false; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | //check run_only_one |
| 123 | if ($subscriber && FunnelHelper::ifAlreadyInFunnel($funnel->id, $subscriber->id)) { |
| 124 | $multipleRun = Arr::get($funnel->conditions, 'run_multiple') == 'yes'; |
| 125 | if ($multipleRun) { |
| 126 | FunnelHelper::removeSubscribersFromFunnel($funnel->id, [$subscriber->id]); |
| 127 | } else { |
| 128 | return false; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return true; |
| 133 | } |
| 134 | } |