PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.4.2
LatePoint – Calendar Booking Plugin for Appointments and Events v5.4.2
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 / controllers / processes_controller.php
latepoint / lib / controllers Last commit date
activities_controller.php 3 months ago auth_controller.php 3 months ago booking_form_settings_controller.php 3 months ago bookings_controller.php 3 months ago calendars_controller.php 3 months ago carts_controller.php 3 months ago controller.php 3 months ago customer_cabinet_controller.php 2 months ago customers_controller.php 3 months ago dashboard_controller.php 3 months ago default_agent_controller.php 3 months ago events_controller.php 3 months ago form_fields_controller.php 3 months ago integrations_controller.php 3 months ago invoices_controller.php 2 months ago manage_booking_by_key_controller.php 3 months ago manage_order_by_key_controller.php 3 months ago notifications_controller.php 3 months ago orders_controller.php 3 months ago pro_controller.php 2 months ago process_jobs_controller.php 3 months ago processes_controller.php 3 months ago search_controller.php 3 months ago services_controller.php 3 months ago settings_controller.php 3 months ago steps_controller.php 3 months ago stripe_connect_controller.php 2 months ago support_topics_controller.php 3 months ago todos_controller.php 3 months ago transactions_controller.php 3 months ago wizard_controller.php 2 months ago
processes_controller.php
431 lines
1 <?php
2 /*
3 * Copyright (c) 2022 LatePoint LLC. All rights reserved.
4 */
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly.
8 }
9
10
11 if ( ! class_exists( 'OsProcessesController' ) ) :
12
13
14 class OsProcessesController extends OsController {
15
16 function __construct() {
17 parent::__construct();
18
19
20 $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'processes/';
21 $this->vars['page_header'] = OsMenuHelper::get_menu_items_by_id( 'processes' );
22 $this->vars['pre_page_header'] = OsMenuHelper::get_label_by_id( 'processes' );
23 $this->vars['breadcrumbs'][] = array(
24 'label' => __( 'Workflows', 'latepoint' ),
25 'link' => OsRouterHelper::build_link( OsRouterHelper::build_route_name( 'processes', 'index' ) ),
26 );
27 }
28
29 public function new_form() {
30 $this->vars['process'] = new OsProcessModel();
31 $this->set_layout( 'none' );
32 $this->format_render( __FUNCTION__ );
33 }
34
35 public function reload_event_trigger_conditions() {
36 $event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['event_type'] ] );
37 $trigger_conditions_form_section_html = OsProcessesHelper::trigger_conditions_html_for_event( $event );
38
39 if ( $this->get_return_format() == 'json' ) {
40 $this->send_json(
41 array(
42 'status' => LATEPOINT_STATUS_SUCCESS,
43 'message' => $trigger_conditions_form_section_html,
44 )
45 );
46 }
47 }
48
49
50 public function available_properties_for_object_code() {
51 $object_code = $this->params['object_code'];
52 $properties_for_select = \LatePoint\Misc\ProcessEvent::get_properties_for_object_code( $object_code, true );
53 $html = '';
54 foreach ( $properties_for_select as $property ) {
55 $html .= '<option value="' . $property['value'] . '">' . $property['label'] . '</option>';
56 }
57 if ( $this->get_return_format() == 'json' ) {
58 $this->send_json(
59 array(
60 'status' => LATEPOINT_STATUS_SUCCESS,
61 'message' => $html,
62 )
63 );
64 }
65 }
66
67
68 public function available_operators_for_trigger_condition_property() {
69 // example format: old_booking__agent_id
70 $property = $this->params['property'];
71 $operators = \LatePoint\Misc\ProcessEvent::trigger_condition_operators_for_property( $property );
72 $html = '';
73 foreach ( $operators as $value => $label ) {
74 $html .= '<option value="' . $value . '">' . $label . '</option>';
75 }
76 if ( $this->get_return_format() == 'json' ) {
77 $this->send_json(
78 array(
79 'status' => LATEPOINT_STATUS_SUCCESS,
80 'message' => $html,
81 )
82 );
83 }
84 }
85
86 public function available_values_for_trigger_condition_property() {
87 $values = [];
88 $property = $this->params['property'];
89 $operator = $this->params['operator'];
90 $trigger_condition_id = $this->params['trigger_condition_id'];
91 $values = OsProcessesHelper::values_for_trigger_condition_property( $property );
92 if ( $this->get_return_format() == 'json' ) {
93 $this->send_json(
94 array(
95 'status' => LATEPOINT_STATUS_SUCCESS,
96 'message' => OsFormHelper::multi_select_field( 'process[event][trigger_conditions][' . $trigger_condition_id . '][value]', false, $values, false, [] ),
97 )
98 );
99 }
100 }
101
102 function destroy() {
103 if ( filter_var( $this->params['id'], FILTER_VALIDATE_INT ) ) {
104 $this->check_nonce( 'destroy_process_' . $this->params['id'] );
105 $process = new OsProcessModel( $this->params['id'] );
106 if ( $process->delete() ) {
107 $status = LATEPOINT_STATUS_SUCCESS;
108 $response_html = __( 'Process Removed', 'latepoint' );
109 } else {
110 $status = LATEPOINT_STATUS_ERROR;
111 $response_html = __( 'Error Removing Workflow', 'latepoint' );
112 }
113 } else {
114 $status = LATEPOINT_STATUS_SUCCESS;
115 $response_html = __( 'Process Removed', 'latepoint' );
116 }
117 if ( $this->get_return_format() == 'json' ) {
118 $this->send_json(
119 array(
120 'status' => $status,
121 'message' => $response_html,
122 )
123 );
124 }
125 }
126
127 function save() {
128 $process_data = $this->params['process'];
129 if ( ! empty( $process_data['id'] ) ) {
130 $this->check_nonce( 'edit_process_' . $process_data['id'] );
131 $process = new OsProcessModel( $process_data['id'] );
132 $new_process = false;
133 } else {
134 $this->check_nonce( 'new_process' );
135 $process = new OsProcessModel();
136 $new_process = true;
137 }
138
139 $process->status = $process_data['status'] ?? 'active';
140 $process->name = $process_data['name'];
141 $process->event_type = $process_data['event']['type'];
142 $actions = [];
143
144
145 // check if conditions are turned ON and exist in params
146 $trigger_conditions = ( isset( $process_data['event']['conditional'] ) && $process_data['event']['conditional'] == LATEPOINT_VALUE_ON && isset( $process_data['event']['trigger_conditions'] ) && ! empty( $process_data['event']['trigger_conditions'] ) ) ? $process_data['event']['trigger_conditions'] : [];
147 if ( isset( $process_data['actions'] ) ) {
148 $actions = OsProcessesHelper::iterate_trigger_conditions( $trigger_conditions, $process_data['actions'] );
149
150 if ( $process_data['event']['has_time_offset'] == LATEPOINT_VALUE_ON ) {
151 $actions[0]['time_offset'] = $process_data['event']['time_offset'];
152 } else {
153 $actions[0]['time_offset'] = [];
154 }
155 } else {
156 $actions = [];
157 }
158
159 $process->actions_json = wp_json_encode( $actions );
160 $old_process = $process->is_new_record() ? [] : clone $process;
161 if ( $process->save() ) {
162 if ( ! $new_process ) {
163 // remove previously created jobs for this process that hasn't run yet
164 $jobs = new OsProcessJobModel();
165 $jobs->delete_where(
166 [
167 'process_id' => $process->id,
168 'status' => LATEPOINT_JOB_STATUS_SCHEDULED,
169 ]
170 );
171 /**
172 * Process was updated
173 *
174 * @param {OsProcessModel} $process instance of process model that was updated
175 * @param {OsProcessModel} $old_process instance of process model before it was updated
176 *
177 * @since 4.7.0
178 * @hook latepoint_process_updated
179 *
180 */
181 do_action( 'latepoint_process_updated', $process, $old_process );
182 } else {
183 /**
184 * Process was created
185 *
186 * @param {OsProcessModel} $process instance of process model that was created
187 *
188 * @since 4.7.0
189 * @hook latepoint_process_created
190 *
191 */
192 do_action( 'latepoint_process_created', $process );
193 }
194 $process->build_from_json();
195 OsProcessJobsHelper::recreate_jobs_for_existing_records( $process );
196 $message = __( 'Process Saved', 'latepoint' );
197 $status = LATEPOINT_STATUS_SUCCESS;
198 } else {
199 $message = __( 'Error saving process', 'latepoint' );
200 $status = LATEPOINT_STATUS_ERROR;
201 }
202
203 if ( $this->get_return_format() == 'json' ) {
204 $this->send_json(
205 array(
206 'status' => $status,
207 'message' => $message,
208 )
209 );
210 }
211 }
212
213 function new_trigger_condition() {
214 $process_event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['event_type'] ] );
215 $trigger_condition_html = $process_event->generate_trigger_condition_form_html();
216 if ( $this->get_return_format() == 'json' ) {
217 $this->send_json(
218 array(
219 'status' => LATEPOINT_STATUS_SUCCESS,
220 'message' => $trigger_condition_html,
221 )
222 );
223 }
224 }
225
226 function new_action() {
227 $action = new \LatePoint\Misc\ProcessAction();
228 $process_id = ! empty( $this->params['process_id'] ) ? sanitize_text_field( $this->params['process_id'] ) : '';
229 $response_html = \LatePoint\Misc\ProcessAction::generate_form( $action, $process_id );
230
231 if ( $this->get_return_format() == 'json' ) {
232 $this->send_json(
233 array(
234 'status' => LATEPOINT_STATUS_SUCCESS,
235 'message' => $response_html,
236 )
237 );
238 }
239 }
240
241 function load_action_settings() {
242 $action = new \LatePoint\Misc\ProcessAction(
243 [
244 'type' => $this->params['action_type'],
245 'id' => $this->params['action_id'],
246 ]
247 );
248
249 $template_id = $this->params['template_id'] ?? false;
250 if ( $template_id ) {
251 $action->load_settings_from_template( $template_id );
252 }
253
254 if ( $this->get_return_format() == 'json' ) {
255 $this->send_json(
256 array(
257 'status' => LATEPOINT_STATUS_SUCCESS,
258 'message' => \LatePoint\Misc\ProcessAction::generate_settings_fields( $action ),
259 )
260 );
261 }
262 }
263
264 function index() {
265 $processes = new OsProcessModel();
266 $this->vars['processes'] = $processes->get_results_as_models();
267 $this->format_render( __FUNCTION__ );
268 }
269
270 function action_test_run() {
271 // Verify CSRF nonce
272 $this->check_nonce( 'test_process_action' );
273
274 $action = new \LatePoint\Misc\ProcessAction();
275 $action->set_from_params( $this->params['action'] );
276
277 $available_data_sources = $action->event->get_available_data_sources();
278 foreach ( $available_data_sources as $data_source ) {
279 $action->selected_data_objects[] = [
280 'model' => $data_source['model'],
281 'id' => $this->params['data_source'][ $data_source['name'] ],
282 ];
283 }
284
285 $result = $action->run();
286 if ( $this->get_return_format() == 'json' ) {
287 $this->send_json(
288 [
289 'status' => $result['status'],
290 'message' => $result['message'],
291 ]
292 );
293 }
294 }
295
296 function test_run() {
297 // Verify CSRF nonce - reuse the nonce from the process form
298 if ( ! empty( $this->params['process']['id'] ) ) {
299 $this->check_nonce( 'edit_process_' . $this->params['process']['id'] );
300 } else {
301 $this->check_nonce( 'new_process' );
302 }
303
304 $process = new OsProcessModel();
305 $process->set_from_params( $this->params['process'] );
306
307 $action_ids_to_run = isset( $this->params['action_ids'] ) ? explode( ',', $this->params['action_ids'] ) : [];
308 $data_sources = $this->params['data_source'];
309
310 $selected_data_objects = [];
311 foreach ( $data_sources as $source_id => $data_source_value ) {
312 $object_data = OsProcessesHelper::get_object_data_by_source( $source_id, $data_source_value );
313 if ( ! empty( $object_data ) ) {
314 $selected_data_objects[] = $object_data;
315 }
316 }
317
318 if ( $process->check_if_objects_satisfy_trigger_conditions( $selected_data_objects ) ) {
319 foreach ( $process->actions as $action ) {
320 if ( $action->status != LATEPOINT_STATUS_ACTIVE ) {
321 continue;
322 }
323 if ( ! in_array( $action->id, $action_ids_to_run ) ) {
324 continue;
325 }
326 $action->selected_data_objects = $selected_data_objects;
327 $action->run();
328 }
329 $status = LATEPOINT_STATUS_SUCCESS;
330 $message = __( 'Run complete', 'latepoint' ) . '. <a href="' . OsRouterHelper::build_link( [ 'activities', 'index' ] ) . '" target="_blank">' . __( 'view logs', 'latepoint' ) . '</a>';
331 } else {
332 $status = LATEPOINT_STATUS_ERROR;
333 $message = __( 'Trigger conditions not met', 'latepoint' );
334 }
335
336
337 if ( $this->get_return_format() == 'json' ) {
338 $this->send_json(
339 [
340 'status' => $status,
341 'message' => $message,
342 ]
343 );
344 }
345 }
346
347 function test_preview() {
348 $process = new OsProcessModel();
349 $process->set_from_params( $this->params['process'] );
350
351 $action_settings_html = '';
352 $available_data_sources = $process->event->get_available_data_sources();
353
354
355 foreach ( $available_data_sources as $data_source ) {
356 $action_settings_html .= OsFormHelper::select_field(
357 'data_source[' . $data_source['name'] . ']',
358 $data_source['label'],
359 $data_source['values'],
360 $data_source['values']['0']['value'],
361 [
362 'class' => 'process-test-data-source-selector',
363 'data-route' => OsRouterHelper::build_route_name( 'processes', 'reload_action_test_preview' ),
364 ]
365 );
366 }
367 $this->vars['action_settings_html'] = $action_settings_html;
368 $this->vars['process'] = $process;
369
370 $this->format_render( __FUNCTION__ );
371 }
372
373 function action_test_preview() {
374 $action = new \LatePoint\Misc\ProcessAction();
375 // because this data is part of a bigger process form, we need to extract just the action params
376 $action->set_from_params( reset( $this->params['process']['actions'] ) );
377 $action->event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['process_event_type'] ] );
378 $action_settings_html = '';
379 $available_data_sources = $action->event->get_available_data_sources();
380 foreach ( $available_data_sources as $data_source ) {
381 $action_settings_html .= OsFormHelper::select_field(
382 'data_source[' . $data_source['name'] . ']',
383 $data_source['label'],
384 $data_source['values'],
385 $data_source['values']['0']['value'],
386 [
387 'class' => 'process-action-test-data-source-selector',
388 'data-route' => OsRouterHelper::build_route_name( 'processes', 'reload_action_test_preview' ),
389 ]
390 );
391 $action->selected_data_objects[] = [
392 'model' => $data_source['model'],
393 'id' => $data_source['values'][0]['value'],
394 ];
395 }
396 $action_settings_html .= OsFormHelper::hidden_field( 'action[type]', $action->type );
397 $action_settings_html .= OsFormHelper::hidden_field( 'action[event][type]', $action->event->type );
398 $action_settings_html .= OsFormHelper::get_hidden_fields_for_array( $action->settings, 'action[settings]' );
399 $preview_html = $action->generate_preview();
400
401 $this->vars['action'] = $action;
402 $this->vars['preview_html'] = $preview_html;
403 $this->vars['action_settings_html'] = $action_settings_html;
404
405 $this->format_render( __FUNCTION__ );
406 }
407
408
409 function reload_action_test_preview() {
410 $action = new \LatePoint\Misc\ProcessAction();
411 $action->set_from_params( $this->params['action'] );
412 $available_data_sources = $action->event->get_available_data_sources();
413 foreach ( $available_data_sources as $data_source ) {
414 $action->selected_data_objects[] = [
415 'model' => $data_source['model'],
416 'id' => $this->params['data_source'][ $data_source['name'] ],
417 ];
418 }
419 if ( $this->get_return_format() == 'json' ) {
420 $this->send_json(
421 [
422 'status' => LATEPOINT_STATUS_SUCCESS,
423 'message' => $action->generate_preview(),
424 ]
425 );
426 }
427 }
428 }
429
430 endif;
431