<?php

namespace common\modules\sessions\controllers;

use Yii;
use yii\web\Response;

use common\components\BaseController;
use common\modules\sessions\models\Session;

/**
 * Default controller for the `sessions` module
 */
class DefaultController extends BaseController
{
	public static function actionsTitles()
    {
        return [
            'Add'           => 'Фиксация сессии',
        ];
    }

    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionAdd()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;

    	if(!Yii::$app->session->has('Session'))
        {
        	list($url, $referrer) = Yii::$app->session->get('SessionData');

	    	$request = Yii::$app->request;

	    	$session = new Session;
	    	$session->PHPSESSID = Yii::$app->session->id;
	    	$session->ip = $request->userIP;

	    	$session->url = $url;
	    	$session->referer = $referrer;

	    	$session->save();

            Yii::$app->session->set('Session', $session->id);
	    }
        
        return ['success' => true];
    }
}
