fitness-web/common/components/Helper.php

381 lines
9.1 KiB
PHP

<?php
namespace common\components;
use \Yii;
class Helper {
public static function isStartDateToEarly($days_between){
$days_visiblity = Helper::getReceptionVisibilityDays();
// $days_between = $this->calcStartDaysSinceToday();
return $days_between > $days_visiblity;
}
/**
* @param string $start the date string, format "datetime => "Y-m-d H:i", "date => "Y-m-d"
* */
public static function calcStartDatimeDaysSinceToday($start, $format = "datetime" ){
$now = time();
if ( $format == "datetime"){
$format = "Y-m-d H:i";
}else if ( $format == "date") {
$format = "Y-m-d";
}else{
//use format
}
$d = \DateTime::createFromFormat( $format , $start)->getTimeStamp();
$days_between = ceil(abs( $now - $d) / 86400 );
return $days_between;
}
/**
* Leellenőriz egy dátumot. Ha az aktuális felhasználó nem admin,
* akkor a params[reception_visibility_days] napnál korábbi , vagy null
* dátumot e params-ban megadott dátumra állítjuk
*
*
* */
public static function restrictIfNotAdminTheStartDate($query,$date, $fields = ['transfer.paid_at','transfer.created_at'], $format = 'datetime'){
$result = null;
$needFix = false;
if ( !isset($date) || empty($date)){
$needFix = true;
}else {
$days = Helper::calcStartDatimeDaysSinceToday($date,$format);
if ( Helper::isStartDateToEarly($days)){
$needFix = true;
}
}
if ( $needFix == true ){
$d = Helper::getReceptionVisibilityDays();
$time = date( "Y-m-d H:i:s", strtotime("today -$d day") );
$conditions = [];
foreach ($fields as $f ){
$conditions[] = ['>=', $f, $time];
}
if ( count($conditions) > 1 ){
$andWhereCond = [];
$andWhereCond[0] = "or";
$i = 1;
foreach ($conditions as $c){
$andWhereCond[$i] = $c;
$i++;
}
}else if ( count($conditions) == 1 ){
$andWhereCond = $conditions[0];
}
// $start_date_condition = ['or',[ '>=', 'transfer.created_at', $time ] ,[ '>=', 'transfer.paid_at', $time] ];
if ( isset($andWhereCond)){
$query->andWhere( $andWhereCond );
}
}
return $result;
}
public static function getDateTimeString( ){
return date("Y-m-d H:i:s");
}
public static function getDateString( ){
return date("Y-m-d");
}
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 );
/*
$out = str_replace ( "Y", "%", $out );
$out = str_replace ( "y", "%", $out );
$out = str_replace ( "Z", "y", $out );
$out = str_replace ( "z", "y", $out );
$out = str_replace ( "%", "y", $out );
*/
return $out;
}
public static function isUserCartVisibilityUser() {
return \Yii::$app->params ['user_cart_item_visibility'] == 'user';
}
public static function isUserCartVisibilityAll() {
return \Yii::$app->params ['user_cart_item_visibility'] == 'all';
}
public static function getBackendSkin() {
return \Yii::$app->params ['backend_skin'] ;
}
public static function getCompany() {
return \Yii::$app->params ['company'] ;
}
public static function getCompanyName() {
return \Yii::$app->params ['company_name'] ;
}
public static function isCompanyMovar() {
return \Yii::$app->params ['company'] == 'movar';
}
public static function isUserCartOn() {
return \Yii::$app->params ['user_cart_on'] == true;
}
public static function getProductSaleDefaultFocus() {
return \Yii::$app->params ['product_sale_default_focus'] ;
}
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 isAccountStateOpenSendMail() {
return \Yii::$app->params ['mail_account_state_open'] == true;
}
public static function isAccountStateCloseSendMail() {
return \Yii::$app->params ['mail_account_state_close'] == true;
}
public static function getReceptionVisibilityDays() {
return \Yii::$app->params ['reception_visibility_days'] ;
}
public static function isTicketCreatePriceEditable() {
return \Yii::$app->params ['ticket_create_price_editable'] == 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;
}
public static function mkYiiSortItem($field, $column){
return [
$field => [
'asc' => [$column => SORT_ASC ],
'desc' => [$column => SORT_DESC],
]
];
}
public static function mkYiiSortItems($config){
$result = [];
foreach ($config as $col ){
$result = $result + Helper::mkYiiSortItem($col[0] ,$col[1]);
}
return $result;
}
public static function generateRandomString($length = 6,$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWX' ) {
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
}