PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/components/bitrix/catalog.product.subscribe.list/class.php

https://gitlab.com/alexprowars/bitrix
PHP | 339 lines | 279 code | 40 blank | 20 comment | 52 complexity | de90bb2751de39a7d1d9d0bfd0cbefc0 MD5 | raw file
  1. <?php
  2. use Bitrix\Main,
  3. Bitrix\Iblock,
  4. Bitrix\Catalog,
  5. Bitrix\Main\Localization\Loc;
  6. if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED !== true) die();
  7. CBitrixComponent::includeComponentClass("bitrix:catalog.viewed.products");
  8. class CatalogProductsSubscribeListComponent extends \CCatalogViewedProductsComponent
  9. {
  10. const ACTION_SUBSCRIBER_IDENTIFICATION = 'subscriberIdentification';
  11. const ACTION_ACCESS_CODE_VERIFICATION = 'accessCodeVerification';
  12. const ACTION_UNSUBSCRIBE = 'unSubscribe';
  13. /**
  14. * @var integer
  15. */
  16. protected $userId = 0;
  17. /**
  18. * List of product ids which will be showed.
  19. * @var array
  20. */
  21. protected $listProductId = array();
  22. protected $codeList = array();
  23. /**
  24. * Event called from includeComponent before component execution.
  25. *
  26. * <p>Takes component parameters as argument and should return it formatted as needed.</p>
  27. * @param array[string]mixed $arParams
  28. * @return array[string]mixed
  29. *
  30. */
  31. public function onPrepareComponentParams($params)
  32. {
  33. $params = parent::onPrepareComponentParams($params);
  34. if(!isset($params['LINE_ELEMENT_COUNT']))
  35. $params['LINE_ELEMENT_COUNT'] = 3;
  36. $params['LINE_ELEMENT_COUNT'] = intval($params['LINE_ELEMENT_COUNT']);
  37. if($params['LINE_ELEMENT_COUNT'] < 2 || $params['LINE_ELEMENT_COUNT'] > 5)
  38. $params['LINE_ELEMENT_COUNT'] = 3;
  39. $params["DETAIL_URL"] = trim($params["DETAIL_URL"]);
  40. if(Main\Loader::includeModule('catalog'))
  41. {
  42. global $USER, $DB;
  43. if(is_object($USER) && $USER->isAuthorized())
  44. $this->userId = $USER->getId();
  45. $filter = array(
  46. '=SITE_ID' => SITE_ID,
  47. array(
  48. 'LOGIC' => 'OR',
  49. array('=DATE_TO' => false),
  50. array('>DATE_TO' => date($DB->dateFormatToPHP(\CLang::getDateFormat('FULL')), time()))
  51. )
  52. );
  53. if($this->userId)
  54. {
  55. $filter['USER_ID'] = $this->userId;
  56. $params['GUEST_ACCESS'] = true;
  57. }
  58. else
  59. {
  60. if(!empty($_SESSION['SUBSCRIBE_PRODUCT']['TOKEN']) && !empty($_SESSION['SUBSCRIBE_PRODUCT']['USER_CONTACT']))
  61. {
  62. $filter['=Bitrix\Catalog\SubscribeAccessTable:SUBSCRIBE.TOKEN'] =
  63. $_SESSION['SUBSCRIBE_PRODUCT']['TOKEN'];
  64. $filter['=Bitrix\Catalog\SubscribeAccessTable:SUBSCRIBE.USER_CONTACT'] =
  65. $_SESSION['SUBSCRIBE_PRODUCT']['USER_CONTACT'];
  66. $params['GUEST_ACCESS'] = true;
  67. }
  68. else
  69. {
  70. return $params;
  71. }
  72. }
  73. $resultObject = Catalog\SubscribeTable::getList(
  74. array(
  75. 'select' => array(
  76. 'ID',
  77. 'ITEM_ID',
  78. 'TYPE' => 'PRODUCT.TYPE',
  79. 'IBLOCK_ID' => 'IBLOCK_ELEMENT.IBLOCK_ID',
  80. ),
  81. 'filter' => $filter,
  82. )
  83. );
  84. $listIblockId = array();
  85. while($item = $resultObject->fetch())
  86. {
  87. $params['SHOW_PRODUCTS'][$item['IBLOCK_ID']] = true;
  88. $params['LIST_SUBSCRIPTIONS'][$item['ITEM_ID']][] = $item['ID'];
  89. $listIblockId[$item['ITEM_ID']] = $item['IBLOCK_ID'];
  90. }
  91. $params['NEED_VALUES'] = array();
  92. $listSubscribeItemId = array();
  93. foreach($listIblockId as $itemId => $iblockId)
  94. {
  95. $sku = CCatalogSKU::getInfoByProductIBlock($iblockId);
  96. if(!empty($sku) && is_array($sku))
  97. {
  98. $this->prepareItemData($itemId, $sku, $params);
  99. $this->listProductId[] = $itemId;
  100. $listSubscribeItemId[] = $itemId;
  101. }
  102. else
  103. {
  104. $parent = CCatalogSKU::getProductList($itemId);
  105. if(!empty($parent))
  106. {
  107. $parentItemId = $parent[$itemId]['ID'];
  108. $parentIblockId = $parent[$itemId]['IBLOCK_ID'];
  109. }
  110. else
  111. {
  112. $parentItemId = $itemId;
  113. $parentIblockId = $iblockId;
  114. }
  115. $offerSku = CCatalogSKU::getInfoByOfferIBlock($iblockId);
  116. if(!empty($offerSku) && is_array($offerSku))
  117. {
  118. $this->prepareItemData($parentItemId, $offerSku, $params, $itemId);
  119. $params['SHOW_PRODUCTS'][$parentIblockId] = true;
  120. }
  121. if(!in_array($parentItemId, $this->listProductId))
  122. $this->listProductId[] = $parentItemId;
  123. $listSubscribeItemId[] = $itemId;
  124. }
  125. }
  126. if(!empty($listSubscribeItemId))
  127. {
  128. $subscribeManager = new Catalog\Product\SubscribeManager;
  129. foreach($listSubscribeItemId as $itemId)
  130. $subscribeManager->setSessionOfSibscribedProducts($itemId);
  131. }
  132. if(!empty($this->codeList))
  133. {
  134. foreach($this->codeList as $iblockId => $code)
  135. $params['PROPERTY_CODE'][$iblockId] = $code;
  136. }
  137. }
  138. return $params;
  139. }
  140. protected function prepareData()
  141. {
  142. parent::prepareData();
  143. }
  144. /**
  145. * Returns list of product ids which will be showed.
  146. *
  147. * @return array
  148. */
  149. protected function getProductIds()
  150. {
  151. return $this->listProductId;
  152. }
  153. protected function prepareItemData($itemId, array $sku, &$params, $offerId = 0)
  154. {
  155. $offersTreeProps = array();
  156. $propertyValue = array();
  157. if (!array_key_exists($itemId, $params['NEED_VALUES']))
  158. $params['NEED_VALUES'][$itemId] = array();
  159. $codeList = $this->getPropertyCodeList($sku);
  160. $offersList = CCatalogSKU::getOffersList($itemId, 0,
  161. array('ACTIVE' => 'Y'), array(), array('CODE' => $codeList));
  162. if(!empty($offersList))
  163. {
  164. foreach($offersList[$itemId] as $offersId => &$offers)
  165. {
  166. if($offerId && $offersId != $offerId)
  167. continue;
  168. foreach($offers['PROPERTIES'] as $propertiesCode => $properties)
  169. {
  170. if($properties['ID'] == $sku['SKU_PROPERTY_ID'] || empty($properties['VALUE']))
  171. continue;
  172. if(!is_array($propertyValue[$propertiesCode]))
  173. $propertyValue[$propertiesCode] = array();
  174. if(!in_array($properties['VALUE'],$propertyValue[$propertiesCode]))
  175. {
  176. if (!array_key_exists($properties['ID'], $params['NEED_VALUES'][$itemId]))
  177. $params['NEED_VALUES'][$itemId][$properties['ID']] = array();
  178. $valueId = ($properties['PROPERTY_TYPE'] == \Bitrix\Iblock\PropertyTable::TYPE_LIST
  179. ? $properties['VALUE_ENUM_ID'] : $properties['VALUE']
  180. );
  181. $params['NEED_VALUES'][$itemId][$properties['ID']][$valueId] = $valueId;
  182. $propertyValue[$propertiesCode][] = $properties['VALUE'];
  183. }
  184. $offersTreeProps[] = $propertiesCode;
  185. }
  186. }
  187. }
  188. $params['OFFER_TREE_PROPS'][$itemId] = array_unique($offersTreeProps);
  189. if(!empty($params['PROPERTY_VALUE'][$itemId]))
  190. {
  191. $params['PROPERTY_VALUE'][$itemId] = array_merge_recursive($params['PROPERTY_VALUE'][$itemId], $propertyValue);
  192. foreach($params['PROPERTY_VALUE'][$itemId] as &$property)
  193. $property = array_unique($property);
  194. }
  195. else
  196. {
  197. $params['PROPERTY_VALUE'][$itemId] = $propertyValue;
  198. }
  199. }
  200. protected function getPropertyCodeList(array $sku)
  201. {
  202. $codeList = array();
  203. $propertyIterator = Iblock\PropertyTable::getList(array(
  204. 'select' => array('CODE', 'PROPERTY_TYPE', 'MULTIPLE', 'USER_TYPE', 'IBLOCK_ID'),
  205. 'filter' => array('=IBLOCK_ID' => $sku['IBLOCK_ID'], '=ACTIVE' => 'Y')
  206. ));
  207. while ($property = $propertyIterator->fetch())
  208. {
  209. if($property['MULTIPLE'] == 'Y' || $property['ID'] == $sku['SKU_PROPERTY_ID'])
  210. continue;
  211. $property['USER_TYPE'] = (string)$property['USER_TYPE'];
  212. if (empty($property['CODE']))
  213. $property['CODE'] = $property['ID'];
  214. if (
  215. $property['PROPERTY_TYPE'] == 'L'
  216. || $property['PROPERTY_TYPE'] == 'E'
  217. || ($property['PROPERTY_TYPE'] == 'S' && $property['USER_TYPE'] == 'directory')
  218. )
  219. {
  220. $codeList[] = $property['CODE'];
  221. $this->codeList[$property['IBLOCK_ID']][] = $property['CODE'];
  222. }
  223. }
  224. return $codeList;
  225. }
  226. protected function formatResult()
  227. {
  228. parent::formatResult();
  229. $this->arResult['USER_ID'] = $this->userId;
  230. $this->arResult['CONTACT_TYPES'] = Catalog\SubscribeTable::getContactTypes();
  231. }
  232. protected function doActionsList()
  233. {
  234. $this->runSubscriberIdentification();
  235. $this->authorizeSubscriber();
  236. $this->unSubscribe();
  237. parent::doActionsList();
  238. }
  239. protected function runSubscriberIdentification()
  240. {
  241. if(empty($_REQUEST[static::ACTION_SUBSCRIBER_IDENTIFICATION]))
  242. return;
  243. $subscribeManager = new Catalog\Product\SubscribeManager;
  244. $result = $subscribeManager->runSubscriberIdentification($_REQUEST);
  245. if($result)
  246. {
  247. $message = Loc::getMessage('CPSL_REQUEST_IDENTIFICATION_SUCCESS');
  248. $stringParams = 'result=identificationOk&message='.urlencode($message).
  249. '&contact='.urlencode($_REQUEST['userContact']);
  250. }
  251. else
  252. {
  253. $errorObject = current($subscribeManager->getErrors());
  254. $message = $errorObject ? $errorObject->getMessage() : Loc::getMessage('CPSL_REQUEST_DEFAULT_ERROR');
  255. $stringParams = 'result=identificationFail&message='.urlencode($message);
  256. }
  257. global $APPLICATION;
  258. $cleanedParams = array('result', 'contact', 'message', static::ACTION_SUBSCRIBER_IDENTIFICATION);
  259. LocalRedirect($APPLICATION->getCurPageParam($stringParams, $cleanedParams));
  260. }
  261. protected function authorizeSubscriber()
  262. {
  263. if(empty($_REQUEST[static::ACTION_ACCESS_CODE_VERIFICATION]))
  264. return;
  265. $subscribeManager = new Catalog\Product\SubscribeManager;
  266. $result = $subscribeManager->authorizeSubscriber($_REQUEST);
  267. $stringParams = '';
  268. if(!$result)
  269. {
  270. $errorObject = current($subscribeManager->getErrors());
  271. $message = $errorObject ? $errorObject->getMessage() : Loc::getMessage('CPSL_REQUEST_DEFAULT_ERROR');
  272. $stringParams = 'result=authorizeFail&message='.urlencode($message);
  273. }
  274. global $APPLICATION;
  275. $cleanedParams = array('result', 'message',
  276. static::ACTION_ACCESS_CODE_VERIFICATION, 'userContact', 'subscribeToken');
  277. LocalRedirect($APPLICATION->getCurPageParam($stringParams, $cleanedParams));
  278. }
  279. protected function unSubscribe()
  280. {
  281. if(empty($_REQUEST[static::ACTION_UNSUBSCRIBE]))
  282. return;
  283. $subscribeManager = new Catalog\Product\SubscribeManager;
  284. $result = $subscribeManager->unSubscribe($_REQUEST);
  285. if($result)
  286. {
  287. $stringParams = 'result=unSubscribeOk&message='.urlencode(Loc::getMessage('CPSL_REQUEST_UNSUBSCRIBE_SUCCESS'));
  288. }
  289. else
  290. {
  291. $errorObject = current($subscribeManager->getErrors());
  292. $message = $errorObject ? $errorObject->getMessage() : Loc::getMessage('CPSL_REQUEST_DEFAULT_ERROR');
  293. $stringParams = 'result=unSubscribeFail&message='.urlencode($message);
  294. }
  295. global $APPLICATION;
  296. $cleanedParams = array('subscribeId', 'message', 'userContact', 'productId', static::ACTION_UNSUBSCRIBE);
  297. LocalRedirect($APPLICATION->getCurPageParam($stringParams, $cleanedParams));
  298. }
  299. }