Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/Model/Back/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/Model/Back/TfModelForm.php

<?php
/*
* @package		Tech Fry Library
* @license		https://www.gnu.org/licenses/gpl-3.0.en.html
*/

namespace TechFry\Library\Model\Back;

defined('_JEXEC') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Associations;
use Joomla\CMS\Language\LanguageHelper;
use Joomla\CMS\MVC\Model\AdminModel;
use Joomla\CMS\Form\Form;
use Joomla\Registry\Registry;
use Joomla\CMS\Form\FormFactoryInterface;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
use TechFry\Library\Model\TfModelTrait;
use TechFry\Library\Model\TfModelFormTrait;
use TechFry\Library\TForm;
use TechFry\Library\TDb;

class TfModelForm extends AdminModel
{
    // AdminModel: delete(), getItem(), publish(), save()

    use TfModelTrait, TfModelFormTrait;
    
    public $form;
    
    public $table;
    
    public $loadData = true;
    
    public $basic_fields = [];
    
    public $date_fields = []; // Shifted to Table Class
    
    // Fields saved as JSON in the database
    public $subform_fields = [];
    public $multiple_fields = [];
    
    public $pro_fields = [];
    
    public $is_ml = false;
    
    public function __construct($config = [], ?MVCFactoryInterface $factory = null, ?FormFactoryInterface $formFactory = null)
	{
		parent::__construct($config, $factory, $formFactory);
		
		if ($this->table_name)
		{
		    $db = new TDb($this->table_name);
            $this->table_columns = $db->get_table_columns();    
		}
		
		$this->set_basic_fields();
		
		$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();
	}
	
	public function getTable($name = '', $prefix = 'Table', $options = array())
    {
        if (empty($name))
        {
            if (empty($this->table))
            {
                $name = ucwords($this->getName());
            }
            else
            {
                $name = $this->table;
            }
        }
        
        if ($table = $this->_createTable($name, $prefix, $options))
        {
            return $table;
        }

        throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_TABLE_NAME_NOT_SUPPORTED', $name), 0);
    }
    
    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' => $this->loadData));
        
        if (empty($this->form))
        {
            return false;
        }
        
        // Change pro fields type
        if (!$this->is_pro())
        {
          	foreach ($this->pro_fields as $group => $fields)
            {
                foreach ($fields as $field_name)
                {
                    $group = ($group == 'default') ? '' : $group;
                  
                  	$current_type = $this->form->getFieldAttribute($field_name, 'type', '', $group);
                  	
                  	$new_type = $current_type . 'pro';
                  	
                  	$this->form->setFieldAttribute($field_name, 'type', $new_type, $group);
                }
            }
        }
        
