8-import_customers_activity.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace cx_newsletter;
  3. class import_customers_activity
  4. extends activity {
  5. public function show_after_button() : string {
  6. return 'import';
  7. }
  8. public function get_site_template_name() : string {
  9. return 'import_customers';
  10. }
  11. public function inside_buttons() : array {
  12. return [ 'clean' ];
  13. }
  14. public function process() : self {
  15. if ($this->is_received('import')) {
  16. return $this->import();
  17. } elseif($this->is_received('clean')) {
  18. return $this->clean();
  19. }
  20. }
  21. public function clean() : self {
  22. $mapper = new customers_mapper(
  23. $this->get_database(),
  24. $this->get_tables()
  25. );
  26. $customers = $mapper->load_all();
  27. try {
  28. foreach ($customers as $customer) {
  29. $mapper->remove($customer);
  30. }
  31. $this->success_toast('Customers database cleaned success.');
  32. } catch (\exception $exception) {
  33. $this->error_toast($exception->getMessage());
  34. }
  35. return $this;
  36. }
  37. public function import() : self {
  38. if (!isset($_FILES['upload'])) {
  39. $this->error_toast('Input with file not exists.');
  40. return $this;
  41. }
  42. if (!is_uploaded_file($_FILES['upload']['tmp_name'])) {
  43. $this->error_toast('Not upload file that would be imported.');
  44. return $this;
  45. }
  46. $coded = file_get_contents($_FILES['upload']['tmp_name']);
  47. $decoded = json_decode($coded, true);
  48. $group = null;
  49. if ($this->get_received('group') !== 'NULL') {
  50. $group = $this->get_received('group');
  51. }
  52. if ($decoded === null) {
  53. $this->error_toast('Uploaded JSON is not valid.');
  54. return $this;
  55. }
  56. ini_set('max_execution_time', '360');
  57. $mapper = new customers_mapper(
  58. $this->get_database(),
  59. $this->get_tables()
  60. );
  61. $validator = new customer_validator(
  62. $this->get_database(),
  63. $this->get_tables()
  64. );
  65. $converter = new customers_converter();
  66. $added = 0;
  67. $failed = 0;
  68. foreach ($decoded as $item) {
  69. $item['grouped'] = $group;
  70. try {
  71. $customer = $converter->load_array($item)->get_object();
  72. $validator->load($customer)->check();
  73. if (!$validator->is_valid()) {
  74. $failed += 1;
  75. continue;
  76. }
  77. } catch (\exception $exception) {
  78. $error = new \cx_appengine\string_builder();
  79. $error->push(
  80. __('Customers file is corrupted.', 'cx_newsletter').' '
  81. );
  82. $error->push($exception->getMessage());
  83. $this->error_toast($error->get());
  84. $failed += 1;
  85. continue;
  86. }
  87. try {
  88. $mapper->create($customer);
  89. $added += 1;
  90. } catch (\exception $exception) {
  91. $this->error_toast($exception->getMessage());
  92. $failed += 1;
  93. continue;
  94. }
  95. }
  96. $result = new \cx_appengine\string_builder();
  97. $result->push(__('Import new customers success.', 'cx_newsletter'));
  98. $result->push('<br>');
  99. $result->push(__('Added: ', 'cx_newsletter').$added.'<br>');
  100. $result->push(__('Failed: ', 'cx_newsletter').$failed);
  101. $this->success_toast($result->get());
  102. return $this;
  103. }
  104. }