<?php
namespace JExtstore\Component\JChat\Administrator\Table;
/**
 *
 * @package JCHAT::FORM::components::com_jchat
 * @subpackage tables
 * @author Joomla! Extensions Store
 * @Copyright (C) 2015 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
 */
// no direct access
defined ( '_JEXEC' ) or die ( 'Restricted access' );
use Joomla\CMS\Language\Text;
use Joomla\CMS\Table\Table;
use Joomla\Database\DatabaseDriver;

/**
 * ORM Table for rooms entities
 *
 * @package JCHAT::FORM::components::com_jchat
 * @subpackage tables
 * @since 1.0
 */
class RoomsTable extends Table {
	/**
	 *
	 * @var int
	 */
	public $id = 0;
	
	/**
	 *
	 * @var string
	 */
	public $name = '';
	
	/**
	 *
	 * @var string
	 */
	public $description = null;
	
	/**
	 *
	 * @var int
	 */
	public $checked_out = null;
	
	/**
	 *
	 * @var datetime
	 */
	public $checked_out_time = null;
	
	/**
	 *
	 * @var int
	 */
	public $published = 1;
	
	/**
	 *
	 * @var int
	 */
	public $ordering = 0;
	
	/**
	 *
	 * @var int
	 */
	public $access = 1;
	
	/**
	 *
	 * @var string
	 */
	public $menuitems = null;
	
	/**
	 * Check Table override
	 * @override
	 *
	 * @see Table::check()
	 */
	public function check() {
		// Name required
		if (! $this->name) {
			$this->setError ( Text::_ ( 'COM_JCHAT_VALIDATION_ERROR' ) );
			return false;
		}
		
		return true;
	}
	
	/**
	 * Bind Table override
	 * @override
	 *
	 * @see Table::bind()
	 */
	public function bind($fromArray, $ignore = array(), $saveTask = false, $sessionTask = false) {
		parent::bind ( $fromArray, $ignore);
	
		// MySql strict mode primary key auto increment compliance
		if(!$this->id) {
			$this->id = 0;
		}
		
		// Serialize comma separated
		if (is_array ( $this->menuitems ) && $saveTask) {
			$this->menuitems = implode(',', $this->menuitems);
		}

		return true;
	}
	
	/**
	 * Loads a row from the database and binds the fields to the object properties
	 *
	 * @override
	 *
	 * @access public
	 * @param
	 *        	mixed	Optional primary key. If not specifed, the value of current key is used
	 * @return boolean if successful
	 */
	public function load($keys = null, $reset = true) {
		// If not $idEntity set return empty object
		if($keys) {
			if(!parent::load ( $keys, $reset )) {
				return false;
			}
		}
	
		// Unserialize comma separated
		if ($this->menuitems) {
			$this->menuitems = explode(',', $this->menuitems );
		} else {
			// Init qui
			$this->menuitems = array ();
		}
	
		return true;
	}
	
	/**
	 * Store Table override
	 * @override
	 *
	 * @see JTable::store()
	 */
	public function store($updateNulls = false) {
		// Force 0 to NULL and update nulls
		if($this->menuitems == '0' || !$this->menuitems) {
			$this->menuitems = null;
			$updateNulls = true;
		}

		$result = parent::store($updateNulls);

		return $result;
	}
	
	/**
	 * Class constructor
	 *
	 * @param Object& $_db
	 *        	return Object&
	 */
	public function __construct(DatabaseDriver $db) {
		parent::__construct ( '#__jchat_rooms', 'id', $db );
		
		// Support null values for datetime field
		$this->_supportNullValue = true;
	}
}