Auxiliary snippets: ListSections
ListSections is a custom snippet used with the Orchestra records in the sample application's custom tables. It resolves a common problem where you have a two-level hierarchy in the records, and in this application it generates both the output page display and the management list display.
<?php
// ListSections
/**********************************************************************
List entries from database table, by section then rank
Skip empty sections
Input:
- $table: database table (without prefix);
- $sections: string 'section,section...';
-- section may be database column name||displayname if they are different;
-- default: all, using column names
- $showUnpublished: include entries marked as unpublished (default=0)
- $outerTpl: template chunk for sections with DT or UL tag;
- $innerTpl: template chunk for player with DD or LI tag;
Expects snippet to be wrapped in DL tag if using DL overall
Output:
string of section names, and entries, wrapped in
placeholders: one for each DB column, with same name as column
for management version:
[+changePub+] = 0 or 1 to (un)publish;
[+hideShow+] for (un)publish command string
***********************************************************************/
$table = $modx->getFullTableName($table);
$showUnpublished = (isset($showUnpublished)) ? $showUnpublished : 0;
// select latest update time from table for footer placeholder
$result = $modx->db->select('MAX(updated)', $table);
$row = $modx->db->getRow($result );
$updated = $row['MAX(updated)'];
$modx->setPlaceholder('updated', strtotime($updated));
$sections = explode(",", $sections);
$output = NULL;
foreach($sections as $section) {
$sectionSplit = explode('||', $section);
$sectionCol = trim($sectionSplit[0]);
$sectionName = isset($sectionSplit[1]) ? $sectionSplit[1] : $sectionSplit[0];
$sectionName = str_replace('_AMP_', '&', $sectionName);
$query = "section = '$sectionCol'";
if ($showUnpublished == 0) {
$query .= "AND published = 1";
}
$result = $modx->db->select('*', $table, $query, 'rank');
$itemCount = $modx->db->getRecordCount($result);
if ($itemCount > 0) {
$chunkArr = array('section'=>$sectionName);
$output .= $modx->parseChunk($outerTpl, $chunkArr, '[+', '+]');
while ($item = $modx->db->getRow( $result)) {
if ($item['published'] == 0) {
$pubUnpub = 1;
$pubCmd = 'Publish';
} else {
$pubUnpub = 0;
$pubCmd = 'Unpublish';
}
$chunkArr = array(
'changePub'=>$pubUnpub,
'pubCmd'=>$pubCmd
);
$chunkArr = array_merge($item, $chunkArr);
$output .= $modx->parseChunk($innerTpl, $chunkArr, '[+', '+]');
}
}
}
return $output;
?>
// ListSections
/**********************************************************************
List entries from database table, by section then rank
Skip empty sections
Input:
- $table: database table (without prefix);
- $sections: string 'section,section...';
-- section may be database column name||displayname if they are different;
-- default: all, using column names
- $showUnpublished: include entries marked as unpublished (default=0)
- $outerTpl: template chunk for sections with DT or UL tag;
- $innerTpl: template chunk for player with DD or LI tag;
Expects snippet to be wrapped in DL tag if using DL overall
Output:
string of section names, and entries, wrapped in
placeholders: one for each DB column, with same name as column
for management version:
[+changePub+] = 0 or 1 to (un)publish;
[+hideShow+] for (un)publish command string
***********************************************************************/
$table = $modx->getFullTableName($table);
$showUnpublished = (isset($showUnpublished)) ? $showUnpublished : 0;
// select latest update time from table for footer placeholder
$result = $modx->db->select('MAX(updated)', $table);
$row = $modx->db->getRow($result );
$updated = $row['MAX(updated)'];
$modx->setPlaceholder('updated', strtotime($updated));
$sections = explode(",", $sections);
$output = NULL;
foreach($sections as $section) {
$sectionSplit = explode('||', $section);
$sectionCol = trim($sectionSplit[0]);
$sectionName = isset($sectionSplit[1]) ? $sectionSplit[1] : $sectionSplit[0];
$sectionName = str_replace('_AMP_', '&', $sectionName);
$query = "section = '$sectionCol'";
if ($showUnpublished == 0) {
$query .= "AND published = 1";
}
$result = $modx->db->select('*', $table, $query, 'rank');
$itemCount = $modx->db->getRecordCount($result);
if ($itemCount > 0) {
$chunkArr = array('section'=>$sectionName);
$output .= $modx->parseChunk($outerTpl, $chunkArr, '[+', '+]');
while ($item = $modx->db->getRow( $result)) {
if ($item['published'] == 0) {
$pubUnpub = 1;
$pubCmd = 'Publish';
} else {
$pubUnpub = 0;
$pubCmd = 'Unpublish';
}
$chunkArr = array(
'changePub'=>$pubUnpub,
'pubCmd'=>$pubCmd
);
$chunkArr = array_merge($item, $chunkArr);
$output .= $modx->parseChunk($innerTpl, $chunkArr, '[+', '+]');
}
}
}
return $output;
?>