/**
  *     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;
 }
Example #2
0
 function uploadImageFTP($name, $dirUpload, $imgName, $width = 0, $height = 0)
 {
     if (Input::hasFile($name)) {
         $input = Input::file($name);
         $fileExtension = strtolower($input->getClientOriginalExtension());
         $fileExtension = $fileExtension == 'jpeg' ? 'jpg' : $fileExtension;
         $realPath = $input->getRealPath();
         if ($width != 0 && $height != 0) {
             $uploadFile = Image::make($input)->resize($width, $height)->save($realPath);
         }
         $MyFTP = new MyFTP();
         $uploadSuccess = $MyFTP->uploadFtp($dirUpload, $realPath, $imgName . '.' . $fileExtension);
         if ($uploadSuccess) {
             return $imgName . '.' . $fileExtension;
         } else {
             return FALSE;
         }
     } else {
         return FALSE;
     }
 }
Example #3
0
 protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null, $content_range = null)
 {
     // $fileName = uniqueString().".".$fileExtention;
     $fileName = $this->get_file_name($uploaded_file, $name, $size, $type, $error, $index, $content_range);
     $fileExtention = explode_end($fileName, ".");
     $file = new \stdClass();
     $file->name = uniqueString() . "." . $fileExtention;
     $file->size = $this->fix_integer_overflow(intval($size));
     $file->type = $type;
     if ($this->validate($uploaded_file, $file, $error, $index)) {
         $this->handle_form_data($file, $index);
         $upload_dir = $this->get_upload_path();
         if (!is_dir($upload_dir)) {
             mkdir($upload_dir, $this->options['mkdir_mode'], true);
         }
         $file_path = $this->get_upload_path($file->name);
         $append_file = $content_range && is_file($file_path) && $file->size > $this->get_file_size($file_path);
         if ($uploaded_file && is_uploaded_file($uploaded_file)) {
             // multipart/formdata uploads (POST method uploads)
             if ($append_file) {
                 file_put_contents($file_path, fopen($uploaded_file, 'r'), FILE_APPEND);
             } else {
                 // pr($upload_dir);
                 // pr($file->name, 1);
                 // pr($file_path, 1);
                 // move_uploaded_file($uploaded_file, $file_path);
                 // $fileExtention  = explode_end($file->name, ".");
                 // $fileName = uniqueString().".".$fileExtention;
                 // $file->name = $fileName;
                 $uploadFile = Input::file('files');
                 $MyFTP = new MyFTP('choilive');
                 $uploadSuccess = $MyFTP->uploadFtp($this->options['upload_dir'], $uploaded_file, $file->name);
             }
         } else {
             // Non-multipart uploads (PUT method support)
             file_put_contents($file_path, fopen('php://input', 'r'), $append_file ? FILE_APPEND : 0);
         }
         $file_size = $this->get_file_size($file_path, $append_file);
         if ($file_size === $file->size) {
             $file->url = $this->get_download_url($file->name);
             if ($this->is_valid_image_file($file_path)) {
                 $this->handle_image_file($file_path, $file);
             }
         } else {
             $file->size = $file_size;
             if (!$content_range && $this->options['discard_aborted_uploads']) {
                 unlink($file_path);
                 $file->error = $this->get_error_message('abort');
             }
         }
         $this->set_additional_file_properties($file);
     }
     return $file;
 }