This is the API documentation for concrete5 version 5.6 and earlier. View Current API Documentation
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 
<?php
defined('C5_EXECUTE') or die("Access Denied.");

/**
 * @package Blocks
 * @category Concrete
 * @author Andrew Embler <[email protected]>
 * @copyright  Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 *
 */

/**
 * An object that represents a block's template, whether it's built-in, or custom.
 *
 * @package Blocks
 * @category Concrete
 * @author Andrew Embler <[email protected]>
 * @category Concrete
 * @copyright  Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 *
 */

class Concrete5_Library_BlockViewTemplate {

    protected $basePath = '';
    
    protected $bFilename;
    protected $btHandle;
    protected $obj;
    protected $baseURL;
    protected $checkHeaderItems = true;
    protected $itemsToCheck = array(
        'CSS' => 'view.css', 
        'JAVASCRIPT' => 'view.js'
    );
    protected $render = FILENAME_BLOCK_VIEW;
    
    public function __construct($obj) {
        $this->btHandle = $obj->getBlockTypeHandle();
        $this->obj = $obj;
        if ($obj instanceof Block) {
            $this->bFilename = $obj->getBlockFilename();
        }
        $this->computeView();
    }
    
    protected function computeView() {
        $bFilename = $this->bFilename;
        $obj = $this->obj;
        
        // if we've passed in "templates/" as the first part, we strip that off.
        if (strpos($bFilename, 'templates/') === 0) {
            $bFilename = substr($bFilename, 10);
        }

        // The filename might be a directory name with .php-appended (BlockView does that), strip it.
        $bFilenameWithoutDotPhp = $bFilename;
        if ( substr( $bFilename, -4 ) === ".php" ) {
            $bFilenameWithoutDotPhp = substr( $bFilename, 0, strlen( $bFilename ) -4 );
        }

        if ($bFilename) {
            if (is_file(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename)) {
                $template = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
                $bv = new BlockView();
                $bv->setBlockObject($obj);
                $this->baseURL = $bv->getBlockURL($this->render);
                $this->basePath = $bv->getBlockPath($this->render);
            } else if (is_file(DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename)) {
                $template = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
                $this->baseURL = ASSETS_URL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
                $this->basePath = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle();
            } else if (is_dir(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename)) {
                $template = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename . '/' . $this->render;
                $this->basePath = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
                $this->baseURL = DIR_REL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
            } else if (is_dir(DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename)) {
                $template = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename . '/'  . $this->render;
                $this->basePath = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
                $this->baseURL = ASSETS_URL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
            } else if ( $bFilename !== $bFilenameWithoutDotPhp ) {
                if (is_dir(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp)) {
                    $template = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp . '/' . $this->render;
                    $this->basePath = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp;
                    $this->baseURL = DIR_REL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp;
                } else if (is_dir(DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp)) {
                    $template = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp . '/'  . $this->render;
                    $this->basePath = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp;
                    $this->baseURL = ASSETS_URL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp;
                }
            }

            // we check all installed packages
            if (!isset($template)) {
                $pl = PackageList::get();
                $packages = $pl->getPackages();
                foreach($packages as $pkg) {
                    $d = '';
                    if (is_dir(DIR_PACKAGES . '/' . $pkg->getPackageHandle())) {
                        $d = DIR_PACKAGES . '/'. $pkg->getPackageHandle();
                    } else if (is_dir(DIR_PACKAGES_CORE . '/'. $pkg->getPackageHandle())) {
                        $d = DIR_PACKAGES_CORE . '/'. $pkg->getPackageHandle();
                    }
                    
                    if ($d != '') {
                        $baseStub = (is_dir(DIR_PACKAGES . '/' . $pkg->getPackageHandle())) ? DIR_REL . '/' . DIRNAME_PACKAGES . '/'. $pkg->getPackageHandle() : ASSETS_URL . '/'. DIRNAME_PACKAGES . '/' . $pkg->getPackageHandle();
                        
                        if (is_file($d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . $bFilename)) {
                            $template = $d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . $bFilename;
                            $this->baseURL = ASSETS_URL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
                            $this->basePath = DIR_FILES_BLOCK_TYPES_CORE . '/' . $obj->getBlockTypeHandle();
                        } else if (is_file($d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename)) {
                            $template = $d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
                            $this->baseURL = $baseStub . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
                            $this->basePath = $d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
                        } else if (is_dir($d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp)) {
                            $template = $d . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilenameWithoutDotPhp . '/' . $this->render;
                            $this->baseURL = $baseStub . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle() . '/' . DIRNAME_BLOCK_TEMPLATES . '/' . $bFilename;
                        }
                    }
                    
                    if ($this->baseURL != '') {
                        continue;
                    }
                    
                }
            }
            
        } else if (file_exists(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '.php')) {
            $template = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '.php';
            $bv = new BlockView();
            $bv->setBlockObject($obj);
            $this->baseURL = $bv->getBlockURL($this->render);
            $this->basePath = $bv->getBlockPath($this->render);
        } else if (file_exists(DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . $this->render)) {
            $template = DIR_FILES_BLOCK_TYPES . '/' . $obj->getBlockTypeHandle() . '/' . $this->render;
            $this->baseURL = DIR_REL . '/' . DIRNAME_BLOCKS . '/' . $obj->getBlockTypeHandle();
        }
        
        if (!isset($template)) {
            $bv = new BlockView();
            $bv->setBlockObject($obj);
            $template = $bv->getBlockPath($this->render) . '/' . $this->render;
            $this->baseURL = $bv->getBlockURL($this->render);
        }
        
        if ($this->basePath == '') {
            $this->basePath = dirname($template);
        }
        $this->template = $template;
    }
    
    
    public function getBasePath() {return $this->basePath;}
    public function getBaseURL() {return $this->baseURL;}
    public function setBlockCustomTemplate($bFilename) {
        $this->bFilename = $bFilename;
        $this->computeView();
    }
    
