| Current Path : /home/digilove/www/administrator/components/com_jmap/controllers/ |
| Current File : /home/digilove/www/administrator/components/com_jmap/controllers/aigenerator.php |
<?php
// namespace administrator\components\com_jmap\controllers;
/**
* @package JMAP::AIGENERATOR::administrator::components::com_jmap
* @subpackage controllers
* @author Joomla! Extensions Store
* @copyright (C) 2021 - Joomla! Extensions Store
* @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
*/
defined('_JEXEC') or die('Restricted access');
/**
* Controller for AIGenerator links entity tasks
* @package JMAP::AIGENERATOR::administrator::components::com_jmap
* @subpackage controllers
* @since 2.0
*/
class JMapControllerAIGenerator extends JMapController {
/**
* Set model state from session userstate
* @access protected
* @param string $scope
* @return object
*/
protected function setModelState($scope = 'default', $ordering = true) {
$option = $this->option;
// Get default model
$defaultModel = $this->getModel();
$language = $this->getUserStateFromRequest ( "$option.aigenerator.language", 'language', '*' );
$contentsapi = $this->getUserStateFromRequest ( "$option.aigenerator.contentsapi", 'contentsapi', '' );
parent::setModelState($scope, false);
$filter_order = $this->getUserStateFromRequest( "$option.$scope.filter_order", 'filter_order', 's.id', 'cmd' );
$filter_order_Dir = $this->getUserStateFromRequest("$option.$scope.filter_order_Dir", 'filter_order_Dir', 'desc', 'word');
$defaultModel->setState('language', $language);
$defaultModel->setState('contentsapi', $contentsapi);
$defaultModel->setState('order', $filter_order);
$defaultModel->setState('order_dir', $filter_order_Dir);
return $defaultModel;
}
/**
* Default listEntities
*
* @access public
* @param $cachable string
* the view output will be cached
* @return void
*/
public function display($cachable = false, $urlparams = false) {
$document = $this->document;
$viewType = $document->getType ();
$coreName = $this->getNames ();
$viewLayout = $this->app->input->get ( 'layout', 'default' );
$view = $this->getView ( $coreName, $viewType, '', array (
'base_path' => $this->basePath
) );
// Set model state
$model = $this->setModelState('aigenerator');
// Push the model into the view (as default)
$view->setModel ( $model, true );
// Set the layout
$view->setLayout ( $viewLayout );
$view->display ();
}
/**
* Edit entity
*
* @access public
* @return void
*/
public function editEntity() {
$this->app->input->set('hidemainmenu', 1);
$option = $this->option;
$cid = $this->app->input->get ( 'cid', array (
0
), 'array' );
$idEntity = ( int ) $cid [0];
$user = $this->user;
$model = $this->getModel();
$model->setState('option', $option);
// Try to load record from model
if(!$record = $model->loadEntity($idEntity)) {
// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
$modelExceptions = $model->getErrors();
foreach ($modelExceptions as $exception) {
$this->app->enqueueMessage($exception->getMessage(), $exception->getErrorLevel());
}
$this->setRedirect ( 'index.php?option=com_jmap&task=aigenerator.display');
return false;
}
// Check out control on record
if ($record->checked_out && $record->checked_out != $user->id) {
$this->setRedirect ( 'index.php?option=' . $option . '&task=aigenerator.display', JText::_('COM_JMAP_CHECKEDOUT_RECORD'), 'notice');
return false;
}
// Access check
if($record->id && !$this->allowEdit($model->getState('option'))) {
$this->setRedirect('index.php?option=' . $option . '&task=aigenerator.display', JText::_('COM_JMAP_ERROR_ALERT_NOACCESS'), 'notice');
return false;
}
if(!$record->id && !$this->allowAdd($model->getState('option'))) {
$this->setRedirect('index.php?option=' . $option . '&task=aigenerator.display', JText::_('COM_JMAP_ERROR_ALERT_NOACCESS'), 'notice');
return false;
}
// Check out del record
if ($record->id) {
$record->checkout ( $user->id );
}
// Get view and pushing model
$view = $this->getView();
$view->setModel ( $model, true );
// Call edit view
$view->editEntity($record);
}
/**
* Manage entity apply/save after edit entity
*
* @access public
* @return void
*/
public function saveEntity() {
$task = $this->task;
$option = $this->option;
$context = implode('.', array($option, strtolower($this->getName()), 'errordataload'));
// Security layer for tags html outputted fields
$sanitizedFields = array('keywords_phrase');
foreach ($sanitizedFields as $field) {
$this->requestArray[$field] = strip_tags($this->requestArray[$field]);
}
//Load della model e bind store
$model = $this->getModel ();
if(!$result = $model->storeEntity()) {
// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
$modelException = $model->getError(null, false);
$this->app->enqueueMessage($modelException->getMessage(), $modelException->getErrorLevel());
// Store data for session recover
$this->app->setUserState($context, $this->requestArray);
$this->setRedirect ( 'index.php?option=com_jmap&task=aigenerator.editEntity&cid[]=' . $this->app->input->get ( 'id' ), JText::_('COM_JMAP_ERROR_SAVING'));
return false;
}
// Security safe if not model record id detected
if(!$id = $result->id) {
$id = $this->app->input->get ( 'id' );
}
// Redirects switcher
switch ($this->task) {
case 'saveEntity' :
$redirects = array (
'task' => 'display',
'msgsufix' => '_SAVING'
);
break;
case 'saveEntity2New' :
$redirects = array (
'task' => 'editEntity',
'msgsufix' => '_STORING'
);
break;
default :
case 'applyEntity' :
$redirects = array (
'task' => 'editEntity&cid[]=' . $id,
'msgsufix' => '_APPLY'
);
break;
}
$msg = 'COM_JMAP_SUCCESS' . $redirects ['msgsufix'];
$controllerTask = $redirects ['task'];
$this->setRedirect ( "index.php?option=$option&task=aigenerator.$controllerTask", JText::_($msg));
}
/**
* Manage cancel edit for entity and unlock record checked out
*
* @access public
* @return void
*/
public function cancelEntity() {
$id = $this->app->input->get ( 'id' );
$option = $this->option;
//Load della model e checkin before exit
$model = $this->getModel ( );
if(!$model->cancelEntity($id)) {
// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
$modelException = $model->getError(null, false);
$this->app->enqueueMessage($modelException->getMessage(), $modelException->getErrorLevel());
}
$this->setRedirect ( "index.php?option=$option&task=aigenerator.display", JText::_('COM_JMAP_CANCELED_OPERATION') );
}
/**
* Delete a db table entity
*
* @access public
* @return void
*/
public function deleteEntity() {
$cids = $this->app->input->get ( 'cid', array (), 'array' );
$option = $this->option;
// Access check
if(!$this->allowDelete($option)) {
$this->setRedirect('index.php?option=com_jmap&task=aigenerator.display', JText::_('COM_JMAP_ERROR_ALERT_NOACCESS'), 'notice');
return false;
}
//Load della model e checkin before exit
$model = $this->getModel ( );
if(!$model->deleteEntity($cids)) {
// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
$modelException = $model->getError(null, false);
$this->app->enqueueMessage($modelException->getMessage(), $modelException->getErrorLevel());
$this->setRedirect ( "index.php?option=$option&task=aigenerator.display", JText::_('COM_JMAP_ERROR_DELETE'));
return false;
}
$this->setRedirect ( "index.php?option=$option&task=aigenerator.display", JText::_('COM_JMAP_SUCCESS_DELETE') );
}
/**
* Delete a db table entity
*
* @access public
* @return void
*/
public function generateEntity() {
$task = $this->task;
$option = $this->option;
$context = implode('.', array($option, strtolower($this->getNames()), 'errordataload'));
// Security layer for tags html outputted fields
$sanitizedFields = array('keywords_phrase');
foreach ($sanitizedFields as $field) {
$this->requestArray[$field] = strip_tags($this->requestArray[$field]);
}
//Load della model e bind store
$model = $this->getModel ();
$keywordsPhrase = $this->app->input->getString('keywords_phrase');
$selectedApi = $this->app->input->get('api');
$model->setState('maxresults', $this->app->input->getInt('maxresults', $model->getComponentParams('aigenerator_default_results', 10)));
$model->setState('temperature', $this->app->input->getString('temperature', '0.5'));
// Get default site language
$langParams = JComponentHelper::getParams('com_languages');
// Setup predefined site language
$defaultLanguageCode = $langParams->get('site');
$model->setState('contentlanguage', $this->app->input->getString('language', $defaultLanguageCode));
$model->setState('removeimgs', $this->app->input->getInt('removeimgs', 0));
// Generate AI contents for this phrase keywords
switch ($selectedApi) {
case 'google':
case 'bing':
$generatedAIContentsResult = $model->generateAIContentData($keywordsPhrase, $selectedApi);
break;
case 'openai':
$generatedAIContentsResult = $model->generateOpenAIContentData($keywordsPhrase);
break;
}
if($generatedAIContentsResult === false) {
// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
$modelException = $model->getError(null, false);
$this->app->enqueueMessage($modelException->getMessage(), $modelException->getErrorLevel());
// Store data for session recover
$this->app->setUserState($context, $this->requestArray);
$this->setRedirect ( 'index.php?option=com_jmap&task=aigenerator.editEntity&cid[]=' . $this->app->input->get ( 'id' ), JText::_('COM_JMAP_ERROR_SAVING'));
return false;
} else {
$this->requestArray['contents'] = $generatedAIContentsResult;
// Check if it was a previous Openai record or if the keywords changes, then delete images folder
if($idRecord = $this->app->input->get('id')) {
$currentRecord = $model->loadEntity($idRecord);
if (($selectedApi != $currentRecord->api && $currentRecord->api == 'openai') || ($keywordsPhrase != $currentRecord->keywords_phrase && $selectedApi == 'openai')) {
// If AI model is ChatGPT, delete also local images files
$imagesFolder = md5 ( $currentRecord->keywords_phrase );
$imagesPath = JPATH_ROOT . '/administrator/components/com_jmap/cache/chatgpt/' . $imagesFolder;
if (is_dir ( $imagesPath )) {
JFolder::delete ( $imagesPath );
}
}
}
}
if(!$result = $model->storeEntity()) {
// Model set exceptions for something gone wrong, so enqueue exceptions and levels on application object then set redirect and exit
$modelException = $model->getError(null, false);
$this->app->enqueueMessage($modelException->getMessage(), $modelException->getErrorLevel());
// Store data for session recover
$this->app->setUserState($context, $this->requestArray);
$this->setRedirect ( 'index.php?option=com_jmap&task=aigenerator.editEntity&cid[]=' . $this->app->input->get ( 'id' ), JText::_('COM_JMAP_ERROR_SAVING'));
return false;
}
// Security safe if not model record id detected
if(!$id = $result->id) {
$id = $this->app->input->get ( 'id' );
}
$msg = 'COM_JMAP_AIGENERATOR_SUCCESS_GENERATING_CONTENT';
$controllerTask = 'editEntity&cid[]=' . $id;
$this->setRedirect ( "index.php?option=$option&task=aigenerator.$controllerTask", JText::_($msg));
}
/**
*
* Class Constructor
*
* @access public
* @param $config
* @return Object&
*/
public function __construct($config = array()) {
parent::__construct ( $config );
// Register Extra tasks
$this->registerTask ( 'applyEntity', 'saveEntity' );
$this->registerTask ( 'saveEntity2New', 'saveEntity' );
}
}
?>