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 | $process_id = !empty($this->params['process_id']) ? sanitize_text_field($this->params['process_id']) : ''; |
| 188 | $response_html = \LatePoint\Misc\ProcessAction::generate_form( $action, $process_id ); |
| 189 | |
| 190 | if ( $this->get_return_format() == 'json' ) { |
| 191 | $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $response_html ) ); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | function load_action_settings() { |
| 196 | $action = new \LatePoint\Misc\ProcessAction( [ 'type' => $this->params['action_type'], 'id' => $this->params['action_id'] ] ); |
| 197 | |
| 198 | $template_id = $this->params['template_id'] ?? false; |
| 199 | if ( $template_id ) { |
| 200 | $action->load_settings_from_template( $template_id ); |
| 201 | } |
| 202 | |
| 203 | if ( $this->get_return_format() == 'json' ) { |
| 204 | $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => \LatePoint\Misc\ProcessAction::generate_settings_fields( $action ) ) ); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | function index() { |
| 209 | $processes = new OsProcessModel(); |
| 210 | $this->vars['processes'] = $processes->get_results_as_models(); |
| 211 | $this->format_render( __FUNCTION__ ); |
| 212 | } |
| 213 | |
| 214 | function action_test_run() { |
| 215 | $action = new \LatePoint\Misc\ProcessAction(); |
| 216 | $action->set_from_params( $this->params['action'] ); |
| 217 | |
| 218 | $available_data_sources = $action->event->get_available_data_sources(); |
| 219 | foreach ( $available_data_sources as $data_source ) { |
| 220 | $action->selected_data_objects[] = [ 'model' => $data_source['model'], 'id' => $this->params['data_source'][ $data_source['name'] ] ]; |
| 221 | } |
| 222 | |
| 223 | $result = $action->run(); |
| 224 | if ( $this->get_return_format() == 'json' ) { |
| 225 | $this->send_json( [ 'status' => $result['status'], 'message' => $result['message'] ] ); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | function test_run() { |
| 230 | $process = new OsProcessModel(); |
| 231 | $process->set_from_params( $this->params['process'] ); |
| 232 | |
| 233 | $action_ids_to_run = isset( $this->params['action_ids'] ) ? explode( ',', $this->params['action_ids'] ) : []; |
| 234 | $data_sources = $this->params['data_source']; |
| 235 | |
| 236 | $selected_data_objects = []; |
| 237 | foreach ( $data_sources as $source_id => $data_source_value ) { |
| 238 | $object_data = OsProcessesHelper::get_object_data_by_source( $source_id, $data_source_value ); |
| 239 | if(!empty($object_data)){ |
| 240 | $selected_data_objects[] = $object_data; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if ( $process->check_if_objects_satisfy_trigger_conditions( $selected_data_objects ) ) { |
| 245 | foreach ( $process->actions as $action ) { |
| 246 | if ( $action->status != LATEPOINT_STATUS_ACTIVE ) { |
| 247 | continue; |
| 248 | } |
| 249 | if ( ! in_array( $action->id, $action_ids_to_run ) ) { |
| 250 | continue; |
| 251 | } |
| 252 | $action->selected_data_objects = $selected_data_objects; |
| 253 | $action->run(); |
| 254 | } |
| 255 | $status = LATEPOINT_STATUS_SUCCESS; |
| 256 | $message = __( 'Run complete', 'latepoint' ) . '. <a href="' . OsRouterHelper::build_link( [ 'activities', 'index' ] ) . '" target="_blank">' . __( 'view logs', 'latepoint' ) . '</a>'; |
| 257 | } else { |
| 258 | $status = LATEPOINT_STATUS_ERROR; |
| 259 | $message = __( 'Trigger conditions not met', 'latepoint' ); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | if ( $this->get_return_format() == 'json' ) { |
| 264 | $this->send_json( [ 'status' => $status, 'message' => $message ] ); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | function test_preview() { |
| 269 | $process = new OsProcessModel(); |
| 270 | $process->set_from_params( $this->params['process'] ); |
| 271 | |
| 272 | $action_settings_html = ''; |
| 273 | $available_data_sources = $process->event->get_available_data_sources(); |
| 274 | |
| 275 | |
| 276 | foreach ( $available_data_sources as $data_source ) { |
| 277 | $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', |
| 278 | 'data-route' => OsRouterHelper::build_route_name( 'processes', 'reload_action_test_preview' ) |
| 279 | ] ); |
| 280 | } |
| 281 | $this->vars['action_settings_html'] = $action_settings_html; |
| 282 | $this->vars['process'] = $process; |
| 283 | |
| 284 | $this->format_render( __FUNCTION__ ); |
| 285 | } |
| 286 | |
| 287 | function action_test_preview() { |
| 288 | $action = new \LatePoint\Misc\ProcessAction(); |
| 289 | // because this data is part of a bigger process form, we need to extract just the action params |
| 290 | $action->set_from_params( reset( $this->params['process']['actions'] ) ); |
| 291 | $action->event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['process_event_type'] ] ); |
| 292 | $action_settings_html = ''; |
| 293 | $available_data_sources = $action->event->get_available_data_sources(); |
| 294 | foreach ( $available_data_sources as $data_source ) { |
| 295 | $action_settings_html .= OsFormHelper::select_field( 'data_source[' . $data_source['name'] . ']', $data_source['label'], $data_source['values'], $data_source['values']['0']['value'], [ |
| 296 | 'class' => 'process-action-test-data-source-selector', |
| 297 | 'data-route' => OsRouterHelper::build_route_name( 'processes', 'reload_action_test_preview' ) |
| 298 | ] ); |
| 299 | $action->selected_data_objects[] = [ 'model' => $data_source['model'], 'id' => $data_source['values'][0]['value'] ]; |
| 300 | } |
| 301 | $action_settings_html .= OsFormHelper::hidden_field( 'action[type]', $action->type ); |
| 302 | $action_settings_html .= OsFormHelper::hidden_field( 'action[event][type]', $action->event->type ); |
| 303 | $action_settings_html.= OsFormHelper::get_hidden_fields_for_array($action->settings, 'action[settings]'); |
| 304 | $preview_html = $action->generate_preview(); |
| 305 | |
| 306 | $this->vars['action'] = $action; |
| 307 | $this->vars['preview_html'] = $preview_html; |
| 308 | $this->vars['action_settings_html'] = $action_settings_html; |
| 309 | |
| 310 | $this->format_render( __FUNCTION__ ); |
| 311 | } |
| 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; |