<?php

/**
* @Copyright Copyright (C) 2010 www.profinvent.com. All rights reserved.
* Copyright (C) 2011-2014 Open Source Group GmbH www.osg-gmbh.de
* @website http://www.profinvent.com
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

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

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

require_once (JPATH_COMPONENT_ADMINISTRATOR . DS . 'helpers' . DS . 'seminarman.php');

class seminarmanModelapplication extends JModelLegacy
{
    var $_id = null;

    var $_data = null;

    function __construct()
    {
        parent::__construct();

        $id = JRequest::getVar('id', 0, '', 'int');
        $this->_course_id = JRequest::getVar('course_id', 0, '', 'int');
        $this->setId((int)$id);
    }

    function setId($id)
    {
        $this->_id = $id;
        $this->_data = null;
    }

    function &getData()
    {
        if ($this->_loadData()) {
            $user = JFactory::getUser();

            if (!$this->_data->published) {
                JError::raiseError(404, JText::_("Resource Not Found"));
                return false;
            }

            if (!$this->_data->cat_pub) {
                JError::raiseError(404, JText::_("Resource Not Found"));
                return;
            }

            if ($this->_data->cat_access > $user->get('aid', 0)) {
                JError::raiseError(403, JText::_('ALERTNOTAUTH'));
                return;
            }
        } else
            $this->_initData();

        return $this->_data;
    }
    
    function hit()
    {
        $mainframe = JFactory::getApplication();

        if ($this->_id) {
            $booking = $this->getTable();
            $booking->hit($this->_id);
            return true;
        }
        return false;
    }

    function isCheckedOut($uid = 0)
    {
        if ($this->_loadData()) {
            if ($uid) {
                return ($this->_data->checked_out && $this->_data->checked_out != $uid);
            } else {
                return $this->_data->checked_out;
            }
        }
    }

    function checkin()
    {
        if ($this->_id) {
            $booking = $this->getTable();
            if (!$booking->checkin($this->_id)) {
                $this->setError($this->_db->getErrorMsg());
                return false;
            }
            return true;
        }
        return false;
    }

    function checkout($uid = null)
    {
        if ($this->_id) {
            if (is_null($uid)) {
                $user = JFactory::getUser();
                $uid = $user->get('id');
            }

            $booking = $this->getTable();
            if (!$booking->checkout($uid, $this->_id)) {
                $this->setError($this->_db->getErrorMsg());
                return false;
            }

            return true;
        }
        return false;
    }

    function store($data)
    {
    	$db = $this->getDBO();
    	 
        JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_seminarman'.DS.'tables');
        $row = JTable::getInstance('application', 'Table');
        
        $tempArray = array();
        $dataArray = array('date'=>date('Y-m-d H:i:s'), 'user'=>JText::_('COM_SEMINARMAN_ONLINE_BOOKING'), 'status'=>0);
        array_push($tempArray, $dataArray);
        $data['params']['protocols'] = json_encode($tempArray);
        
        if (!$row->bind($data)) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        $row->date = gmdate('Y-m-d H:i:s');
        
        if (!$row->id) {
        	$is_insert = 1;
            $where = 'course_id = ' . (int)$row->course_id;
            $row->ordering = $row->getNextOrder($where);
        }
        else
        	$is_insert = 0;

        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        
        if (!$row->store()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        
        $user = JFactory::getUser();
        if ($user->guest)
        	$uid = $data['user_id'];
        else
        	$uid = $user->id;
        
        $title = $db->quote($data['title']);
        $salutation = $db->quote($data['salutation']);
        $db->setQuery('REPLACE INTO `#__seminarman_fields_values_users_static` (user_id, salutation, title) VALUES ('.$uid.','.$salutation.','.$title.')');
        $db->query();
        
        return ($row->id);
    }
    
    function uploadfile()
    {
        $mainframe = JFactory::getApplication();
        $params = JComponentHelper::getParams('com_seminarman');

        $file = JRequest::getVar('cv', null, 'files', 'array');

        if ($file['size'] > 0) {
            jimport('joomla.filesystem.file');

            $path = COM_SEMINARMAN_CVFILEPATH . DS;

            $filename = seminarman_upload::sanitize($path, $file['name']);

            $filepath = JPath::clean($path . strtolower($filename));

            $src = $file['tmp_name'];

            $dest = $filepath;

            if (!seminarman_upload::check($file, $err)) {
                if ($format == 'json') {
                    jimport('joomla.error.log');
                    $log = JLog::getInstance('com_seminarman.error.php');
                    $log->addEntry(array('comment' => 'Invalid: ' . $filepath . ': ' . $err));
                    header('HTTP/1.0 415 Unsupported Media Type');
                    die('Error. Unsupported Media Type!');
                } else {
                    JError::raiseNotice(100, JText::_($err));

                    if ($return) {
                        $mainframe->redirect(base64_decode($return));
                    }
                    return;
                }
            }

            if (JFile::upload($src, $dest)) {
                return $filename;
            } else {
                $msg .= "File upload failed.";
                return false;
            }
        }
    }

    function copyfile($file)
    {
        $mainframe = JFactory::getApplication();

        jimport('joomla.filesystem.file');
        $filename = JFile::makeSafe($file);

        $path = COM_SEMINARMAN_CVFILEPATH . DS;

        $filename_new = $this->updateFilename($filename);
        $filename_new = seminarman_upload::sanitize($path, $filename_new);

        $src = COM_SEMINARMAN_UPLOADEDCVFILEPATH . DS . $filename;
        $dst = COM_SEMINARMAN_CVFILEPATH . DS . $filename_new;
        if (JFile::copy($src, $dst)) {
            return $filename_new;
        }
        return false;
    }
    function updateFilename($filename)
    {
        $mainframe = JFactory::getApplication();

        jimport('joomla.filesystem.file');

        $filename = preg_replace("/^[.]*/", '', $filename);
        $filename = preg_replace("/[.]*$/", '', $filename);

        $lastdotpos = strrpos($filename, '.');

        $chars = '[^0-9a-zA-Z()_-]';
        $filename = strtolower(preg_replace("/$chars/", '_', $filename));

        $beforedot = substr($filename, 0, $lastdotpos);
        $afterdot = substr($filename, $lastdotpos + 1);

        $filename_new = substr($beforedot, 0, (strlen($beforedot) - 11)) . '.' . $afterdot;
        return $filename_new;
    }

    function sendemail($emaildata, $emailTemplate = 0, $attachment = '')
    {
        $mainframe = JFactory::getApplication();
        $db = JFactory::getDBO();

        if ($emailTemplate != 0) {
            $emailCond = " AND id=" . $emailTemplate;
        } else {
        	$emailCond = " AND isdefault=1";
        }

        $query = "SELECT * FROM #__seminarman_emailtemplate WHERE (templatefor=0".  $emailCond . ")";
        $db->setQuery($query);
        $template = $db->loadObject();
        if ($template) {
    		$config = JFactory::getConfig();
            $msgSubject = $template->subject;
            $msgBody = $template->body;
            $jversion = new JVersion();
            $short_version = $jversion->getShortVersion();
            if (version_compare($short_version, "3.0", 'ge')) {
                $msgSender = array($config->get('mailfrom'), $config->get('fromname'));
        	} else {
        		$msgSender = array($config->getValue('mailfrom'), $config->getValue('fromname'));
        	}
            $msgRecipient = $template->recipient;
            $msgRecipientBCC = $template->bcc;
            
            if (!JHTMLSeminarman::sendEmailToUserApplication($emaildata, $msgSubject, $msgBody, $msgSender, $msgRecipient, NULL, $msgRecipientBCC, $attachment))
            	return false;
            return true;
        }
        return false;
    }

    function saveCustomfields($applicationId, $userId, $fields)
    {
        $db = $this->getDBO();

        foreach ($fields as $id => $value) {
        	
        	$q = 'REPLACE INTO `#__seminarman_fields_values`'.
        	     ' (applicationid, user_id, field_id, value)'.
        	     ' VALUES ('.(int)$applicationId.','.(int)$userId.','.(int)$id.','.$db->quote($value).')';
        	$db->setQuery($q);
        	$db->query();
        	
        	$user = JFactory::getUser();
        	$q = 'REPLACE INTO `#__seminarman_fields_values_users`'.
        	     ' (user_id, fieldcode, value)'.
        	     ' VALUES ('.(int)$userId.',(SELECT fieldcode FROM `#__seminarman_fields` WHERE id='.(int)$id.'),'.$db->quote($value).')';
        	 
        	$db->setQuery($q);
        	$db->query();
        }
    }

    function getAdminEmails()
    {
        $emails = '';
        $db = $this->getDBO();

        $query = 'SELECT ' . $db->quoteName('email') . ' FROM ' . $db->quoteName('#__users') .
            ' WHERE ' . $db->quoteName('gid') . '=' . $db->quote(24) . ' OR ' . $db->
            quoteName('gid') . '=' . $db->Quote(25);

        $db->setQuery($query);
        $emails = $db->loadResultArray();

        return $emails;
    }

    function getCurrentBookings()
    {
        $mainframe = JFactory::getApplication();
        $params = $mainframe->getParams();
        if ($params->get('current_capacity') == 1) {
            $statusTopLimit = 3;
            $statusBottomLimit = -1;
        }
        if ($params->get('current_capacity') == 2) {
            $statusTopLimit = 3;
            $statusBottomLimit = 0;
        }

        $db = JFactory::getDBO();
        $sql = 'SELECT SUM(b.attendees)' . ' FROM #__seminarman_application AS b' .
            ' WHERE b.published = 1' . ' AND b.course_id = ' . $this->_course_id .
            ' AND b.status <' . $statusTopLimit = 3 . ' AND b.status >' . $statusBottomLimit;
        $db->setQuery($sql);
        $current_bookings = $db->loadResult();
        return $current_bookings;
    }
    
    function getCurrentBookingsForUser($user_id = 0)
    {
    	if ($user_id == 0)
    		return 0;
    	
    	$db = JFactory::getDBO();
    	$q = 'SELECT COUNT(id) FROM `#__seminarman_application`'.
    	          ' WHERE user_id = '. (int)$user_id.
    	          ' AND course_id = ' . $this->_course_id .
    	          ' AND published = 1 AND status < 3';
    	$db->setQuery($q);
    	return (int)$db->loadResult();
    }


	function sendRegistrationEmail($user, $password)
	{
		$config	= JFactory::getConfig();
		$params = JComponentHelper::getParams('com_users');
		

		$subject = JText::sprintf('COM_SEMINARMAN_EMAIL_ACCOUNT_DETAILS',
			$user->name,
			$config->get('sitename')
		);
		
		if ($params->get('useractivation') == 2)
			$body = JText::sprintf('COM_SEMINARMAN_EMAIL_REGISTERED_WITH_ADMIN_ACTIVATION_BODY', 
				$user->name,
				$config->get('sitename'),
				JUri::base().'index.php?option=com_users&task=registration.activate&token='.$user->activation,
				JUri::base(),
				 $user->username, 
				 $password
			);
		else if ($params->get('useractivation') == 1)
			$body = JText::sprintf('COM_SEMINARMAN_EMAIL_REGISTERED_WITH_ACTIVATION_BODY',
				$user->name,
				$config->get('sitename'),
				JUri::base().'index.php?option=com_users&task=registration.activate&token='.$user->activation,
				JUri::base(),
				$user->username,
				$password
			);
		else
			$body = JText::sprintf('COM_SEMINARMAN_EMAIL_REGISTERED_BODY',
				$user->name,
				$config->get('sitename'),
				JUri::base()
			);
		
		$message = JFactory::getMailer();
		$message->addRecipient($user->email);
		$message->addBcc($config->get('mailfrom'));
		$message->setSubject(html_entity_decode($subject, ENT_QUOTES));
		$message->setBody($body);
		$message->setSender(array($config->get('mailfrom'), $config->get('fromname')));
		$message->IsHTML(false);
		
		return $message->send();
	}
    
    function getInvoiceNumber()
    {
    	$db = JFactory::getDBO();
    	$params = JComponentHelper::getParams('com_seminarman');

    	$db->setQuery('LOCK TABLES `#__seminarman_invoice_number` WRITE');
    	$db->query();

    	$db->setQuery('UPDATE `#__seminarman_invoice_number` SET number=GREATEST(number+1,'.(int)$params->get('invoice_number_start').')');
    	$db->query();
    	
    	$db->setQuery('SELECT number FROM `#__seminarman_invoice_number`');
    	$db->query();
    	$next = $db->loadResult();
    	
    	$db->setQuery('UNLOCK TABLES');
    	$db->query();
    	
    	return $next;
    }
}