Further modules included.
[yaffs-website] / web / modules / contrib / advanced_help / src / AdvancedHelpManager.php
1 <?php
2
3 namespace Drupal\advanced_help;
4
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\Core\Extension\ThemeHandlerInterface;
8 use Drupal\Core\StringTranslation\TranslationInterface;
9 use Drupal\Core\Plugin\DefaultPluginManager;
10 use Drupal\Core\StringTranslation\StringTranslationTrait;
11 use Drupal\Component\Serialization\Yaml;
12
13 /**
14  * AdvancedHelp plugin manager.
15  */
16 class AdvancedHelpManager extends DefaultPluginManager {
17   use StringTranslationTrait;
18
19   /**
20    * Constructs an AdvancedHelpManager instance.
21    *
22    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
23    *   Cache backend instance to use.
24    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
25    *   The module handler to invoke the alter hook with.
26    * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
27    *   Theme handler.
28    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
29    *   The string translation service.
30    */
31   public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache_backend, TranslationInterface $string_translation) {
32     $this->module_handler = $module_handler;
33     $this->theme_handler = $theme_handler;
34     $this->setStringTranslation($string_translation);
35     $this->alterInfo('advanced_help');
36     $this->setCacheBackend($cache_backend, 'advanced_help', ['advanced_help']);
37   }
38
39   /**
40    * Get the modules/theme list.
41    * @todo cache
42    */
43   public function getModuleList() {
44     $modules = $this->module_handler->getModuleList();
45     $themes = $this->theme_handler->listInfo();
46     $result = [];
47
48     foreach ($modules + $themes  as $name => $data) {
49       $result[$name] = $this->module_handler->getName($name);
50     }
51     return $result;
52   }
53
54   /**
55    * Get the information for a single help topic.
56    * @param $module
57    * @param $topic
58    * @return string|bool
59    */
60   function getTopic($module, $topic) {
61     $topics = $this->getTopics();
62     if (!empty($topics[$module][$topic])) {
63       return $topics[$module][$topic];
64     }
65     return FALSE;
66   }
67
68   /**
69    * Return the name of the module.
70    * @param string $module
71    * @return string
72    */
73   function getModuleName($module) {
74     return $this->module_handler->getName($module);
75   }
76
77   /**
78    * Search the system for all available help topics.
79    * @todo check visibility of the method.
80    */
81   public function getTopics() {
82     $ini = $this->parseHelp();
83     return $ini['topics'];
84   }
85
86   /**
87    * Returns advanced help settings.
88    * @todo check visibility of the method.
89    */
90   public function getSettings() {
91     $ini = $this->parseHelp();
92     return $ini['settings'];
93   }
94
95
96   /**
97    * Function to parse yml / txt files.
98    *
99    * @todo implement cache
100    * @todo check visibility of the method.
101    */
102   public function parseHelp() {
103     $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
104     static $ini = NULL;
105
106     $cache = $this->cacheGet('advanced_help_ini_' . $language);
107     if ($cache) {
108       $ini = $cache->data;
109     }
110
111     if (!isset($ini)) {
112       $ini = ['topics' => [], 'settings' => []];
113
114       foreach ($this->module_handler->getModuleList() + $this->theme_handler->listInfo() as $plugin_name => $extension) {
115         $module = $plugin_name;
116         $module_path = $extension->getPath();
117         $info = [];
118
119         if (file_exists("$module_path/help/$module.help.yml")) {
120           $path = "$module_path/help";
121           $info =  Yaml::decode(file_get_contents("$module_path/help/$module.help.yml"));
122         }
123         elseif (!file_exists("$module_path/help")) {
124           // Look for one or more README files.
125           $files = file_scan_directory("./$module_path",
126             '/^(readme).*\.(txt|md)$/i', ['recurse' => FALSE]);
127           $path = "./$module_path";
128           foreach ($files as $name => $fileinfo) {
129             $info[$fileinfo->filename] = [
130               'line break' => TRUE,
131               'readme file' => TRUE,
132               'file' => $fileinfo->filename,
133               'title' => $fileinfo->name,
134             ];
135           }
136         }
137
138         if (!empty($info)) {
139           // Get translated titles:
140           $translation = [];
141           if (file_exists("$module_path/translations/help/$language/$module.help.yml")) {
142             $translation = Yaml::decode(file_get_contents("$module_path/translations/help/$language/$module.help.yml"));
143           }
144
145           $ini['settings'][$module] = [];
146           if (!empty($info['advanced help settings'])) {
147             $ini['settings'][$module] = $info['advanced help settings'];
148             unset($info['advanced help settings']);
149
150             // Check translated strings for translatable global settings.
151             if (isset($translation['advanced help settings']['name'])) {
152               $ini['settings']['name'] = $translation['advanced help settings']['name'];
153             }
154             if (isset($translation['advanced help settings']['index name'])) {
155               $ini['settings']['index name'] = $translation['advanced help settings']['index name'];
156             }
157
158           }
159
160           foreach ($info as $name => $topic) {
161             // Each topic should have a name, a title, a file and path.
162             $file = !empty($topic['file']) ? $topic['file'] : $name;
163             $ini['topics'][$module][$name] = [
164               'name' => $name,
165               'module' => $module,
166               'ini' => $topic,
167               'title' => !empty($translation[$name]['title']) ? $translation[$name]['title'] : $topic['title'],
168               'weight' => isset($topic['weight']) ? $topic['weight'] : 0,
169               'parent' => isset($topic['parent']) ? $topic['parent'] : 0,
170               'popup width' => isset($topic['popup width']) ? $topic['popup width'] : 500,
171               'popup height' => isset($topic['popup height']) ? $topic['popup height'] : 500,
172               // Require extension.
173               'file' => isset($topic['readme file']) ? $file : $file . '.html',
174               // Not in .ini file.
175               'path' => $path,
176               'line break' => isset($topic['line break']) ? $topic['line break'] : (isset($ini['settings'][$module]['line break']) ? $ini['settings'][$module]['line break'] : FALSE),
177               'navigation' => isset($topic['navigation']) ? $topic['navigation'] : (isset($ini['settings'][$module]['navigation']) ? $ini['settings'][$module]['navigation'] : TRUE),
178               'css' => isset($topic['css']) ? $topic['css'] : (isset($ini['settings'][$module]['css']) ? $ini['settings'][$module]['css'] : NULL),
179               'readme file' => isset($topic['readme file']) ? $topic['readme file'] : FALSE,
180             ];
181           }
182         }
183       }
184       // drupal_alter('advanced_help_topic_info', $ini);
185       $this->cacheSet('advanced_help_ini_' . $language, $ini);
186     }
187     return $ini;
188   }
189
190   /**
191    * Load and render a help topic.
192    *
193    * @todo allow the theme override the help.
194    * @param $module.
195    * @param $topic.
196    * @return array.
197   */
198   public function getTopicFileInfo($module, $topic) {
199     $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
200
201     $info = $this->getTopic($module, $topic);
202     if (empty($info)) {
203       return FALSE;
204     }
205
206     $path_type = (preg_match('/themes/', $info['path'])) ? 'theme' : 'module';
207     // Search paths:
208     $paths = [
209 //      // Allow theme override.
210 //      path_to_theme() . '/help',
211       // Translations.
212       drupal_get_path($path_type, $module) . "/translations/help/$language",
213       // In same directory as .inc file.
214       $info['path'],
215     ];
216
217     foreach ($paths as $path) {
218       if (file_exists("$path/$info[file]")) {
219         return ['path' => $path, 'file' => $info['file']];
220       }
221     }
222
223     return FALSE;
224   }
225
226   public function getTopicFileName($module, $topic) {
227     $info = $this->getTopicFileInfo($module, $topic);
228     if ($info) {
229       return "./{$info['path']}/{$info['file']}";
230     }
231   }
232
233 }