CommonMethods.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace App\Helpers;
  3. use App\Companies;
  4. use App\Models\Restaurants;
  5. use http\Cookie;
  6. use Illuminate\Support\Facades\Crypt;
  7. use Str;
  8. use App\ApiTokens;
  9. use Aws\S3\S3Client;
  10. use Illuminate\Support\Facades\Auth;
  11. Class CommonMethods {
  12. public static function formatDate($date=""){
  13. if(empty($date))
  14. $date = now();
  15. return date('d, M Y',strtotime($date));
  16. }
  17. public static function formatDateTime($date=""){
  18. if(empty($date))
  19. $date = now();
  20. return date('Y-m-d h:i a',strtotime($date));
  21. }
  22. public static function input_string_sanitize($string){
  23. return htmlentities($string, ENT_QUOTES, 'UTF-8', false);
  24. }
  25. public static function format_report_number($id){
  26. return str_pad($id,6,0, STR_PAD_LEFT);
  27. }
  28. public static function generateRandomString($length = 8) {
  29. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!%$#*&^(){}[]';
  30. $charactersLength = strlen($characters);
  31. $randomString = '';
  32. for ($i = 0; $i < $length; $i++) {
  33. $randomString .= $characters[rand(0, $charactersLength - 1)];
  34. }
  35. return $randomString;
  36. }
  37. public static function generate_user_name($restaurants){
  38. return Str::slug(strtolower(($restaurants).'-'.rand(1000,9999).'-'.substr(str_shuffle($restaurants),0,3)));
  39. }
  40. public static function encrypt($id){
  41. return Crypt::encryptString($id);
  42. }
  43. public static function decrypt($string){
  44. return Crypt::decryptString($string);
  45. }
  46. public static function fileSize($bytes){
  47. $kb = 1024;
  48. $mb = $kb * 1024;
  49. $gb = $mb * 1024;
  50. $tb = $gb * 1024;
  51. if (($bytes >= 0) && ($bytes < $kb)) {
  52. return $bytes . ' B';
  53. } elseif (($bytes >= $kb) && ($bytes < $mb)) {
  54. return ceil($bytes / $kb) . ' KB';
  55. } elseif (($bytes >= $mb) && ($bytes < $gb)) {
  56. return ceil($bytes / $mb) . ' MB';
  57. } elseif (($bytes >= $gb) && ($bytes < $tb)) {
  58. return ceil($bytes / $gb) . ' GB';
  59. } elseif ($bytes >= $tb) {
  60. return ceil($bytes / $tb) . ' TB';
  61. } else {
  62. return $bytes . ' B';
  63. }
  64. }
  65. public static function generateRequestID($user_id){
  66. return date('Ymdhis').'-'.$user_id;
  67. }
  68. public static function authenticate_token($token){
  69. $user = ApiTokens::where('api_token',$token)->first();
  70. if($user->users->is_active=="0" || $user->users->is_active==0){
  71. //dd($user->users->is_active);
  72. $response = [
  73. 'type' => 'error',
  74. 'message' => 'Account is inative, contact to administrator!',
  75. 'code' =>'X003'
  76. ];
  77. echo json_encode($response);
  78. exit;
  79. }
  80. return $user;
  81. }
  82. public static function sendResponse($result, $message)
  83. {
  84. $response = [
  85. 'type' => "success",
  86. 'data' => $result,
  87. 'message' => $message,
  88. ];
  89. return response()->json($response, 200,[],JSON_UNESCAPED_UNICODE);
  90. }
  91. public static function sendError($error, $errorMessages = [], $code = 404)
  92. {
  93. $response = [
  94. 'type' => 'error',
  95. 'message' => $error,
  96. 'code' =>$code
  97. ];
  98. if(!empty($errorMessages)){
  99. $response['data'] = $errorMessages;
  100. }
  101. return response()->json($response, 400);
  102. }
  103. public static function idFormat($restoId,$id){
  104. return str_pad($restoId,4,0, STR_PAD_LEFT).'-'.str_pad($id,6,0, STR_PAD_LEFT);
  105. }
  106. public static function idFormatItem($restoId,$order_id,$id){
  107. 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);
  108. }
  109. public static function uploadFileToAWSCDN($budketName, $restoID ,$restoName ,$sourceFile,$fileName ){
  110. $s3 = new S3Client([
  111. 'version' => 'latest',
  112. 'region' => env('AWS_DEFAULT_REGION')
  113. ]);
  114. $bucket = $budketName;
  115. $image = "";
  116. try {
  117. // Upload data.
  118. $result = $s3->putObject([
  119. 'Bucket' => $bucket,
  120. 'Key' => env('BUCKET_FOLDER')."/".$restoID.'-'.\Illuminate\Support\Str::slug($restoName)."/".$fileName ,
  121. 'SourceFile' => $sourceFile,
  122. 'Body' => 'Hello, world!',
  123. 'ACL' => 'public-read'
  124. ]);
  125. // Print the URL to the object.
  126. $image = $result['ObjectURL'];
  127. $result = array('type'=>'success','url'=>$image);
  128. return $result;
  129. } catch (S3Exception $e) {
  130. $result = array('type'=>'error','message'=>$e->getMessage());
  131. return $result;
  132. }
  133. }
  134. public static function getRestuarantID(){
  135. // return 110;
  136. if(Auth::user()->role=='administrator')
  137. return Auth::id();
  138. else if(Auth::user()->role=='restaurant')
  139. return Auth::user()->restaurants->id;
  140. else if(Auth::user()->role=='admin_user')
  141. return Auth::user()->admin_users->id;
  142. else
  143. return Auth::user()->resto_users->resto_id;
  144. }
  145. public static function changeImageLinkAWStoImgix($aws_image_link,$imgix, $width,$height,$quality){
  146. return str_replace(env('AWS_URL_API'),$imgix,$aws_image_link).'?fm=webp&h='.$width.'&w='.$height.'&q='.$quality.'&fit=center&crop=center';
  147. }
  148. public static function getRestoMetas(Restaurants $restaurants){
  149. // $resto_metas = \Illuminate\Support\Facades\Cookie::get($restaurants->resto_unique_name.'_resto_metas');
  150. // if(!isset($resto_metas)){
  151. if(1){
  152. $resto_meta = isset($restaurants->resto_metas)?$restaurants->resto_metas:null;
  153. //dump($resto_meta);
  154. $resto_metas = [];
  155. $billing = [];
  156. if(isset($resto_meta)){
  157. foreach($resto_meta as $meta){
  158. if($meta->outlet_id!=""){
  159. continue;
  160. }
  161. $index_name = isset($meta->resto_meta_defs->parents)?$meta->resto_meta_defs->parents->meta_def_name:$meta->resto_meta_defs->meta_def_name;
  162. // dump($meta->resto_meta_defs);
  163. if($index_name=="BILLING_GATEWAY"){
  164. // dump($meta->resto_meta_defs->meta_def_name);
  165. // $resto_metas['BILLING_GATEWAY'][] = $meta->meta_val;
  166. $billing[] = array('id'=>$meta->meta_id,'value'=>$meta->meta_val);
  167. }
  168. $resto_metas[$index_name] = $meta->meta_val;
  169. }
  170. }
  171. $resto_metas['BILLING_GATEWAY'] = $billing;
  172. //\Illuminate\Support\Facades\Cookie::queue($restaurants->resto_unique_name.'_resto_metas',json_encode($resto_metas),'3600');
  173. return $resto_metas;
  174. }else{
  175. $resto_metas = json_decode($resto_metas,true);
  176. return $resto_metas;
  177. }
  178. }
  179. public static function getIPBasedInfo($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
  180. $output = NULL;
  181. if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
  182. $ip = $_SERVER["REMOTE_ADDR"];
  183. if ($deep_detect) {
  184. if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
  185. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  186. if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
  187. $ip = $_SERVER['HTTP_CLIENT_IP'];
  188. }
  189. }
  190. $purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
  191. $support = array("country", "countrycode", "state", "region", "city", "location", "address");
  192. $continents = array(
  193. "AF" => "Africa",
  194. "AN" => "Antarctica",
  195. "AS" => "Asia",
  196. "EU" => "Europe",
  197. "OC" => "Australia (Oceania)",
  198. "NA" => "North America",
  199. "SA" => "South America"
  200. );
  201. if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
  202. $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
  203. if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
  204. switch ($purpose) {
  205. case "location":
  206. $output = array(
  207. "city" => @$ipdat->geoplugin_city,
  208. "state" => @$ipdat->geoplugin_regionName,
  209. "country" => @$ipdat->geoplugin_countryName,
  210. "country_code" => @$ipdat->geoplugin_countryCode,
  211. "continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
  212. "continent_code" => @$ipdat->geoplugin_continentCode,
  213. 'currency' => @$ipdat->geoplugin_currencyCode
  214. );
  215. break;
  216. case "address":
  217. $address = array($ipdat->geoplugin_countryName);
  218. if (@strlen($ipdat->geoplugin_regionName) >= 1)
  219. $address[] = $ipdat->geoplugin_regionName;
  220. if (@strlen($ipdat->geoplugin_city) >= 1)
  221. $address[] = $ipdat->geoplugin_city;
  222. $output = implode(", ", array_reverse($address));
  223. break;
  224. case "city":
  225. $output = @$ipdat->geoplugin_city;
  226. break;
  227. case "state":
  228. $output = @$ipdat->geoplugin_regionName;
  229. break;
  230. case "region":
  231. $output = @$ipdat->geoplugin_regionName;
  232. break;
  233. case "country":
  234. $output = @$ipdat->geoplugin_countryName;
  235. break;
  236. case "countrycode":
  237. $output = @$ipdat->geoplugin_countryCode;
  238. break;
  239. }
  240. }
  241. }
  242. return $output;
  243. }
  244. }