<?php
/**
 * @package DJ-Events
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 * @developer Szymon Woronowski - szymon.woronowski@design-joomla.eu
 *
 */

defined('_JEXEC') or die('Restricted access');

class DJEventsTableLocations extends JTable
{
	public function __construct(&$db)
	{
		parent::__construct('#__djev_locations', 'id', $db);
	}
	public function bind($array, $ignore = '')
	{

		if(empty($array['alias'])) {
			$array['alias'] = $array['name'];
            $array['alias'] = JFilterOutput::stringURLSafe($array['alias']);
		}


		
		return parent::bind($array, $ignore);
	}
	
	public function check() {
		
		return parent::check();
	}
	
	public function store($updateNulls = false)
	{
		$date	= JFactory::getDate();
		$user	= JFactory::getUser();
		$app = JFactory::getApplication();
		$db = JFactory::getDbo();
		
		if (!$this->id) {
			if (!intval($this->created)) {
				$this->created = $date->toSql();
			}
			if (empty($this->created_by)) {
				$this->created_by = $user->get('id');
			}
		}	
		
		$table = JTable::getInstance('Locations', 'DJEventsTable');
		if ($table->load(array('name'=>$this->name)) && $app->input->get('task') == 'save2copy') {
			$this->name .= ' (copy)';
		}
		
		//if ($table->load(array('alias'=>$this->alias,'cat_id'=>$this->cat_id,'city_id'=>$this->city_id)) && ($table->id != $this->id || $this->id==0)) {
		if ($table->load(array('alias'=>$this->alias)) && ($table->id != $this->id || $this->id==0)) {
			
			$alias = explode('-', $this->alias);
			$no = array_pop($alias);
			if(is_numeric($no)) {
				$this->alias = implode('-', $alias).'-'.++$no;
			} else {
				$this->alias .= '-1';
			}
		}
		

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