/** * Insert or update product 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) { $data = $image['data']; if (is_numeric($image['id']) && $image['id'] > 0) { // Update existing image $item = Model_Infotab_Image::find_one_by_id($image['id']); $item->set($data); } else { $item = Model_Infotab_Image::forge($data); } $item->save(); } }
/** * Edit product hotspot position * * @param $product_id = Product ID * @param $infotab_id = Infotab ID * */ public function action_infotab_hotspot($produt_id = false, $infotab_id = false, $hotspot_id = false) { // Check for product if (!is_numeric($produt_id)) { \Response::redirect('admin/product/list'); } // Get news item to edit if (!($product = Model_Product::find_one_by_id($produt_id))) { \Response::redirect('admin/product/list'); } // Check for infotab if (!is_numeric($infotab_id)) { \Response::redirect('admin/product/list'); } // Get news item to edit if (!($item = Model_Product_To_Infotabs::find_by_pk($infotab_id))) { \Response::redirect('admin/product/list'); } // Get hotspot is exist if (is_numeric($hotspot_id)) { if (!($hotspot = Model_Infotab_Image::find_by_pk($hotspot_id))) { unset($hotspot); } } if (\Input::post()) { $insert = \Input::post(); if (!\Input::is_ajax()) { $val = Model_Infotab_Image::validate('create'); if (!$val->run()) { if ($val->error() != array()) { // show validation errors \Messages::error('<strong>There was an error while trying to create hotspot</strong>'); foreach ($val->error() as $e) { \Messages::error($e->get_message()); } } \Response::redirect(\Uri::create('admin/product/infotab_edit/' . $product->id . '/' . $item->unique_id . (isset($hotspot) ? '/' . $hotspot->id : ''))); } $insert['title'] = trim($insert['title']) != '' ? $insert['title'] : NULL; $insert['description'] = trim($insert['description']) != '' ? $insert['description'] : NULL; } $insert['infotab_id'] = $infotab_id; if (\Input::post('create', false)) { $hotspot = Model_Infotab_Image::forge($insert); try { $hotspot->save(); if (\Input::is_ajax()) { $return['hotspot_id'] = $hotspot->id; echo json_encode($return); exit; } } catch (\Database_Exception $e) { // show validation errors \Messages::error('<strong>There was an error while trying to create hotspot</strong>'); // Uncomment lines below to show database errors $errors = $e->getMessage(); \Messages::error($errors); if (\Input::is_ajax()) { $return['message'] = \Messages::display(); $return['hotspot_id'] = false; echo json_encode($return); exit; } } } if (\Input::post('update', false)) { if (isset($hotspot)) { /** IMAGES **/ // Upload image and display errors if there are any $image = $this->upload_infotab_image(); if (!$image['exists'] && \Config::get('infotab.image.required', false) && empty($hotspot->image)) { // No previous images and image is not selected and it is required \Messages::error('<strong>There was an error while trying to upload hotspot image</strong>'); \Messages::error('You have to select image'); } elseif ($image['errors']) { \Messages::error('<strong>There was an error while trying to upload hotspot image</strong>'); foreach ($image['errors'] as $error) { \Messages::error($error); } } if ($image['is_valid'] && !(!$image['exists'] && \Config::get('infotab.image.required', false) && empty($hotspot->image)) || \Input::post('use_cover_image', false)) { // Clear previous messages if exists \Messages::reset(); $item_image['alt_text'] = \Input::post('alt_text', false) ? \Input::post('alt_text', false) : NULL; // Save images if new files are submitted if (isset($this->_infotab_image_data)) { foreach ($this->_infotab_image_data as $image_data) { $item_image['image'] = $image_data['saved_as']; // Delete old infotab image if (\Input::post('image_db', false)) { $this->delete_infotab_image(\Input::post('image_db', '')); } } } if (isset($item_image)) { $insert['alt_text'] = isset($item_image['alt_text']) ? $item_image['alt_text'] : NULL; $insert['image'] = isset($item_image['image']) ? $item_image['image'] : $hotspot->image; } } else { // Delete uploaded images if there is product saving error if (isset($this->_infotab_image_data)) { foreach ($this->_infotab_image_data as $image_data) { $this->delete_infotab_image($image_data['saved_as']); } } } /** END OF IMAGES **/ /** VIDEOS **/ $item_video['video_title'] = \Input::post('video_title', false) ? \Input::post('video_title', false) : NULL; $item_video['video'] = \Input::post('video_url', false) ? \Input::post('video_url', false) : NULL; if (!is_null($item_video['video'])) { // Check video $youtube = \App\Youtube::forge(); $video = $youtube->parse($item_video['video'])->get(); if (!$video) { \Messages::error('"' . $item_video['video'] . '" is invalid video URL. Video not updated.'); // Revert to old values $item_video['video_title'] = $hotspot->video_title; $item_video['video'] = $hotspot->video; } } if (isset($item_video)) { $insert['video'] = isset($item_video['video']) ? $item_video['video'] : NULL; $insert['video_title'] = isset($item_video['video_title']) ? $item_video['video_title'] : NULL; // Unset video title is there is no video if (is_null($insert['video'])) { $insert['video_title'] = NULL; } } /** END OF: VIDEOS **/ $hotspot->set($insert); try { $hotspot->save(); \Messages::success('Hotspot sucessfully updated.'); } catch (\Database_Exception $e) { // show validation errors \Messages::error('There was an error while trying to update hotspot.'); \Messages::error('Please try again.'); // Uncomment lines below to show database errors $errors = $e->getMessage(); \Messages::error($errors); // Delete uploaded images if there is product saving error if (isset($this->_infotab_image_data)) { foreach ($this->_infotab_image_data as $image_data) { $this->delete_infotab_image($image_data['saved_as']); } } } if (\Input::is_ajax()) { echo \Messages::display(); exit; } } } } \Response::redirect(\Uri::create('admin/product/infotab_edit/' . $product->id . '/' . $item->unique_id . (isset($hotspot) ? '/' . $hotspot->id : ''))); }