/** * Edit product infotabs * * @param $product_id = Product ID * @param $infotab_id = Infotab ID * @param $hotspot_id = Hotspot ID * */ public function action_infotab_edit($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()) { $val = Model_Product_To_Infotabs::validate('update', $item->type); // Upload image and display errors if there are any $image = $this->upload_infotab_image(); if (!$image['exists'] && \Config::get('infotab.image.required', false) && empty($item->images)) { // No previous images and image is not selected and it is required \Messages::error('<strong>There was an error while trying to upload infotab image</strong>'); \Messages::error('You have to select image'); } elseif ($image['errors']) { \Messages::error('<strong>There was an error while trying to upload infotab image</strong>'); foreach ($image['errors'] as $error) { \Messages::error($error); } } if ($val->run() && $image['is_valid'] && !(!$image['exists'] && \Config::get('infotab.image.required', false) && empty($item->images))) { /** IMAGES **/ // Get all alt texts to update if there is no image change foreach (\Arr::filter_prefixed(\Input::post(), 'alt_text_') as $image_id => $alt_text) { if (strpos($image_id, 'new_') === false) { $item_images[$image_id] = array('id' => $image_id, 'data' => array('alt_text' => \Input::post('alt_text_' . $image_id, ''))); } } // Save images if new files are submitted if (isset($this->_infotab_image_data)) { foreach ($this->_infotab_image_data as $image_data) { $cover_count = count($item->images); if (strpos($image_data['field'], 'new_') === false) { // Update existing image if (str_replace('image_', '', $image_data['field']) != 0) { $image_id = (int) str_replace('image_', '', $image_data['field']); $cover_count--; $item_images[$image_id] = array('id' => $image_id, 'data' => array('infotab_id' => $item->unique_id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_' . $image_id, ''))); $this->delete_infotab_image(\Input::post('image_db_' . $image_id, '')); } } else { // Save new image $image_tmp = str_replace('image_new_', '', $image_data['field']); $item_images[0] = array('id' => 0, 'data' => array('infotab_id' => $item->unique_id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_new_' . $image_tmp, ''), 'cover' => $cover_count == 0 ? 1 : 0, 'sort' => $cover_count + 1)); } } } Model_Infotab::bind_images($item_images); /** END OF IMAGES **/ // Get POST values $insert = \Input::post(); // Prep some values $insert['description'] = trim($insert['description']); $insert['active'] = !empty($insert['description']) ? 1 : 0; $item->set($insert); try { $item->save(); \Messages::success('Infotab successfully updated.'); \Response::redirect(\Input::post('exit', false) ? \Uri::create('admin/product/infotab_list/' . $product->id) : \Uri::admin('current')); } catch (\Database_Exception $e) { // show validation errors \Messages::error('<strong>There was an error while trying to update infotab</strong>'); // Uncomment lines below to show database errors //$errors = $e->getMessage(); //\Messages::error($errors); } } 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']); } } if ($val->error() != array()) { // show validation errors \Messages::error('<strong>There was an error while trying to update infotab</strong>'); foreach ($val->error() as $e) { \Messages::error($e->get_message()); } } } } $item = Model_Product_To_Infotabs::find_by_pk($infotab_id); $theme_partial = \Theme::instance()->set_partial('content', $this->view_dir . 'infotabs_edit_' . strtolower($item->type))->set('product', $product)->set('infotab', $item); if (isset($hotspot)) { $theme_partial->set('hotspot', $hotspot); } }
/** * 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 : ''))); }
/** * 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 product categories $item->get_categories = static::lazy_load(function () use($item) { $categories = Model_Product_To_Categories::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'category_id'); if (!empty($categories)) { $categories = '(' . implode(',', array_keys($categories)) . ')'; return Model_Category::find(function ($query) use($categories) { return $query->where('id', 'IN', \DB::expr($categories)); }, 'id'); } return array(); }, $item->id, 'categories'); // Get product infotabs $item->get_infotabs = static::lazy_load(function () use($item) { return Model_Product_To_Infotabs::find(function ($query) use($item) { $query->order_by('sort', 'asc'); return $query->where('product_id', $item->id); }); }, $item->id, 'infotabs'); // Get product groups $item->get_groups = static::lazy_load(function () use($item) { $groups = Model_Product_To_Groups::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'group_id'); if (!empty($groups)) { $groups = '(' . implode(',', array_keys($groups)) . ')'; return Model_Group::find(function ($query) use($groups, $item) { $query->where('id', 'IN', \DB::expr($groups)); $query->order_by('sort', 'asc'); return $query; }, 'id'); } return array(); }, $item->id, 'groups'); // Get product groups $item->get_pricing_group = static::lazy_load(function () use($item) { $groups = Model_Product_To_Groups::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'group_id'); if (!empty($groups)) { $groups = '(' . implode(',', array_keys($groups)) . ')'; $groups = Model_Group::find(function ($query) use($groups, $item) { $query->where('type', 'pricing'); $query->where('id', 'IN', \DB::expr($groups)); $query->order_by('sort', 'asc'); return $query; }); return !empty($groups) ? $groups[0] : false; } return false; }, $item->id, 'pricing_group', 'object'); // Get related products $item->get_related_products = static::lazy_load(function () use($item) { $related = Model_Product_To_Related::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'related_id'); if (!empty($related)) { $related = '(' . implode(',', array_keys($related)) . ')'; return Model_Product::find(function ($query) use($related, $item) { $query->where('id', 'IN', \DB::expr($related)); $query->where('id', '<>', $item->id); $query->order_by('sort', 'asc'); return $query; }, 'id'); } return array(); }, $item->id, 'related_products'); // Get upsell products $item->get_upsell_products = static::lazy_load(function () use($item) { $upsell = Model_Product_To_Upsell::find(function ($query) use($item) { return $query->where('product_id', $item->id); }, 'upsell_id'); if (!empty($upsell)) { $upsell = '(' . implode(',', array_keys($upsell)) . ')'; return Model_Product::find(function ($query) use($upsell, $item) { $query->select(Model_Product_To_Upsell::get_protected('_table_name') . '.discount', Model_Product::get_protected('_table_name') . '.*'); $query->join(Model_Product_To_Upsell::get_protected('_table_name')); $query->on(Model_Product_To_Upsell::get_protected('_table_name') . '.upsell_id', '=', Model_Product::get_protected('_table_name') . '.id'); $query->where(Model_Product::get_protected('_table_name') . '.id', 'IN', \DB::expr($upsell)); $query->where(Model_Product::get_protected('_table_name') . '.id', '<>', $item->id); $query->order_by(Model_Product::get_protected('_table_name') . '.sort', 'asc'); return $query; }, 'id'); } return array(); }, $item->id, 'upsell_products'); // Get content images $item->get_images = static::lazy_load(function () use($item) { return Model_Product_Image::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'images'); // Get content files $item->get_files = static::lazy_load(function () use($item) { return Model_Product_File::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'files'); // Get content videos $item->get_videos = static::lazy_load(function () use($item) { return Model_Product_Video::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'videos'); // Get content children $item->get_seo = static::lazy_load(function () use($item) { return Model_Product_Seo::find_one_by_content_id($item->id); }, $item->id, 'seo', 'object'); // Get attributes $item->get_attributes = static::lazy_load(function () use($item) { return Model_Attribute::find(array('where' => array('product_id' => $item->id))); }, $item->id, 'attributes'); // Get default attribute $item->get_default_attributes = static::lazy_load(function () use($item) { return Model_Attribute::find(array('where' => array('product_id' => $item->id, 'default' => 1, 'active' => 1))); }, $item->id, 'default_attributes'); // Get all active prices $item->get_all_prices = static::lazy_load(function () use($item) { $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); $query->and_where('attributes', '!=', ''); $query->and_where('active', 1); return $query; }, 'id'); return $product_attributes; }, $item->id, 'unit_price'); // Get prices for product $item->get_prices = static::lazy_load(function () use($item) { $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); return $query; }, 'id'); return $product_attributes; }, $item->id, 'unit_price'); // Get unit price $item->get_unit_price = static::lazy_load(function () use($item) { $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); $query->and_where('type', 'unit_price'); $query->and_where('attributes', '!=', ''); $query->and_where('active', 1); $query->limit(1); return $query; }, 'id'); return $product_attributes; }, $item->id, 'unit_price'); // Get sale price $item->get_sale_price = static::lazy_load(function () use($item) { $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); $query->and_where('type', 'sale_price'); $query->and_where('attributes', '!=', ''); $query->and_where('active', 1); $query->limit(1); return $query; }, 'id'); return $product_attributes; }, $item->id, 'sale_price'); // Get default price (RRP or Sale price) $item->get_default_price = static::lazy_load(function () use($item) { $out = array(0); $product_attributes = Model_Attribute::find(function ($query) use($item) { $query->join(Model_Attribute_Price::get_protected('_table_name')); $query->on(Model_Attribute_Price::get_protected('_table_name') . '.product_attribute_id', '=', Model_Attribute::get_protected('_table_name') . '.id'); $query->where('product_id', $item->id); $query->and_where('type', 'sale_price'); $query->and_where('attributes', '!=', ''); $query->and_where('active', 1); $query->and_where('default', 1); $query->limit(1); return $query; }, 'id'); if ($product_attributes) { $obj = reset($product_attributes); if ($obj->price > 0) { $out[0] = $obj->price; $out[1] = 'sale_price'; } else { $out[0] = $obj->retail_price; $out[1] = 'retail_price'; } } return $out; }, $item->id, 'default_price'); // Get active attribute group $item->get_active_attribute_group = static::lazy_load(function () use($item) { if ($attribute = Model_Attribute::find_one_by_product_id($item->id)) { return $attribute->attribute_group; } return false; }, $item->id, 'active_attribute_group'); // Get product data $item->get_data = static::lazy_load(function () use($item) { return \Product\Model_Product::product_data($item); }, $item->id, 'data'); } } } // return the result return $result; }
/** * Delete content image * * @param $content_id = Content ID */ public function action_delete_image($id = false) { if (is_numeric($id)) { // Get news item to edit if ($item = Model_Product_To_Infotabs::find_by_pk($id)) { // Delete hotspot images if (!empty($item->images)) { foreach ($item->images as $image) { \Request::forge('admin/product/delete_infotab_image/' . $image->id . '/' . $image->infotab_id)->execute()->response(); } } // Delete item try { $this->delete_image($item->image); $item->image = NULL; $item->alt_text = NULL; // Make infotab inactive as it cant show without image $item->active = 0; $item->save(); \Messages::success('Info Tab image successfully deleted.'); } catch (\Database_Exception $e) { // show validation errors \Messages::error('<strong>There was an error while trying to delete info tab image</strong>'); // Uncomment lines below to show database errors //$errors = $e->getMessage(); //\Messages::error($errors); } } } if (\Request::is_hmvc()) { \Messages::reset(); } else { \Response::redirect(\Input::referrer(\Uri::create('admin/product/infotab/list'))); } }