Backend\Core\Engine\Header::addJS PHP Method

addJS() public method

If you don't specify a module, the current one will be used If you set overwritePath to true we expect a full path (It has to start with a /)
public addJS ( string $file, string $module = null, boolean $minify = true, boolean $overwritePath = false, boolean $addTimestamp = false )
$file string The file to load.
$module string The module wherein the file is located.
$minify boolean Should the module be minified?
$overwritePath boolean Should we overwrite the full path?
$addTimestamp boolean May we add a timestamp for caching purposes?
    public function addJS($file, $module = null, $minify = true, $overwritePath = false, $addTimestamp = false)
    {
        $file = (string) $file;
        $module = (string) ($module !== null) ? $module : $this->URL->getModule();
        $minify = (bool) $minify;
        $overwritePath = (bool) $overwritePath;
        $addTimestamp = (bool) $addTimestamp;
        // no minifying when debugging
        if ($this->getContainer()->getParameter('kernel.debug')) {
            $minify = false;
        }
        // is the given path the real path?
        if (!$overwritePath) {
            // we have to build the path, but core is a special one
            if ($module !== 'Core') {
                $file = '/src/Backend/Modules/' . $module . '/Js/' . $file;
            } else {
                // core is special because it isn't a real module
                $file = '/src/Backend/Core/Js/' . $file;
            }
        }
        // try to minify
        if ($minify) {
            $file = $this->minifyJS($file);
        }
        // already in array?
        if (!in_array(array('file' => $file, 'add_timestamp' => $addTimestamp), $this->jsFiles)) {
            // add to files
            $this->jsFiles[] = array('file' => $file, 'add_timestamp' => $addTimestamp);
        }
    }

Usage Example

Example #1
0
 /**
  * Execute the action
  */
 public function execute()
 {
     // add jquery, we will need this in every action, so add it globally
     $this->header->addJS('jquery/jquery.js', 'Core', false);
     $this->header->addJS('jquery/jquery.ui.js', 'Core', false);
     $this->header->addJS('jquery/jquery.ui.dialog.patch.js', 'Core');
     $this->header->addJS('jquery/jquery.tools.js', 'Core', false);
     $this->header->addJS('jquery/jquery.backend.js', 'Core');
     // add items that always need to be loaded
     $this->header->addJS('utils.js', 'Core');
     $this->header->addJS('backend.js', 'Core');
     // add module js
     if (is_file($this->getBackendModulePath() . '/Js/' . $this->getModule() . '.js')) {
         $this->header->addJS($this->getModule() . '.js');
     }
     // add action js
     if (is_file($this->getBackendModulePath() . '/Js/' . $this->getAction() . '.js')) {
         $this->header->addJS($this->getAction() . '.js');
     }
     // add core css files
     $this->header->addCSS('reset.css', 'Core');
     $this->header->addCSS('jquery_ui/fork/jquery_ui.css', 'Core', false, false);
     $this->header->addCSS('screen.css', 'Core');
     $this->header->addCSS('debug.css', 'Core');
     // add module specific css
     if (is_file($this->getBackendModulePath() . '/Layout/Css/' . $this->getModule() . '.css')) {
         $this->header->addCSS($this->getModule() . '.css');
     }
     // store var so we don't have to call this function twice
     $var = array_map('strip_tags', $this->getParameter('var', 'array', array()));
     // is there a report to show?
     if ($this->getParameter('report') !== null) {
         // show the report
         $this->tpl->assign('report', true);
         // camelcase the string
         $messageName = strip_tags(\SpoonFilter::toCamelCase($this->getParameter('report'), '-'));
         // if we have data to use it will be passed as the var parameter
         if (!empty($var)) {
             $this->tpl->assign('reportMessage', vsprintf(BL::msg($messageName), $var));
         } else {
             $this->tpl->assign('reportMessage', BL::msg($messageName));
         }
         // highlight an element with the given id if needed
         if ($this->getParameter('highlight')) {
             $this->tpl->assign('highlight', strip_tags($this->getParameter('highlight')));
         }
     }
     // is there an error to show?
     if ($this->getParameter('error') !== null) {
         // camelcase the string
         $errorName = strip_tags(\SpoonFilter::toCamelCase($this->getParameter('error'), '-'));
         // if we have data to use it will be passed as the var parameter
         if (!empty($var)) {
             $this->tpl->assign('errorMessage', vsprintf(BL::err($errorName), $var));
         } else {
             $this->tpl->assign('errorMessage', BL::err($errorName));
         }
     }
 }