activity_model.php
1 year ago
agent_meta_model.php
1 year ago
agent_model.php
1 year ago
booking_meta_model.php
1 year ago
booking_model.php
1 year ago
bundle_model.php
1 year ago
cart_item_model.php
1 year ago
cart_meta_model.php
1 year ago
cart_model.php
1 year ago
connector_model.php
1 year ago
customer_meta_model.php
1 year ago
customer_model.php
1 year ago
invoice_model.php
1 year ago
join_bundles_services_model.php
1 year ago
location_category_model.php
1 year ago
location_model.php
1 year ago
meta_model.php
1 year ago
model.php
1 year ago
order_intent_meta_model.php
1 year ago
order_intent_model.php
1 year ago
order_item_model.php
1 year ago
order_meta_model.php
1 year ago
order_model.php
1 year ago
payment_request_model.php
1 year ago
process_job_model.php
1 year ago
process_model.php
1 year ago
service_category_model.php
1 year ago
service_meta_model.php
1 year ago
service_model.php
1 year ago
session_model.php
1 year ago
settings_model.php
1 year ago
step_settings_model.php
1 year ago
transaction_intent_model.php
1 year ago
transaction_model.php
1 year ago
transaction_refund_model.php
1 year ago
work_period_model.php
1 year ago
process_model.php
167 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (c) 2022 LatePoint LLC. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | class OsProcessModel extends OsModel{ |
| 7 | var $id, |
| 8 | $name, |
| 9 | $status = LATEPOINT_STATUS_ACTIVE, |
| 10 | $event_type, //'booking_created', 'booking_updated', 'booking_start', 'booking_end', 'customer_created', 'transaction_created' |
| 11 | $actions_json, |
| 12 | $trigger_conditions, |
| 13 | $actions, |
| 14 | $time_offset, |
| 15 | $updated_at, |
| 16 | $created_at; |
| 17 | |
| 18 | function __construct($id = false){ |
| 19 | parent::__construct(); |
| 20 | $this->table_name = LATEPOINT_TABLE_PROCESSES; |
| 21 | |
| 22 | if($id){ |
| 23 | $this->load_by_id($id); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | public function should_be_active(){ |
| 28 | return $this->where(['status' => LATEPOINT_STATUS_ACTIVE]); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @param array $objects example format: ['model' => 'booking', 'id' => $booking->id, 'model_ready' => OsModel $booking] |
| 33 | * @return bool |
| 34 | */ |
| 35 | public function check_if_objects_satisfy_trigger_conditions(array $objects): bool{ |
| 36 | if($this->event->trigger_conditions){ |
| 37 | foreach($this->event->trigger_conditions as $condition){ |
| 38 | foreach($objects as $object){ |
| 39 | if($object['model'] == \LatePoint\Misc\ProcessEvent::get_object_from_property($condition['property'])){ |
| 40 | $attribute = \LatePoint\Misc\ProcessEvent::get_object_attribute_from_property($condition['property']); |
| 41 | switch($condition['operator']){ |
| 42 | case 'equal': |
| 43 | $value_arr = explode(',', $condition['value']); |
| 44 | if(!in_array($object['model_ready']->$attribute, $value_arr)){ |
| 45 | return false; |
| 46 | } |
| 47 | break; |
| 48 | case 'not_equal': |
| 49 | $value_arr = explode(',', $condition['value']); |
| 50 | if(in_array($object['model_ready']->$attribute, $value_arr)){ |
| 51 | return false; |
| 52 | } |
| 53 | break; |
| 54 | // below cases are similar: |
| 55 | // this operator is only available for models prefixed with "old_", we need to iterate through other |
| 56 | // objects and find the matching one by stripping "old_" from the one that we are comparing change to |
| 57 | case 'not_changed': |
| 58 | foreach($objects as $object_to_compare){ |
| 59 | if($object_to_compare['model'] == str_replace('old_', '', $object['model'])){ |
| 60 | if($object['model_ready']->$attribute != $object_to_compare['model_ready']->$attribute){ |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | case 'changed': |
| 66 | foreach($objects as $object_to_compare){ |
| 67 | if($object_to_compare['model'] == str_replace('old_', '', $object['model'])){ |
| 68 | if($object['model_ready']->$attribute == $object_to_compare['model_ready']->$attribute){ |
| 69 | return false; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | public function get_info(){ |
| 83 | return ['name' => $this->name, 'event_type' => $this->event_type]; |
| 84 | } |
| 85 | |
| 86 | public function delete($id = false){ |
| 87 | if(!$id && isset($this->id)){ |
| 88 | $id = $this->id; |
| 89 | } |
| 90 | if($id && $this->db->delete( $this->table_name, array('id' => $id), array( '%d' ))){ |
| 91 | $this->db->delete(LATEPOINT_TABLE_PROCESS_JOBS, array('process_id' => $id, 'status' => LATEPOINT_JOB_STATUS_SCHEDULED), array( '%d', '%s' ) ); |
| 92 | do_action('latepoint_process_deleted', $id); |
| 93 | return true; |
| 94 | }else{ |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public function set_from_params(array $params){ |
| 100 | $this->name = $params['name']; |
| 101 | if(!empty($params['event'])){ |
| 102 | $this->event_type = $params['event']['type']; |
| 103 | $this->event = new \LatePoint\Misc\ProcessEvent(); |
| 104 | $this->event->set_from_params($params['event']); |
| 105 | |
| 106 | } |
| 107 | |
| 108 | |
| 109 | if(!empty($params['actions'])){ |
| 110 | foreach($params['actions'] as $action_id => $action_params){ |
| 111 | $action = new \LatePoint\Misc\ProcessAction(); |
| 112 | $action->id = $action_id; |
| 113 | $action->set_from_params($action_params); |
| 114 | $this->actions[] = $action; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public function build_from_json(){ |
| 120 | $groups = empty($this->actions_json) ? [] : json_decode($this->actions_json, true); |
| 121 | $this->trigger_conditions = OsProcessesHelper::extract_trigger_conditions_from_groups($groups); |
| 122 | $this->actions = OsProcessesHelper::extract_actions_from_groups($groups); |
| 123 | $this->time_offset = $groups[0]['time_offset'] ?? []; |
| 124 | } |
| 125 | |
| 126 | protected function get_event() :\LatePoint\Misc\ProcessEvent{ |
| 127 | $event_data = []; |
| 128 | if(!empty($this->event_type)) $event_data['type'] = $this->event_type; |
| 129 | if(!empty($this->trigger_conditions)) $event_data['trigger_conditions'] = $this->trigger_conditions; |
| 130 | if(!empty($this->time_offset)) $event_data['time_offset'] = $this->time_offset; |
| 131 | |
| 132 | $this->event = new \LatePoint\Misc\ProcessEvent($event_data); |
| 133 | return $this->event; |
| 134 | } |
| 135 | |
| 136 | protected function params_to_sanitize(){ |
| 137 | return []; |
| 138 | } |
| 139 | |
| 140 | protected function params_to_save($role = 'admin'){ |
| 141 | $params_to_save = [ |
| 142 | 'id', |
| 143 | 'event_type', |
| 144 | 'status', |
| 145 | 'name', |
| 146 | 'actions_json' |
| 147 | ]; |
| 148 | return $params_to_save; |
| 149 | } |
| 150 | |
| 151 | protected function allowed_params($role = 'admin'){ |
| 152 | $allowed_params = [ |
| 153 | 'id', |
| 154 | 'event_type', |
| 155 | 'status', |
| 156 | 'name', |
| 157 | 'actions_json' |
| 158 | ]; |
| 159 | return $allowed_params; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | protected function properties_to_validate(){ |
| 164 | $validations = []; |
| 165 | return $validations; |
| 166 | } |
| 167 | } |