Home / Admin / MST Case Container Initiate Case
Duplicate Snippet

Embed Snippet on Your Site

MST Case Container Initiate Case

* Called by Uncanny Automator via "Call a custom function/method" action
* When Form 269 (MST Case Activation) is submitted

ismail daugherty PRO
<10
Code Preview
php
<?php
<?php
/**
 * MST Case Activation Handler v1.0.0
 * 
 * Called by Uncanny Automator via "Call a custom function/method" action
 * When Form 269 (MST Case Activation) is submitted
 * 
 * Function: mst_activate_case
 * Arguments: 
 *   $entry_id (int) - Form 269 entry ID
 *   $user_id (int)  - Submitting user ID
 * 
 * Actions performed:
 *   1. Create mst_case post with therapist as author
 *   2. Populate 86+ ACF fields from Form 269/223 data
 *   3. Update Form 223 entry (Field 153 = "case_activated", Field 156 = post ID)
 *   4. Trigger Recipe 11781 chain (via post publish)
 * 
 * @package MST_Case_Management
 * @version 1.0.0
 * @author Portal Blueprint
 */
// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}
/**
 * Main activation function - called by Uncanny Automator
 * 
 * @param int $entry_id Form 269 entry ID
 * @param int $user_id  User who submitted the form
 * @return int|false    Created post ID or false on failure
 */
function mst_activate_case($entry_id, $user_id) {
    
    // === VALIDATION ===
    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}");
    
    // === GET FORM 269 ENTRY ===
    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;
    }
    
    // Verify this is Form 269
    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;
    }
    
    // === GET SOURCE FORM 223 ENTRY ===
    $source_entry_id = intval(rgar($entry_269, '160')); // Field 160 = Select Pending Referral (entry ID)
    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');
    }
    
    // === GET KEY VALUES ===
    $therapist_id = intval(rgar($entry_269, '149')); // Field 149 = Assigned Therapist (user ID)
    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')); // Field 148 = MSTI Case Number
    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')); // Field 161 = Activation Date
    
    // === HELPER FUNCTION: Get value from 269 first, fallback to 223 ===
    $get = function($field_269, $field_223 = null) use ($entry_269, $entry_223, $has_223) {
        // Try Form 269 first
        if ($field_269 !== null) {
            $val = rgar($entry_269, (string)$field_269);
            if (!empty($val)) {
                return $val;
            }
        }
        // Fallback to Form 223
        if ($field_223 !== null && $has_223) {
            return rgar($entry_223, (string)$field_223);
        }
        return '';
    };
    
    // === BUILD POST TITLE ===
    $youth_first = $get('30.3', '30.3');
    $youth_last = $get('30.6', '30.6');
    $post_title = "{$youth_last}, {$youth_first} - MSTI-{$msti_number}";
    
    // === CREATE MST_CASE POST ===
    $post_data = array(
        'post_type'   => 'mst_case',
        'post_title'  => $post_title,
        'post_status' => 'publish',  // CRITICAL: Must be 'publish' to trigger Recipe 11781
        '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}");
    
    // === POPULATE ACF FIELDS ===
    // Using update_field() which is stable ACF API
    
    // --- CASE IDENTIFIERS ---
    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);
    
    // --- REFERRAL TRACKING ---
    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);
    
    // --- REFERRAL SOURCE ---
    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);
    
    // --- YOUTH DEMOGRAPHICS ---
    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);
    
    // Calculate age from DOB
    $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);
    
    // --- YOUTH CONTACT ---
    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);
    
    // --- GUARDIAN/CAREGIVER ---
    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);
    
    // --- LEGAL STATUS ---
    update_field('ref_legal_status', $get(null, '50'), $post_id);
    update_field('ref_key_participants', $get(null, '51'), $post_id);
    
    // --- PROBATION OFFICER ---
    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);
    
    // --- MENTAL HEALTH WORKER ---
    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);
    
    // --- SOCIAL SERVICES WORKER ---
    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);
    
    // --- CLINICAL/BEHAVIORAL ---
    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);
    
    // --- OUTCOMES ---
    update_field('ref_desired_outcomes', $get(null, '55'), $post_id);
    update_field('ref_other_outcomes', $get(null, '19'), $post_id);
    
    // --- HOUSEHOLD ---
    update_field('ref_household_summary', $get(null, '38'), $post_id);
    
    // --- DOCUMENTATION ---
    update_field('ref_docs_submitted', $get(null, '56'), $post_id);
    // Note: File uploads would need special handling if required
    
    // --- DEFAULT CASE VALUES ---
    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 FORM 223 ENTRY ===
    // Mark as activated and store the created 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());
    }
    
    // === COMPLETE ===
    error_log("MST Activation COMPLETE: Post {$post_id} created, Form 223 Entry {$source_entry_id} marked activated");
    
    // Return post ID (can be used as action token in Automator)
    return $post_id;
}
/**
 * Optional: Direct hook for testing without Automator
 * Can be triggered via: do_action('mst_test_activation', $entry_id, $user_id);
 */
add_action('mst_test_activation', 'mst_activate_case', 10, 2);

Comments

Add a Comment