| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\WorkFlow; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Database\WorkFlowModel; |
| 6 |
|
| 7 |
final class WorkFlowHandler |
| 8 |
{ |
| 9 |
private static $_formID; |
| 10 |
private static $_workFlowModel; |
| 11 |
private $_user_details; |
| 12 |
|
| 13 |
public function __construct($formID, array $user_details = null) |
| 14 |
{ |
| 15 |
static::$_formID = $formID; |
| 16 |
static::$_workFlowModel = new WorkFlowModel(); |
| 17 |
$this->_user_details = $user_details; |
| 18 |
} |
| 19 |
|
| 20 |
public function oldgetAllworkFlow() |
| 21 |
{ |
| 22 |
$workFlows = static::$_workFlowModel->get( |
| 23 |
[ |
| 24 |
'id', |
| 25 |
'workflow_name', |
| 26 |
'workflow_type', |
| 27 |
'workflow_run', |
| 28 |
'workflow_behaviour', |
| 29 |
'workflow_condition', |
| 30 |
], |
| 31 |
[ |
| 32 |
'form_id' => static::$_formID, |
| 33 |
], |
| 34 |
null, |
| 35 |
null, |
| 36 |
'id', |
| 37 |
'desc' |
| 38 |
); |
| 39 |
$workFlowsFormated = []; |
| 40 |
if (empty($workFlows) || is_wp_error($workFlows)) { |
| 41 |
return []; |
| 42 |
} |
| 43 |
foreach ($workFlows as $key => $value) { |
| 44 |
$allAction = json_decode($value->workflow_action); |
| 45 |
$workFlow['id'] = $value->id; |
| 46 |
$workFlow['title'] = $value->workflow_name; |
| 47 |
$workFlow['action_type'] = $value->workflow_type; |
| 48 |
$workFlow['action_run'] = $value->workflow_run; |
| 49 |
$workFlow['action_behaviour'] = $value->workflow_behaviour; |
| 50 |
$workFlow['logics'] = json_decode($value->workflow_condition); |
| 51 |
$workFlow['actions'] = $allAction->action; |
| 52 |
$workFlow['successAction'] = $allAction->successAction; |
| 53 |
if (isset($allAction->validateMsg)) { |
| 54 |
$workFlow['validateMsg'] = $allAction->validateMsg; |
| 55 |
} |
| 56 |
if (isset($allAction->avoid_delete)) { |
| 57 |
$workFlow['avoid_delete'] = $allAction->avoid_delete; |
| 58 |
} |
| 59 |
$workFlowsFormated[$key] = $workFlow; |
| 60 |
} |
| 61 |
|
| 62 |
return $workFlowsFormated; |
| 63 |
} |
| 64 |
|
| 65 |
public function getAllworkFlow() |
| 66 |
{ |
| 67 |
$workFlows = static::$_workFlowModel->get( |
| 68 |
[ |
| 69 |
'id', |
| 70 |
'workflow_name', |
| 71 |
'workflow_type', |
| 72 |
'workflow_run', |
| 73 |
'workflow_behaviour', |
| 74 |
'workflow_condition', |
| 75 |
'workflow_order', |
| 76 |
], |
| 77 |
[ |
| 78 |
'form_id' => static::$_formID, |
| 79 |
], |
| 80 |
null, |
| 81 |
null, |
| 82 |
'workflow_order', |
| 83 |
'ASC' |
| 84 |
); |
| 85 |
$workFlowsFormated = []; |
| 86 |
if (empty($workFlows) || is_wp_error($workFlows)) { |
| 87 |
return []; |
| 88 |
} |
| 89 |
foreach ($workFlows as $workFlowKey => $value) { |
| 90 |
$workFlow['id'] = $value->id; |
| 91 |
$workFlow['title'] = $value->workflow_name; |
| 92 |
$workFlow['action_type'] = $value->workflow_type; |
| 93 |
$workFlow['action_run'] = $value->workflow_run; |
| 94 |
$workFlow['action_behaviour'] = $value->workflow_behaviour; |
| 95 |
$workFlow['conditions'] = json_decode($value->workflow_condition); |
| 96 |
|
| 97 |
foreach ($workFlow['conditions'] as $conIndex => $condition) { |
| 98 |
if (property_exists($condition, 'logics')) { |
| 99 |
$workFlow['conditions'][$conIndex]->logics = $condition->logics; |
| 100 |
} |
| 101 |
if (property_exists($condition, 'actions')) { |
| 102 |
$workFlow['conditions'][$conIndex]->actions = $condition->actions; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
$workFlowsFormated[$workFlowKey] = $workFlow; |
| 107 |
} |
| 108 |
|
| 109 |
return $workFlowsFormated; |
| 110 |
} |
| 111 |
|
| 112 |
public function updateworkFlow($workFlowID, $workFlowDetails, $actionIntegrationDetails, $workFlowOrder, $errorCode = []) |
| 113 |
{ |
| 114 |
$conditions = $workFlowDetails->conditions; |
| 115 |
foreach ($conditions as $conIndex => $condition) { |
| 116 |
$actions = $condition->actions; |
| 117 |
if (!empty($actions->success)) { |
| 118 |
foreach ($actions->success as $actionKey => $actionValue) { |
| 119 |
$detailIds = $actionValue->details->id; |
| 120 |
if (!empty($detailIds)) { |
| 121 |
if (is_array($detailIds)) { |
| 122 |
foreach ($detailIds as $key => $id) { |
| 123 |
$actionIntegrationID = \json_decode($id); |
| 124 |
if (isset($actionIntegrationID->index)) { |
| 125 |
$value = json_encode(['id' => (string) $actionIntegrationDetails[$actionValue->type][$actionIntegrationID->index]]); |
| 126 |
$errorCode['workflow'] = 1; |
| 127 |
} else { |
| 128 |
$value = $id; |
| 129 |
} |
| 130 |
$workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->id[$key] = $value; |
| 131 |
} |
| 132 |
} else { |
| 133 |
$actionIntegrationID = \json_decode($detailIds); |
| 134 |
if (isset($actionIntegrationDetails[$actionValue->type])) { |
| 135 |
if (isset($actionIntegrationID->index)) { |
| 136 |
$value = json_encode(['id' => (string) $actionIntegrationDetails[$actionValue->type][$actionIntegrationID->index]]); |
| 137 |
$errorCode['workflow'] = 1; |
| 138 |
} else { |
| 139 |
$value = $actionValue->details->id; |
| 140 |
} |
| 141 |
|
| 142 |
$workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->id = $value; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
// pdfId update |
| 147 |
if (isset($actionIntegrationDetails['pdfTem']) && !empty($actionValue->details->pdfId)) { |
| 148 |
$pdfDetailId = $actionValue->details->pdfId; |
| 149 |
$actionIntegrationID = \json_decode($pdfDetailId); |
| 150 |
if (isset($actionIntegrationID->index)) { |
| 151 |
$value = json_encode(['id' => (string) $actionIntegrationDetails['pdfTem'][$actionIntegrationID->index]]); |
| 152 |
$errorCode['workflow'] = 1; |
| 153 |
} else { |
| 154 |
$value = $actionValue->details->pdfId; |
| 155 |
} |
| 156 |
|
| 157 |
$workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->pdfId = $value; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
if (!empty($actions->failure)) { |
| 162 |
$validateMsgID = \json_decode($actions->failure); |
| 163 |
$failure = isset($validateMsgID->index) ? |
| 164 |
json_encode( |
| 165 |
[ |
| 166 |
'id' => $actionIntegrationDetails['successMsg'][$validateMsgID->index], |
| 167 |
] |
| 168 |
) |
| 169 |
: $actions->failure; |
| 170 |
|
| 171 |
$conditions[$conIndex]->actions->failure = $failure; |
| 172 |
} |
| 173 |
|
| 174 |
if (!empty($actions->avoid_delete)) { |
| 175 |
$conditions[$conIndex]->actions['avoid_delete'] = $workFlowDetails->avoid_delete; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
$status = static::$_workFlowModel->update( |
| 180 |
[ |
| 181 |
'workflow_name' => $workFlowDetails->title, |
| 182 |
'workflow_type' => 'delete' === $workFlowDetails->action_run ? 'delete' : $workFlowDetails->action_type, |
| 183 |
'workflow_run' => $workFlowDetails->action_run, |
| 184 |
'workflow_behaviour' => $workFlowDetails->action_behaviour, |
| 185 |
'workflow_condition' => json_encode($conditions), |
| 186 |
// "workflow_action" => json_encode($workFlowActions), |
| 187 |
'workflow_order' => $workFlowOrder, |
| 188 |
'user_id' => $this->_user_details['id'], |
| 189 |
'user_ip' => $this->_user_details['ip'], |
| 190 |
'user_location' => '', |
| 191 |
'user_device' => $this->_user_details['device'], |
| 192 |
'updated_at' => $this->_user_details['time'], |
| 193 |
], |
| 194 |
[ |
| 195 |
'id' => $workFlowID, |
| 196 |
'form_id' => static::$_formID, |
| 197 |
] |
| 198 |
); |
| 199 |
|
| 200 |
if (is_wp_error($status) && 'result_empty' !== $status->get_error_code()) { |
| 201 |
$errorCode['workflow'] = 2; |
| 202 |
} |
| 203 |
return $errorCode; |
| 204 |
} |
| 205 |
|
| 206 |
public function saveworkFlow($workFlowDetails, $actionIntegrationDetails, $workFlowOrder) |
| 207 |
{ |
| 208 |
$conditions = $workFlowDetails->conditions; |
| 209 |
foreach ($conditions as $conIndex => $condition) { |
| 210 |
$actions = $condition->actions; |
| 211 |
if (property_exists($actions, 'success')) { |
| 212 |
foreach ($actions->success as $actionKey => $actionValue) { |
| 213 |
$detailIds = $actionValue->details->id; |
| 214 |
if (!empty($detailIds)) { |
| 215 |
if (is_array($detailIds)) { |
| 216 |
foreach ($detailIds as $key => $id) { |
| 217 |
$actionIntegrationID = \json_decode($id); |
| 218 |
|
| 219 |
$value = $this->maybeGetSuccessActionID($actionIntegrationID, $actionIntegrationDetails, $actionValue->type); |
| 220 |
|
| 221 |
$workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->id[$key] = $value ? json_encode(['id' => "$value"]) : $value; |
| 222 |
} |
| 223 |
} else { |
| 224 |
$actionIntegrationID = \json_decode($detailIds); |
| 225 |
$value = $this->maybeGetSuccessActionID($actionIntegrationID, $actionIntegrationDetails, $actionValue->type); |
| 226 |
$workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->id = $value ? json_encode(['id' => "$value"]) : $actionValue->details->id; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
// pdfId update |
| 231 |
if (isset($actionIntegrationDetails['pdfTem']) && !empty($actionValue->details->pdfId)) { |
| 232 |
$pdfDetailId = $actionValue->details->pdfId; |
| 233 |
$actionIntegrationID = \json_decode($pdfDetailId); |
| 234 |
$value = $this->maybeGetSuccessActionID($actionIntegrationID, $actionIntegrationDetails, 'pdfTem'); |
| 235 |
|
| 236 |
$workFlowDetails->conditions[$conIndex]->actions->success[$actionKey]->details->pdfId = $value; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
if (!empty($actions->failure)) { |
| 242 |
$validateMsgID = \json_decode($actions->failure); |
| 243 |
$failure = isset($validateMsgID->index) ? |
| 244 |
json_encode( |
| 245 |
[ |
| 246 |
'id' => $actionIntegrationDetails['successMsg'][$validateMsgID->index], |
| 247 |
] |
| 248 |
) |
| 249 |
: $actions->failure; |
| 250 |
|
| 251 |
$conditions[$conIndex]->actions->failure = $failure; |
| 252 |
} |
| 253 |
|
| 254 |
if (!empty($actions->avoid_delete)) { |
| 255 |
$conditions[$conIndex]->actions->avoid_delete = $workFlowDetails->avoid_delete; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
return static::$_workFlowModel->insert( |
| 260 |
[ |
| 261 |
'workflow_name' => $workFlowDetails->title, |
| 262 |
'workflow_type' => 'delete' === $workFlowDetails->action_run ? 'delete' : $workFlowDetails->action_type, |
| 263 |
'workflow_run' => $workFlowDetails->action_run, |
| 264 |
'workflow_behaviour' => $workFlowDetails->action_behaviour, |
| 265 |
'workflow_condition' => json_encode($conditions), |
| 266 |
'workflow_order' => $workFlowOrder, |
| 267 |
'form_id' => static::$_formID, |
| 268 |
'user_id' => $this->_user_details['id'], |
| 269 |
'user_ip' => $this->_user_details['ip'], |
| 270 |
'user_location' => '', |
| 271 |
'user_device' => $this->_user_details['device'], |
| 272 |
'created_at' => $this->_user_details['time'], |
| 273 |
'updated_at' => $this->_user_details['time'], |
| 274 |
] |
| 275 |
); |
| 276 |
} |
| 277 |
|
| 278 |
public function oldupdateworkFlow($workFlowID, $workFlowDetails, $actionIntegrationDetails) |
| 279 |
{ |
| 280 |
// $workFlowActions['action'] = $workFlowDetails->actions; |
| 281 |
// foreach ($workFlowDetails->successAction as $successActionkey => $successActionValue) { |
| 282 |
// if (!empty($successActionValue->details->id)) { |
| 283 |
// if (is_array($successActionValue->details->id)) { |
| 284 |
// foreach ($successActionValue->details->id as $key => $value) { |
| 285 |
// $actionIntegrationID = \json_decode($value); |
| 286 |
// $workFlowDetails->successAction[$successActionkey]->details->id[$key] |
| 287 |
// = isset($actionIntegrationID->index) ? |
| 288 |
// json_encode( |
| 289 |
// [ |
| 290 |
// 'id'=> |
| 291 |
// $actionIntegrationDetails[$successActionValue->type][$actionIntegrationID->index] |
| 292 |
// ] |
| 293 |
// ) |
| 294 |
// : $value; |
| 295 |
// } |
| 296 |
// } else { |
| 297 |
// $actionIntegrationID = \json_decode($successActionValue->details->id); |
| 298 |
// $workFlowDetails->successAction[$successActionkey]->details->id |
| 299 |
// = isset($actionIntegrationID->index) ? |
| 300 |
// json_encode( |
| 301 |
// [ |
| 302 |
// 'id'=> |
| 303 |
// $actionIntegrationDetails[$successActionValue->type][$actionIntegrationID->index] |
| 304 |
// ] |
| 305 |
// ) |
| 306 |
// : $successActionValue->details->id; |
| 307 |
// } |
| 308 |
// } |
| 309 |
// } |
| 310 |
// $workFlowActions['success'] = $workFlowDetails->successAction; |
| 311 |
// if (!empty($workFlowDetails->validateMsg)) { |
| 312 |
// $validateMsgID = \json_decode($workFlowDetails->validateMsg); |
| 313 |
// $workFlowActions['validateMsg'] = isset($validateMsgID->index) ? |
| 314 |
// json_encode( |
| 315 |
// [ |
| 316 |
// 'id'=> |
| 317 |
// $actionIntegrationDetails['successMsg'][$validateMsgID->index] |
| 318 |
// ] |
| 319 |
// ) |
| 320 |
// : $workFlowDetails->validateMsg; |
| 321 |
// } |
| 322 |
// if (!empty($workFlowDetails->avoid_delete)) { |
| 323 |
// $workFlowActions['avoid_delete'] = $workFlowDetails->avoid_delete; |
| 324 |
// } |
| 325 |
return static::$_workFlowModel->update( |
| 326 |
[ |
| 327 |
'workflow_name' => $workFlowDetails->title, |
| 328 |
'workflow_type' => 'delete' === $workFlowDetails->action_run ? 'delete' : $workFlowDetails->action_type, |
| 329 |
'workflow_run' => $workFlowDetails->action_run, |
| 330 |
'workflow_behaviour' => $workFlowDetails->action_behaviour, |
| 331 |
'workflow_condition' => json_encode($workFlowDetails->conditions), |
| 332 |
// "workflow_action" => null, |
| 333 |
'user_id' => $this->_user_details['id'], |
| 334 |
'user_ip' => $this->_user_details['ip'], |
| 335 |
'user_location' => '', |
| 336 |
'user_device' => $this->_user_details['device'], |
| 337 |
'updated_at' => $this->_user_details['time'], |
| 338 |
], |
| 339 |
[ |
| 340 |
'id' => $workFlowID, |
| 341 |
'form_id' => static::$_formID, |
| 342 |
] |
| 343 |
); |
| 344 |
} |
| 345 |
|
| 346 |
public function duplicateWorkFlow($currentFormID) |
| 347 |
{ |
| 348 |
$workFlowCols = [ |
| 349 |
'workflow_name', 'workflow_type', 'workflow_run', 'workflow_behaviour', |
| 350 |
'workflow_condition', 'workflow_action', 'form_id', 'user_id', |
| 351 |
'user_ip', 'user_location', 'user_device', 'created_at', 'updated_at', |
| 352 |
]; |
| 353 |
$dupData = [ |
| 354 |
'workflow_name', |
| 355 |
'workflow_type', |
| 356 |
'workflow_run', |
| 357 |
'workflow_behaviour', |
| 358 |
'workflow_condition', |
| 359 |
'workflow_action', |
| 360 |
static::$_formID, |
| 361 |
$this->_user_details['id'], |
| 362 |
$this->_user_details['ip'], |
| 363 |
'', |
| 364 |
$this->_user_details['device'], |
| 365 |
$this->_user_details['time'], |
| 366 |
$this->_user_details['time'], |
| 367 |
]; |
| 368 |
return static::$_workFlowModel->duplicate($workFlowCols, $dupData, ['form_id' => $currentFormID]); |
| 369 |
} |
| 370 |
|
| 371 |
public function deleteworkFlow($workFlowID) |
| 372 |
{ |
| 373 |
return static::$_workFlowModel->delete( |
| 374 |
[ |
| 375 |
'id' => $workFlowID, |
| 376 |
'form_id' => static::$_formID, |
| 377 |
] |
| 378 |
); |
| 379 |
} |
| 380 |
|
| 381 |
public function maybeGetSuccessActionID($actionIntegrationID, $actionIntegrationDetails, $actionType) |
| 382 |
{ |
| 383 |
if (isset($actionIntegrationDetails[$actionType])) { |
| 384 |
if (isset($actionIntegrationID->index)) { |
| 385 |
return $actionIntegrationDetails[$actionType][$actionIntegrationID->index]; |
| 386 |
} elseif (isset($actionIntegrationID->id, $actionIntegrationDetails[$actionType][json_encode($actionIntegrationID)])) { |
| 387 |
return $actionIntegrationDetails[$actionType][json_encode($actionIntegrationID)]; |
| 388 |
} |
| 389 |
} |
| 390 |
return false; |
| 391 |
} |
| 392 |
} |
| 393 |
|