diff --git a/public/main/inc/ajax/model.ajax.php b/public/main/inc/ajax/model.ajax.php index 635433478a..25554eb499 100644 --- a/public/main/inc/ajax/model.ajax.php +++ b/public/main/inc/ajax/model.ajax.php @@ -2331,13 +2331,13 @@ switch ($action) { $result = $new_result; break; case 'get_mail_template': - $columns = ['name', 'type', 'default_template', 'actions']; + $columns = ['title', 'type', 'default_template', 'actions']; if (!in_array($sidx, $columns)) { - $sidx = 'name'; + $sidx = 'title'; } if (!in_array($sidx, $columns)) { - $sidx = 'name'; + $sidx = 'title'; } $result = Database::select( diff --git a/public/main/inc/lib/MailTemplateManager.php b/public/main/inc/lib/MailTemplateManager.php index dfec341831..55ec3f2482 100644 --- a/public/main/inc/lib/MailTemplateManager.php +++ b/public/main/inc/lib/MailTemplateManager.php @@ -11,7 +11,7 @@ class MailTemplateManager extends Model { public $columns = [ 'id', - 'name', + 'title', 'template', 'type', 'system', @@ -83,12 +83,15 @@ class MailTemplateManager extends Model $id = isset($_GET['id']) ? (int) $_GET['id'] : ''; $form->addElement('header', '', $header); - $form->addElement('hidden', 'id', $id); + if (!empty($id)) { + $form->addElement('hidden', 'id', $id); + } + $form->addElement('hidden', 'default_template', 0); $form->addElement( 'text', - 'name', - get_lang('Name'), - ['size' => '70', 'id' => 'name'] + 'title', + get_lang('Title'), + ['size' => '70', 'id' => 'title'] ); /*$form->addHtmlEditor( @@ -146,7 +149,7 @@ class MailTemplateManager extends Model $form->setDefaults($defaults); // Setting the rules - $form->addRule('name', get_lang('Required field'), 'required'); + $form->addRule('title', get_lang('Required field'), 'required'); return $form; } diff --git a/public/main/inc/lib/api.lib.php b/public/main/inc/lib/api.lib.php index a948d812a7..35d7eeae9d 100644 --- a/public/main/inc/lib/api.lib.php +++ b/public/main/inc/lib/api.lib.php @@ -2676,7 +2676,7 @@ function api_get_setting($variable, $isArray = false, $key = null) break; default: $settingValue = $settingsManager->getSetting($variable, true); - if ($isArray && !empty($settingValue)) { + if (is_string($settingValue) && $isArray && !empty($settingValue)) { // Check if the value is a valid JSON string $decodedValue = json_decode($settingValue, true); diff --git a/public/main/mail_template/list.php b/public/main/mail_template/list.php new file mode 100644 index 0000000000..3bb3df931b --- /dev/null +++ b/public/main/mail_template/list.php @@ -0,0 +1,160 @@ +returnForm($url, 'add'); + + // The validation or display + if ($form->validate()) { + if ($check) { + $values = $form->exportValues(); + $values['template'] = $values['email_template']; + $values['author_id'] = api_get_user_id(); + $values['url_id'] = api_get_current_access_url_id(); + + var_dump($values); + + $res = $mailTemplate->save($values); + if ($res) { + Display::addFlash(Display::return_message(get_lang('ItemAdded'), 'confirm')); + } + } + header('Location: '.api_get_self()); + exit; + } else { + $content .= '
'; + $content .= Display::url( + Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM), + api_get_self() + ); + $content .= '
'; + $form->addElement('hidden', 'sec_token'); + $form->setConstants(['sec_token' => $token]); + $content .= $form->returnForm(); + } + break; + case 'edit': + $url = api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&id='.$id; + $form = $mailTemplate->returnForm($url, 'edit'); + + $content .= '
'; + $content .= Display::url( + Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM), + api_get_self() + ); + $content .= '
'; + $content .= $form->returnForm(); + + // The validation or display + if ($form->validate()) { + //if ($check) { + $values = $form->exportValues(); + $values['template'] = $values['email_template']; + $res = $mailTemplate->update($values); + if ($res) { + Display::addFlash( + Display::return_message(get_lang('ItemUpdated').': '.$values['title'], 'confirm') + ); + } + //} + header('Location: '.api_get_self()); + exit; + } + break; + case 'delete': + $mailTemplate->delete($id); + Display::addFlash( + Display::return_message(get_lang('Deleted'), 'confirm') + ); + header('Location: '.api_get_self()); + exit; + break; + case 'set_default': + $mailTemplate->setDefault($id); + Display::addFlash( + Display::return_message(get_lang('Updated'), 'confirm') + ); + header('Location: '.api_get_self()); + break; + case 'list': + $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_mail_template'; + $columns = [ + get_lang('Name'), + get_lang('Type'), + get_lang('Default'), + get_lang('Actions'), + ]; + $column_model = [ + [ + 'name' => 'name', + 'index' => 'name', + 'width' => '380px', + 'align' => 'left', + ], + [ + 'name' => 'type', + 'index' => 'type', + 'width' => '150px', + 'align' => 'left', + ], + [ + 'name' => 'default_template', + 'index' => 'default_template', + 'width' => '200px', + 'align' => 'left', + 'hidden' => 'true', + ], + [ + 'name' => 'actions', + 'index' => 'actions', + 'width' => '300px', + 'align' => 'left', + 'formatter' => 'action_formatter', + 'sortable' => 'false', + ], + ]; + $extra_params['autowidth'] = 'true'; //use the width of the parent + $extra_params['height'] = 'auto'; //use the width of the parent + + //With this function we can add actions to the jgrid + $action_links = 'function action_formatter (cellvalue, options, rowObject) { + + var defaultIcon = ""; + if (rowObject[2] == 1) { + defaultIcon = ""; + } + return \' '. + ' \'+ defaultIcon + \''. + '  \'; + }'; + + $content = $mailTemplate->display(); + $content .= ' + '; + break; +} + +$template = new Template(); +$template->assign('content', $content); +$template->display_one_col_template(); diff --git a/src/CoreBundle/Entity/MailTemplate.php b/src/CoreBundle/Entity/MailTemplate.php index 526edb31e4..b6a7b87ebb 100644 --- a/src/CoreBundle/Entity/MailTemplate.php +++ b/src/CoreBundle/Entity/MailTemplate.php @@ -29,11 +29,13 @@ class MailTemplate #[ORM\Column(name: 'type', type: 'string', nullable: false)] protected string $type; - #[ORM\Column(name: 'score', type: 'float', nullable: true)] - protected float $authorId; + #[ORM\ManyToOne(targetEntity: "User")] + #[ORM\JoinColumn(name: 'author_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')] + protected ?User $author = null; - #[ORM\Column(name: 'result_id', type: 'integer', nullable: false)] - protected int $urlId; + #[ORM\ManyToOne(targetEntity: "AccessUrl")] + #[ORM\JoinColumn(name: 'url_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] + protected ?AccessUrl $url = null; #[ORM\Column(name: 'default_template', type: 'boolean', nullable: false)] protected bool $defaultTemplate; diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20240416132900.php b/src/CoreBundle/Migrations/Schema/V200/Version20240416132900.php new file mode 100644 index 0000000000..7cb3957068 --- /dev/null +++ b/src/CoreBundle/Migrations/Schema/V200/Version20240416132900.php @@ -0,0 +1,52 @@ +hasTable('mail_template')) { + // Adding author_id and setting the foreign key + $this->addSql('ALTER TABLE mail_template ADD author_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE mail_template ADD CONSTRAINT FK_4AB7DECBF675F31B FOREIGN KEY (author_id) REFERENCES user (id) ON DELETE SET NULL'); + $this->addSql('CREATE INDEX IDX_4AB7DECBF675F31B ON mail_template (author_id)'); + + // Updating result_id to url_id and adjusting foreign key + $this->addSql('ALTER TABLE mail_template CHANGE result_id url_id INT NOT NULL'); + $this->addSql('ALTER TABLE mail_template ADD CONSTRAINT FK_4AB7DECB81CFDAE7 FOREIGN KEY (url_id) REFERENCES access_url (id) ON DELETE CASCADE'); + $this->addSql('CREATE INDEX IDX_4AB7DECB81CFDAE7 ON mail_template (url_id)'); + + // Dropping unused column 'score' + $this->addSql('ALTER TABLE mail_template DROP score'); + } + } + + public function down(Schema $schema): void + { + if ($schema->hasTable('mail_template')) { + // Reverting changes + $this->addSql('ALTER TABLE mail_template DROP FOREIGN KEY FK_4AB7DECBF675F31B'); + $this->addSql('ALTER TABLE mail_template DROP author_id'); + $this->addSql('DROP INDEX IDX_4AB7DECBF675F31B ON mail_template'); + + $this->addSql('ALTER TABLE mail_template DROP FOREIGN KEY FK_4AB7DECB81CFDAE7'); + $this->addSql('ALTER TABLE mail_template CHANGE url_id result_id INT NOT NULL'); + $this->addSql('DROP INDEX IDX_4AB7DECB81CFDAE7 ON mail_template'); + + $this->addSql('ALTER TABLE mail_template ADD score FLOAT NULL DEFAULT NULL'); + } + } +}