    public function setBlockCustomRender($renderFilename) {
        // if we've passed in "templates/" as the first part, we strip that off.
        if (strpos($renderFilename, 'templates/') === 0) {
            $bFilename = substr($renderFilename, 10);
            $this->setBlockCustomTemplate($bFilename);
        } else {
            $this->render = $renderFilename;
        }
        $this->computeView();
    }
    
    
    public function getTemplate() {
        return $this->template;
    }
    
    public function getTemplateHeaderItems() {
        $items = array();
        $h = Loader::helper("html");
        $dh = Loader::helper('file');
        if ($this->checkHeaderItems == false) {
            return $items;
        } else {
            foreach($this->itemsToCheck as $t => $i) {
                if (file_exists($this->basePath . '/' . $i)) {
                    switch($t) {
                        case 'CSS':
                            $items[] = $h->css($this->getBaseURL() . '/' . $i);
                            break;
                        case 'JAVASCRIPT':
                            $items[] = $h->javascript($this->getBaseURL() . '/' . $i);
                            break;
                    }
                }
            }
            $css = $dh->getDirectoryContents($this->basePath . '/' . DIRNAME_CSS);
            $js = $dh->getDirectoryContents($this->basePath . '/' . DIRNAME_JAVASCRIPT);
            if (count($css) > 0) {
                foreach($css as $i) {
                    if(substr($i,-4)=='.css') {
                        $items[] = $h->css($this->getBaseURL() . '/' . DIRNAME_CSS . '/' . $i);
                    }
                }
            }
            if (count($js) > 0) {
                foreach($js as $i) {
                    if (substr($i,-3)=='.js') {
                        $items[] = $h->javascript($this->getBaseURL() . '/' . DIRNAME_JAVASCRIPT . '/' . $i);
                    }
                }
            }
            return $items;
        }
    }



}