Your IP : 216.73.216.240


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

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

namespace TechFry\Library\Model;

\defined('_JEXEC') or die;

use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Table\Table;
use Joomla\Database\DatabaseDriver;
use Joomla\Registry\Registry;
use Joomla\CMS\Language\Text;
use TechFry\Library\TDb;

class TfTable extends Table
{
    // getFields, getTableName, getKeyName, getDbo, bind, load, check, store, save, delete, hasPrimaryKey, hit, getNextOrder

    protected $_supportNullValue = true;
    
    public $table_name;

    public $columns;

    public $required_fields = array();
    public $date_fields = array();
    
    public function __construct(DatabaseDriver $db)
    {
        $this->init();
        
        parent::__construct('#__' . $this->table_name, 'id', $db);

        $this->columns = $this->getFields();
    }

    public function bind($array, $ignore = '')
    {
        // Search for the {readmore} tag and split the text up accordingly.

        // Convert array to json string using registry

        return parent::bind($array, $ignore);
    }

    public function check()
    {
        try 
        {
            parent::check();
        } 
        catch (\Exception $e) 
        {
            $this->setError($e->getMessage());

            return false;
        }

        // Check for required fields
        foreach ($this->required_fields as $required)
        {
            if (trim($this->$required) == '')
            {
                $this->setError(Text::_('COM_CONTENT_WARNING_PROVIDE_VALID_NAME'));

                return false;
            }
        }

        // Generate alias from title
        if (array_key_exists('alias', $this->columns))
        {
            if (trim($this->alias) == '') 
            {
                $this->alias = $this->title;
            }

            $this->alias = ApplicationHelper::stringURLSafe($this->alias, $this->language);
        }

        if (array_key_exists('ordering', $this->columns))
        {
            if (!$this->ordering)
            {
                $this->ordering = $this->getNextOrder();
            }
        }

        // Hits must be zero on a new item
        if (array_key_exists('hits', $this->columns))
        {
            if (!$this->id)
            {
                $this->hits = 0;
            }
        }

        // Set default access to Public
        if (array_key_exists('access', $this->columns))
        {
            if (!isset($this->access))
            {
                $this->access = 1;
            }
        }

        // Set default state to Published
        if (array_key_exists('published', $this->columns))
        {
            if (!isset($this->published))
            {
                $this->published = 1;
            }
        }

        // Set default language
        if (array_key_exists('language', $this->columns))
        {
            if (!isset($this->language))
            {
                $this->language = '*';
            }
        }

        // Set publish_up and publish down to null if not set
        if (array_key_exists('publish_up', $this->columns))
        {
            if (!$this->publish_up) 
            {
                $this->publish_up = null;
            }

            if (!$this->publish_down) 
            {
                $this->publish_down = null;
            }

            // Check the publish down date is not earlier than publish up
            if (!is_null($this->publish_up) && !is_null($this->publish_down) && $this->publish_down < $this->publish_up)
            {
                // Swap the dates
                $temp = $this->publish_up;
                $this->publish_up = $this->publish_down;
                $this->publish_down = $temp;
            }
        }

        // Set other date fields to null if not set
        foreach ($this->date_fields as $date_field)
        {
        	if (!$this->$date_field)
    		{
    			$this->$date_field = null;
    		}
            else
            {
                $date_for_sql = Factory::getDate($this->$date_field);
    		    $this->$date_field = $date_for_sql->toSql();
            }
        }
        
        return true;
    }

    public function store($updateNulls = true)
    {
        $date = Factory::getDate()->toSql();
        $user = Factory::getUser();
        
        // Set created date if not set
        if (array_key_exists('created', $this->columns))
        {
            if (!(int) $this->created) 
            {
                $this->created = $date;
            }
            else
            {
                $created_date = Factory::getDate($this->created);
                $this->created = $created_date->toSql();
            }
        }

        // Set modified date
        if (array_key_exists('modified', $this->columns))
        {
            $this->modified = $date;
        }

        // Set created by
        if (array_key_exists('created_by', $this->columns))
        {
            if (empty($this->created_by)) 
            {
                $this->created_by = $user->get('id');
            }
        }

        // Verify alias is unique
        if (array_key_exists('alias', $this->columns))
        {
            $found = 0;
            $i = 2;
            $base_alias = $this->alias;
            do
            {
                $db = new TDb($this->table_name);
                $item = $db->get_item(array('id' => $this->alias, 'column' => 'alias'));
                if ($item && ($item->id != $this->id || $this->id == 0))
                {
                    $this->alias = $base_alias . '-' . $i;
                    $found = 1;
                    $i++;
                }
                else
                {
                    $found = 0;
                }
            } while ($found);
        }

        unset($this->columns);

        return parent::store($updateNulls);
    }
}