Beispiel #1
0
 /**
  * Insert or update product files
  * Certain array structure need to be passed
  * 
  * array(
  *  0 => array(
  *      'id'    => If numeric and larger than 0, file will be updated. Otherwise file is considered new
  *      'data'  => Array of file data to insert
  *  ),
  *  1 => array(
  *      'id'    => If numeric and larger than 0, file will be updated. Otherwise file is considered new
  *      'data'  => Array of file data to insert
  *  ),
  *  etc.
  * )
  * 
  * @param $files
  */
 public static function bind_files($files = array())
 {
     if (empty($files) || !is_array($files)) {
         return false;
     }
     foreach ($files as $key => $file) {
         $data = $file['data'];
         if (is_numeric($file['id']) && $file['id'] > 0) {
             // Update existing file
             $item = Model_Infotab_File::find_one_by_id($file['id']);
             $item->set($data);
         } else {
             $item = Model_Infotab_File::forge($data);
         }
         $item->save();
     }
 }
Beispiel #2
0
 /**
  * Get additional content data selected
  * 
  * @param $result = Query result
  */
 public static function post_find($result)
 {
     if ($result !== null) {
         if (is_array($result)) {
             foreach ($result as $item) {
                 // It will first check if we already have result in temporary result,
                 // and only execute query if we dont. That way we dont have duplicate queries
                 // Get content images
                 $item->get_images = static::lazy_load(function () use($item) {
                     return Model_Infotab_Image::find(array('where' => array('infotab_id' => $item->unique_id), 'order_by' => array('sort' => 'asc')));
                 }, $item->unique_id, 'images');
                 // Get content files
                 $item->get_files = static::lazy_load(function () use($item) {
                     return Model_Infotab_File::find(array('where' => array('infotab_id' => $item->id, 'product_id' => $item->product_id), 'order_by' => array('sort' => 'asc')));
                 }, $item->id, 'files');
             }
         }
     }
     // return the result
     return $result;
 }
Beispiel #3
0
 /**
  * Delete content file
  * 
  * @param $file_id		= File ID
  * @param $content_id	= Content ID
  */
 public function action_delete_infotab_file($file_id = false, $content_id = false)
 {
     if ($file_id && $content_id) {
         $files = Model_Infotab_File::find(array('where' => array('infotab_id' => $content_id), 'order_by' => array('sort' => 'asc')), 'id');
         if ($files) {
             if (isset($files[$file_id])) {
                 $file = $files[$file_id];
                 // If there is only one image and image is required
                 if (count($files) == 1) {
                     if (\Config::get('details.file.required', false)) {
                         \Messages::error('You can\'t delete all files. Please add new file in order to delete this one.');
                     } else {
                         // Reset sort fields
                         \DB::update(Model_Infotab_File::get_protected('_table_name'))->value('sort', \DB::expr('sort - 1'))->where('sort', '>', $file->sort)->execute();
                         // Delete file
                         $this->delete_file($file->file);
                         $file->delete();
                         \Messages::success('File was successfully deleted.');
                     }
                 } else {
                     // Dont use cover option for files
                     if (FALSE && $file->cover == 1) {
                         \Messages::error('You can\'t delete cover file. Set different file as cover in order to delete this one.');
                     } else {
                         // Reset sort fields
                         \DB::update(Model_Infotab_File::get_protected('_table_name'))->value('sort', \DB::expr('sort - 1'))->where('sort', '>', $file->sort)->execute();
                         // Delete file
                         $this->delete_file($file->file);
                         $file->delete();
                         // Set another file as cover if cover is deleted
                         if ($file->cover == 1) {
                             $files = Model_Infotab_File::find(array('where' => array('infotab_id' => $content_id), 'order_by' => array('sort' => 'asc')));
                             $files[0]->cover = 1;
                             $files[0]->save();
                         }
                         \Messages::success('File was successfully deleted.');
                     }
                 }
             } else {
                 \Messages::error('File you are trying to delete don\'t exists. Check your url and try again.');
             }
         } else {
             \Messages::error('File you are trying to delete don\'t exists. Check your url and try again.');
         }
     }
     \Response::redirect(\Input::referrer());
 }