fitness-web/common/components/Helper.php
2016-01-25 01:43:19 +01:00

215 lines
4.7 KiB
PHP

<?php
namespace common\components;
use \Yii;
class Helper {
public static function getDateTimeString( ){
return date("Y-m-d H:i:s");
}
public static function getArrayValue($arr,$key,$def){
$result = $def;
if ( array_key_exists($key, $arr)){
$result = $arr[$key];
}
return $result;
}
public static function hufRound($m) {
$result = round ( $m / 5, 0 ) * 5;
return $result;
}
public static function notInInterval($query, $field, $start, $end) {
$query->andFilterWhere ( [
'or',
[
'<',
$field,
isset ( $start ) ? $start : '1900-01-01'
],
[
'>=',
$field,
isset ( $end ) ? $end : '3000-01-01'
]
] );
}
public static function notPaid($query, $field, $start, $end) {
$query->andFilterWhere ( [
'or',
[
'<',
$field,
isset ( $start ) ? $start : '1900-01-01'
],
[
'>=',
$field,
isset ( $end ) ? $end : '3000-01-01'
],
[
"transfer.status" => Transfer::STATUS_NOT_PAID
]
] );
}
public static function inInterval($query, $field, $start, $end) {
$query->andFilterWhere ( [
'>=',
$field,
$start
] );
$query->andFilterWhere ( [
'<',
$field,
$end
] );
}
public static function queryInIntervalRule($field, $start, $end) {
return [
'and',
[
'>=',
$field,
$start
],
[
'<',
$field,
$end
]
];
}
public static function queryExpireRule($field_start, $field_end, $start, $end) {
return [
'and',
[
'<',
$field_start,
$end
],
[
'>=',
$field_end,
$start
],
[
'<=',
$field_end,
$end
]
];
}
public static function queryValidRule($field_start, $field_end, $start, $end) {
return [
'and',
[
'<',
$field_start,
$end
],
[
'>=',
$field_end,
$start
]
];
}
public static function sqlInIntervalRule($field, $paramStart, $paramEnd) {
return ' ' . $field . ' >= ' . $paramStart . ' and ' . $field . ' < ' . $paramEnd;
}
public static function sqlExpireRule($field_start, $field_end, $paramStart, $paramEnd) {
return ' ' . $field_start . ' < ' . $paramEnd . ' and ' . $field_end . ' < ' . $paramEnd;
}
public static function sqlValidRule($field_start, $field_end, $paramStart, $paramEnd) {
return ' ' . $field_start . ' < ' . $paramEnd . ' and ' . $field_end . ' >=' . $paramStart;
}
public static function queryAccountConstraint($query, $field) {
if (! RoleDefinition::isAdmin ()) {
$query->innerJoin ( "user_account_assignment", $field . ' = user_account_assignment.id_account' );
$query->andWhere ( [
'user_account_assignment.id_user' => Yii::$app->user->id
] );
}
}
public static function roleLabels() {
return [
'reception' => Yii::t ( 'common/role', 'Reception' ),
'admin' => Yii::t ( 'common/role', 'Administrator' ),
'employee' => Yii::t ( 'common/role', 'Alkalmazott' )
];
}
public static function roleDefinitions() {
return [
'employee' => [
'canAllow' => [
'employee'
]
],
'admin' => [
'canAllow' => [
'admin',
'reception',
'employee'
]
],
'reception' => [
'canAllow' => [ ]
]
];
}
public static function flash($mode, $message) {
\Yii::$app->session->setFlash ( $mode, $message );
}
public static function fixAsciiChars($in) {
$out = str_replace ( "ö", "0", $in );
$out = str_replace ( "Ö", "0", $out );
return $out;
}
public static function isCompanyMovar() {
return \Yii::$app->params ['company'] == 'movar';
}
public static function isProductVisibilityAccount() {
return \Yii::$app->params ['product_visiblity'] == 'account';
}
public static function isAccountStateClosePreloadMoney() {
return \Yii::$app->params ['account_state_close_preload_money'] == true;
}
public static function getRealUserIp() {
$client = @$_SERVER ['HTTP_CLIENT_IP'];
$forward = @$_SERVER ['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER ['REMOTE_ADDR'];
if (filter_var ( $client, FILTER_VALIDATE_IP )) {
$ip = $client;
} elseif (filter_var ( $forward, FILTER_VALIDATE_IP )) {
$ip = $forward;
} else {
$ip = $remote;
}
return $ip;
}
public static function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
public static function getGeoIp() {
$ip = Helper::getRealUserIp ();
$details = json_decode ( Helper::url_get_contents( "http://ipinfo.io/{$ip}/json" ) );
return $details;
}
}