SettingsController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. 'invoice_prefix' => InvoiceSettings::get()->first()->invoice_prefix
  29. ]);
  30. }
  31. public function updateIcons(Request $request)
  32. {
  33. $request->validate([
  34. 'icon' => 'nullable|max:10000|mimes:jpg,png,jpeg',
  35. 'favicon' => 'nullable|max:10000|mimes:ico',
  36. ]);
  37. if ($request->hasFile('icon')) {
  38. $request->file('icon')->storeAs('public', 'icon.png');
  39. }
  40. if ($request->hasFile('favicon')) {
  41. $request->file('favicon')->storeAs('public', 'favicon.ico');
  42. }
  43. return redirect()->route('admin.settings.index')->with('success', 'Icons updated!');
  44. }
  45. public function updateInvoiceSettings(Request $request)
  46. {
  47. $request->validate([
  48. 'logo' => 'nullable|max:10000|mimes:jpg,png,jpeg',
  49. ]);
  50. InvoiceSettings::updateOrCreate(['id' => "1"], ['company_name' => $request->get('company-name')]);
  51. InvoiceSettings::updateOrCreate(['id' => "1",], ['company_adress' => $request->get('company-adress')]);
  52. InvoiceSettings::updateOrCreate(['id' => "1",], ['company_phone' => $request->get('company-phone')]);
  53. InvoiceSettings::updateOrCreate(['id' => "1",], ['company_mail' => $request->get('company-mail')]);
  54. InvoiceSettings::updateOrCreate(['id' => "1",], ['company_vat' => $request->get('company-vat')]);
  55. InvoiceSettings::updateOrCreate(['id' => "1",], ['company_web' => $request->get('company-web')]);
  56. InvoiceSettings::updateOrCreate(['id' => "1",], ['invoice_prefix' => $request->get('invoice-prefix')]);
  57. if ($request->hasFile('logo')) {
  58. $request->file('logo')->storeAs('public', 'logo.png');
  59. }
  60. return redirect()->route('admin.settings.index')->with('success', 'Invoice settings updated!');
  61. }
  62. public function downloadAllInvoices()
  63. {
  64. $zip = new ZipArchive;
  65. $zip_safe_path = storage_path('invoices.zip');
  66. $res = $zip->open($zip_safe_path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
  67. $result = $this::rglob(storage_path('app/invoice/*'));
  68. if ($res === TRUE) {
  69. $zip->addFromString("1. Info.txt", "This Archive contains all Invoices from all Users!\nIf there are no Invoices here, no Invoices have ever been created!");
  70. foreach ($result as $file) {
  71. if (file_exists($file) && is_file($file)) {
  72. $zip->addFile($file, basename($file));
  73. }
  74. }
  75. $zip->close();
  76. }
  77. return response()->download($zip_safe_path);
  78. }
  79. public function rglob($pattern, $flags = 0)
  80. {
  81. $files = glob($pattern, $flags);
  82. foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
  83. $files = array_merge($files, $this::rglob($dir . '/' . basename($pattern), $flags));
  84. }
  85. return $files;
  86. }
  87. }