Forked from jack2jm/Important functions - Laravel-PHP mast mast
Created
February 12, 2024 05:50
-
-
Save Bhavya8181/edb490eb2dcaa2c68b3eeb8abcb1bbe9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. **************************************** Import CSV ************************************* | |
public static function importCsv() | |
{ | |
$file = public_path('file/test.csv'); | |
$customerArr = $this->csvToArray($file); | |
echo "<pre>"; | |
for ($i = 0; $i < count($customerArr); $i ++){ | |
print_r($customerArr[$i]); | |
} | |
return 'Jobi done or what ever'; | |
} | |
2. **************************************** CSV to Array ************************************* | |
//Use it like below | |
$data = Functions::csvToArray(public_path('items.csv')); | |
echo "<pre>"; | |
print_r(['Total count' => count($data)]); | |
//Define function | |
public static function csvToArray($filename = '', $delimiter = ','){ | |
if (!file_exists($filename) || !is_readable($filename)) | |
return false; | |
$header = null; | |
$data = array(); | |
if (($handle = fopen($filename, 'r')) !== false) | |
{ | |
while (($row = fgetcsv($handle, 10000, $delimiter)) !== false) | |
{ | |
if (!$header) | |
$header = $row; | |
else | |
$data[] = array_combine($header, $row); | |
} | |
fclose($handle); | |
} | |
return $data; | |
} | |
3. Create storage folder/directory | |
$base_path = 'public/clients-documents/'. $client_id; | |
$path=$base_path; | |
if(!\Storage::exists($base_path)) { | |
\Storage::makeDirectory($base_path); //creates directory | |
} | |
$case_folder_path = $base_path."/case_".$case_id; | |
if(!\Storage::exists($case_folder_path)) { | |
\Storage::makeDirectory($case_folder_path); //creates directory | |
} | |
//Lopping to create multiple folders | |
$folders_arr = ['Correspondence','Documents','Submissions','Shared Workspace']; | |
foreach($folders_arr as $folder){ | |
if(!\Storage::exists($case_folder_path."/".$folder)) { | |
\Storage::makeDirectory($case_folder_path."/".$folder); //creates directory | |
} | |
} | |
4. Strip Tags Input value laravel request | |
public static function addStripTagsInValue($input){ | |
foreach($input as $key=>$value){ | |
$input[$key] = strip_tags($value); | |
} | |
return $input; | |
} | |
5. Contains Files in folders | |
public static function countFilesInFolder($folder) | |
{ | |
$count = 0; | |
// Get the list of files and directories in the current folder | |
$contents = \Storage::files($folder); | |
$subfolders = \Storage::directories($folder); | |
// Count files in the current folder | |
$count += count($contents); | |
// Recursively count files in subfolders | |
foreach ($subfolders as $subfolder) { | |
$count += self::countFilesInFolder($subfolder); | |
} | |
return $count; | |
} | |
6. Convert to date to timezone | |
//use below like | |
$display_date_format = "M d, Y g:i A T"; | |
$eventTimezone = "Asia/Kolkata"; | |
$display_start_date_time = Functions::convertDateFromTimezone($row->start_datetime, 'UTC', $eventTimezone, $display_date_format); | |
public static function convertDateFromTimezone($date,$fromTimezone,$toTimezone,$dateFormat){ | |
$date = Carbon::create($date, $fromTimezone)->setTimezone($toTimezone)->toDateTime()->format($dateFormat); | |
return $date; | |
} | |
7. Test mail route to check email is working ornot | |
MAIL_DRIVER=smtp | |
MAIL_HOST=smtp.sendgrid.net | |
MAIL_PORT=587/465 | |
MAIL_USERNAME=apikey | |
MAIL_PASSWORD="here you will get key from sendgrid" | |
MAIL_ENCRYPTION=TLS/SSL | |
MAIL_FROM_ADDRESS=jatin@gmail.com | |
MAIL_FROM_NAME="Jatin Mandanka" | |
Route::get('/test-mail', function () { | |
try { | |
Mail::raw('Test Email!', function ($message) { | |
$message->to('jatin@gmail.com')->subject('Testing mails'); | |
}); | |
echo "Sent..."; | |
} catch(Exception $e) { | |
echo $e->getMessage(); | |
} | |
dd('sent mail'); | |
}); | |
8. Get Site or Base url | |
public static function getSiteUrl(){ | |
return sprintf( | |
"%s://%s%s", | |
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', | |
$_SERVER['SERVER_NAME'], | |
$_SERVER['REQUEST_URI'] | |
); | |
} | |
public static function getSiteBaseUrl(){ | |
return sprintf( | |
"%s://%s", | |
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', | |
$_SERVER['SERVER_NAME'] | |
); | |
} | |
9. GET CURL / POST CURL | |
public static function httpGetByURL($url, $headers = false, $timeout = 3, $config = false){ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
if($headers) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
if($config) { | |
foreach ($config as $item) { | |
curl_setopt($ch, $item['option'], $item['value']); | |
} | |
} | |
$data = curl_exec($ch); | |
curl_close($ch); | |
return $data; | |
} | |
public static function httpPostDataByURL($url, $post_data, $headers = false, $timeout = 3, $config = false) { | |
$ch = curl_init(); | |
$timeout = $timeout; | |
curl_setopt($ch,CURLOPT_URL, $url); | |
curl_setopt($ch,CURLOPT_POST,1); | |
curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | |
if($headers) | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
if($config) { | |
foreach ($config as $item) { | |
curl_setopt($ch, $item['option'], $item['value']); | |
} | |
} | |
$data = curl_exec($ch); | |
curl_close($ch); | |
return $data; | |
} | |
10. DOM pdf / view render html | |
$path = storage_path('app/public/downloaded-resume/'. $user['id']); | |
Storage::makeDirectory('public/downloaded-resume/'. $user['id']); | |
//View mate | |
return view('user.resume-templates.1.preview1', compact(['user', 'input', 'section_order_list'])); | |
//generate pdf | |
$pdf = \PDF::loadView('user.resume-templates.1.preview1', compact(['user', 'input', 'section_order_list'])); | |
$userResume = UserResume::where('id', $id)->first(); | |
$generated_pdf_name = $userResume['resume_name'] . '.pdf'; | |
// $generated_pdf_name = 'Jack-' . $user['id'] . '_' . $user['first_name'] . '_' . $user['last_name'] . '.pdf'; | |
$pdf->save($path. '/' . $generated_pdf_name); | |
$fileURL = url('storage/downloaded-resume/' . $user['id'] . '/'. $generated_pdf_name); | |
return $pdf->download($generated_pdf_name); | |
11. Genrate html string for PDF | |
//generate HTML string | |
$view = \View::make("resume.template1", [ | |
'user' => $userResumeData, 'input' => $input, | |
'default_theme_color_codes' => $default_theme_color_codes | |
]); | |
$html = $view->render(); | |
$fileURL = url('storage/downloaded-resume/' . $userResumeData['user_id'] . '/'. $generated_pdf_name); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment