| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Services\Intergrations\FluentCRM\Automations; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Task; |
| 6 |
use FluentBoards\App\Services\Constant; |
| 7 |
use FluentBoards\App\Services\TaskService; |
| 8 |
use FluentCrm\App\Services\Funnel\BaseAction; |
| 9 |
use FluentCrm\App\Services\Funnel\FunnelHelper; |
| 10 |
use FluentCrm\Framework\Support\Arr; |
| 11 |
use FluentBoards\App\Services\Helper; |
| 12 |
|
| 13 |
class TaskCreateAction extends BaseAction |
| 14 |
{ |
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
$this->actionName = 'add_task_to_board'; |
| 18 |
$this->priority = 20; |
| 19 |
parent::__construct(); |
| 20 |
} |
| 21 |
|
| 22 |
public function getBlock() |
| 23 |
{ |
| 24 |
return [ |
| 25 |
'category' => __('FluentBoards', 'fluent-boards'), |
| 26 |
'title' => __('Create Task', 'fluent-boards'), |
| 27 |
'description' => __('Create Task to the selected Board', 'fluent-boards'), |
| 28 |
'icon' => 'fc-icon-apply_list', |
| 29 |
'settings' => [ |
| 30 |
'stage' => [], |
| 31 |
'create_task_type' => 'new', |
| 32 |
'title' => 'Task from automation of {{contact.email}}' |
| 33 |
] |
| 34 |
]; |
| 35 |
} |
| 36 |
|
| 37 |
public function getBlockFields() |
| 38 |
{ |
| 39 |
return [ |
| 40 |
'title' => __('Create Task', 'fluent-boards'), |
| 41 |
'sub_title' => __('Select which Board & Stage where task will be created', 'fluent-boards'), |
| 42 |
'fields' => [ |
| 43 |
'stage' => [ |
| 44 |
'type' => 'grouped-select', |
| 45 |
'is_multiple' => false, |
| 46 |
'label' => __('Select Board & It\'s stage', 'fluent-boards'), |
| 47 |
'placeholder' => __('Select Board & It\'s stage', 'fluent-boards'), |
| 48 |
'options' => Helper::getStagesByBoardGroup() |
| 49 |
], |
| 50 |
|
| 51 |
'create_task_type' => [ |
| 52 |
'type' => 'radio', |
| 53 |
'wrapper_class' => 'fc_half_field', |
| 54 |
'label' => __('Task Create Type', 'fluent-boards'), |
| 55 |
'options' => [ |
| 56 |
[ |
| 57 |
'id' => 'new', |
| 58 |
'title' => __('Create from stratch', 'fluent-boards') |
| 59 |
], |
| 60 |
[ |
| 61 |
'id' => 'template', |
| 62 |
'title' => __('Create from template', 'fluent-boards') |
| 63 |
] |
| 64 |
] |
| 65 |
], |
| 66 |
|
| 67 |
'task_template' => [ |
| 68 |
'type' => 'rest_selector', |
| 69 |
'option_key' => 'task_templates', |
| 70 |
'is_multiple' => false, |
| 71 |
'label' => __('Select Task Template', 'fluent-boards'), |
| 72 |
'placeholder' => __('Select Template', 'fluent-boards'), |
| 73 |
'dependency' => [ |
| 74 |
'depends_on' => 'create_task_type', |
| 75 |
'operator' => '=', |
| 76 |
'value' => 'template' |
| 77 |
] |
| 78 |
], |
| 79 |
|
| 80 |
'title_template' => [ |
| 81 |
'type' => 'input-text-popper', |
| 82 |
'field_type' => 'text', |
| 83 |
'label' => __('Task Title', 'fluent-boards'), |
| 84 |
'placeholder' => __('Task Title', 'fluent-boards'), |
| 85 |
'inline_help' => __('Leaving it blank will make the title the same as the template task title.', 'fluent-boards'), |
| 86 |
'dependency' => [ |
| 87 |
'depends_on' => 'create_task_type', |
| 88 |
'operator' => '=', |
| 89 |
'value' => 'template' |
| 90 |
] |
| 91 |
], |
| 92 |
|
| 93 |
'title' => [ |
| 94 |
'type' => 'input-text-popper', |
| 95 |
'field_type' => 'text', |
| 96 |
'label' => __('Task Title', 'fluent-boards'), |
| 97 |
'placeholder' => __('Task Title', 'fluent-boards'), |
| 98 |
'dependency' => [ |
| 99 |
'depends_on' => 'create_task_type', |
| 100 |
'operator' => '=', |
| 101 |
'value' => 'new' |
| 102 |
] |
| 103 |
], |
| 104 |
|
| 105 |
'due_day' => [ |
| 106 |
'label' => __('Due Date', 'fluent-boards'), |
| 107 |
'type' => 'input-number', |
| 108 |
'inline_help' => __('Set to your_input days after task creation, values less than zero will set due date to null', 'fluent-boards'), |
| 109 |
'dependency' => [ |
| 110 |
'depends_on' => 'create_task_type', |
| 111 |
'operator' => '=', |
| 112 |
'value' => 'new' |
| 113 |
] |
| 114 |
], |
| 115 |
|
| 116 |
'description' => [ |
| 117 |
'type' => 'html_editor', |
| 118 |
'smart_codes' => 'yes', |
| 119 |
'context_codes' => 'yes', |
| 120 |
'label' => __('Description', 'fluent-boards'), |
| 121 |
'dependency' => [ |
| 122 |
'depends_on' => 'create_task_type', |
| 123 |
'operator' => '=', |
| 124 |
'value' => 'new' |
| 125 |
] |
| 126 |
], |
| 127 |
|
| 128 |
'priority' => [ |
| 129 |
'type' => 'select', |
| 130 |
'label' => __('Select Priority', 'fluent-boards'), |
| 131 |
'options' => Helper::getPriorityOptions(), |
| 132 |
'inline_help' => __('Keeping it blank will select priority to low', 'fluent-boards'), |
| 133 |
'dependency' => [ |
| 134 |
'depends_on' => 'create_task_type', |
| 135 |
'operator' => '=', |
| 136 |
'value' => 'new' |
| 137 |
] |
| 138 |
] |
| 139 |
] |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
public function handle( $subscriber, $sequence, $funnelSubscriberId, $funnelMetric ) |
| 144 |
{ |
| 145 |
$data = $sequence->settings; |
| 146 |
|
| 147 |
$createType = Arr::get($data, 'create_task_type'); |
| 148 |
$stage = Arr::get($data, 'stage'); // this is a string of the pipeline stage id |
| 149 |
|
| 150 |
if ( empty($stage) ) { |
| 151 |
FunnelHelper::changeFunnelSubSequenceStatus( $funnelSubscriberId, $sequence->id, 'skipped' ); |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
$stageId = (int) $stage; |
| 156 |
if ($stageId <= 0) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
$board = Helper::getBoardByStageId( $stageId ); |
| 161 |
|
| 162 |
if ($createType === 'template') { |
| 163 |
$templateTaskId = Arr::get($data, 'task_template'); |
| 164 |
if (!$templateTaskId) { |
| 165 |
FunnelHelper::changeFunnelSubSequenceStatus($funnelSubscriberId, $sequence->id, 'skipped'); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
$title = Arr::get($data, 'title_template'); |
| 170 |
|
| 171 |
$templateTask = Task::find($templateTaskId); |
| 172 |
|
| 173 |
$taskData = array(); |
| 174 |
$taskData['board_id'] = $board->id; |
| 175 |
$taskData['stage_id'] = $stageId; |
| 176 |
$taskData['assignee'] = true; |
| 177 |
$taskData['label'] = true; |
| 178 |
|
| 179 |
$taskService = new TaskService(); |
| 180 |
|
| 181 |
$task = new Task(); |
| 182 |
|
| 183 |
$task->fill($templateTask->toArray()); |
| 184 |
|
| 185 |
if (!empty($title)) { |
| 186 |
$task['title'] = $title; |
| 187 |
} |
| 188 |
|
| 189 |
$task['board_id'] = $taskData['board_id']; |
| 190 |
$task['stage_id'] = $taskData['stage_id']; |
| 191 |
$task['created_by'] = get_current_user_id(); |
| 192 |
$task['comments_count'] = 0; |
| 193 |
$task->moveToNewPosition(1); |
| 194 |
$task->save(); |
| 195 |
|
| 196 |
if (isset($taskData['assignee']) && $taskData['assignee'] == 'true') { |
| 197 |
$templateTask->load('assignees'); |
| 198 |
foreach ($templateTask->assignees as $assignee) { |
| 199 |
$taskService->updateAssignee($assignee->ID, $task); |
| 200 |
} |
| 201 |
} |
| 202 |
if (isset($taskData['label']) && $taskData['label'] == 'true') { |
| 203 |
$templateTask->load('labels'); |
| 204 |
foreach ($templateTask->labels as $label) { |
| 205 |
$task->labels()->syncWithoutDetaching([$label->id => ['object_type' => Constant::OBJECT_TYPE_TASK_LABEL]]); |
| 206 |
} |
| 207 |
} |
| 208 |
} else { |
| 209 |
$title = Arr::get( $data, 'title'); |
| 210 |
$priority = Arr::get( $data, 'priority'); |
| 211 |
$due_day = Arr::get( $data, 'due_day'); |
| 212 |
|
| 213 |
if(isset($due_day)) { |
| 214 |
$due_date = Helper::dueDateConversion($due_day, 'day'); |
| 215 |
} |
| 216 |
|
| 217 |
$description = Arr::get( $data, 'description'); |
| 218 |
|
| 219 |
$description = apply_filters('fluent_crm/parse_campaign_email_text', $description, $subscriber); |
| 220 |
$title = apply_filters('fluent_crm/parse_campaign_email_text', $title, $subscriber); |
| 221 |
|
| 222 |
(new Task())->createTask([ |
| 223 |
'title' => $title, |
| 224 |
'board_id' => $board->id, |
| 225 |
'crm_contact_id' => $subscriber->id, |
| 226 |
'type' => 'task', |
| 227 |
'status' => 'open', |
| 228 |
'stage_id' => $stageId, |
| 229 |
'source' => 'funnel', |
| 230 |
'description' => $description, |
| 231 |
'priority' => $priority ?? 'low', |
| 232 |
'due_at' => $due_date ?? null, |
| 233 |
'position' => (new TaskService())->getLastPositionOfTasks($stageId), |
| 234 |
]); |
| 235 |
} |
| 236 |
|
| 237 |
FunnelHelper::changeFunnelSubSequenceStatus($funnelSubscriberId, $sequence->id, 'completed'); |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
} |
| 242 |
|