Skip to content

Commit

Permalink
Improved BaseModel with timezone getting support
Browse files Browse the repository at this point in the history
  • Loading branch information
libern committed Jul 27, 2016
1 parent 98e8589 commit df3c13f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
51 changes: 47 additions & 4 deletions src/Someline/Base/Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ protected function api_auth()
return app('Dingo\Api\Auth\Auth');
}

/**
* Get current auth user
*
* @return User|null
*/
public function getAuthUser()
{
$user = null;
if ($this->api_auth()->check()) {
$user = $this->api_auth()->user();
} else if (\Auth::check()) {
$user = \Auth::user();
}
return $user;
}

/**
* Get current auth user_id
*
Expand All @@ -58,10 +74,9 @@ protected function api_auth()
public function getAuthUserId()
{
$user_id = null;
if ($this->api_auth()->check()) {
$user_id = $this->api_auth()->user()->user_id;
} else if (\Auth::check()) {
$user_id = \Auth::user()->user_id;
$user = $this->getAuthUser();
if ($user) {
$user_id = $user->user_id;
}
return $user_id;
}
Expand Down Expand Up @@ -157,4 +172,32 @@ public function setModelPresenter()
return $this;
}

/**
* Return a timezone for all Datetime objects
*
* @return mixed
*/
protected function getDateTimezone()
{
$user = $this->getAuthUser();
if ($user && !empty($user->timezone)) {
return $user->timezone;
} else {
return app_timezone();
}
}

/**
* Return a timestamp as DateTime object with timezone support.
*
* @param mixed $value
* @return \Carbon\Carbon
*/
protected function asDateTime($value)
{
$carbon = parent::asDateTime($value);
$carbon->setTimezone($this->getDateTimezone());
return $carbon;
}

}
15 changes: 15 additions & 0 deletions src/Someline/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function auth_user()

if (!function_exists('smart_get_client_ip')) {

/**
* @return array|string
*/
function smart_get_client_ip()
{
$request = request();
Expand All @@ -32,6 +35,18 @@ function smart_get_client_ip()

}

if (!function_exists('app_timezone')) {

/**
* @return mixed
*/
function app_timezone()
{
return \Config::get("app.timezone");
}

}

if (!function_exists('jwt_token')) {

/**
Expand Down

0 comments on commit df3c13f

Please sign in to comment.