Skip to content

Instantly share code, notes, and snippets.

@sirlancelot
Created November 21, 2009 00:38
Show Gist options
  • Save sirlancelot/239915 to your computer and use it in GitHub Desktop.
Save sirlancelot/239915 to your computer and use it in GitHub Desktop.
<?php
function GetDocumentData() {
$Database = $this->modx->db;
$DocIDs = $this->modx->getChildIds($this->Configuration['StartID'], $this->Configuration['Level']);
$Select = array(
'ID' => 'sc.id',
'Parent' => 'sc.parent',
'Hidden' => 'sc.hidemenu',
'Title' => 'IF(sc.menutitle!=\'\',sc.menutitle,sc.pagetitle)',
'Order' => 'sc.menuindex',
'Description' => 'IF(sc.description!=\'\',sc.description,sc.longtitle)',
'Alias' => 'sc.alias',
'Type' => 'sc.type',
'Url' => 'IF(sc.type=\'reference\',sc.content,NULL)',
'Attributes' => 'sc.link_attributes',
);
$From = array(
'sc' => $this->modx->getFullTableName('site_content'),
);
$Where = array(
'sc.id IN ('.$Database->escape(implode(', ', $DocIDs)).')',
);
$Order = array(
'parent ASC',
'menuindex ASC',
);
$LeftJoin = array();
if ($this->Configuration['SubMenuTV']) {
$ContentValues = $this->modx->getFullTableName('site_tmplvar_contentvalues');
$TemplateVars = $this->modx->getFullTableName('site_tmplvars');
if (strpos($this->Configuration['SubMenuTV'], ',') !== false)
$SubMenuTVs = preg_split('/\s*,\s*/', $this->Configuration['SubMenuTV']);
else $SubMenuTVs = array($this->Configuration['SubMenuTV']);
// Build a LEFT JOIN for each Template Variable supplied
foreach ($SubMenuTVs as $i => $TV) {
$TV = $Database->escape($TV);
$CVAlias = $Database->escape('tv'.$i);
$TVAlias = $Database->escape($CVAlias.'name');
$LeftJoin[] = '('.$ContentValues.' AS '.$CVAlias.', '.$TemplateVars.' AS '.$TVAlias.')'.
"\n\tON (".$CVAlias.'.contentid = sc.id AND '.$CVAlias.'.tmplvarid = '.$TVAlias.'.id AND '.$TVAlias.'.name = "'.$TV.'")';
$Select['X-'.$TV] = '`'.$CVAlias.'`.value';
}
}
// Now build the SQL Query
$Sql = "SELECT DISTINCT\n";
foreach ($Select as $Alias => $Column)
$Sql .= "\t".$Column.' AS `'.$Alias."`,\n";
$Sql = rtrim($Sql, ",\n")."\nFROM\n";
foreach ($From as $Alias => $Table)
$Sql .= "\t".$Table.' AS `'.$Alias."`,\n";
$Sql = rtrim($Sql, ",\n")."\n";
foreach ($LeftJoin as $JoinItem)
$Sql .= 'LEFT JOIN '.$JoinItem."\n";
$Sql .= "WHERE\n";
$WhereItem = reset($Where);
for ($Count = count($Where); $WhereItem !== false; $WhereItem = next($Where), $Count--) {
$Sql .= "\t".$WhereItem;
if ($Count <= 1)
$Sql .= "\n";
else $Sql .= " AND\n";
}
$Sql .= "ORDER BY\n";
foreach ($Order as $Sort)
$Sql .= "\t".$Sort.",\n";
$Sql = rtrim($Sql, ",\n");
$Result = $Database->query($Sql);
// Bring everything together by parent so we can build a hierarchy next.
$Records = array();
while ($Row = $Database->getRow($Result))
$Records[$Row['Parent']][$Row['ID']] = $Row;
$Data = $this->BuildHierarchy($this->Configuration['StartID'], $Records);
if (!empty($Records)) {
// Extraneous records were found. This might be a problem...
$RecordsOutput = htmlentities(print_r($Records, true));
$DataOutput = htmlentities(print_r($Data, true));
$ErrorMessage = <<<HTML
<h2>Extraneous Records Found</h2>
<pre>{$RecordsOutput}</pre>
<h2>Valid Data</h2>
<pre>{$DataOutput}</pre>
HTML;
$this->modx->logEvent(1, 2, $ErrorMessage, get_class($this));
}
return $Data;
}
function BuildHierarchy($ParentID, &$Records) {
if (!array_key_exists($ParentID, $Records))
return false;
$Return = $Records[$ParentID];
unset($Records[$ParentID]);
foreach ($Return as $ID => $Item) {
// Clear out some unneeded variables
unset($Return[$ID]['ID'], $Return[$ID]['Parent']);
if ($Return[$ID]['Url'] === null) unset($Return[$ID]['Url']);
// Build the rest
$Children = $this->BuildHierarchy($ID, $Records);
if ($Children)
$Return[$ID]['Children'] = $Children;
elseif ($Return[$ID]['Hidden'])
unset($Return[$ID]);
}
return $Return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment