Skip to content

Instantly share code, notes, and snippets.

@bigin
Last active July 11, 2017 19:10
Show Gist options
  • Save bigin/0b0a045dd78255fb1e541d92c6ecd5e2 to your computer and use it in GitHub Desktop.
Save bigin/0b0a045dd78255fb1e541d92c6ecd5e2 to your computer and use it in GitHub Desktop.
<?php
/**
*** Enter here the ID of the products category ***
*
*/
$category_id = 3;
/**
* URL segments array after catalog www.my-site.com/catalog/(segment-1/segment-2/etc...)
*/
$urlSegments = getUrlSegments();
/**
* Shorten config
*/
$config = $catalog->processor->config;
/**
* GS page slug
*/
$slug = get_page_slug(false);
/**
* Declare some vars
*
* @route - Current catalog route
* @items - Item array (List view)
* @currentItem - Current item object (Item details view)
* @category - Current category object
* @msg - Error/Notice message
*/
$route = array();
$items = array();
$currentItem = null;
$category = null;
$msg = null;
/**
* Get the catalog route
*/
if($catalog->router->isRouteExists($urlSegments[0])) {
$route = $catalog->router->getRoute($urlSegments[0]);
}
/**
* Get the current item object
*/
if(!empty($route['id'])) {
$currentItem = $catalog->processor->getItem($route['categoryid'], $route['id']);
}
/**
* Check if the categy exists, select it
*/
$category = $catalog->processor->getItem(
$config->dummyCategoryId, $category_id
);
// Item details page data
if(isset($currentItem->id))
{
// Create thumbnail if it does not exist yet
$currentItem->thumbnail = \ImCatalog\Util::getResizedUrl($currentItem, 0, 700, 0, 'resize');
// Build our tags output
$tags = array_map('trim', explode(',', $currentItem->tags));
// Override the default tags variable
$currentItem->tags = '';
foreach($tags as $tag) {
$currentItem->tags .= ($tag) ?
'<span class="tag"><a href="'.$config->startPageUri.'tags/'.strtolower($tag).'/">'.$tag.'</a></span>' : '';
}
// Item list page data
} elseif($slug == $config->catalogStartPage)
{
// Intercept the tags input
$tag_selector = '';
$pager_prefix = '';
if($urlSegments[0] == 'tags' && isset($urlSegments[1]))
{
$tag_selector = '&&tags=%' . $catalog->imanager->sanitizer->text($urlSegments[1]) . '%';
$pager_prefix = $urlSegments[0] . '/' . $urlSegments[1] . '/';
}
if(!empty($category->id))
{
$params = array(
'section' => 'frontend',
'selector' => 'active=1'.$tag_selector,
'pageurl' => $config->startPageUri.$pager_prefix.'page',
'perpage' => 3,
'tagurl' => (isset($_GET['tags']) ? $catalog->imanager->sanitizer->text($_GET['tags']) : '')
);
$result = $catalog->processor->getItems($category->id, $params);
} else
{
$msg = '<div class="alert alert-danger">' .
'<strong>Attention!</strong> The category ID "'.(int)$category_id.'" you entered does not exist yet.' .
'</div>';
}
$pagination = '';
if(!empty($result['items']))
{
$items = itemsPrepare($result['items']);
$pagination = ($result['pagination']) ?
'<div class="container bts-24 pull-left">'.$result['pagination'].'</div>' : '';
}
}
/**
* Prepare the SimpleItem objects for output
*
* @param array $items - An array with SimpleItem objects
*
* @return array - Prepared array with SimpleItem objects
*/
function itemsPrepare($items = array())
{
global $catalog;
foreach($items as $item)
{
// Create thumbnail if it does not exist yet
$item->thumbnail = \ImCatalog\Util::getResizedUrl($item, 0, 0, 360, 'resize');
// Build our tags output
$tags = array_map('trim', explode(',', $item->tags));
// Override the default tags variable
$item->tags = '';
foreach($tags as $tag) {
$item->tags .= ($tag) ?
'<span class="tag"><a href="'.
$catalog->processor->config->startPageUri.'tags/'.strtolower($tag).'/">'.$tag.'</a></span>' : '';
}
}
return $items;
}
/**
* It's an URL segmentation function
*
* Separates a segment part of the URL: "www.my-site.com/catalog/(segment)" by slashes, for instance:
* "www.my-site.com/catalog/tags/clothing/" becomes "array(0 => 'tags', 1 => 'clothing')"
*
*
* @return array|null - URL segments
*
*/
function getUrlSegments()
{
if(!isset($_GET['seg'])) return null;
global $catalog;
return explode('/', $catalog->imanager->sanitizer->text(trim($_GET['seg'], '/')));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment