1<?php 2/** 3 * Copyright since 2007 PrestaShop SA and Contributors 4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA 5 * 6 * NOTICE OF LICENSE 7 * 8 * This source file is subject to the Open Software License (OSL 3.0) 9 * that is bundled with this package in the file LICENSE.md. 10 * It is also available through the world-wide-web at this URL: 11 * https://opensource.org/licenses/OSL-3.0 12 * If you did not receive a copy of the license and are unable to 13 * obtain it through the world-wide-web, please send an email 14 * to license@prestashop.com so we can send you a copy immediately. 15 * 16 * DISCLAIMER 17 * 18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer 19 * versions in the future. If you wish to customize PrestaShop for your 20 * needs please refer to https://devdocs.prestashop.com/ for more information. 21 * 22 * @author PrestaShop SA and Contributors <contact@prestashop.com> 23 * @copyright Since 2007 PrestaShop SA and Contributors 24 * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) 25 */ 26 27namespace PrestaShopBundle\Controller\Admin; 28 29use PrestaShopBundle\Form\Admin\Product\ProductVirtual; 30use Symfony\Component\HttpFoundation\BinaryFileResponse; 31use Symfony\Component\HttpFoundation\JsonResponse; 32use Symfony\Component\HttpFoundation\Request; 33use Symfony\Component\HttpFoundation\ResponseHeaderBag; 34 35/** 36 * Admin controller for the virtual product on the /product/form page. 37 */ 38class VirtualProductController extends FrameworkBundleAdminController 39{ 40 /** 41 * Process Ajax Form to create/update virtual product. 42 * 43 * @param string|int $idProduct 44 * @param Request $request 45 * 46 * @return JsonResponse 47 */ 48 public function saveAction($idProduct, Request $request) 49 { 50 $response = new JsonResponse(); 51 $legacyContext = $this->get('prestashop.adapter.legacy.context'); 52 $adminProductWrapper = $this->get('prestashop.adapter.admin.wrapper.product'); 53 $productAdapter = $this->get('prestashop.adapter.data_provider.product'); 54 $router = $this->get('router'); 55 56 //get product 57 $product = $productAdapter->getProduct((int) $idProduct); 58 59 if (!$product || !$request->isXmlHttpRequest()) { 60 return $response; 61 } 62 63 $form = $this->createForm( 64 ProductVirtual::class, 65 null, 66 ['csrf_protection' => false] 67 ); 68 69 $form->handleRequest($request); 70 if ($form->isValid()) { 71 $data = $form->getData(); 72 $res = $adminProductWrapper->updateDownloadProduct($product, $data); 73 $res->file_download_link = 74 $res->filename ? 75 $router->generate( 76 'admin_product_virtual_download_file_action', 77 ['idProduct' => $idProduct] 78 ) : 79 ''; 80 81 $product->is_virtual = 1; 82 $product->save(); 83 84 $response->setData($res); 85 } else { 86 $response->setStatusCode(400); 87 $response->setData($this->getFormErrorsForJS($form)); 88 } 89 90 return $response; 91 } 92 93 /** 94 * Download the content of the virtual product. 95 * 96 * @param int $idProduct 97 * 98 * @return BinaryFileResponse 99 */ 100 public function downloadFileAction($idProduct) 101 { 102 $configuration = $this->get('prestashop.adapter.legacy.configuration'); 103 $download = $this->getDoctrine() 104 ->getRepository('PrestaShopBundle:ProductDownload') 105 ->findOneBy([ 106 'idProduct' => $idProduct, 107 ]); 108 109 $response = new BinaryFileResponse( 110 $configuration->get('_PS_DOWNLOAD_DIR_') . $download->getFilename() 111 ); 112 113 $response->setContentDisposition( 114 ResponseHeaderBag::DISPOSITION_ATTACHMENT, 115 $download->getDisplayFilename() 116 ); 117 118 return $response; 119 } 120 121 /** 122 * Process Ajax Form to remove attached file. 123 * 124 * @param string|int $idProduct 125 * @param Request $request 126 * 127 * @return JsonResponse 128 */ 129 public function removeFileAction($idProduct, Request $request) 130 { 131 $response = new JsonResponse(); 132 $adminProductWrapper = $this->get('prestashop.adapter.admin.wrapper.product'); 133 $productAdapter = $this->get('prestashop.adapter.data_provider.product'); 134 135 //get product 136 $product = $productAdapter->getProduct((int) $idProduct); 137 138 if (!$product || !$request->isXmlHttpRequest()) { 139 return $response; 140 } 141 142 $adminProductWrapper->processDeleteVirtualProductFile($product); 143 144 return $response; 145 } 146 147 /** 148 * Process Ajax remove action. 149 * 150 * @param string|int $idProduct 151 * @param Request $request 152 * 153 * @return JsonResponse 154 */ 155 public function removeAction($idProduct, Request $request) 156 { 157 $response = new JsonResponse(); 158 $adminProductWrapper = $this->get('prestashop.adapter.admin.wrapper.product'); 159 $productAdapter = $this->get('prestashop.adapter.data_provider.product'); 160 161 //get product 162 $product = $productAdapter->getProduct((int) $idProduct); 163 164 if (!$product || !$request->isXmlHttpRequest()) { 165 return $response; 166 } 167 168 $adminProductWrapper->processDeleteVirtualProduct($product); 169 170 return $response; 171 } 172} 173