Skip to content

Instantly share code, notes, and snippets.

@stefanheimes
Created October 23, 2013 13:27
Show Gist options
  • Save stefanheimes/7118746 to your computer and use it in GitHub Desktop.
Save stefanheimes/7118746 to your computer and use it in GitHub Desktop.
Example for mapping MM elements to the wf_extendedBreadcrumb.
<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
class XBreadCrumb extends Frontend
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
}
////////////////////////////////////////////////////////////////////////////
// Hooks
////////////////////////////////////////////////////////////////////////////
/**
* Add new items to the list.
* Add a mapping for each site:
* $strKey = Name of Filter attribute
* $strValue = Name of attribute for the displayed entry.
*/
public function addItem($arrItems, $objBreadcrumb)
{
global $objPage;
// News
if ($objPage->id == 236)
{
$arrMapping = array('alias' => 'title');
$arrItems = $this->searchForMetaModelsModule($arrItems, $objBreadcrumb, true, $arrMapping);
}
// Press
else if ($objPage->id == 249)
{
$arrMapping = array('alias' => 'title');
$arrItems = $this->searchForMetaModelsModule($arrItems, $objBreadcrumb, true, $arrMapping);
}
// Seminar
else if ($objPage->id == 239)
{
$arrMapping = array('alias' => 'title');
$arrItems = $this->searchForMetaModelsModule($arrItems, $objBreadcrumb, true, $arrMapping);
}
return $arrItems;
}
////////////////////////////////////////////////////////////////////////////
// MM - Helper functions
////////////////////////////////////////////////////////////////////////////
protected function searchForMetaModelsModule($arrItems, $objBreadcrumb, $blnLookup, $arrFieldMapping)
{
global $objPage;
// Search all Conten elements.
$arrArticle = $this->getChildRecords(array($objPage->id), 'tl_article');
$arrContent = $this->getChildRecords($arrArticle, 'tl_content');
// Search for a mm module.
$objNewsModule = null;
// Run each content element.
foreach ($arrContent as $intContentId)
{
if ($this->getMetaModelsModule($intContentId) != null)
{
$objNewsModule = $this->getMetaModelsModule($intContentId);
break;
}
}
// Nothing found.
if ($objNewsModule == null)
{
return $arrItems;
}
// Get data from metamodels.
return $this->addItemsForModule($objBreadcrumb, $objNewsModule->id, $arrItems, true, $blnLookup, $arrFieldMapping);
}
/**
* Search for a 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 array $arrBreadcrumbItems List with all Items from the breadcrumb
* @param type $blnRemoveLastEntrie Flag if the last elemnt should removed.
* @param type $blnLookup Flag if a the system should lookup some other values.
* @param type $strField Name for the attribute for lookup.
*
* @return array List with all new and old Items from the breadcrumb
*/
protected function addItemsForModule($objBreadcrumb, $intModuelId, $arrBreadcrumbItems, $blnRemoveLastEntrie, $blnLookup, $arrFieldMapping)
{
$intMaxLentgh = $objBreadcrumb->contextLength;
$strPlaceholder = $objBreadcrumb->wf_extendedBreadcrumb_placeholder;
// 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' || !empty($objModule->metamodel_filtering))
{
return $arrBreadcrumbItems;
}
// 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 $arrBreadcrumbItems;
}
// Get filter url params and mm.
$arrFilterUrls = $objFilterSettings->getParameters();
$arrAttributes = $objFilterSettings->getReferencedAttributes();
$objMM = $objFilterSettings->getMetaModel();
$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)
{
if ($blnRemoveLastEntrie == true)
{
// Remove the last entry.
array_pop($arrBreadcrumbItems);
}
else
{
// Remove the isActive from the last entry.
$arrArrayKeys = array_keys($arrBreadcrumbItems);
$intLastKey = array_pop($arrArrayKeys);
$arrBreadcrumbItems[$intLastKey]['isActive'] = false;
}
// Short the title if needed.
$strFullTitle = $strTitle = implode(' & ', $arrNavTitles);
if (strlen($strTitle) > $intMaxLentgh)
{
$strTitle = String::getInstance()->substr($strTitle, (int) $intMaxLentgh, $strPlaceholder);
}
// Refresh the contextLength.
$objBreadcrumb->contextLength = $intMaxLentgh - strlen($strTitle);
// Add the new entry.
$arrBreadcrumbItems[] = array
(
'isActive' => TRUE,
'href' => '#',
'title' => $strTitle,
'longtitle' => $strFullTitle,
'class' => ''
);
}
return $arrBreadcrumbItems;
}
/**
*
* @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;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment