[ "Registration for %value% is already in the Shopping Cart", "Registrations for %value% are already in the Shopping Cart", ] ]; function __construct(array $options) { if ($options instanceof Zend_Config) { $options = $options->toArray(); } else if (!is_array($options)) { throw new Exception('Invalid options'); } if (!array_key_exists('cartId', $options)) { throw new Exception('cartId is undefined'); } if (!array_key_exists('elementId', $options)) { throw new Exception('elementId is undefined'); } $this->setCartId($options['cartId']); $this->setElementId($options['elementId']); } /** * @param mixed $cartId * @return $this */ public function setCartId($cartId) { $this->_cartId = $cartId; return $this; } /** * @return mixed */ public function getCartId() { return $this->_cartId; } /** * @param mixed $elementId * @return $this */ public function setElementId($elementId) { $this->_elementId = $elementId; return $this; } protected function _setMessageTemplate($errorsCount) { $this->_messageTemplates[self::IN_CART] = Qs_Translate::getPlural($this->_messages[self::IN_CART], $errorsCount); return $this; } public function isValid($value, $context = null) { if ($this->getCartId() && $attendeeInCart = $this->_getAttendeesInCart()) { $alreadyAddedToCartUsers = []; if (is_array($context[$this->_elementId])) { foreach ($context[$this->_elementId] as $value) { if ($this->isInCart($value, $attendeeInCart)) { $alreadyAddedToCartUsers[] = $value['_' . $this->_elementId]; } } } else { if ($this->isInCart($context, $attendeeInCart)) { $alreadyAddedToCartUsers[] = $context['_' . $this->_elementId]; } } if ($alreadyAddedToCartUsers) { $this->_setMessageTemplate(count($alreadyAddedToCartUsers)); $this->_error(self::IN_CART, implode(', ', $alreadyAddedToCartUsers)); return false; } } return true; } protected function isInCart($value, $attendeeInCart) { return in_array($value[$this->_elementId], $attendeeInCart); } protected function _getAttendeesInCart() { $select = Qs_Db::getSelect(); $select->from(Qs_Db::getPair('CartItem', 'ci'), ['tsa.userId']); $select->joinLeft(Qs_Db::getPair('TradeShowRegistration', 'tsr'), '`ci`.`productId` = `tsr`.`id`'); $select->joinLeft(Qs_Db::getPair('TradeShowAttendee', 'tsa'), '`tsa`.`registrationId` = `tsr`.`id`'); $select->where('`ci`.`cartId` = ?', $this->getCartId(), Qs_Db::INT_TYPE); $select->where('`ci`.`cartItemType` = ?', CartItemObj::ITEM_TYPE); $select->where('`tsr`.`bought` = ?', 'n'); return Qs_Db::getInstance()->fetchCol($select); } }