<?php
/**
 * @author QuanticaLabs - http://codecanyon.net/user/QuanticaLabs/portfolio?ref=QuanticaLabs
 *
 * @link			http://codecanyon.net/item/timetable-responsive-schedule-for-joomla/9749539?ref=QuanticaLabs
 * @copyright		Copyright (C) 2008 - 2014 quanticalabs.com . All rights reserved.
 * @license			GNU General Public License version 2 or later; see LICENSE under Licensing/ directory
 */
 
defined('_JEXEC') or die('Restricted access');

JLoader::register('TimetableHelper', JPATH_ADMINISTRATOR . '/components/com_timetable/helpers/timetable.php');

/**
 * Column Model
 */
class TimetableModelColumn extends JModelAdmin
{
	
	public $typeAlias = 'com_timetable.column';
	protected $text_prefix = 'COM_TIMETABLE';
	
	/**
	 * Returns a reference to the a Table object, always creating it.
	 *
	 * @param       type    The table type to instantiate
	 * @param       string  A prefix for the table class name. Optional.
	 * @param       array   Configuration array for model. Optional.
	 *
	 * @return      JTable  A database object
	 * @since       2.5
	 */
	public function getTable($type = 'Columns', $prefix = 'TimetableTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}

	/**
	 * Method to get the record form.
	 *
	 * @param       array   $data     Data for the form.
	 * @param       boolean $loadData True if the form is to load its own data (default case), false if not.
	 *
	 * @return      mixed   A JForm object on success, false on failure
	 * @since       2.5
	 */
	public function getForm($data = array(), $loadData = true)
	{
		// Get the form.
		$form = $this->loadForm('com_timetable.column', 'column',
			array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form))
		{
			return false;
		}
		return $form;
	}

	/**
	 * Method to get the data that should be injected in the form.
	 *
	 * @return      mixed   The data for the form.
	 * @since       2.5
	 */
	protected function loadFormData()
	{
		// Check the session for previously entered form data.
		$data = JFactory::getApplication()->getUserState('com_timetable.edit.column.data', array());
		if(empty($data))
		{
			$data = $this->getItem();
		}
		return $data;
	}
	
	protected function prepareTable($table)
	{
		$table->title = htmlspecialchars_decode($table->title, ENT_QUOTES);
		$table->alias = JApplication::stringURLSafe($table->alias);

		if (empty($table->alias))
		{
			$table->alias = JApplication::stringURLSafe($table->title);
		}
	}
	
	public function save($data)
	{
		//generate column alias if not specified
		if($data['alias'] == null)
		{
			if (JFactory::getConfig()->get('unicodeslugs') == 1)
			{
				$data['alias'] = JFilterOutput::stringURLUnicodeSlug($data['title']);
			}
			else
			{
				$data['alias'] = JFilterOutput::stringURLSafe($data['title']);
			}
		}
		
		$table = JTable::getInstance('Columns', 'TimetableTable');
		
		//check if the column alias is already used
		if($table->load(array('alias' => $data['alias'])))
		{
			//ignore cases when saving the same column
			if(!($data['id']!=null && $data['id']==(int)$table->id))
			{
				$msg = JText::_('COM_TIMETABLE_COLUMN_ALIAS_EXISTS');
				$alias = $data['alias'];
				while($table->load(array('alias' => $alias)))
				{
					$alias = JString::increment($alias, 'dash');
				}
				$data['alias'] = $alias;
			}
		}
		
		if(isset($msg))
		{
			JFactory::getApplication()->enqueueMessage($msg, 'warning');			
		}
		
		return parent::save($data);
	}
	
}