<?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');

require_once JPath::clean(JPATH_ADMINISTRATOR.'/components/com_djevents/models/event.php');

jimport('joomla.application.component.helper');

class DJEventsModelEventForm extends DJEventsModelEvent
{
	
	public function __construct($config = array()) {
		parent::__construct($config);
		JForm::addFormPath(__DIR__.JPath::clean('/forms'));
	}
	
	public function getForm($data = array(), $loadData = true)
	{
		// Initialise variables.
		$app	= JFactory::getApplication();
	
		// Get the form.
		$form = $this->loadForm('com_djevents.'.$this->form_name, $this->form_name, array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form)) {
			return false;
		}
		
		$user = JFactory::getUser();
		if ($user->id > 0) {
			$form->setValue('created_by', null, $user->id);
		}
		
		return $form;
	}
	
	public function validate($form, $data, $group = null) {
		
		$user = JFactory::getUser();
		$params  = DJEventsHelper::getParams();
		$db = JFactory::getDbo();
		
		if (isset($data['id']) && $data['id'] > 0) {
			// if the event has been edited we don't change it's state
			unset($data['published']);
			
			if ($item = $this->getItem($data['id'])) {
				$data['featured'] = $item->featured;
				$data['created'] = $item->created;
				$data['created_by'] = $item->created_by;
			}
			
		} else {
			if ($user->authorise('event.autopublish', 'com_djevents') || $user->authorise('core.edit.state', 'com_djevents')){
				$data['published'] = 1;
			} else {
				$data['published'] = 0;
			}
			
			if ($user->id > 0) {
				$data['created_by'] = $user->id;
			}
			
			$data['featured'] = 0;
			$data['created'] = '';
		}
		
		return parent::validate($form, $data, $group);
	}
	
	protected function preprocessForm(JForm $form, $data, $group = 'content') {
        $params  = DJEventsHelper::getParams();
        $user = JFactory::getUser();


        $showAlias = $params->get('show_alias', 1);
        $show_external_url = $params->get('show_external_url', 1);
        $show_price_information = $params->get('show_price_information', 1);
        $show_short_description = $params->get('show_short_description', 1);
        $show_video_link = $params->get('show_video_link', 1);

        if(!$showAlias) {
            $form->removeField('alias');
        }

        if(!$show_external_url) {
            $form->removeField('external_url');
        }

        if(!$show_price_information) {
            $form->removeField('price');
        }

        if(!$show_short_description) {
            $form->removeField('intro');
        }

        if(!$show_video_link) {
            $form->removeField('video');
        }

        if(!$user->authorise('location.selector', 'com_djevents') || (isset($data->location_id) && !$data->location_id)) {
            $form->setFieldAttribute('new_location', 'default', '1');
        }


	}
	
	protected function canDelete($record)
	{
		$user = JFactory::getUser();
	
		return (bool)($user->authorise('core.delete', 'com_djevents') 
				|| ($user->authorise('event.delete.own', 'com_djevents') && $user->id == $record->created_by));
	}

	public function save($data) {

	    $app = JFactory::getApplication();
	    $saveLocation = $app->input->get('save_location');
	    $newLocation = $data['new_location'];
	    $locationId = $data['location_id'];
        $locationSaved = false;

	    if($newLocation || $data['online_event']) {
	        $data['location_id'] = 0;
        }

	    if($saveLocation) {
	        JModelLegacy::addIncludePath(JPATH_ROOT . '/administrator/components/com_djevents/models', 'DJEventsModel');
	        $locationModel = JModelLegacy::getInstance('Location','DJEventsModel');
            $locationData = $data;
            $locationData['id'] = 0;
            $locationData['name'] = $data['location'];

            $locationSaved = $locationModel->save($locationData);
            if (!$locationSaved) {
                $this->setError($locationModel->getError());
                return;
            }



	        $location = $locationModel->getItem();


            $locationId = $location->id;

            unset($data['new_location']);
            $data['location_id'] = $locationId;
        }

        if (!$newLocation || $locationSaved) {
            $data['location'] = '';
            $data['address'] = '';
            $data['post_code'] = '';
            $data['latitude'] = '';
            $data['longitude'] = '';
        }

	    return parent::save($data);
    }


	protected function canEditState($record)
	{
		$user = JFactory::getUser();
		$canEdit = $user->authorise('core.edit.state', $this->option);
		$canEditOwn = $user->authorise('event.autopublish', $this->option);
		
		return (($user->id == $record->created_by && $canEditOwn) || $canEdit) ? true : false;
	}
	
}
