예제 #1
0
 /**
  * Insert or update page images
  * Certain array structure need to be passed
  * 
  * array(
  * 	0 => array(
  * 		'id' 	=> If numeric and larger than 0, image will be updated. Otherwise image is considered new
  * 		'data'	=> Array of image data to insert
  * 	),
  * 	1 => array(
  * 		'id' 	=> If numeric and larger than 0, image will be updated. Otherwise image is considered new
  * 		'data'	=> Array of image data to insert
  * 	),
  * 	etc.
  * )
  * 
  * @param $images
  */
 public static function bind_images($images = array())
 {
     if (empty($images) || !is_array($images)) {
         return false;
     }
     foreach ($images as $key => $image) {
         $item = Model_Attribute_Image::forge($image['data']);
         if (is_numeric($image['id']) && $image['id'] > 0) {
             // Update existing image
             $item->set(array('id' => $image['id']));
             $item->is_new(false);
         }
         $item->save();
     }
 }
예제 #2
0
 /**
  * Delete content image
  * 
  * @param $image_id		= Image ID
  * @param $content_id	= Content ID
  */
 public function action_delete_image($image_id = false, $content_id = false)
 {
     if ($image_id && $content_id) {
         $images = Model_Attribute_Image::find(array('where' => array('content_id' => $content_id), 'order_by' => array('sort' => 'asc')), 'id');
         if ($images) {
             if (isset($images[$image_id])) {
                 $image = $images[$image_id];
                 // Check if image used for more than one combination
                 $image2 = Model_Attribute_Image::find(array('where' => array('image' => $image->image)), 'id');
                 if ($image2 && count($image2) > 1) {
                     // Reset sort fields
                     \DB::update(Model_Attribute_Image::get_protected('_table_name'))->value('sort', \DB::expr('sort - 1'))->where('sort', '>', $image->sort)->execute();
                     $image->delete();
                     \Messages::success('Image was successfully deleted.');
                     \Response::redirect(\Input::referrer());
                 }
                 // If there is only one image and image is required
                 if (count($images) == 1) {
                     if (\Config::get('details.image.required', false)) {
                         \Messages::error('You can\'t delete all images. Please add new image in order to delete this one.');
                     } else {
                         // Reset sort fields
                         \DB::update(Model_Attribute_Image::get_protected('_table_name'))->value('sort', \DB::expr('sort - 1'))->where('sort', '>', $image->sort)->execute();
                         // Delete image
                         $this->delete_image($image->image);
                         $image->delete();
                         \Messages::success('Image was successfully deleted.');
                     }
                 } else {
                     if ($image->cover == 1) {
                         \Messages::error('You can\'t delete cover image. Set different image as cover in order to delete this one.');
                     } else {
                         // Reset sort fields
                         \DB::update(Model_Attribute_Image::get_protected('_table_name'))->value('sort', \DB::expr('sort - 1'))->where('sort', '>', $image->sort)->execute();
                         // Delete image
                         $this->delete_image($image->image);
                         $image->delete();
                         \Messages::success('Image was successfully deleted.');
                     }
                 }
             } else {
                 \Messages::error('Image you are trying to delete don\'t exists. Check your url and try again.');
             }
         } else {
             \Messages::error('Content Image you are trying to delete don\'t exists. Check your url and try again.');
         }
     }
     \Response::redirect(\Input::referrer());
 }