SettingsController.php 3.9 KB

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