Skip to content

Commit b586388

Browse files
committed
Skip flag flashed in session
The skip flag is now stored in the session using the flash function
1 parent 3439712 commit b586388

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,17 @@ CSRF token.
5656

5757
## Example
5858

59+
### Carbon
60+
5961
ClientTimezone is best used with Carbon to display the users current time.
6062

6163
```
6264
$carbon = Carbon::now();
6365
$carbon->addMinutes(ClientTimezone::getOffset());
6466
```
6567

68+
### Skipping
69+
6670
There may be times when you do not want javascript to check the client timezone. An example
6771
of this is when the client creates a new session on your application by following a URL
6872
to verify their email address, this page displays a flash message, but will be shown briefly
@@ -73,6 +77,17 @@ just add this line to your controller before returning a view.
7377

7478
`ClientTimezone::skip();`
7579

80+
### Overrides
81+
82+
You can override any of the ClientTimezone constant values by simply adding a value of the same
83+
name inside the .env file, for example:
84+
85+
```
86+
CLIENT_TIMEZONE_SESSION=myexample
87+
CLIENT_TIMEZONE_POST=newurl
88+
CLIENT_TIMEZONE_SKIP=skipkey
89+
```
90+
7691
## Authors
7792

7893
* **Kevin Orriss** - [Website](http://kevinorriss.com)

src/ClientTimezone.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ class ClientTimezone
3131
const CLIENT_TIMEZONE_POST = "clienttimezone";
3232

3333
/**
34-
* Flag to skip getting the client timezone
34+
* The session key for if checking the client timezone should be skipped
3535
*
36-
* @var boolean
36+
* @var string
3737
*/
38-
protected static $skip = FALSE;
38+
const CLIENT_TIMEZONE_SKIP = "clienttimezoneskip";
3939

4040

4141
/**
@@ -45,7 +45,7 @@ class ClientTimezone
4545
*
4646
* @return string
4747
*/
48-
protected static function getSessionKey()
48+
protected static function getOffsetSessionKey()
4949
{
5050
return env('CLIENT_TIMEZONE_SESSION', self::CLIENT_TIMEZONE_SESSION);
5151
}
@@ -62,14 +62,24 @@ public static function getPostUrl()
6262
return env('CLIENT_TIMEZONE_POST', self::CLIENT_TIMEZONE_POST);
6363
}
6464

65+
/**
66+
* Returns the session key where the skip flag is stored
67+
*
68+
* @return string
69+
*/
70+
protected static function getSkipSessionKey()
71+
{
72+
return env('CLIENT_TIMEZONE_SKIP', static::CLIENT_TIMEZONE_SKIP);
73+
}
74+
6575
/**
6676
* Returns if the client timezone is stored in session or not
6777
*
6878
* @return boolean
6979
*/
70-
public static function hasOffset()
80+
protected static function hasOffset()
7181
{
72-
return Session::has(static::getSessionKey());
82+
return Session::has(static::getOffsetSessionKey());
7383
}
7484

7585
/**
@@ -82,7 +92,7 @@ public static function getOffset()
8292
{
8393
if (static::hasOffset())
8494
{
85-
return intval(Session::get(static::getSessionKey()));
95+
return intval(Session::get(static::getOffsetSessionKey()));
8696
}
8797
return NULL;
8898
}
@@ -94,32 +104,42 @@ public static function getOffset()
94104
*/
95105
public static function setOffset($offset)
96106
{
97-
Session::put(static::getSessionKey(), intval($offset));
107+
Session::put(static::getOffsetSessionKey(), intval($offset));
98108
}
99109

100110
/**
101111
* Removes the timezone offset from the session
102112
*/
103113
public static function forget()
104114
{
105-
Session::forget(static::getSessionKey());
115+
Session::forget(static::getOffsetSessionKey());
106116
}
107117

108118
/**
109119
* Sets the skip flag to true, meaning that the javascript will not check for the client timezone
110120
*/
111121
public static function skip()
112122
{
113-
static::$skip = TRUE;
123+
Session::flash(static::getSkipSessionKey(), TRUE);
124+
}
125+
126+
/**
127+
* Returns if checking the client timezone is going to be skipped or not
128+
*
129+
* @return boolean
130+
*/
131+
protected static function skipping()
132+
{
133+
return Session::has(static::getSkipSessionKey()) && Session::get(static::getSkipSessionKey()) == TRUE;
114134
}
115135

116136
/**
117-
* Returns if the client timezone should be checked or not
137+
* Returns if the client timezone will be checked or not
118138
*
119139
* @return boolean
120140
*/
121-
public static function check()
141+
public static function checking()
122142
{
123-
return !static::$skip && is_null(static::getOffset());
143+
return !static::skipping() && is_null(static::getOffset());
124144
}
125145
}

src/views/javascript.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<script>
22
$(document).ready(function()
33
{
4-
if({{ (ClientTimezone::check() ? "true" : "false") }})
4+
if({{ ClientTimezone::checking() ? "true" : "false" }})
55
{
66
var clienttime = new Date();
77
var clienttimezone = -clienttime.getTimezoneOffset();
88
$.ajax(
99
{
1010
type: "POST",
11-
url: "{{ url(env('CLIENT_TIMEZONE_POST', ClientTimezone::CLIENT_TIMEZONE_POST)) }}",
11+
url: "{{url(ClientTimezone::getPostUrl()) }}",
1212
data:
1313
{
1414
timezoneoffset: clienttimezone

0 commit comments

Comments
 (0)