/** * Get additional content data selected * * @param $result = Query result */ public static function post_find($result) { $config = \Config::load('stock-option.db'); 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 category products $item->get_products = static::lazy_load(function () use($item, $config) { $products = Model_Product_To_Categories::find(function ($query) use($item) { return $query->where('category_id', $item->id); }, 'product_id'); if (!empty($products)) { $products = '(' . implode(',', array_keys($products)) . ')'; return Model_Product::find(function ($query) use($products, $item, $config) { $query->select('product.*'); if ($config['hide_out_of_stock']) { $query->join('product_attributes'); $query->on('product.id', '=', 'product_attributes.product_id'); $query->where('product_attributes.stock_quantity', '>', 0); } $query->and_where('product.id', 'IN', \DB::expr($products)); $query->group_by('product.id'); $query->order_by('product.sort', 'asc'); // print_r((string)$query); // die; return $query; }, 'id'); } return array(); }, $item->id, 'products'); // Get all category products (including subcategory products) $item->get_all_products = static::lazy_load(function () use($item) { $categories_ids = \Product\Model_Category::get_all_children($item); $products = Model_Product_To_Categories::find(function ($query) use($item, $categories_ids) { return $query->where('category_id', 'in', $categories_ids); }, 'product_id'); if (!empty($products)) { $products = '(' . implode(',', array_keys($products)) . ')'; return Model_Product::find(function ($query) use($products, $item) { $query->where('id', 'IN', \DB::expr($products)); $query->order_by('sort', 'asc'); return $query; }, 'id'); } return array(); }, $item->id, 'all_products'); // Get content images $item->get_images = static::lazy_load(function () use($item) { return Model_Category_Image::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'images'); // Get content children $item->get_children = static::lazy_load(function () use($item) { return \Product\Model_Category::find(array('where' => array('parent_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'children'); // Get content children $item->get_seo = static::lazy_load(function () use($item) { return Model_Category_Seo::find_one_by_content_id($item->id); }, $item->id, 'seo', 'object'); } } } // return the result return $result; }
/** * 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_Category_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]; // 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_Category_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_Category_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()); }