| |
| <?php
|
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if (!defined('ABSPATH')) {
|
| exit;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| function mst_activate_case($entry_id, $user_id) {
|
|
|
|
|
| if (empty($entry_id) || !is_numeric($entry_id)) {
|
| error_log('MST Activation ERROR: Invalid entry_id received: ' . print_r($entry_id, true));
|
| return false;
|
| }
|
|
|
| $entry_id = intval($entry_id);
|
| $user_id = intval($user_id);
|
|
|
| error_log("MST Activation START: Entry {$entry_id}, User {$user_id}");
|
|
|
|
|
| if (!class_exists('GFAPI')) {
|
| error_log('MST Activation ERROR: Gravity Forms not available');
|
| return false;
|
| }
|
|
|
| $entry_269 = GFAPI::get_entry($entry_id);
|
| if (is_wp_error($entry_269)) {
|
| error_log('MST Activation ERROR: Could not get Form 269 entry ' . $entry_id . ' - ' . $entry_269->get_error_message());
|
| return false;
|
| }
|
|
|
|
|
| if (intval($entry_269['form_id']) !== 269) {
|
| error_log('MST Activation ERROR: Entry ' . $entry_id . ' is not from Form 269 (got form ' . $entry_269['form_id'] . ')');
|
| return false;
|
| }
|
|
|
|
|
| $source_entry_id = intval(rgar($entry_269, '160'));
|
| if (empty($source_entry_id)) {
|
| error_log('MST Activation ERROR: No source entry ID in Field 160');
|
| return false;
|
| }
|
|
|
| $entry_223 = GFAPI::get_entry($source_entry_id);
|
| $has_223 = !is_wp_error($entry_223);
|
|
|
| if (!$has_223) {
|
| error_log('MST Activation WARNING: Could not get Form 223 entry ' . $source_entry_id . ' - will use 269 data only');
|
| }
|
|
|
|
|
| $therapist_id = intval(rgar($entry_269, '149'));
|
| if (empty($therapist_id)) {
|
| error_log('MST Activation ERROR: No therapist ID in Field 149');
|
| return false;
|
| }
|
|
|
| $msti_number = sanitize_text_field(rgar($entry_269, '148'));
|
| if (empty($msti_number)) {
|
| error_log('MST Activation ERROR: No MSTI number in Field 148');
|
| return false;
|
| }
|
|
|
| $activation_date = sanitize_text_field(rgar($entry_269, '161'));
|
|
|
|
|
| $get = function($field_269, $field_223 = null) use ($entry_269, $entry_223, $has_223) {
|
|
|
| if ($field_269 !== null) {
|
| $val = rgar($entry_269, (string)$field_269);
|
| if (!empty($val)) {
|
| return $val;
|
| }
|
| }
|
|
|
| if ($field_223 !== null && $has_223) {
|
| return rgar($entry_223, (string)$field_223);
|
| }
|
| return '';
|
| };
|
|
|
|
|
| $youth_first = $get('30.3', '30.3');
|
| $youth_last = $get('30.6', '30.6');
|
| $post_title = "{$youth_last}, {$youth_first} - MSTI-{$msti_number}";
|
|
|
|
|
| $post_data = array(
|
| 'post_type' => 'mst_case',
|
| 'post_title' => $post_title,
|
| 'post_status' => 'publish',
|
| 'post_author' => $therapist_id,
|
| );
|
|
|
| $post_id = wp_insert_post($post_data, true);
|
|
|
| if (is_wp_error($post_id)) {
|
| error_log('MST Activation ERROR: Failed to create post - ' . $post_id->get_error_message());
|
| return false;
|
| }
|
|
|
| error_log("MST Activation: Created mst_case post {$post_id}");
|
|
|
|
|
|
|
|
|
|
|
| update_field('msti_number', $msti_number, $post_id);
|
| update_field('assigned_therapist_id', $therapist_id, $post_id);
|
| update_field('case_created_date', $activation_date, $post_id);
|
| update_field('ref_entry_id', $source_entry_id, $post_id);
|
|
|
|
|
| update_field('ref_referral_date', $get('159', '1'), $post_id);
|
| update_field('ref_referral_id', $get(null, '49'), $post_id);
|
| update_field('ref_msti_number', $get(null, '138'), $post_id);
|
| update_field('ref_disposition', $get(null, '57'), $post_id);
|
|
|
|
|
| update_field('ref_source_type', $get(null, '128'), $post_id);
|
| update_field('ref_source_prefix', $get(null, '32.2'), $post_id);
|
| update_field('ref_source_first', $get(null, '32.3'), $post_id);
|
| update_field('ref_source_middle', $get(null, '32.4'), $post_id);
|
| update_field('ref_source_last', $get(null, '32.6'), $post_id);
|
| update_field('ref_source_suffix', $get(null, '32.8'), $post_id);
|
| update_field('ref_source_phone', $get(null, '39'), $post_id);
|
| update_field('ref_source_email', $get(null, '40'), $post_id);
|
| update_field('ref_djs_specialist', $get(null, '141'), $post_id);
|
| update_field('ref_djs_assist', $get('157', '142'), $post_id);
|
| update_field('ref_dss_worker', $get(null, '143'), $post_id);
|
| update_field('ref_dss_cjams', $get(null, '144'), $post_id);
|
| update_field('ref_source_other_specify', $get(null, '129'), $post_id);
|
| update_field('ref_djs_involvement', $get(null, '130'), $post_id);
|
| update_field('ref_dss_involvement', $get(null, '131'), $post_id);
|
|
|
|
|
| update_field('ref_youth_prefix', $get(null, '30.2'), $post_id);
|
| update_field('ref_youth_first', $get('30.3', '30.3'), $post_id);
|
| update_field('ref_youth_middle', $get(null, '30.4'), $post_id);
|
| update_field('ref_youth_last', $get('30.6', '30.6'), $post_id);
|
| update_field('ref_youth_suffix', $get(null, '30.8'), $post_id);
|
| update_field('ref_youth_dob', $get('4', '4'), $post_id);
|
|
|
|
|
| $dob = $get('4', '4');
|
| if (!empty($dob)) {
|
| try {
|
| $dob_date = new DateTime($dob);
|
| $today = new DateTime('today');
|
| $age = $dob_date->diff($today)->y;
|
| update_field('ref_youth_age', $age, $post_id);
|
| } catch (Exception $e) {
|
| error_log('MST Activation WARNING: Could not calculate age from DOB: ' . $dob);
|
| }
|
| }
|
|
|
| update_field('ref_youth_gender', $get('123', '123'), $post_id);
|
| update_field('ref_youth_ethnicity', $get('124', '124'), $post_id);
|
| update_field('ref_youth_race', $get('125', '125'), $post_id);
|
| update_field('ref_county_residence', $get('126', '126'), $post_id);
|
|
|
|
|
| update_field('ref_youth_address_street', $get('5.1', '5.1'), $post_id);
|
| update_field('ref_youth_address_line2', $get('5.2', '5.2'), $post_id);
|
| update_field('ref_youth_address_city', $get('5.3', '5.3'), $post_id);
|
| update_field('ref_youth_address_state', $get('5.4', '5.4'), $post_id);
|
| update_field('ref_youth_address_zip', $get('5.5', '5.5'), $post_id);
|
| update_field('ref_youth_phone', $get(null, '6'), $post_id);
|
| update_field('ref_youth_email', $get(null, '25'), $post_id);
|
| update_field('ref_youth_school', $get(null, '7'), $post_id);
|
| update_field('ref_youth_grade', $get(null, '26'), $post_id);
|
|
|
|
|
| update_field('ref_guardian_relationship', $get(null, '10'), $post_id);
|
| update_field('ref_guardian_prefix', $get(null, '11.2'), $post_id);
|
| update_field('ref_guardian_first', $get(null, '11.3'), $post_id);
|
| update_field('ref_guardian_middle', $get(null, '11.4'), $post_id);
|
| update_field('ref_guardian_last', $get(null, '11.6'), $post_id);
|
| update_field('ref_guardian_suffix', $get(null, '11.8'), $post_id);
|
| update_field('ref_guardian_email', $get(null, '12'), $post_id);
|
| update_field('ref_guardian_phone', $get(null, '13'), $post_id);
|
|
|
|
|
| update_field('ref_legal_status', $get(null, '50'), $post_id);
|
| update_field('ref_key_participants', $get(null, '51'), $post_id);
|
|
|
|
|
| update_field('ref_po_prefix', $get(null, '34.2'), $post_id);
|
| update_field('ref_po_first', $get(null, '34.3'), $post_id);
|
| update_field('ref_po_middle', $get(null, '34.4'), $post_id);
|
| update_field('ref_po_last', $get(null, '34.6'), $post_id);
|
| update_field('ref_po_suffix', $get(null, '34.8'), $post_id);
|
| update_field('ref_po_phone', $get(null, '41'), $post_id);
|
| update_field('ref_po_email', $get(null, '42'), $post_id);
|
|
|
|
|
| update_field('ref_mh_prefix', $get(null, '37.2'), $post_id);
|
| update_field('ref_mh_first', $get(null, '37.3'), $post_id);
|
| update_field('ref_mh_middle', $get(null, '37.4'), $post_id);
|
| update_field('ref_mh_last', $get(null, '37.6'), $post_id);
|
| update_field('ref_mh_suffix', $get(null, '37.8'), $post_id);
|
| update_field('ref_mh_phone', $get(null, '43'), $post_id);
|
| update_field('ref_mh_email', $get(null, '45'), $post_id);
|
|
|
|
|
| update_field('ref_ss_prefix', $get('33.2', '33.2'), $post_id);
|
| update_field('ref_ss_first', $get('33.3', '33.3'), $post_id);
|
| update_field('ref_ss_middle', $get(null, '33.4'), $post_id);
|
| update_field('ref_ss_last', $get('33.6', '33.6'), $post_id);
|
| update_field('ref_ss_suffix', $get(null, '33.8'), $post_id);
|
| update_field('ref_ss_phone', $get(null, '46'), $post_id);
|
| update_field('ref_ss_email', $get(null, '47'), $post_id);
|
|
|
|
|
| update_field('ref_clinical_inventory', $get(null, '121'), $post_id);
|
| update_field('ref_other_behavioral', $get(null, '15'), $post_id);
|
| update_field('ref_school_characteristics', $get(null, '53'), $post_id);
|
| update_field('ref_school_other', $get(null, '145'), $post_id);
|
| update_field('ref_peer_characteristics', $get(null, '54'), $post_id);
|
| update_field('ref_peer_other', $get(null, '146'), $post_id);
|
| update_field('ref_behaviors_summary', $get(null, '52'), $post_id);
|
|
|
|
|
| update_field('ref_desired_outcomes', $get(null, '55'), $post_id);
|
| update_field('ref_other_outcomes', $get(null, '19'), $post_id);
|
|
|
|
|
| update_field('ref_household_summary', $get(null, '38'), $post_id);
|
|
|
|
|
| update_field('ref_docs_submitted', $get(null, '56'), $post_id);
|
|
|
|
|
|
|
| update_field('case_status', 'assigned', $post_id);
|
| update_field('treatment_phase', 'engagement', $post_id);
|
| update_field('week_number', 0, $post_id);
|
|
|
| error_log("MST Activation: Populated ACF fields for post {$post_id}");
|
|
|
|
|
|
|
| $update_153 = GFAPI::update_entry_field($source_entry_id, '153', 'case_activated');
|
| $update_156 = GFAPI::update_entry_field($source_entry_id, '156', $post_id);
|
|
|
| if (is_wp_error($update_153)) {
|
| error_log('MST Activation WARNING: Failed to update Form 223 Field 153 - ' . $update_153->get_error_message());
|
| }
|
| if (is_wp_error($update_156)) {
|
| error_log('MST Activation WARNING: Failed to update Form 223 Field 156 - ' . $update_156->get_error_message());
|
| }
|
|
|
|
|
| error_log("MST Activation COMPLETE: Post {$post_id} created, Form 223 Entry {$source_entry_id} marked activated");
|
|
|
|
|
| return $post_id;
|
| }
|
|
|
|
|
|
|
|
|
|
|
| add_action('mst_test_activation', 'mst_activate_case', 10, 2);
|
| |
| |
Comments