123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace App\Helpers;
- use App\Companies;
- use Illuminate\Support\Facades\Crypt;
- use Str;
- use App\ApiTokens;
- 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);
- }
- }
|