File: D:/HostingSpaces/SBogers10/keystud.komma-mediadesign.nl/wwwroot/app/models/m_horse.class.php
<?php
/**
* m_horses.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 7/31/13
*/
class Horse_Model extends Model
{
// Data
private $_info;
public function __construct()
{
parent::__construct();
}
public function set($info)
{
if( ! empty($info) && is_array($info))
{
$this->_info = $info;
}
}
/*
* Get Horse information from the database
*/
public function get($input=null,$value = null)
{
$data = $id = $year = null;
if($input!=null && is_numeric($input)) $id = $input;
if($input!=null && is_array($input)) $data = $input;
if($input!=null && is_string($input)) $year = true;
$Db = new DatabaseHandler();
$Db->setTableName(TABLE_PREFIX . $this->_info['name'] . '_items');
$Db->setActiveOnly(TRUE,$this->_info['page_id']);
if($data != null) $Db->setData($data);
$Db->addRule('active',1);
$Db->setOrder('itemOrder','DESC');
if($id != null)
{
$Db->addRule('id',$id);
}
else if($year)
{
if(defined('URL_SUB') && empty($value))
{
$Db->addRule('birthyear',URL_SUB);
}
else
{
$Db->addRule('birthyear',$value);
}
}
if($result = $Db->select())
{
return $result;
}
return false;
}
public function getImages($id)
{
$Db = new DatabaseHandler();
$Db->setTableName(TABLE_PREFIX . $this->_info['name'] . '_images');
if($id != null)
{
$Db->addRule('itemId',$id);
}
$images = $Db->select();
return $Db->twoDimensional($images);
}
/*
* Get Videos from the database
*/
public function getVideos($id)
{
$Db = new DatabaseHandler();
$Db->setData(array('youtube_id'=>''));
$Db->setTableName(TABLE_PREFIX . $this->_info['name'] . '_videos');
{
$Db->addRule('itemId',$id);
}
if($videos = $Db->select())
{
return $Db->twoDimensional($videos);
}
return false;
}
/*
* Get ID by a title in the url
*/
function getId($urlTitle)
{
$id = false;
$horses = $this->get(array('id'=>'','title'=>''));
$horses = $this->Db->twoDimensional($horses);
foreach($horses as $horse)
{
if($this->Functions->encodeUrl($horse['title']) == $urlTitle)
{
$id = $horse['id'];
}
}
return $id;
}
/*
CREATE OUTPUT
*/
/*
* Create a dashboard on a flexible grid
*/
public function createThumbs($horses=null)
{
// Get all the horses
if($horses == null)
{
$horses = $this->get('year');
}
$horses = $this->Db->twoDimensional($horses);
if(empty($horses) || empty($horses[key($horses)]))
{
echo 'ok';
$F = new Functions();
$F->notFound();
}
// Build the output
$output = '';
foreach($horses as $key => $horse)
{
// Display origin
$temp = explode(',',$horse['origin']);
$origin = '';
if(is_array($temp) && count($temp) >= 3 )
{
$origin = $temp[0] . ' × ' . $temp[1] . ' × ' . $temp[2];
}
// Get Images
$images = $this->getImages($horse['id']);
// Create Link
$url = LANG_ROOT . URL_PAGE . '/' . $this->Functions->encodeUrl($horse['title']);
isset($horse['thumb']) ? $thumbKey = count($images) - $horse['thumb'] -1 : $thumbKey = 0;
$src = UPLOADS_ROOT . $images[$thumbKey]['thumb'];
if($key == 0) $src = UPLOADS_ROOT . $images[$thumbKey]['filename'];
$output .= '<li data-eq="' . $key . '"';
if($key==0) $output .= ' class="first"';
$output .= '>
<span class="hover_text">
<span class="balance valign"></span>
<span class="inner valign">
<span class="gender sprite ' . $horse['gender'] . '"></span>
<a href="' . $url . '"><span class="name">'. $horse['title'] . '</span></a>
<span class="pedigree">'. $origin . '</span>
<span class="read_more">' . $this->_info['lang']['read_more'] .' <span>›</span></span>
</span>
</span>
<span class="hover_bg"></span>
<img src="' . $src . '" data-small="' . UPLOADS_ROOT . $images[$thumbKey]['thumb'] . '" data-large="' . UPLOADS_ROOT . $images[$thumbKey]['filename'] . '" alt="horse" class="resp"/>
</li>';
}
return $output;
}
/*
* Create the years list aside
*/
public function createYears()
{
// Get all years
$temp = $this->get(array('birthyear'=>''));
$temp = $this->Db->twoDimensional($temp);
$years = array();
foreach($temp as $data)
{
$years[] = $data['birthyear'];
}
arsort($years);
// Create output
$output = '';
foreach($years as $year)
{
$url = LANG_ROOT . URL_PAGE . '/' . $year;
$output .= '<li';
if(defined('URL_SUB') && URL_SUB == $year ) $output .= ' class="active"';
$output .= '><a href="' . $url . '">' . $year .'</a></li>';
}
return $output;
}
/*
* Create the horse list aside
*/
public function createList($horseId = null,$allYears = false)
{
if($horseId == null)
{
// Get all the horses
if( ! $allYears)
{
$horses = $this->get('year');
}
else
{
$horses = $this->get();
}
$horses = $this->Db->twoDimensional($horses);
}
else
{
// Get Year of current Horse
$thisHorse = $this->get($horseId);
$year = $thisHorse['birthyear'];
$this->_info['name'] == 'references' ? $horses = $this->get() : $horses = $this->get('year',$year);
$horses = $this->Db->twoDimensional($horses);
}
// Create output
$output = '';
foreach($horses as $horse)
{
// Create link
$url = LANG_ROOT . URL_PAGE . '/' . $this->Functions->encodeUrl($horse['title']);
if($this->_info['name'] != 'mares')
{
$max = 32;
$maxTitle = 30;
}
else
{
$max = 55;
$maxTitle = 50;
}
if(strlen($horse['origin']) > $max) $horse['origin'] = substr($horse['origin'],0,$max - 3) . '…';
if(strlen($horse['title']) > $maxTitle) $horse['title'] = substr($horse['title'],0,$maxTitle - 3) . '…';
// Display origin
$temp = explode(',',$horse['origin']);
if(is_array($temp))
{
$origin = '';
foreach($temp as $t => $n)
{
if($t != 0) $origin .= ' × ';
$origin .= $n;
}
}
else
{
// Try with dot
$temp = explode('.',$horse['origin']);
$origin = '';
if(is_array($temp) )
{
foreach($temp as $t => $n)
{
if($t != 0) $origin .= ' × ';
$origin .= $n;
}
}
}
$output .= '<li';
if( $horseId != null && $horseId == $horse['id']) $output .= ' class="active"';
$output .= '>
<span class="shadow sprite"></span>
<a href="' . $url . '"><span class="name">' . $horse['title'] . '</span></a>
<span class="pedigree">' . $origin . '</span>
<span class="line"></span>
</li>';
}
return $output;
}
/*
CREATE OUTPUT DETAIL
*/
public function createInfo($id)
{
$info = false;
// Get horse information
if($horse = $this->get($id))
{
$info = array();
// Create info output
$info['title_bar'] = '<li>' . $this->_info['lang']['meet'] . '</li>
<li class="name">' . $horse['title'] . '</li>
<li><span class="icon sprite ' . $horse['gender'] . '"></span></li>';
// Horse Name
$info['name'] = $horse['title'];
$info['birthyear'] = $horse['birthyear'];
$info['sold'] = $horse['sold'];
$info['tribe_number'] = $horse['tribe_number'];
// Horse Description
URL_LANG == 'nl' ? $info['description'] = $horse['description'] : $info['description'] = $horse['description_en'];
if(empty($info['description'])) $info['description'] = $horse['description'];
// Horse Details
$info['details_table'] = '';
$details = array('birthday','tribe_number','origin','appearance','height','jump_height','age','achievements','country');
$en = array('appearance','height','achievements');
foreach($details as $detail)
{
if(isset($horse[$detail]))
{
if($detail == 'origin')
{
// Display origin
$temp = explode(',',$horse['origin']);
if(is_array($temp) && count($temp) >= 3 )
{
$horse['origin'] = trim($temp[0]) . ' × ' . trim($temp[1]) . ' × ' . trim($temp[2]);
}
else
{
// Try with dot
$temp = explode('.',$horse['origin']);
if(is_array($temp) && count($temp) >= 3 )
{
$horse['origin'] = $temp[0] . ' × ' . $temp[1] . ' × ' . $temp[2];
}
}
}
$extra = '';
if($detail == 'country')
{
$c = $horse[$detail];
if(is_file(DOCUMENT_IMAGE_ROOT . 'static/flags/' . $c . '.png'))
{
$extra = '<img src="' . IMAGE_ROOT . 'static/flags/' . $c . '.png" alt="' . $c . '" />';
}
$horse[$detail] = str_replace('_',' ',$horse[$detail]);
}
// En / Nl
if(in_array($detail,$en))
{
$temp = $horse[$detail];
if(URL_LANG == 'en') $horse[$detail] = $horse[$detail . '_en'];
if(empty($horse[$detail])) $horse[$detail] = $temp;
}
$info['details_table'] .= '<tr>
<td>' . $this->_info['lang'][$detail] . '</td>
<td>' . $horse[$detail] . $extra . '</td>
</tr>';
}
}
// Buttons
$info['buttons'] = '';
if( ! empty($horse['origin_extern']) )
{
if(substr($horse['origin_extern'],0,4) != 'http') $horse['origin_extern'] = 'http://' . $horse['origin_extern'];
$info['buttons'] .= '<li><a class="btn fade" target="_blank" href="' . $horse['origin_extern'] . '">' . $this->_info['lang']['origin'] . '<span class="arrow sprite"></span></a></li>';
}
if( ! empty($horse['damline_extern']) )
{
if(substr($horse['damline_extern'],0,4) != 'http') $horse['damline_extern'] = 'http://' . $horse['damline_extern'];
$info['buttons'] .= '<li><a class="btn fade" target="_blank" href="'. $horse['damline_extern']. '">' . $this->_info['lang']['damline'] . '<span class="arrow sprite"></span></span><span class="arrow sprite"></a></li>';
}
// IMAGES
if($images = $this->getImages($id))
{
$imageData = array();
$info['index_nav'] = $info['image_list'] = '';
foreach($images as $key => $image)
{
$info['index_nav'] .= '<li';
if($key == 0) $info['index_nav'] .= ' class="active"';
$info['index_nav'] .= '>'. ($key + 1) . '</li>';
$info['image_list'] .= ' <li><img src="' . IMAGE_ROOT . 'structure/spacer.gif" data-src="' . UPLOADS_ROOT . $image['filename'] . '" alt="horse" class="resp"/></li>';
}
if( ! isset($horse['thumb'])) $horse['thumb'] = 0;
$info['thumb'] = UPLOADS_ROOT . $images[count($images) - $horse['thumb'] -1]['filename'];
}
// Videos
if($videos = $this->getVideos($id))
{
$info['video_list'] = $info['video_nav'] = '';
foreach($videos as $key => $video)
{
$ytId = $video['youtube_id'];
$params = array();
//$params['modestbranding'] = 1; // hide youtube logo
$params['showinfo'] = 0; // do not show info
$params['autohide'] = 1; // automatically hide controls
$params['rel'] = 0; // do not show related videos when video ends
$params['theme'] = 'light'; // player skin
$params['color'] = 'white'; // progress bar color
$params['iv_load_policy'] = 3; // video annotations not to be shown by default
$info['video_list'] .= '<li><iframe width="100%" height="100%" src="http://www.youtube.com/embed/' . $ytId;
$info['video_list'] .= '?';
$i = 0;
foreach($params as $param => $value)
{
if($i>0) $info['video_list'] .= '&';
$info['video_list'] .= $param . '=' . $value;
$i++;
}
$info['video_list'] .= '" frameborder="0" allowfullscreen></iframe></li>';
$info['video_nav'] .= '<li';
if($key == 0) $info['video_nav'] .= ' class="active"';
$info['video_nav'] .= '>'. ($key + 1) . '</li>';
}
}
// References
$allHorses = $this->get();
$info['related'] = '';
$count = 0;
foreach($allHorses as $related)
{
$isRelated = false;
if($this->_info['name'] == 'mares')
{
if($related['tribe_number'] == $horse['tribe_number'] && $related['id'] != $horse['id'])
{
$isRelated = true;
}
}
else
{
if($related['birthyear'] == $horse['birthyear'] && $related['id'] != $horse['id'])
{
$isRelated = true;
}
}
if($isRelated && $count < 4)
{
$relatedImages = $this->getImages($related['id']);
$origin = '';
$temp = explode(',',$related['origin']);
if(is_array($temp) && count($temp) >= 3 )
{
$origin = $temp[0] . ' × ' . $temp[1] . ' × ' . $temp[2];
}
// Create link
$url = LANG_ROOT . URL_PAGE . '/' . $this->Functions->encodeUrl($related['title']);
$info['related'] .= '<li>
<span class="image"><img src="' . UPLOADS_ROOT . $relatedImages[0]['filename'] . '" alt="'. $related['title'] .'" class="resp"/><span class="arrow sprite"></span></span>
<a class="name" href="' . $url. '">' . $related['title'] . '</a>
<span class="pedigree">' . $origin . '</span>
</li>';
$count++;
}
}
}
return $info;
}
}