[TYPO3-ect] recursive pid function for tx_div
Steve Ryan
stever at syntithenai.com
Mon Aug 13 16:10:41 CEST 2007
>
> Something lost here?
>
>> $pidQuery='(pid='.implode(' or pid=',$pidList).')';
>
> Maybe?
>
> $pidQuery='( 0 '.implode(' or pid=',$pidList).')';
>
as long as there is one element in the array of pids, the first is OK
(updated code below ensures one element or returns empty array)
>
>> $cObj=$this->findCObject();
>
> Leads to another idea for tx_div:
>
> function findCObjectSingleton() {
> static $cObject;
> if(!is_object($cObject)) {
> // create it here
> }
> return $cObject;
> }
>
> Does this work as expected?
>
>
More or less
The static variable needs to be in the scope of the class
eg
class tx_markablequiz_controller {
static $cObj ;
static function findCObjectSingleton() {
if(!is_object(self::$cObj)) {
self::$cObj=t3lib_div::makeInstance('tslib_cObj');
debug(array('creating cobj'));
}
debug(array('returning cobj'));
return self::$cObj;
}
static function plr($pidList,$recursive) {
// ensure array format
if (!is_array($pidList)) {
$pidList=explode(',',$pidList);
}
// dump when recursion reaches 0
if ($recursive==0) {
return $pidList;
} else {
// prep unique array
$unique=array();
foreach ($pidList as $thispid) {
// check for +ve integer
if ($thispid>0) $unique[$thispid]=$thispid;
}
$newTally=0;
if (count($unique)>0) {
// get next level of pids
$pidQuery='(pid='.implode(' or pid=',array_keys($unique)).')';
$cObj=tx_markablequiz_controller::findCObjectSingleton();
$query=$pidQuery.$cObj->enableFields('pages');
$res=$GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,pid','pages',$query,'','','');
while ($row=$GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
$unique[$row['uid']]=$row['uid'];
$newTally++;
}
// no results, bail on recursion now
if ($newTally>0) {
return
tx_markablequiz_controller::plr(array_keys($unique),$recursive-1);
} else {
return array_keys($unique);
}
} else {
return array();
}
}
}
Steve
More information about the TYPO3-team-extension-coordination
mailing list