javascript($file); } /** * Add the javascript code to the header of the page * * See {@link Requirements_Backend::customScript()} for more info * @param script The script content * @param uniquenessID Use this to ensure that pieces of code only get added once. */ static function customScript($script, $uniquenessID = null) { self::backend()->customScript($script, $uniquenessID); } /** * Add the CSS styling to the header of the page * * See {@link Requirements_Backend::customCSS()} */ static function customCSS($script, $uniquenessID = null) { self::backend()->custom($script, $uniquenessID); } /** * Add the following custom code to the
section of the page. * See {@link Requirements_Backend::insertHeadTags()} * * @param string $html * @param string $uniquenessID */ static function insertHeadTags($html, $uniquenessID = null) { self::backend()->insertHeadTags($html, $uniquenessID); } /** * Load the given javascript template with the page. * See {@link Requirements_Backend::javascriptTemplate()} * * @param file The template file to load. * @param vars The array of variables to load. These variables are loaded via string search & replace. */ static function javascriptTemplate($file, $vars, $uniquenessID = null) { self::backend()->javascriptTemplate($file, $vars, $uniquenessID); } /** * Register the given stylesheet file as required. * See {@link Requirements_Backend::css()} * * @param $file String Filenames should be relative to the base, eg, 'jsparty/tree/tree.css' * @param $media String Comma-separated list of media-types (e.g. "screen,projector") * @see http://www.w3.org/TR/REC-CSS2/media.html */ static function css($file, $media = null) { self::backend()->css($file, $media); } /** * Register the given "themeable stylesheet" as required. See {@link Requirements_Backend::themedCSS()} * * @param $name String The identifier of the file. For example, css/MyFile.css would have the identifier "MyFile" * @param $media String Comma-separated list of media-types (e.g. "screen,projector") */ static function themedCSS($name, $media = null) { return self::backend()->themedCSS($name, $media); } /** * Clear either a single or all requirements. * Caution: Clearing single rules works only with customCSS and customScript if you specified a {@uniquenessID}. * * See {@link Requirements_Backend::clear()} * * @param $file String */ static function clear($fileOrID = null) { self::backend()->clear($fileOrID); } /** * Blocks inclusion of a specific file * See {@link Requirements_Backend::block()} * * @param unknown_type $fileOrID */ static function block($fileOrID) { self::backend()->block($fileOrID); } /** * Removes an item from the blocking-list. * See {@link Requirements_Backend::unblock()} * * @param string $fileOrID */ static function unblock($fileOrID) { self::backend()->unblock($fileOrID); } /** * Removes all items from the blocking-list. * See {@link Requirements_Backend::unblock_all()} */ static function unblock_all() { self::backend()->unblock_all(); } /** * Restore requirements cleared by call to Requirements::clear * See {@link Requirements_Backend::restore()} */ static function restore() { self::backend()->restore(); } /** * Update the given HTML content with the appropriate include tags for the registered * requirements. * See {@link Requirements_Backend::includeInHTML()} for more information. * * @param string $templateFilePath Absolute path for the *.ss template file * @param string $content HTML content that has already been parsed from the $templateFilePath through {@link SSViewer}. * @return string HTML content thats augumented with the requirements before the closing tag. */ static function includeInHTML($templateFile, $content) { return self::backend()->includeInHTML($templateFile, $content); } static function include_in_response(HTTPResponse $response) { return self::backend()->include_in_response($response); } /** * Automatically includes the necessary lang-files from the module. * * See {@link Requirements_Backend::process_i18n_javascript()} for more info. */ protected static function process_i18n_javascript() { return self::backend()->process_i18n_javascript(); } /** * Concatenate several css or javascript files into a single dynamically generated file. * See {@link Requirements_Backend::combine_files()} for more info. * * @param string $combinedFileName * @param array $files */ static function combine_files($combinedFileName, $files) { self::backend()->combine_files($combinedFileName, $files); } /** * Returns all combined files. * See {@link Requirements_Backend::get_combine_files()} * * @return array */ static function get_combine_files() { return self::backend()->get_combine_files(); } /** * Deletes all dynamically generated combined files from the filesystem. * See {@link Requirements_Backend::delete_combine_files()} * * @param string $combinedFileName If left blank, all combined files are deleted. */ static function delete_combined_files($combinedFileName = null) { return self::backend()->delete_combined_files($combinedFileName); } /** * Re-sets the combined files definition. See {@link Requirements_Backend::clear_combined_files()} */ static function clear_combined_files() { self::backend()->clear_combined_files(); } /** * See {@link combine_files()}. */ static function process_combined_files() { return self::backend()->process_combined_files(); } /** * Returns all custom scripts * See {@link Requirements_Backend::get_custom_scripts()} * * @return array */ static function get_custom_scripts() { return self::backend()->get_custom_scripts(); } /** * Set whether you want to write the JS to the body of the page or * in the head section * * @see {@link Requirements_Backend::set_write_js_to_body()} * @param boolean */ static function set_write_js_to_body($var) { self::backend()->set_write_js_to_body($var); } static function debug() { return self::backend()->debug(); } } class Requirements_Backend { /** * Paths to all required .js files relative to the webroot. * * @var array $javascript */ protected $javascript = array(); /** * Paths to all required .css files relative to the webroot. * * @var array $css */ protected $css = array(); /** * All custom javascript code that is inserted * directly at the bottom of the HTML tag. * * @var array $customScript */ protected $customScript = array(); /** * All custom CSS rules which are inserted * directly at the bottom of the HTML tag. * * @var array $customCSS */ protected $customCSS = array(); /** * All custom HTML markup which is added before * the closing tag, e.g. additional metatags. * This is preferred to entering tags directly into */ protected $customHeadTags = array(); /** * Remembers the filepaths of all cleared Requirements * through {@link clear()}. * * @usedby {@link restore()} * * @var array $disabled */ protected $disabled = array(); /** * The filepaths (relative to webroot) or * uniquenessIDs of any included requirements * which should be blocked when executing {@link inlcudeInHTML()}. * This is useful to e.g. prevent core classes to modifying * Requirements without subclassing the entire functionality. * Use {@link unblock()} or {@link unblock_all()} to revert changes. * * @var array $blocked */ protected $blocked = array(); /** * See {@link combine_files()}. * * @var array $combine_files */ public $combine_files = array(); /** * Using the JSMin library to minify any * javascript file passed to {@link combine_files()}. * * @var boolean */ public $combine_js_with_jsmin = true; /** * Put all javascript includes at the bottom of the template * before the closing tag instead of the tag. * This means script downloads won't block other HTTP-requests, * which can be a performance improvement. * Caution: Doesn't work when modifying the DOM from those external * scripts without listening to window.onload/document.ready * (e.g. toplevel document.write() calls). * * @see http://developer.yahoo.com/performance/rules.html#js_bottom * * @var boolean */ public $write_js_to_body = true; /** * Set whether you want the files written to the head or the body. It * writes to the body by default which can break some scripts * * @param boolean */ public function set_write_js_to_body($var) { $this->write_js_to_body = $var; } /** * Register the given javascript file as required. * Filenames should be relative to the base, eg, 'sapphire/javascript/loader.js' */ public function javascript($file) { $this->javascript[$file] = true; } /** * Returns an array of all included javascript * * @return array */ public function get_javascript() { return array_keys(array_diff_key($this->javascript,$this->blocked)); } /** * Add the javascript code to the header of the page * @todo Make Requirements automatically put this into a separate file :-) * @param script The script content * @param uniquenessID Use this to ensure that pieces of code only get added once. */ public function customScript($script, $uniquenessID = null) { if($uniquenessID) $this->customScript[$uniquenessID] = $script; else { $this->customScript[] = $script; } $script .= "\n"; } function customCSS($script, $uniquenessID = null) { if($uniquenessID) $this->customCSS[$uniquenessID] = $script; else { $this->customCSS[] = $script; } } /** * Add the following custom code to the section of the page. * * @param string $html * @param string $uniquenessID */ function insertHeadTags($html, $uniquenessID = null) { if($uniquenessID) $this->customHeadTags[$uniquenessID] = $html; else { $this->customHeadTags[] = $html; } } /** * Load the given javascript template with the page. * @param file The template file to load. * @param vars The array of variables to load. These variables are loaded via string search & replace. */ function javascriptTemplate($file, $vars, $uniquenessID = null) { $script = file_get_contents(Director::getAbsFile($file)); foreach($vars as $k => $v) { $search[] = '$' . $k; $replace[] = str_replace("\\'","'", Convert::raw2js($v)); } $script = str_replace($search, $replace, $script); $this->customScript($script, $uniquenessID); } /** * Register the given stylesheet file as required. * * @param $file String Filenames should be relative to the base, eg, 'jsparty/tree/tree.css' * @param $media String Comma-separated list of media-types (e.g. "screen,projector") * @see http://www.w3.org/TR/REC-CSS2/media.html */ function css($file, $media = null) { $this->css[$file] = array( "media" => $media ); } function get_css() { return array_diff_key($this->css,$this->blocked); } /** * Needed to actively prevent the inclusion of a file, * e.g. when using your own prototype.js. * Blocking should only be used as an exception, because * it is hard to trace back. You can just block items with an * ID, so make sure you add an unique identifier to customCSS() and customScript(). * * @param string $fileOrID */ function block($fileOrID) { $this->blocked[$fileOrID] = $fileOrID; } /** * Clear either a single or all requirements. * Caution: Clearing single rules works only with customCSS and customScript if you specified a {@uniquenessID}. * * @param $file String */ function clear($fileOrID = null) { if($fileOrID) { foreach(array('javascript','css', 'customScript', 'customCSS') as $type) { if(isset($this->{$type}[$fileOrID])) { $this->disabled[$type][$fileOrID] = $this->{$type}[$fileOrID]; unset($this->{$type}[$fileOrID]); } } } else { $this->disabled['javascript'] = $this->javascript; $this->disabled['css'] = $this->css; $this->disabled['customScript'] = $this->customScript; $this->disabled['customCSS'] = $this->customCSS; $this->javascript = array(); $this->css = array(); $this->customScript = array(); $this->customCSS = array(); $this->customHeadTags = array(); } } /** * Removes an item from the blocking-list. * CAUTION: Does not "re-add" any previously blocked elements. * @param string $fileOrID */ function unblock($fileOrID) { if(isset($this->blocked[$fileOrID])) unset($this->blocked[$fileOrID]); } /** * Removes all items from the blocking-list. */ static function unblock_all() { self::backend()->blocked = array(); } /** * Restore requirements cleared by call to Requirements::clear */ function restore() { $this->javascript = $this->disabled['javascript']; $this->css = $this->disabled['css']; $this->customScript = $this->disabled['customScript']; $this->customCSS = $this->disabled['customCSS']; } /** * Update the given HTML content with the appropriate include tags for the registered * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, * including a tag. The requirements will insert before the closing tag automatically. * * @todo Calculate $prefix properly * * @param string $templateFilePath Absolute path for the *.ss template file * @param string $content HTML content that has already been parsed from the $templateFilePath through {@link SSViewer}. * @return string HTML content thats augumented with the requirements before the closing tag. */ function includeInHTML($templateFile, $content) { if(isset($_GET['debug_profile'])) Profiler::mark("Requirements::includeInHTML"); if(strpos($content, 'javascript || $this->css || $this->customScript || $this->customHeadTags)) { $requirements = ''; $jsRequirements = ''; // Combine files - updates $this->javascript and $this->css $this->process_i18n_javascript(); $this->process_combined_files(); foreach(array_diff_key($this->javascript,$this->blocked) as $file => $dummy) { $path = self::path_for_file($file); if($path) { $jsRequirements .= "\n"; } } // add all inline javascript *after* including external files which // they might rely on if($this->customScript) { foreach(array_diff_key($this->customScript,$this->blocked) as $script) { $jsRequirements .= "\n"; } } foreach(array_diff_key($this->css,$this->blocked) as $file => $params) { $path = self::path_for_file($file); if($path) { $media = (isset($params['media']) && !empty($params['media'])) ? " media=\"{$params['media']}\"" : ""; $requirements .= "\n"; } } foreach(array_diff_key($this->customCSS,$this->blocked) as $css) { $requirements .= "\n"; } foreach(array_diff_key($this->customHeadTags,$this->blocked) as $customHeadTag) { $requirements .= "$customHeadTag\n"; } if($this->write_js_to_body) { // Remove all newlines from code to preserve layout $jsRequirements = preg_replace('/>\n*/', '>', $jsRequirements); // We put script tags into the body, for performance. // If your template already has script tags in the body, then we put our script tags at the top of the body. // Otherwise, we put it at the bottom. $p1 = strripos($content, '