SettingsController.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\invoiceSettings;
  5. use Illuminate\Contracts\Foundation\Application;
  6. use Illuminate\Contracts\View\Factory;
  7. use Illuminate\Contracts\View\View;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Http\Response;
  10. use ZipArchive;
  11. class SettingsController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return Application|Factory|View|Response
  17. */
  18. public function index()
  19. {
  20. return view('admin.settings.index',
  21. [
  22. 'company_name' => invoiceSettings::get()->first()->company_name,
  23. 'company_adress' => invoiceSettings::get()->first()->company_adress,
  24. 'company_phone' => invoiceSettings::get()->first()->company_phone,
  25. 'company_vat' => invoiceSettings::get()->first()->company_vat,
  26. 'company_mail' => invoiceSettings::get()->first()->company_mail,
  27. 'company_web' => invoiceSettings::get()->first()->company_web
  28. ]);
  29. }
  30. public function updateIcons(Request $request)
  31. {
  32. $request->validate([
  33. 'icon' => 'nullable|max:10000|mimes:jpg,png,jpeg',
  34. 'favicon' => 'nullable|max:10000|mimes:ico',
  35. ]);
  36. if ($request->hasFile('icon')) {
  37. $request->file('icon')->storeAs('public', 'icon.png');
  38. }
  39. if ($request->hasFile('favicon')) {
  40. $request->file('favicon')->storeAs('public', 'favicon.ico');
  41. }
  42. return redirect()->route('admin.settings.index')->with('success', 'Icons updated!');
  43. }
  44. public function updateInvoiceSettings(Request $request)
  45. {
  46. $request->validate([
  47. 'logo' => 'nullable|max:10000|mimes:jpg,png,jpeg',
  48. ]);
  49. invoiceSettings::updateOrCreate(['id' => "1"], ['company_name' => $request->get('company-name')]);
  50. invoiceSettings::updateOrCreate(['id' => "1",], ['company_adress' => $request->get('company-adress')]);
  51. invoiceSettings::updateOrCreate(['id' => "1",], ['company_phone' => $request->get('company-phone')]);
  52. invoiceSettings::updateOrCreate(['id' => "1",], ['company_mail' => $request->get('company-mail')]);
  53. invoiceSettings::updateOrCreate(['id' => "1",], ['company_vat' => $request->get('company-vat')]);
  54. invoiceSettings::updateOrCreate(['id' => "1",], ['company_web' => $request->get('company-web')]);
  55. if ($request->hasFile('logo')) {
  56. $request->file('logo')->storeAs('public', 'logo.png');
  57. }
  58. return redirect()->route('admin.settings.index')->with('success', 'Invoice settings updated!');
  59. }
  60. public function rglob($pattern, $flags = 0) {
  61. $files = glob($pattern, $flags);
  62. foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
  63. $files = array_merge($files, $this::rglob($dir.'/'.basename($pattern), $flags));
  64. }
  65. return $files;
  66. }
  67. public function downloadAllInvoices(){
  68. $zip = new ZipArchive;
  69. $zip_safe_path = storage_path('invoices.zip');
  70. $res = $zip->open($zip_safe_path, ZipArchive::CREATE);
  71. $result = $this::rglob(storage_path('app/invoice/*'));
  72. if ($res === TRUE) {
  73. foreach($result as $file){
  74. if (file_exists($file) && is_file($file)) {
  75. $zip->addFile($file,basename($file));
  76. }
  77. }
  78. $zip->close();
  79. }
  80. return response()->download($zip_safe_path);
  81. }
  82. }