All pastes #334755 Raw Edit

Unnamed

public php v1 · immutable
#334755 ·published 2007-01-31 21:08 UTC
rendered paste body
class modResource extends xPDOSimpleObject {    var $content= '';    var $output= '';    var $contextKey= null;    var $processed= false;    var $template= null;    var $cacheFileName= null;    var $refreshCache= true;    function modResource(& $xpdo) {        $this->__construct($xpdo);    }    function __construct(& $xpdo) {        parent :: __construct($xpdo);        $this->contextKey= $this->xpdo->context->get('key');        $this->cacheFileName= "[contextKey]/resources/[id].cache.php";    }        /**     * Processes the resource.     */    function process() {        if ($this->processed && $this->content && $this->get('cacheable')) {            return $this->content;        }        if ($baseElement= $this->getOne('modTemplate', '', null, true)) {            $this->xpdo->getParser();            if ($baseElement->process()) {                $this->content= $baseElement->output;                $this->processed= true;            }        } else {            $this->content= '[[*content]]';        }        return $this->content;    }    /**     * Gets the cache file name for the resource.     */    function getCacheFileName() {        if ($this->get('id') && strpos($this->cacheFileName, '[') !== false) {            $this->cacheFileName= str_replace('[contextKey]', $this->contextKey, $this->cacheFileName);            $this->cacheFileName= str_replace('[id]', $this->get('id'), $this->cacheFileName);        }        return $this->cacheFileName;    }        function getOne($class, $key= '', $criteria= null) {        if ($criteria === null && ($class === 'modWebUser' || $class === 'modManagerUser' || $class === 'modUser') && $key) {            if ($userid= intval($this->get($key))) {                if ($userid < 0) {                    $class= 'modWebUser';                    $userid= abs($userid);                    $webUserTable= $this->xpdo->getTableName('modWebUser');                    $sql= "SELECT * FROM {$webUserTable} WHERE `id` = :user_id LIMIT 1";                    $bindings= array(                        ':user_id' => array ('value' => $userid, 'type' => PDO_PARAM_INT)                    );                    $criteria= new xPDOCriteria($this->xpdo, $sql, $bindings);                }                elseif ($userid > 0) {                    $class= 'modManagerUser';                    $mgrUserTable= $this->xpdo->getTableName('modManagerUser');                    $sql= "SELECT * FROM {$mgrUserTable} WHERE `id` = :user_id LIMIT 1";                    $bindings= array(                        ':user_id' => array ('value' => $userid, 'type' => PDO_PARAM_INT)                    );                    $criteria= new xPDOCriteria($this->xpdo, $sql, $bindings);                }            }        }        return parent :: getOne($class, $key, $criteria);    }    function getMany($class, $fk= '', $criteria= null, $cacheFlag= false) {        if ($class === 'modTemplateVar' && ($criteria === null || strtolower($criteria) === 'all')) {            $tvTable= $this->xpdo->getTableName('modTemplateVar');            $tvtTable= $this->xpdo->getTableName('modTemplateVarTemplate');            $tvdTable= $this->xpdo->getTableName('modTemplateVarDocument');            $tvColumns= $this->xpdo->getSelectColumns('modTemplateVar', 'tv', 'tv_');            $tvdColumns= $this->xpdo->getSelectColumns('modTemplateVarDocument', 'tvd', 'tvd_');            $sql= "SELECT {$tvColumns}, {$tvdColumns} FROM {$tvTable} tv ";            $sql .= "INNER JOIN {$tvtTable} tvtpl ON tvtpl.templateid = :template_id AND tvtpl.tmplvarid = tv.id ";            $sql .= "LEFT JOIN {$tvdTable} tvd ON tvd.tmplvarid=tv.id AND tvd.contentid = :content_id ";                        if (strtolower($criteria) === 'all') {                $sql .= "ORDER BY tv.rank ";            }            elseif ($criteria === null) {                 $fk= 'id';                $docgrp= false;                if ($this->xpdo->isFrontend() && isset ($_SESSION['webDocgroups'])) {                    $docgrp= implode(",", $_SESSION['webDocgroups']);                }                elseif ($this->xpdo->isBackend() && $_SESSION['mgrDocgroups']) {                    $docgrp= implode(",", $_SESSION['mgrDocgroups']);                }                    $tvaTable= $this->xpdo->getTableName('modTemplateVarDocumentGroup');                $sql .= "LEFT JOIN {$tvaTable} tva ON tva.tmplvarid=tv.id ";                $sql .= "WHERE (1='" . $_SESSION['mgrRole'] . "' OR ISNULL(tva.documentgroup)" . ((!$docgrp) ? "" : " OR tva.documentgroup IN ($docgrp)") . ") ORDER BY tv.rank";            }            $collection= null;            if ($stmt= $this->xpdo->prepare($sql)) {                $stmt->bindValue(':template_id', $this->get('template'), PDO_PARAM_INT);                $stmt->bindValue(':content_id', $this->get('id'), PDO_PARAM_INT);            }            if ($stmt && $stmt->execute()) {                while ($row= $stmt->fetch(PDO_FETCH_ASSOC)) {                    $tv= $this->xpdo->newObject('modTemplateVar');                    $tv->fromArray($row, 'tv_', true);                    $tv->_new= false;                    $tv->_dirty= array ();                    if (isset ($row['tvd_tmplvarid']) && $row['tvd_tmplvarid'] != null) {                        $tvd= $this->xpdo->newObject('modTemplateVarDocument');                        $tvd->fromArray($row, 'tvd_', true);                        $tvd->_new= false;                        $tvd->_dirty= array ();                    }                    $collection[$tv->getPrimaryKey()]= $tv;                }            }            return $collection;        }        return parent :: getMany($class, $fk, $criteria);    }    function set($k, $v= null) {        if ($k === 'alias') {            $v= $this->cleanAlias($v);        }        return parent :: set($k, $v);    }    function cleanAlias($alias) {        if (!isset ($this->xpdo->config['default_charset']) || strtoupper($this->xpdo->config['default_charset']) == 'UTF-8') {            $alias= utf8_decode($alias);        }        $alias= strtr($alias, array (chr(196) => 'Ae', chr(214) => 'Oe', chr(220) => 'Ue', chr(228) => 'ae', chr(246) => 'oe', chr(252) => 'ue', chr(223) => 'ss'));        $alias= strip_tags($alias);        //$alias = strtolower($alias);         $alias= preg_replace('/&.+?;/', '', $alias); // kill entities         $alias= preg_replace('/[^\.%A-Za-z0-9 _-]/', '', $alias);        $alias= preg_replace('/\s+/', '-', $alias);        $alias= preg_replace('|-+|', '-', $alias);        $alias= trim($alias);        return $alias;    }}