<?php
//tvars is an associative array with key as tvname and value as tv value
function getDocumentChildrenByTVar($parentid=0, $tvars=array(), $published=1, $fields="*", $sort="menuindex", $dir="ASC", $limit="")
{
$tbn = $this->dbConfig['dbase'].".".$this->dbConfig['table_prefix'];
$limit = ($limit != "") ? "LIMIT $limit" : "";
$sql = "SELECT sc.$fields ";
$i=0;
while(list($key, $value) = each($tvars))
{
$i++;
$sql .= " , stc$i.value AS '$key' ";
}
$sql .= "FROM ".$tbn."site_content sc ";
reset($tvars);
$fields = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(",",$fields)));
$sort = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(',', $sort)));
if($_SESSION['docgroups']) $docgrp = implode(",",$_SESSION['docgroups']);
$i=0;
while(list($key, $value) = each($tvars))
{
$i++;
$sql .= "INNER JOIN ".$tbn."site_tmplvar_contentvalues stc$i ON stc$i.contentid = sc.id INNER JOIN ".$tbn."site_tmplvars st$i ON st$i.id = stc$i.tmplvarid AND st$i.name = '$key' AND stc$i.value = '$value' ";
}
$sql .= " LEFT JOIN ".$tbn."document_groups dg on dg.document = sc.id LEFT JOIN ".$tbn."documentgroup_names dgn ON dgn.id = dg.document_group WHERE sc.parent = $parentid AND sc.published = $published AND (1='".$_SESSION['role']."' OR ".(IN_ETOMITE_PARSER ? "NOT(dgn.private_webgroup<=>1)":"NOT(dgn.private_memgroup<=>1)").(!$docgrp ? "":" OR dg.document_group IN ($docgrp)").") ORDER BY $sort $dir $limit;";
$result = mysql_query($sql) or die($sql);
$resourceArray = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$resourceArray[] = $row;
}
return $resourceArray;
}
function getDocumentChildrenTVarOutputByTVar($parentid=0, $tvidnames=array(), $tvars=array(), $published=1, $docsort="menuindex", $docsortdir="ASC")
{
$docs = $this->getDocumentChildrenByTVar($parentid, $tvars, $published, 'id', $docsort, $docsortdir);
if(!$docs) return false;
else
{
$result = array();
for($i=0;$i<count($docs);$i++)
{
$tvs = $this->getTemplateVarOutput($tvidnames, $docs[$i]["id"],$published);
if($tvs) array_push($result, $tvs);
}
return $result;
}
}
?>
<?php
// Written by Adam Crownoble 12/8/2004
function getParents($page) {
$parents = array();
// Loop while the page hase a valid parent
while($page = $this->getPageInfo($page,0,'id, parent')) {
// Add the current page to the parents array
$parents[] = $page['id'];
// Set page to the parent of the current page
$page = $page['parent'];
}
// Reverse the array so we go from the top down
$parents = array_reverse($parents);
// Return the array
return $parents;
}
// Written by Adam Crownoble 1/11/2005
// Checks to see if a page has a certain page as one of its parents
function hasParent($page,$parent) {
// Get an array of all parents
$parents = $this->getParents($page);
// check to see if the page we're looking for is in the list of parents
if(in_array($parent,$parents)) {
// We found it return true
return true;
}
}
// Written by Adam Crownoble 12/8/2004
// Get a pages depth in the document tree
function getPageDepth($pageid) {
// Get an array of all parents
$parents = $this->getParents($pageid);
// Return the number of parents
return count($parents);
}
?>
<?php
// When the * is replaced with another value, the function matches for that value.
$tvars = array(
"date" => "*",
"file" => "*"
);
$parent=10;
$docs = $modx->getDocumentChildrenByTVar($parent , $tvars, 1, "*", "date ASC, pagetitle DESC, file ASC");
// Print variable types and values found
for ($index=0; $index<count($data); $index++) {
foreach ($data[$index] as $name => $value) {
if ($data[$index]["$name.type"]) {
print "Variable value: ".$data[$index]["$name"]."<br /";
print "Variable type: ".$data[$index]["$name.type"]."<br /";
}
}
}
?>
<?php
// Version 1.1
// Now supports the TV-type
//
// tvars is an associative array with key as tvname and value as tv value
function getDocumentChildrenByTVar($parentid=0, $tvars=array(), $published=1, $fields="*", $sort="menuindex ASC", $limit="")
{
$tbn = $this->dbConfig['dbase'].".".$this->dbConfig['table_prefix'];
$limit = ($limit != "") ? "LIMIT $limit" : "";
$fields = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(",",$fields)));
$sql = "SELECT $fields ";
$i=0;
$sort = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(',', $sort)));
while(list($key, $value) = each($tvars))
{
$i++;
$sql .= " , stc$i.value AS '$key' ";
$sql .= " , st$i.type AS '$key.type' ";
// replace the TV names with stc[number].value in the sort string
$sort = str_replace("sc.$key","stc$i.value",$sort);
}
$sql .= "FROM ".$tbn."site_content sc ";
reset($tvars);
if($_SESSION['docgroups']) $docgrp = implode(",",$_SESSION['docgroups']);
$i=0;
while(list($key, $value) = each($tvars))
{
$i++;
// Create the 'join on value=' part of the join
$join_on_value = ($value=="*") ? "" : " AND stc$i.value like '$value'";
$sql .= "INNER JOIN ".$tbn."site_tmplvar_contentvalues stc$i ON stc$i.contentid = sc.id INNER JOIN ".$tbn."site_tmplvars st$i ON st$i.id = stc$i.tmplvarid AND st$i.name = '$key'$join_on_value";
}
$sql .= " LEFT JOIN ".$tbn."document_groups dg on dg.document = sc.id LEFT JOIN ".$tbn."documentgroup_names dgn ON dgn.id = dg.document_group WHERE sc.parent = $parentid AND sc.published = $published AND (1='".$_SESSION['role']."' OR ".(IN_ETOMITE_PARSER ? "NOT(dgn.private_webgroup<=>1)":"NOT(dgn.private_memgroup<=>1)").(!$docgrp ? "":" OR dg.document_group IN ($docgrp)").") ORDER BY $sort $limit;";
$result = mysql_query($sql) or die($sql);
$resourceArray = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$resourceArray[] = $row;
}
return $resourceArray;
}
?>
Unfortunately a date-TV is stored in the database in text format like ’DD-MM-YYYY HH:MM’. Sorting this text string doesn’t make sense. So you’ll still have to sort it later on.Here’s how I did that:
<?php
// Parameters:
// data: an array of MODx documents
// field1, field2: sort on these fields fields
// order: 'ASC' or 'DESC'
function customSort(&$data, $field1, $field2, $order) {
$code = "\$retval = strnatcmp(\$a['$field1'], \$b['$field1']); if(!\$retval) return strnatcmp(\$a['$field2'], \$b['$field2']); return \$retval;";
$params = ($order=='ASC') ? '$a,$b' : '$b,$a';
usort($data, create_function($params, $code));
}
// This function corrects locally formatted date-TVs
// like 'dd-mm-yyyy' to the universal unix time stamp
// You might adapt this to your local time format
// Parameter: data: an array of MODx documents
function correctTime(&$data) {
for ($index=0; $index<count($data); $index++) {
foreach ($data[$index] as $name => $value) {
if ($data[$index]["$name.type"]=='date') {
list($date,$time) = split('[ ]', $data[$index][$name]);
list($mday,$mon,$year) = split('[-]', $date);
$data[$index][$name] = strtotime("$mon/$mday/$year");
}
}
}
}
?>