NEW display when click on NEWS templateDrop down for model (#30332)

* New update for templateMail by creating files

* New updating for get php content

* Update html.formmail.class.php

* Fix updating content news

* Fix CTI Errors

* edit html file for news

* NEW display for news with option

* replace file in ajax folder

* fix multiselect news

* Fix phan errors and remove part of useless css

* Fix brakets needed in script

* Fix Phan errors

* Fix display multiselect and fix security injection

* cancel removing part of code for classe modelMail

* Fix phstan errors

* Fix space line

* Fix error phpStan

---------

Co-authored-by: Laurent Destailleur <eldy@destailleur.fr>
This commit is contained in:
lamrani abdelwadoud 2024-09-17 15:37:34 +02:00 committed by GitHub
parent c15e2f888f
commit 5fcca27fc4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 189 additions and 34 deletions

View file

@ -0,0 +1,48 @@
<?php
// Just for display errors in editor
ini_set('display_errors', 1);
if (!defined('NOTOKENRENEWAL')) {
define('NOTOKENRENEWAL', '1'); // Disables token renewal
}
if (!defined('NOREQUIREMENU')) {
define('NOREQUIREMENU', '1');
}
if (!defined('NOREQUIREHTML')) {
define('NOREQUIREHTML', '1');
}
if (!defined('NOREQUIREAJAX')) {
define('NOREQUIREAJAX', '1');
}
if (!defined('NOREQUIRESOC')) {
define('NOREQUIRESOC', '1');
}
require_once '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
top_httphead();
if ($_SERVER['REQUEST_METHOD'] == 'POST' && GETPOSTISSET('selectedIds')) {
$selectedIds = json_decode(GETPOST('selectedIds'), true);
$websitepage = new WebsitePage($db);
$selectedPosts = array();
foreach ($selectedIds as $id) {
$blog = new WebsitePage($db);
$blog->fetch($id);
$selectedPosts[] = array(
'id' => $blog->id,
'title' => $blog->title,
'description' => $blog->description,
'date_creation' => $blog->date_creation,
'image' => $blog->image,
);
}
print json_encode($selectedPosts);
} else {
print json_encode(array('error' => 'Invalid request'));
}

View file

@ -44,12 +44,7 @@ require_once '../../main.inc.php';
require_once '../lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
// There is no permission test on this component for the moment. Test will be added when knowing which data it read.
// TODO $selectedPosts is not initialised, i set it to '' but this is surely a bug and not the expected behaviour.
// Should be set to list of last news...
$selectedPosts = '';
require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';
/*
@ -60,12 +55,16 @@ top_httphead();
// TODO Replace with ID of template
if (GETPOSTISSET('content')) {
$content = GETPOST('content');
$content = filter_input(INPUT_POST, 'content', FILTER_UNSAFE_RAW);
if (!empty($selectedPosts)) {
$selectedPostsStr = GETPOST('selectedPosts', 'alpha');
$selectedPosts = explode(',', $selectedPostsStr);
if (is_array($selectedPosts) && !empty($selectedPosts)) {
$newsList = '';
foreach ($selectedPosts as $post) {
foreach ($selectedPosts as $postId) {
$post = getNewsDetailsById($postId);
$newsList .= '<div style="display: flex; align-items: flex-start; justify-content: flex-start; width: 100%; max-width: 800px; margin-top: 20px;margin-bottom: 50px; padding: 20px;">
<div style="flex-grow: 1; margin-right: 30px; max-width: 600px; margin-left: 100px;">
<h2 style="margin: 0; font-size: 1.5em;">' . htmlentities($post['title']) . '</h2>

View file

@ -499,7 +499,7 @@ class FormMail extends Form
$modelmail_array = array();
if ($this->param['models'] != 'none') {
$result = $this->fetchAllEMailTemplate($this->param["models"], $user, $outputlangs); // Fill $this->lines_model
$result = $this->fetchAllEMailTemplate($this->param["models"], $user, $outputlangs);
if ($result < 0) {
setEventMessages($this->error, $this->errors, 'errors');
}
@ -1514,16 +1514,21 @@ class FormMail extends Form
* Return HTML code for selection of email layout
*
* @param string $htmlContent HTML name of WYSIWYG field to fill
* @return string HTML for model email boxes
* @return string HTML for model email boxes
*/
public function getModelEmailTemplate($htmlContent = 'message')
{
global $langs;
global $websitepage, $langs, $user;
require_once DOL_DOCUMENT_ROOT.'/core/lib/emaillayout.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
$out = '<div id="template-selector" class="template-container hidden">';
// Fetch blogs
$websitepage = new WebsitePage($this->db);
$arrayofblogs = $websitepage->fetchAll('', 'DESC', 'date_creation', 0, 0, array('type_container' => 'blogpost'));
$out = '<div id="template-selector" class="template-container">';
$templates = array(
'empty' => 'empty',
);
@ -1546,35 +1551,63 @@ class FormMail extends Form
}
$out .= '</div>';
// Prepare the array for multiselect
$blogArray = array();
if (!empty($arrayofblogs)) {
foreach ($arrayofblogs as $blog) {
$blogArray[$blog->id] = substr(htmlentities($blog->title), 0, 30);
}
}
// Use the multiselect array function to create the dropdown
$out .= '<div id="post-dropdown-container" style="display:none;">';
$out .= '<label for="blogpost-select">Select Posts: </label>';
$out .= self::multiselectarray('blogpost-select', $blogArray);
$out .= '</div>';
$out .= '<script type="text/javascript">
$(document).ready(function() {
$(".template-option").click(function() {
var template = $(this).data("template");
var subject = jQuery("#subject").val();
var fromtype = jQuery("#fromtype").val();
var sendto = jQuery("#sendto").val();
var sendtocc = jQuery("#sendtocc").val();
var sendtoccc = jQuery("#sendtoccc").val();
$(document).ready(function() {
$(".template-option").click(function() {
var template = $(this).data("template");
var subject = jQuery("#subject").val();
var fromtype = jQuery("#fromtype").val();
var sendto = jQuery("#sendto").val();
var sendtocc = jQuery("#sendtocc").val();
var sendtoccc = jQuery("#sendtoccc").val();
console.log("We choose a layout for email template=" + template + ", subject="+subject);
$(".template-option").removeClass("selected");
$(this).addClass("selected");
console.log("We choose a layout for email template " + template);
var contentHtml = $(this).data("content");
var csrfToken = "'.newToken().'";
$(".template-option").removeClass("selected");
$(this).addClass("selected");
// Remplacer la variable de substitution dans le contenu HTML
contentHtml = contentHtml.replace(/__SUBJECT__/g, subject);
var subject = $("#sujet").val();
// Envoyer le contenu HTML à process_template.php pour traitement PHP
var contentHtml = $(this).data("content");
contentHtml = contentHtml.replace(/__SUBJECT__/g, subject);
if (template === "news") {
$("#post-dropdown-container").show();
console.log("Displaying dropdown for news template");
} else {
$("#post-dropdown-container").hide();
var csrfToken = "' .newToken().'";
$.ajax({
type: "POST",
url: "'.DOL_URL_ROOT.'/core/ajax/mailtemplate.php",
data: { template: template, subject: subject, fromtype: fromtype, sendto: sendto, sendtocc: sendtocc, sendtoccc: sendtoccc, content: contentHtml, token: csrfToken },
data: { content: contentHtml, token: csrfToken },
url: "/core/ajax/mailtemplate.php",
data: {
template: template, subject: subject, fromtype: fromtype, sendto: sendto, sendtocc: sendtocc, sendtoccc: sendtoccc,
content: contentHtml,
selectedPosts: "[]",
token: csrfToken
},
success: function(response) {
jQuery("#'.dol_sanitizeKeyCode($htmlContent).'").val(response);
var editorInstance = CKEDITOR.instances["'.dol_sanitizeKeyCode($htmlContent).'"];
jQuery("#'.$htmlContent.'").val(response);
var editorInstance = CKEDITOR.instances["'.$htmlContent.'"];
if (editorInstance) {
editorInstance.setData(response);
}
@ -1583,9 +1616,58 @@ class FormMail extends Form
console.error("An error occurred: " + xhr.responseText);
}
});
});
}
});
</script>';
$("#blogpost-select").change(function() {
var selectedIds = $(this).val();
var contentHtml = $(".template-option.selected").data("content");
updateSelectedPostsContent(contentHtml, selectedIds);
});
function updateSelectedPostsContent(contentHtml, selectedIds) {
var csrfToken = "' .newToken().'";
$.ajax({
type: "POST",
url: "/core/ajax/getnews.php",
data: {
selectedIds: JSON.stringify(selectedIds),
token : csrfToken
},
success: function(response) {
var selectedPosts = JSON.parse(response);
var subject = $("#sujet").val();
contentHtml = contentHtml.replace(/__SUBJECT__/g, subject);
$.ajax({
type: "POST",
url: "/core/ajax/mailtemplate.php",
data: {
content: contentHtml,
selectedPosts: selectedIds.join(","),
token: csrfToken
},
success: function(response) {
jQuery("#'.$htmlContent.'").val(response);
var editorInstance = CKEDITOR.instances["'.$htmlContent.'"];
if (editorInstance) {
editorInstance.setData(response);
}
},
error: function(xhr, status, error) {
console.error("An error occurred: " + xhr.responseText);
}
});
},
error: function(xhr, status, error) {
console.error("An error occurred: " + xhr.responseText);
}
});
}
});
</script>';
return $out;
}

View file

@ -1528,3 +1528,29 @@ function getAllImages($object, $objectpage, $urltograb, &$tmp, &$action, $modify
}
}
}
/**
* Retrieves the details of a news post by its ID.
*
* @param string $postId The ID of the news post to retrieve.
* @return array|int Return array if OK, -1 if KO
*/
function getNewsDetailsById($postId)
{
global $db;
if (empty($postId)) {
return -1;
}
$sql = "SELECT p.title, p.description, p.date_creation, p.image
FROM ".MAIN_DB_PREFIX."website_page as p
WHERE p.rowid = ".(intval($postId));
$resql = $db->query($sql);
if ($resql) {
return $db->fetch_array($resql);
} else {
return -1;
}
}

View file

@ -1,6 +1,7 @@
<h1 style="margin-left:100px;">__LAST_NEWS__</h1>
<h2 style="margin-left:100px;">__CONTENOFMAILHOLDER__ </h2>
<!-- NEWS_START -->
__NEWS_LIST__
<!-- NEWS_END -->

View file

@ -3017,7 +3017,6 @@ a.tmenuimage:hover{
text-decoration: none;
}
/* To show text of top menu according to option THEME_TOPMENU_DISABLE_IMAGE */
/* Text hidden by default */