00001 <?php
00002
00020
00021
00022
00023 class Help {
00025 var $mappingFiles;
00026
00030 function &getHelp() {
00031 $instance =& Registry::get('help');
00032 if ($instance == null) {
00033 unset($instance);
00034 $instance = new Help();
00035 Registry::set('help', $instance);
00036 }
00037 return $instance;
00038 }
00039
00043 function Help() {
00044 import('help.OJSHelpMappingFile');
00045 $mainMappingFile =& new OJSHelpMappingFile();
00046 $this->mappingFiles = array();
00047 $this->addMappingFile($mainMappingFile);
00048 }
00049
00050 function &getMappingFiles() {
00051 return $this->mappingFiles;
00052 }
00053
00054 function addMappingFile(&$mappingFile) {
00055 $this->mappingFiles[] =& $mappingFile;
00056 }
00057
00063 function getLocale() {
00064 $locale = Locale::getLocale();
00065 if (!file_exists("help/$locale/.")) {
00066 return 'en_US';
00067 }
00068 return $locale;
00069 }
00070
00076 function translate($key) {
00077 $key = trim($key);
00078 if (empty($key)) {
00079 return '';
00080 }
00081
00082 $mappingFiles =& $this->getMappingFiles();
00083 for ($i=0; $i < count($mappingFiles); $i++) {
00084
00085 $mappingFile =& $mappingFiles[$i];
00086 $value = $mappingFile->map($key);
00087 if ($value !== null) return $value;
00088 unset($mappingFile);
00089 }
00090
00091 if (!isset($value)) {
00092 return '##' . $key . '##';
00093 }
00094 }
00095
00096 function &_getTocCache() {
00097 static $cache;
00098
00099 if (!isset($cache)) {
00100 import('cache.CacheManager');
00101 $cacheManager =& CacheManager::getManager();
00102 $cache = $cacheManager->getFileCache(
00103 'help', 'toc',
00104 array('Help', '_tocCacheMiss')
00105 );
00106
00107
00108 $cacheTime = $cache->getCacheTime();
00109 if ($cacheTime !== null && $cacheTime < $this->dirmtime('help/'. $this->getLocale() . '/.', true)) {
00110
00111 $cache->flush();
00112 }
00113 }
00114 return $cache;
00115 }
00116
00117 function _mappingCacheMiss(&$cache, $id) {
00118
00119
00120 static $mappings;
00121
00122 $result = null;
00123 if (HookRegistry::call('Help::_mappingCacheMiss', array(&$cache, &$id, &$mappings, &$result))) return $result;
00124
00125 if (!isset($mappings)) {
00126 $mappings =& $this->loadHelpMappings();
00127 $cache->setEntireCache($mappings);
00128 }
00129 return isset($mappings[$id])?$mappings[$id]:null;
00130 }
00131
00132 function _tocCacheMiss(&$cache, $id) {
00133
00134
00135 static $toc;
00136 if (!isset($toc)) {
00137 $helpToc = array();
00138 $topicId = 'index/topic/000000';
00139 $help =& Help::getHelp();
00140 $helpToc = $help->buildTopicSection($topicId);
00141 $toc =& $help->buildToc($helpToc);
00142
00143 $cache->setEntireCache($toc);
00144 }
00145 return null;
00146 }
00147
00153 function &getTableOfContents() {
00154 $cache =& $this->_getTocCache();
00155 return $cache->getContents();
00156 }
00157
00163 function &buildToc($helpToc) {
00164
00165 $toc = array();
00166 foreach($helpToc as $topicId => $section) {
00167 $toc[$topicId] = array('title' => $section['title'], 'prefix' => '');
00168 $this->buildTocHelper($toc, $section['section'], '');
00169 }
00170 return $toc;
00171 }
00172
00179 function buildTocHelper(&$toc, $section, $prefix) {
00180 if (isset($section)) {
00181 $prefix = " $prefix";
00182 foreach($section as $topicId => $sect) {
00183 $toc[$topicId] = array('title' => $sect['title'], 'prefix' => $prefix);
00184 $this->buildTocHelper($toc, $sect['section'], $prefix);
00185 }
00186 }
00187 }
00188
00195 function buildTopicSection($topicId, $prevTocId = null) {
00196 $topicDao =& DAORegistry::getDAO('HelpTopicDAO');
00197 $tocDao =& DAORegistry::getDAO('HelpTocDAO');
00198
00199 $topic = $topicDao->getTopic($topicId);
00200 if ($topicId == 'index/topic/000000') {
00201 $tocId = $topic->getTocId();
00202 } else {
00203 $tocId = $topic->getSubTocId();
00204 }
00205
00206 $section = array();
00207 if ($tocId && $tocId != $prevTocId) {
00208 $toc = $tocDao->getToc($tocId);
00209 $topics = $toc->getTopics();
00210 foreach($topics as $currTopic) {
00211 $currId = $currTopic->getId();
00212 $currTitle = $currTopic->getTitle();
00213 if ($currId != $topicId) {
00214 $section[$currId] = array('title' => $currTitle, 'section' => $this->buildTopicSection($currId, $tocId));
00215 }
00216 }
00217 }
00218 if (empty($section)) {
00219 $section = null;
00220 }
00221
00222 return $section;
00223 }
00224
00232 function dirmtime($dirName,$doRecursive) {
00233 $d = dir($dirName);
00234 $lastModified = 0;
00235 while($entry = $d->read()) {
00236 if ($entry != "." && $entry != "..") {
00237 if (!is_dir($dirName."/".$entry)) {
00238 $currentModified = filemtime($dirName."/".$entry);
00239 } else if ($doRecursive && is_dir($dirName."/".$entry)) {
00240 $currentModified = $this->dirmtime($dirName."/".$entry,true);
00241 }
00242 if ($currentModified > $lastModified) {
00243 $lastModified = $currentModified;
00244 }
00245 }
00246 }
00247 $d->close();
00248 return $lastModified;
00249 }
00250
00251 function getSearchPaths() {
00252 $mappingFiles =& $this->getMappingFiles();
00253 $searchPaths = array();
00254 for ($i = 0; $i < count($mappingFiles); $i++) {
00255 $searchPaths[$mappingFiles[$i]->getSearchPath()] =& $mappingFiles[$i];
00256 }
00257 return $searchPaths;
00258 }
00259 }
00260
00261 ?>