Beispiel #1
0
 public function action_icon()
 {
     //get icon
     if (isset($_FILES['location_icon'])) {
         $icon = $_FILES['location_icon'];
     } else {
         $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'index')));
     }
     $location = new Model_Location($this->request->param('id'));
     if (core::config('image.aws_s3_active')) {
         require_once Kohana::find_file('vendor', 'amazon-s3-php-class/S3', 'php');
         $s3 = new S3(core::config('image.aws_access_key'), core::config('image.aws_secret_key'));
     }
     if (core::post('icon_delete') and $location->delete_icon() == TRUE) {
         Alert::set(Alert::SUCCESS, __('Icon deleted.'));
         $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $location->id_location)));
     }
     // end of icon delete
     if (!Upload::valid($icon) or !Upload::not_empty($icon) or !Upload::type($icon, explode(',', core::config('image.allowed_formats'))) or !Upload::size($icon, core::config('image.max_image_size') . 'M')) {
         if (Upload::not_empty($icon) && !Upload::type($icon, explode(',', core::config('image.allowed_formats')))) {
             Alert::set(Alert::ALERT, $icon['name'] . ' ' . sprintf(__('Is not valid format, please use one of this formats "%s"'), core::config('image.allowed_formats')));
             $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $location->id_location)));
         }
         if (!Upload::size($icon, core::config('image.max_image_size') . 'M')) {
             Alert::set(Alert::ALERT, $icon['name'] . ' ' . sprintf(__('Is not of valid size. Size is limited to %s MB per image'), core::config('image.max_image_size')));
             $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $location->id_location)));
         }
         Alert::set(Alert::ALERT, $icon['name'] . ' ' . __('Image is not valid. Please try again.'));
         $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $location->id_location)));
     } else {
         if ($icon != NULL) {
             // saving/uploading img file to dir.
             $path = 'images/locations/';
             $root = DOCROOT . $path;
             //root folder
             $icon_name = $location->seoname . '.png';
             // if folder does not exist, try to make it
             if (!file_exists($root) and !@mkdir($root, 0775, true)) {
                 // mkdir not successful ?
                 Alert::set(Alert::ERROR, __('Image folder is missing and cannot be created with mkdir. Please correct to be able to upload images.'));
                 return;
                 // exit function
             }
             // save file to root folder, file, name, dir
             if ($file = Upload::save($icon, $icon_name, $root)) {
                 // put icon to Amazon S3
                 if (core::config('image.aws_s3_active')) {
                     $s3->putObject($s3->inputFile($file), core::config('image.aws_s3_bucket'), $path . $icon_name, S3::ACL_PUBLIC_READ);
                 }
                 // update location info
                 $location->has_image = 1;
                 $location->last_modified = Date::unix2mysql();
                 $location->save();
                 Alert::set(Alert::SUCCESS, $icon['name'] . ' ' . __('Icon is uploaded.'));
             } else {
                 Alert::set(Alert::ERROR, $icon['name'] . ' ' . __('Icon file could not been saved.'));
             }
             $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $location->id_location)));
         }
     }
 }