protected static function pre_find(&$query) { $query->select(static::$_table_name . '.*', Model_Infotab::get_protected('_table_name') . '.*'); $query->join(Model_Infotab::get_protected('_table_name')); $query->on(Model_Infotab::get_protected('_table_name') . '.id', '=', static::$_table_name . '.infotab_id'); }
/** * 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); } }
public function action_sort($type = false) { if (!$type) { return false; } $items = \Input::post('sort'); if (is_array($items)) { foreach ($items as $item) { list($item, $old_item) = explode('_', $item); if (is_numeric($item)) { $sort[] = $item; } if (is_numeric($old_item)) { $old_sort[] = $old_item; } } if (is_array($sort)) { // Get starting point for sort $start = min($old_sort); $start = $start > 0 ? --$start : $start; $model = Model_Infotab::factory(ucfirst($type)); foreach ($sort as $key => $id) { $item = $model::find_one_by_id($id); $item->set(array('cover' => $key == 0 ? 1 : 0, 'sort' => ++$start)); $item->save(); } \Messages::success('Items successfully reordered.'); echo \Messages::display('left', false); } } }