<?php
/*
 * Breadcrumbs for Next Generation CMS 0.9.3
 * Copyright (C) 2010-2011 Alexey N. Zhukov (http://digitalplace.ru)
 * web:    http://digitalplace.ru
 * e-mail: zhukov.alexei@gmail.com
 *
 * Modified with ng-helpers v0.2.0 functions (2026)
 * - Added cache support for performance
 * - Added array_pluck for data extraction
 * - Improved code readability
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
if (!defined('NGCMS')) die('Galaxy in danger');

// Import ng-helpers functions
use function Plugins\{cache_get, cache_put, array_pluck, logger, sanitize, array_get, get_ip};

add_act('index', 'breadcrumbs');
LoadPluginLang('breadcrumbs', 'main', '', 'bc', ':');

function breadcrumbs()
{
	global $lang, $catz, $catmap, $template, $CurrentHandler, $config,
		$SYSTEM_FLAGS, $tpl, $systemAccessURL, $twig;

	// Hide breadcrumbs on main page if configured
	if ($systemAccessURL == '/' && pluginGetVariable('breadcrumbs', 'hide_on_main')) {
		$template['vars']['breadcrumbs'] = '';
		return;
	}

	// Generate cache key based on current URL and language
	$cacheKey = 'breadcrumbs:' . md5($systemAccessURL . array_get($lang, 'locale', 'en') . serialize($CurrentHandler));

	// Try to get from cache (5 minutes TTL)
	$cached = cache_get($cacheKey);
	if ($cached !== null) {
		$template['vars']['breadcrumbs'] = $cached;
		logger('Breadcrumbs served from cache: url=' . sanitize($systemAccessURL, 'string') . ', IP=' . get_ip(), 'debug', 'breadcrumbs.log');
		return;
	}
	$tpath = locatePluginTemplates(
		array('breadcrumbs'),
		'breadcrumbs',
		pluginGetVariable('breadcrumbs', 'template_source')
	);
	$location = array();
	$location_last = '';
	# processing 404 page
	if ($SYSTEM_FLAGS['info']['title']['group'] == $lang['404.title']) {
		$link = str_replace(
			array(
				'{home_url}',
				'{home_title}'
			),
			array(
				$config['home_url'],
				$lang['bc:mainpage']
			),
			$lang['bc:page_404']
		);
		$location[] = array(
			'url'   => $config['home_url'],
			'title' => $lang['bc:mainpage'],
			'link'  => $link,
		);
		$location_last = $lang['404.title'];
	} else {
		if ($CurrentHandler) {
			$params = array_get($CurrentHandler, 'params', []);
			$pluginName = array_get($CurrentHandler, 'pluginName', '');
		}
		# generate main page with or without link
		if ($systemAccessURL != '/') {
			$main_page = str_replace(
				array(
					'{home_url}',
					'{home_title}'
				),
				array(
					$config['home_url'],
					$lang['bc:mainpage']
				),
				$lang['bc:page_404']
			);
			$location[] = array(
				'url'   => $config['home_url'],
				'title' => $lang['bc:mainpage'],
				'link'  => $main_page,
			);
		} else {
			# on main page - don't add to location array, only set location_last
			$location_last = $lang['bc:mainpage'];
		}
		# if category
		if ($CurrentHandler['handlerName'] == 'by.category') {
			$location_last = GetCategories($catz[$params['category']]['id'], true);
			# show full path [if requested]
			if ($catz[$params['category']]['parent'] != 0 && !pluginGetVariable('breadcrumbs', 'block_full_path')) {
				$id = $catz[$params['category']]['parent'];
				do {
					$row = $catz[$catmap[$id]];
					$location_tmp[] = [
						'url' => generateLink('news', 'by.category', [
							'category' => $row['alt'],
							'catid' => $row['id'],
						]),
						'title' => $row['name'],
						'link' => GetCategories($id),
					];
					$id = $row['parent'];
				} while ($id != 0);
				$location = array_merge($location, array_reverse($location_tmp));
			}
		} # news by date
		elseif ($params['year']) {
			# if we have only year then $year = plain text, if we have month then $year = link
			$year = (!$params['month'])
				? $params['year']
				: str_replace(
					array(
						'{year_url}',
						'{year}'
					),
					array(
						generateLink('news', 'by.year', array('year' => $params['year'])),
						$params['year']
					),
					$lang['bc:by.year']
				);
			$month_p = LangDate("F", mktime(0, 0, 0, $params['month'], 7, 0));
			# if we have only year and month then $month = plain text, if we have day then $month = link
			$month = (!$params['day'])
				? $month_p
				: str_replace(
					array(
						'{month_url}',
						'{month_p}'
					),
					array(
						generateLink('news', 'by.month', array('year' => $params['year'], 'month' => $params['month'])),
						$month_p
					),
					$lang['bc:by.month']
				);
			$day = $params['day'];
			$location_last = $year;
			if ($params['month']) {
				$location[] = array(
					'url'   => (!$params['month']) ? '' : generateLink('news', 'by.year', array('year' => $params['year'])),
					'title' => (!$params['month']) ? $params['year'] : $params['year'],
					'link'  => $year,
				);
				$location_last = $month;
			}
			if ($params['day']) {
				$location[] = array(
					'url'   => (!$params['day']) ? '' : generateLink('news', 'by.month', array('year' => $params['year'], 'month' => $params['month'])),
					'title' => (!$params['day']) ? $month_p : $month_p,
					'link'  => $month,
				);
				$location_last = $day;
			}
			# plugin, static, etc.
		} elseif ($pluginName != 'news') {
			if ($pluginName == "static") $location_last = $SYSTEM_FLAGS['info']['title']['item'];
			elseif (($pluginName == 'uprofile' && $CurrentHandler['handlerName'] == 'edit') || $pluginName == 'search')
				$location_last = $SYSTEM_FLAGS['info']['title']['group'];
			elseif ($pluginName == 'uprofile' && $CurrentHandler['handlerName'] == 'show')
				$location_last = $SYSTEM_FLAGS['info']['title']['group'] . ' ' . $SYSTEM_FLAGS['info']['title']['item'];
			elseif ($pluginName == 'core' && (in_array($CurrentHandler['handlerName'], array('registration', 'lostpassword', 'login'))))
				$location_last = $SYSTEM_FLAGS['info']['title']['group'];
			elseif ($params['plugin'] || $pluginName) {
				# if plugin provide put some info
				if ($SYSTEM_FLAGS['info']['breadcrumbs']) {
					# plugin name becomes link
					$count = count($SYSTEM_FLAGS['info']['breadcrumbs']) - 1;
					# all items except last become links
					for ($i = 0; $i < $count; $i++) {
						$link = str_replace(
							array(
								'{plugin_url}',
								'{plugin}'
							),
							array(
								$SYSTEM_FLAGS['info']['breadcrumbs'][$i]['link'],
								$SYSTEM_FLAGS['info']['breadcrumbs'][$i]['text']
							),
							$lang['bc:plugin']
						);
						$location[] = array(
							'url'   => $SYSTEM_FLAGS['info']['breadcrumbs'][$i]['link'],
							'title' => $SYSTEM_FLAGS['info']['breadcrumbs'][$i]['text'],
							'link'  => $link,
						);
					}
					# last item becomes plain text
					$location_last = $SYSTEM_FLAGS['info']['breadcrumbs'][$i]['text'];
				} else {
					$location_last = $SYSTEM_FLAGS['info']['title']['group'] != $lang['loc_plugin']
						? $SYSTEM_FLAGS['info']['title']['group']
						: $params['plugin'];
				}
			}
			# full news
		} elseif ($CurrentHandler['pluginName'] == 'news' && $CurrentHandler['handlerName'] == 'news') {
			$catids = $SYSTEM_FLAGS['news']['db.categories'];
			$location_last = $SYSTEM_FLAGS['info']['title']['item'];
			if (count($catids) != 1 || pluginGetVariable('breadcrumbs', 'block_full_path')) {
				if ($CurrentHandler['params']['category'] != 'none') {
					foreach ($catids as $cid) {
						foreach ($catz as $cc) {
							if ($cc['id'] == $cid) {
								$location[] = array(
									'url'   => generateLink('news', 'by.category', array('category' => $cc['alt'], 'catid' => $cc['id'])),
									'title' => $cc['name'],
									'link'  => GetCategories($cc['id'], false),
								);
							}
						}
					}
				}
			} else {
				$id = $catz[$params['category']]['parent'];
				$location_tmp[] = array(
					'url'   => generateLink('news', 'by.category', array('category' => $catz[$params['category']]['alt'], 'catid' => $catz[$params['category']]['id'])),
					'title' => $catz[$params['category']]['name'],
					'link'  => GetCategories($catz[$params['category']]['id'], false),
				);
				while ($id != 0) {
					foreach ($catz as $cc) {
						if ($cc['id'] == $id) {
							$location_tmp[] = array(
								'url'   => generateLink('news', 'by.category', array('category' => $cc['alt'], 'catid' => $cc['id'])),
								'title' => $cc['name'],
								'link'  => GetCategories($cc['id'], false),
							);
							$id = $catz[$cc['alt']]['parent'];
						}
					}
				}
				$location = array_merge($location, array_reverse($location_tmp));
			}
		}
	}
	$tVars = array(
		'location'      => $location,
		'location_last' => $location_last,
		'separator'     => $separator
	);
	$xt = $twig->loadTemplate($tpath['breadcrumbs'] . 'breadcrumbs.tpl');
	$result = $xt->render($tVars);

	// Cache the result for 5 minutes (300 seconds / 60 = 5 minutes)
	cache_put($cacheKey, $result, 5);

	logger('Breadcrumbs generated: url=' . sanitize($systemAccessURL, 'string') . ', items=' . count($location) . ', IP=' . get_ip(), 'info', 'breadcrumbs.log');

	$template['vars']['breadcrumbs'] = $result;
}
