Created
May 20, 2010 14:33
-
-
Save netProphET/407626 to your computer and use it in GitHub Desktop.
This file contains 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 | |
class xPDOSimpleTemplatingObject extends xPDOSimpleObject { | |
function __construct(& $xpdo) { | |
parent :: __construct($xpdo); | |
} | |
/** | |
* Load a collection of xPDOObject instances. | |
* | |
* @static | |
* @param xPDO &$xpdo A valid xPDO instance. | |
* @param string $className Name of the class. | |
* @param string $template Template code | |
* @param string $placeholder_prefix Placeholders in template will be this prefix plus object's field names | |
* @param mixed $criteria A valid primary key, criteria array, or xPDOCriteria instance. | |
* @param boolean|integer $cacheFlag Indicates if the objects should be | |
* cached and optionally, by specifying an integer value, for how many | |
* seconds. | |
* @return array An array of xPDOObject instances or an empty array if no instances are loaded. | |
*/ | |
public static function iterateCollection(xPDO & $xpdo, $className, $template, $placeholder_prefix = '', $criteria= null, $cacheFlag= true) { | |
$fromCache = false; | |
$output = ''; | |
if (!$className= $xpdo->loadClass($className)) return $output; | |
$rows= false; | |
$fromCache= false; | |
$collectionCaching = (integer) $xpdo->getOption(xPDO::OPT_CACHE_DB_COLLECTIONS, array(), 1); | |
if (!is_object($criteria)) { | |
$criteria= $xpdo->getCriteria($className, $criteria, $cacheFlag); | |
} | |
if ($collectionCaching > 0 && $xpdo->_cacheEnabled && $cacheFlag) { | |
$rows= $xpdo->fromCache($criteria); | |
$fromCache = (is_array($rows) && !empty($rows)); | |
} | |
if (!$fromCache && is_object($criteria)) { | |
$rows= xPDOObject :: _loadRows($xpdo, $className, $criteria); | |
} | |
if (is_array ($rows)) { | |
foreach ($rows as $row) { | |
self :: _templateCollectionInstance($xpdo, $output, $className, $template, $placeholder_prefix, $criteria, $row, $fromCache, $cacheFlag); | |
} | |
} elseif (is_object($rows)) { | |
$cacheRows = array(); | |
while ($row = $rows->fetch(PDO::FETCH_ASSOC)) { | |
self :: _templateCollectionInstance($xpdo, $output, $className, $template, $placeholder_prefix, $criteria, $row, $fromCache, $cacheFlag); | |
if ($collectionCaching > 0 && $xpdo->_cacheEnabled && $cacheFlag && !$fromCache) $cacheRows[] = $row; | |
} | |
if ($collectionCaching > 0 && $xpdo->_cacheEnabled && $cacheFlag && !$fromCache) $rows =& $cacheRows; | |
} | |
if (!$fromCache && $xpdo->_cacheEnabled && $collectionCaching > 0 && $cacheFlag && !empty($rows)) { | |
$xpdo->toCache($criteria, $rows, $cacheFlag); | |
} | |
return $output; | |
} | |
protected static function _templateCollectionInstance(xPDO & $xpdo, &$output, $className, $template, $placeholder_prefix = '', $criteria, $row, $fromCache, $cacheFlag=true){ | |
$loaded = false; | |
$search = array(); | |
$replace = array(); | |
if($obj= xPDOObject :: _loadInstance($xpdo, $className, $criteria, $row)) { | |
if (($cacheKey= $obj->getPrimaryKey()) && !$obj->isLazy()) { | |
if (is_array($cacheKey)) { | |
$pkval= implode('-', $cacheKey); | |
} else { | |
$pkval= $cacheKey; | |
} | |
if ($xpdo->getOption(xPDO::OPT_CACHE_DB_COLLECTIONS, array(), 1) == 2 && $xpdo->_cacheEnabled && $cacheFlag) { | |
if (!$fromCache) { | |
$pkCriteria = $xpdo->newQuery($className, $cacheKey, $cacheFlag); | |
$xpdo->toCache($pkCriteria, $obj, $cacheFlag); | |
} else { | |
$obj->_cacheFlag= true; | |
} | |
} | |
$loaded = true; | |
} else { | |
$loaded = true; | |
} | |
} | |
if($loaded) { | |
$vals = $obj->toArray(); | |
foreach($vals as $k=>$v) { | |
$search[] = '[[+' . $placeholder_prefix . $k . ']]'; | |
$replace[] = $v; | |
unset($vals[$k]); | |
} | |
$output .= str_replace($search, $replace, $template); | |
} | |
unset($obj); | |
return $loaded; | |
} | |
} | |
class Country extends xPDOSimpleTemplatingObject { | |
function __construct(& $xpdo) { | |
parent :: __construct($xpdo); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment