userListColumns; } public function setUserListColumns($userListColumns) { $this->userListColumns = $userListColumns; return $this; } public function initFromForm(array $data) { parent::initFromForm($data); if (empty($this->_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['officialFullName'] = Model::getOfficialFullNameDbExpr(); $columns['fullName'] = Model::getFullNameWithNicknameExpr(); return $columns; } protected function _getFromDbColumns() { $columns = parent::_getFromDbColumns(); $columns['officialFullName'] = Model::getOfficialFullNameDbExpr(); $columns['fullName'] = Model::getFullNameWithNicknameExpr(); return $columns; } protected function _addDependenciesFromDb(array &$data) { $this->_addAddressesFromDb($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->joinLeft($this->_getPair('DState', 's'), '`s`.`id` = `ua`.`state`', ['stateTitle' => 'title']); $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)) { $company = $this->_getCompanyData($data['companyId']); $companyAddress = Model::createAddress($data, $company); if ($companyAddress == $addresses[Entity::ADDRESS_BILLING]) { $addresses[Entity::ADDRESS_BILLING]['asCompanyAddress'] = 'y'; } } $data = array_merge($data, $addresses); return $this; } protected function _addListsFromDb(array &$data) { $select = $this->_db->select(); $select->from($this->_getPair('List', 'l'), $this->getUserListColumns()); $select->join( $this->_getPair($this->_tableAlias . 'List', 'ul'), '`ul`.`userId` = ' . $this->_db->quote($data['id'], Qs_Db::INT_TYPE) . ' ' . 'AND `ul`.`listId` = `l`.`id`', [] ); $data['lists'] = 1 === count($this->getUserListColumns()) ? $this->_db->fetchCol($select) : $this->_db->fetchAll($select); return $this; } public function getAddressTypes() { return $this->_addressTypes; } public function getObjectInfo() { $data = (array) $this->getData(); if (!empty($data)) { $data['itemTitle'] = (($data['nickname']) ? $data['nickname'] : $data['firstName']) . (($data['middleInitial']) ? (' ' . $data['middleInitial']) : '') . ' ' . $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') ); $this->_select->joinLeft( $this->_getPair('CompanyType'), '`Company`.`type` = `CompanyType`.`id`', array('companyType' => 'title') ); } return $this->_select; } protected function _filter(Zend_Db_Select $select) { if ($companyTypeId = Qs_Array::get($this->_filter, 'companyTypeId')) { $select->where('`Company`.`type` = ?', $companyTypeId); } return parent::_filter($select); } public static function getAutocompleteColumns($type = null) { $columns = [ 'value' => 'id', 'title' => 'CONCAT_WS(", ", ' . Model::getFullNameWithNicknameExpr() . ', `c`.`name`)' ]; return $columns; } public static function getAutocompleteItemTitle($userId, $type = null) { $columns = self::getAutocompleteColumns($type); unset($columns['value']); $select = Qs_Db::getSelect(); $select->from(Qs_Db::getPair('User'), $columns); $select->joinLeft(Qs_Db::getPair('Company', 'c'), '`c`.`id` = `User`.`companyId`', []); $select->where('`User`.`bought` = "y"'); $select->where('`User`.`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['leadershipPhoto'] = ($data['leadershipPhoto']) ? $data['leadershipPhoto'] : '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) { if (!Qs_Array::get($this->_companyData, (string) $companyId)) { $select = $this->_db->select(); $select->from($this->_getPair('Company', 'cm'), ['*', 'counties' => CompanyModel::getCountiesColumn('cm')]); $select->where('`cm`.`id` = ?', (string) $companyId, Qs_Db::INT_TYPE); $data = $this->_db->fetchRow($select); $this->_prepareCompanyMailingAddress($data); $data['utilityCategoryId'] = $this->_getCompanyUtilityCategories($companyId); $this->_companyData[$companyId] = $data; } return $this->_companyData[$companyId]; } protected function _prepareCompanyMailingAddress(array &$data) { $addressFields = ['address', 'address2', 'city', 'state', 'zip']; $physicalAddress = []; $mailingAddress = []; foreach ($addressFields as $field) { $physicalAddress[] = $data[$field]; $mailingAddress[] = $data['mailing' . ucfirst($field)]; } if ($physicalAddress == $mailingAddress) { $data['asPhysicalAddress'] = 'y'; foreach ($addressFields as $field) { $data['mailing' . ucfirst($field)] = ''; } } return $this; } protected function _getCompanyUtilityCategories($companyId) { $select = $this->_db->select(); $select->from($this->_getPair('CompanyUtilityCategory', 'cuc'), ['utilityCategoryId']); $select->where('`cuc`.`companyId` = ?', (string) $companyId, Qs_Db::INT_TYPE); return $this->_db->fetchCol($select); } public function companyMembers4Select($companyId) { $select = $this->_db->select(); $select->from($this->_getPair(), ['id', 'name' => Model::getFullNameWithNicknameExpr()]); $select->where('`User`.`companyId` = ?', (string) $companyId); $select->where('`User`.`status` = ?', Entity::STATUS_ACTIVE); $select->order('User.lastName'); return Qs_Array::group($this->_db->fetchAll($select), ['id'], []); } /** * @return \Zend_Db_Select */ protected function _getCompanySelect() { $select = $this->_db->select(); $select->from($this->_getPair('Company', 'c'), array('id', 'title' => Model::getCompanyTitleDbExpr())); return $select; } /** * @param string|array $filter * @param bool $activeOnly * @return array */ public function getCompany4Select($filter, $activeOnly = false) { $select = $this->_getCompanySelect(); if ($activeOnly) { $select->where('`c`.`status` = ?', CompanyEntity::STATUS_ACTIVE); } if (is_string($filter)) { $filter['query'] = $filter; } $query = trim($filter['query']); if ($query) { $filterColumns = ['c' => ['name', 'address', 'address2', 'city', 'state', 'zip', 'phone', 'fax']]; Qs_Db_Filter::where($select, $filterColumns, $query); } Qs_Db::filter($select, $filter, 'c'); $select->order('c.name'); $select->limit('30'); return $this->_db->fetchPairs($select); } public function prepareEmailData(array &$data) { if ($data['companyId']) { $data['company'] = $this->_getCompanyData($data['companyId']); } else { $fields = ['name', 'address', 'address2', 'city', 'state', 'zip', 'email', 'phone', 'fax']; $data['company'] = array_combine($fields, array_fill(0, sizeof($fields), '')); } $data = Qs_Array::flattenAssoc($data, true); array_walk($data, function (&$item) { $item = ('' == $item) ? '-' : $item; }); return $this; } protected function isAssociationMode() { return (Module::MODE_ASSOCIATION == $this->getConfig('mode')); } protected function _updateAddresses() { $this->_deleteAddresses(); $this->_insertAddresses(); 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; } 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('asCompanyAddress', $address)) { if ('y' == $address['asCompanyAddress']) { $company = $this->_getCompanyData($this->_getTable()->search($this->getPrimaryKey(), 'companyId')); $address = Model::createAddress($this->_data, $company); } else { unset($address['asCompanyAddress']); } } if (!empty($address) && array_filter($address)) { $address['userId'] = $this->getPrimaryKey(); $address['type'] = $type; $this->_getTable($this->_tableAlias . 'Address')->insert($address); } return $this; } public function getCompanyJobTitles4Select($companyId) { $select = $this->_db->select(); $select->distinct()->from($this->_getPair('User', 'u'), ['jobTitle']); $select->where('`u`.`companyId` = ?', (int) $companyId); $select->where('`u`.`jobTitle` != ""'); $select->order('u.jobTitle'); $list = $this->_db->fetchCol($select); return array_combine($list, $list); } }