NameRuleExport.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Exports;
  3. use App\Models\CustomField;
  4. use App\Models\Enums\CustomFieldType;
  5. use App\Models\NamingRule;
  6. use Maatwebsite\Excel\Concerns\FromCollection;
  7. use Maatwebsite\Excel\Concerns\WithHeadings;
  8. class NameRuleExport implements FromCollection, WithHeadings
  9. {
  10. public function collection()
  11. {
  12. $objects = collect([]);
  13. $customFields=CustomField::query()->where('group',request()->group)->get();
  14. foreach ($customFields as $customField){
  15. $key=$customField['key'];
  16. $options=$customField->options;
  17. $inputs=$customField->inputs;
  18. $type=CustomFieldType::from($customField->type)->name;
  19. if($customField->type==3){
  20. foreach ($options as $option){
  21. $customKey=$option['lang']['en'];
  22. $customValue=$option['value'];
  23. $objects->push($this->formatRow($key,$type,$customKey,$customValue));
  24. }
  25. }
  26. if($customField->type==1){
  27. foreach ($inputs as $input){
  28. $customKey=$input['lang']['en'];
  29. $customValue=$input['value'];
  30. $objects->push($this->formatRow($key,$type,$customKey,$customValue));
  31. }
  32. }
  33. $objects->push($this->formatRow('','','',''));
  34. }
  35. return $objects;
  36. // TODO: Implement collection() method.
  37. }
  38. protected function formatRow($key,$type,$customKey,$customValue)
  39. {
  40. return [
  41. $key,
  42. $type,
  43. $customKey,
  44. $customValue,
  45. ];
  46. }
  47. public function headings(): array
  48. {
  49. return [
  50. 'key',
  51. 'type',
  52. 'customKey',
  53. 'customValue',
  54. ];
  55. }
  56. }