public static function prepareDocument($document, $params, $category = false, $item = false, $header = '')
{
$app = Factory::getApplication();
$menus = $app->getMenu();
$menu = $menus->getActive();
$pathway = $app->getPathway();
$title = null;
$metakey = $params->get('cart_metakey', '');
$metadesc = $params->get('cart_metadesc', '');
$render_canonical_url = $params->get('render_canonical_url', 0);
$nameInTitle = 1;
// TO DO possible parameter Category or Title name
$viewCurrent = $app->input->get('view');
$viewLink = '';
if (isset($menu->query['view'])) {
$viewLink = $menu->query['view'];
}
$type = '';
$name = array();
if (!empty($item) && isset($item->title)) {
$name = $item;
$type = 'item';
} else {
if (!empty($category) && isset($category->title)) {
$name = $category;
$type = 'category';
}
}
if ($menu) {
$params->def('page_heading', $params->get('page_title', $menu->title));
} else {
$params->def('page_heading', Text::_('JGLOBAL_ARTICLES'));
}
$title = $params->get('page_title', '');
if (isset($name->title) && $name->title != '') {
/*if ($title != '') {
$title = $title .' - ' . $name->title;
} else {
$title = $name->title;
}*/
$title = $name->title;
}
if ($viewLink != $viewCurrent && $header != '') {
$title = $header;
} else {
if (empty($title)) {
$title = htmlspecialchars_decode($app->get('sitename'));
} else {
if ($app->get('sitename_pagetitles', 0) == 1) {
$titleInclSiteName = Text::sprintf('JPAGETITLE', htmlspecialchars_decode($app->get('sitename')), $title);
if ($nameInTitle == 1 && isset($name->title) && $name->title != '' && $title != $name->title) {
$title = $titleInclSiteName . ' - ' . $name->title;
} else {
$title = $titleInclSiteName;
}
} else {
if ($app->get('sitename_pagetitles', 0) == 2) {
if ($nameInTitle == 1 && isset($name->title) && $name->title != '') {
if ($title != $name->title) {
$title = $title . ' - ' . $name->title;
}
}
$title = Text::sprintf('JPAGETITLE', $title, htmlspecialchars_decode($app->get('sitename')));
}
}
}
}
if ($type == 'category' && isset($category->metatitle) && $category->metatitle != '') {
$title = $category->metatitle;
}
if ($type == 'item' && isset($item->metatitle) && $item->metatitle != '') {
$title = $item->metatitle;
}
if ($type == 'item' && $render_canonical_url == 1) {
$canonical = PhocacartRoute::getProductCanonicalLink($item->id, $item->catid, $item->alias, $item->catalias, $item->preferred_catid);
$document->addHeadLink(htmlspecialchars($canonical), 'canonical');
}
$document->setTitle($title);
/* if (isset($item->image) && $item->image != '') {
$pathItem = PhocacartPath::getPath('productimage');
$thumbnail = PhocacartImage::getThumbnailName($pathItem, $item->image, 'large');
if (isset($thumbnail->rel)) {
$document->setMetadata('og:image', Uri::base(true) . '/' .$thumbnail->rel);
}
} else if (isset($category->image) && $category->image != '') {
$pathItem = PhocacartPath::getPath('categoryimage');
$document->setMetadata('og:image', Uri::base(true) . $pathItem['orig_rel_ds'] .$category->image);
}*/
if (isset($item->metadesc) && $item->metadesc != '') {
$document->setMetadata('description', $item->metadesc);
} else {
if (isset($item->description) && strip_tags($item->description) != '') {
$description = str_replace('<', ' <', $item->description);
$description = strip_tags($description);
$description = preg_replace("/\\s\\s+/", " ", $description);
$description = htmlspecialchars(trim($description), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$document->setMetadata('description', $description);
} else {
if (isset($item->description_long) && strip_tags($item->description_long) != '') {
$description = str_replace('<', ' <', $item->description_long);
$description = strip_tags($description);
$description = preg_replace("/\\s\\s+/", " ", $description);
$description = PhocacartText::truncateText($description, 156, ' ...');
// 160 - (" ...")
$description = htmlspecialchars(trim($description), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$document->setMetadata('description', $description);
} else {
if (isset($category->metadesc) && $category->metadesc != '') {
$document->setMetadata('description', $category->metadesc);
} else {
if (isset($category->description) && strip_tags($category->description) != '') {
$description = str_replace('<', ' <', $category->description);
$description = strip_tags($description);
$description = preg_replace("/\\s\\s+/", " ", $description);
$description = htmlspecialchars(trim($description), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$document->setMetadata('description', $description);
} else {
if ($metadesc != '') {
$document->setDescription($metadesc);
} else {
if ($params->get('menu-meta_description', '')) {
$document->setDescription($params->get('menu-meta_description', ''));
}
}
}
}
}
}
}
if (isset($item->metakey) && $item->metakey != '') {
$document->setMetadata('keywords', $item->metakey);
} else {
if (isset($category->metakey) && $category->metakey != '') {
$document->setMetadata('keywords', $category->metakey);
} else {
if ($metakey != '') {
$document->setMetadata('keywords', $metakey);
} else {
if ($params->get('menu-meta_keywords', '')) {
$document->setMetadata('keywords', $params->get('menu-meta_keywords', ''));
}
}
}
}
if ($app->get('MetaTitle') == '1' && $params->get('menupage_title', '')) {
$document->setMetaData('title', $params->get('page_title', ''));
}
if (isset($category->metadata)) {
$registry = new Registry();
$registry->loadString($category->metadata);
$category->metadata = $registry->toArray();
}
if (isset($item->metadata)) {
$registry = new Registry();
$registry->loadString($item->metadata);
$category->metadata = $registry->toArray();
}
if ($type == 'category' && isset($category->metadata['robots']) && $category->metadata['robots'] != '') {
$document->setMetadata('robots', $category->metadata['robots']);
}
if ($type == 'item' && isset($item->metadata['robots']) && $item->metadata['robots'] != '') {
$document->setMetadata('robots', $item->metadata['robots']);
}
if ($params->get('robots')) {
$document->setMetadata('robots', $params->get('robots'));
}
// Breadcrumbs TO DO (Add the whole tree)
if ($type == 'category') {
$path = PhocacartCategory::getPath(array(), (int) $category->id, (int) $category->parent_id, $category->title, $category->alias);
//$curpath = $pathway->getPathwayNames();
$pathWayIdA = $pathway->getPathway();
$pIdA = array();
if (!empty($pathWayIdA)) {
foreach ($pathWayIdA as $k => $v) {
if (isset($v->link)) {
$parts = parse_url($v->link);
if (isset($parts['query'])) {
parse_str($parts['query'], $query);
if (isset($query['id'])) {
$pIdA[] = (int) $query['id'];
}
}
}
}
}
if (!empty($path)) {
$path = array_reverse($path);
foreach ($path as $k => $v) {
if (!in_array((int) $v['id'], $pIdA)) {
// Don't duplicate breadcrumbs
$pathway->addItem($v['title'], Route::_(PhocacartRoute::getCategoryRoute($v['id'], $v['alias'])));
//$pathway->addItem($v['title'], PhocacartRoute::getCategoryRoute($v['id'], $v['alias']));
}
}
}
/*if (isset($category->parentid)) {
if ($category->parentid == 0) {
// $pathway->addItem( JText::_('COM_PHOCACART_CATEGORIES'), JRoute::_(PhocacartRoute::getCategoriesRoute()));
} else if ($category->parentid > 0) {
$curpath = $pathway->getPathwayNames();
$countCurPath = count($curpath)-1;
if(!isset($curpath[$countCurPath]) || ($category->parenttitle != $curpath[$countCurPath])){
$pathway->addItem($category->parenttitle, Route::_(PhocacartRoute::getCategoryRoute($category->parentid, $category->parentalias)));
}
}
}*/
/*if (isset($category->title) && !empty($category->title)) {
$curpath = $pathway->getPathwayNames();
if( (!empty($curpath) && $category->title != $curpath[count($curpath)-1]) || empty($curpath)){
$pathway->addItem($category->title);
}
}*/
} else {
if ($type == 'item' || $type == 'question') {
$path = PhocacartCategory::getPath(array(), (int) $category->id, (int) $category->parent_id, $category->title, $category->alias);
$pathWayIdA = $pathway->getPathway();
$pIdA = array();
if (!empty($pathWayIdA)) {
foreach ($pathWayIdA as $k => $v) {
if (isset($v->link)) {
$parts = parse_url($v->link);
if (isset($parts['query'])) {
parse_str($parts['query'], $query);
if (isset($query['id'])) {
$pIdA[] = (int) $query['id'];
}
}
}
}
}
if (!empty($path)) {
$path = array_reverse($path);
foreach ($path as $k => $v) {
if (!in_array((int) $v['id'], $pIdA)) {
// Don't duplicate breadcrumbs
$pathway->addItem($v['title'], Route::_(PhocacartRoute::getCategoryRoute($v['id'], $v['alias'])));
//$pathway->addItem($v['title'], PhocacartRoute::getCategoryRoute($v['id'], $v['alias']));
}
}
}
/*$curpath = $pathway->getPathwayNames();
if (!empty($path)) {
$path = array_reverse($path);
foreach ($path as $k => $v) {
$pathway->addItem($v['title'], Route::_(PhocacartRoute::getCategoryRoute($v['id'], $v['alias'])));
}
}*/
/* if (isset($category->id) && isset($category->title) && isset($category->alias)) {
if ($category->id > 0) {
$pathway->addItem($category->title, Route::_(PhocacartRoute::getCategoryRoute($category->id, $category->alias)));
}
}*/
if (isset($item->title) && !empty($item->title)) {
$pathway->addItem($item->title);
}
} else {
if ($viewCurrent == $viewLink) {
// Don't add anything to pathway as we display the title from menu link
// for example - comparision view has an menu link, use menu link title
} else {
// we are e.g. in comparison view but we use a menu link from categories
// so we need to add comparison header if exists
if ($header != '') {
$pathway->addItem($header);
}
}
}
}
}