where($select, 'ea'); $select->where('`ea`.`eventId` = ?', $this->getEventId(), Qs_Db::INT_TYPE); return $select; } protected function _getFromDbColumns() { $columns = parent::_getFromDbColumns(); $columns['name'] = Model::getAttendeeNameExpr(); return $columns; } protected function _getFromColumns() { $columns = parent::_getFromColumns(); $columns['name'] = Model::getAttendeeNameExpr(); return $columns; } public function getListSelect() { if (null == $this->_select) { parent::getListSelect(); $this->where($this->_select, 'ea'); $this->_select->where('`ea`.`eventId` = ?', $this->getEventId(), Qs_Db::INT_TYPE); } return $this->_select; } /** * Condition also matches attendees just added to shopping cart (bought === NULL) * @param \Zend_Db_Select $select * @param string $alias EventAttendee table alias * @return $this */ public function where(Zend_Db_Select $select, $alias) { $select->where(Qs_Db::getWhereSql($this->getWhere($alias))); return $this; } /** * filter only bought attendees * @param string $alias EventAttendee table alias * @return array */ public function getWhere($alias = null) { $where = "`bought` = 'y'"; if (null !== $alias) { $where = "{$alias}." . $where; } return $where; } protected function _getRegisteredAttendeesSelect() { $select = $this->_db->select(); $select->from( $this->_getPair(), [ 'userId', 'email', 'name' => Model::getAttendeeNameExpr(), ] ); $select->where('`ea`.`eventId` = ?', $this->getEventId(), Qs_Db::INT_TYPE); return $select; } /** * Returns list of attendees registered on current event * @return array [[userId, email], ...] */ public function getRegisteredAttendees() { $select = $this->_getRegisteredAttendeesSelect(); $this->where($select, 'ea'); // exclude current attendee (for unique validation) if ($this->hasPrimaryKey()) { $select->where('ea.id != ?', $this->getPrimaryKey(), Qs_Db::INT_TYPE); } return $this->_db->fetchAll($select); } /** * Returns list of bought attendees * @return array [[userId, email], ...] */ public function getBoughtAttendees() { $select = $this->_getRegisteredAttendeesSelect(); $select->where('ea.bought = "y"'); return $this->_db->fetchAll($select); } public function getAttendeeRegistered($userId, $userEmail) { $select = $this->_db->select(); $select->from($this->_getPair(), [new Zend_Db_Expr('1')]); $select->where('`ea`.`eventId` = ?', $this->getEventId(), Qs_Db::INT_TYPE); $select->where( $this->_db->quoteInto('`ea`.`userId` = ?', $userId, Qs_Db::INT_TYPE) . $this->_db->quoteInto(' OR `ea`.`email` = ?', $userEmail) ); $this->where($select, 'ea'); return (bool) $this->_db->fetchOne($select); } public function setEventId($eventId) { $this->_eventId = (int) $eventId; $this->_eventObj = null; $this->_eventData = null; return $this; } public function getEventId() { if (null == $this->_eventId) { throw new Qs_Exception_EmptyPropertyException($this, '_eventId'); } return $this->_eventId; } /** * @param array $event * @param array $data * [ * 'attendType' => 'myself' | 'myselfOther' | 'other', * 'attendee' => [ * [ * 'mode' => 'current' | 'choose', * 'type' => 'member' | 'nonmember', * 'id' => null|int '', * 'firstName' => null|string '', * 'lastName' => null|string '', * 'company' => null|string '', * 'email' => null|string '', * ] * ] * * ] * @throws Exception * @throws Qs_Exception_EmptyPropertyException * @return array */ public function prepareSignupFormData(array $event, array $data) { if (EventEntity::TYPE_REGISTRATION !== $event['type']) { throw new Exception('Event with type "' . $event['type'] . '" does not support registration'); } $result = $data; $memberPrice = (float) $event['memberPrice']; $nonmemberPrice = (float) $event['nonmemberPrice']; // prepare attendees data, calculate total $userInfoList = null; if (($userIds = array_filter(Qs_Array::fetchColAll($data['attendee'], 'id')))) { $userInfoList = $this->_getUserInfoList($userIds); } $total = 0; $attendees = []; foreach ($data['attendee'] as $idx => $row) { $attendee = [ 'eventId' => $event['id'], 'type' => $row['type'], 'userId' => null, 'firstName' => null, 'lastName' => null, 'company' => null, 'email' => null, 'amount' => null, 'bought' => null, ]; if (AttendeeItemForm::MODE_CURRENT_USER === $row['mode']) { $attendee['createdBy'] = Entity::CREATED_BY_ME; } else { $attendee['createdBy'] = Entity::CREATED_BY_ANOTHER_MEMBER; } if (AttendeeForm::TYPE_MEMBER === $row['type']) { $total = bcadd($total, $memberPrice); $attendee['amount'] = $memberPrice; if (empty($row['id'])) { // edit deleted member unset( $attendee['userId'], $attendee['firstName'], $attendee['lastName'], $attendee['company'], $attendee['email'] ); } else { $userId = $row['id']; $attendee['userId'] = $userId; if (array_key_exists($userId, $userInfoList)) { $userInfo = $userInfoList[$userId]; $attendee['firstName'] = $userInfo['firstName']; $attendee['lastName'] = $userInfo['lastName']; $attendee['company'] = $userInfo['company']; $attendee['email'] = $userInfo['email']; } } } elseif (AttendeeForm::TYPE_NONMEMBER === $row['type']) { $total = bcadd($total, $nonmemberPrice); $attendee['amount'] = $nonmemberPrice; $attendee['firstName'] = $row['firstName']; $attendee['lastName'] = $row['lastName']; $attendee['company'] = $row['company']; $attendee['email'] = $row['email']; } else { continue; } $attendees[$idx] = $attendee; } $result['attendee'] = $attendees; $result['total'] = $total; return $result; } /** * @param $userIds * @return array [userId => ['firstName', 'lastName', 'email'], ...] */ protected function _getUserInfoList(array $userIds) { if (empty($userIds)) { return []; } $select = $this->_db->select(); $select->from($this->_getPair('User', 'u'), ['id', 'firstName', 'lastName', 'email']); $select->joinLeft($this->_getPair('Company', 'c'), 'c.id = u.companyId', ['company' => 'name']); $select->where('`u`.`id` IN(?)', $userIds); return $this->_db->fetchAll($select, [], Qs_Db::FETCH_GROUP | Qs_Db::FETCH_UNIQUE); } public function getEventData($field = null, $default = null) { if (null === $this->_eventData) { $this->_eventData = $this->_getEventFromDb($this->getEventId()); } return (null === $field) ? $this->_eventData : Qs_Array::get($this->_eventData, $field, $default); } protected function _getEventFromDb($eventId) { $select = $this->_db->select(); $select->from($this->_getPair('Event', 'e'), ['*']); $select->where('e.id = ?', $eventId, Qs_Db::INT_TYPE); return $this->_db->fetchRow($select); } public function getEventObj() { if (null === $this->_eventObj) { $this->_eventObj = new EventObj(['primaryKey' => $this->getEventId()]); } return $this->_eventObj; } public function initFromForm(array $data) { parent::initFromForm($data); $this->_data = $this->prepareSignupFormData($this->getEventData(), $this->_data); return $this; } /** * Returns email to userId map for specified emails * @param array $names * @return array Map [$name => $userId, ..] */ public function getUsersIds(array $names) { if (empty($names)) { return []; } $select = $this->_db->select(); $select->from($this->_getPair('User', 'u'), ['name' => 'CONCAT_WS(" ", firstName, lastName)', 'id']); $select->where('status = ?', UserEntity::STATUS_ACTIVE); $select->where('CONCAT_WS(" ", firstName, lastName) IN(?)', $names); return $this->_db->fetchAll($select, [], PDO::FETCH_KEY_PAIR); } /** * Returns userId to email to map for specified ids * @param array $userIds * @return array Map [$userId => $name, ..] */ public function getUsersNames(array $userIds) { if (empty($userIds)) { return []; } $select = $this->_db->select(); $select->from($this->_getPair('User', 'u'), ['id', 'name' => 'CONCAT_WS(" ", firstName, lastName)']); $select->where('`u`.`id` IN(?)', $userIds); return $this->_db->fetchAll($select, [], PDO::FETCH_KEY_PAIR); } protected function _getCartItemDescription($attendee) { static $pattern; if (null === $pattern) { $pattern = $this->getConfig('cartItemDescription'); } return str_replace( '{attendeeName}', $attendee['firstName'] . ' ' . $attendee['lastName'], $pattern ); } }