Created
February 24, 2013 22:44
-
-
Save Haraldson/5026072 to your computer and use it in GitHub Desktop.
Bildegalleri som baserer seg på at bilder hentes fra DAM basert på tags som lagres av redaktør i CMS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once(CLASSES_ROOT.'/mediabase/api/kmcontroller.php'); | |
require_once(CLASSES_ROOT.'/mediabase/keymedia.php'); | |
require_once(CLASSES_ROOT.'/system/mediabase/kpmediabase.php'); | |
class CustomerImageGalleryGUI | |
{ | |
protected $mb; | |
protected $tpl; | |
protected $articleEntityContainer; | |
protected $articleEntity; | |
protected $cache; | |
const IMAGENEWS_CATEGORYID = 302; | |
const GALLERY_CATEGORYID = 297; | |
const ARTICLEATTRIBUTE_TAGSOPERATOR = 93; | |
const ARTICLEATTRIBUTE_MAXIMAGESINALBUM = 94; | |
const ARTICLETYPEID_ALBUM = 13; | |
public function __construct() | |
{ | |
$this->mb = new KPMediabase; | |
$this->tpl = new Smarty; | |
$this->tpl->template_dir = EXTERNAL_DOC_ROOT . '/views/'; | |
$this->tpl->compile_dir = EXTERNAL_DOC_ROOT . '/views_c/'; | |
$this->articleEntityContainer = new ArticleEntityContainer(); | |
$this->articleEntity = new ArticleEntity(); | |
$this->cache = KPCache::instance(); | |
} | |
/* | |
* album | |
* | |
* @param int $kpmid | |
* @param string $tag | |
* @param int $limit | |
* @return void | |
* | |
*/ | |
public function album($foo, $sitedesign, $tagsoperator = 'AND', $limit = 40) | |
{ | |
$rest = $this->getRest(); | |
if(array_key_exists('articleId', $rest)) | |
$articleId = $rest['articleId']; | |
if(isset($articleId)) | |
{ | |
$this->articleEntityContainer->getOne($articleId); | |
$article = $this->articleEntityContainer->nextref(); | |
} | |
if(!isset($articleId) || !isset($article)) | |
{ | |
$this->articleEntityContainer->getAllInCategoryWithTypeSortLimit(301, 'modified', 'desc', self::ARTICLETYPEID_ALBUM, 1, 0, true, true); | |
$article = unserialize($this->articleEntityContainer->nextref()); | |
$articleId = $article->getarticleId(); | |
} | |
$this->articleEntity->getOne($articleId); | |
$tagsoperatorFromArticle = $this->articleEntity->getValue(self::ARTICLEATTRIBUTE_TAGSOPERATOR); | |
if($tagsoperatorFromArticle) | |
$tagsoperator = $tagsoperatorFromArticle; | |
$limitFromArticle = $this->articleEntity->getValue(self::ARTICLEATTRIBUTE_MAXIMAGESINALBUM); | |
if($limitFromArticle) | |
$limit = $limitFromArticle; | |
$tags = $this->getArticleTags($articleId); | |
if($tags) | |
{ | |
$args = array( | |
'tag' => $tags, | |
'tagsoperator' => $tagsoperator, | |
'limit' => $limit, | |
'width' => 790, | |
'height' => 410 | |
); | |
$images = $this->fetchData($args); | |
$templateVariables = array( | |
'images' => $images, | |
'tags' => $tags, | |
'index' => 0, | |
'title' => $this->articleEntity->getHeading(), | |
'albumid' => $articleId | |
); | |
$this->printTemplate($templateVariables, 'imagegallery/album.tpl'); | |
} | |
} | |
/* | |
* albumThumbs | |
* | |
* @param int $articleId | |
* @param int $articleCollection | |
* @param string $tagsoperator | |
* @param int $offset | |
* @param int $limit | |
* @param string $title | |
* @param int imageMaxWidth | |
* @param int imageMaxHeight | |
* @return void | |
* | |
*/ | |
public function albumThumbs($articleId, $articleCollection, $tagsoperator = 'AND', $offset = 0, $limit = 1, $title = '', $w = 218, $h = 124) | |
{ | |
$tags = $this->getArticleTags($articleId); | |
if($tags) | |
{ | |
if(!is_numeric($offset) || $offset < 0) | |
$offset = 0; | |
if(!is_numeric($limit) || $limit < 1) | |
$limit = 1; | |
$args = array( | |
'tag' => $tags, | |
'tagsoperator' => $tagsoperator, | |
'limit' => $limit, | |
'offset' => $offset, | |
'width' => $w, | |
'height' => $h | |
); | |
$images = $this->fetchData($args); | |
$templateVariables = array( | |
'images' => $images, | |
'tags' => $tags, | |
'index' => $offset, | |
'title' => $title, | |
'albumid' => $articleId | |
); | |
$this->printTemplate($templateVariables, 'imagegallery/albumthumbs.tpl'); | |
} | |
} | |
/* | |
* Retrieves all images for an album as an array | |
* @param int $articleId ID of article containing links to images | |
* @param string $tagsoperator Possible values 'AND' and 'OR'. Defines how to select images based on tags | |
* *param int $limit Max number of images in gallery. Defaults to 40 | |
* @return Array of images | |
*/ | |
public function albumImages($articleId, $tagsoperator = 'AND', $limit = 40) | |
{ | |
if (!isset($articleId)) | |
return null; | |
//--Get article container | |
$this->articleEntityContainer->getOne($articleId); | |
$article = $this->articleEntityContainer->nextref(); | |
//--Get article tags | |
$tags = $this->getArticleTags($articleId); | |
//--Tag operator | |
$tagsoperatorFromArticle = $this->articleEntity->getValue(self::ARTICLEATTRIBUTE_TAGSOPERATOR); | |
if ($tagsoperatorFromArticle) | |
$tagsoperator = $tagsoperatorFromArticle; | |
//--gallery limit | |
$limitFromArticle = $this->articleEntity->getValue(self::ARTICLEATTRIBUTE_MAXIMAGESINALBUM); | |
if ($limitFromArticle) | |
$limit = $limitFromArticle; | |
if (!$tags) | |
return null; | |
$args = array( | |
'tag' => $tags, | |
'tagsoperator' => $tagsoperator, | |
'limit' => $limit, | |
'width' => 790, | |
'height' => 410 | |
); | |
$images = $this->fetchData($args); | |
return $images; | |
} | |
/* | |
* albumThumbsTwoByTwo | |
* | |
* @param int $articleId | |
* @param int $articleCollection | |
* @param string $tagsoperator | |
* @param int $offset | |
* @param int $limit | |
* @param string $title | |
* @param int imageMaxWidth | |
* @param int imageMaxHeight | |
* @return void | |
* | |
*/ | |
public function albumThumbsTwoByTwo($articleId, $articleCollection, $tagsoperator = 'AND', $offset = 0, $limit = 1, $title = '', $w = 105, $h = 60) | |
{ | |
$tags = $this->getArticleTags($articleId); | |
if($tags) | |
{ | |
if(!is_numeric($offset) || $offset < 0) | |
$offset = 0; | |
if(!is_numeric($limit) || $limit < 1) | |
$limit = 1; | |
$args = array( | |
'tag' => $tags, | |
'tagsoperator' => $tagsoperator, | |
'limit' => $limit, | |
'offset' => $offset, | |
'width' => $w, | |
'height' => $h | |
); | |
$images = $this->fetchData($args); | |
$templateVariables = array( | |
'images' => $images, | |
'tags' => $tags, | |
'index' => $offset, | |
'title' => $title, | |
'albumid' => $articleId | |
); | |
$this->printTemplate($templateVariables, 'imagegallery/albumthumbstwobytwo.tpl'); | |
} | |
} | |
/* | |
* frontAlbumThumbs | |
* | |
* @param int $articleId | |
* @param int $articleCollection | |
* @param string $tagsoperator | |
* @param int $offset | |
* @param int $limit | |
* @param string $title | |
* @param int imageMaxWidth | |
* @param int imageMaxHeight | |
* @return void | |
* | |
*/ | |
public function frontAlbumThumbs($articleId, $articleCollection, $tagsoperator = 'AND', $offset = 0, $limit = 1, $title = '', $w = 176, $h = 104) | |
{ | |
$tags = $this->getArticleTags($articleId); | |
if($tags) | |
{ | |
if(!is_numeric($offset) || $offset < 0) | |
$offset = 0; | |
if(!is_numeric($limit) || $limit < 1) | |
$limit = 1; | |
$args = array( | |
'tag' => $tags, | |
'tagsoperator' => $tagsoperator, | |
'limit' => $limit, | |
'offset' => $offset, | |
'width' => $w, | |
'height' => $h | |
); | |
$images = $this->fetchData($args); | |
$templateVariables = array( | |
'images' => $images, | |
'tags' => $tags, | |
'index' => $offset, | |
'title' => $title, | |
'albumid' => $articleId | |
); | |
$this->printTemplate($templateVariables, 'imagegallery/albumthumbs.tpl'); | |
} | |
} | |
/* | |
* fetchData | |
* | |
* @param array $args | |
* @return array of simplified image data | |
* | |
*/ | |
private function fetchData($args, $method = 'tag') | |
{ | |
$cachename = 'CustomerImages_' . serialize($args); | |
$images = $this->cache->get($cachename); | |
if(!$images) | |
{ | |
$res = $this->mb->getRequestUrl('customermedia.keydev.no', $method, $args); | |
$content = file_get_contents($res); | |
$rawImageData = json_decode($content, true); | |
$images = $this->simplifyData($rawImageData); | |
$this->cache->set($cachename, $images, 300); | |
} | |
return $images; | |
} | |
/* | |
* simplifyData | |
* | |
* @param array $raw: Raw image data | |
* @return array of simplified image data | |
* | |
*/ | |
private function simplifyData($raw) | |
{ | |
$images = array(); | |
foreach($raw as $i=>$image) | |
{ | |
$scaledImages = array(); | |
$imageDimensions = array(); | |
foreach($image['images'] as $scaledImage) | |
{ | |
$imageInfo = getimagesize($scaledImage['url']); | |
$imageDimensions['w'] = $imageInfo[0]; | |
$imageDimensions['h'] = $imageInfo[1]; | |
$imageDimensions['bbw'] = (int)$scaledImage['w']; | |
$imageDimensions['bbh'] = (int)$scaledImage['h']; | |
$imageBoundingBoxSizeLabel = $scaledImage['w'] . 'x' . $scaledImage['h']; | |
$scaledImages[$imageBoundingBoxSizeLabel] = $scaledImage['url']; | |
} | |
$title = array_shift($image['attributes']); | |
$title = $title['value']; | |
$description = array_shift($image['attributes']); | |
$description = $description['value']; | |
$images[$i] = array( | |
'id' => $image['id'], | |
'tags' => $image['tags'], | |
'attr' => array( | |
'title' => $title, | |
'description' => $description | |
), | |
'url' => $scaledImages, | |
'dims' => $imageDimensions | |
); | |
} | |
return $images; | |
} | |
/* | |
* printTemplate | |
* | |
* @param array $templateVariables: Variables passed to the template | |
* @return void | |
* | |
*/ | |
private function printTemplate($templateVariables, $templatepath) | |
{ | |
$reqindex = KQHttp::fromUser('image', 0); | |
$cachename = 'CustomerImageTemplate_' . serialize($templateVariables) . $templatepath . $reqindex; | |
$template = $this->cache->get($cachename); | |
if(!$template) | |
{ | |
$tpl = $this->tpl; | |
$query = array(); | |
if(strlen(KQHttp::fromUser('tab')) > 0) | |
$query['tab'] = KQHttp::fromUser('tab'); | |
if(strlen(KQHttp::fromUser('page')) > 0) | |
$query['page'] = KQHttp::fromUser('page'); | |
$query = '?' . http_build_query($query, '&'); | |
if(strlen($query) > 1) | |
$tpl->assign('query', $query); | |
if(array_key_exists('albumid', $templateVariables)) | |
$tpl->assign('albumid', urlencode($templateVariables['albumid'])); | |
if(array_key_exists('title', $templateVariables)) | |
$tpl->assign('title', $templateVariables['title']); | |
$tpl->assign('images', $templateVariables['images']); | |
$tpl->assign('baseurl', URLBuilder::getCategoryUrl(self::GALLERY_CATEGORYID)); | |
$tpl->assign('reqindex', $reqindex); | |
$template = $tpl->fetch($templatepath); | |
$this->cache->set($cachename, $template, 300); | |
} | |
echo $template; | |
} | |
/* | |
* getRest | |
* | |
* @param void | |
* @return rest parameters in array with named keys | |
* | |
*/ | |
private function getRest() | |
{ | |
UrlController::interpretUrl(false, true, $notFoundDepth, $exclusiveMatch, $externalMethod, $restparams, false); | |
$rest = array(); | |
if(strlen($restparams[0]) > 0) | |
$rest['articleId'] = $restparams[0]; | |
return $rest; | |
} | |
/* | |
* getArticleTags | |
* | |
* @param $articleId | |
* @return tags in array | |
* | |
*/ | |
private function getArticleTags($articleId = 0) | |
{ | |
if(!is_numeric($articleId) || !$articleId > 0) return; | |
$cachename = 'CustomerArticleTags_' . $articleId; | |
$articleTags = $this->cache->get($cachename); | |
if(!$articleTags) | |
{ | |
$articleEntitySubjectContainer = new ArticleEntitySubjectContainer(); | |
$articleEntitySubjectContainer->getAllByArticleId($articleId); | |
$articleTags = array(); | |
while($obj = $articleEntitySubjectContainer->nextref()) | |
{ | |
$articleTags[] = strtolower($obj->getSubject()); | |
} | |
$this->cache->set($cachename, $articleTags, 600); | |
} | |
return $articleTags; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment