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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 
<?php
/**
 * @package Helpers
 * @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
 */

/**
 * Functions to help with using HTML. Does not include form elements - those have their own helper. 
 * @package Helpers
 * @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
 */

defined('C5_EXECUTE') or die("Access Denied.");
class Concrete5_Helper_Html {
    
    protected $legacyJavascript = array(
        'ccm.dialog.js' => 'ccm.app.js',
        'jquery.metadata.js' => 'ccm.app.js',
        'ccm.themes.js' => 'ccm.app.js',
        'ccm.filemanager.js' => 'ccm.app.js',
        /*'jquery.rating.js' => 'ccm.app.js',*/
        'jquery.colorpicker.js' => 'ccm.app.js',
        'jquery.liveupdate.js' => 'ccm.app.js',
        'ccm.ui.js' => 'ccm.app.js',
        'ccm.search.js' => 'ccm.app.js'
    );
    protected $legacyCSS = array(
        'ccm.dialog.css' => 'ccm.app.css',
        'ccm.ui.css' => 'ccm.app.css',
        'ccm.forms.css' => 'ccm.app.css',
        'ccm.menus.css' => 'ccm.app.css',
        'ccm.search.css' => 'ccm.app.css',
        'ccm.filemanager.css' => 'ccm.app.css',
        'ccm.calendar.css' => 'ccm.app.css'
    );

    /** 
     * Includes a CSS file. This function looks in several places. 
     * First, if the item is either a path or a URL it just returns the link to that item (as XHTML-formatted style tag.) 
     * Then it checks the currently active theme, then if a package is specified it checks there. Otherwise if nothing is found it
     * fires off a request to the relative directory CSS directory. If nothing is there, then it checks to the assets directories
     *
     * @param string $file name of css file
     * @param string $pkgHandle handle of the package that the css file is located in (if applicable)
     * @param array $uniqueItemHandle contains two elements: 'handle' and 'version' (both strings) -- helps prevent duplicate output of the same css file (in View::addHeaderItem() and View::addFooterItem()).
     * @return CSSOutputObject
     */
    public function css($file, $pkgHandle = null, $uniqueItemHandle = array()) {

        $css = new CSSOutputObject($uniqueItemHandle);

        // if the first character is a / then that means we just go right through, it's a direct path
        if (substr($file, 0, 1) == '/' || substr($file, 0, 4) == 'http' || strpos($file, DISPATCHER_FILENAME) > -1) {
            $css->compress = false;
            $css->file = $file;
        }
        
        $v = View::getInstance();
        // checking the theme directory for it. It's just in the root.
        if ($v->getThemeDirectory() != '' && file_exists($v->getThemeDirectory() . '/' . $file)) {
            $css->file = $v->getThemePath() . '/' . $file;
        } else if (file_exists(DIR_BASE . '/' . DIRNAME_CSS . '/' . $file)) {
            $css->file = DIR_REL . '/' . DIRNAME_CSS . '/' . $file;
        } else if ($pkgHandle != null) {
            if (file_exists(DIR_BASE . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_CSS . '/' . $file)) {
                $css->file = DIR_REL . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_CSS . '/' . $file;
            } else if (file_exists(DIR_BASE_CORE . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_CSS . '/' . $file)) {
                $css->file = ASSETS_URL . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_CSS . '/' . $file;
            }
        }
            
        if ($css->file == '') {
            if (isset($this->legacyCSS[$file])) {
                $file = $this->legacyCSS[$file];
            }
            $css->file = ASSETS_URL_CSS . '/' . $file;
        }

        // for the javascript addHeaderItem we need to have a full href available
        $css->href = $css->file;
        if (substr($css->file, 0, 4) != 'http') {
            $css->href = BASE_URL . $css->file;
        }
        return $css;
    }
    
    /** 
     * Includes a JavaScript file. This function looks in several places. 
     * First, if the item is either a path or a URL it just returns the link to that item (as XHTML-formatted script tag.) 
     * If a package is specified it checks there. Otherwise if nothing is found it
     * fires off a request to the relative directory JavaScript directory.
     *
     * @param string $file name of javascript file
     * @param string $pkgHandle handle of the package that the javascript file is located in (if applicable)
     * @param array $uniqueItemHandle contains two elements: 'handle' and 'version' (both strings) -- helps prevent duplicate output of the same javascript file (in View::addHeaderItem() and View::addFooterItem()).
     * @return JavaScriptOutputObject
     */
    public function javascript($file, $pkgHandle = null, $uniqueItemHandle = array()) {

        $js = new JavaScriptOutputObject($uniqueItemHandle);
        
        if (substr($file, 0, 1) == '/' || substr($file, 0, 4) == 'http' || strpos($file, DISPATCHER_FILENAME) > -1) {
            $js->compress = false;
            $js->file = $file;
        }

        if (file_exists(DIR_BASE . '/' . DIRNAME_JAVASCRIPT . '/' . $file)) {
            $js->file = DIR_REL . '/' . DIRNAME_JAVASCRIPT . '/' . $file;
        } else if ($pkgHandle != null) {
            if (file_exists(DIR_BASE . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_JAVASCRIPT . '/' . $file)) {
                $js->file = DIR_REL . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_JAVASCRIPT . '/' . $file;
            } else if (file_exists(DIR_BASE_CORE . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_JAVASCRIPT . '/' . $file)) {
                $js->file = ASSETS_URL . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_JAVASCRIPT . '/'. $file;
            }
        }
            
        if ($js->file == '') {
            if (isset($this->legacyJavascript[$file])) {
                $file = $this->legacyJavascript[$file];
            }
            $js->file = ASSETS_URL_JAVASCRIPT . '/' . $file;
        }

        // for the javascript addHeaderItem we need to have a full href available
        $js->href = $js->file;
        return $js;
    }
    
    
    /** 
     * Includes a JavaScript inline script.
     *
     * @param string $script javascript code (not including the surrounding <script> tags)
     * @param array $uniqueItemHandle contains two elements: 'handle' and 'version' (both strings) -- helps prevent duplicate output of the same script (in View::addHeaderItem() and View::addFooterItem()).
     * @return InlineScriptOutputObject
     */
    public function script($script, $uniqueItemHandle = array()) {
        $js = new InlineScriptOutputObject($uniqueItemHandle);
        $js->script = $script;
        return $js;
    }
    
    
    /** 
     * Includes an image file when given a src, width and height. Optional attribs array specifies style, other properties.
     * First checks the PATH off the root of the site
     * Then checks the PATH off the images directory at the root of the site.
     * @param string $src
     * @param int $width
     * @param int $height
     * @param array $attribs
     * @return string $html
     */
    public function image($src, $width = false, $height = false, $attribs = null) {
        $image = parse_url($src);
        $attribsStr = '';
        
        if (is_array($width) && $height == false) {
            $attribs = $width;
            $width = false;
        }
        
        if (is_array($attribs)) {
            foreach($attribs as $key => $at) {
                $attribsStr .= " {$key}=\"{$at}\" ";
            }
        }
        
        if ($width == false && $height == false && (!isset($image['scheme']))) {
            // if our file is not local we DON'T do getimagesize() on it. too slow
            $v = View::getInstance();
            if ($v->getThemeDirectory() != '' && file_exists($v->getThemeDirectory() . '/' . DIRNAME_IMAGES . '/' . $src)) {
                $s = getimagesize($v->getThemeDirectory() . '/' . DIRNAME_IMAGES . '/' . $src);
                $width = $s[0];
                $height = $s[1];
                $src = $v->getThemePath() . '/' . DIRNAME_IMAGES . '/' . $src;
            } else if (file_exists(DIR_BASE . '/' . $src)) {
                $s = getimagesize(DIR_BASE . '/' . $src);
                $width = $s[0];
                $height = $s[1];
            } else if (file_exists(DIR_BASE . '/' . DIRNAME_IMAGES . '/' . $src)) {
                $s = getimagesize(DIR_BASE . '/'  . DIRNAME_IMAGES . '/' . $src);
                $width = $s[0];
                $height = $s[1];
                $src = DIR_REL . '/' . DIRNAME_IMAGES . '/' . $src;
            } else if (file_exists(DIR_BASE_CORE . '/' . DIRNAME_IMAGES . '/' . $src)) {
                $s = getimagesize(DIR_BASE_CORE . '/'  . DIRNAME_IMAGES . '/' . $src);
                $width = $s[0];
                $height = $s[1];
                $src = ASSETS_URL_IMAGES . '/' . $src;
            }
        }
        
        if ($width > 0) {
            $str = '<img src="' . $src . '" width="' . $width . '" border="0" height="' . $height . '" ' . $attribsStr . ' />';
        } else {
            $str = '<img src="' . $src . '" border="0" ' . $attribsStr . ' />';
        }
        return $str;
    }   
    
    
}

class Concrete5_Helper_Html_HeaderOutputObject {

    public $file = '';
    public $href = '';
    public $script = '';
    public $compress = true;
    public $handle = array(); //optional 'handle' and 'version' that the View class can use to avoid duplicate output of conflicting js/css files
    
    /**
     * @param optional: pass in handle and version (as array) or just handle (as string; version will be '0')
     *                  to avoid duplicate output of the same js/css items from other blocks/theme code.
     */
    public function __construct($uniqueItemHandle = array()) {
        if (is_array($uniqueItemHandle) && array_key_exists('handle', $uniqueItemHandle) && !empty($uniqueItemHandle['handle'])) {
            $this->handle = array(
                'handle' => $uniqueItemHandle['handle'],
                'version' => array_key_exists('version', $uniqueItemHandle) ? $uniqueItemHandle['version'] : '0',
            );
        } else if (is_string($uniqueItemHandle) && !empty($uniqueItemHandle)) {
            $this->handle = array(
                'handle' => $uniqueItemHandle,
                'version' => '0',
            );
        }
    }

}

class Concrete5_Helper_Html_JavascriptOutputObject extends Concrete5_Helper_Html_HeaderOutputObject {
    public function __toString() {
        return '<script type="text/javascript" src="' . $this->file . '"></script>';
    }
    
}

class Concrete5_Helper_Html_InlinescriptOutputObject extends Concrete5_Helper_Html_HeaderOutputObject {

  public function __toString() {
    return '<script type="text/javascript">/*<![CDATA[*/'. $this->script .'/*]]>*/</script>';
  }
  
}

class Concrete5_Helper_Html_CSSOutputObject extends Concrete5_Helper_Html_HeaderOutputObject {

    public function __toString() {
        return '<link rel="stylesheet" type="text/css" href="' . $this->file . '" />';
    }
    
}