Skip to content

Instantly share code, notes, and snippets.

@stefanheimes
Last active July 19, 2016 12:20
Show Gist options
  • Save stefanheimes/7578782 to your computer and use it in GitHub Desktop.
Save stefanheimes/7578782 to your computer and use it in GitHub Desktop.
wf_extendedBreadcrumb - Add missing items like MetaModels/Isotope etc.
<?php
// MM
use MetaModels\Filter\Rules\SearchAttribute as MetaModelFilterRuleSearchAttribute;
use MetaModels\Filter\Setting\Factory as MetaModelFilterSettingsFactory;
// Isotope
use Isotope\Model\Product;
/**
* PHP version 5
* @copyright MEN AT WORK 2012
* @package x-extendenBreadcrumb
* @license LGPL
* @filesource
*/
class XBreadCrumb extends Frontend
{
/**
* Breadcrumb
*
* @var \wf_extendedBreadcrumb
*/
protected $objBreadcrumb = null;
/**
* List with all items.
*
* @var array
*/
protected $arrNavigationItems = array();
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
}
////////////////////////////////////////////////////////////////////////////
// Hooks
////////////////////////////////////////////////////////////////////////////
/**
* Add new items to the list.
*/
public function addItem($arrItems, $objBreadcrumb)
{
// Get current page.
global $objPage;
// Save some vars.
$this->arrNavigationItems = $arrItems;
$this->objBreadcrumb = $objBreadcrumb;
try
{
// Check if we have a page for lookup
switch ($objPage->id)
{
// MM.
case 236:
$this->searchForMetaModelsModule(true, true, array('alias' => 'title'));
break;
case 249:
$this->searchForMetaModelsModule(true, true, array('alias' => 'title'));
break;
case 239:
$this->searchForMetaModelsModule(true, true, array('alias' => 'title'));
break;
case 630:
$this->searchForMetaModelsModule(true, true, array('alias' => 'title'));
break;
}
}
catch (Exception $exc)
{
// Nothing to do.
}
// Return the new list.
return $this->arrNavigationItems;
}
////////////////////////////////////////////////////////////////////////////
// Helper functions isotope
////////////////////////////////////////////////////////////////////////////
/**
* Add the stat of the checkout to the breadcrumb.
*
* @param boolean $blnRemoveLastEntry Flag for removing the last
* entry befor adding the new one.
*/
protected function getCheckoutStep($blnRemoveLastEntry)
{
$strCurrentStep = \Haste\Input\Input::getAutoItem('step');
if (isset($GLOBALS['TL_LANG']['MSC']['checkout_' . $strCurrentStep]))
{
// Add new entry.
$this->addItemToList($GLOBALS['TL_LANG']['MSC']['checkout_' . $strCurrentStep], $blnRemoveLastEntry);
}
}
/**
* Get the full name for a isotope product.
*
* @param boolean $blnRemoveLastEntry Flag for removing the last
* entry befor adding the new one.
*/
protected function addItemsForIsotope($blnRemoveLastEntry)
{
// Get the product.
$objProduct = Product::findAvailableByIdOrAlias(\Haste\Input\Input::getAutoItem('product'));
// Check if we have a product.
if ($objProduct == null)
{
return;
}
// Add new entry.
$this->addItemToList($objProduct->name, $blnRemoveLastEntry);
}
////////////////////////////////////////////////////////////////////////////
// Helper functions mm
////////////////////////////////////////////////////////////////////////////
/**
* Search for a metamodels element.
*
* @global type $objPage
*
* @param type $blnRemoveLastEntry Flag if the last elemnt should removed.
* @param type $blnLookup Flag if a the system should lookup some other values.
* @param type $arrFieldMapping Name for the attribute for lookup.
*/
protected function searchForMetaModelsModule($blnRemoveLastEntry, $blnLookup, $arrFieldMapping)
{
global $objPage;
// Get all articles.
$objArticles = Database::getInstance()
->prepare('SELECT id FROM tl_article WHERE pid=?')
->execute($objPage->id);
// Get all content elements.
$arrContentElements = array();
while ($objArticles->next())
{
$arrResultElements = Database::getInstance()
->prepare('SELECT id FROM tl_content WHERE pid=?')
->execute($objArticles->id)
->fetchAllAssoc();
if(count($arrResultElements) != 0)
{
$arrContentElements = array_merge($arrContentElements, $arrResultElements);
}
}
// Search for a news reader module.
$objMMModule = null;
// Run each content element.
foreach ($arrContentElements as $arrContent)
{
$objMMModule = $this->getMetaModelsModule($arrContent['id']);
if ($objMMModule != null)
{
break;
}
}
// Nothing found.
if ($objMMModule == null)
{
return;
}
// Get data from metamodels.
return $this->addItemsForMetaModelsModule($objMMModule->id, $blnRemoveLastEntry, $blnLookup, $arrFieldMapping);
}
/**
* Search for an mm list module.
*
* @param int $intContentId Id of the content element.
*
* @return Database_Result Reuslt for the module or null if nothing was found.
*/
protected function getMetaModelsModule($intContentId)
{
$objContentElement = Database::getInstance()
->prepare('SELECT * FROM tl_content WHERE id=?')
->execute($intContentId);
if ($objContentElement->numRows == 0 || $objContentElement->type != 'module')
{
return null;
}
$objModule = Database::getInstance()
->prepare('SELECT * FROM tl_module WHERE id=?')
->execute($objContentElement->module);
if ($objModule->numRows == 0 || $objModule->type != 'metamodel_list')
{
return null;
}
return $objModule;
}
/**
* Try to get all current filter values for a speical module.
*
* @param int $intModuelId Id of the module
* @param type $blnRemoveLastEntry Flag if the last elemnt should removed.
* @param type $blnLookup Flag if a the system should lookup some other values.
* @param type $arrFieldMapping Name for the attribute for lookup.
*/
protected function addItemsForMetaModelsModule($intModuelId, $blnRemoveLastEntry, $blnLookup, $arrFieldMapping)
{
// Get the module
$objModule = $this->Database
->prepare('SELECT * FROM tl_module WHERE id=?')
->execute($intModuelId);
// Check if we have some data.
if ($objModule->numRows == 0 || $objModule->type != 'metamodel_list' || $objModule->metamodel_filtering == '')
{
return;
}
// Get the filter id
$intFilterRulesID = $objModule->metamodel_filtering;
// Get the current filter and check if we have a value for it.
$objFilterSettings = MetaModelFilterSettingsFactory::byId($intFilterRulesID);
if ($objFilterSettings == null)
{
return;
}
// Get filter url params and mm. If we have a error, mayber we
// have no filter for this page or something else goes
// wrong.
try
{
$arrFilterUrls = $objFilterSettings->getParameters();
$arrAttributes = $objFilterSettings->getReferencedAttributes();
$objMM = $objFilterSettings->getMetaModel();
}
catch (Exception $exc)
{
return;
}
$arrNavTitles = array();
// Check each filter param.
foreach ($arrFilterUrls as $strKey => $strValue)
{
// Get value.
$strGetValue = $this->Input->get($strValue);
// Get values from the attribute
$objAttribute = $objMM->getAttribute($arrAttributes[$strKey]);
$arrValues = $objAttribute->getFilterOptions(null, false);
// Add value to array.
if ($blnLookup && !empty($arrFieldMapping) && array_key_exists($objAttribute->getColName(), $arrFieldMapping))
{
// Get return field from mapping.
$strReturnField = $arrFieldMapping[$objAttribute->getColName()];
// Make a lookup.
$arrNavTitles[] = $this->getValuesFromMM($objMM, $objAttribute, $strGetValue, $strReturnField);
}
else
{
// Use the value as it is.
$arrNavTitles[] = $arrValues[$strGetValue];
}
}
// Clear empty values.
$arrNavTitles = array_filter($arrNavTitles);
// Check if we have a entry.
if (count($arrNavTitles) != 0)
{
$this->addItemToList(implode(' & ', $arrNavTitles), $blnRemoveLastEntry);
}
}
/**
*
* @param IMetaModel $objMM
* @param IMetaModelAttribute $objAttribute
* @param string $strSearchValue
* @param string $strReturnAttribute
*/
protected function getValuesFromMM(&$objMM, &$objAttribute, $strSearchValue, $strReturnAttribute)
{
// Catch the item.
$objFilter = $objMM->getEmptyFilter();
$objFilter->addFilterRule(new MetaModelFilterRuleSearchAttribute($objAttribute, $strSearchValue));
$objItems = $objMM->findByFilter($objFilter, $objAttribute->getColName(), 0, 1);
// Check if we have values.
if ($objItems->getCount() == 0)
{
return $strSearchValue;
}
// Parse values.
$objItem = $objItems->getItem();
$arrValues = $objItem->parseAttribute($strReturnAttribute);
// Return values.
if (isset($arrValues['text']))
{
return $arrValues['text'];
}
return $strSearchValue;
}
////////////////////////////////////////////////////////////////////////////
// Support functions
////////////////////////////////////////////////////////////////////////////
/**
* Add a new entry to the list.
*
* @param string $strFullTitle The fill title for the new element.
* @param boolean $blnRemoveLastEntry Flag for removing the last entry in list.
*/
protected function addItemToList($strFullTitle, $blnRemoveLastEntry)
{
// Get some basic information.
$intMaxLentgh = $this->objBreadcrumb->contextLength;
$strPlaceholder = $this->objBreadcrumb->wf_extendedBreadcrumb_placeholder;
// Get some vars.
$arrItems = $this->arrNavigationItems;
// Remove the last entry.
if ($blnRemoveLastEntry == true)
{
// Remove the last entry.
array_pop($arrItems);
}
else
{
// Remove the isActive from the last entry.
$arrArrayKeys = array_keys($arrItems);
$intLastKey = array_pop($arrArrayKeys);
$arrItems[$intLastKey]['isActive'] = false;
}
// Short the title if needed.
$strTitle = $strFullTitle;
if (strlen($strTitle) > $intMaxLentgh)
{
$strTitle = String::getInstance()->substr($strTitle, (int) $intMaxLentgh, $strPlaceholder);
}
// Refresh the contextLength.
$this->objBreadcrumb->contextLength = $intMaxLentgh - strlen($strTitle);
// Add the new entry.
$arrItems[] = array
(
'isActive' => TRUE,
'href' => '#',
'title' => $strTitle,
'longtitle' => $strFullTitle,
'class' => ''
);
// Save.
$this->arrNavigationItems = $arrItems;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment