<?php
namespace ALPCAL\Remote;
/**
 * Aoa_xml_aoaevents Remote fetch class
 
 Events must return from returnEvents() with minimum:
 EvID (guid for the event)
 EvTi (Title for the event to show on the web calendar)
 StDT (YYYY-MM-DD format)
 EnDT (YYYY-MM-DD format)
 
 Model output functions:
 modelBody()
 modelFooter()
 
 */

Class Aoa_xml_aoaevents extends \ALPCAL\Models\Remote implements \ALPCAL\Interfaces\Remote
{
	public $buildUrl = "https://registration.alpineontario.ca/xml/aoaevents/?date_start=[date_start]&date_end=[date_end]";
	public $uniqueId = "aoaxmlevent"; // gets appended to all EvID for these events to differentiate them from other calendars
	// https://registration.alpineontario.ca/xml/aoaevents/?date_start=2017-11-04&date_end=2017-11-05

	function __construct($alpcal_options)
	{
		$this->alpcal_options = $alpcal_options;
		$this->renderTime = time();
  	}
	
	/**
	 * Returns array of events in specified format
	 */
	public function returnEvents()
	{
		// build the URL
		if (!$this->stDt) { throw new \Exception("stDt not set in Remote\RegistrationAlpineontarioCa_xml_aoaevents"); }
		if (!$this->enDt) { throw new \Exception("enDt not set in Remote\RegistrationAlpineontarioCa_xml_aoaevents"); }
		$this->buildUrl = str_replace("[date_start]",$this->stDt->format('Y-m-d'),$this->buildUrl);
		$this->buildUrl = str_replace("[date_end]",$this->enDt->format('Y-m-d'),$this->buildUrl);
		
		try {
			// fetch via curl
			$GetRemoteFile = new \ALPCAL\Util\GetRemoteFile;
			$GetRemoteFile->set_url($this->buildUrl);
			$getEvents = $GetRemoteFile->get('curl');
			}
		catch(\Exception $e)
			{
			if ($e->getCode() == 22)
				{
				// there was an error thrown receiving
				die("Error: failed to retrieve file - ".$e->getMessage());
				}
			throw new \Exception($e->getMessage());
			}
		$xml = simplexml_load_string($getEvents);
		$eventsDecoded = array();
		foreach ($xml->calendar->event as $k=>$v)
		{
			$v->EvID = $this->uniqueId."_".$v->EventCode;
			$v->EvTi = $v->EventDesc;
			$v->StDT = \DateTime::createFromFormat('D, d M Y H:i:s e', $v->EventStart)->format('Y-m-d'); // normalize the StDT
			$v->EnDT = \DateTime::createFromFormat('D, d M Y H:i:s e', $v->EventEnd)->format('Y-m-d'); // normalize the EnDT
			$eventsDecoded[] = (object)(array) $v; // convert to stdClass  object
		}
//		echo "<pre>";
//		print_r($eventsDecoded);
//		echo "</pre> XML OUTPTTTED";
//		die();
		
		return $eventsDecoded;
		
	}
	
	/**
	 * Outputs the Body for model when clicked on
	 */
	public function modelBody($ev)
	{
/*
		stdClass Object
(
    [EventCode] => LDFSNOV4-17
    [EventDesc] => NCCP Module - Leading Drug-Free Sport Nov 5
    [EventComment] => <strong>Leading Drug-Free Sport</strong>&nbsp;NCCP Module will be offered Sunday November 5, 2017 during the AOA Education Summit at the Toronto Pan AM Centre from 1pm-4:30pm.
    [RegistrationStart] => Mon, 18 Sep 2017 00:00:00 GMT
    [RegistrationEnd] => Mon, 06 Nov 2017 12:00:00 GMT
    [EventStart] => Fri, 03 Nov 2017 13:00:00 GMT
    [EventEnd] => Mon, 06 Nov 2017 17:00:00 GMT
    [CapacityMax] => 24
    [AgeStart] => 15
    [AgeEnd] => 0
    [Gender] => Any
    [Price] => $0.00
    [EmailContact] => admin@alpineontario.ca
    [ProgramLink] => https://registration.alpineontario.ca/Default.aspx?productid=291
    [EvID] => aoaxmlevent_LDFSNOV4-17
    [EvTi] => NCCP Module - Leading Drug-Free Sport Nov 5
    [StDT] => 2017-11-03
    [EnDT] => 2017-11-06
    [calPlugin] => Array
        (
            [calOrd] => 1
            [ord] => 4
            [whichCal] => Aoa_xml_aoaevents
        )

)

KIT WANTED per email Mon 10/16/2017 12:31 PM


*/
		?>
		<div class="row">
			<div class="col-sm-12">
				<div class="table-responsive">
					<table class="table table-striped">
						<tbody>
							<tr>
								<td><strong>Event Date:</strong></td>
								<td>
									<?
									echo $ev->EventStart;
									?> - <?
									echo $ev->EventEnd;
									?>
								</td>
							</tr>							
							<tr>
								<td><strong>Event Description:</strong></td>
								<td>
									<?
									echo $ev->EventDesc;
									?>
								</td>
							</tr>
							<tr>
								<td><strong>Registration Start:</strong></td>
								<td>
									<?
									echo $ev->RegistrationStart;
									?>
								</td>
							</tr>
							<tr>
								<td><strong>Registration End:</strong></td>
								<td>
									<?
									echo $ev->RegistrationEnd;
									?>
								</td>
							</tr>
							<tr>
								<td><strong>Capacity (Max):</strong></td>
								<td>
									<?
									echo $ev->CapacityMax;
									?>
								</td>
							</tr>
							<?php
							if ($ev->AgeStart || $ev->AgeEnd)
							{
							?>
							<tr>
								<td><strong>Age:</strong></td>
								<td>
									<?
									echo $ev->AgeStart;
									echo ($ev->AgeEnd>0 ? ' to '.$ev->AgeEnd : '+' );
									?>
								</td>
							</tr>
							<?php
							} // if age
							?>
							<tr>
								<td><strong>Gender:</strong></td>
								<td>
									<?
									echo $ev->Gender;
									?>
								</td>
							</tr>
							<tr>
								<td><strong>Price:</strong></td>
								<td>
									<?
									echo $ev->Price;
									?>
								</td>
							</tr>
							<tr>
								<td><strong>Email Contact:</strong></td>
								<td>
									<?
									echo '<a href="mailto:'.$ev->EmailContact.'">'.$ev->EmailContact.'</a>';
									?>
								</td>
							</tr>
							
							
							
							
						</tbody>
					</table>
				</div>		
			</div>
		</div>
		<?
		
	}
	
	/**
	 * Outputs the Footer for model when clicked on
	 */
	public function modelFooter($ev)
	{
		?>
		<a href="<?=$ev->ProgramLink?>" target="_blank" class="btn btn-success">
			Event Registration
		</a>
		<?
	}
	
	/**
	 * Returns array of categories in specified format
	 */
	public function returnCategories($inSpecifiedDateRange=false)
	{
		
	}
	
}
