_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']); } if (array_key_exists('securityAnswer', $this->_data)) { if (empty($this->_data['securityAnswer'])) { unset($this->_data['securityAnswer']); } else { $this->_data['securityAnswer'] = $this->prepareSecurityAnswer($this->_data['securityAnswer']); } } return $this; } protected function _getFromColumns() { $columns = parent::_getFromColumns(); $columns['fullName'] = $this->getModel()->getFullNameExpr(); return $columns; } protected function _getFromDbColumns() { $columns = parent::_getFromDbColumns(); $columns['fullName'] = $this->getModel()->getFullNameExpr(); return $columns; } protected function _addDependenciesFromDb(array &$data) { $this->_addAddressesFromDb($data); $this->_addSocialLinks($data); return $this; } protected function _addSocialLinks(&$data) { $ownSocialLinks = $this->_getOwnSocialLinks(); if ($ownSocialLinks) { $data['socialLinks'] = $ownSocialLinks; } 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['phone']; } } unset($item); if (array_key_exists(Entity::ADDRESS_BILLING, $addresses) && array_key_exists(Entity::ADDRESS_SHIPPING, $addresses) ) { $billing = $addresses[Entity::ADDRESS_BILLING]; $shipping = $addresses[Entity::ADDRESS_SHIPPING]; if (empty($shipping['firstName']) && empty($shipping['lastName'])) { $billing['firstName'] = $shipping['firstName']; $billing['lastName'] = $shipping['lastName']; } if ($billing == $shipping) { $addresses[Entity::ADDRESS_SHIPPING]['sameAs'] = Entity::ADDRESS_BILLING; } } $data = array_merge($data, $addresses); 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 getDataForView() { $data = $this->getData(); if (!$data) { return false; } $data['photoH'] = ($data['photoH']) ? $data['photoH'] : 'images/member-no-photo.png'; $data['photoV'] = ($data['photoV']) ? $data['photoV'] : 'images/member-no-photo.png'; 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; } /** * returns social links from dictionary * * @return array */ public static function getSocialLinks() { $select = Qs_Db::getSelect(); $select->from(Qs_Db::getPair('SocialLinkDictionary')); $select->where('`show` = "y"'); $select->order('sorter'); return Qs_Db::getInstance()->fetchAll($select); } protected function _prepareList(&$list) { $this->_addSocialLinksToList($list); return parent::_prepareList($list); } protected function _addSocialLinksToList(&$list) { $select = $this->_db->select(); $select->from($this->_getPair('UserSocialLink', 'usl'), ['userId', 'link']) ->join( $this->_getPair('SocialLinkDictionary', 'sld'), '`sld`.`id` = `usl`.`linkId`', ['iconName', 'serviceName'] ) ->order('sld.sorter'); $links = $this->_db->fetchAll($select, [], \Qs_Db::FETCH_GROUP); foreach ($list as &$user) { if (isset($links[$user['id']])) { $user['socialLinks'] = $links[$user['id']]; } } unset($user); return $this; } /** * return current member social links * * @return array */ protected function _getOwnSocialLinks() { $select = $this->_db->select(); $select->from($this->_getPair('UserSocialLink', 'usl'), ['link', 'linkId']); $select->where('`userId` = ?', $this->_primaryKey, Qs_Db::INT_TYPE); $select->join( $this->_getPair('SocialLinkDictionary', 'sld'), '`sld`.`id` = `usl`.`linkId`', ['iconName', 'serviceName'] ); $select->order('sld.sorter'); return $this->_db->fetchAll($select); } protected function _saveSocialLinkDependency() { $socialLinks = self::getSocialLinks(); $table = new Qs_Db_Table('UserSocialLink'); foreach ($socialLinks as $socialLink) { if (!empty($this->_data['socialLink' . $socialLink['id']])) { $table->insert( [ 'userId' => $this->_primaryKey, 'linkId' => $socialLink['id'], 'link' => $this->_data['socialLink' . $socialLink['id']], ] ); } } return $this; } protected function _saveUserSocialLinks() { $this->_deleteIds('UserSocialLink', 'userId'); $this->_saveSocialLinkDependency(); return $this; } public function prepareSecurityAnswer($answer) { $answer = trim($answer); return (empty($answer)) ? '' : md5(strtolower($answer)); } }