3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2009-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2009-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7 - 2009-03-09
37 namespace Galette\Core
;
41 use Galette\Common\ClassLoader
;
42 use Galette\Core\Preferences
;
45 * Plugins class for galette
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2009-2014 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 * @since Available since 0.7 - 2009-03-09
59 public const DISABLED_COMPAT
= 0;
60 public const DISABLED_MISS
= 1;
61 public const DISABLED_EXPLICIT
= 2;
64 protected $modules = array();
65 protected $disabled = array();
70 protected $preferences;
71 protected $autoload = false;
75 * Register autoloaders for all plugins
77 * @param string $path could be a separated list of paths
78 * (path separator depends on your OS).
82 public function autoload($path)
84 $this->path
= explode(PATH_SEPARATOR
, $path);
85 $this->autoload
= true;
86 $this->parseModules();
90 * Parse modules in current path
94 protected function parseModules()
96 foreach ($this->path
as $root) {
97 if (!is_dir($root) ||
!is_readable($root)) {
101 if (substr($root, -1) != '/') {
105 if (($d = @dir
($root)) === false) {
109 while (($entry = $d->read()) !== false) {
110 $full_entry = realpath($root . $entry);
111 if ($entry != '.' && $entry != '..' && is_dir($full_entry)) {
113 $this->mroot
= $full_entry;
114 if ($this->autoload
=== true) {
116 !file_exists($full_entry . '/_define.php')
117 ||
!file_exists($full_entry . '/_routes.php')
119 //plugin is not compatible with that version of galette.
121 'Plugin ' . $entry . ' is missing a _define.php and/or _routes.php ' .
122 'files that are required.',
125 $this->setDisabled(self
::DISABLED_MISS
);
126 } elseif (!file_exists($full_entry . '/_disabled')) {
127 include $full_entry . '/_define.php';
130 //set autoloader to PluginName.
131 if (isset($this->modules
[$entry]) && file_exists($full_entry . '/lib')) {
132 $varname = $entry . 'Loader';
133 $
$varname = new ClassLoader(
134 $this->getNamespace($entry),
137 $
$varname->register();
140 //plugin is not compatible with that version of galette.
142 'Plugin ' . $entry . ' is explicitely disabled',
145 $this->setDisabled(self
::DISABLED_EXPLICIT
);
157 * @param Preferences $preferences Galette's Preferences
158 * @param string $path could be a separated list of paths
159 * (path separator depends on your OS).
160 * @param string $lang Indicates if we need to load a lang file on plugin
165 public function loadModules(Preferences
$preferences, $path, $lang = null)
167 $this->preferences
= $preferences;
168 $this->path
= explode(PATH_SEPARATOR
, $path);
170 $this->parseModules();
173 uasort($this->modules
, array($this, 'sortModules'));
175 // Load translation, _prepend and ns_file
176 foreach ($this->modules
as $id => $m) {
177 $this->loadModuleL10N($id, $lang);
178 $this->loadSmarties($id);
179 $this->loadEventProviders($id);
180 $this->overridePrefs($id);
185 * This method registers a module in modules list. You should use this to
186 * register a new module.
188 * <var>$permissions</var> is a comma separated list of permissions for your
189 * module. If <var>$permissions</var> is null, only super admin has access to
192 * <var>$priority</var> is an integer. Modules are sorted by priority and name.
193 * Lowest priority comes first.
195 * @param string $name Module name
196 * @param string $desc Module description
197 * @param string $author Module author name
198 * @param string $version Module version
199 * @param string $compver Galette version compatibility
200 * @param string $route Module route name
201 * @param string $date Module release date
202 * @param string $acls Module routes ACLs
203 * @param integer $priority Module priority
207 public function register(
218 if ($compver === null) {
219 //plugin compatibility missing!
221 'Plugin ' . $name . ' does not contains mandatory version ' .
222 'compatiblity information. Please contact the author.',
225 $this->setDisabled(self
::DISABLED_COMPAT
);
226 } elseif (version_compare($compver, GALETTE_COMPAT_VERSION
, '<')) {
227 //plugin is not compatible with that version of galette.
229 'Plugin ' . $name . ' is known to be compatible with Galette ' .
230 $compver . ' only, but you current installation require a ' .
231 'plugin compatible with at least ' . GALETTE_COMPAT_VERSION
,
234 $this->setDisabled(self
::DISABLED_COMPAT
);
235 } else if ($this->id
) {
236 $this->modules
[$this->id
] = array(
237 'root' => $this->mroot
,
241 'version' => $version,
244 'priority' => $priority === null ?
245 1000 : (int)$priority,
246 'root_writable' => is_writable($this->mroot
),
257 public function resetModulesList()
259 $this->modules
= array();
263 * Deactivate specified module
265 * @param string $id Module's ID
267 * @return void|exception
269 public function deactivateModule($id)
271 if (!isset($this->modules
[$id])) {
272 throw new \
Exception(_T("No such module."));
275 if (!$this->modules
[$id]['root_writable']) {
276 throw new \
Exception(_T("Cannot deactivate plugin."));
279 if (@file_put_contents
($this->modules
[$id]['root'] . '/_disabled', '')) {
280 throw new \
Exception(_T("Cannot deactivate plugin."));
285 * Activate specified module
287 * @param string $id Module's ID
289 * @return void|exception
291 public function activateModule($id)
293 if (!isset($this->disabled
[$id])) {
294 throw new \
Exception(_T("No such module."));
297 if (!$this->disabled
[$id]['root_writable']) {
298 throw new \
Exception(_T("Cannot activate plugin."));
301 if (@unlink
($this->disabled
[$id]['root'] . '/_disabled') === false) {
302 throw new \
Exception(_T("Cannot activate plugin."));
307 * This method will search for file <var>$file</var> in language
308 * <var>$lang</var> for module <var>$id</var>.
309 * <var>$file</var> should not have any extension.
311 * @param string $id Module ID
312 * @param string $language Language code
316 public function loadModuleL10N($id, $language)
320 if (!$language ||
!isset($this->modules
[$id])) {
325 $this->modules
[$id]['route']
327 foreach ($domains as $domain) {
328 //load translation file for domain
329 $translator->addTranslationFilePattern(
331 $this->modules
[$id]['root'] . '/lang/',
332 '/%s/LC_MESSAGES/' . $domain . '.mo',
336 //check if a local lang file exists and load it
337 $translator->addTranslationFilePattern(
339 $this->modules
[$id]['root'] . '/lang/',
340 $domain . '_%s_local_lang.php',
347 * Loads smarties specific (headers, assigments and so on)
349 * @param string $id Module ID
353 public function loadSmarties($id)
355 $f = $this->modules
[$id]['root'] . '/_smarties.php';
356 if (file_exists($f)) {
358 if (isset($_tpl_assignments)) {
359 $this->modules
[$id]['tpl_assignments'] = $_tpl_assignments;
365 * Loads event provider
367 * @param string $id Module ID
371 public function loadEventProviders($id)
375 $providerClassName = '\\' . $this->getNamespace($id) . '\\' . 'PluginEventProvider';
377 class_exists($providerClassName)
378 && method_exists($providerClassName, 'provideListeners')
380 $emitter->useListenerProvider(new $providerClassName());
385 * Returns all modules associative array or only one module if <var>$id</var>
388 * @param string $id Optionnal module ID
392 public function getModules($id = null)
394 if ($id && isset($this->modules
[$id])) {
395 return $this->modules
[$id];
397 return $this->modules
;
401 * Returns true if the module with ID <var>$id</var> exists.
403 * @param string $id Module ID
407 public function moduleExists($id)
409 return isset($this->modules
[$id]);
413 * Returns all disabled modules in an array
417 public function getDisabledModules()
419 return $this->disabled
;
423 * Returns root path for module with ID <var>$id</var>.
425 * @param string $id Module ID
429 public function moduleRoot($id)
431 return $this->moduleInfo($id, 'root');
435 * Returns a module information that could be:
445 * @param string $id Module ID
446 * @param string $info Information to retrieve
448 * @return module's information
450 public function moduleInfo($id, $info)
452 return isset($this->modules
[$id][$info]) ?
$this->modules
[$id][$info] : null;
456 * Search and load menu templates from plugins.
457 * Also sets the web path to the plugin with the var "galette_[plugin-name]_path"
459 * @param Smarty $tpl Smarty template
463 public function getMenus($tpl)
465 $modules = $this->getModules();
466 foreach (array_keys($this->getModules()) as $r) {
467 $menu_path = $this->getTemplatesPath($r) . '/menu.tpl';
468 if ($tpl->templateExists($menu_path)) {
469 $name2path = strtolower(
470 str_replace(' ', '_', $modules[$r]['name'])
473 'galette_' . $name2path . '_path',
474 'plugins/' . $r . '/'
476 $tpl->display($menu_path);
482 * Search and load public menu templates from plugins.
483 * Also sets the web path to the plugin with the var "galette_[plugin-name]_path"
485 * @param Smarty $tpl Smarty template
486 * @param boolean $public_page Called from a public page
490 public function getPublicMenus($tpl, $public_page = false)
492 $modules = $this->getModules();
493 foreach (array_keys($this->getModules()) as $r) {
494 $menu_path = $this->getTemplatesPath($r) . '/public_menu.tpl';
495 if ($tpl->templateExists($menu_path)) {
496 $name2path = strtolower(
497 str_replace(' ', '_', $modules[$r]['name'])
500 'galette_' . $name2path . '_path',
501 'plugins/' . $r . '/'
507 $tpl->display($menu_path);
513 * Get plugins dashboard entries.
515 * @param Smarty $tpl Smarty template
519 public function getDashboard($tpl)
521 $modules = $this->getModules();
522 foreach (array_keys($this->getModules()) as $r) {
523 $dash_path = $this->getTemplatesPath($r) . '/dashboard.tpl';
524 if ($tpl->templateExists($dash_path)) {
525 $name2path = strtolower(
526 str_replace(' ', '_', $modules[$r]['name'])
528 $tpl->display($dash_path);
534 * Get plugins single member dashboard entries.
536 * @param Smarty $tpl Smarty template
540 public function getMemberDashboard($tpl)
542 $modules = $this->getModules();
543 foreach (array_keys($this->getModules()) as $r) {
544 $dash_path = $this->getTemplatesPath($r) . '/dashboard_member.tpl';
545 if ($tpl->templateExists($dash_path)) {
546 $name2path = strtolower(
547 str_replace(' ', '_', $modules[$r]['name'])
549 $tpl->display($dash_path);
557 * @param array $a A module
558 * @param array $b Another module
560 * @return 1|-1 1 if a has the highest priority, -1 otherwise
562 private function sortModules($a, $b)
564 if ($a['priority'] == $b['priority']) {
565 return strcasecmp($a['name'], $b['name']);
568 return ($a['priority'] < $b['priority']) ?
-1 : 1;
572 * Get the templates path for a specified module
574 * @param string $id Module's ID
576 * @return string Concatenated templates path for requested module
578 public function getTemplatesPath($id)
580 return $this->moduleRoot($id) . '/templates/' . $this->preferences
->pref_theme
;
584 * Get the templates path for a specified module name
586 * @param string $name Module's name
588 * @return string Concatenated templates path for requested module
590 public function getTemplatesPathFromName($name)
593 foreach (array_keys($this->getModules()) as $r) {
594 $mod = $this->getModules($r);
595 if ($mod['name'] === $name) {
596 return $this->getTemplatesPath($r);
602 * For each module, returns the headers.tpl full path, if present.
604 * @return array of headers to include for all modules
606 public function getTplHeaders()
609 foreach (array_keys($this->modules
) as $key) {
610 $headers_path = $this->getTemplatesPath($key) . '/headers.tpl';
611 if (file_exists($headers_path)) {
612 $_headers[$key] = $headers_path;
619 * For each module, return the adh_actions.tpl full path, if present.
621 * @return array of adherent actions to include on member list for all modules
623 public function getTplAdhActions()
626 foreach (array_keys($this->modules
) as $key) {
627 $actions_path = $this->getTemplatesPath($key) . '/adh_actions.tpl';
628 if (file_exists($actions_path)) {
629 $_actions['actions_' . $key] = $actions_path;
636 * For each module, return the adh_batch_action.tpl full path, if present.
638 * @return array of adherents batch actions to include on members list
641 public function getTplAdhBatchActions()
644 foreach (array_keys($this->modules
) as $key) {
645 $actions_path = $this->getTemplatesPath($key) . '/adh_batch_action.tpl';
646 if (file_exists($actions_path)) {
647 $_actions['batch_action_' . $key] = $actions_path;
654 * For each module, return the adh_fiche_action.tpl full path, if present.
656 * @return array of adherent actions to include on member detailled view for
659 public function getTplAdhDetailledActions()
662 foreach (array_keys($this->modules
) as $key) {
663 $actions_path = $this->getTemplatesPath($key) . '/adh_fiche_action.tpl';
664 if (file_exists($actions_path)) {
665 $_actions['det_actions_' . $key] = $actions_path;
672 * For each module, gets templates assignements ; and replace some path variables
674 * @return array of Smarty templates assignement for all modules
676 public function getTplAssignments()
679 foreach ($this->modules
as $key => $module) {
680 if (isset($module['tpl_assignments'])) {
681 foreach ($module['tpl_assignments'] as $k => $v) {
684 'plugins/' . $key . '/',
688 '__plugin_include_dir__',
689 'plugins/' . $key . '/includes/',
693 '__plugin_templates_dir__',
694 'plugins/' . $key . '/templates/' .
695 $this->preferences
->pref_theme
. '/',
706 * Does module needs a database?
708 * @param string $id Module's ID
712 public function needsDatabase($id)
714 if (isset($this->modules
[$id])) {
715 $d = $this->modules
[$id]['root'] . '/scripts/';
716 if (file_exists($d)) {
722 throw new \
Exception(_T("Module does not exists!"));
727 * Override preferences from plugin
729 * @param string $id Module ID
733 public function overridePrefs($id)
735 $overridables = ['pref_adhesion_form'];
737 $f = $this->modules
[$id]['root'] . '/_preferences.php';
738 if (file_exists($f)) {
740 if (isset($_preferences)) {
741 foreach ($_preferences as $k => $v) {
742 if (in_array($k, $overridables)) {
743 $this->preferences
->$k = $v;
751 * Get plugins routes ACLs
755 public function getAcls()
758 foreach ($this->modules
as $module) {
759 $acls = array_merge($acls, $module['acls']);
765 * Retrieve a file that should be publically exposed
767 * @param int $id Module id
768 * @param string $path File path
772 public function getFile($id, $path)
774 if (isset($this->modules
[$id])) {
775 $file = $this->modules
[$id]['root'] . '/webroot/' . $path;
776 if (file_exists($file)) {
779 throw new \
RuntimeException(_T("File not found!"));
782 throw new \
Exception(_T("Module does not exists!"));
787 * Set a module as disabled
789 * @param integer $cause Cause (one of Plugins::DISABLED_* constants)
793 private function setDisabled($cause)
795 $this->disabled
[$this->id
] = array(
796 'root' => $this->mroot
,
797 'root_writable' => is_writable($this->mroot
),
805 * Get module namespace
807 * @param integer $id Module ID
811 public function getNamespace($id)
813 return str_replace(' ', '', $this->modules
[$id]['name']);