PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / trunk
LatePoint – Calendar Booking Plugin for Appointments and Events vtrunk
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 1 month ago auth_controller.php 3 months ago booking_form_settings_controller.php 3 months ago bookings_controller.php 10 hours ago calendars_controller.php 3 months ago carts_controller.php 10 hours ago controller.php 3 months ago customer_cabinet_controller.php 2 months ago customers_controller.php 10 hours ago dashboard_controller.php 2 months ago default_agent_controller.php 3 months ago events_controller.php 3 months ago form_fields_controller.php 1 week ago integrations_controller.php 3 months ago invoices_controller.php 10 hours 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 10 hours ago pro_controller.php 2 weeks ago process_jobs_controller.php 3 months ago processes_controller.php 1 month ago razorpay_connect_controller.php 1 week ago search_controller.php 3 months ago services_controller.php 3 months ago settings_controller.php 2 months ago steps_controller.php 2 weeks ago stripe_connect_controller.php 1 week ago support_topics_controller.php 3 months ago todos_controller.php 3 months ago transactions_controller.php 10 hours ago wizard_controller.php 1 week ago
processes_controller.php
433 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 $message = \LatePoint\Misc\ProcessEvent::value_field_html_for_trigger_condition( $property, '', $trigger_condition_id );
93
94 if ( $this->get_return_format() == 'json' ) {
95 $this->send_json(
96 array(
97 'status' => LATEPOINT_STATUS_SUCCESS,
98 'message' => $message,
99 )
100 );
101 }
102 }
103
104 function destroy() {
105 if ( filter_var( $this->params['id'], FILTER_VALIDATE_INT ) ) {
106 $this->check_nonce( 'destroy_process_' . $this->params['id'] );
107 $process = new OsProcessModel( $this->params['id'] );
108 if ( $process->delete() ) {
109 $status = LATEPOINT_STATUS_SUCCESS;
110 $response_html = __( 'Process Removed', 'latepoint' );
111 } else {
112 $status = LATEPOINT_STATUS_ERROR;
113 $response_html = __( 'Error Removing Workflow', 'latepoint' );
114 }
115 } else {
116 $status = LATEPOINT_STATUS_SUCCESS;
117 $response_html = __( 'Process Removed', 'latepoint' );
118 }
119 if ( $this->get_return_format() == 'json' ) {
120 $this->send_json(
121 array(
122 'status' => $status,
123 'message' => $response_html,
124 )
125 );
126 }
127 }
128
129 function save() {
130 $process_data = $this->params['process'];
131 if ( ! empty( $process_data['id'] ) ) {
132 $this->check_nonce( 'edit_process_' . $process_data['id'] );
133 $process = new OsProcessModel( $process_data['id'] );
134 $new_process = false;
135 } else {
136 $this->check_nonce( 'new_process' );
137 $process = new OsProcessModel();
138 $new_process = true;
139 }
140
141 $process->status = $process_data['status'] ?? 'active';
142 $process->name = $process_data['name'];
143 $process->event_type = $process_data['event']['type'];
144 $actions = [];
145
146
147 // check if conditions are turned ON and exist in params
148 $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'] : [];
149 if ( isset( $process_data['actions'] ) ) {
150 $actions = OsProcessesHelper::iterate_trigger_conditions( $trigger_conditions, $process_data['actions'] );
151
152 if ( $process_data['event']['has_time_offset'] == LATEPOINT_VALUE_ON ) {
153 $actions[0]['time_offset'] = $process_data['event']['time_offset'];
154 } else {
155 $actions[0]['time_offset'] = [];
156 }
157 } else {
158 $actions = [];
159 }
160
161 $process->actions_json = wp_json_encode( $actions );
162 $old_process = $process->is_new_record() ? [] : clone $process;
163 if ( $process->save() ) {
164 if ( ! $new_process ) {
165 // remove previously created jobs for this process that hasn't run yet
166 $jobs = new OsProcessJobModel();
167 $jobs->delete_where(
168 [
169 'process_id' => $process->id,
170 'status' => LATEPOINT_JOB_STATUS_SCHEDULED,
171 ]
172 );
173 /**
174 * Process was updated
175 *
176 * @param {OsProcessModel} $process instance of process model that was updated
177 * @param {OsProcessModel} $old_process instance of process model before it was updated
178 *
179 * @since 4.7.0
180 * @hook latepoint_process_updated
181 *
182 */
183 do_action( 'latepoint_process_updated', $process, $old_process );
184 } else {
185 /**
186 * Process was created
187 *
188 * @param {OsProcessModel} $process instance of process model that was created
189 *
190 * @since 4.7.0
191 * @hook latepoint_process_created
192 *
193 */
194 do_action( 'latepoint_process_created', $process );
195 }
196 $process->build_from_json();
197 OsProcessJobsHelper::recreate_jobs_for_existing_records( $process );
198 $message = __( 'Process Saved', 'latepoint' );
199 $status = LATEPOINT_STATUS_SUCCESS;
200 } else {
201 $message = __( 'Error saving process', 'latepoint' );
202 $status = LATEPOINT_STATUS_ERROR;
203 }
204
205 if ( $this->get_return_format() == 'json' ) {
206 $this->send_json(
207 array(
208 'status' => $status,
209 'message' => $message,
210 )
211 );
212 }
213 }
214
215 function new_trigger_condition() {
216 $process_event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['event_type'] ] );
217 $trigger_condition_html = $process_event->generate_trigger_condition_form_html();
218 if ( $this->get_return_format() == 'json' ) {
219 $this->send_json(
220 array(
221 'status' => LATEPOINT_STATUS_SUCCESS,
222 'message' => $trigger_condition_html,
223 )
224 );
225 }
226 }
227
228 function new_action() {
229 $action = new \LatePoint\Misc\ProcessAction();
230 $process_id = ! empty( $this->params['process_id'] ) ? sanitize_text_field( $this->params['process_id'] ) : '';
231 $response_html = \LatePoint\Misc\ProcessAction::generate_form( $action, $process_id );
232
233 if ( $this->get_return_format() == 'json' ) {
234 $this->send_json(
235 array(
236 'status' => LATEPOINT_STATUS_SUCCESS,
237 'message' => $response_html,
238 )
239 );
240 }
241 }
242
243 function load_action_settings() {
244 $action = new \LatePoint\Misc\ProcessAction(
245 [
246 'type' => $this->params['action_type'],
247 'id' => $this->params['action_id'],
248 ]
249 );
250
251 $template_id = $this->params['template_id'] ?? false;
252 if ( $template_id ) {
253 $action->load_settings_from_template( $template_id );
254 }
255
256 if ( $this->get_return_format() == 'json' ) {
257 $this->send_json(
258 array(
259 'status' => LATEPOINT_STATUS_SUCCESS,
260 'message' => \LatePoint\Misc\ProcessAction::generate_settings_fields( $action ),
261 )
262 );
263 }
264 }
265
266 function index() {
267 $processes = new OsProcessModel();
268 $this->vars['processes'] = $processes->get_results_as_models();
269 $this->format_render( __FUNCTION__ );
270 }
271
272 function action_test_run() {
273 // Verify CSRF nonce
274 $this->check_nonce( 'test_process_action' );
275
276 $action = new \LatePoint\Misc\ProcessAction();
277 $action->set_from_params( $this->params['action'] );
278
279 $available_data_sources = $action->event->get_available_data_sources();
280 foreach ( $available_data_sources as $data_source ) {
281 $action->selected_data_objects[] = [
282 'model' => $data_source['model'],
283 'id' => $this->params['data_source'][ $data_source['name'] ],
284 ];
285 }
286
287 $result = $action->run();
288 if ( $this->get_return_format() == 'json' ) {
289 $this->send_json(
290 [
291 'status' => $result['status'],
292 'message' => $result['message'],
293 ]
294 );
295 }
296 }
297
298 function test_run() {
299 // Verify CSRF nonce - reuse the nonce from the process form
300 if ( ! empty( $this->params['process']['id'] ) ) {
301 $this->check_nonce( 'edit_process_' . $this->params['process']['id'] );
302 } else {
303 $this->check_nonce( 'new_process' );
304 }
305
306 $process = new OsProcessModel();
307 $process->set_from_params( $this->params['process'] );
308
309 $action_ids_to_run = isset( $this->params['action_ids'] ) ? explode( ',', $this->params['action_ids'] ) : [];
310 $data_sources = $this->params['data_source'];
311
312 $selected_data_objects = [];
313 foreach ( $data_sources as $source_id => $data_source_value ) {
314 $object_data = OsProcessesHelper::get_object_data_by_source( $source_id, $data_source_value );
315 if ( ! empty( $object_data ) ) {
316 $selected_data_objects[] = $object_data;
317 }
318 }
319
320 if ( $process->check_if_objects_satisfy_trigger_conditions( $selected_data_objects ) ) {
321 foreach ( $process->actions as $action ) {
322 if ( $action->status != LATEPOINT_STATUS_ACTIVE ) {
323 continue;
324 }
325 if ( ! in_array( $action->id, $action_ids_to_run ) ) {
326 continue;
327 }
328 $action->selected_data_objects = $selected_data_objects;
329 $action->run();
330 }
331 $status = LATEPOINT_STATUS_SUCCESS;
332 $message = __( 'Run complete', 'latepoint' ) . '. <a href="' . OsRouterHelper::build_link( [ 'activities', 'index' ] ) . '" target="_blank">' . __( 'view logs', 'latepoint' ) . '</a>';
333 } else {
334 $status = LATEPOINT_STATUS_ERROR;
335 $message = __( 'Trigger conditions not met', 'latepoint' );
336 }
337
338
339 if ( $this->get_return_format() == 'json' ) {
340 $this->send_json(
341 [
342 'status' => $status,
343 'message' => $message,
344 ]
345 );
346 }
347 }
348
349 function test_preview() {
350 $process = new OsProcessModel();
351 $process->set_from_params( $this->params['process'] );
352
353 $action_settings_html = '';
354 $available_data_sources = $process->event->get_available_data_sources();
355
356
357 foreach ( $available_data_sources as $data_source ) {
358 $action_settings_html .= OsFormHelper::select_field(
359 'data_source[' . $data_source['name'] . ']',
360 $data_source['label'],
361 $data_source['values'],
362 $data_source['values']['0']['value'],
363 [
364 'class' => 'process-test-data-source-selector',
365 'data-route' => OsRouterHelper::build_route_name( 'processes', 'reload_action_test_preview' ),
366 ]
367 );
368 }
369 $this->vars['action_settings_html'] = $action_settings_html;
370 $this->vars['process'] = $process;
371
372 $this->format_render( __FUNCTION__ );
373 }
374
375 function action_test_preview() {
376 $action = new \LatePoint\Misc\ProcessAction();
377 // because this data is part of a bigger process form, we need to extract just the action params
378 $action->set_from_params( reset( $this->params['process']['actions'] ) );
379 $action->event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['process_event_type'] ] );
380 $action_settings_html = '';
381 $available_data_sources = $action->event->get_available_data_sources();
382 foreach ( $available_data_sources as $data_source ) {
383 $action_settings_html .= OsFormHelper::select_field(
384 'data_source[' . $data_source['name'] . ']',
385 $data_source['label'],
386 $data_source['values'],
387 $data_source['values']['0']['value'],
388 [
389 'class' => 'process-action-test-data-source-selector',
390 'data-route' => OsRouterHelper::build_route_name( 'processes', 'reload_action_test_preview' ),
391 ]
392 );
393 $action->selected_data_objects[] = [
394 'model' => $data_source['model'],
395 'id' => $data_source['values'][0]['value'],
396 ];
397 }
398 $action_settings_html .= OsFormHelper::hidden_field( 'action[type]', $action->type );
399 $action_settings_html .= OsFormHelper::hidden_field( 'action[event][type]', $action->event->type );
400 $action_settings_html .= OsFormHelper::get_hidden_fields_for_array( $action->settings, 'action[settings]' );
401 $preview_html = $action->generate_preview();
402
403 $this->vars['action'] = $action;
404 $this->vars['preview_html'] = $preview_html;
405 $this->vars['action_settings_html'] = $action_settings_html;
406
407 $this->format_render( __FUNCTION__ );
408 }
409
410
411 function reload_action_test_preview() {
412 $action = new \LatePoint\Misc\ProcessAction();
413 $action->set_from_params( $this->params['action'] );
414 $available_data_sources = $action->event->get_available_data_sources();
415 foreach ( $available_data_sources as $data_source ) {
416 $action->selected_data_objects[] = [
417 'model' => $data_source['model'],
418 'id' => $this->params['data_source'][ $data_source['name'] ],
419 ];
420 }
421 if ( $this->get_return_format() == 'json' ) {
422 $this->send_json(
423 [
424 'status' => LATEPOINT_STATUS_SUCCESS,
425 'message' => $action->generate_preview(),
426 ]
427 );
428 }
429 }
430 }
431
432 endif;
433