New functionality Generate img product (#29731)

* New functionality for generate image for product

* New functionality for generate image for product

* Fix errors CTI

* remove null for second params

* fix CTI errors again

* Fix whitespace

* Fix whitespaces again

* Fix run phan

* Update commonobject.class.php

---------

Co-authored-by: Laurent Destailleur <eldy@destailleur.fr>
This commit is contained in:
lamrani abdelwadoud 2024-10-28 14:51:36 +01:00 committed by GitHub
parent 7409e3c492
commit cc4f86dac3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 64 additions and 4 deletions

View File

@ -1506,6 +1506,10 @@ class FormMail extends Form
$out .= '<i class="fa fa-spinner fa-spin fa-2x fa-fw valignmiddle marginrightonly"></i>'.$langs->trans("AIProcessingPleaseWait", getDolGlobalString('AI_API_SERVICE', 'chatgpt'));
$out .= '</div>';
if ($function == 'imagegeneration') {
$out .= '<div id="ai_image_result" class="margintoponly"></div>'; // Div for displaying the generated image
}
$out .= "</div>\n";
$out .= "<script type='text/javascript'>
@ -1534,11 +1538,43 @@ class FormMail extends Form
}
}, 2000);
// set editor in readonly
if (CKEDITOR.instances.".$htmlContent.") {
CKEDITOR.instances.".$htmlContent.".setReadOnly(1);
}
if ('".$function."' === 'imagegeneration') {
// Handle image generation request
$.ajax({
url: '". DOL_URL_ROOT."/ai/ajax/generate_content.php?token=".currentToken()."',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
'format': '".dol_escape_js($format)."', /* the format for output */
'function': '".dol_escape_js($function)."', /* the AI feature to call */
'instructions': instructions, /* the prompt string */
}),
success: function(response) {
console.log('Received image URL: '+response);
// Assuming response is the URL of the generated image
var imageUrl = response;
$('#ai_image_result').html('<img src=\"' + imageUrl + '\" alt=\"Generated Image\" />');
// Clear the input field
$('#ai_instructions').val('');
apicallfinished = 1;
if (timeoutfinished) {
$('#ai_status_message').hide();
}
},
error: function(xhr, status, error) {
alert(error);
console.error('error ajax', status, error);
$('#ai_status_message').hide();
}
});
} else {
// set editor in readonly
if (CKEDITOR.instances.".$htmlContent.") {
CKEDITOR.instances.".$htmlContent.".setReadOnly(1);
}
$.ajax({
url: '". DOL_URL_ROOT."/ai/ajax/generate_content.php?token=".currentToken()."',

View File

@ -443,4 +443,6 @@ AllowStockMovementVariantParentHelp=By default, a parent of a variant is a virtu
ConfirmSetToDraftInventory=Are you sure you want to go back to Draft status?<br>The quantities currently set in the inventory will be reset.
WarningLineProductNotToSell=Product or service "%s" is not to sell and was cloned
PriceLabel=Price label
GenerateImage=Generate image
GenerateWithAI=Generate with AI
PriceByCustomeAndMultiPricesAbility=Different prices for each customer + Multiple price segments per product/service (each customer is in one price segment)

View File

@ -263,6 +263,28 @@ if ($object->id > 0) {
print dol_get_fiche_end();
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
$formmail = new FormMail($db);
$formmail->withaiprompt = 'text';
$out = '';
$showlinktoai = ($formmail->withaiprompt && isModEnabled('ai')) ? 'textgenerationemail' : '';
$showlinktoailabel = $langs->trans("GenerateImage");
$formatforouput = 'image';
$htmlname = 'bodyemail';
print load_fiche_titre($langs->trans('GenerateWithAI'), '', '');
print '<table class="border centpercent">';
// Fill $out
require DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
print $out;
print '</table>';
$param = '&id='.$object->id;
include DOL_DOCUMENT_ROOT.'/core/tpl/document_actions_post_headers.tpl.php';