PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.1.1
LatePoint – Calendar Booking Plugin for Appointments and Events v5.1.1
5.6.7 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 / controllers / processes_controller.php
latepoint / lib / controllers Last commit date
activities_controller.php 1 year ago auth_controller.php 1 year ago booking_form_settings_controller.php 1 year ago bookings_controller.php 1 year ago calendars_controller.php 1 year ago carts_controller.php 1 year ago controller.php 1 year ago customer_cabinet_controller.php 1 year ago customers_controller.php 1 year ago dashboard_controller.php 1 year ago default_agent_controller.php 1 year ago events_controller.php 1 year ago form_fields_controller.php 1 year ago integrations_controller.php 1 year ago invoices_controller.php 1 year ago manage_booking_by_key_controller.php 1 year ago manage_order_by_key_controller.php 1 year ago notifications_controller.php 1 year ago orders_controller.php 1 year ago pro_controller.php 1 year ago process_jobs_controller.php 1 year ago processes_controller.php 1 year ago search_controller.php 1 year ago services_controller.php 1 year ago settings_controller.php 1 year ago steps_controller.php 1 year ago stripe_connect_controller.php 1 year ago support_topics_controller.php 1 year ago todos_controller.php 1 year ago transactions_controller.php 1 year ago wizard_controller.php 1 year ago
processes_controller.php
328 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( 'label' => __( 'Workflows', 'latepoint' ), 'link' => OsRouterHelper::build_link( OsRouterHelper::build_route_name( 'processes', 'index' ) ) );
24 }
25
26 public function new_form() {
27 $this->vars['process'] = new OsProcessModel();
28 $this->set_layout( 'none' );
29 $this->format_render( __FUNCTION__ );
30 }
31
32 public function reload_event_trigger_conditions() {
33 $event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['event_type'] ] );
34 $trigger_conditions_form_section_html = OsProcessesHelper::trigger_conditions_html_for_event( $event );
35
36 if ( $this->get_return_format() == 'json' ) {
37 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $trigger_conditions_form_section_html ) );
38 }
39 }
40
41
42 public function available_properties_for_object_code() {
43 $object_code = $this->params['object_code'];
44 $properties_for_select = \LatePoint\Misc\ProcessEvent::get_properties_for_object_code( $object_code, true );
45 $html = '';
46 foreach ( $properties_for_select as $property ) {
47 $html .= '<option value="' . $property['value'] . '">' . $property['label'] . '</option>';
48 }
49 if ( $this->get_return_format() == 'json' ) {
50 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $html ) );
51 }
52 }
53
54
55 public function available_operators_for_trigger_condition_property() {
56 // example format: old_booking__agent_id
57 $property = $this->params['property'];
58 $operators = \LatePoint\Misc\ProcessEvent::trigger_condition_operators_for_property( $property );
59 $html = '';
60 foreach ( $operators as $value => $label ) {
61 $html .= '<option value="' . $value . '">' . $label . '</option>';
62 }
63 if ( $this->get_return_format() == 'json' ) {
64 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $html ) );
65 }
66 }
67
68 public function available_values_for_trigger_condition_property() {
69 $values = [];
70 $property = $this->params['property'];
71 $operator = $this->params['operator'];
72 $trigger_condition_id = $this->params['trigger_condition_id'];
73 $values = OsProcessesHelper::values_for_trigger_condition_property( $property );
74 if ( $this->get_return_format() == 'json' ) {
75 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS,
76 'message' => OsFormHelper::multi_select_field( 'process[event][trigger_conditions][' . $trigger_condition_id . '][value]', false, $values, false, [] )
77 ) );
78 }
79 }
80
81 function destroy() {
82 if ( filter_var( $this->params['id'], FILTER_VALIDATE_INT ) ) {
83 $this->check_nonce( 'destroy_process_' . $this->params['id'] );
84 $process = new OsProcessModel( $this->params['id'] );
85 if ( $process->delete() ) {
86 $status = LATEPOINT_STATUS_SUCCESS;
87 $response_html = __( 'Process Removed', 'latepoint' );
88 } else {
89 $status = LATEPOINT_STATUS_ERROR;
90 $response_html = __( 'Error Removing Workflow', 'latepoint' );
91 }
92 } else {
93 $status = LATEPOINT_STATUS_SUCCESS;
94 $response_html = __( 'Process Removed', 'latepoint' );
95 }
96 if ( $this->get_return_format() == 'json' ) {
97 $this->send_json( array( 'status' => $status, 'message' => $response_html ) );
98 }
99 }
100
101 function save() {
102 $process_data = $this->params['process'];
103 if ( ! empty( $process_data['id'] ) ) {
104 $this->check_nonce( 'edit_process_' . $process_data['id'] );
105 $process = new OsProcessModel( $process_data['id'] );
106 $new_process = false;
107 } else {
108 $this->check_nonce( 'new_process' );
109 $process = new OsProcessModel();
110 $new_process = true;
111 }
112
113 $process->status = $process_data['status'] ?? 'active';
114 $process->name = $process_data['name'];
115 $process->event_type = $process_data['event']['type'];
116 $actions = [];
117
118
119 // check if conditions are turned ON and exist in params
120 $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'] : [];
121 if ( isset( $process_data['actions'] ) ) {
122 $actions = OsProcessesHelper::iterate_trigger_conditions( $trigger_conditions, $process_data['actions'] );
123
124 if ( $process_data['event']['has_time_offset'] == LATEPOINT_VALUE_ON ) {
125 $actions[0]['time_offset'] = $process_data['event']['time_offset'];
126 } else {
127 $actions[0]['time_offset'] = [];
128 }
129 } else {
130 $actions = [];
131 }
132
133 $process->actions_json = wp_json_encode( $actions );
134 $old_process = $process->is_new_record() ? [] : clone $process;
135 if ( $process->save() ) {
136 if ( ! $new_process ) {
137 // remove previously created jobs for this process that hasn't run yet
138 $jobs = new OsProcessJobModel();
139 $jobs->delete_where( [ 'process_id' => $process->id, 'status' => LATEPOINT_JOB_STATUS_SCHEDULED ] );
140 /**
141 * Process was updated
142 *
143 * @param {OsProcessModel} $process instance of process model that was updated
144 * @param {OsProcessModel} $old_process instance of process model before it was updated
145 *
146 * @since 4.7.0
147 * @hook latepoint_process_updated
148 *
149 */
150 do_action( 'latepoint_process_updated', $process, $old_process );
151 } else {
152 /**
153 * Process was created
154 *
155 * @param {OsProcessModel} $process instance of process model that was created
156 *
157 * @since 4.7.0
158 * @hook latepoint_process_created
159 *
160 */
161 do_action( 'latepoint_process_created', $process );
162 }
163 $process->build_from_json();
164 OsProcessJobsHelper::recreate_jobs_for_existing_records( $process );
165 $message = __( 'Process Saved', 'latepoint' );
166 $status = LATEPOINT_STATUS_SUCCESS;
167 } else {
168 $message = __( 'Error saving process', 'latepoint' );
169 $status = LATEPOINT_STATUS_ERROR;
170 }
171
172 if ( $this->get_return_format() == 'json' ) {
173 $this->send_json( array( 'status' => $status, 'message' => $message ) );
174 }
175 }
176
177 function new_trigger_condition() {
178 $process_event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['event_type'] ] );
179 $trigger_condition_html = $process_event->generate_trigger_condition_form_html();
180 if ( $this->get_return_format() == 'json' ) {
181 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $trigger_condition_html ) );
182 }
183 }
184
185 function new_action() {
186 $action = new \LatePoint\Misc\ProcessAction();
187 $response_html = \LatePoint\Misc\ProcessAction::generate_form( $action );
188
189 if ( $this->get_return_format() == 'json' ) {
190 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $response_html ) );
191 }
192 }
193
194 function load_action_settings() {
195 $action = new \LatePoint\Misc\ProcessAction( [ 'type' => $this->params['action_type'], 'id' => $this->params['action_id'] ] );
196
197 $template_id = $this->params['template_id'] ?? false;
198 if ( $template_id ) {
199 $action->load_settings_from_template( $template_id );
200 }
201
202 if ( $this->get_return_format() == 'json' ) {
203 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => \LatePoint\Misc\ProcessAction::generate_settings_fields( $action ) ) );
204 }
205 }
206
207 function index() {
208 $processes = new OsProcessModel();
209 $this->vars['processes'] = $processes->get_results_as_models();
210 $this->format_render( __FUNCTION__ );
211 }
212
213 function action_test_run() {
214 $action = new \LatePoint\Misc\ProcessAction();
215 $action->set_from_params( $this->params['action'] );
216
217 $available_data_sources = $action->event->get_available_data_sources();
218 foreach ( $available_data_sources as $data_source ) {
219 $action->selected_data_objects[] = [ 'model' => $data_source['model'], 'id' => $this->params['data_source'][ $data_source['name'] ] ];
220 }
221
222 $result = $action->run();
223 if ( $this->get_return_format() == 'json' ) {
224 $this->send_json( [ 'status' => $result['status'], 'message' => $result['message'] ] );
225 }
226 }
227
228 function test_run() {
229 $process = new OsProcessModel();
230 $process->set_from_params( $this->params['process'] );
231
232 $action_ids_to_run = isset( $this->params['action_ids'] ) ? explode( ',', $this->params['action_ids'] ) : [];
233 $data_sources = $this->params['data_source'];
234
235 $selected_data_objects = [];
236 foreach ( $data_sources as $source_id => $data_source_value ) {
237 $object_data = OsProcessesHelper::get_object_data_by_source( $source_id, $data_source_value );
238 if(!empty($object_data)){
239 $selected_data_objects[] = $object_data;
240 }
241 }
242
243 if ( $process->check_if_objects_satisfy_trigger_conditions( $selected_data_objects ) ) {
244 foreach ( $process->actions as $action ) {
245 if ( $action->status != LATEPOINT_STATUS_ACTIVE ) {
246 continue;
247 }
248 if ( ! in_array( $action->id, $action_ids_to_run ) ) {
249 continue;
250 }
251 $action->selected_data_objects = $selected_data_objects;
252 $action->run();
253 }
254 $status = LATEPOINT_STATUS_SUCCESS;
255 $message = __( 'Run complete', 'latepoint' ) . '. <a href="' . OsRouterHelper::build_link( [ 'activities', 'index' ] ) . '" target="_blank">' . __( 'view logs', 'latepoint' ) . '</a>';
256 } else {
257 $status = LATEPOINT_STATUS_ERROR;
258 $message = __( 'Trigger conditions not met', 'latepoint' );
259 }
260
261
262 if ( $this->get_return_format() == 'json' ) {
263 $this->send_json( [ 'status' => $status, 'message' => $message ] );
264 }
265 }
266
267 function test_preview() {
268 $process = new OsProcessModel();
269 $process->set_from_params( $this->params['process'] );
270
271 $action_settings_html = '';
272 $available_data_sources = $process->event->get_available_data_sources();
273
274
275 foreach ( $available_data_sources as $data_source ) {
276 $action_settings_html .= OsFormHelper::select_field( 'data_source[' . $data_source['name'] . ']', $data_source['label'], $data_source['values'], $data_source['values']['0']['value'], [ 'class' => 'process-test-data-source-selector',
277 'data-route' => OsRouterHelper::build_route_name( 'processes', 'reload_action_test_preview' )
278 ] );
279 }
280 $this->vars['action_settings_html'] = $action_settings_html;
281 $this->vars['process'] = $process;
282
283 $this->format_render( __FUNCTION__ );
284 }
285
286 function action_test_preview() {
287 $action = new \LatePoint\Misc\ProcessAction();
288 // because this data is part of a bigger process form, we need to extract just the action params
289 $action->set_from_params( reset( $this->params['process']['actions'] ) );
290 $action->event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['process_event_type'] ] );
291 $action_settings_html = '';
292 $available_data_sources = $action->event->get_available_data_sources();
293 foreach ( $available_data_sources as $data_source ) {
294 $action_settings_html .= OsFormHelper::select_field( 'data_source[' . $data_source['name'] . ']', $data_source['label'], $data_source['values'], $data_source['values']['0']['value'], [
295 'class' => 'process-action-test-data-source-selector',
296 'data-route' => OsRouterHelper::build_route_name( 'processes', 'reload_action_test_preview' )
297 ] );
298 $action->selected_data_objects[] = [ 'model' => $data_source['model'], 'id' => $data_source['values'][0]['value'] ];
299 }
300 $action_settings_html .= OsFormHelper::hidden_field( 'action[type]', $action->type );
301 $action_settings_html .= OsFormHelper::hidden_field( 'action[event][type]', $action->event->type );
302 foreach ( $action->settings as $key => $setting ) {
303 $action_settings_html .= OsFormHelper::hidden_field( 'action[settings][' . $key . ']', $setting );
304 }
305 $preview_html = $action->generate_preview();
306
307 $this->vars['action'] = $action;
308 $this->vars['preview_html'] = $preview_html;
309 $this->vars['action_settings_html'] = $action_settings_html;
310
311 $this->format_render( __FUNCTION__ );
312 }
313
314 function reload_action_test_preview() {
315 $action = new \LatePoint\Misc\ProcessAction();
316 $action->set_from_params( $this->params['action'] );
317 $available_data_sources = $action->event->get_available_data_sources();
318 foreach ( $available_data_sources as $data_source ) {
319 $action->selected_data_objects[] = [ 'model' => $data_source['model'], 'id' => $this->params['data_source'][ $data_source['name'] ] ];
320 }
321 if ( $this->get_return_format() == 'json' ) {
322 $this->send_json( [ 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $action->generate_preview() ] );
323 }
324 }
325
326 }
327
328 endif;