        $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))
        {
            $data = $this->getItem();
        }
        
        return $data;
    }
    
    public function complete_form()
    {
        foreach ($this->basic_fields as $field)
        {
            switch ($field)
            {
                case 'title' :
                    $fieldset = '';
                    $element = '<field name="title" type="text" label="JGLOBAL_TITLE" required="true" />';
                    break;
                    
                case 'alias' :
                    $fieldset = '';
                    $element = '<field name="alias" type="text" label="JFIELD_ALIAS_LABEL" hint="JFIELD_ALIAS_PLACEHOLDER" />';
                    break;
                
                case 'description' :
                    $fieldset = 'description';
                    $element = '<field name="description" type="editor" label="JGLOBAL_DESCRIPTION" hiddenLabel="true" buttons="true" filter="JComponentHelper::filterText" />';
                    break;
            
                case 'note' : 
                    $fieldset = 'options';
                    $element = '<field name="note" type="text" label="COM_TF_NOTE" />';
                    break;
                    
                case 'published' :
                    $fieldset = 'options';
                    $element = '<field name="published" type="list" label="JSTATUS" default="1"';
                    $element .= ' class="form-select-color-state">';
                    $element .= '<option value="1">JPUBLISHED</option><option value="0">JUNPUBLISHED</option>';
                    $element .= '<option value="2">JARCHIVED</option><option value="-2">JTRASHED</option></field>';
                    break;
                    
                case 'access' :
                    $fieldset = 'options';
                    $element = '<field name="access" type="accesslevel" label="JFIELD_ACCESS_LABEL" description="JFIELD_ACCESS_DESC" default="1" />';
                    break;
                    
                case 'language' :
                    $fieldset = 'options';
                    $element = '<field name="language" type="contentlanguage" label="JFIELD_LANGUAGE_LABEL"><option value="*">JALL</option></field>';
                    break;
                    
                case 'ordering' :
                    $fieldset = 'options';
                    $element = '<field name="ordering" type="number" label="COM_TF_ORDERING" default="0" min="0" />';
                    break;
                    
                case 'id' :
                    $fieldset = 'details';
                    $element = '<field name="id" type="text" label="JGLOBAL_FIELD_ID_LABEL" description="JGLOBAL_FIELD_ID_DESC"';
                    $element .= ' class="readonly" size="10" default="0" readonly="true" />';
                    break;

                case 'publish_up' :
                    $fieldset = 'details';
                    $element = '<field name="publish_up" type="calendar" label="JGLOBAL_FIELD_PUBLISH_UP_LABEL" translateformat="true" showtime="true" filter="none" />';
                    break;

                case 'publish_down' :
                    $fieldset = 'details';
                    $element = '<field name="publish_down" type="calendar" label="JGLOBAL_FIELD_PUBLISH_DOWN_LABEL" translateformat="true" showtime="true" filter="none" />';
                    break;

                case 'created' :
                    $fieldset = 'details';
                    $element = '<field name="created" type="calendar" label="JGLOBAL_CREATED_DATE" translateformat="true" showtime="true" filter="none" readonly="true" />';
                    break;
                    
                case 'modified' :
                    $fieldset = 'details';
                    $element = '<field name="modified" type="calendar" label="JGLOBAL_FIELD_MODIFIED_LABEL" translateformat="true" showtime="true" filter="none" readonly="true" />';
                    break;
                    
                case 'created_by' :
                    $fieldset = 'details';
                    $element = '<field name="created_by" type="user" label="COM_TF_CREATED_BY" />';
                    break;
                    
                case 'hits' :
                    $fieldset = 'details';
                    $element = '<field name="hits" type="number" label="JGLOBAL_HITS" readonly="true" default="0" />';
                    break;
            }
            
            $this->set_field($element, $fieldset);
        }
    }
    
    public function save($data)
    {
        if (!$this->is_pro() && isset($this->db_limit))
        {
            $db = new TDb($this->table_name);
            $total_rows = $db->total_rows();
            
            if ($total_rows > $this->db_limit)
            {
                $this->setError(Text::sprintf('COM_TF_PRO_DB_MESSAGE', $this->db_limit));
            
                return;
            }
        }

        // 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 (group) 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;
            }
            elseif (!isset($data[$field])) // If not set, then it should be empty
            {
                // Depreciated, set at component level
                // $data[$field] = '';
            }
        }

        return parent::save($data);
    }
    
    public function validate($form, $data, $group = null)
    {
        return $data;
    }
    
    public function getItem($pk = null)
    {
        $item = parent::getItem($pk);
        
        // Convert JSON string to array
        foreach ($this->multiple_fields as $k => $v)
        {
            if ($item && property_exists($item, $v))
            {
                $registry = new Registry($item->$v);
                
                $item->$v = $registry->toArray();
            }
        }
        
        // Load associated content items
        $assoc = Associations::isEnabled();
		if ($assoc && $this->is_ml)
		{
			$item->associations = array();
			if ($item->id != null)
			{
				$associations = Associations::getAssociations($this->option, '#__' . $this->table_name, $this->associationsContext, $item->id,  'id', 'alias', '');

				foreach ($associations as $tag => $association)
				{
					$item->associations[$tag] = $association->id;
				}
			}
		}
        
        return $item; 
    }
    
    protected function preprocessForm(Form $form, $data, $group = 'content')
	{
        $str = ucwords(substr($this->option, 6));
		
		// Association content items
		if (Associations::isEnabled() && $this->is_ml)
		{
			$languages = LanguageHelper::getContentLanguages(false, false, null, 'ordering', 'asc');

			if (count($languages) > 1)
			{
				$addform = new \SimpleXMLElement('<form />');
				$fields = $addform->addChild('fields');
				$fields->addAttribute('name', 'associations');
				$fieldset = $fields->addChild('fieldset');
				$fieldset->addAttribute('name', 'item_associations');
				$fieldset->addAttribute('addfieldprefix', 'TechFry\Component\Tf' . $str . '\Administrator\Field');

				foreach ($languages as $language)
				{
					$field = $fieldset->addChild('field');
					$field->addAttribute('name', $language->lang_code);
					$field->addAttribute('type', 'modal_' . $this->name);
					$field->addAttribute('language', $language->lang_code);
					$field->addAttribute('label', $language->title);
					$field->addAttribute('translate_label', 'false');
					
					$field->addAttribute('select', 'true');
					// $field->addAttribute('new', 'true');
					// $field->addAttribute('edit', 'true');
					$field->addAttribute('clear', 'true');
					// $field->addAttribute('propagate', 'true');
				}

				$form->load($addform, false);
			}
		}
		
		parent::preprocessForm($form, $data);
	}
    
    public function bind_form()
    {
        if ($this->loadData)
        {
            $this->form->bind($this->getItem());
        }
    }
    
    // Save data in junction table
    public function save_injunction($table_name, $conditions = array(), $column_name = 'label_id', $ids = array())
    {
        // First, delete all current ids
        $db = Factory::getDbo();
        $query = $db->getQuery(true);
        
        foreach ($conditions as $k => $v)
        {
            $where[] = $db->quoteName($k) . ' = ' . $db->quote($v);
        }

        $query->delete($db->quoteName('#__' . $table_name));
        $query->where($where);
        
        $db->setQuery($query);
        $db->execute();
        
        // Then, add records
        foreach ($ids as $id)
        {
            $data_obj = new \stdClass;
            foreach ($conditions as $k => $v)
            {
                $data_obj->$k = $v;
            }
            
            $data_obj->$column_name = $id;
            
            Factory::getDbo()->insertObject('#__' . $table_name, $data_obj);
        }
    }
    
    // Get data from junction table
    public function get_injunction($table_name, $conditions = array(), $column_name = 'label_id')
    {
        $db = Factory::getDbo();
        $query = $db->getQuery(true);
        
        $query->select($column_name)
            ->from($db->quoteName('#__' . $table_name));
        
        foreach ($conditions as $k => $v)
        {
            $query->where($db->quoteName($k) . ' = ' . $db->quote($v));
        }
            
        $db->setQuery($query);

		$column_arr = $db->loadColumn();

	    return $column_arr;
    }
    
    public function set_basic_fields()
    {
        $core_fields = array('title', 'description', 'alias', 'published', 'access', 'language', 'ordering', 'note', 'publish_up', 'publish_down', 'created', 'modified', 'created_by', 'hits', 'id');
        foreach ($core_fields as $core_field)
        {
            if (array_key_exists($core_field, $this->table_columns))
            {
                $this->basic_fields[] = $core_field;
            }
        }
    }
}