public function execute() { $id = $this->get('id', true); $images_model = new shopProductImagesModel(); $image = $images_model->getById($id); if (!$image) { throw new waAPIException('invalid_param', 'Product image not found', 404); } $this->response = $image; $size = waRequest::get('size', wa('shop')->getConfig()->getImageSize('thumb')); $this->response['url_thumb'] = shopImage::getUrl($image, $size, true); }
public function execute() { $id = $this->post('id', true); $images_model = new shopProductImagesModel(); $image = $images_model->getById($id); if (!$image) { throw new waAPIException('invalid_param', 'Product image not found', 404); } // check product rights $this->getProduct($image['product_id']); $product_images_model = new shopProductImagesModel(); if ($product_images_model->delete($id)) { $this->response = true; } else { throw new waAPIException('server_error', 500); } }
public function execute() { $id = waRequest::post('id', 0, waRequest::TYPE_INT); if (!$id) { throw new waException(_w("Unknown image")); } $product_images_model = new shopProductImagesModel(); $image = $product_images_model->getById($id); if (!$image) { throw new waException(_w("Unknown image")); } // check rights $product_model = new shopProductModel(); if (!$product_model->checkRights($image['product_id'])) { throw new waException(_w("Access denied")); } $product_images_model->updateById($id, $this->getData()); }
public function execute() { $id = waRequest::get('id', null, waRequest::TYPE_INT); if (!$id) { throw new waException(_w("Unknown image")); } $product_images_model = new shopProductImagesModel(); $image = $product_images_model->getById($id); if (!$image) { throw new waException(_w("Unknown image")); } $product_model = new shopProductModel(); if (!$product_model->checkRights($image['product_id'])) { throw new waException(_w("Access denied")); } if (!$product_images_model->delete($id)) { throw new waException(_w("Coudn't delete image")); } $this->response['id'] = $id; }
public function execute() { $id = waRequest::get('id', 0, waRequest::TYPE_INT); if (!$id) { throw new waException(_w("Image not found"), 404); } $product_images_model = new shopProductImagesModel(); $image = $product_images_model->getById($id); if (!$image) { throw new waException(_w("Image not found"), 404); } if (waRequest::get('original')) { $path = shopImage::getOriginalPath($image); } else { $path = shopImage::getPath($image); } if (!$path || !file_exists($path)) { throw new waException(_w("Image not found"), 404); } waFiles::readFile($path, "Image_{$image['product_id']}_{$image['id']}.{$image['ext']}", true, true); }
public function execute() { $id = waRequest::get('id', null, waRequest::TYPE_INT); if (!$id) { throw new waException("Unknown image"); } $direction = waRequest::post('direction', 'left', waRequest::TYPE_STRING_TRIM); if (!isset($this->angles[$direction])) { throw new waException("Can't rotate image"); } $product_images_model = new shopProductImagesModel(); $image = $product_images_model->getById($id); if (!$image) { throw new waException("Unknown image"); } // check rights $product_model = new shopProductModel(); if (!$product_model->checkRights($image['product_id'])) { throw new waException(_w("Access denied")); } $image_path = shopImage::getPath($image); $paths = array(); try { $result_image_path = preg_replace('/(\\.[^\\.]+)$/', '.result$1', $image_path); $backup_image_path = preg_replace('/(\\.[^\\.]+)$/', '.backup$1', $image_path); $paths[] = $result_image_path; if ($this->rotate($image_path, $result_image_path, $this->angles[$direction])) { $count = 0; while (!file_exists($result_image_path) && ++$count < 5) { sleep(1); } if (!file_exists($result_image_path)) { throw new waException(_w("Error while rotate. I/O error")); } if (!waFiles::move($image_path, $backup_image_path)) { throw new waException(_w("Error while rotate. Operation canceled")); } $paths[] = $backup_image_path; if (!waFiles::move($result_image_path, $image_path)) { if (!waFiles::move($backup_image_path, $image_path)) { throw new waException(_w("Error while rotate. Original file corupted but backuped")); } throw new waException(_w("Error while rotate. Operation canceled")); } $datetime = date('Y-m-d H:i:s'); $data = array('edit_datetime' => $datetime, 'width' => $image['height'], 'height' => $image['width']); $product_images_model->updateById($id, $data); $image = array_merge($image, $data); $thumb_dir = shopImage::getThumbsPath($image); $back_thumb_dir = preg_replace('@(/$|$)@', '.back$1', $thumb_dir, 1); $paths[] = $back_thumb_dir; waFiles::delete($back_thumb_dir); if (!(waFiles::move($thumb_dir, $back_thumb_dir) || waFiles::delete($back_thumb_dir)) && !waFiles::delete($thumb_dir)) { throw new waException(_w("Error while rebuild thumbnails")); } $config = $this->getConfig(); try { shopImage::generateThumbs($image, $config->getImageSizes()); } catch (Exception $e) { waLog::log($e->getMessage()); } $this->response = $image; $edit_datetime_ts = strtotime($image['edit_datetime']); $this->response['url_big'] = shopImage::getUrl($image, $config->getImageSize('big')) . '?' . $edit_datetime_ts; $this->response['url_crop'] = shopImage::getUrl($image, $config->getImageSize('crop')) . '?' . $edit_datetime_ts; } foreach ($paths as $path) { waFiles::delete($path); } } catch (Exception $e) { foreach ($paths as $path) { waFiles::delete($path); } throw $e; } }
public function cart() { $this->getResponse()->addHeader("Cache-Control", "no-store, no-cache, must-revalidate"); $this->getResponse()->addHeader("Expires", date("r")); if (waRequest::method() == 'post') { $data = wa()->getStorage()->get('shop/checkout', array()); if ($coupon_code = waRequest::post('coupon_code')) { $data['coupon_code'] = $coupon_code; } elseif (isset($data['coupon_code'])) { unset($data['coupon_code']); } if (($use = waRequest::post('use_affiliate')) !== null) { if ($use) { $data['use_affiliate'] = 1; } elseif (isset($data['use_affiliate'])) { unset($data['use_affiliate']); } } if ($coupon_code || $use) { wa()->getStorage()->set('shop/checkout', $data); wa()->getStorage()->remove('shop/cart'); } } $cart_model = new shopCartItemsModel(); $cart = new shopCart(); $code = $cart->getCode(); $errors = array(); if (waRequest::post('checkout')) { $saved_quantity = $cart_model->select('id,quantity')->where("type='product' AND code = s:code", array('code' => $code))->fetchAll('id'); $quantity = waRequest::post('quantity'); foreach ($quantity as $id => $q) { if ($q != $saved_quantity[$id]) { $cart->setQuantity($id, $q); } } $not_available_items = $cart_model->getNotAvailableProducts($code, !wa()->getSetting('ignore_stock_count')); foreach ($not_available_items as $row) { if ($row['sku_name']) { $row['name'] .= ' (' . $row['sku_name'] . ')'; } if ($row['available']) { $errors[$row['id']] = sprintf(_w('Only %d pcs of %s are available, and you already have all of them in your shopping cart.'), $row['count'], $row['name']); } else { $errors[$row['id']] = _w('Oops! %s is not available for purchase at the moment. Please remove this product from your shopping cart to proceed.'); } } if (!$errors) { $this->redirect(wa()->getRouteUrl('/frontend/checkout')); } } $items = $cart_model->where('code= ?', $code)->order('parent_id')->fetchAll('id'); $product_ids = $sku_ids = $service_ids = $type_ids = array(); foreach ($items as $item) { $product_ids[] = $item['product_id']; $sku_ids[] = $item['sku_id']; } $product_ids = array_unique($product_ids); $sku_ids = array_unique($sku_ids); $product_model = new shopProductModel(); if (waRequest::param('url_type') == 2) { $products = $product_model->getWithCategoryUrl($product_ids); } else { $products = $product_model->getById($product_ids); } $sku_model = new shopProductSkusModel(); $skus = $sku_model->getByField('id', $sku_ids, 'id'); $image_model = new shopProductImagesModel(); $delete_items = array(); foreach ($items as $item_id => &$item) { if (!isset($skus[$item['sku_id']])) { unset($items[$item_id]); $delete_items[] = $item_id; continue; } if ($item['type'] == 'product') { $item['product'] = $products[$item['product_id']]; $sku = $skus[$item['sku_id']]; if ($sku['image_id'] && $sku['image_id'] != $item['product']['image_id']) { $img = $image_model->getById($sku['image_id']); if ($img) { $item['product']['image_id'] = $sku['image_id']; $item['product']['ext'] = $img['ext']; } } $item['sku_name'] = $sku['name']; $item['sku_code'] = $sku['sku']; $item['price'] = $sku['price']; $item['compare_price'] = $sku['compare_price']; $item['currency'] = $item['product']['currency']; $type_ids[] = $item['product']['type_id']; if (isset($errors[$item_id])) { $item['error'] = $errors[$item_id]; if (strpos($item['error'], '%s') !== false) { $item['error'] = sprintf($item['error'], $item['product']['name'] . ($item['sku_name'] ? ' (' . $item['sku_name'] . ')' : '')); } } } } unset($item); if ($delete_items) { $cart_model->deleteByField(array('code' => $code, 'id' => $delete_items)); } $type_ids = array_unique($type_ids); // get available services for all types of products $type_services_model = new shopTypeServicesModel(); $rows = $type_services_model->getByField('type_id', $type_ids, true); $type_services = array(); foreach ($rows as $row) { $service_ids[] = $row['service_id']; $type_services[$row['type_id']][$row['service_id']] = true; } // get services for all products $product_services_model = new shopProductServicesModel(); $rows = $product_services_model->getByProducts($product_ids); $product_services = $sku_services = array(); foreach ($rows as $row) { if ($row['sku_id'] && !in_array($row['sku_id'], $sku_ids)) { continue; } $service_ids[] = $row['service_id']; if (!$row['sku_id']) { $product_services[$row['product_id']][$row['service_id']]['variants'][$row['service_variant_id']] = $row; } if ($row['sku_id']) { $sku_services[$row['sku_id']][$row['service_id']]['variants'][$row['service_variant_id']] = $row; } } $service_ids = array_unique($service_ids); $service_model = new shopServiceModel(); $variant_model = new shopServiceVariantsModel(); $services = $service_model->getByField('id', $service_ids, 'id'); foreach ($services as &$s) { unset($s['id']); } unset($s); $rows = $variant_model->getByField('service_id', $service_ids, true); foreach ($rows as $row) { $services[$row['service_id']]['variants'][$row['id']] = $row; unset($services[$row['service_id']]['variants'][$row['id']]['id']); } foreach ($items as $item_id => $item) { if ($item['type'] == 'product') { $p = $item['product']; $item_services = array(); // services from type settings if (isset($type_services[$p['type_id']])) { foreach ($type_services[$p['type_id']] as $service_id => &$s) { $item_services[$service_id] = $services[$service_id]; } } // services from product settings if (isset($product_services[$item['product_id']])) { foreach ($product_services[$item['product_id']] as $service_id => $s) { if (!isset($s['status']) || $s['status']) { if (!isset($item_services[$service_id])) { $item_services[$service_id] = $services[$service_id]; } // update variants foreach ($s['variants'] as $variant_id => $v) { if ($v['status']) { if ($v['price'] !== null) { $item_services[$service_id]['variants'][$variant_id]['price'] = $v['price']; } } else { unset($item_services[$service_id]['variants'][$variant_id]); } } } elseif (isset($item_services[$service_id])) { // remove disabled service unset($item_services[$service_id]); } } } // services from sku settings if (isset($sku_services[$item['sku_id']])) { foreach ($sku_services[$item['sku_id']] as $service_id => $s) { if (!isset($s['status']) || $s['status']) { // update variants foreach ($s['variants'] as $variant_id => $v) { if ($v['status']) { if ($v['price'] !== null) { $item_services[$service_id]['variants'][$variant_id]['price'] = $v['price']; } } else { unset($item_services[$service_id]['variants'][$variant_id]); } } } elseif (isset($item_services[$service_id])) { // remove disabled service unset($item_services[$service_id]); } } } foreach ($item_services as $s_id => &$s) { if (!$s['variants']) { unset($item_services[$s_id]); continue; } if ($s['currency'] == '%') { foreach ($s['variants'] as $v_id => $v) { $s['variants'][$v_id]['price'] = $v['price'] * $item['price'] / 100; } $s['currency'] = $item['currency']; } if (count($s['variants']) == 1) { $v = reset($s['variants']); $s['price'] = $v['price']; unset($s['variants']); } } unset($s); uasort($item_services, array('shopServiceModel', 'sortServices')); $items[$item_id]['services'] = $item_services; } else { $items[$item['parent_id']]['services'][$item['service_id']]['id'] = $item['id']; if (isset($item['service_variant_id'])) { $items[$item['parent_id']]['services'][$item['service_id']]['variant_id'] = $item['service_variant_id']; } unset($items[$item_id]); } } foreach ($items as $item_id => $item) { $price = shop_currency($item['price'] * $item['quantity'], $item['currency'], null, false); if (isset($item['services'])) { foreach ($item['services'] as $s) { if (!empty($s['id'])) { if (isset($s['variants'])) { $price += shop_currency($s['variants'][$s['variant_id']]['price'] * $item['quantity'], $s['currency'], null, false); } else { $price += shop_currency($s['price'] * $item['quantity'], $s['currency'], null, false); } } } } $items[$item_id]['full_price'] = $price; } $total = $cart->total(false); $order = array('total' => $total, 'items' => $items); $order['discount'] = $discount = shopDiscounts::calculate($order); $order['total'] = $total = $total - $order['discount']; $data = wa()->getStorage()->get('shop/checkout'); $this->view->assign('cart', array('items' => $items, 'total' => $total, 'count' => $cart->count())); $this->view->assign('coupon_code', isset($data['coupon_code']) ? $data['coupon_code'] : ''); if (shopAffiliate::isEnabled()) { $affiliate_bonus = 0; if ($this->getUser()->isAuth()) { $customer_model = new shopCustomerModel(); $customer = $customer_model->getById($this->getUser()->getId()); $affiliate_bonus = $customer ? round($customer['affiliate_bonus'], 2) : 0; } $this->view->assign('affiliate_bonus', $affiliate_bonus); $use = !empty($data['use_affiliate']); $this->view->assign('use_affiliate', $use); if ($use) { $discount -= shop_currency(shopAffiliate::convertBonus($order['params']['affiliate_bonus']), $this->getConfig()->getCurrency(true), null, false); $this->view->assign('used_affiliate_bonus', $order['params']['affiliate_bonus']); } $order['currency'] = $this->getConfig()->getCurrency(false); $add_affiliate_bonus = shopAffiliate::calculateBonus($order); $this->view->assign('add_affiliate_bonus', round($add_affiliate_bonus, 2)); } $this->view->assign('discount', $discount); /** * @event frontend_cart * @return array[string]string $return[%plugin_id%] html output */ $this->view->assign('frontend_cart', wa()->event('frontend_cart')); $checkout_flow = new shopCheckoutFlowModel(); $checkout_flow->add(array('code' => $code, 'step' => 0, 'description' => null)); }
public function execute() { $id = waRequest::post('id', null, waRequest::TYPE_INT); if (!$id) { throw new waException("Can't restore image"); } $product_images_model = new shopProductImagesModel(); $image = $product_images_model->getById($id); if (!$image) { throw new waException("Can't restore image"); } // check rights $product_model = new shopProductModel(); if (!$product_model->checkRights($image['product_id'])) { throw new waException(_w("Access denied")); } $original_image_path = shopImage::getOriginalPath($image); if (!wa('shop')->getConfig()->getOption('image_save_original') || !file_exists($original_image_path)) { throw new waException("Can't restore image. Original image doesn't exist"); } $image_path = shopImage::getPath($image); $paths = array(); try { $backup_image_path = preg_replace('/(\\.[^\\.]+)$/', '.backup$1', $image_path); if (!waFiles::move($image_path, $backup_image_path)) { throw new waException("Error while restore. Operation canceled"); } $paths[] = $backup_image_path; if (!waFiles::move($original_image_path, $image_path)) { if (!waFiles::move($backup_image_path, $image_path)) { throw new waException("Error while restore. Current file corupted but backuped"); } throw new waException("Error while restore. Operation canceled"); } $data = $this->getData($image_path); $product_images_model->updateById($id, $data); $image = array_merge($image, $data); $thumb_dir = shopImage::getThumbsPath($image); $back_thumb_dir = preg_replace('@(/$|$)@', '.back$1', $thumb_dir, 1); $paths[] = $back_thumb_dir; waFiles::delete($back_thumb_dir); // old backups if (!(waFiles::move($thumb_dir, $back_thumb_dir) || waFiles::delete($back_thumb_dir)) && !waFiles::delete($thumb_dir)) { throw new waException(_w("Error while rebuild thumbnails")); } /** * @var shopConfig $config */ $config = $this->getConfig(); try { shopImage::generateThumbs($image, $config->getImageSizes()); } catch (Exception $e) { waLog::log($e->getMessage()); } $this->response = $image; $edit_datetime_ts = strtotime($image['edit_datetime']); $this->response['url_big'] = shopImage::getUrl($image, $config->getImageSize('big')) . '?' . $edit_datetime_ts; $this->response['url_crop'] = shopImage::getUrl($image, $config->getImageSize('crop')) . '?' . $edit_datetime_ts; foreach ($paths as $path) { waFiles::delete($path); } } catch (Exception $e) { foreach ($paths as $path) { waFiles::delete($path); } throw $e; } }
/** * @param $items * @param $sku_ids * @param $product_ids * @return array */ private function getTypeIds(&$items, $sku_ids, $product_ids) { $product_model = new shopProductModel(); $sku_model = new shopProductSkusModel(); $image_model = new shopProductImagesModel(); /** * @todo: $products = $product_model->getWithCategoryUrl($product_ids); * $products = $product_model->getById($product_ids); */ $products = $product_model->getWithCategoryUrl($product_ids); $skus = $sku_model->getByField('id', $sku_ids, 'id'); $type_ids = array(); foreach ($items as $item_id => &$item) { if (!isset($skus[$item['sku_id']])) { unset($items[$item_id]); continue; } if ($item['type'] == 'product') { $item['product'] = $products[$item['product_id']]; $sku = $skus[$item['sku_id']]; if ($sku['image_id'] && $sku['image_id'] != $item['product']['image_id']) { $img = $image_model->getById($sku['image_id']); if ($img) { $item['product']['image_id'] = $sku['image_id']; $item['product']['ext'] = $img['ext']; } } $item['sku_name'] = $sku['name']; $item['sku_code'] = $sku['sku']; $item['price'] = $sku['price']; $item['currency'] = $item['product']['currency']; $type_ids[] = $item['product']['type_id']; } } return array_unique($type_ids); }