62 if (is_object($USER) && $USER->isAuthorized())
64 $this->isAdmin = ($USER->isAdmin() || $USER->canDoOperation(
'bitrix24_config'));
65 $this->userId = $USER->getId();
74 return $this->errorCollection->toArray();
88 $this->checkRequiredInputParams($subscribeData,
89 array(
'USER_CONTACT',
'ITEM_ID',
'SITE_ID',
'CONTACT_TYPE'));
90 if(!$this->errorCollection->isEmpty())
98 'USER_CONTACT' =>
'Bitrix\Catalog\SubscribeTable:PRODUCT.USER_CONTACT',
99 'CONTACT_TYPE' =>
'Bitrix\Catalog\SubscribeTable:PRODUCT.CONTACT_TYPE',
100 'DATE_TO' =>
'Bitrix\Catalog\SubscribeTable:PRODUCT.DATE_TO',
103 '=ID' => $subscribeData[
'ITEM_ID'],
106 while($productSubscribeData = $resultObject->fetch())
108 if(!$this->checkDataBeforeSave($productSubscribeData, $subscribeData))
111 if(!$this->errorCollection->isEmpty())
116 $this->fields = array();
117 foreach($subscribeData as $fieldId => $fieldValue)
119 if(in_array($fieldId, $this->listAvailableFields))
121 $this->fields[$fieldId] = $fieldValue;
125 $this->validateFields();
126 if(!$this->errorCollection->isEmpty())
131 $result = SubscribeTable::add($this->fields);
132 if($result->isSuccess())
135 return $result->getId();
139 foreach($result->getErrorMessages() as $errorMessage)
140 $this->errorCollection->add(array(
new Error($errorMessage, self::ERROR_ADD_SUBSCRIBE)));
155 foreach($listSubscribeId as $subscribeId)
159 $result = SubscribeTable::delete($subscribeId);
160 if(!$result->isSuccess())
162 foreach($result->getErrorMessages() as $errorMessage)
163 $this->errorCollection->add(array(
new Error($errorMessage, self::ERROR_DELETE_SUBSCRIBE)));
169 $this->errorCollection->add(array(
new Error(
170 Loc::getMessage(
'ERROR_ACCESS_DENIDE_DELETE_SUBSCRIBE'), self::ERROR_DELETE_SUBSCRIBE)));
175 $this->deleteSessionOfSibscribedProducts($itemId);
195 $resultObject = SubscribeTable::getList(array(
198 'TOKEN' =>
'Bitrix\Catalog\SubscribeAccessTable:SUBSCRIBE.TOKEN',
200 'filter' => array(
'=ID' => intval($subscribeId)),
202 if($subscribeData = $resultObject->fetch())
206 if($subscribeData[
'USER_ID'] == $this->userId)
213 if(isset($_SESSION[
'SUBSCRIBE_PRODUCT'][
'TOKEN'])
214 && $subscribeData[
'TOKEN'] == $_SESSION[
'SUBSCRIBE_PRODUCT'][
'TOKEN'])
234 $this->checkRequiredInputParams($subscriberData,
235 array(
'contactType',
'userContact',
'siteId'));
236 if(!$this->errorCollection->isEmpty())
241 $currentContactType = $this->contactTypes[$subscriberData[
'contactType']];
242 if(!preg_match($currentContactType[
'RULE'], $subscriberData[
'userContact']))
244 $this->errorCollection->add(array(
new Error(
245 Loc::getMessage(
'ERROR_SUBSCRIBE_ENTRY_CONFIRMATION_CODE'), self::ERROR_SUBSCRIBER_IDENTIFICATION)));
249 $token = Random::getString(6);
254 $accessRow = SubscribeAccessTable::getRow(
256 'select' => array(
'ID'),
257 'filter' => array(
'=USER_CONTACT' => $subscriberData[
'userContact'])
262 $result = SubscribeAccessTable::update($accessRow[
'ID'], array(
269 $result = SubscribeAccessTable::add(array(
271 'USER_CONTACT' => $subscriberData[
'userContact'],
275 if(!$result->isSuccess())
277 $this->errorCollection->add(array(
new Error(
278 Loc::getMessage(
'ERROR_SUBSCRIBE_ENTRY_CONFIRMATION_CODE'), self::ERROR_SUBSCRIBER_IDENTIFICATION)));
283 $this->errorCollection->add(array(
new Error($errorObject->getMessage())));
286 if(!$this->errorCollection->isEmpty())
297 $dataSendToNotice = array();
298 $listSubscribesUrl = \CHTTP::URN2URI(
'/personal/subscribe/');
299 $dataSendToNotice[$subscriberData[
'contactType']][$subscriberData[
'userContact']][] = array(
300 'EVENT_NAME' =>
'CATALOG_PRODUCT_SUBSCRIBE_LIST_CONFIRM',
301 'EMAIL_TO' => $subscriberData[
'userContact'],
302 'SITE_ID' => $subscriberData[
'siteId'],
305 'LIST_SUBSCRIBES' => $listSubscribesUrl,
306 'TOKEN_URL' => \CHTTP::urlAddParams($listSubscribesUrl, array(
'accessCodeVerification' =>
'Y',
307 'userContact' => $subscriberData[
'userContact'],
'subscribeToken' => $token)),
308 'URL_PARAMETERS' => \CHTTP::urlAddParams(
'', array(
'accessCodeVerification' =>
'Y',
309 'userContact' => $subscriberData[
'userContact'],
'subscribeToken' => $token))
312 foreach($this->contactTypes as $typeId => $typeData)
314 $eventKey = EventManager::getInstance()
315 ->addEventHandler(
'catalog',
'OnSubscribeSubmit', $typeData[
'HANDLER']);
317 $event =
new Event(
'catalog',
'OnSubscribeSubmit', $dataSendToNotice[$typeId]);
320 EventManager::getInstance()->removeEventHandler(
'catalog',
'OnSubscribeSubmit', $eventKey);
334 $this->checkRequiredInputParams($authorizationData, array(
'userContact',
'subscribeToken'));
335 if(!$this->errorCollection->isEmpty())
343 $accessRow = SubscribeAccessTable::getRow(
345 'select' => array(
'ID'),
347 'TOKEN' => $authorizationData[
'subscribeToken'],
348 'USER_CONTACT' => $authorizationData[
'userContact']
353 if(!$accessRow[
'ID'])
355 $this->errorCollection->add(array(
new Error(
356 Loc::getMessage(
'ERROR_AUTHORIZATION_ACCESS_ROW_NOT_FOUND'), self::ERROR_AUTHORIZATION)));
360 $_SESSION[
'SUBSCRIBE_PRODUCT'] = array(
361 'TOKEN' => $authorizationData[
'subscribeToken'],
362 'USER_CONTACT' => $authorizationData[
'userContact']
367 $this->errorCollection->add(array(
new Error($errorObject->getMessage())));
370 if(!$this->errorCollection->isEmpty())
388 $this->checkRequiredInputParams($data, array(
'unSubscribe',
'userContact',
'subscribeId',
'productId'));
389 if(!$this->errorCollection->isEmpty())
394 $itemId = intval($data[
'productId']);
396 $subscribe = SubscribeTable::getList(array(
397 'select' => array(
'CNT'),
399 '=ID' => intval($data[
'subscribeId']),
400 '=ITEM_ID' => $itemId,
401 '=USER_CONTACT' => $data[
'userContact'],
405 if(intval($subscribe[
'CNT']))
407 SubscribeTable::delete($data[
'subscribeId']);
411 $this->errorCollection->add(array(
new Error(
412 Loc::getMessage(
'ERROR_UNSUBSCRIBE_ALREADY_UNSUBSCRIBE'), self::ERROR_UNSUBSCRIBE)));
416 $this->deleteSessionOfSibscribedProducts($itemId);
437 $fields = array(
'DATE_TO' =>
false);
440 foreach($listSubscribeId as $subscribeId)
442 $result = SubscribeTable::update($subscribeId,
$fields);
443 if(!$result->isSuccess())
445 foreach($result->getErrorMessages() as $errorMessage)
446 $this->errorCollection->add(array(
new Error($errorMessage, self::ERROR_ACTIVITY_CHANGE)));
463 foreach($listSubscribeId as $subscribeId)
465 $result = SubscribeTable::update($subscribeId, array(
'DATE_TO' =>
new DateTime()));
466 if(!$result->isSuccess())
468 foreach($result->getErrorMessages() as $errorMessage)
469 $this->errorCollection->add(array(
new Error($errorMessage, self::ERROR_ACTIVITY_CHANGE)));
507 $itemId = intval($itemId);
508 if(!empty($_SESSION[
'SUBSCRIBE_PRODUCT'][
'LIST_PRODUCT_ID'])
509 && is_array($_SESSION[
'SUBSCRIBE_PRODUCT'][
'LIST_PRODUCT_ID']))
511 $_SESSION[
'SUBSCRIBE_PRODUCT'][
'LIST_PRODUCT_ID'][$itemId] =
true;
515 if(empty($_SESSION[
'SUBSCRIBE_PRODUCT']))
516 $_SESSION[
'SUBSCRIBE_PRODUCT'] = array();
518 $_SESSION[
'SUBSCRIBE_PRODUCT'][
'LIST_PRODUCT_ID'] = array();
519 $_SESSION[
'SUBSCRIBE_PRODUCT'][
'LIST_PRODUCT_ID'][$itemId] =
true;
523 private function deleteSessionOfSibscribedProducts($itemId = 0)
525 if(!empty($_SESSION[
'SUBSCRIBE_PRODUCT'][
'LIST_PRODUCT_ID'][$itemId]))
527 unset($_SESSION[
'SUBSCRIBE_PRODUCT'][
'LIST_PRODUCT_ID'][$itemId]);
531 private function checkDataBeforeSave($productSubscribeData, array $subscribeData)
533 if(!$productSubscribeData || !is_array($productSubscribeData))
535 $this->errorCollection->add(array(
new Error(
536 Loc::getMessage(
'ERROR_PRODUCT_NOT_FOUND'), self::ERROR_ADD_SUBSCRIBE)));
541 $this->errorCollection->add(array(
new Error(
542 Loc::getMessage(
'ERROR_SUBSCRIBE_DENIED'), self::ERROR_ADD_SUBSCRIBE)));
545 if(!array_key_exists($subscribeData[
'CONTACT_TYPE'], $this->contactTypes))
547 $this->errorCollection->add(array(
new Error(
551 if($productSubscribeData[
'USER_CONTACT'] == $subscribeData[
'USER_CONTACT']
552 && $productSubscribeData[
'DATE_TO'] ==
null)
555 $this->errorCollection->add(array(
new Error(
556 Loc::getMessage(
'ERROR_SUBSCRIBE_ALREADY_EXISTS'), self::ERROR_ADD_SUBSCRIBE_ALREADY_EXISTS)));
562 private function validateFields()
564 foreach($this->fields as $fieldId => $fieldValue)
569 if(!($fieldValue instanceof DateTime) ||
570 ($fieldValue instanceof DateTime && $fieldValue->getTimestamp() < time()))
573 array(
'#FIELD#' => $fieldId)), self::ERROR_VALIDATE_FIELDS)));
577 $currentContactType = $this->contactTypes[$this->fields[
'CONTACT_TYPE']];
578 if(!preg_match($currentContactType[
'RULE'], $fieldValue))
581 array(
'#FIELD#' => $fieldId)), self::ERROR_VALIDATE_FIELDS)));
588 private function checkRequiredInputParams(array $inputParams, array $requiredParams)
590 foreach ($requiredParams as $param)
592 if(!isset($inputParams[$param]) || (!$inputParams[$param] &&
593 !(is_string($inputParams[$param]) && mb_strlen($inputParams[$param]))))
596 array(
'#PARAM#' => $param)), self::ERROR_REQUIRED_PARAMATERS)));
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
static getList(array $parameters=array())
static createFromTimestamp($timestamp)