Your IP : 216.73.216.240
<?php
/*
* @package TF Components
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace TechFry\Library\Model\Front;
defined('_JEXEC') or die;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Form\FormFactoryInterface;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\MVC\Model\AdminModel;
use Joomla\Registry\Registry;
use TechFry\Library\TUser;
use TechFry\Library\Model\TfModelTrait;
use TechFry\Library\Model\TfModelFormTrait;
use TechFry\Library\TForm;
use TechFry\Library\TDb;
class TfModelForm extends AdminModel
{
use TfModelTrait, TfModelFormTrait;
public $form;
public $loadData = true;
public $basic_fields = [];
public $date_fields = []; // Shifted to Table Class
public $subform_fields = [];
public $multiple_fields = [];
public $file_fields = []; // Array with names of file upload fields
public $upload_options = []; // 2D Array (allow_ext, max_size, save_folder)
public $last_id;
public $created_by;
public $create_user = 1;
public function __construct($config = [], ?MVCFactoryInterface $factory = null, ?FormFactoryInterface $formFactory = null)
{
parent::__construct($config, $factory, $formFactory);
$this->app = Factory::getApplication();
$this->input = $this->app->input;
$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();
$db = new TDb($this->table_name);
$this->table_columns = $db->get_table_columns();
}
public function getForm($data = array(), $loadData = true)
{
// Get the form
$this->form = $this->loadForm($this->option . '.' . $this->getName(), $this->getName(), array('control' => 'jform', 'load_data' => $loadData));
if (empty($this->form))
{
return false;
}
$this->complete_form();
$this->bind_form();
return $this->form;
}
protected function loadFormData()
{
// Check the session for previously entered form data.
$app = Factory::getApplication();
$data = $app->getUserState($this->option . '.edit.' . $this->getName() . '.data', array());
if (empty($data) && ($this->loadData))
{
$data = $this->getItem();
}
if ($data)
{
return $data;
}
else
{
return parent::loadFormData();
}
}
public function complete_form()
{
$this->add_fieldset('details');
$this->add_fieldset('captcha', 'Captcha');
$fieldset = 'details';
$element = '<field name="custom_redirect" type="hidden" />';
$this->set_field($element, $fieldset);
$fieldset = 'details';
$element = '<field name="custom_message" type="hidden" />';
$this->set_field($element, $fieldset);
$fieldset = 'captcha';
$element = '<field name="captcha" type="number" label="' . strtoupper($this->option) . '_CAPTCHA" />';
$this->set_field($element, $fieldset);
if (array_key_exists('id', $this->table_columns))
{
$fieldset = 'details';
$element = '<field name="id" type="hidden" default="0" />';
$this->set_field($element, $fieldset);
}
foreach ($this->basic_fields as $field)
{
switch ($field)
{
case 'published' :
$fieldset = 'details';
$element = '<field name="published" type="hidden" />';
break;
/*
case 'id' :
$fieldset = 'details';
$element = '<field name="id" type="hidden" default="0" />';
break;
case 'custom_redirect' :
$fieldset = 'details';
$element = '<field name="custom_redirect" type="hidden" />';
break;
case 'custom_message' :
$fieldset = 'details';
$element = '<field name="custom_message" type="hidden" />';
break;
case 'captcha' :
$fieldset = 'captcha';
$element = '<field name="captcha" type="number" label="' . strtoupper($this->option) . '_CAPTCHA" />';
break;
*/
case 'created' :
$fieldset = 'details';
$element = '<field name="created" type="hidden" />';
break;
case 'modified' :
$fieldset = 'details';
$element = '<field name="modified" type="hidden" />';
break;
case 'created_by' :
$fieldset = 'details';
$element = '<field name="created_by" type="hidden" />';
break;
case 'alias' :
$fieldset = 'details';
$element = '<field name="alias" type="hidden" />';
break;
case 'hits' :
$fieldset = 'details';
$element = '<field name="hits" type="hidden" />';
break;
case 'access' :
$fieldset = 'details';
$element = '<field name="access" type="hidden" />';
}
$this->set_field($element, $fieldset);
}
}
public function save($data)
{
// A. Check for captcha
if (isset($data['answer']))
{
if ($data['answer'] != $data['captcha'])
{
Factory::getApplication()->enqueueMessage('<strong>' . Text::_('COM_TF_ERROR_INCORRECT_CAPTCHA') . '</strong>', 'error');
return false;
}
unset($data['captcha']);
unset($data['answer']);
}
// B. Check for valid email
if (isset($data['email']))
{
$data['email'] = strtolower($data['email']);
if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL))
{
Factory::getApplication()->enqueueMessage('<strong>' . Text::_('COM_TF_ERROR_INVALID_EMAIL') . '</strong>', 'error');
return false;
}
if (!$data['created_by'])
{
$u = new TUser();
$data['created_by'] = $u->find_user($data['email']);
if (!$data['created_by'] && $this->create_user)
{
$u = new TUser();
$juser = $u->create_user($data);
$data['created_by'] = $juser->id;
}
}
}
// $data = $this->prepare_data($data, $this->table_name, $this->date_fields, $this->subform_fields);
// Convert subform fields to JSON string
foreach ($this->subform_fields as $field)
{
if (isset($data[$field]) && \is_array($data[$field]))
{
$registry = new Registry($data[$field]);
$data[$field] = (string) $registry;
}
}
// Convert arrays of multiple fields to JSON string (Add reverse function in getItem)
foreach ($this->multiple_fields as $field)
{
if (isset($data[$field]) && \is_array($data[$field]))
{
$registry = new Registry($data[$field]);
$data[$field] = (string) $registry;
}
}
$return = parent::save($data);
// Set created by
$this->created_by = $data['created_by'];
// Get ID of last inserted row
if (array_key_exists('id', $this->table_columns) && !$data['id'])
{
$db = new TDb($this->table_name);
$last_record = $db->get_item(array('order' => 'id DESC', 'select' => 'id'));
$this->last_id = $last_record->id;
}
return $return;
}
public function bind_form()
{
if ($this->loadData)
{
$this->form->bind($this->getItem());
}
}
}