/** * Loads a javascript file into memory * @param string $directory * @param array $data * @param string $base Base directory of Javascript. Defaults to null, looking in the main javascript directory. * For a module, base would be 'mod/modulename/' * @param boolean $wrap_header If true, wrap the contents of head.js with <script> tags * @param boolean $wrap_body If true, wrap the contents of body.js with <script> tags * @return unknown_type */ public static function getJavascript($directory, array $data = NULL, $base = NULL, $wrap_header = false, $wrap_body = false) { // previously a choice, now mandated. Leaving this in for backwards // compatibility if (preg_match('/^modules\\//', $directory)) { $directory = preg_replace('@^\\./@', '', $directory); $js_dir = explode('/', $directory); foreach ($js_dir as $key => $dir) { if ($dir == 'modules') { $start_key = $key + 1; break; } } $js = null; $directory = sprintf('mod/%s/javascript/%s', $js_dir[$start_key++], $js_dir[$start_key]); } else { $js = 'javascript/'; } PHPWS_CORE::initCoreClass('File.php'); $headfile = PHPWS_SOURCE_DIR . $base . $js . $directory . '/head.js'; $bodyfile = PHPWS_SOURCE_DIR . $base . $js . $directory . '/body.js'; $defaultfile = PHPWS_SOURCE_DIR . $base . $js . $directory . '/default.php'; if (is_file($defaultfile)) { require $defaultfile; } if (isset($default)) { if (isset($data)) { $data = array_merge($default, $data); } else { $data = $default; } } $data['source_http'] = PHPWS_SOURCE_HTTP; $data['source_dir'] = PHPWS_SOURCE_DIR; $data['home_http'] = PHPWS_Core::getHomeHttp(); $data['home_dir'] = PHPWS_HOME_DIR; try { Layout::loadJavascriptFile($headfile, $directory, $data, $wrap_header); } catch (Exception $e) { trigger_error($e->getMessage(), E_USER_ERROR); } if (is_file($bodyfile)) { if (!empty($data)) { if ($wrap_body) { return '<script type="text/javascript">' . PHPWS_Template::process($data, 'layout', $bodyfile, TRUE) . '</script>'; } else { return PHPWS_Template::process($data, 'layout', $bodyfile, TRUE); } } else { if ($wrap_body) { return '<script type="text/javascript">' . file_get_contents($bodyfile) . '</script>'; } else { return file_get_contents($bodyfile); } } } }