Example #1
0
 public function updateResultByOrder($attr, $urls)
 {
     $check = ResultMedlatec::model()->findByAttributes(array('order_id' => $attr['order_id']));
     if ($check) {
         $check->setAttributes($attr);
         $check->updated_at = time();
         $check->save(FALSE);
         $order = OrderMedlatec::model()->findByPk($attr['order_id']);
         $order->status = 4;
         $order->save(FALSE);
         $meboo = $order->user_meboo;
         $device_tokens = DeviceTk::model()->findAllByAttributes(array('user_id' => $meboo));
         $message_android = array('medlatec_order' => array('order_id' => $attr['order_id']));
         $message_ios = array('alert' => 'Dịch vụ của bạn vừa có thông tin', 'sound' => 'default', 'data' => array('id' => $attr['order_id'], 'type' => '0'));
         $message = array('message_android' => $message_android, 'message_ios' => $message_ios);
         foreach ($device_tokens as $token) {
             //  echo $token->platform;
             Util::sendNotificationBasedOnStatus($token->device_token, $order->status, $message);
         }
         //die;
         if (!empty($urls) && is_array($urls)) {
             $files = ResultFile::model()->findAllByAttributes(array('result_id' => $check->id));
             if ($files) {
                 foreach ($files as $file) {
                     $file->delete();
                 }
             }
             foreach ($urls as $url) {
                 $file = new ResultFile();
                 $file->url = $url;
                 $file->updated_at = time();
                 $file->created_at = time();
                 $file->result_id = $check->id;
                 $file->save(FALSE);
             }
         }
         return TRUE;
     } else {
         $model = new ResultMedlatec();
         $model->setAttributes($attr);
         $model->order_id = $attr['order_id'];
         $model->created_at = time();
         $model->updated_at = time();
         $model->save(FALSE);
         $order = OrderMedlatec::model()->findByPk($attr['order_id']);
         $order->status = 4;
         $order->save(FALSE);
         $meboo = $order->user_meboo;
         $meboo_token = User::model()->findByPk($meboo);
         if ($meboo_token) {
             $message = array('medlatec_order' => array('order_id' => $attr['order_id']));
             Util::sendNotificationBasedOnStatus($meboo_token->device_token, $order->status, $message);
         }
         if (!empty($urls) && is_array($urls)) {
             foreach ($urls as $url) {
                 $file = new ResultFile();
                 $file->url = $url;
                 $file->updated_at = time();
                 $file->created_at = time();
                 $file->result_id = $model->id;
                 $file->save(FALSE);
             }
         }
         return TRUE;
     }
 }