SettingsController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 rglob($pattern, $flags = 0) {
  63. $files = glob($pattern, $flags);
  64. foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
  65. $files = array_merge($files, $this::rglob($dir.'/'.basename($pattern), $flags));
  66. }
  67. return $files;
  68. }
  69. public function downloadAllInvoices(){
  70. $zip = new ZipArchive;
  71. $zip_safe_path = storage_path('invoices.zip');
  72. $res = $zip->open($zip_safe_path, ZipArchive::CREATE|ZipArchive::OVERWRITE);
  73. $result = $this::rglob(storage_path('app/invoice/*'));
  74. if ($res === TRUE) {
  75. $zip->addFromString("1. Info.txt","This Archive contains all Invoices from all Users!");
  76. foreach($result as $file){
  77. if (file_exists($file) && is_file($file)) {
  78. $zip->addFile($file,basename($file));
  79. }
  80. }
  81. $zip->close();
  82. }
  83. if (file_exists($zip_safe_path) && is_file($zip_safe_path)) {
  84. return response()->download($zip_safe_path);
  85. }else{
  86. $this->index()->with('failure', 'No Invoices in Storage!');
  87. }
  88. }
  89. }