public function get_upload_files($whitelist = array(), $name = null)
 {
     $result = array();
     $config = array('path' => $this->files_dir_path(), 'ext_whitelist' => $whitelist, 'randomize' => true);
     \Fuel\Core\Upload::process($config);
     if (!\Fuel\Core\Upload::is_valid()) {
         $errors = \Fuel\Core\Upload::get_errors();
         foreach ($errors as $error) {
             if (is_null($name)) {
                 $name = $error['field'];
             } else {
                 $name = str_replace('.png', '', $name);
             }
             $name = str_replace('@2x', ' retina', $name);
             $this->error = 'Error ' . $name . ' upload';
         }
         return false;
     }
     \Fuel\Core\Upload::save();
     $files = \Fuel\Core\Upload::get_files();
     foreach ($files as $file) {
         if (is_null($name)) {
             $name = $file['field'];
         }
         if ($name == 'certificate') {
             $name .= '.p12';
         }
         $this->remove_file($this->file_path($name));
         \Fuel\Core\File::rename($file['saved_to'] . $file['saved_as'], $this->file_path($name));
     }
     return true;
 }
Example #2
0
 public static function run($type = null, $options = array())
 {
     $data = array();
     $photo_name = array();
     try {
         Upload::process();
     } catch (Exception $e) {
         $data['error'] = Lang::get('notice.upload.no_file');
         return $data;
     }
     if (Upload::is_valid()) {
         Upload::save();
     }
     foreach (Upload::get_errors() as $file) {
         $data['error'] = $file['errors']['0']['message'];
         return $data;
     }
     foreach (Upload::get_files() as $file) {
         $resize = self::resize_photo($file['saved_to'], $file['saved_as'], $type);
         $photo_name[] = $file['saved_as'];
     }
     if ($resize) {
         switch ($type) {
             case 'icon':
                 if (Model_Base_User::update($options['user_id'], array('user_photo' => $photo_name[0]))) {
                     $old_photo = $file['saved_to'] . $type . '/' . $options['user_photo'];
                     if (File::exists($old_photo)) {
                         File::delete($old_photo);
                     }
                     $data['photo_name'] = _PATH_ICON_ . $photo_name[0];
                 } else {
                     $data['error'] = Lang::get('notice.upload.save_icon_error');
                 }
                 break;
             case 'category':
                 $old_photo = $options['type'] === 'new' ?: $file['saved_to'] . $type . '/' . Model_Category::find($options['category_id'])->category_photo;
                 if (Model_Base_Category::update($options['category_id'], array('category_photo' => $photo_name[0]))) {
                     if (File::exists($old_photo)) {
                         File::delete($old_photo);
                     }
                     $data['photo_name'] = _PATH_CATEGORY_ . $photo_name[0];
                 } else {
                     $data['error'] = Lang::get('notice.upload.save_category_error');
                 }
                 break;
             case 'product':
                 $old_photo = $file['saved_to'] . $type . '/' . Model_Product::find($options['product_id'])->product_photo;
                 if (Model_Base_Product::update($options['product_id'], array('product_photo' => $photo_name[0]))) {
                     if (File::exists($old_photo)) {
                         File::delete($old_photo);
                     }
                     $data['photo_name'] = _PATH_PRODUCT_ . $photo_name[0];
                 } else {
                     $data['error'] = Lang::get('notice.upload.save_product_error');
                 }
                 break;
             case 'photo':
                 if ($options['type'] === 'sub_product_photo') {
                     $photo_props = array('product_id' => $options['product_id'], 'photo_name' => $photo_name[0]);
                     if ($photo_id = Model_Base_Photo::insert($photo_props)) {
                         $data['photo_id'] = $photo_id;
                         $data['photo_name'] = _PATH_PHOTO_ . $photo_name[0];
                     } else {
                         $data['error'] = Lang::get('notice.upload.save_photo_error');
                     }
                 }
                 break;
             default:
                 $data['error'] = Lang::get('system_error');
                 break;
         }
     } else {
         $data['error'] = Lang::get('system_error');
     }
     return $data;
 }