123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace App\Helpers;
- use App\Companies;
- use App\Models\Restaurants;
- use http\Cookie;
- use Illuminate\Support\Facades\Crypt;
- use Str;
- use App\ApiTokens;
- use Aws\S3\S3Client;
- use Illuminate\Support\Facades\Auth;
- Class CommonMethods {
- public static function formatDate($date=""){
- if(empty($date))
- $date = now();
- return date('d, M Y',strtotime($date));
- }
- public static function formatDateTime($date=""){
- if(empty($date))
- $date = now();
- return date('Y-m-d h:i a',strtotime($date));
- }
- public static function input_string_sanitize($string){
- return htmlentities($string, ENT_QUOTES, 'UTF-8', false);
- }
- public static function format_report_number($id){
- return str_pad($id,6,0, STR_PAD_LEFT);
- }
- public static function generateRandomString($length = 8) {
- $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!%$#*&^(){}[]';
- $charactersLength = strlen($characters);
- $randomString = '';
- for ($i = 0; $i < $length; $i++) {
- $randomString .= $characters[rand(0, $charactersLength - 1)];
- }
- return $randomString;
- }
- public static function generate_user_name($restaurants){
- return Str::slug(strtolower(($restaurants).'-'.rand(1000,9999).'-'.substr(str_shuffle($restaurants),0,3)));
- }
- public static function encrypt($id){
- return Crypt::encryptString($id);
- }
- public static function decrypt($string){
- return Crypt::decryptString($string);
- }
- public static function fileSize($bytes){
- $kb = 1024;
- $mb = $kb * 1024;
- $gb = $mb * 1024;
- $tb = $gb * 1024;
- if (($bytes >= 0) && ($bytes < $kb)) {
- return $bytes . ' B';
- } elseif (($bytes >= $kb) && ($bytes < $mb)) {
- return ceil($bytes / $kb) . ' KB';
- } elseif (($bytes >= $mb) && ($bytes < $gb)) {
- return ceil($bytes / $mb) . ' MB';
- } elseif (($bytes >= $gb) && ($bytes < $tb)) {
- return ceil($bytes / $gb) . ' GB';
- } elseif ($bytes >= $tb) {
- return ceil($bytes / $tb) . ' TB';
- } else {
- return $bytes . ' B';
- }
- }
- public static function generateRequestID($user_id){
- return date('Ymdhis').'-'.$user_id;
- }
- public static function authenticate_token($token){
- $user = ApiTokens::where('api_token',$token)->first();
- if($user->users->is_active=="0" || $user->users->is_active==0){
- //dd($user->users->is_active);
- $response = [
- 'type' => 'error',
- 'message' => 'Account is inative, contact to administrator!',
- 'code' =>'X003'
- ];
- echo json_encode($response);
- exit;
- }
- return $user;
- }
- public static function sendResponse($result, $message)
- {
- $response = [
- 'type' => "success",
- 'data' => $result,
- 'message' => $message,
- ];
- return response()->json($response, 200,[],JSON_UNESCAPED_UNICODE);
- }
- public static function sendError($error, $errorMessages = [], $code = 404)
- {
- $response = [
- 'type' => 'error',
- 'message' => $error,
- 'code' =>$code
- ];
- if(!empty($errorMessages)){
- $response['data'] = $errorMessages;
- }
- return response()->json($response, 400);
- }
- public static function idFormat($restoId,$id){
- return str_pad($restoId,4,0, STR_PAD_LEFT).'-'.str_pad($id,6,0, STR_PAD_LEFT);
- }
- public static function idFormatItem($restoId,$order_id,$id){
- return str_pad($restoId,4,0, STR_PAD_LEFT).'-'.str_pad($order_id,6,0, STR_PAD_LEFT).'-'.str_pad($id,6,0, STR_PAD_LEFT);
- }
- public static function uploadFileToAWSCDN($budketName, $restoID ,$restoName ,$sourceFile,$fileName ){
- $s3 = new S3Client([
- 'version' => 'latest',
- 'region' => env('AWS_DEFAULT_REGION')
- ]);
- $bucket = $budketName;
- $image = "";
- try {
- // Upload data.
- $result = $s3->putObject([
- 'Bucket' => $bucket,
- 'Key' => env('BUCKET_FOLDER')."/".$restoID.'-'.\Illuminate\Support\Str::slug($restoName)."/".$fileName ,
- 'SourceFile' => $sourceFile,
- 'Body' => 'Hello, world!',
- 'ACL' => 'public-read'
- ]);
- // Print the URL to the object.
- $image = $result['ObjectURL'];
- $result = array('type'=>'success','url'=>$image);
- return $result;
- } catch (S3Exception $e) {
- $result = array('type'=>'error','message'=>$e->getMessage());
- return $result;
- }
- }
- public static function getRestuarantID(){
- // return 110;
- if(Auth::user()->role=='administrator')
- return Auth::id();
- else if(Auth::user()->role=='restaurant')
- return Auth::user()->restaurants->id;
- else if(Auth::user()->role=='admin_user')
- return Auth::user()->admin_users->id;
- else
- return Auth::user()->resto_users->resto_id;
- }
- public static function changeImageLinkAWStoImgix($aws_image_link,$imgix, $width,$height,$quality){
- return str_replace(env('AWS_URL_API'),$imgix,$aws_image_link).'?fm=webp&h='.$width.'&w='.$height.'&q='.$quality.'&fit=center&crop=center';
- }
- public static function getRestoMetas(Restaurants $restaurants){
- // $resto_metas = \Illuminate\Support\Facades\Cookie::get($restaurants->resto_unique_name.'_resto_metas');
- // if(!isset($resto_metas)){
- if(1){
- $resto_meta = isset($restaurants->resto_metas)?$restaurants->resto_metas:null;
- //dump($resto_meta);
- $resto_metas = [];
- $billing = [];
- if(isset($resto_meta)){
- foreach($resto_meta as $meta){
- if($meta->outlet_id!=""){
- continue;
- }
- $index_name = isset($meta->resto_meta_defs->parents)?$meta->resto_meta_defs->parents->meta_def_name:$meta->resto_meta_defs->meta_def_name;
- // dump($meta->resto_meta_defs);
- if($index_name=="BILLING_GATEWAY"){
- // dump($meta->resto_meta_defs->meta_def_name);
- // $resto_metas['BILLING_GATEWAY'][] = $meta->meta_val;
- $billing[] = array('id'=>$meta->meta_id,'value'=>$meta->meta_val);
- }
- $resto_metas[$index_name] = $meta->meta_val;
- }
- }
- $resto_metas['BILLING_GATEWAY'] = $billing;
- //\Illuminate\Support\Facades\Cookie::queue($restaurants->resto_unique_name.'_resto_metas',json_encode($resto_metas),'3600');
- return $resto_metas;
- }else{
- $resto_metas = json_decode($resto_metas,true);
- return $resto_metas;
- }
- }
- public static function getIPBasedInfo($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
- $output = NULL;
- if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
- $ip = $_SERVER["REMOTE_ADDR"];
- if ($deep_detect) {
- if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
- $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
- if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
- $ip = $_SERVER['HTTP_CLIENT_IP'];
- }
- }
- $purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
- $support = array("country", "countrycode", "state", "region", "city", "location", "address");
- $continents = array(
- "AF" => "Africa",
- "AN" => "Antarctica",
- "AS" => "Asia",
- "EU" => "Europe",
- "OC" => "Australia (Oceania)",
- "NA" => "North America",
- "SA" => "South America"
- );
- if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
- $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
- if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
- switch ($purpose) {
- case "location":
- $output = array(
- "city" => @$ipdat->geoplugin_city,
- "state" => @$ipdat->geoplugin_regionName,
- "country" => @$ipdat->geoplugin_countryName,
- "country_code" => @$ipdat->geoplugin_countryCode,
- "continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
- "continent_code" => @$ipdat->geoplugin_continentCode,
- 'currency' => @$ipdat->geoplugin_currencyCode
- );
- break;
- case "address":
- $address = array($ipdat->geoplugin_countryName);
- if (@strlen($ipdat->geoplugin_regionName) >= 1)
- $address[] = $ipdat->geoplugin_regionName;
- if (@strlen($ipdat->geoplugin_city) >= 1)
- $address[] = $ipdat->geoplugin_city;
- $output = implode(", ", array_reverse($address));
- break;
- case "city":
- $output = @$ipdat->geoplugin_city;
- break;
- case "state":
- $output = @$ipdat->geoplugin_regionName;
- break;
- case "region":
- $output = @$ipdat->geoplugin_regionName;
- break;
- case "country":
- $output = @$ipdat->geoplugin_countryName;
- break;
- case "countrycode":
- $output = @$ipdat->geoplugin_countryCode;
- break;
- }
- }
- }
- return $output;
- }
- }
|