| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace cx_newsletter;
- class manage_messages_activity
- extends activity {
- public function show_after_button() : null {
- return null;
- }
- public function inside_buttons() : array {
- return [ 'remove' ];
- }
- public function inside_inputs() : array {
- return [
- 'id' => 'int'
- ];
- }
- public function get_site_template_name() : string {
- return 'manage_messages';
- }
- public function remove(messages_mapper $mapper) : void {
- if (!$this->is_validated('id')) {
- $this->error_toast('ID of the message is wrong.');
- }
- $message = new message($this->get_validated('id'));
- try {
- $mapper->remove($message);
- $this->success_toast('Removed success.');
- } catch (\exception $exception) {
- $this->error_toast($exception->getMessage());
- }
- }
- private function load_groups() : \cx_appengine\string_builder {
- $mapper = new groups_mapper(
- $this->get_database(),
- $this->get_tables()
- );
- $default = new group();
- $default->name = 'ALL';
- $groups = $mapper->load_all();
- $list = new \cx_appengine\string_builder();
- array_push($groups, $default);
- foreach ($groups as $group) {
- $options = [];
- $options['group_name'] = $group->name;
- if ($group->has_id()) {
- $options['group_id'] = $group->get_id();
- } else {
- $options['group_id'] = 'NULL';
- $options['selected'] = 'selected';
- }
- $list->push(
- $this
- ->get_templates()
- ->prepare('single_group_option')
- ->render($options)
- );
- }
- return $list;
- }
- public function process() : self {
- $converter = new messages_converter();
- $messages = new \cx_appengine\string_builder();
-
- $mapper = new messages_mapper(
- $this->get_database(),
- $this->get_tables()
- );
- if ($this->is_received('remove')) {
- $this->remove($mapper);
- }
-
- $groups = $this->load_groups();
- foreach ($mapper->load_all() as $message) {
- $options = $converter->load_object($message)->get_array();
- $options['groups'] = $groups;
- $messages->push(
- $this
- ->get_templates()
- ->prepare('manage_messages_single_message')
- ->render($options)
- );
- }
- $this->set('messages_place', $messages);
-
- return $this;
- }
- }
|