5-manage_customers_activity.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace cx_newsletter;
  3. class manage_customers_activity
  4. extends activity {
  5. public function show_after_button() : null {
  6. return null;
  7. }
  8. public function inside_buttons() : array {
  9. return [ 'add', 'save', 'remove', 'search' ];
  10. }
  11. public function inside_inputs() : array {
  12. return [
  13. 'company' => 'string',
  14. 'name' => 'string',
  15. 'surname' => 'string',
  16. 'comments' => 'string ',
  17. 'email' => 'email',
  18. 'phone_number' => 'phone',
  19. 'tester' => 'bool',
  20. 'limit' => 'int',
  21. 'filter' => 'string',
  22. 'id' => 'int',
  23. 'group' => 'custom',
  24. ];
  25. }
  26. public function get_site_template_name() : string {
  27. return 'manage_customers';
  28. }
  29. private function get_all_groups() : array {
  30. }
  31. private function get_limit() : int {
  32. if ($this->is_validated('limit')) {
  33. $this->set('limit', $this->get_validated('limit'));
  34. return $this->get_validated('limit');
  35. }
  36. $this->warning_toast('Limit is not correct, it must be number.');
  37. return 50;
  38. }
  39. private function get_filter() : string {
  40. if ($this->is_validated('filter')) {
  41. $this->set('filter', $this->get_validated('filter'));
  42. return $this->get_validated('filter');
  43. }
  44. $this->warning_toast('Filter value is not correct.');
  45. return '';
  46. }
  47. private function prepare_groups(?group $target = null) : string {
  48. $render = new \cx_appengine\string_builder();
  49. if ($target !== null) {
  50. $target = $target->get_id();
  51. }
  52. foreach ($this->groups as $group) {
  53. $options = [];
  54. if ($group->has_id()) {
  55. $options['group_id'] = $group->get_id();
  56. } else {
  57. $options['group_id'] = 'NULL';
  58. }
  59. $selected = false;
  60. $group_id = null;
  61. if ($group->has_id()) {
  62. $group_id = $group->get_id();
  63. }
  64. if ($target === $group_id) {
  65. $selected = true;
  66. }
  67. if ($selected) {
  68. $options['selected'] = 'selected';
  69. }
  70. $options['group_name'] = $group->name;
  71. $render->push(
  72. $this
  73. ->get_templates()
  74. ->prepare('single_group_option')
  75. ->render($options)
  76. );
  77. }
  78. return $render->get();
  79. }
  80. private function load_customers() : \cx_appengine\string_builder {
  81. $limit = 50;
  82. $filter = '';
  83. $list = new \cx_appengine\string_builder();
  84. if ($this->is_received('search')) {
  85. $limit = $this->get_limit();
  86. $filter = $this->get_filter();
  87. }
  88. try {
  89. $customers = $this->mapper->find($filter, $limit);
  90. $converter = new customers_converter();
  91. foreach ($customers as $customer) {
  92. $options = $converter->load_object($customer)->get_array();
  93. if ($customer->tester) {
  94. $options['tester'] = 'checked';
  95. }
  96. $options['groups'] = $this->prepare_groups($customer->group);
  97. $list->push(
  98. $this
  99. ->get_templates()
  100. ->prepare('single_customer')
  101. ->render($options)
  102. );
  103. }
  104. } catch (\exception $exception) {
  105. $this->error_toast('Can not list. '.$exception->getMessage());
  106. } finally {
  107. return $list;
  108. }
  109. }
  110. public function preprocess() : void {
  111. $this->mapper = new customers_mapper(
  112. $this->get_database(),
  113. $this->get_tables()
  114. );
  115. $this->groups_mapper = new groups_mapper(
  116. $this->get_database(),
  117. $this->get_tables()
  118. );
  119. $this->groups = $this->groups_mapper->load_all();
  120. $nogroup = new group();
  121. $nogroup->name = 'nogroup';
  122. array_push($this->groups, $nogroup);
  123. }
  124. private function check_validation(array $list) : bool {
  125. foreach ($list as $item) {
  126. if (!$this->is_validated($item)) {
  127. $this->error_toast(
  128. $item.__(' is not correct.', 'cx_newsletter')
  129. );
  130. return false;
  131. }
  132. }
  133. return true;
  134. }
  135. private function get_customer_from_inputs() : ?customer {
  136. $id = null;
  137. if ($this->is_validated('id')) {
  138. $id = $this->get_validated('id');
  139. }
  140. $list = [
  141. 'name',
  142. 'surname',
  143. 'comments',
  144. 'company',
  145. ];
  146. if (!$this->check_validation($list)) {
  147. return null;
  148. }
  149. $customer = new customer($id);
  150. $customer->name = $this->get_validated('name');
  151. $customer->surname = $this->get_validated('surname');
  152. $customer->company = $this->get_validated('company');
  153. $customer->comments = $this->get_validated('comments');
  154. $customer->email = $this->get_received('email', '');
  155. $customer->phone_number = $this->get_received('phone_number', '');
  156. $customer->tester = $this->is_received('tester');
  157. $group = $this->get_received('group');
  158. if ($group === 'NULL') {
  159. $customer->group = null;
  160. } else {
  161. $customer->group = new group(intval($group));
  162. }
  163. $validator = new customer_validator(
  164. $this->get_database(),
  165. $this->get_tables()
  166. );
  167. $validator
  168. ->load($customer)
  169. ->check();
  170. if (!$validator->is_valid()) {
  171. $this->error_toast($validator->error_on());
  172. return null;
  173. }
  174. return $customer;
  175. }
  176. public function add_customer() : void {
  177. $customer = $this->get_customer_from_inputs();
  178. if ($customer === null) {
  179. return;
  180. }
  181. try {
  182. $this->mapper->create($customer);
  183. $this->success_toast('Add customer success.');
  184. } catch (\exception $exception) {
  185. $this->error_toast($exception->getMessage());
  186. }
  187. }
  188. public function save_customer() : void {
  189. $customer = $this->get_customer_from_inputs();
  190. if ($customer === null) {
  191. return;
  192. }
  193. try {
  194. $this->mapper->save($customer);
  195. $this->success_toast('Save customer success.');
  196. } catch (\exception $exception) {
  197. $this->error_toast($exception->getMessage());
  198. }
  199. }
  200. public function remove_customer() : void {
  201. $customer = $this->get_customer_from_inputs();
  202. if ($customer === null) {
  203. return;
  204. }
  205. try {
  206. $this->mapper->remove($customer);
  207. $this->success_toast('Remove customer success.');
  208. } catch (\exception $exception) {
  209. $this->error_toast($exception->getMessage());
  210. }
  211. }
  212. public function process() : self {
  213. $this->preprocess();
  214. if ($this->is_received('add')) {
  215. $this->add_customer();
  216. }
  217. if ($this->is_received('save')) {
  218. $this->save_customer();
  219. }
  220. if ($this->is_received('remove')) {
  221. $this->remove_customer();
  222. }
  223. $this->set('groups', $this->prepare_groups());
  224. $this->set('all_customers', $this->load_customers());
  225. $this->set(
  226. 'export',
  227. \get_home_url().'/cx-newsletter/customers-export'
  228. );
  229. return $this;
  230. }
  231. private array $groups;
  232. private groups_mapper $groups_mapper;
  233. private customers_mapper $mapper;
  234. }