<?php
/**
* @Enterprise: S&S Media Solutions
* @author: Yannick Spang
* @creation date: July 2009
* @url: http://www.yagendoo.com
* @copyright: Copyright (C) 2009 S&S Media Solutions
* @license: Commercial, see LICENSE.php
* @product: Yagendoo Banner Adspot Pro - Joomla! Module
* @version: 1.0
*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class modyagendoobanneradspotproHelper {
	function expiredmail($module, $params) {
    	$datenow					= JFactory::getDate()->toFormat('%Y-%m-%d');
    	$from 						= $params->get('adminmail');
    	$fromname					= $params->get('adminname');
    	$advertiserssubject			= $params->get('advertiserssubject');
    	$advertisersmail			= $params->get('advertisersmail');
    	
    	
    	   	
    	$i = 1;
    		while($i <= 52) {
    			$expiredate 				= $params->get('date'.$i);
    			$bannerstatus				= $params->get('banner'.$i);
    			$expiredmailstatus 			= $params->get('expiredmail'.$i);
    			$advertisersadresse			= $params->get('advertisersadresse'.$i);
    			$advertisersname			= $params->get('advertisersname'.$i);
    			$targetlink 		    	= $params->get('link'.$i);
    			$targettitle 		    	= $params->get('title'.$i);
    	
    				if (((($expiredate <= $datenow) && ($expiredate != '0') && ($bannerstatus == '1') && ($expiredmailstatus == '1') && ($advertisersadresse != '')))) :
						$recipient = $advertisersadresse; 
						$subject = $advertiserssubject; 
						$body = JText::_('HELLO').' '.$advertisersname.',<br/><br/>'.JText::_('ADVERTISERS_TEXT').' '.$targetlink.' '.JText::_('ADVERTISERS_TEXT1').' "'.$targettitle.'" '.JText::_('ADVERTISERS_TEXT2').'<br/><br/>'.$advertisersmail.'<br/><br/>'.JText::_('ADSPOTMAIL_FAREWELL').',<br/>'.$fromname;
						$mode=1; 
			
						JUtility::sendMail($from, $fromname, $recipient, $subject, $body, $mode);	
			
						$recipient = $from; 
						$subject = JText::_('ADSPOTMAIL_ADMINSUBJECT'); 
						$body = JText::_('HELLO').' '.$fromname.',<br/><br/>'.JText::_('ADMIN_TEXT').' '.$i.', '.JText::_('ADMIN_TEXT1').' '.$targetlink.' '.JText::_('ADMIN_TEXT2').' "'.$targettitle.'" '.JText::_('ADMIN_TEXT3').'<br/><br/>'.JText::_('ADSPOTMAIL_FAREWELL').',<br/>'.JText::_('ADMIN_TEXT4'); 
						$mode=1;
																		
						JUtility::sendMail($from,$fromname,$recipient,$subject,$body,$mode);
												
						$params->set('expiredmail'.$i, 3);

					endif;
 			$i++;
 		}

	$module->params = $params->toString();
	$module = JArrayHelper::fromObject($module);
	$table = JTable::getInstance('module');
		if (!$table->save($module)) {
    		echo 'eaa';
		}
		
    }

	function preexpiredmail($module, $params) {    	
    	$from 						= $params->get('adminmail');
    	$fromname					= $params->get('adminname');
    	$advertiserssubject			= $params->get('advertiserssubjectpremail');
    	$advertisersmail			= $params->get('advertiserspremail');  
    	$datenow					= JFactory::getDate()->toFormat('%Y-%m-%d'); 	
    	   	
    	$i = 1;
    		while($i <= 52) {
    		$expiredate 				= $params->get('date'.$i);
    		 if($expiredate != '0') :
    			$bannerstatus				= $params->get('banner'.$i);
    			$preexpiredmailstatus 		= $params->get('preexpiredmail'.$i);
    			$advertisersadresse			= $params->get('advertisersadresse'.$i);
    			$advertisersname			= $params->get('advertisersname'.$i);
    			$targetlink 		    	= $params->get('link'.$i);
    			$targettitle 		    	= $params->get('title'.$i);
     			$premailtime = $params->get('preexpiredmailtime'.$i, '')*24;
				$expidate = $params->get('date'.$i);
				$newexpidate = JFactory::getDate($expidate)->toUnix();
				$sevendate = JFactory::getDate($newexpidate, +$premailtime)->toFormat('%Y-%m-%d');
    		  endif;
    	
    				if (((($expiredate != '0') && ($datenow == $sevendate) && ($bannerstatus == '1') && ($preexpiredmailstatus == '1') && ($advertisersadresse != '')))) :
						$recipient = $advertisersadresse; 
						$subject = $advertiserssubject; 
						$body = JText::_('HELLO').' '.$advertisersname.',<br/><br/>'.JText::_('PREADVERTISERS_TEXT').' '.$targetlink.' '.JText::_('PREADVERTISERS_TEXT1').' "'.$targettitle.'" '.JText::_('PREADVERTISERS_TEXT2').'<br/><br/>'.$advertisersmail.'<br/><br/>'.JText::_('ADSPOTMAIL_FAREWELL').',<br/>'.$fromname;
						$mode=1; 
			
						JUtility::sendMail($from, $fromname, $recipient, $subject, $body, $mode);	
			
						$recipient = $from; 
						$subject = JText::_('PREADSPOTMAIL_ADMINSUBJECT'); 
						$body = JText::_('HELLO').' '.$fromname.',<br/><br/>'.JText::_('PREADMIN_TEXT').' '.$i.' '.JText::_('PREADMIN_TEXT1').' '.$targetlink.' '.JText::_('PREADMIN_TEXT2').' "'.$targettitle.'" '.JText::_('PREADMIN_TEXT3').'<br/><br/>'.JText::_('ADSPOTMAIL_FAREWELL').',<br/>'.JText::_('PREADMIN_TEXT4'); 
						$mode=1;
																		
						JUtility::sendMail($from,$fromname,$recipient,$subject,$body,$mode);
						if ($advertisersadresse != '') {							
							$params->set('preexpiredmail'.$i, 3);
						}
					endif;
 			$i++;
 		}

	$module->params = $params->toString();
	$module = JArrayHelper::fromObject($module);
	$table = JTable::getInstance('module');
		if (!$table->save($module)) {
    		echo 'eaa';
		}
		
    }

}
?>