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

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

class DJEventsControllerEvent extends JControllerForm {
	public function save($key = null, $urlVar = null) {
		return parent::save($key, $urlVar);
	}

	// saving tags
	protected function postSaveHook(JModelLegacy $model, $validData = array()){
		$app = JFactory::getApplication();
		$db = JFactory::getDBO();
		$params = JComponentHelper::getParams('com_djevents');

		if($params->get('tags_type', 'djevents') == 'joomla'){
			$item = $model->getItem();
			$event_id = $item->get('id');
			$input_arr = $app->input->getArray();
			$tags = $input_arr['jform']['tags'];

			$tag_helper = new JHelperTags;
			$tags = $tag_helper->createTagsFromField($input_arr['jform']['tags']);

			$query = "DELETE FROM #__djev_jtags_xref WHERE event_id=".$event_id;
			$db->setQuery($query);
			$db->execute();

			foreach($tags as $tag){
				$query = "INSERT INTO #__djev_jtags_xref(tag_id, event_id) VALUES(".$tag.", ".$event_id.")";
				$db->setQuery($query);
				$db->execute();
			}
		}
	}
}
