activity_model.php
3 months ago
agent_meta_model.php
3 months ago
agent_model.php
3 months ago
booking_meta_model.php
3 months ago
booking_model.php
3 months ago
bundle_meta_model.php
3 months ago
bundle_model.php
3 months ago
cart_item_model.php
3 months ago
cart_meta_model.php
3 months ago
cart_model.php
3 months ago
connector_model.php
3 months ago
customer_meta_model.php
3 months ago
customer_model.php
3 months ago
invoice_model.php
3 months ago
join_bundles_services_model.php
3 months ago
location_category_model.php
3 months ago
location_model.php
3 months ago
meta_model.php
3 months ago
model.php
3 months ago
off_period_model.php
3 months ago
order_intent_meta_model.php
3 months ago
order_intent_model.php
3 months ago
order_item_model.php
3 months ago
order_meta_model.php
3 months ago
order_model.php
3 months ago
otp_model.php
3 months ago
payment_request_model.php
3 months ago
process_job_model.php
3 months ago
process_model.php
3 months ago
recurrence_model.php
3 months ago
service_category_model.php
3 months ago
service_meta_model.php
3 months ago
service_model.php
3 months ago
session_model.php
3 months ago
settings_model.php
3 months ago
step_settings_model.php
3 months ago
transaction_intent_model.php
3 months ago
transaction_model.php
3 months ago
transaction_refund_model.php
3 months ago
work_period_model.php
3 months ago
process_model.php
184 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 [ |
| 84 | 'name' => $this->name, |
| 85 | 'event_type' => $this->event_type, |
| 86 | ]; |
| 87 | } |
| 88 | |
| 89 | public function delete( $id = false ) { |
| 90 | if ( ! $id && isset( $this->id ) ) { |
| 91 | $id = $this->id; |
| 92 | } |
| 93 | if ( $id && $this->db->delete( $this->table_name, array( 'id' => $id ), array( '%d' ) ) ) { |
| 94 | $this->db->delete( |
| 95 | LATEPOINT_TABLE_PROCESS_JOBS, |
| 96 | array( |
| 97 | 'process_id' => $id, |
| 98 | 'status' => LATEPOINT_JOB_STATUS_SCHEDULED, |
| 99 | ), |
| 100 | array( '%d', '%s' ) |
| 101 | ); |
| 102 | do_action( 'latepoint_process_deleted', $id ); |
| 103 | return true; |
| 104 | } else { |
| 105 | return false; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public function set_from_params( array $params ) { |
| 110 | $this->name = $params['name']; |
| 111 | if ( ! empty( $params['event'] ) ) { |
| 112 | $this->event_type = $params['event']['type']; |
| 113 | $this->event = new \LatePoint\Misc\ProcessEvent(); |
| 114 | $this->event->set_from_params( $params['event'] ); |
| 115 | |
| 116 | } |
| 117 | |
| 118 | |
| 119 | if ( ! empty( $params['actions'] ) ) { |
| 120 | foreach ( $params['actions'] as $action_id => $action_params ) { |
| 121 | $action = new \LatePoint\Misc\ProcessAction(); |
| 122 | $action->id = $action_id; |
| 123 | $action->set_from_params( $action_params ); |
| 124 | $this->actions[] = $action; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public function build_from_json() { |
| 130 | $groups = empty( $this->actions_json ) ? [] : json_decode( $this->actions_json, true ); |
| 131 | $this->trigger_conditions = OsProcessesHelper::extract_trigger_conditions_from_groups( $groups ); |
| 132 | $this->actions = OsProcessesHelper::extract_actions_from_groups( $groups ); |
| 133 | $this->time_offset = $groups[0]['time_offset'] ?? []; |
| 134 | } |
| 135 | |
| 136 | protected function get_event(): \LatePoint\Misc\ProcessEvent { |
| 137 | $event_data = []; |
| 138 | if ( ! empty( $this->event_type ) ) { |
| 139 | $event_data['type'] = $this->event_type; |
| 140 | } |
| 141 | if ( ! empty( $this->trigger_conditions ) ) { |
| 142 | $event_data['trigger_conditions'] = $this->trigger_conditions; |
| 143 | } |
| 144 | if ( ! empty( $this->time_offset ) ) { |
| 145 | $event_data['time_offset'] = $this->time_offset; |
| 146 | } |
| 147 | |
| 148 | $this->event = new \LatePoint\Misc\ProcessEvent( $event_data ); |
| 149 | return $this->event; |
| 150 | } |
| 151 | |
| 152 | protected function params_to_sanitize() { |
| 153 | return []; |
| 154 | } |
| 155 | |
| 156 | protected function params_to_save( $role = 'admin' ) { |
| 157 | $params_to_save = [ |
| 158 | 'id', |
| 159 | 'event_type', |
| 160 | 'status', |
| 161 | 'name', |
| 162 | 'actions_json', |
| 163 | ]; |
| 164 | return $params_to_save; |
| 165 | } |
| 166 | |
| 167 | protected function allowed_params( $role = 'admin' ) { |
| 168 | $allowed_params = [ |
| 169 | 'id', |
| 170 | 'event_type', |
| 171 | 'status', |
| 172 | 'name', |
| 173 | 'actions_json', |
| 174 | ]; |
| 175 | return $allowed_params; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | protected function properties_to_validate() { |
| 180 | $validations = []; |
| 181 | return $validations; |
| 182 | } |
| 183 | } |
| 184 |