protected static function _findItem($needles, $notCheckId = 0, $lang = array())
{
$app = Factory::getApplication();
$menus = $app->getMenu('site', array());
//$items = $menus->getItems('component', 'com_phocacart');
$component = ComponentHelper::getComponent('com_phocacart');
$attributes = array('component_id');
$values = array($component->id);
// Find menu items of current language
$items = $menus->getItems($attributes, $values);
// Multilanguage feature - find only items of selected language (e.g. when language module displays flags of different language - each language can have own menu item)
if (!empty($lang)) {
$attributes[] = 'language';
$values[] = $lang;
// If multilanguage feature enabled and specific lang set then set menu item of such language
$itemsLang = $menus->getItems($attributes, $values);
// If no language items try to find items of current lang and if not found set the current Itemid
if ($itemsLang) {
$items = $itemsLang;
}
} else {
if (Multilanguage::isEnabled()) {
// Just prioritize the current language menu item
$langCurrent = Factory::getLanguage();
$langTag = $langCurrent->getTag();
if ($langTag != '' && $langTag != '*') {
$attributes[] = 'language';
$values[] = $langTag;
$itemsLang = $menus->getItems($attributes, $values);
if ($itemsLang) {
$items = $itemsLang;
}
}
}
}
if (!$items) {
return $app->input->get('Itemid', 0, '', 'int');
//return null;
}
$match = null;
foreach ($needles as $needle => $id) {
if ($notCheckId == 0) {
foreach ($items as $item) {
// Unifiy $item->query['id']
$queryId = '';
if (isset($item->query['id']) && $item->query['id'] != null && $item->query['id'] != 0) {
$queryId = $item->query['id'];
}
if (@$item->query['view'] == $needle && $queryId == $id) {
$match = $item;
break;
}
}
} else {
foreach ($items as $item) {
if (@$item->query['view'] == $needle) {
$match = $item;
break;
}
}
}
if (isset($match)) {
break;
}
}
if (!$match) {
// Nothing found, try to set back "categories menu link" so e.g. menu links in module to some category
// gets no ID from another category which do have a menu link
// Category A have menu link
// Category B gets its link becasue view = categories is assigned with active id, which is not categories active id
foreach ($items as $item) {
// Nothing found, gets some categories view, better than some category view from another category
if (@$item->query['view'] == 'categories') {
$match = $item;
break;
}
}
}
return $match;
}