31 lines
1.0 KiB
PHP
31 lines
1.0 KiB
PHP
<?php
|
|
|
|
|
|
|
|
/* @var $this \yii\web\View */
|
|
|
|
use app\models\UserExplorerObjects;
|
|
use app\services\Auth;
|
|
|
|
$user = Auth::getUserByToken();
|
|
if($user) {
|
|
header('Content-type: application/json');
|
|
$body = json_decode(Yii::$app->request->getRawBody());
|
|
if($body->id)
|
|
$explorerObject = \app\models\ExplorerObjects::findOne($body->id);
|
|
$userExplorerObject = UserExplorerObjects::find()->where(['user_id' => $user->id, 'explorer_object_id' => $body->id])->one();
|
|
if(!$userExplorerObject) {
|
|
$userExplorerObject = new UserExplorerObjects();
|
|
$userExplorerObject->user_id = $user->id;
|
|
$userExplorerObject->explorer_object_id = $body->id;
|
|
$userExplorerObject->date_visit = date('Y-m-d H:i:s');
|
|
$userExplorerObject->save();
|
|
echo json_encode(['visit' => 'success', 'points' => $explorerObject->points]);
|
|
} else {
|
|
echo json_encode(['visit' => 'visited', 'points' => $explorerObject->points]);
|
|
}
|
|
exit;
|
|
}
|
|
echo json_encode(['error' => 'User not authenticated']);
|
|
exit;
|