SettingsController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Http\Controllers\Admin\SettingsControllers;
  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. //Get all tabs as laravel view paths
  21. $tabs = [];
  22. foreach (glob(resource_path('views/admin/settings/tabs/*.blade.php')) as $filename) {
  23. $tabs[] = 'admin.settings.tabs.' . basename($filename, '.blade.php');
  24. }
  25. //Generate a html list item for each tab based on tabs file basename, set first tab as active
  26. $tabListItems = [];
  27. foreach ($tabs as $tab) {
  28. $tabName = str_replace('admin.settings.tabs.', '', $tab);
  29. $tabListItems[] = '<li class="nav-item">
  30. <a class="nav-link ' . (empty($tabListItems) ? 'active' : '') . '" data-toggle="pill" href="#' . $tabName . '">
  31. ' . __(ucfirst($tabName)) . '
  32. </a></li>';
  33. }
  34. return view('admin.settings.index', [
  35. 'tabs' => $tabs,
  36. 'tabListItems' => $tabListItems,
  37. ]);;
  38. }
  39. public function updateIcons(Request $request)
  40. {
  41. $request->validate([
  42. 'icon' => 'nullable|max:10000|mimes:jpg,png,jpeg',
  43. 'favicon' => 'nullable|max:10000|mimes:ico',
  44. ]);
  45. if ($request->hasFile('icon')) {
  46. $request->file('icon')->storeAs('public', 'icon.png');
  47. }
  48. if ($request->hasFile('favicon')) {
  49. $request->file('favicon')->storeAs('public', 'favicon.ico');
  50. }
  51. return redirect()->route('admin.settings.index')->with('success', __('Icons updated!'));
  52. }
  53. public function updateInvoiceSettings(Request $request)
  54. {
  55. $request->validate([
  56. 'logo' => 'nullable|max:10000|mimes:jpg,png,jpeg',
  57. ]);
  58. InvoiceSettings::updateOrCreate([
  59. 'id' => "1"
  60. ], [
  61. 'company_name' => $request->get('company-name'),
  62. 'company_adress' => $request->get('company-address'),
  63. 'company_phone' => $request->get('company-phone'),
  64. 'company_mail' => $request->get('company-mail'),
  65. 'company_vat' => $request->get('company-vat'),
  66. 'company_web' => $request->get('company-web'),
  67. 'invoice_prefix' => $request->get('invoice-prefix'),
  68. ]);
  69. if ($request->hasFile('logo')) {
  70. $request->file('logo')->storeAs('public', 'logo.png');
  71. }
  72. return redirect()->route('admin.settings.index')->with('success', 'Invoice settings updated!');
  73. }
  74. public function downloadAllInvoices()
  75. {
  76. $zip = new ZipArchive;
  77. $zip_safe_path = storage_path('invoices.zip');
  78. $res = $zip->open($zip_safe_path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
  79. $result = $this::rglob(storage_path('app/invoice/*'));
  80. if ($res === TRUE) {
  81. $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!");
  82. foreach ($result as $file) {
  83. if (file_exists($file) && is_file($file)) {
  84. $zip->addFile($file, basename($file));
  85. }
  86. }
  87. $zip->close();
  88. }
  89. return response()->download($zip_safe_path);
  90. }
  91. public function rglob($pattern, $flags = 0)
  92. {
  93. $files = glob($pattern, $flags);
  94. foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
  95. $files = array_merge($files, $this::rglob($dir . '/' . basename($pattern), $flags));
  96. }
  97. return $files;
  98. }
  99. }