|
|
@@ -0,0 +1,297 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace cx_newsletter;
|
|
|
+
|
|
|
+class manage_customers_activity
|
|
|
+extends activity {
|
|
|
+ public function show_after_button() : null {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function inside_buttons() : array {
|
|
|
+ return [ 'add', 'save', 'remove', 'search' ];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function inside_inputs() : array {
|
|
|
+ return [
|
|
|
+ 'company' => 'string',
|
|
|
+ 'name' => 'string',
|
|
|
+ 'surname' => 'string',
|
|
|
+ 'comments' => 'string ',
|
|
|
+ 'email' => 'email',
|
|
|
+ 'phone_number' => 'phone',
|
|
|
+ 'tester' => 'bool',
|
|
|
+ 'limit' => 'int',
|
|
|
+ 'filter' => 'string',
|
|
|
+ 'id' => 'int',
|
|
|
+ 'group' => 'custom',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function get_site_template_name() : string {
|
|
|
+ return 'manage_customers';
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_all_groups() : array {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_limit() : int {
|
|
|
+ if ($this->is_validated('limit')) {
|
|
|
+ $this->set('limit', $this->get_validated('limit'));
|
|
|
+ return $this->get_validated('limit');
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->warning_toast('Limit is not correct, it must be number.');
|
|
|
+ return 50;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_filter() : string {
|
|
|
+ if ($this->is_validated('filter')) {
|
|
|
+ $this->set('filter', $this->get_validated('filter'));
|
|
|
+ return $this->get_validated('filter');
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->warning_toast('Filter value is not correct.');
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+
|
|
|
+ private function prepare_groups(?group $target = null) : string {
|
|
|
+ $render = new \cx_appengine\string_builder();
|
|
|
+
|
|
|
+ if ($target !== null) {
|
|
|
+ $target = $target->get_id();
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($this->groups as $group) {
|
|
|
+ $options = [];
|
|
|
+
|
|
|
+ if ($group->has_id()) {
|
|
|
+ $options['group_id'] = $group->get_id();
|
|
|
+ } else {
|
|
|
+ $options['group_id'] = 'NULL';
|
|
|
+ }
|
|
|
+
|
|
|
+ $selected = false;
|
|
|
+ $group_id = null;
|
|
|
+
|
|
|
+ if ($group->has_id()) {
|
|
|
+ $group_id = $group->get_id();
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($target === $group_id) {
|
|
|
+ $selected = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($selected) {
|
|
|
+ $options['selected'] = 'selected';
|
|
|
+ }
|
|
|
+
|
|
|
+ $options['group_name'] = $group->name;
|
|
|
+
|
|
|
+ $render->push(
|
|
|
+ $this
|
|
|
+ ->get_templates()
|
|
|
+ ->prepare('single_group_option')
|
|
|
+ ->render($options)
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ return $render->get();
|
|
|
+ }
|
|
|
+
|
|
|
+ private function load_customers() : \cx_appengine\string_builder {
|
|
|
+ $limit = 50;
|
|
|
+ $filter = '';
|
|
|
+ $list = new \cx_appengine\string_builder();
|
|
|
+
|
|
|
+ if ($this->is_received('search')) {
|
|
|
+ $limit = $this->get_limit();
|
|
|
+ $filter = $this->get_filter();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ $customers = $this->mapper->find($filter, $limit);
|
|
|
+ $converter = new customers_converter();
|
|
|
+
|
|
|
+ foreach ($customers as $customer) {
|
|
|
+ $options = $converter->load_object($customer)->get_array();
|
|
|
+
|
|
|
+ if ($customer->tester) {
|
|
|
+ $options['tester'] = 'checked';
|
|
|
+ }
|
|
|
+
|
|
|
+ $options['groups'] = $this->prepare_groups($customer->group);
|
|
|
+
|
|
|
+ $list->push(
|
|
|
+ $this
|
|
|
+ ->get_templates()
|
|
|
+ ->prepare('single_customer')
|
|
|
+ ->render($options)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ } catch (\exception $exception) {
|
|
|
+ $this->error_toast('Can not list. '.$exception->getMessage());
|
|
|
+ } finally {
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function preprocess() : void {
|
|
|
+ $this->mapper = new customers_mapper(
|
|
|
+ $this->get_database(),
|
|
|
+ $this->get_tables()
|
|
|
+ );
|
|
|
+
|
|
|
+ $this->groups_mapper = new groups_mapper(
|
|
|
+ $this->get_database(),
|
|
|
+ $this->get_tables()
|
|
|
+ );
|
|
|
+
|
|
|
+ $this->groups = $this->groups_mapper->load_all();
|
|
|
+
|
|
|
+ $nogroup = new group();
|
|
|
+ $nogroup->name = 'nogroup';
|
|
|
+
|
|
|
+ array_push($this->groups, $nogroup);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function check_validation(array $list) : bool {
|
|
|
+ foreach ($list as $item) {
|
|
|
+ if (!$this->is_validated($item)) {
|
|
|
+ $this->error_toast(
|
|
|
+ $item.__(' is not correct.', 'cx_newsletter')
|
|
|
+ );
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_customer_from_inputs() : ?customer {
|
|
|
+ $id = null;
|
|
|
+
|
|
|
+ if ($this->is_validated('id')) {
|
|
|
+ $id = $this->get_validated('id');
|
|
|
+ }
|
|
|
+
|
|
|
+ $list = [
|
|
|
+ 'name',
|
|
|
+ 'surname',
|
|
|
+ 'comments',
|
|
|
+ 'company',
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (!$this->check_validation($list)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ $customer = new customer($id);
|
|
|
+ $customer->name = $this->get_validated('name');
|
|
|
+ $customer->surname = $this->get_validated('surname');
|
|
|
+ $customer->company = $this->get_validated('company');
|
|
|
+ $customer->comments = $this->get_validated('comments');
|
|
|
+ $customer->email = $this->get_received('email', '');
|
|
|
+ $customer->phone_number = $this->get_received('phone_number', '');
|
|
|
+ $customer->tester = $this->is_received('tester');
|
|
|
+
|
|
|
+ $group = $this->get_received('group');
|
|
|
+
|
|
|
+ if ($group === 'NULL') {
|
|
|
+ $customer->group = null;
|
|
|
+ } else {
|
|
|
+ $customer->group = new group(intval($group));
|
|
|
+ }
|
|
|
+
|
|
|
+ $validator = new customer_validator(
|
|
|
+ $this->get_database(),
|
|
|
+ $this->get_tables()
|
|
|
+ );
|
|
|
+
|
|
|
+ $validator
|
|
|
+ ->load($customer)
|
|
|
+ ->check();
|
|
|
+
|
|
|
+ if (!$validator->is_valid()) {
|
|
|
+ $this->error_toast($validator->error_on());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $customer;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function add_customer() : void {
|
|
|
+ $customer = $this->get_customer_from_inputs();
|
|
|
+
|
|
|
+ if ($customer === null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ $this->mapper->create($customer);
|
|
|
+ $this->success_toast('Add customer success.');
|
|
|
+ } catch (\exception $exception) {
|
|
|
+ $this->error_toast($exception->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function save_customer() : void {
|
|
|
+ $customer = $this->get_customer_from_inputs();
|
|
|
+
|
|
|
+ if ($customer === null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ $this->mapper->save($customer);
|
|
|
+ $this->success_toast('Save customer success.');
|
|
|
+ } catch (\exception $exception) {
|
|
|
+ $this->error_toast($exception->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function remove_customer() : void {
|
|
|
+ $customer = $this->get_customer_from_inputs();
|
|
|
+
|
|
|
+ if ($customer === null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ $this->mapper->remove($customer);
|
|
|
+ $this->success_toast('Remove customer success.');
|
|
|
+ } catch (\exception $exception) {
|
|
|
+ $this->error_toast($exception->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function process() : self {
|
|
|
+ $this->preprocess();
|
|
|
+
|
|
|
+ if ($this->is_received('add')) {
|
|
|
+ $this->add_customer();
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->is_received('save')) {
|
|
|
+ $this->save_customer();
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->is_received('remove')) {
|
|
|
+ $this->remove_customer();
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->set('groups', $this->prepare_groups());
|
|
|
+ $this->set('all_customers', $this->load_customers());
|
|
|
+ $this->set(
|
|
|
+ 'export',
|
|
|
+ \get_home_url().'/cx-newsletter/customers-export'
|
|
|
+ );
|
|
|
+
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ private array $groups;
|
|
|
+ private groups_mapper $groups_mapper;
|
|
|
+ private customers_mapper $mapper;
|
|
|
+}
|