PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.1.3
LatePoint – Calendar Booking Plugin for Appointments and Events v5.1.3
5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / models / process_job_model.php
latepoint / lib / models Last commit date
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_job_model.php
269 lines
1 <?php
2 /*
3 * Copyright (c) 2022 LatePoint LLC. All rights reserved.
4 */
5
6 class OsProcessJobModel extends OsModel{
7 var $id,
8 $process_id,
9 $object_id,
10 $object_model_type,
11 $to_run_after_utc,
12 $status = LATEPOINT_JOB_STATUS_SCHEDULED,
13 $settings,
14 $run_result,
15 $process_info,
16 $updated_at,
17 $created_at;
18
19 function __construct($id = false){
20 parent::__construct();
21 $this->table_name = LATEPOINT_TABLE_PROCESS_JOBS;
22
23 if($id){
24 $this->load_by_id($id);
25 }
26 }
27
28 public function get_link_to_object(){
29 $href = '#';
30 $attrs = '';
31 switch($this->process->event_type){
32 case 'order_updated':
33 case 'order_created':
34 $attrs = OsOrdersHelper::quick_order_btn_html($this->object_id);
35 break;
36 case 'customer_created':
37 $attrs = OsCustomerHelper::quick_customer_btn_html($this->object_id);
38 break;
39 case 'booking_start':
40 case 'booking_end':
41 case 'booking_created':
42 case 'booking_updated':
43 $attrs = OsBookingHelper::quick_booking_btn_html($this->object_id);
44 break;
45 }
46 $link = '<a href="'.esc_url($href).'" '.$attrs.'>'.esc_html($this->object_id).'</a>';
47 return $link;
48 }
49
50 public function get_original_process_attribute($attribute){
51 $process_info = json_decode($this->process_info, true);
52 return $process_info[$attribute] ?? __('n/a', 'latepoint');
53 }
54
55 public function get_action_by_id_from_settings($action_id){
56 foreach($this->actions as $action){
57 if($action->id == $action_id) return $action;
58 }
59 return null;
60 }
61
62 public function get_actions($force_refresh = false){
63 if(!isset($this->actions) || $force_refresh){
64 $this->actions = [];
65 $settings = json_decode($this->settings, true);
66 if($settings){
67 foreach($settings['action_data'] as $action_id => $action_data){
68 $action = new \LatePoint\Misc\ProcessAction();
69 $action->id = $action_id;
70 $action->type = $settings['action_info'][$action_id]['type'];
71 $action->prepared_data_for_run = $action_data;
72 $this->actions[] = $action;
73 }
74 }
75 }
76 return $this->actions;
77 }
78
79 public function get_actions_summary(){
80 $actions_html = '';
81
82 $process_actions = $this->get_actions();
83 $run_result = $result = json_decode($this->run_result, true);
84 if(empty($process_actions) && empty($run_result)) return __('No Actions', 'latepoint');
85
86 foreach($process_actions as $action){
87 if(isset($run_result['ran_actions_info'][$action->id])){
88 $status_icon = $run_result['ran_actions_info'][$action->id]['run_status'] == LATEPOINT_STATUS_SUCCESS ? '<i style="color: #78ad06;" class="latepoint-icon latepoint-icon-checkmark"></i>' : '<i style="color: #ad0606;" class="latepoint-icon latepoint-icon-x-square"></i>';
89 $action_name = '<span>'.\LatePoint\Misc\ProcessAction::get_action_name_for_type($run_result['ran_actions_info'][$action->id]['type']).'</span>'.$status_icon.'</span>';
90 }else{
91 $action_name = '<span>'.\LatePoint\Misc\ProcessAction::get_action_name_for_type($action->type).'</span><i class="latepoint-icon latepoint-icon-bell"></i></span>';
92 }
93 $action_icon = '';
94 switch($action->type){
95 case 'send_email':
96 $action_icon = '<i class="latepoint-icon latepoint-icon-mail"></i>';
97 break;
98 case 'send_sms':
99 $action_icon = '<i class="latepoint-icon latepoint-icon-message-circle"></i>';
100 break;
101 case 'trigger_webhook':
102 $action_icon = '<i class="latepoint-icon latepoint-icon-globe"></i>';
103 break;
104 }
105
106 $actions_html.= '<span
107 data-os-action="'.OsRouterHelper::build_route_name('process_jobs', 'preview_job_action').'"
108 data-os-output-target="side-panel"
109 data-os-after-call="latepoint_init_json_view"
110 data-os-params="'.OsUtilHelper::build_os_params(['job_id' => $this->id, 'action_id' => $action->id]).'"
111 data-os-lightbox-classes="width-800"
112 class="action-run-info-pill">'.$action_icon.$action_name.'</span>';
113 }
114
115 return $actions_html;
116 }
117
118 protected function get_process() {
119 if ($this->process_id) {
120 if (!isset($this->process) || (isset($this->process) && ($this->process->id != $this->process_id))) {
121 $this->process = new OsProcessModel($this->process_id);
122 $this->process->build_from_json();
123 }
124 } else {
125 $this->process = new OsProcessModel();
126 }
127 return $this->process;
128 }
129
130 /**
131 * @param $action_ids_to_run IDs of actions that need to be run, all will run by default
132 * @return $this
133 */
134 public function run(array $action_ids_to_run = []){
135 $actions = $this->get_actions();
136 if($actions){
137 $settings = json_decode($this->settings, true);
138 $error_messages = [];
139 // try to load from previous run, if it ever existed and if select action IDs are passed, otherwise overwrite. This is done to not override actions that ran before with partial actions that ran now
140 $previous_run_result = empty($this->run_result) ? [] : json_decode($this->run_result, true);
141 $ran_actions_info = (isset($previous_run_result['ran_actions_info']) && $action_ids_to_run) ? $previous_run_result['ran_actions_info'] : [];
142 foreach($actions as $action){
143 // skip if specific action IDs are passed and action in this loop is not one of the requested to be run
144 if($action_ids_to_run && !in_array($action->id, $action_ids_to_run)) continue;
145 if(!isset($settings['action_data'][$action->id])){
146 $error_messages[] = $action->id.': '.__('Process action have been modified since the job was created.', 'latepoint');
147 continue;
148 }
149 $action->prepared_data_for_run = $settings['action_data'][$action->id];
150 $result = $action->run(false);
151 $ran_actions_info[$action->id] = [
152 'type' => $action->type,
153 'id' => $action->id,
154 'run_status' => $result['status'],
155 'run_datetime_utc' => OsTimeHelper::now_datetime_utc_in_db_format(),
156 'run_message' => $result['message']];
157 if($result['status'] != LATEPOINT_STATUS_SUCCESS){
158 $error_messages[] = $action->id.': '.$result['message'];
159 }
160 }
161 if(empty($error_messages)){
162 $this->status = LATEPOINT_JOB_STATUS_COMPLETED;
163 $message = $action_ids_to_run ? __('Selected actions ran successfully.', 'latepoint').' ['.implode(',', $action_ids_to_run).']' : __('The job ran successfully.', 'latepoint');
164 $this->run_result = wp_json_encode(['status' => LATEPOINT_STATUS_SUCCESS, 'run_datetime_utc' => OsTimeHelper::now_datetime_utc_in_db_format(), 'message' => $message, 'ran_actions_info' => $ran_actions_info]);
165 }else{
166 $this->status = LATEPOINT_JOB_STATUS_ERROR;
167 $this->run_result = wp_json_encode(['status' => LATEPOINT_STATUS_ERROR, 'run_datetime_utc' => OsTimeHelper::now_datetime_utc_in_db_format(), 'message' => implode(', ', $error_messages), 'ran_actions_info' => $ran_actions_info]);
168 }
169 }else{
170 $this->run_result = wp_json_encode(['status' => LATEPOINT_STATUS_SUCCESS, 'run_datetime_utc' => OsTimeHelper::now_datetime_utc_in_db_format(), 'message' => __('Job process has no actions to run', 'latepoint')]);
171 $this->status = LATEPOINT_JOB_STATUS_COMPLETED;
172 }
173 $activity = new OsActivityModel();
174
175 $event_type = $this->get_original_process_attribute('event_type');
176 switch($event_type){
177 case 'order_created':
178 case 'order_updated':
179 $order = new OsOrderModel($this->object_id);
180 $activity->customer_id = $order->customer_id;
181 $activity->order_id = $order->id;
182 break;
183 case 'booking_created':
184 case 'booking_updated':
185 case 'booking_start':
186 case 'booking_end':
187 $booking = new OsBookingModel($this->object_id);
188 $activity->booking_id = $this->object_id;
189 $activity->customer_id = $booking->customer_id;
190 $activity->agent_id = $booking->agent_id;
191 $activity->service_id = $booking->service_id;
192 $activity->location_id = $booking->location_id;
193 $activity->order_id = $booking->order->id;
194 break;
195 case 'customer_created':
196 $activity->customer_id = $this->object_id;
197 break;
198 case 'transaction_created':
199 $transaction = new OsTransactionModel($this->object_id);
200 $activity->order_id = $transaction->order_id;
201 break;
202 case 'payment_request_created':
203 $payment_request = new OsPaymentRequestModel($this->object_id);
204 $activity->order_id = $payment_request->order_id;
205 break;
206 }
207
208 $this->save();
209
210 $activity->code = 'process_job_run';
211
212 if(OsAuthHelper::get_highest_current_user_type()){
213 $activity->initiated_by = OsAuthHelper::get_highest_current_user_type();
214 $activity->initiated_by_id = OsAuthHelper::get_highest_current_user_id();
215 }
216 $activity->description = wp_json_encode(['job_id' => $this->id, 'processed_datetime' => OsTimeHelper::now_datetime_in_db_format(), 'run_result' => $this->run_result, 'status' => LATEPOINT_STATUS_SUCCESS]);
217
218 /**
219 * Activity that is created when a process job is run
220 *
221 * @since 5.1.0
222 * @hook latepoint_job_run_activity
223 *
224 * @param {OsActivityModel} $activity Activity model
225 * @param {OsProcessJobModel} $process_job Process job model
226 *
227 * @returns {OsActivityModel} Filtered activity model
228 */
229 $activity = apply_filters('latepoint_job_run_activity', $activity, $this, $event_type);
230 $activity->save();
231 return $this;
232 }
233
234 protected function params_to_save($role = 'admin'){
235 $params_to_save = [
236 'id',
237 'process_id',
238 'object_id',
239 'object_model_type',
240 'to_run_after_utc',
241 'status',
242 'process_info',
243 'settings',
244 'run_result'
245 ];
246 return $params_to_save;
247 }
248
249 protected function allowed_params($role = 'admin'){
250 $allowed_params = [
251 'id',
252 'process_id',
253 'object_id',
254 'object_model_type',
255 'to_run_after_utc',
256 'status',
257 'process_info',
258 'settings',
259 'run_result'
260 ];
261 return $allowed_params;
262 }
263
264
265 protected function properties_to_validate(){
266 $validations = [];
267 return $validations;
268 }
269 }