Created
December 11, 2013 23:27
-
-
Save stefanheimes/7920427 to your computer and use it in GitHub Desktop.
Inserttags für MM - Überarbeitete Version - Alpha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The MetaModels extension allows the creation of multiple collections of custom items, | |
* each with its own unique set of selectable attributes, with attribute extendability. | |
* The Front-End modules allow you to build powerful listing and filtering of the | |
* data in each collection. | |
* | |
* PHP version 5 | |
* @package MetaModels | |
* @subpackage metamodels_inserttags | |
* @author Tim Gatzky <[email protected]> | |
* @author Stefan Heimes <[email protected]> | |
* @copyright The MetaModels team. | |
* @license LGPL. | |
* @filesource | |
*/ | |
/* | |
Available inserttags | |
{{metamodel::total::list::ce::*}} | |
{{metamodel::total::list::mod::*}} | |
{{metamodel::total::filter::ce::*}} | |
{{metamodel::total::filter::mod::*}} | |
--> no need to tell the inserttag its a list or filter type | |
{{metamodel::total::mod::*}} | |
{{metamodel::total::ce::*}} | |
-- item based | |
{{metamodelitem::table-or-id::item-or-setOfIds::rendersetting}} -> html output | |
{{metamodeldetailitem::table-or-id::item::rendersetting}} -> html output | |
-- attribute based | |
{{metamodelattribute::table-or-id::item-id::field}} -> value, plain text | |
*/ | |
class MetaModelInsertTags extends Controller | |
{ | |
protected $arrMappingTagFunction = array( | |
'metamodel' => array( | |
'total' => array( | |
'function' => 'getCount', | |
'param' => array(3, 4) | |
) | |
), | |
'metamodelattribute' => array( | |
'function' => 'getAttribute', | |
'param' => array(1, 2, 3) | |
) | |
); | |
public function replaceTags($strTag) | |
{ | |
$arrElements = explode('::', $strTag); | |
// Init the mapping array. | |
$arrPartElement = $this->arrMappingTagFunction; | |
// Run each param. | |
for ($i = 0; $i < count($arrElements); $i++) | |
{ | |
// Get the current key. | |
$strCurrentKey = $arrElements[$i]; | |
// Check if we known this key. | |
if (isset($arrPartElement[$strCurrentKey])) | |
{ | |
// Check if we have a function set. | |
if (isset($arrPartElement[$strCurrentKey]['function'])) | |
{ | |
// Get the function name. | |
$strFunction = $arrPartElement[$strCurrentKey]['function']; | |
$arrParameters = array(); | |
// Get the parameters. | |
if (isset($arrPartElement[$strCurrentKey]['param'])) | |
{ | |
foreach ($arrPartElement[$strCurrentKey]['param'] as $intKey) | |
{ | |
$arrParameters[] = $arrElements[$intKey]; | |
} | |
} | |
// Call the function. | |
return call_user_func_array(array($this, $strFunction), $arrParameters); | |
} | |
else | |
{ | |
$arrPartElement = $arrPartElement[$strCurrentKey]; | |
} | |
} | |
else | |
{ | |
// We did not found the key in list. return false. | |
return false; | |
} | |
} | |
// End of all. If we reach this point, something wents wrong. | |
return false; | |
} | |
/** | |
* Replace MetaModels related inserttags | |
* | |
* @param string | |
* @return mixed | |
*/ | |
public function replaceTagsOriginal($strTag) | |
{ | |
$arrElements = explode('::', $strTag); | |
switch ($arrElements[0]) | |
{ | |
//-- metamodelitem | |
case 'metamodelitem': | |
// metamodel by table or id | |
$objMetaModel = null; | |
if (is_numeric($arrElements[1])) | |
{ | |
$objMetaModel = MetaModelFactory::byId($arrElements[1]); | |
} | |
else if (is_string($arrElements[1])) | |
{ | |
$objMetaModel = MetaModelFactory::byTableName($arrElements[1]); | |
} | |
else | |
{ | |
return false; | |
} | |
$objMetaModelList = new MetaModelList(); | |
$objMetaModelList->setMetaModel($objMetaModel->get('id'), $arrElements[3]); | |
#$objMetaModelList->setFilterParam(0,array(),$_GET); | |
// handle a set of ids | |
$arrIds = explode(',', $arrElements[2]); | |
// check publish state of item | |
$objDB = Database::getInstance(); | |
$objAttrCheckPublish = $objDB->prepare("SELECT colname FROM tl_metamodel_attribute WHERE pid=? AND check_publish=1")->limit(1)->execute($objMetaModel->get('id')); | |
if ($objAttrCheckPublish->numRows > 0) | |
{ | |
foreach ($arrIds as $i => $itemId) | |
{ | |
$objItem = $objMetaModel->findById($itemId); | |
if (!$objItem->get($objAttrCheckPublish->colname)) | |
{ | |
unset($arrIds[$i]); | |
} | |
} | |
} | |
// render an empty inserttag rather than displaying a list with an empty result information. do not return false here because the inserttag itself is correct. | |
if (count($arrIds) < 1) | |
{ | |
return ''; | |
} | |
$objMetaModelList->addFilterRule(new MetaModelFilterRuleStaticIdList($arrIds)); | |
return $objMetaModelList->render(false, $this); | |
break; | |
default: | |
return false; | |
break; | |
} | |
} | |
//////////////////////////////////////////////////////////////////////////// | |
// Tag functions | |
//////////////////////////////////////////////////////////////////////////// | |
/** | |
* Get from MM X the item with the id Y and parse the attribute Z and | |
* return it. | |
* | |
* @param mixed $mixMMName Name or id of mm. | |
* @param type $intDataId ID form the row. | |
* @param type $strAttributeName Name of the attribute. | |
* | |
* @return boolean|mixed | |
*/ | |
protected function getAttribute($mixMMName, $intDataId, $strAttributeName) | |
{ | |
// Get the MM. | |
$objMM = $this->loadMM($mixMMName); | |
if ($objMM == null) | |
{ | |
return false; | |
} | |
// Get item. | |
$objMetaModelItem = $objMM->findById($intDataId); | |
// Parse attribute. | |
$arrAttr = $objMetaModelItem->parseAttribute($strAttributeName); | |
// ToDo: Maybe this should not allways be a text element. | |
return $arrAttr['text']; | |
} | |
/** | |
* Get count from a module or content element of a mm. | |
* | |
* @param string $strType Type of element like mod or ce. | |
* @param type $intID | |
* | |
* @return boolean | |
*/ | |
protected function getCount($strType, $intID) | |
{ | |
switch ($strType) | |
{ | |
// From module, can be a metamodel list or filter | |
case 'mod': | |
$objMMResult = $this->getMMDataFrom('tl_module', $intID); | |
break; | |
// From content element, can be a metamodel list or filter. | |
case 'ce': | |
$objMMResult = $this->getMMDataFrom('tl_content', $intID); | |
break; | |
// Unknow element type. | |
default: | |
return false; | |
} | |
// Check if we have data | |
if ($objMMResult != null) | |
{ | |
return $this->getCountFor($objMMResult->metamodel, $objMMResult->metamodel_filtering); | |
} | |
return false; | |
} | |
//////////////////////////////////////////////////////////////////////////// | |
// Helper | |
//////////////////////////////////////////////////////////////////////////// | |
/** | |
* Get some informations about a table. | |
* | |
* @param string $strTable Name of table | |
* @return null | |
*/ | |
protected function getMMDataFrom($strTable, $intID) | |
{ | |
$objDB = Database::getInstance(); | |
// Check if we know the table | |
if (!$objDB->tableExists($strTable)) | |
{ | |
return null; | |
} | |
// Get all information form table or retunr null if we have no data. | |
$objResult = $objDB | |
->prepare("SELECT metamodel, metamodel_filtering FROM " . $strTable . " WHERE id=?") | |
->limit(1) | |
->execute($intID); | |
// Check if we have some data. | |
if ($objResult->numRows < 1) | |
{ | |
return null; | |
} | |
return $objResult; | |
} | |
/** | |
* Try to laod the mm by id or name. | |
* | |
* @param mixed $mixMMName Name or id of mm. | |
* | |
* @return IMetaModel|null | |
*/ | |
protected function loadMM($mixMMName) | |
{ | |
// ID. | |
if (is_numeric($mixMMName)) | |
{ | |
return MetaModelFactory::byId($mixMMName); | |
} | |
// Name. | |
else if (is_string($mixMMName)) | |
{ | |
return MetaModelFactory::byTableName($mixMMName); | |
} | |
// Unknown. | |
return null; | |
} | |
/** | |
* Get count form one MM for chosen filter. | |
* | |
* @param int $intMMId ID of the metamodels | |
* @param int $intFilterID ID of the filter | |
* | |
* @return boolean|int False for no data or integer for the count result. | |
*/ | |
protected function getCountFor($intMMId, $intFilterID) | |
{ | |
// ToDo: Add check if we have realy a mm and ff. | |
$objMetaModel = $this->loadMM($intMMId); | |
if ($objMetaModel == null) | |
{ | |
return false; | |
} | |
$objFilter = $objMetaModel->prepareFilter($intFilterID, $_GET); | |
return $objMetaModel->getCount($objFilter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment