_tradeShowId = (int) $tradeShowId; $this->_tradeShowObj = null; $this->_tradeShowData = null; return $this; } public function getTradeShowId() { if (null == $this->_tradeShowId) { throw new Qs_Exception_EmptyPropertyException($this, '_tradeShowId'); } return $this->_tradeShowId; } public function getTradeShowData($field = null, $default = null) { if (null === $this->_tradeShowData) { $this->_tradeShowData = $this->_getTradeShowFromDb($this->getTradeShowId()); } return (null === $field) ? $this->_tradeShowData : Qs_Array::get($this->_tradeShowData, $field, $default); } protected function _getTradeShowFromDb($tradeShowId) { $select = $this->_db->select(); $select->from($this->_getPair('TradeShow', 'ts'), ['*']); $select->where('ts.id = ?', $tradeShowId, Qs_Db::INT_TYPE); return $this->_db->fetchRow($select); } public function getTradeShowObj() { if (null === $this->_tradeShowObj) { $this->_tradeShowObj = new TradeShowObj(['primaryKey' => $this->getTradeShowId()]); } return $this->_tradeShowObj; } protected function _insertAttendeeData(array $data) { if (is_array($data['attendeeId'])) { $attendeeList = $this->_prepareAttendeeList($data['attendeeId']); } else { $attendeeList = $this->_prepareSingleAttendee($data); } foreach ($attendeeList as $attendee) { $this->_getTable('TradeShowAttendee')->insert([ 'registrationId' => $this->_primaryKey, 'userId' => $attendee['attendeeId'], 'companyName' => $attendee['companyName'], 'firstName' => $attendee['firstName'], 'nickname' => $attendee['nickname'], 'lastName' => $attendee['lastName'], 'email' => $attendee['email'], ]); } return $this; } protected function _prepareAttendeeList($attendeeList) { $result = []; foreach ($attendeeList as $attendee) { if ($attendee['attendeeId']) { /* set by Autocomplite */ $userData = $this->_getUserInfoList($attendee['attendeeId']); } else { /* set Manually */ $userData = $this->_parseUserInfo($attendee['_attendeeId']); } $result[] = $userData; } return $result; } protected function _prepareSingleAttendee($data) { $result = []; if ($data['attendeeId']) { /* set by Autocomplite */ $result[] = $this->_getUserInfoList($data['attendeeId']); } else { /* set Manually */ $result[] = $this->_parseUserInfo($data['_attendeeId']); } return $result; } /** * @param int $userId * @return array */ protected function _getUserInfoList($userId) { $select = $this->_db->select(); $select->from($this->_getPair('User', 'u'), ['attendeeId' => 'id', 'firstName', 'nickname', 'lastName', 'email']); $select->joinLeft($this->_getPair('Company', 'c'), 'c.id = u.companyId', ['companyName' => 'name']); $select->where('`u`.`id` = ? ', $userId, \Qs_Db::INT_TYPE); return $this->_db->fetchRow($select); } /** * Parse user firstName and lastName * @param string $userNameString * @return array */ protected function _parseUserInfo($userNameString) { $matches = []; $userInfo = []; if (preg_match('/(?^[a-zA-Z.-]+)\s(?[a-zA-Z.-\s]+)/', $userNameString, $matches)) { $userInfo = [ 'userId' => null, 'companyName' => null, 'firstName' => $matches['firstName'], 'nickname' => null, 'lastName' => $matches['lastName'] ]; } return $userInfo; } /** * @param string|null $value allowed values: 'y', null * @return int Return count updated rows * @throws Qs_Exception */ public function setBought($value = 'y') { if (!in_array($value, array('y', null))) { throw new Qs_Exception('Incorrect "bought" value.'); } if (!$this->getPrimaryKey()) { throw new Qs_Exception('Primary key is not defined.'); } return $this->_getTable()->updateByKey(array('bought' => $value), $this->getPrimaryKey()); } /** * @param array $data * Example: array('paymentDate' => '2014-05-29', 'paymentType' => 'cash') * @return int Return count updated rows * @throws Qs_Exception */ public function setPaymentInfo($data) { if (!array_key_exists('paymentDate', $data) || !$data['paymentDate']) { throw new Qs_Exception('Incorrect "paymentDate" value.'); } if (!array_key_exists('paymentType', $data) || !$data['paymentType']) { throw new Qs_Exception('Incorrect "paymentType" value.'); } if (!$this->getPrimaryKey()) { throw new Qs_Exception('Primary key is not defined.'); } return $this->_getTable()->updateByKey( array( 'paymentDate' => $data['paymentDate'], 'paymentType' => $data['paymentType'], 'registrationDate' => date('Y-m-d H:i:s') ), $this->getPrimaryKey() ); } }