/**
  *     handle form add/update agency
  *     @param  integer $id 
  */
 function postUpdate($id = 0)
 {
     // check validate
     $validate = Validator::make(Input::all(), $this->model->getUpdateRules(), $this->model->getUpdateLangs());
     if ($validate->passes()) {
         $campaignName = Input::get('campaign');
         $adFormatID = Input::get('ad_format_id');
         $adFormatName = AdFormatBaseModel::find($adFormatID)->name;
         $updateData = array('campaign_id' => Input::get('campaign_id'), 'ad_format_id' => $adFormatID, 'ad_type' => Input::get('ad_type'), 'width' => Input::get('width'), 'height' => Input::get('height'), 'source_url' => Input::get('source_url'), 'destination_url' => Input::get('destination_url'), 'flash_wmode' => Input::get('flash_wmode'), 'video_duration' => Input::get('video_duration'), 'video_linear' => Input::get('video_linear'), 'video_type_vast' => Input::get('video_type_vast'), 'video_wrapper_tag' => Input::get('video_wrapper_tag'), 'video_bitrate' => Input::get('video_bitrate'), 'video_impression_track' => Input::get('video_impression_track'), 'updated_by' => $this->user->id);
         if (!empty($campaignName) || !empty($adFormatName)) {
             $updateData['name'] = $campaignName . ' ' . $adFormatName;
         }
         if ($id == 0) {
             // INSERT
             $updateData['created_by'] = $this->user->id;
             if ($item = $this->model->create($updateData)) {
                 $this->data['id'] = $item->id;
                 return TRUE;
             }
         } else {
             // UPDATE
             // GET CURRENT ITEM
             $item = $this->model->with('flight')->find($id);
             if ($item) {
                 if ($this->model->where("id", $id)->update($updateData)) {
                     return TRUE;
                 }
             }
         }
     } else {
         $this->data['errors'] = $validate->messages();
     }
     return FALSE;
 }
 /**
  *     handle form add/update agency
  *     @param  integer $id 
  */
 function postUpdate($id = 0)
 {
     // check validate
     $validate = Validator::make(Input::all(), $this->model->getUpdateRules(), $this->model->getUpdateLangs());
     if ($validate->passes()) {
         $campaignName = Input::get('campaign');
         $adFormatID = Input::get('ad_format_id');
         $adFormat = AdFormatBaseModel::find($adFormatID);
         if ('HTML' == $adFormat->name && 'video' == Input::get('ad_type')) {
             $campaign_id = Input::get('campaign_id');
             $name = Input::get('name');
             if (!isset($_FILES['vast_file']) || empty($_FILES['vast_file']['name'])) {
                 if (!empty($campaign_id) && !empty($name)) {
                     $file_name = str_replace(' ', '_', strtolower($name)) . '.xml';
                     $url = "http://static.yomedia.vn/xml/{$campaign_id}/{$file_name}";
                     $file_headers = @get_headers($url);
                     if ($file_headers[0] == 'HTTP/1.1 404 Not Found') {
                         Session::flash('flash-message', "Vast file is required");
                         return FALSE;
                     }
                 }
             } else {
                 $file = $_FILES['vast_file'];
                 if (isset($file['name']) && $file['name'] != "") {
                     $ftp = new MyFTP();
                     $type = substr($file['name'], strrpos($file['name'], '.'), strlen($file['name']));
                     if ('.xml' != $type) {
                         Session::flash('flash-message', "Extension of vast file error");
                         return FALSE;
                     }
                     $file_name = str_replace(' ', '_', strtolower(Input::get('name'))) . '.xml';
                     $dirUpload = "xml/{$campaign_id}/";
                     if (!$ftp->uploadFtp($dirUpload, $file['tmp_name'], $file_name)) {
                         Session::flash('flash-message', "Upload file error");
                         return FALSE;
                     }
                 }
             }
         }
         $updateData = array('campaign_id' => Input::get('campaign_id'), 'name' => Input::get('name'), 'ad_format_id' => $adFormatID, 'ad_type' => Input::get('ad_type'), 'width' => Input::get('width'), 'height' => Input::get('height'), 'width_2' => Input::get('width_2'), 'height_2' => Input::get('height_2'), 'width_after' => Input::get('width_after'), 'height_after' => Input::get('height_after'), 'source_url' => Input::get('source_url'), 'source_url2' => Input::get('source_url2'), 'destination_url' => Input::get('destination_url'), 'flash_wmode' => Input::get('flash_wmode', 'none'), 'video_duration' => Input::get('video_duration'), 'video_linear' => Input::get('video_linear'), 'video_type_vast' => Input::get('video_type_vast'), 'video_wrapper_tag' => Input::get('video_wrapper_tag'), 'third_impression_track' => Input::get('third_impression_track'), 'third_click_track' => Input::get('third_click_track'), 'video_bitrate' => Input::get('video_bitrate'), 'third_party_tracking' => Input::get('tracking') == "" ? "" : json_encode(Input::get('tracking')), 'main_source' => Input::get('main_source'), 'updated_by' => $this->user->id, 'platform' => json_encode(Input::get('platform')), 'ad_view_type' => Input::get('ad_view_type'), 'source_url_backup' => Input::get('source_url_backup'), 'html_source' => Input::get('html_source'), 'skipads' => Input::get('skipads'), 'display_type' => Input::get('display_type'), 'bar_height' => Input::get('bar_height'), 'vast_include' => Input::get('vast_include', 0), 'audience_id' => Input::get('audience_id', 0), 'position' => Input::get('position'));
         if (!$updateData['ad_view_type']) {
             if ($adFormat->ad_view) {
                 $updateData['ad_view'] = $adFormat->ad_view;
             } else {
                 $updateData['ad_view'] = '';
             }
         } else {
             $updateData['ad_view'] = Input::get('ad_view');
         }
         // Upload FILE FTP
         if (isset($_FILES['file_source_url'])) {
             $file = $_FILES['file_source_url'];
             if (isset($file['name']) && $file['name'] != "") {
                 $ftp = new MyFTP();
                 $file_name = time() . $file['name'];
                 if (!$ftp->uploadFtp("", $file['tmp_name'], $file_name)) {
                     Session::flash('flash-message', "Extension file error");
                     return FALSE;
                 }
                 $updateData['source_url'] = STATIC_URL . date("Y") . "/" . date("m") . "/" . $file_name;
             }
         }
         // Upload FILE FTP
         if (isset($_FILES['file_source_url_2'])) {
             $file = $_FILES['file_source_url_2'];
             if (isset($file['name']) && $file['name'] != "") {
                 $ftp = new MyFTP();
                 $type = substr($file['name'], strrpos($file['name'], '.'), strlen($file['name']));
                 $file_name = time() . $file['name'];
                 $ftp->uploadFtp("", $file['tmp_name'], $file_name);
                 if (!$ftp->uploadFtp("", $file['tmp_name'], $file_name)) {
                     Session::flash('flash-message', "Extension file error");
                     return FALSE;
                 }
                 $updateData['source_url2'] = STATIC_URL . date("Y") . "/" . date("m") . "/" . $file_name;
             }
         }
         // Upload FILE BACKUP
         if (isset($_FILES['file_source_backup_url'])) {
             $file = $_FILES['file_source_backup_url'];
             if (isset($file['name']) && $file['name'] != "") {
                 $ftp = new MyFTP();
                 $type = substr($file['name'], strrpos($file['name'], '.'), strlen($file['name']));
                 $file_name = time() . $file['name'];
                 if (!$ftp->uploadFtp("", $file['tmp_name'], $file_name)) {
                     Session::flash('flash-message', "Extension file error");
                     return FALSE;
                 }
                 $updateData['source_url_backup'] = STATIC_URL . date("Y") . "/" . date("m") . "/" . $file_name;
             }
         }
         $mime = '';
         if ($updateData['ad_type'] == 'html') {
             $mime = 'text/html';
         } else {
             if (!empty($updateData['source_url'])) {
                 $ch = curl_init($updateData['source_url']);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                 curl_exec($ch);
                 $mime = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
             }
         }
         $updateData['mime'] = $mime;
         // if (!empty($campaignName) || !empty($adFormatName)) {
         //     $updateData['name'] = $campaignName . ' ' . $adFormatName;
         // }
         if ($id == 0) {
             // INSERT
             $updateData['created_by'] = $this->user->id;
             if ($item = $this->model->create($updateData)) {
                 $this->data['id'] = $item->id;
                 (new Delivery())->renewCache('ad', $item->id);
                 Session::flash('flash-message', 'Create Ad Success!');
                 return TRUE;
             }
         } else {
             // UPDATE
             // GET CURRENT ITEM
             $item = $this->model->find($id);
             if ($item) {
                 if ($this->model->where("id", $id)->update($updateData)) {
                     //update audience id
                     $this->audience->updateCampaign($id, Input::get('campaign_id'));
                     $flights = FlightBaseModel::where('ad_id', $item->id)->get();
                     if ($flights) {
                         foreach ($flights as $flight) {
                             (new Delivery())->removeCache('flight', $flight->id);
                             $flight->ad_format_id = $updateData['ad_format_id'];
                             if ($flight->save()) {
                                 (new Delivery())->renewCache('flight', $flight->id);
                             }
                         }
                     }
                     (new Delivery())->renewCache('ad', $id);
                     Session::flash('flash-message', 'Update Ad Success!');
                     return TRUE;
                 }
             }
         }
     } else {
         $this->data['errors'] = $validate->messages();
     }
     return FALSE;
 }