Exemple #1
0
 public static function save_images($file_tmps, $parent_id, $parent_id_field, $related_table, $public_flag = null, $type = 'img')
 {
     if (!in_array($type, array('img', 'file'))) {
         throw new InvalidArgumentException('Parameter type is invalid.');
     }
     $moved_files = array();
     $related_table_ids = array();
     if (!$file_tmps) {
         return array($moved_files, $related_table_ids);
     }
     $model = Site_Model::get_model_name($related_table);
     $related_table_obj = $model::forge();
     if (!($file_cate = $related_table_obj->get_image_prefix())) {
         throw new \InvalidArgumentException('Parameter $related_table is invalid.');
     }
     $new_filepath_prefix = Site_Upload::get_filepath_prefix($file_cate, $parent_id);
     $new_filename_prefix = Site_Upload::convert_filepath2filename($new_filepath_prefix);
     $is_save_storage = conf('upload.storageType') != 'normal';
     if (!$is_save_storage) {
         $new_file_dir = Site_Upload::get_uploaded_path('raw', 'img', true, false, $new_filepath_prefix);
         if (!Site_Upload::check_and_make_uploaded_dir($new_file_dir, conf('upload.check_and_make_dir_level'), conf('upload.mkdir_mode'))) {
             throw newFuelException('Failed to make save dirs.');
         }
     }
     foreach ($file_tmps as $id => $file_tmp) {
         $old_file_path = Site_Upload::get_uploaded_file_path($file_tmp->name, 'raw', 'img', true);
         $moved_files[$file_tmp->id] = array('from' => $old_file_path);
         if (!$is_save_storage) {
             $new_file_path = $new_file_dir . $file_tmp->name;
             Util_file::move($old_file_path, $new_file_path);
             $moved_files[$file_tmp->id]['to'] = $new_file_path;
             $moved_files[$file_tmp->id]['filepath_prefix'] = $new_filepath_prefix;
         }
         if ($type == 'img') {
             $moved_files[$file_tmp->id]['from_thumbnail'] = Site_Upload::get_uploaded_file_path($file_tmp->name, 'thumbnail', 'img', true);
         }
         $file = Model_File::move_from_file_tmp($file_tmp, $new_filename_prefix, $type);
         $related_table_obj = $model::forge();
         $related_table_obj->{$parent_id_field} = $parent_id;
         $related_table_obj->file_name = $file->name;
         $related_table_obj->name = $file_tmp->description;
         $related_table_obj->shot_at = !empty($file->shot_at) ? $file->shot_at : date('Y-m-d H:i:s');
         if (!is_null($public_flag)) {
             $related_table_obj->public_flag = $public_flag;
         }
         $related_table_obj->save();
         $related_table_ids[] = $related_table_obj->id;
     }
     return array($moved_files, $related_table_ids);
 }