<?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\Factory;
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 2.44
 */
class MeetingsTable extends Table {
	/**
	 *
	 * @var int
	 */
	public $id = null;
	
	/**
	 *
	 * @var string
	 */
	public $hash = null;
	
	/**
	 *
	 * @var string
	 */
	public $name = null;
	
	/**
	 *
	 * @var string
	 */
	public $description = null;
	
	/**
	 *
	 * @var string
	 */
	public $participants = null;
	
	/**
	 *
	 * @var datetime
	 */
	public $start_datetime = null;
	
	/**
	 *
	 * @var datetime
	 */
	public $end_datetime = null;
	
	/**
	 *
	 * @var int
	 */
	public $checked_out = 0;
	
	/**
	 *
	 * @var datetime
	 */
	public $checked_out_time = null;
	
	/**
	 *
	 * @var int
	 */
	public $published = 1;
	
	/**
	 *
	 * @var int
	 */
	public $ordering = 0;
	
	/**
	 * Auto generate the random meeting hash string
	 *
	 * @access private
	 * @param int $length
	 * @return string
	 */
	private function generateRandomString($length = 10) {
		return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
	}
	
	/**
	 * Check Table override
	 * @override
	 *
	 * @see JTable::check()
	 */
	public function check() {
		// Name required
		if (! $this->name) {
			$this->setError ( Text::_ ( 'COM_JCHAT_VALIDATION_ERROR' ) );
			return false;
		}
		
		return true;
	}
	
	/**
	 * Binds a named array/hash to this object
	 *
	 * @override
	 *
	 * @access public
	 * @param
	 *        	mixed	Optional primary key. If not specifed, the value of current key is used
	 * @param boolean $saveTask
	 * @return boolean if successful
	 */
	function bind($from, $ignore = array(), $saveTask = false) {
		// Get the user timezone setting defaulting to the server timezone setting.
		if($saveTask) {
			$offset = Factory::getApplication()->getIdentity()->getTimezone();
			$from['start_datetime'] = Factory::getDate($from['start_datetime'], $offset)->toSql();
			$from['end_datetime'] = Factory::getDate($from['end_datetime'], $offset)->toSql();
		}
		
		// Parent std load
		parent::bind ( $from, $ignore );
	
		// Serialize comma separated
		if (is_array ( $this->participants ) && $saveTask) {
			$this->participants = implode(',', $this->participants);
		}

		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->participants) {
			$this->participants = explode(',', $this->participants );
		} else {
			// Init qui
			$this->participants = array ();
		}
	
		return true;
	}
	
	/**
	 * Store Table override
	 * @override
	 *
	 * @see JTable::store()
	 */
	public function store($updateNulls = true) {
		// Force 0 to NULL and update nulls
		if($this->participants == '0') {
			$this->participants = null;
		}
		
		// Auto generate the meeting Hash ID on first new record save
		if(!$this->hash) {
			$this->hash = $this->generateRandomString(5) . '-' . $this->generateRandomString(5) . '-' . $this->generateRandomString(5);
		}

		$result = parent::store($updateNulls);

		return $result;
	}
	
	/**
	 * Class constructor
	 *
	 * @param Object& $_db
	 *        	return Object&
	 */
	public function __construct(DatabaseDriver $_db) {
		parent::__construct ( '#__jchat_meetings', 'id', $_db );
		
		// Support null values for datetime field
		$this->_supportNullValue = true;
	}
}