Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\models\api;
- use Yii;
- use app\models\db\User;
- use yii\db\Exception;
- use SoapClient;
- use DateTime;
- /**
- * This is the model class for table "profile".
- *
- * @property integer $id
- * @property string $image_url
- * @property integer $height
- * @property double $weight
- * @property double $MDG
- * @property double $IMT
- * @property double $water_mass
- * @property string $pressure
- * @property integer $user_id
- * @property double $fat_mass
- * @property double $weight_without_fat
- * @property double $FIB
- * @property integer $pulse
- * @property integer $respiratory
- * @property integer $heart_movements
- * @property double $sugar
- * @property double $PH
- * @property double $leucocytes
- * @property double $cholesterol
- * @property integer $activity_level_id
- * @property string $age
- * @property integer $gender
- * @property integer $change_date_of_age
- * @property string $onesignal_user_id
- * @property integer $keito_user_id
- * @property integer $keito_barcode
- *
- * @property ActivityLevel $activityLevel
- * @property User $user
- */
- class Profile extends \yii\db\ActiveRecord
- {
- public $characteristicsHistory = '21';
- static $_soapClient = null;
- private static function getSoapClient () {
- if (!self::$_soapClient) {
- self::$_soapClient = new SoapClient('http://www.keitovs.net/keitows/fingerprint.asmx?wsdl');
- }
- return self::$_soapClient;
- }
- static $ERROR_NONE = 'resErrNoError';
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'profile';
- }
- public static function findForCurrentUser()
- {
- return self::findForUserId(Yii::$app->user->id);
- }
- public static function findForUserId($userId)
- {
- $profile = parent::find()->where(['user_id' => $userId])->one();
- if ($profile['change_date_of_age'] < strtotime('-1 year')) {
- $profile['change_date_of_age'] = time();
- $profile['age'] = $profile['age'] + 1;
- $profile->save(false);
- }
- $newProfile = [];
- $newProfile["activityLevel"] = $profile->hasOne(ActivityLevel::className(), ['id' => 'activity_level_id'])->one();
- $user = User::findOne($userId);
- $newProfile['email'] = $user['email'];
- foreach ($profile as $key => $value)
- $newProfile[$key] = $value;
- return $newProfile;
- }
- public static function updateProfile($params)
- {
- $profile = isset($params["id"]) ? self::findOne($params["id"]) : new Profile();
- /* if (isset($params["firstname"]))
- $profile->firstname = $params["firstname"];
- if (isset($params["lastname"]))
- $profile->lastname = $params["lastname"]; */
- /* if (isset($params["email"])) { // todo тут нужен мех-м изменения имейла
- } */
- if (isset($params["age"])) {
- if ($profile->age != $params["age"] || !$profile->age) {
- $profile->change_date_of_age = time();
- }
- $profile->age = $params["age"];
- }
- if (isset($params["gender"]))
- $profile->gender = $params["gender"];
- if (isset($params["height"]))
- $profile->height = $params["height"];
- if (isset($params["pressure"]))
- $profile->pressure = $params["pressure"];
- if (isset($params["pulse"]))
- $profile->pulse = $params["pulse"];
- if (isset($params["respiratory"]))
- $profile->respiratory = $params["respiratory"];
- if (isset($params["heart_movements"]))
- $profile->heart_movements = $params["heart_movements"];
- if (isset($params["activity_level_id"]))
- $profile->activity_level_id = $params["activity_level_id"];
- if (isset($params["weight"]))
- $profile->weight = $params["weight"];
- if (isset($params["MDG"]))
- $profile->MDG = $params["MDG"];
- if (isset($params["IMT"]))
- $profile->IMT = $params["IMT"];
- if (isset($params["water_mass"]))
- $profile->water_mass = $params["water_mass"];
- if (isset($params["fat_mass"]))
- $profile->fat_mass = $params["fat_mass"];
- if (isset($params["weight_without_fat"]))
- $profile->weight_without_fat = $params["weight_without_fat"];
- if (isset($params["FIB"]))
- $profile->FIB = $params["FIB"];
- if (isset($params["sugar"]))
- $profile->sugar = $params["sugar"];
- if (isset($params["PH"]))
- $profile->PH = $params["PH"];
- if (isset($params["leucocytes"]))
- $profile->leucocytes = $params["leucocytes"];
- if (isset($params["cholesterol"]))
- $profile->cholesterol = $params["cholesterol"];
- if (isset($params["image_url"]))
- $profile->image_url = $params["image_url"];
- if (isset($params["keito_user_id"]))
- $profile->keito_user_id = $params["keito_user_id"];
- if (isset($params["onesignal_user_id"]))
- $profile->onesignal_user_id = $params["onesignal_user_id"];
- if (isset($params["keito_barcode"])) {
- $profile->keito_barcode = $params["keito_barcode"];
- $settings = GlobalSettings::find()->all();
- if ($settings && count($settings) && $settings[0]['keito_login'] && $settings[0]['keito_password']) {
- $login = $settings[0]['keito_login'];
- $pass = $settings[0]['keito_password'];
- $organizationCode = self::getOrganizationCode($login, $pass);
- $tempId = self::getUserIDFromCardCode($params["keito_barcode"], $organizationCode, $login, $pass);
- if ($tempId) {
- $profile->keito_user_id = $tempId;
- }
- }
- }
- $profile->user_id = Yii::$app->user->id;
- $achievedTargets = [];
- $id = isset($params["id"]) ? isset($params["id"]) : '';
- $isSuccess = $profile->save(false);
- if (!$isSuccess) {
- throw new Exception('Profile ' . $id . ' was not updated.', ['$profile' => $profile], 1);
- } else if (isset($params["weight"])) {
- $achievedTargets = Competition::checkForAchieveTarget($params["weight"]);
- }
- $newProf = self::convertToArray($profile, [
- "achievedTargets" =>$achievedTargets
- ]);
- return $newProf;
- }
- public static function convertToArray($obj, $arrNewProps)
- {
- $result = [];
- foreach ($arrNewProps as $key => $value)
- $result[$key] = $value;
- foreach ($obj as $key => $value)
- $result[$key] = $value;
- return $result;
- }
- public static function updateCharacteristicsFromKeito($cardCode)
- {
- $measure = self::getLastUserMeasure($cardCode);
- $organizationCode = isset($measure['organizationCode']) ? $measure['organizationCode'] : null;
- $userId = isset($measure['userId']) ? $measure['userId'] : null;
- $login = isset($measure['login']) ? $measure['login'] : null;
- $password = isset($measure['password']) ? $measure['password'] : null;
- $keitoUser = self::getUserInfoFromKeito($cardCode, $organizationCode, $userId, $login, $password);
- $maxPressure = $measure['nMaxBloodPressure'];
- $minPressure = $measure['nMinBloodPressure'];
- $pressure = "$maxPressure/$minPressure";
- $fatMass = $measure['sngWeight'] * $measure['sngIGP'] / 100;
- // $MDG = $fatMass * 100 / $measure['sngWeight'];
- $IMT = $measure['sngWeight'] / ($measure['nHeight'] * $measure['nHeight'] / 10000);
- $FIB = $fatMass / ($measure['nHeight'] * $measure['nHeight'] / 10000);
- $weightWithoutFat = $measure['sngWeight'] - $fatMass;
- $datetime = new DateTime($keitoUser->dBirthDate);
- $now = new DateTime();
- $age = $now->format('Y') - $datetime->format('Y');
- $gender = $keitoUser->Sex === 'sexMale' ? 1 : 0;
- return self::updateProfile([
- 'id' => $measure['profileId'],
- 'height' => $measure['nHeight'],
- 'weight' => $measure['sngWeight'],
- 'pulse' => $measure['nPulse'],
- 'FIB' => $FIB,
- 'pressure' => $pressure,
- 'keito_user_id' => $measure['userId'],
- 'fat_mass' => $fatMass,
- 'MDG' => $measure['sngIGP'],
- 'IMT' => $IMT,
- 'weight_without_fat' => $weightWithoutFat,
- 'keito_barcode' => $cardCode,
- 'gender' => $gender,
- 'age' => $age
- ]);
- }
- public static function getLastUserMeasure($cardCode)
- {
- $result = null;
- $newResult = [];
- $settings = GlobalSettings::find()->all();
- if ($settings && count($settings) && $settings[0]['keito_login'] && $settings[0]['keito_password']) {
- $login = $settings[0]['keito_login'];
- $pass = $settings[0]['keito_password'];
- $organizationCode = self::getOrganizationCode($login, $pass);
- $currentProfile = self::findForCurrentUser();
- $barcode = !!$cardCode ? $cardCode : $currentProfile['keito_barcode'];
- $userId = self::getUserIDFromCardCode($barcode, $organizationCode, $login, $pass);
- if ($userId) {
- $response = self::getSoapClient()->AppGetUserMeasurements([
- 'sLoginName' => $login,
- 'sPassWord' => $pass,
- 'nOrganizationID' => $organizationCode,
- 'nUserID' => $userId,
- 'nNumberOfMeasures' => 1 // последнее измерение
- ]);
- $result = (
- $response &&
- $response->AppGetUserMeasurementsResult &&
- $response->AppGetUserMeasurementsResult->Result &&
- $response->AppGetUserMeasurementsResult->Result->nErrorCode == self::$ERROR_NONE &&
- $response->AppGetUserMeasurementsResult->Measures &&
- $response->AppGetUserMeasurementsResult->Measures->CMeasure &&
- $response->AppGetUserMeasurementsResult->Measures->CMeasure
- ) ? $response->AppGetUserMeasurementsResult->Measures->CMeasure : null;
- $newResult['userId'] = $userId;
- $newResult['profileId'] = $currentProfile['id'];
- $newResult['login'] = $login;
- $newResult['password'] = $pass;
- $newResult['organizationCode'] = $organizationCode;
- $newResult['userId'] = $userId;
- foreach ($result as $key => $value)
- $newResult[$key] = $value;
- } else {
- throw new Exception('User with this barcode not exist', null, 1);
- }
- }
- return $newResult;
- }
- public static function getUserInfoFromKeito($cardCode, $organizationCode = null, $userId = null, $login = null, $password = null)
- {
- $result = null;
- $settings = !($login && $password) ? GlobalSettings::find()->all() : [['keito_login' => $login, 'keito_password' => $password]];
- if ($settings && count($settings) && $settings[0]['keito_login'] && $settings[0]['keito_password']) {
- $login = $settings[0]['keito_login'];
- $pass = $settings[0]['keito_password'];
- $organizationCode = $organizationCode ? $organizationCode : self::getOrganizationCode($login, $pass);
- // $currentProfile = self::findForCurrentUser();
- $userId = $userId ? $userId : self::getUserIDFromCardCode($cardCode, $organizationCode, $login, $pass);
- if ($userId) {
- $result = self::getUserInfo($userId, $organizationCode, $login, $pass);
- }
- }
- return $result;
- }
- private static function getOrganizationCode($login, $password)
- {
- $response = self::getSoapClient()->AppGetAdminOrganizations([
- 'sLoginName' => $login,
- 'sPassWord' => $password
- ]);
- $result = (
- $response &&
- $response->AppGetAdminOrganizationsResult &&
- $response->AppGetAdminOrganizationsResult->Result &&
- $response->AppGetAdminOrganizationsResult->Result->nErrorCode == self::$ERROR_NONE &&
- $response->AppGetAdminOrganizationsResult->Organizations &&
- $response->AppGetAdminOrganizationsResult->Organizations->COrganization
- ) ? $response->AppGetAdminOrganizationsResult->Organizations->COrganization->nID : null;
- return $result;
- }
- private static function getUserIDFromCardCode($cardCode, $organizationCode, $login, $password)
- {
- $result = null;
- /*$result = self::getSoapClient()->AppGetUserIDFromCardCode([ // todo не работает, возможно причина в неверном типе кода
- 'sLoginName' => $login,
- 'sPassWord' => $password,
- 'nOrganizationID' => $organizationCode,
- 'eCardType' => 'ctBarcodeEAN13',
- 'sCardCode' => $cardCode
- ]);*/
- $response = self::getSoapClient()->AppGetOrgUsersEx([
- 'sLoginName' => $login,
- 'sPassWord' => $password,
- 'nOrganizationID' => $organizationCode
- ]);
- $userList = (
- $response &&
- $response->AppGetOrgUsersExResult &&
- $response->AppGetOrgUsersExResult->Result &&
- $response->AppGetOrgUsersExResult->Result->nErrorCode == self::$ERROR_NONE &&
- $response->AppGetOrgUsersExResult->Users &&
- $response->AppGetOrgUsersExResult->Users->CUserEx
- ) ? $response->AppGetOrgUsersExResult->Users->CUserEx : null;
- if ($userList) {
- foreach ($userList as $user) {
- if (isset($user->asCardCodes) && isset($user->asCardCodes->string) && $user->asCardCodes->string == $cardCode) {
- $result = $user->nUserID;
- break;
- }
- }
- }
- return $result;
- }
- private static function getUserInfo($userId, $organizationCode, $login, $password)
- {
- $response = self::getSoapClient()->AppGetUserInfo([
- 'sLoginName' => $login,
- 'sPassWord' => $password,
- 'nOrganizationID' => $organizationCode,
- 'nUserID' => $userId
- ]);
- $result = (
- $response &&
- $response->AppGetUserInfoResult &&
- $response->AppGetUserInfoResult->Result &&
- $response->AppGetUserInfoResult->Result->nErrorCode == self::$ERROR_NONE &&
- $response->AppGetUserInfoResult->Users &&
- $response->AppGetUserInfoResult->Users->CUser
- ) ? $response->AppGetUserInfoResult->Users->CUser : null;
- return $result;
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['gender'], 'required'],
- [['height', 'user_id', 'pulse', 'respiratory', 'heart_movements', 'activity_level_id', 'gender', 'age', 'keito_user_id'], 'integer'],
- [['weight', 'MDG', 'IMT', 'water_mass', 'fat_mass', 'weight_without_fat', 'FIB', 'sugar', 'PH', 'leucocytes', 'cholesterol'], 'number'],
- [['image_url'], 'string'],
- [['pressure'], 'string', 'max' => 128],
- [['keito_barcode'], 'string', 'max' => 32],
- [['activity_level_id'], 'exist', 'skipOnError' => true, 'targetClass' => ActivityLevel::className(), 'targetAttribute' => ['activity_level_id' => 'id']],
- [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'image_url' => 'Image Url',
- 'height' => 'Height',
- 'weight' => 'Weight',
- 'MDG' => 'Mdg',
- 'IMT' => 'Imt',
- 'water_mass' => 'Water Mass',
- 'pressure' => 'Pressure',
- 'user_id' => 'User ID',
- 'fat_mass' => 'Fat Mass',
- 'weight_without_fat' => 'Weight Without Fat',
- 'FIB' => 'Fib',
- 'pulse' => 'Pulse',
- 'respiratory' => 'Respiratory',
- 'heart_movements' => 'Heart Movements',
- 'sugar' => 'Sugar',
- 'PH' => 'Ph',
- 'leucocytes' => 'Leucocytes',
- 'cholesterol' => 'Cholesterol',
- 'activity_level_id' => 'Activity Level ID',
- 'age' => 'Age',
- 'gender' => 'Gender',
- ];
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getActivityLevel()
- {
- return $this->hasOne(ActivityLevel::className(), ['id' => 'activity_level_id']);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getUser()
- {
- return $this->hasOne(User::className(), ['id' => 'user_id']);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement