| Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/Controller/Front/ |
| Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/Controller/Front/TfControllerForm.php |
<?php
/*
* @package TF Components
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace TechFry\Library\Controller\Front;
defined('_JEXEC') or die;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\FormController;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Router\Route;
use Joomla\Input\Input;
use Joomla\CMS\Uri\Uri;
use TechFry\Library\TFile;
// Frontend controller for single item and form
class TfControllerForm extends FormController
{
/*
Define in child class:
protected $view_item; // view to redirect to form
protected $view_list; // view to redirect to list
*/
public $data;
public $file_fields = array();
public $upload_options = array();
public $cparams;
public $today;
public $date_sql;
public $date_unix;
public $date_lc3;
public $juser;
public $user_levels;
public $url;
public $post;
public $get;
public $asset; // For checking permissions
/* Define in child class:
public function getModel($name = 'Form', $prefix = 'Site', $config = ['ignore_request' => true])
{
return parent::getModel($name, $prefix, $config);
}
*/
// Methods Available in FormController: add(), edit(), cancel(), save()
public function __construct($config = [], ?MVCFactoryInterface $factory = null, ?CMSApplication $app = null, ?Input $input = null)
{
parent::__construct($config, $factory, $app, $input);
$model = $this->getModel();
if (empty($this->file_fields))
{
$this->file_fields = $model->file_fields;
}
if (empty($this->upload_options))
{
$this->upload_options = $model->upload_options;
}
$this->today = Factory::getDate();
$this->date_sql = $this->today->toSql();
$this->date_unix = $this->today->toUnix();
$this->date_lc3 = $this->today->format(Text::_('DATE_FORMAT_LC3'));
$this->juser = $this->app->getIdentity();
$this->user_levels = $this->juser->getAuthorisedViewLevels();
$this->cparams = ComponentHelper::getParams($this->option);
$this->url = Uri::getInstance()->toString();
$this->post = $this->input->post->getArray();
$this->get = $this->input->get->getArray();
}
// Save method
public function save($key = null, $urlVar = null)
{
$this->checkToken();
$model = $this->getModel();
$data = $this->input->post->get('jform', [], 'array');
if (empty($key))
{
$key = 'id'; // Primary key table
}
if (empty($urlVar))
{
$urlVar = $key;
}
$recordId = $this->input->getInt($urlVar);
$custom_message = (isset($data['custom_message']) && !empty($data['custom_message'])) ? $data['custom_message'] : Text::_('COM_TF_SUBMIT_SUCCESSFUL');
$custom_redirect = (isset($data['custom_redirect']) && !empty($data['custom_redirect'])) ? $data['custom_redirect'] : Uri::getInstance();
$context = $this->option . '.edit.' . $this->context;
if (!$this->allowSave($data, $key))
{
$this->setMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
$this->setRedirect($custom_redirect);
return false;
}
// File Handling and Uploading
if (!empty($this->file_fields))
{
$files = $this->input->files->get('jform');
foreach ($this->file_fields as $field_name)
{
$newfile = '';
$this->upload_options[$field_name] = isset($this->upload_options[$field_name]) ? $this->upload_options[$field_name] : [];
// For multiple files in one field, $files[$field_name] is 2-D array
if (isset($files[$field_name][0]))
{
$arr = [];
foreach ($files[$field_name] as $f)
{
if ($f['size'] > 0)
{
$fh = new TFile();
$arr[] = $fh->handle_file($f, $this->upload_options[$field_name]);
}
}
if (!empty($arr))
{
$newfile = implode(',', $arr);
}
}
else // For single file upload
{
if ($files[$field_name]['size'] > 0)
{
$fh = new TFile();
$newfile = $fh->handle_file($files[$field_name], $this->upload_options[$field_name]);
}
}
if ($newfile === false)
{
// Save the data in the session
$this->app->setUserState($context . '.data', $data);
// Redirect back to the edit screen
$this->setRedirect(Uri::getInstance());
return false;
}
if ($newfile)
{
$data[$field_name] = $newfile;
}
}
}
if (!$model->save($data))
{
// Save the data in the session
$this->app->setUserState($context . '.data', $data);
$this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'error');
// Redirect back to the edit screen
$this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false));
return false;
}
// Clear the data in the form
$this->app->setUserState($context . '.data', null);
$this->setRedirect($custom_redirect, $custom_message);
return true;
}
// Method to check if new record can be added
protected function allowAdd($data = [])
{
return $this->juser->authorise('core.create', $this->asset);
}
// Method to check if record can be edited
protected function allowEdit($data = [], $key = 'id')
{
return $this->juser->authorise('core.edit', $this->asset);
}
}