hi, I don’t understand how to get the state valorized after an ajax call.
this is my index:
public function actionIndex() {
// Get timezone client
Yii::app()->clientScript->registerScriptFile("//cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js", CClientScript::POS_END );
$urlTimezone = Yii::app()->createAbsoluteUrl('brief/timezone');
Yii::app()->clientScript->registerScript('get_data_timezone', "
mytimezone = function(){
var tz = jstz.determine();
var timezone = tz.name();
$.ajax({
url: '".$urlTimezone."',
type: 'POST',
data: {tz: timezone}
});
}
window.addEventListener('load',
mytimezone
);
", CClientScript::POS_END);
// richiamo i contest con fase diversa da END
$dataProvider = new CActiveDataProvider('VwContest', array(
'criteria' => array(
'condition' => 'end= 0 AND id_winner is null',
))
);
$this->render('index', array(
'dataProvider' => $dataProvider,
));
}
action ajax url:
public function actionTimezone() {
$timezone = $_POST['tz'];
// inserire valore del timezone in sessione
Yii::app()->user->setState('timezone', $timezone);
Yii::app()->end();
}
my view:
$clientTimezone = Yii::app()->user->getState('timezone') ? Yii::app()->user->getState('timezone') : 'Europe/Berlin';
$datetime = new DateTime($data->data_fine_fase );
$la_time = new DateTimeZone($clientTimezone);
$datetime->setTimezone($la_time);
I get timezone from the client but I need to refresh page twice because first it changes the session value, second it renders a view.
How can I get the value just when the client load the page first time?
// sorry for my english hope u understand well to help
When it is the first visit, the page is refreshed if the cookie was not present.
As I am using cookies, I am not using state - the state is limited to the session.
Here is a partial implementation:
/**
* Instruments the browser with code to determine the browser's timezone.
*
* Stores the value in a browser cookie (so that we get it on each request).
* Can be configured to get the timezone through a post request.
*
* @param string $route The route to post the timezone to (if not null)
* @param string $key The key for the timezone value of the route.
* @param string $cookiekey the key to store the time zone in.
*/
public static function determineBrowserTimezone($forceCheck=false,$route=null,$key='tz') {
// If cookie already set, do nothing
if(!$forceCheck&&(self::$_hasTzScript||isset($_COOKIE[self::$timezoneCookieKey]))) return;
self::$_hasTzScript=true;// Avoid adding script twice for speed.
$cookieKey=self::$timezoneCookieKey;
$assetsUrl=Yii::app()->assetManager->publish(__DIR__.DIRECTORY_SEPARATOR.'assets');
$jstzUrl=$assetsUrl.'/jsts.min.js';
// Set cookie (if tz changed) and reload page
$jsPost="jQuery.cookie('{$cookieKey}',timezone,{path:'/'/*,expires:new Date(((new Date()).getTime()+30000000000))*/});window.location.reload();";
if($route!==null) {
$jsUrl=CJavaScript::encode(Yii::app()->createUrl($route));
$jsPost="jQuery.post($jsUrl,{{$key}:timezone},function(data){{$jsPost}});}";
}
$js=<<<EOJS
var tz = jstz.determine();
var timezone = tz.name();
var ck=jQuery.cookie('{$cookieKey}');
if(ck!==timezone){{$jsPost}}
EOJS;
Yii::app()->clientScript
->registerCoreScript('jquery')
->registerCoreScript('cookie')
->registerScriptFile($jstzUrl)
->registerScript('tzcky-'.$cookieKey.$key,$js);
}
This could be optimized by :
Avoid page rendering if the cookie is missing and just send a simple, essentially empty page to set the cookie;
Reload the page only if the timezone information was needed on the current page (by checking the use of the local time conversion functions).