_select) { parent::getListSelect(); $this->_joinStatus($this->_select); $this->_joinUser($this->_select); $this->_joinPaymentType($this->_select); } return $this->_select; } protected function _joinStatus(Zend_Db_Select $select) { $select->join( $this->_getPair('EventAttendeeStatus', 'eas'), 'eas.id = ea.status', ['statusTitle' => 'title'] ); } protected function _joinUser(Zend_Db_Select $select) { $select->joinLeft( $this->_getPair('User', 'u'), '`u`.`id` = `ea`.`userId`', ['memberId' => 'u.id'] ); return $this; } protected function _joinPaymentType(Zend_Db_Select $select) { $select->joinLeft( $this->_getPair('PaymentType', 'pt'), '`pt`.`id` = `ea`.`paymentType`', ['paymentTypeTitle' => 'title'] ); return $this; } protected function _addDependenciesFromDb(array &$data) { parent::_addDependenciesFromDb($data); // prepare data for edit form $attendee = [ 'type' => $data['type'], 'mode' => AttendeeitemForm::MODE_CHOOSE_TYPE, ]; if (AttendeeForm::TYPE_MEMBER === $data['type']) { $attendee['id'] = $data['userId']; } if (empty($attendee['id'])) { // removed member or non member $attendee['firstName'] = $data['firstName']; $attendee['lastName'] = $data['lastName']; $attendee['email'] = $data['email']; $attendee['company'] = $data['company']; } $data['attendee'] = [0 => $attendee]; return $this; } public function getPaymentMethods4Select() { $select = $this->_db->select(); $select->from($this->_getPair('PaymentType'), ['id', 'title']); $select->where('FIND_IN_SET("admin", showFor)'); $select->order('sorter'); return $this->_db->fetchPairs($select); } /** * Returns a list of events to which specified user be registered * * @param $userId * * @return array */ public function getAvailableEvents4Select($userId) { $db = Qs_Db::getInstance(); $user = $this->_getUserInfoList([$userId])[$userId]; $_userId = (int) $userId; $_firstName = $db->quote($user['firstName']); $_lastName = $db->quote($user['lastName']); $attendeeCheck = Qs_Db::getSelect(); $attendeeCheck->from(Qs_Db::getPair('EventAttendee', 'ea'), ['found' => new Zend_Db_Expr('1')]); $attendeeCheck->where("ea.eventId = e.id"); $attendeeCheck->where("ea.userId = {$_userId} OR (ea.firstName = {$_firstName} AND ea.lastName = {$_lastName})"); $attendeeCheck->limit(1); $select = Qs_Db::getSelect(); $select->from(Qs_Db::getPair('Event', 'e'), ['id', 'title']); $select->where(Model::getRegistrationAllowedExpr()); $select->where('NOT EXISTS(' . $attendeeCheck . ')'); return $db->fetchPairs($select); } public function prepareSignupFormData(array $event, array $data) { $amount = (float) $data['amount']; $result = parent::prepareSignupFormData($event, $data); $result['total'] = bcmul($amount, count($result['attendee'])); foreach ($result['attendee'] as &$attendee) { $attendee['amount'] = $amount; } return $result; } /** * @param array $data * [ * 'total' => float, * 'attendee' => [ * [ * 'teamId' => int, * 'userId' => int, * 'firstName' => string, * 'lastName' => string, * 'email' => string, * 'amount' => string, * ] * ], * * ] * @throws \Exception * @throws \Qs_Exception_EmptyPropertyException * @return int|null */ public function insert(array $data = null) { $data = (null === $data) ? $this->_data : $data; // check eventId $this->getEventId(); if (!isset($data['total']) || empty($data['attendee'])) { throw new Exception('Not enough data'); } $admin = null; $adminAuth = App_Admin_Auth::getInstance(); if ($adminAuth->isLoggedIn()) { $admin = $adminAuth->getData(); } $this->_clearErrors(); $this->_db->beginTransaction(); try { $event = $this->getEventData(); $attendees = (array) $data['attendee']; $attendeeTable = $this->_getTable('EventAttendee'); foreach ($attendees as &$attendee) { $attendee['eventId'] = $event['id']; $attendee['cartId'] = null; $attendee['createdBy'] = Entity::CREATED_BY_ADMIN; $attendee['createdByName'] = $admin ? $admin['firstName'] . ' ' . $admin['lastName'] : null; $attendee['bought'] = 'y'; $attendee['paymentType'] = $data['paymentType']; $attendee['paymentDate'] = $data['paymentDate']; $attendee['registrationDate'] = date('Y-m-d H:i:s'); $attendee['amount'] = (empty($data['paymentType'])) ? null : $attendee['amount']; $attendeeTable->insert($attendee); } $this->_insertDependency(); $this->_db->commit(); } catch (Exception $e) { $this->_db->rollBack(); Qs_Debug::processException($e); return false; } return $this->getPrimaryKey(); } public function update(array $data = null) { $data = (null === $data) ? $this->_data : $data; unset($data['id']); if (!isset($data['total']) || empty($data['attendee'])) { throw new Exception('Not enough data'); } if (count($data['attendee']) > 1) { throw new Exception('Update method can save only with single attendee'); } $this->_clearErrors(); $this->_db->beginTransaction(); try { // _ve($data); $attendee = (array) $data['attendee']; $attendee = reset($attendee); $data['amount'] = (empty($data['paymentType'])) ? null : $attendee['amount']; $attendeeFields = [ 'userId' => null, 'firstName' => null, 'lastName' => null, 'company' => null, 'email' => null, ]; $data = array_merge( $data, array_intersect_key($attendee, $attendeeFields) ); $this->_getTable()->update($data, ['id = ?' => $this->getPrimaryKey()]); $this->_updateDependency(); $this->_db->commit(); } catch (Exception $e) { $this->_db->rollBack(); Qs_Debug::processException($e); return false; } return $this->getPrimaryKey(); } public function setUserId($userId) { $this->_userId = $userId; return $this; } public function getUserId() { if (null == $this->_userId) { throw new Qs_Exception_EmptyPropertyException($this, '_userId'); } return $this->_userId; } public function hasUserId() { return (bool) $this->_userId; } public function setStatus($status) { $this->_clearErrors(); if (!defined('\App\Event\Attendee\Entity::STATUS_' . strtoupper($status))) { $this->_addError('Invalid status'); return false; } $this->_getTable()->updateByKey(compact('status'), $this->_primaryKey); return true; } public function setAllAttended() { $this->_getTable()->update( ['changed' => date('Y-m-d'), 'status' => Entity::STATUS_ATTENDED], $this->getWhere() . ' AND `eventId` = ' . $this->_db->quote($this->getEventId(), Qs_Db::INT_TYPE) ); return $this; } public function getExportCsvStatement() { return $this->getListSelect()->query(); } public function getRegistrationList() { $select = $this->getListSelect(); $select->reset(Zend_Db_Select::COLUMNS)->columns( [ 'ea.firstName', 'ea.lastName', 'company' => 'IF("" = IFNULL(ea.company, ""), "No company", ea.company)', 'companySorter' => 'IF("" = IFNULL(ea.company, ""), "ZZZ", ea.company)', ] ); $this->_joinUserType($select); Qs_Db::filter($select, ['status' => Entity::STATUS_ENROLLED], 'ea'); $select->order(['userTypeSorter', 'companySorter', 'lastName', 'firstName']); return $this->_db->fetchAll($select); } public function getSignInList() { $select = $this->getListSelect(); $select->reset(Zend_Db_Select::COLUMNS)->columns(['ea.firstName', 'ea.lastName', 'ea.company', 'email']); $select->order(['lastName', 'firstName', 'company']); return $this->_db->fetchAll($select); } public function getAttendees4NameBadges() { $select = $this->getListSelect(); $select->reset(Zend_Db_Select::COLUMNS)->columns(['ea.firstName', 'ea.lastName', 'ea.company']); Qs_Db::filter($select, ['status' => Entity::STATUS_ENROLLED], 'ea'); $select->order(['lastName', 'firstName', 'company']); return $this->_db->fetchAll($select); } protected function _joinUserType(Zend_Db_Select $select) { $select->joinLeft( $this->_getPair('MembershipType', 'mt'), '`mt`.`id` = `u`.`membershipTypeId`', [ 'userType' => 'IFNULL(`mt`.`title`, "Nonmember")', 'userTypeSorter' => 'IFNULL(`mt`.`sorter`, ~0 >> 32)' ] ); return $this; } }