_data['password'])) { unset($this->_data['password']); } else { $this->_data['salt'] = substr(md5(time()), 0, 8); $this->_data['password'] = $this->_encryptPass($this->_data['password'], $this->_data['salt']); } return $this; } protected function _getFromColumns() { $columns = parent::_getFromColumns(); $columns['fullName'] = Model::getFullNameDbExpr(); return $columns; } protected function _getFromDbColumns() { $columns = parent::_getFromDbColumns(); $columns['fullName'] = Model::getFullNameDbExpr(); return $columns; } protected function _addDependenciesFromDb(array &$data) { $this->_addAddressesFromDb($data); $this->_addCommitteesFromDb($data); $this->_addListsFromDb($data); return $this; } protected function _addAddressesFromDb(array &$data) { $select = $this->_db->select(); $columns = ['type', 'firstName', 'lastName', 'address', 'address2', 'city', 'state', 'zip']; $select->from($this->_getPair($this->_tableAlias . 'Address', 'ua'), $columns); $select->where('`ua`.`userId` = ?', $data['id'], Qs_Db::INT_TYPE); $addresses = $this->_db->fetchAssoc($select); foreach ($addresses as &$item) { unset($item['type']); if (empty($item['email'])) { $item['email'] = $data['email']; } if (empty($item['phone'])) { $item['phone'] = $data['directPhone']; } } unset($item); if (array_key_exists(Entity::ADDRESS_BILLING, $addresses) && array_key_exists(Entity::ADDRESS_SHIPPING, $addresses) ) { if ($addresses[Entity::ADDRESS_BILLING] == $addresses[Entity::ADDRESS_SHIPPING]) { $addresses[Entity::ADDRESS_SHIPPING] = ['asBilling' => 'y']; } } $data = array_merge($data, $addresses); return $this; } protected function _addCommitteesFromDb(array &$data) { $select = $this->_db->select(); $select->from($this->_getPair($this->_tableAlias . 'Committee', 'uc'), array('committeeId', 'positionId')); $select->join($this->_getPair('Committee', 'c'), '`c`.`id` = `uc`.`committeeId`', array()); $select->where('`uc`.`userId` = ?', $data['id'], Qs_Db::INT_TYPE); $select->order('c.sorter'); $data['committees'] = $this->_db->fetchAll($select); return $this; } protected function _addListsFromDb(array &$data) { $select = $this->_db->select(); $select->from($this->_getPair($this->_tableAlias . 'List', 'ul'), array('listId')); $select->where('`ul`.`userId` = ?', $data['id'], Qs_Db::INT_TYPE); $data['lists'] = $this->_db->fetchCol($select); return $this; } protected function _insertDependency() { $this->_insertAddresses(); return $this; } protected function _updateDependency() { $this->_deleteDependency(); $this->_insertDependency(); return $this; } protected function _deleteDependency() { $this->_deleteAddresses(); return $this; } protected function _insertAddresses() { $types = $this->getAddressTypes(); foreach ($types as $type) { $this->_insertAddress($type); } return $this; } protected function _insertAddress($type) { $address = Qs_Array::get($this->_data, (string) $type, []); if (array_key_exists('asBilling', $address)) { if ('y' == $address['asBilling']) { $address = Qs_Array::get($this->_data, Entity::ADDRESS_BILLING); } else { unset($address['asBilling']); } } if (!empty($address) && array_filter($address)) { $address['userId'] = $this->getPrimaryKey(); $address['type'] = $type; $this->_getTable($this->_tableAlias . 'Address')->insert($address); } return $this; } protected function _deleteAddresses() { $types = $this->getAddressTypes(); foreach ($types as $type) { $this->_deleteAddress($type); } return $this; } protected function _deleteAddress($type) { $where[] = $this->_db->quoteInto('`userId` = ?', $this->getPrimaryKey(), Qs_Db::INT_TYPE); $where[] = $this->_db->quoteInto('`type` = ?', (string) $type); $this->_getTable($this->_tableAlias . 'Address')->delete($where); return $this; } public function getAddressTypes() { return $this->_addressTypes; } public function getObjectInfo() { $data = (array) $this->getData(); if (!empty($data)) { $data['itemTitle'] = $data['firstName'] . ' ' . $data['lastName']; } return $data; } public function getListSelect() { if (null === $this->_select) { parent::getListSelect(); $this->_select->joinLeft( $this->_getPair('Company'), '`Company`.`id` = `' . $this->_tableAlias . '`.`companyId`', array('companyName' => 'name') ); } return $this->_select; } public static function getAutocompleteColumns() { return array( 'value' => 'id', 'title' => Model::getFullNameDbExpr() ); } protected function _getAutocompleteSelect($type = null) { $select = $this->_db->select(); $select->from($this->_getPair('User'), self::getAutocompleteColumns()); $select->where('User.`bought` = "y"'); $select->limit(10); if ('event' === $type) { $select->where('User.`status` = "active"'); $auth = UserAuth::getInstance(); if ($auth->isLoggedIn()) { $select->where('User.id != ?', $auth->getData('id'), Qs_Db::INT_TYPE); } } return $select; } public function getAutocomplete($term, $type = null) { $select = $this->_getAutocompleteSelect($type); $select->where('`firstName` LIKE ? OR `lastName` LIKE ?', '%' . $term . '%'); return $this->_db->fetchAll($select); } public static function getAutocompleteItemTitle($userId) { $columns = self::getAutocompleteColumns(); unset($columns['value']); $select = Qs_Db::getSelect(); $select->from(Qs_Db::getPair('User'), $columns); $select->where('`bought` = "y"'); $select->where('id = ?', $userId, Qs_Db::INT_TYPE); $select->limit(1); return Qs_Db::getInstance()->fetchOne($select); } public function getDataForView() { $data = $this->getData(); if (!$data) { return false; } if ($this->isAssociationMode()) { $data['photo'] = ($data['photo']) ? $data['photo'] : 'images/member-no-photo.png'; $data['company'] = $this->_getCompanyData($data['companyId']); } if ('y' == Qs_Array::get($data, 'shipping[asBilling]')) { $data['shipping'] = $data['billing']; $data['shipping']['asBilling'] = 'y'; unset($data['shipping']['email'], $data['shipping']['phone']); } return $data; } protected function _getCompanyData($companyId) { $select = $this->_db->select(); $select->from($this->_getPair('Company'))->where('`Company`.`id` = ?', (string) $companyId, Qs_Db::INT_TYPE); $data = $this->_db->fetchRow($select); return $data; } protected function isAssociationMode() { return (Module::MODE_ASSOCIATION == $this->getConfig('mode')); } }