556 lines
14 KiB
PHP
556 lines
14 KiB
PHP
<?php
|
|
|
|
namespace common\components;
|
|
|
|
use common\models\TicketType;
|
|
use common\models\Transfer;
|
|
use \Yii;
|
|
|
|
class Helper {
|
|
|
|
public static function isStartDateToEarly($days_between){
|
|
$days_visiblity = Helper::getReceptionVisibilityDays();
|
|
// $days_between = $this->calcStartDaysSinceToday();
|
|
return $days_between > $days_visiblity;
|
|
}
|
|
|
|
/**
|
|
* Calculate time until today.
|
|
* @param string $start the date string, format "datetime => "Y-m-d H:i", "date => "Y-m-d"
|
|
* @param string $format the format of date
|
|
* @return float the days between start and today.
|
|
*/
|
|
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
|
|
*
|
|
*
|
|
* @param \yii\db\Query $query
|
|
* @param string $date
|
|
* @param string[] array $fields
|
|
* @param string string $format
|
|
* @return null
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @param \yii\db\Query $query
|
|
* @param $field
|
|
* @param $start
|
|
* @param $end
|
|
*/
|
|
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'
|
|
]
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* @param \yii\db\Query $query the query
|
|
* @param string $field the field name
|
|
* @param string $start the start date
|
|
* @param string $end the end date
|
|
*/
|
|
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
|
|
]
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* @param \yii\db\Query $query the query
|
|
* @param string $field the field name
|
|
* @param string $start the start date
|
|
* @param string $end the end date
|
|
*/
|
|
public static function inInterval($query, $field, $start, $end) {
|
|
$query->andFilterWhere ( [
|
|
'>=',
|
|
$field,
|
|
$start
|
|
] );
|
|
$query->andFilterWhere ( [
|
|
'<',
|
|
$field,
|
|
$end
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* @param string $field the field name
|
|
* @param string $start the start date
|
|
* @param string $end the end date
|
|
* @return array the query 'in interval' rule
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @noinspection PhpUnusedParameterInspection
|
|
*
|
|
* @param $field_start
|
|
* @param $field_endsqlExpireRulesqlExpireRule
|
|
* @param $paramStart
|
|
* @param $paramEnd
|
|
* @return string
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @param \yii\db\Query $query the query
|
|
* @param string $field the field name
|
|
*/
|
|
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 isCompanyGyor() {
|
|
return \Yii::$app->params ['company'] == 'gyor';
|
|
}
|
|
|
|
public static function isUserCartOn() {
|
|
return \Yii::$app->params ['user_cart_on'] == true;
|
|
}
|
|
|
|
public static function isKeyRequired() {
|
|
return \Yii::$app->params ['key_required'] == true;
|
|
}
|
|
|
|
public static function isTicketTypeDoorAllowedCheckOn() {
|
|
return \Yii::$app->params ['ticket_type_door_allowed_check_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 isReceptionShowStock() {
|
|
return \Yii::$app->params ['reception_show_stock'] ;
|
|
}
|
|
|
|
public static function isTicketCreatePriceEditable() {
|
|
return \Yii::$app->params ['ticket_create_price_editable'] == true ;
|
|
}
|
|
|
|
public static function isKeyToggleDoorLogEnabled() {
|
|
return \Yii::$app->params ['key_toggle_door_log_enabled'] == true ;
|
|
}
|
|
|
|
public static function isReceptionTransferListToday(){
|
|
return \Yii::$app->params['reception_transfer_list_only_today'];
|
|
}
|
|
public static function isReceptionAccountStateIndexToday(){
|
|
return \Yii::$app->params['reception_account_state_index_only_today'];
|
|
}
|
|
|
|
public static function isReceptionTransferIndexEnabled(){
|
|
return !Helper::isReceptionTransferListToday();
|
|
}
|
|
|
|
public static function getGroupTrainingUrl(){
|
|
return \Yii::$app->params['group_training.url'];
|
|
}
|
|
|
|
public static function getGroupTrainingRegistrationCancelLimitMinutes(){
|
|
return \Yii::$app->params['group_training.registration.cancel.limit.minutes'];
|
|
}
|
|
|
|
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 ){
|
|
if ( count($col) == 1){
|
|
$result = $result + Helper::mkYiiSortItem($col[0] ,$col[0]);
|
|
}else{
|
|
$result = $result + Helper::mkYiiSortItem($col[0] ,$col[1]);
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public static function setBit($val,$index,$bit_on){
|
|
$flag = +$val;
|
|
if ( $bit_on ){
|
|
$flag |= 1 << $index;
|
|
}else{
|
|
$flag &= ~ (1 << $index);
|
|
}
|
|
return $flag;
|
|
}
|
|
|
|
public static function isBitOn($flag,$n){
|
|
$result = +$flag & (1 << +$n) ;
|
|
$result = $result > 0;
|
|
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;
|
|
}
|
|
|
|
public static function getWebUrl(){
|
|
if ( \Yii::$app->params['development'] == true ){
|
|
return "http://localhost/fitness-web";
|
|
}else{
|
|
if ( self::isCompanyMovar()){
|
|
return "https://fitnessadmin.hu/fitness-web";
|
|
}else if ( self::isCompanyGyor()){
|
|
return "https://fitnessadmin.hu/cutler";
|
|
}else{
|
|
return "https://localhost/fitness-web";
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $start \DateTime datetime
|
|
* @param $ticketType \common\models\TicketType the ticket type
|
|
*/
|
|
public static function getTicketExpirationDate($start,$ticketType){
|
|
$result = new \DateTime();
|
|
$result->setTimezone(new \DateTimeZone("UTC") );
|
|
$result->setTimestamp($start->getTimestamp());
|
|
|
|
|
|
$unitCount = $ticketType->time_unit_count;
|
|
$unitType = $ticketType->time_unit_type;
|
|
|
|
switch ($unitType){
|
|
case TicketType::TIME_UNIT_DAY:
|
|
$result->add(new \DateInterval("P".$unitCount."D"));
|
|
$result->sub(new \DateInterval("P1D"));
|
|
break;
|
|
case TicketType::TIME_UNIT_MONTH:
|
|
$result->add(new \DateInterval("P".$unitCount."M"));
|
|
$result->sub(new \DateInterval("P1D"));
|
|
break;
|
|
case TicketType::TIME_UNIT_MONTH_REFERENCE:
|
|
if ( $unitCount > 1 ){
|
|
$result->add(new \DateInterval("P". ($unitCount -1 )."M"));
|
|
}
|
|
$result->sub(new \DateInterval("P1D"));
|
|
break;
|
|
}
|
|
|
|
$result->setTime(0, 0, 0);
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
|
|
public static function isRestAllowVerifyOnly() {
|
|
return \Yii::$app->params ['rest_allow_verify_only'] == true;
|
|
}
|
|
|
|
public static function getDoorEntryStrategy(){
|
|
return Helper::getArrayValue(\Yii::$app->params ,'door_entry_strategy','strategy_key');
|
|
}
|
|
|
|
public static function getDoorPassValidityIntervalMinutes(){
|
|
return Helper::getArrayValue(\Yii::$app->params ,'door_pass_validity_interval_minutes','10');
|
|
}
|
|
|
|
}
|