PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/blockwishlist/controllers/front/mywishlist.php

https://gitlab.com/ptisky/API_prestashop
PHP | 246 lines | 186 code | 26 blank | 34 comment | 48 complexity | 0be9ad100d97f39156581dba01b4b82b MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/afl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * @since 1.5.0
  28. */
  29. class BlockWishListMyWishListModuleFrontController extends ModuleFrontController
  30. {
  31. public $ssl = true;
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. $this->context = Context::getContext();
  36. include_once($this->module->getLocalPath().'WishList.php');
  37. }
  38. /**
  39. * @see FrontController::initContent()
  40. */
  41. public function initContent()
  42. {
  43. parent::initContent();
  44. $action = Tools::getValue('action');
  45. if (!Tools::isSubmit('myajax'))
  46. $this->assign();
  47. elseif (!empty($action) && method_exists($this, 'ajaxProcess'.Tools::toCamelCase($action)))
  48. $this->{'ajaxProcess'.Tools::toCamelCase($action)}();
  49. else
  50. die(Tools::jsonEncode(array('error' => 'method doesn\'t exist')));
  51. }
  52. /**
  53. * Assign wishlist template
  54. */
  55. public function assign()
  56. {
  57. $errors = array();
  58. if ($this->context->customer->isLogged())
  59. {
  60. $add = Tools::getIsset('add');
  61. $add = (empty($add) === false ? 1 : 0);
  62. $delete = Tools::getIsset('deleted');
  63. $delete = (empty($delete) === false ? 1 : 0);
  64. $default = Tools::getIsset('default');
  65. $default = (empty($default) === false ? 1 : 0);
  66. $id_wishlist = Tools::getValue('id_wishlist');
  67. if (Tools::isSubmit('submitWishlist'))
  68. {
  69. if (Configuration::get('PS_TOKEN_ACTIVATED') == 1 && strcmp(Tools::getToken(), Tools::getValue('token')))
  70. $errors[] = $this->module->l('Invalid token', 'mywishlist');
  71. if (!count($errors))
  72. {
  73. $name = Tools::getValue('name');
  74. if (empty($name))
  75. $errors[] = $this->module->l('You must specify a name.', 'mywishlist');
  76. if (WishList::isExistsByNameForUser($name))
  77. $errors[] = $this->module->l('This name is already used by another list.', 'mywishlist');
  78. if (!count($errors))
  79. {
  80. $wishlist = new WishList();
  81. $wishlist->id_shop = $this->context->shop->id;
  82. $wishlist->id_shop_group = $this->context->shop->id_shop_group;
  83. $wishlist->name = $name;
  84. $wishlist->id_customer = (int)$this->context->customer->id;
  85. !$wishlist->isDefault($wishlist->id_customer) ? $wishlist->default = 1 : '';
  86. list($us, $s) = explode(' ', microtime());
  87. srand($s * $us);
  88. $wishlist->token = strtoupper(substr(sha1(uniqid(rand(), true)._COOKIE_KEY_.$this->context->customer->id), 0, 16));
  89. $wishlist->add();
  90. Mail::Send(
  91. $this->context->language->id,
  92. 'wishlink',
  93. Mail::l('Your wishlist\'s link', $this->context->language->id),
  94. array(
  95. '{wishlist}' => $wishlist->name,
  96. '{message}' => $this->context->link->getModuleLink('blockwishlist', 'view', array('token' => $wishlist->token))
  97. ),
  98. $this->context->customer->email,
  99. $this->context->customer->firstname.' '.$this->context->customer->lastname,
  100. null,
  101. strval(Configuration::get('PS_SHOP_NAME')),
  102. null,
  103. null,
  104. $this->module->getLocalPath().'mails/');
  105. Tools::redirect($this->context->link->getModuleLink('blockwishlist', 'mywishlist'));
  106. }
  107. }
  108. }
  109. else if ($add)
  110. WishList::addCardToWishlist($this->context->customer->id, Tools::getValue('id_wishlist'), $this->context->language->id);
  111. elseif ($delete && empty($id_wishlist) === false)
  112. {
  113. $wishlist = new WishList((int)$id_wishlist);
  114. if ($this->context->customer->isLogged() && $this->context->customer->id == $wishlist->id_customer && Validate::isLoadedObject($wishlist))
  115. $wishlist->delete();
  116. else
  117. $errors[] = $this->module->l('Cannot delete this wishlist', 'mywishlist');
  118. }
  119. elseif ($default)
  120. {
  121. $wishlist = new WishList((int)$id_wishlist);
  122. if ($this->context->customer->isLogged() && $this->context->customer->id == $wishlist->id_customer && Validate::isLoadedObject($wishlist))
  123. $wishlist->setDefault();
  124. else
  125. $errors[] = $this->module->l('Cannot delete this wishlist', 'mywishlist');
  126. }
  127. $this->context->smarty->assign('wishlists', WishList::getByIdCustomer($this->context->customer->id));
  128. $this->context->smarty->assign('nbProducts', WishList::getInfosByIdCustomer($this->context->customer->id));
  129. }
  130. else
  131. Tools::redirect('index.php?controller=authentication&back='.urlencode($this->context->link->getModuleLink('blockwishlist', 'mywishlist')));
  132. $this->context->smarty->assign(array(
  133. 'id_customer' => (int)$this->context->customer->id,
  134. 'errors' => $errors,
  135. 'form_link' => $errors,
  136. ));
  137. $this->setTemplate('mywishlist.tpl');
  138. }
  139. public function ajaxProcessDeleteList()
  140. {
  141. if (!$this->context->customer->isLogged())
  142. die(Tools::jsonEncode(array('success' => false,
  143. 'error' => $this->module->l('You aren\'t logged in', 'mywishlist'))));
  144. $default = Tools::getIsset('default');
  145. $default = (empty($default) === false ? 1 : 0);
  146. $id_wishlist = Tools::getValue('id_wishlist');
  147. $wishlist = new WishList((int)$id_wishlist);
  148. if (Validate::isLoadedObject($wishlist) && $wishlist->id_customer == $this->context->customer->id)
  149. {
  150. $default_change = $wishlist->default ? true : false;
  151. $id_customer = $wishlist->id_customer;
  152. $wishlist->delete();
  153. }
  154. else
  155. die(Tools::jsonEncode(array('success' => false,
  156. 'error' => $this->module->l('Cannot delete this wishlist', 'mywishlist'))));
  157. if ($default_change)
  158. {
  159. $array = WishList::getDefault($id_customer);
  160. if (count($array))
  161. die(Tools::jsonEncode(array(
  162. 'success' => true,
  163. 'id_default' => $array[0]['id_wishlist']
  164. )));
  165. }
  166. die(Tools::jsonEncode(array('success' => true)));
  167. }
  168. public function ajaxProcessSetDefault()
  169. {
  170. if (!$this->context->customer->isLogged())
  171. die(Tools::jsonEncode(array('success' => false,
  172. 'error' => $this->module->l('You aren\'t logged in', 'mywishlist'))));
  173. $default = Tools::getIsset('default');
  174. $default = (empty($default) === false ? 1 : 0);
  175. $id_wishlist = Tools::getValue('id_wishlist');
  176. if ($default)
  177. {
  178. $wishlist = new WishList((int)$id_wishlist);
  179. if (Validate::isLoadedObject($wishlist) && $wishlist->id_customer == $this->context->customer->id && $wishlist->setDefault())
  180. die(Tools::jsonEncode(array('success' => true)));
  181. }
  182. die(Tools::jsonEncode(array('error' => true)));
  183. }
  184. public function ajaxProcessProductChangeWishlist()
  185. {
  186. if (!$this->context->customer->isLogged())
  187. die(Tools::jsonEncode(array('success' => false,
  188. 'error' => $this->module->l('You aren\'t logged in', 'mywishlist'))));
  189. $id_product = (int)Tools::getValue('id_product');
  190. $id_product_attribute = (int)Tools::getValue('id_product_attribute');
  191. $quantity = (int)Tools::getValue('quantity');
  192. $priority = (int)Tools::getValue('priority');
  193. $id_old_wishlist = (int)Tools::getValue('id_old_wishlist');
  194. $id_new_wishlist = (int)Tools::getValue('id_new_wishlist');
  195. $new_wishlist = new WishList((int)$id_new_wishlist);
  196. $old_wishlist = new WishList((int)$id_old_wishlist);
  197. //check the data is ok
  198. if (!$id_product || !is_int($id_product_attribute) || !$quantity ||
  199. !is_int($priority) || ($priority < 0 && $priority > 2) || !$id_old_wishlist || !$id_new_wishlist ||
  200. (Validate::isLoadedObject($new_wishlist) && $new_wishlist->id_customer != $this->context->customer->id) ||
  201. (Validate::isLoadedObject($old_wishlist) && $old_wishlist->id_customer != $this->context->customer->id))
  202. die(Tools::jsonEncode(array('success' => false, 'error' => $this->module->l('Error while moving product to another list', 'mywishlist'))));
  203. $res = true;
  204. $check = (int)Db::getInstance()->getValue('SELECT quantity FROM '._DB_PREFIX_.'wishlist_product
  205. WHERE `id_product` = '.$id_product.' AND `id_product_attribute` = '.$id_product_attribute.' AND `id_wishlist` = '.$id_new_wishlist);
  206. if ($check)
  207. {
  208. $res &= $old_wishlist->removeProduct($id_old_wishlist, $this->context->customer->id, $id_product, $id_product_attribute);
  209. $res &= $new_wishlist->updateProduct($id_new_wishlist, $id_product, $id_product_attribute, $priority, $quantity + $check);
  210. }
  211. else
  212. {
  213. $res &= $old_wishlist->removeProduct($id_old_wishlist, $this->context->customer->id, $id_product, $id_product_attribute);
  214. $res &= $new_wishlist->addProduct($id_new_wishlist, $this->context->customer->id, $id_product, $id_product_attribute, $quantity);
  215. }
  216. if (!$res)
  217. die(Tools::jsonEncode(array('success' => false, 'error' => $this->module->l('Error while moving product to another list', 'mywishlist'))));
  218. die(Tools::jsonEncode(array('success' => true, 'msg' => $this->module->l('The product has been correctly moved', 'mywishlist'))));
  219. }
  220. }