where($select, 'ea'); $select->where('`ea`.`eventId` = ?', $this->getEventId(), \Qs_Db::INT_TYPE); return $select; } 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 (approved === 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; } /** * @param string $alias EventAttendee table alias * @return array */ public function getWhere($alias) { $where = array("{$alias}.`active` = 'y'"); if (self::MODE_APPROVED === $this->getMode()) { // filter only approved attendees $where[] = "{$alias}.`approved` = 'y'"; } else { // self::MODE_ALL // filter approved and non approved attendees of current user $inCartExpr = $this->_getInCartExpr($alias); $where[] = new \Zend_Db_Expr( "{$alias}.`approved` = 'y' OR ({$alias}.`approved` IS NULL AND EXISTS({$inCartExpr}))" ); } return $where; } /** * @param string $alias EventAttendee table alias * @return \Zend_Db_Expr */ protected function _getInCartExpr($alias) { $_cartId = (int) $this->getCartId(); $sql = "SELECT 1 FROM `apw_Cart` `c` " . "WHERE `c`.`id` = {$alias}.`cartId` " . " AND `c`.`id` = {$_cartId} " . "LIMIT 1"; return new Zend_Db_Expr($sql); } /** * Returns list of attendees registered on current event * @return array [[userId, email], ...] */ public function getRegisteredAttendees() { $select = $this->_db->select(); $select->from( $this->_getPair(), array( 'userId', 'email' => $this->_getAttendeeEmailExpr(), 'name' => $this->_getAttendeeNameExpr(), ) ); $select->joinLeft($this->_getPair('User', 'u'), '`u`.`id` = `ea`.`userId`', array()); $select->where('`ea`.`eventId` = ?', $this->getEventId(), Qs_Db::INT_TYPE); $this->where($select, 'ea'); return $this->_db->fetchAll($select); } public function getAttendeeRegistered($userId, $userEmail) { $select = $this->_db->select(); $select->from($this->_getPair(), array(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) . ' OR ' . $this->_db->quoteInto('`ea`.`email` = ?', $userEmail) ); $this->where($select, 'ea'); return (bool) $this->_db->fetchOne($select); } /** * @param int|string $teamId teamId or teamId field * @return \Zend_Db_Expr */ public function getTeamAttendeeCountExpr($teamId) { $_eventId = (int) $this->getEventId(); $_teamId = (is_numeric($teamId)) ? (int) $teamId : $this->_db->quoteIdentifier($teamId); $where = $this->getWhere('ea'); $where[] = 'eventId = ' . (int) $_eventId; $where[] = 'teamId = ' . $_teamId; return \Qs_Db::getCountExpr(array('EventAttendee', 'ea'), $where); } /** * @param string $eventAlias Event table alias * @return \Zend_Db_Expr */ public function getEventRegisteredCountExpr($eventAlias) { $_event = $this->_db->quoteIdentifier($eventAlias); $_regular = $this->_db->quote(EventObj::TYPE_REGULAR); $_group = $this->_db->quote(EventObj::TYPE_GROUP); $where = $this->getWhere('ea'); $where[] = "ea.`eventId` = {$_event}.id"; $_expr = \Qs_Db::getCountExpr(array('EventAttendee', 'ea'), $where); return new \Zend_Db_Expr( "IF({$_event}.`type` IN({$_regular}, {$_group}) " . "AND (ISNULL({$_event}.`registrationUrl`) OR {$_event}.`registrationUrl` = ''), " . "{$_expr}, NULL)" ); } public function getTeamGroupedAttendees($parentAttendeeId) { $select = $this->_db->select(); $select->from( $this->_getPair('EventAttendee', 'ea'), array('key' => 'teamId', 'id', 'eventId', 'cartId', 'userId', 'parentAttendeeId', 'teamId', 'firstName', 'lastName', 'email', 'amount', 'approved') ); $select->joinleft( $this->_getPair('EventTeam', 'et'), '`et`.`id` = `ea`.`teamId`', array('teamName' => 'title') ); $select->where('parentAttendeeId = ?', $parentAttendeeId, Qs_Db::INT_TYPE); $this->where($select, 'ea'); return $this->_db->fetchAll($select, array(), Qs_Db::FETCH_GROUP); } protected function _getAttendeeNameExpr() { return new \Zend_Db_Expr( 'IF(u.id, CONCAT_WS(" ", u.firstName, u.lastName), CONCAT_WS(" ", ea.firstName, ea.lastName))' ); } protected function _getAttendeeEmailExpr() { return new \Zend_Db_Expr( 'IF(u.id, u.email, ea.email)' ); } public function delete() { $this->_clearErrors(); if (null === $this->getData()) { $this->_addError(static::MSG_INVALID_RECORD_ID); return false; } $this->_deleteDependency(); $this->_deleteFiles(); return $this->_getTable()->updateByKey(array('active' => new \Zend_Db_Expr('NULL')), $this->getPrimaryKey()); } public function setEventId($eventId) { $this->_eventId = (int) $eventId; return $this; } public function getEventId() { if (null == $this->_eventId) { throw new \Qs_Exception_EmptyPropertyException($this, '_eventId'); } return $this->_eventId; } public function setMode($mode) { if (self::MODE_ALL !== $mode && self::MODE_APPROVED !== $mode) { throw new Exception('Unknown mode "' . $mode . '"'); } $this->_mode = $mode; return $this; } public function getMode() { if (null == $this->_mode) { throw new \Qs_Exception_EmptyPropertyException($this, '_mode'); } return $this->_mode; } public function setCartId($cartId) { $this->_cartId = $cartId; return $this; } public function getCartId() { if (null == $this->_cartId) { throw new \Qs_Exception_EmptyPropertyException($this, '_cartId'); } return $this->_cartId; } }