static function processGCM($arrhasil, $app_id, $psn, $array_ids, $app, $isTest = 0)
 {
     $json['status_code'] = 1;
     $json['status_message'] = "Success";
     $json['result'] = $arrhasil;
     foreach ($arrhasil as $numw => $hasil) {
         //simpan hasil
         $gcm = new GCMResult();
         $gcm->multicast_id = $hasil->multicast_id;
         $gcm->success = $hasil->success;
         $gcm->failure = $hasil->failure;
         $gcm->results = serialize($hasil->results);
         $gcm->canonical_ids = $hasil->canonical_ids;
         $gcm->camp_id = $psn->camp_id;
         $gcm->gcm_date = leap_mysqldate();
         $gcm->gcm_test = $isTest;
         $gcm->app_id = $app_id;
         $gcm->client_camp_id = $psn->camp_client_id;
         $gcm->client_id = $app->app_client_id;
         $gcm->save();
         if (!$isTest) {
             //kurangin pulsa ...
             $old = $app->app_pulsa;
             $app->app_pulsa = $app->app_pulsa - $hasil->success;
             $app->load = 1;
             $app->save();
             //save transactions
             $tt = new AppPulsa();
             $tt->pulsa_acc_id = Account::getMyID();
             $tt->pulsa_action = "debit";
             $tt->pulsa_app_id = $app_id;
             $tt->pulsa_date = leap_mysqldate();
             $tt->pulsa_jumlah = $hasil->success;
             $tt->pulsa_new = $app->app_pulsa;
             $tt->pulsa_old = $old;
             $tt->pulsa_camp_id = $psn->camp_id;
             $tt->save();
             //
         }
         //            echo "ID : ".$hasil->multicast_id."<br>";
         //            echo "Success : ".$hasil->success."<br>";
         //            echo "Failure : ".$hasil->failure."<br>";
         //            echo "<a target='_blank' href='"._SPPATH."PushNotResults/res?id={$psn->camp_id}&token=".IMBAuth::createOAuth()."' class='btn btn primary'>Complete Results</a><br><br>";
         $page = 999 * $numw;
         foreach ($hasil->results as $num => $res) {
             if (isset($res->error)) {
                 //error
                 $status = 0;
                 $log_text = $res->error;
                 //delete device_id from table
                 $dv = new DeviceModel();
                 global $db;
                 //repaired using update
                 $q = "UPDATE  {$dv->table_name} SET dev_not_send = 1 WHERE device_id = '" . $array_ids[$page + $num] . "'";
                 if ($_GET['test']) {
                     echo "<br>query : " . $q . "<br>";
                     echo "delete succ :" . $db->query($q, 0);
                     echo "<br>";
                 }
             } else {
                 //success
                 $status = 1;
                 $log_text = $res->message_id;
             }
             // repaired macc id cannot get
             PushLogger::savelog($psn->camp_id, $array_ids[$num], $app->app_client_id, $status, $log_text, $hasil->multicast_id, $app_id, $psn->camp_client_id);
         }
     }
     $psn->camp_status = 1;
     $psn->camp_send_date = leap_mysqldate();
     $psn->load = 1;
     $psn->save();
     return $json;
 }
 static function simpanHasilGCM($arrhasil, $psn, $array_id, $arrDevs, $app, $isTest = 0)
 {
     if ($_GET['test']) {
         pr($arrhasil);
     }
     if ($_GET['test']) {
         echo "<h1>Results</h1>";
     }
     foreach ($arrhasil as $numw => $hasil) {
         //simpan hasil
         $gcm = new GCMResultCaps();
         $gcm->multicast_id = $hasil->multicast_id;
         $gcm->success = $hasil->success;
         $gcm->failure = $hasil->failure;
         $gcm->results = serialize($hasil->results);
         $gcm->canonical_ids = $hasil->canonical_ids;
         $gcm->camp_id = $psn->camp_id;
         $gcm->gcm_date = leap_mysqldate();
         $gcm->gcm_test = $isTest;
         $gcm->save();
         if ($_GET['test']) {
             echo "ID : " . $hasil->multicast_id . "<br>";
             echo "Success : " . $hasil->success . "<br>";
             echo "Failure : " . $hasil->failure . "<br>";
             echo "<a target='_blank' href='" . _SPPATH . "PushNotResults/res?id={$psn->camp_id}&token=" . IMBAuth::createOAuth() . "' class='btn btn primary'>Complete Results</a><br><br>";
         }
         $page = 999 * $numw;
         foreach ($hasil->results as $num => $res) {
             if (isset($res->error)) {
                 //error
                 $status = 0;
                 $log_text = $res->error;
                 //delete device_id from table
                 $dv = new DeviceModel();
                 global $db;
                 //                    $q = "DELETE FROM {$dv->table_name} WHERE device_id = '".$array_id[$page+$num]."'";
                 $q = "UPDATE {$dv->table_name} SET dev_not_send = 1 WHERE device_id = '" . $array_id[$page + $num] . "'";
                 if ($_GET['test']) {
                     echo "<br>query : " . $q . "<br>";
                     echo "delete succ :" . $db->query($q, 0);
                     echo "<br>";
                 }
             } else {
                 //success
                 $status = 1;
                 $log_text = $res->message_id;
             }
             PushLogger::savelog($psn->camp_id, $array_id[$page + $num], $arrDevs[$page + $num]->acc_id, $status, $log_text, $hasil->multicast_id, $app->app_id, $app->app_client_id);
             //                PushLogger::savelog($psn->camp_id,$array_id[$page+$num],$arrDevs[$page+$num]->acc_id,$status,$log_text,$hasil->multicast_id);
         }
     }
 }
 static function sendUsingAccountArray($arrAcc, $psn, $isTest = 0)
 {
     if ($_GET['test']) {
         pr($arrAcc);
     }
     if ($_GET['test']) {
         echo "<h1>Push Complete Results</h1>";
     }
     foreach ($arrAcc as $to) {
         $dev = new DeviceModel();
         //            $arrD = $dev->getWhere("acc_id = '$to' AND device_type = 'android' ORDER BY logindate DESC");
         //kalau dijaddin ga aktif 16 dec 2015
         //            $arrD = $dev->getWhere("acc_id = '$to' ORDER BY logindate DESC");
         $arrD = $dev->getWhere("acc_id = '{$to}' AND dev_active = 1  ORDER BY logindate DESC");
         //            $json['arrD'] = $arrD;
         //            foreach ($arrD as $d) {
         //                $json['dev'] = $d->device_id;
         ////                if($d->device_id == "")continue;
         //                $hasil = self::push($d->device_id, $msg, $website);
         //                $json['hasil'] = $hasil;
         //                if ($hasil->success > 0) {
         //                    $json['bool'] = 1;
         ////                    $json['hasil'] = $hasil;
         //                }
         //            }
         if (count($arrD) > 0) {
             foreach ($arrD as $ddd) {
                 $devices[] = $ddd;
             }
         } else {
             if ($_GET['test']) {
                 echo "no device id : " . $to . " <br>";
             }
         }
     }
     $action = self::getAction($psn);
     if ($_GET['test']) {
         echo "action = " . $action . "<br>";
     }
     $array_id = array();
     if ($_GET['test']) {
         echo "<h3>Devices</h3>";
     }
     foreach ($devices as $dev) {
         if ($_GET['test']) {
             echo " acc_id : " . $dev->acc_id;
             echo "<br> dev_id : " . $dev->device_id;
             echo "<br> type : " . $dev->device_type;
             echo "<br>";
         }
         $array_id[] = $dev->device_id;
     }
     $url2push = Efiwebsetting::getData('PUBLIC_IP') . "WebViewer/messages/" . $psn->camp_id;
     //penambahan atas permintaan tbs 10 sept 2015, bisa push url
     if ($psn->camp_url != "") {
         $url2push = $psn->camp_url;
     }
     /*
      *
      *  PILIHAN antara server luar dan dalam
      *  27 Nov 2015
      *
      */
     $pss = new Pusher();
     if ($pss->pakai_server_luar == '1') {
         $app_id = 1;
         $app_key = "1qay2wsx";
         $push_api_url = "http://push.indomegabyte.com/PushAPI/doPushByDevID";
         $devs = implode(",", $array_id);
         $fields = array('devs' => $devs, 'app_id' => $app_id, 'key' => $app_key, 'camp_id' => $psn->camp_id, 'camp_name' => $psn->camp_name, 'camp_title' => $psn->camp_title, 'camp_url' => $url2push, 'isTest' => $isTest);
         $headers = array('Content-Type: application/json');
         //            pr($fields);
         //"postvar1=value1&postvar2=value2&postvar3=value3"
         foreach ($fields as $k => $val) {
             $jadi[] = $k . "=" . $val;
         }
         $imp = implode("&", $jadi);
         //            echo http_build_query($fields);
         //            $ch = curl_init();
         //            curl_setopt($ch, CURLOPT_URL, $push_api_url);
         //            curl_setopt($ch, CURLOPT_POST, true);
         //            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
         //            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         //            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
         //            curl_setopt($ch, CURLOPT_POSTFIELDS, $imp);
         //            $result = json_decode(curl_exec($ch));
         //            curl_close($ch);
         //
         //            pr($result);
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $push_api_url);
         curl_setopt($ch, CURLOPT_POST, 1);
         curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
         // in real life you should use something like:
         // curl_setopt($ch, CURLOPT_POSTFIELDS,
         //          http_build_query(array('postvar1' => 'value1')));
         // receive server response ...
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         $server_output = curl_exec($ch);
         curl_close($ch);
         //            pr($server_output);
         $ress = json_decode($server_output);
         if (!isset($_POST['no_echo'])) {
             echo "<h1>" . $ress->status_message . " Status Code [" . $ress->status_code . "]</h1>";
         }
         $arrhasil = $ress->result;
         //            pr($arrhasil);
     } elseif ($pss->pakai_server_luar == '2') {
         //create temporary files to read from
         $myfile = fopen(_PHOTOPATH . "campaign.txt", "w") or die("Unable to create file!");
         $txt = implode(",", $array_id);
         if (fwrite($myfile, $txt)) {
             fclose($myfile);
             $app_id = 1;
             $app_key = "1qay2wsx";
             $push_api_url = "http://push.indomegabyte.com/PushAPI/doPushByDevIDwithFile";
             //                $push_api_url = "http://localhost:8888/push/PushAPI/doPushByDevIDwithFile";
             $devs = Efiwebsetting::getData('PUBLIC_IP') . "uploads/campaign.txt";
             //                pr($devs);
             //                pr($push_api_url);
             $fields = array('devs' => $devs, 'app_id' => $app_id, 'key' => $app_key, 'camp_id' => $psn->camp_id, 'camp_name' => $psn->camp_name, 'camp_title' => $psn->camp_title, 'camp_url' => $url2push, 'isTest' => $isTest);
             $headers = array('Content-Type: application/json');
             $ch = curl_init();
             curl_setopt($ch, CURLOPT_URL, $push_api_url);
             curl_setopt($ch, CURLOPT_POST, 1);
             curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
             $server_output = curl_exec($ch);
             curl_close($ch);
             $ress = json_decode($server_output);
             if (!isset($_POST['no_echo'])) {
                 echo "<h1>" . $ress->status_message . " Status Code [" . $ress->status_code . "]</h1>";
             }
             $arrhasil = $ress->result;
             //                pr($fields);
             //                pr($arrhasil);
             //                pr($server_output);
         } else {
             die("Unable to write");
         }
     } else {
         //after we collected the device ids in an array, kita push
         $arrhasil = self::push($array_id, $psn->camp_title, $url2push, $action);
     }
     if ($_GET['test']) {
         pr($arrhasil);
     }
     if (!isset($_POST['no_echo'])) {
         echo "<h1>Results</h1>";
     }
     foreach ($arrhasil as $numw => $hasil) {
         //simpan hasil
         $gcm = new GCMResult();
         $gcm->multicast_id = $hasil->multicast_id;
         $gcm->success = $hasil->success;
         $gcm->failure = $hasil->failure;
         $gcm->results = serialize($hasil->results);
         $gcm->canonical_ids = $hasil->canonical_ids;
         $gcm->camp_id = $psn->camp_id;
         $gcm->gcm_date = leap_mysqldate();
         $gcm->gcm_test = $isTest;
         $gcm->save();
         if (!isset($_POST['no_echo'])) {
             echo "ID : " . $hasil->multicast_id . "<br>";
             echo "Success : " . $hasil->success . "<br>";
             echo "Failure : " . $hasil->failure . "<br>";
             echo "<a target='_blank' href='" . _SPPATH . "PushNotResults/res?id={$psn->camp_id}&token=" . IMBAuth::createOAuth() . "' class='btn btn primary'>Complete Results</a><br><br>";
         }
         $page = 999 * $numw;
         foreach ($hasil->results as $num => $res) {
             if (isset($res->error)) {
                 //error
                 $status = 0;
                 $log_text = $res->error;
                 //delete device_id from table
                 $dv = new DeviceModel();
                 global $db;
                 $q = "DELETE FROM {$dv->table_name} WHERE device_id = '" . $array_id[$page + $num] . "'";
                 $q = "UPDATE {$dv->table_name} SET dev_active = 0 WHERE device_id = '" . $array_id[$page + $num] . "'";
                 //coba test..
                 //                        echo $q;
                 $del = $db->query($q, 0);
                 if ($_GET['test']) {
                     echo "<br>query : " . $q . "<br>";
                     echo "delete succ :" . $del;
                     echo "<br>";
                 }
             } else {
                 //success
                 $status = 1;
                 $log_text = $res->message_id;
             }
             PushLogger::savelog($psn->camp_id, $array_id[$num], $devices[$num]->acc_id, $status, $log_text, $hasil->multicast_id);
             //disini save camp_id ke device_id dan date
             //pakai PushLogger Bisa !!! 11 January 2016
         }
     }
     //        foreach($hasil->results as $
     //        foreach($devices as $dev) {
     //            PushLogger::savelog($psn->camp_id,)
     //        }
     //        echo json_encode($json);
     //        die();
 }
 static function sendUsingAccountArray($arrAcc, $psn, $isTest = 0)
 {
     if ($_GET['test']) {
         pr($arrAcc);
     }
     if ($_GET['test']) {
         echo "<h1>Push Complete Results</h1>";
     }
     foreach ($arrAcc as $to) {
         $dev = new DeviceModel();
         //            $arrD = $dev->getWhere("acc_id = '$to' AND device_type = 'android' ORDER BY logindate DESC");
         $arrD = $dev->getWhere("acc_id = '{$to}' ORDER BY logindate DESC");
         //            $json['arrD'] = $arrD;
         //            foreach ($arrD as $d) {
         //                $json['dev'] = $d->device_id;
         ////                if($d->device_id == "")continue;
         //                $hasil = self::push($d->device_id, $msg, $website);
         //                $json['hasil'] = $hasil;
         //                if ($hasil->success > 0) {
         //                    $json['bool'] = 1;
         ////                    $json['hasil'] = $hasil;
         //                }
         //            }
         if (count($arrD) > 0) {
             foreach ($arrD as $ddd) {
                 $devices[] = $ddd;
             }
         } else {
             if ($_GET['test']) {
                 echo "no device id : " . $to . " <br>";
             }
         }
     }
     $action = self::getAction($psn);
     if ($_GET['test']) {
         echo "action = " . $action . "<br>";
     }
     $array_id = array();
     if ($_GET['test']) {
         echo "<h3>Devices</h3>";
     }
     foreach ($devices as $dev) {
         if ($_GET['test']) {
             echo " acc_id : " . $dev->acc_id;
             echo "<br> dev_id : " . $dev->device_id;
             echo "<br> type : " . $dev->device_type;
             echo "<br>";
         }
         $array_id[] = $dev->device_id;
     }
     $url2push = Efiwebsetting::getData('PUBLIC_IP') . "WebViewer/messages/" . $psn->camp_id;
     //penambahan atas permintaan tbs 10 sept 2015, bisa push url
     if ($psn->camp_url != "") {
         $url2push = $psn->camp_url;
     }
     //after we collected the device ids in an array, kita push
     $arrhasil = self::push($array_id, $psn->camp_title, $url2push, $action);
     if ($_GET['test']) {
         pr($arrhasil);
     }
     echo "<h1>Results</h1>";
     foreach ($arrhasil as $numw => $hasil) {
         //simpan hasil
         $gcm = new GCMResult();
         $gcm->multicast_id = $hasil->multicast_id;
         $gcm->success = $hasil->success;
         $gcm->failure = $hasil->failure;
         $gcm->results = serialize($hasil->results);
         $gcm->canonical_ids = $hasil->canonical_ids;
         $gcm->camp_id = $psn->camp_id;
         $gcm->gcm_date = leap_mysqldate();
         $gcm->gcm_test = $isTest;
         $gcm->save();
         echo "ID : " . $hasil->multicast_id . "<br>";
         echo "Success : " . $hasil->success . "<br>";
         echo "Failure : " . $hasil->failure . "<br>";
         echo "<a target='_blank' href='" . _SPPATH . "PushNotResults/res?id={$psn->camp_id}&token=" . IMBAuth::createOAuth() . "' class='btn btn primary'>Complete Results</a><br><br>";
         $page = 999 * $numw;
         foreach ($hasil->results as $num => $res) {
             if (isset($res->error)) {
                 //error
                 $status = 0;
                 $log_text = $res->error;
                 //delete device_id from table
                 $dv = new DeviceModel();
                 global $db;
                 $q = "DELETE FROM {$dv->table_name} WHERE device_id = '" . $array_id[$page + $num] . "'";
                 if ($_GET['test']) {
                     echo "<br>query : " . $q . "<br>";
                     echo "delete succ :" . $db->query($q, 0);
                     echo "<br>";
                 }
             } else {
                 //success
                 $status = 1;
                 $log_text = $res->message_id;
             }
             PushLogger::savelog($psn->camp_id, $array_id[$num], $devices[$num]->acc_id, $status, $log_text, $hasil->multicast_id);
         }
     }
     //        foreach($hasil->results as $
     //        foreach($devices as $dev) {
     //            PushLogger::savelog($psn->camp_id,)
     //        }
     //        echo json_encode($json);
     //        die();
 }