SettingsController.php 3.2 KB

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