Best practice to send data to an external site and displaying the return in sugar as an alert

Hi all,

I have a custom module of printers, when certain fields are updated in the record I want to send that record data to an external laravel site to update its database.

I currently use logic hooks to do this

    $hook_array['after_save'] = Array(); 
    $hook_array['after_save'][] = Array(
        1,
        'Update Serial Cache',
        'custom/modules/S_SerialNumber/update_serial_cache.php',
        'update_serial_cache',
        'update_to_cache',
    );

    $hook_array['before_save'] = Array(); 
    $hook_array['before_save'][] = Array(
        1,
        'Update Serial Cache',
        'custom/modules/S_SerialNumber/update_serial_cache.php',
        'update_serial_cache',
        'get_record',
    );

 the update_to_cache function checks if the fields I want to check have changed and if they have pushes that data to our laravel site over curl to a custom endpoint. The problem is Id like to inform the user with an alert box that the cache was updated or display the error returned from laravel

is it possible to do this in sidecar and have a custome script  running an ajax reguest to the laravel server then using the alerts  to display the return?

I cant use the built in web_logic_hooks as I only want this to happen if certain fields are updated. 

this is my logic hooks custom class

   class update_serial_cache
    {
        public function get_record($bean, $event, $arguments)
        {
            $bean->stored_fetched_row_c = $bean->fetched_row;
            // sugar_die('just a test sugar_die.');
        }

        public function update_to_cache($bean, $event, $arguments)
        {
            if (!$bean->cached_c) {
                $GLOBALS['log']->warn('Not cached: exiting');
                return false;
            }

            $update = false;            
            if (isset($bean->stored_fetched_row_c)) {
                $GLOBALS['log']->warn('OLD: '.json_encode($bean->stored_fetched_row_c));
                // Warranty change 
                if ($bean->stored_fetched_row_c['warrantystart_c'] != $bean->warrantystart_c) { 
                    $GLOBALS['log']->warn('New: '.$bean->warrantystart_c);
                    $update = true;
                }
                if ($bean->stored_fetched_row_c['warrantyend_c'] != $bean->warrantyend_c) {
                    $GLOBALS['log']->warn('New: '.$bean->warrantyend_c);
                    $update = true;
                }
                if ($bean->stored_fetched_row_c['warrantystatus_c'] != $bean->warrantystatus_c) {
                    $GLOBALS['log']->warn('New: '.$bean->warrantystatus_c);
                    $update = true;
                }                
                if ($bean->stored_fetched_row_c['status'] != $bean->status) {
                    $GLOBALS['log']->warn('New: '.$bean->status);
                    $update = true;
                }
                // Dealer change
                if ($bean->stored_fetched_row_c['dealer_id_c'] != $bean->dealer_id_c) {
                    $GLOBALS['log']->warn('New: '.$bean->dealer_id_c);
                    $update = true;
                }
                if ($bean->stored_fetched_row_c['dealersoldto'] != $bean->dealersoldto) {
                    $GLOBALS['log']->warn('New: '.$bean->dealersoldto);
                    $update = true;
                }
                // Product change
                if ($bean->stored_fetched_row_c['product_code_related_c'] != $bean->product_code_related_c) {
                    $GLOBALS['log']->warn('New: '.$bean->p_product_s_serialnumber_1_name);
                    $update = true;
                }

                // Update Cache
                if ($update) {
                    $args = array(
                        'Serial_Number' => MC_ContactsApi::obfuscate('encrypt', $bean->name),
                    );
                    $printer    = MC_SerialNumberApi::serial_number_check(null, $args);
                    $GLOBALS['log']->warn('NEW: '.json_encode($printer));

                    // CURL
                    $curl       = curl_init("https://services.magicard.com/hs/public/serial-cache");
                    curl_setopt($curl, CURLOPT_HEADER, false);
                    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($curl, CURLOPT_POST, true);
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $printer['records']);
            
                    $response   = curl_exec($curl);
                    $err        = curl_error($curl);
                    curl_close($curl);
            
                    if ($err) {
                        $GLOBALS['log']->warn("cURL Error #:" . $err);
                        return false;
                    }
            
                    $GLOBALS['log']->warn('RESP: '.$response);
                    return true;
                }
                return false;
            }
        }

i used the log  just to check the hook was firing

Parents Reply Children
No Data