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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7 - 2009-03-09
38 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 const DISABLED_COMPAT
= 0;
60 const DISABLED_MISS
= 1;
61 const DISABLED_EXPLICIT
= 2;
64 protected $modules = array();
65 protected $disabled = array();
70 protected $preferences;
71 protected $autoload = false;
74 * Register autoloaders for all plugins
76 * @param string $path could be a separated list of paths
77 * (path separator depends on your OS).
81 public function autoload($path)
83 $this->path
= explode(PATH_SEPARATOR
, $path);
84 $this->autoload
= true;
85 $this->parseModules();
89 * Parse modules in current path
93 protected function parseModules()
95 foreach ($this->path
as $root) {
96 if (!is_dir($root) ||
!is_readable($root)) {
100 if (substr($root, -1) != '/') {
104 if (($d = @dir
($root)) === false) {
108 while (($entry = $d->read()) !== false) {
109 $full_entry = realpath($root . $entry);
110 if ($entry != '.' && $entry != '..' && is_dir($full_entry)) {
112 $this->mroot
= $full_entry;
113 if (!file_exists($full_entry . '/_define.php')
114 ||
!file_exists($full_entry . '/_routes.php')
116 //plugin is not compatible with that version of galette.
118 'Plugin ' . $entry . ' is missing a _define.php and/or _routes.php ' .
119 'files that are required.',
122 $this->setDisabled(self
::DISABLED_MISS
);
123 } elseif (!file_exists($full_entry . '/_disabled')) {
124 include $full_entry . '/_define.php';
127 if ($this->autoload
== true) {
128 //set autoloader to PluginName.
129 if (file_exists($full_entry . '/lib') && isset($this->modules
[$entry])) {
130 $varname = $entry . 'Loader';
131 $
$varname = new ClassLoader(
132 $this->getNamespace($entry),
135 $
$varname->register();
139 //plugin is not compatible with that version of galette.
141 'Plugin ' . $entry . ' is explicitely disabled',
144 $this->setDisabled(self
::DISABLED_EXPLICIT
);
155 * @param Preferences $preferences Galette's Preferences
156 * @param string $path could be a separated list of paths
157 * (path separator depends on your OS).
158 * @param string $lang Indicates if we need to load a lang file on plugin
163 public function loadModules(Preferences
$preferences, $path, $lang = null)
165 $this->preferences
= $preferences;
166 $this->path
= explode(PATH_SEPARATOR
, $path);
168 $this->parseModules();
171 uasort($this->modules
, array($this, 'sortModules'));
173 // Load translation, _prepend and ns_file
174 foreach ($this->modules
as $id => $m) {
175 $this->loadModuleL10N($id, $lang);
176 $this->loadSmarties($id);
177 $this->loadEventProviders($id);
178 $this->overridePrefs($id);
183 * This method registers a module in modules list. You should use this to
184 * register a new module.
186 * <var>$permissions</var> is a comma separated list of permissions for your
187 * module. If <var>$permissions</var> is null, only super admin has access to
190 * <var>$priority</var> is an integer. Modules are sorted by priority and name.
191 * Lowest priority comes first.
193 * @param string $name Module name
194 * @param string $desc Module description
195 * @param string $author Module author name
196 * @param string $version Module version
197 * @param string $compver Galette version compatibility
198 * @param string $route Module route name
199 * @param string $date Module release date
200 * @param string $acls Module routes ACLs
201 * @param integer $priority Module priority
205 public function register(
216 if ($compver === null) {
217 //plugin compatibility missing!
219 'Plugin ' . $name . ' does not contains mandatory version ' .
220 'compatiblity information. Please contact the author.',
223 $this->setDisabled(self
::DISABLED_COMPAT
);
224 } elseif (version_compare($compver, GALETTE_COMPAT_VERSION
, '<')) {
225 //plugin is not compatible with that version of galette.
227 'Plugin ' . $name . ' is known to be compatible with Galette ' .
228 $compver . ' only, but you current installation require a ' .
229 'plugin compatible with at least ' . GALETTE_COMPAT_VERSION
,
232 $this->setDisabled(self
::DISABLED_COMPAT
);
235 $release_date = $date;
236 if ($date !== null && $this->autoload
=== false) {
237 //try to localize release date
239 $release_date = new \
DateTime($date);
240 $release_date = $release_date->format(__("Y-m-d"));
241 } catch (\Exception
$e) {
243 'Unable to localize release date for plugin ' . $name,
249 $this->modules
[$this->id
] = array(
250 'root' => $this->mroot
,
254 'version' => $version,
256 'date' => $release_date,
257 'priority' => $priority === null ?
258 1000 : (integer)$priority,
259 'root_writable' => is_writable($this->mroot
),
271 public function resetModulesList()
273 $this->modules
= array();
277 * Deactivate specified module
279 * @param string $id Module's ID
281 * @return void|exception
283 public function deactivateModule($id)
285 if (!isset($this->modules
[$id])) {
286 throw new \
Exception(_T("No such module."));
289 if (!$this->modules
[$id]['root_writable']) {
290 throw new \
Exception(_T("Cannot deactivate plugin."));
293 if (@file_put_contents
($this->modules
[$id]['root'] . '/_disabled', '')) {
294 throw new \
Exception(_T("Cannot deactivate plugin."));
299 * Activate specified module
301 * @param string $id Module's ID
303 * @return void|exception
305 public function activateModule($id)
307 if (!isset($this->disabled
[$id])) {
308 throw new \
Exception(_T("No such module."));
311 if (!$this->disabled
[$id]['root_writable']) {
312 throw new \
Exception(_T("Cannot activate plugin."));
315 if (@unlink
($this->disabled
[$id]['root'] . '/_disabled') === false) {
316 throw new \
Exception(_T("Cannot activate plugin."));
321 * This method will search for file <var>$file</var> in language
322 * <var>$lang</var> for module <var>$id</var>.
323 * <var>$file</var> should not have any extension.
325 * @param string $id Module ID
326 * @param string $language Language code
330 public function loadModuleL10N($id, $language)
334 if (!$language ||
!isset($this->modules
[$id])) {
339 $this->modules
[$id]['route']
341 foreach ($domains as $domain) {
342 //load translation file for domain
343 $translator->addTranslationFilePattern(
345 $this->modules
[$id]['root'] . '/lang/',
346 '/%s/LC_MESSAGES/' . $domain . '.mo',
350 //check if a local lang file exists and load it
351 $translator->addTranslationFilePattern(
353 $this->modules
[$id]['root'] . '/lang/',
354 $domain . '_%s_local_lang.php',
361 * Loads smarties specific (headers, assigments and so on)
363 * @param string $id Module ID
367 public function loadSmarties($id)
369 $f = $this->modules
[$id]['root'] . '/_smarties.php';
370 if (file_exists($f)) {
372 if (isset($_tpl_assignments)) {
373 $this->modules
[$id]['tpl_assignments'] = $_tpl_assignments;
379 * Loads event provider
381 * @param string $id Module ID
385 public function loadEventProviders($id)
389 $providerClassName = '\\' . $this->getNamespace($id) . '\\' . 'PluginEventProvider';
390 if (class_exists($providerClassName)
391 && method_exists($providerClassName, 'provideListeners')
393 $emitter->useListenerProvider(new $providerClassName());
398 * Returns all modules associative array or only one module if <var>$id</var>
401 * @param string $id Optionnal module ID
403 * @return <b>array</b>
405 public function getModules($id = null)
407 if ($id && isset($this->modules
[$id])) {
408 return $this->modules
[$id];
410 return $this->modules
;
414 * Returns true if the module with ID <var>$id</var> exists.
416 * @param string $id Module ID
418 * @return <b>boolean</b>
420 public function moduleExists($id)
422 return isset($this->modules
[$id]);
426 * Returns all disabled modules in an array
428 * @return <b>array</b>
430 public function getDisabledModules()
432 return $this->disabled
;
436 * Returns root path for module with ID <var>$id</var>.
438 * @param string $id Module ID
440 * @return <b>string</b>
442 public function moduleRoot($id)
444 return $this->moduleInfo($id, 'root');
448 * Returns a module information that could be:
458 * @param string $id Module ID
459 * @param string $info Information to retrieve
461 * @return module's information
463 public function moduleInfo($id, $info)
465 return isset($this->modules
[$id][$info]) ?
$this->modules
[$id][$info] : null;
469 * Search and load menu templates from plugins.
470 * Also sets the web path to the plugin with the var "galette_[plugin-name]_path"
472 * @param Smarty $tpl Smarty template
476 public function getMenus($tpl)
478 $modules = $this->getModules();
479 foreach (array_keys($this->getModules()) as $r) {
480 $menu_path = $this->getTemplatesPath($r) . '/menu.tpl';
481 if ($tpl->templateExists($menu_path)) {
482 $name2path = strtolower(
483 str_replace(' ', '_', $modules[$r]['name'])
486 'galette_' . $name2path . '_path',
487 'plugins/' . $r . '/'
489 $tpl->display($menu_path);
495 * Search and load public menu templates from plugins.
496 * Also sets the web path to the plugin with the var "galette_[plugin-name]_path"
498 * @param Smarty $tpl Smarty template
499 * @param boolean $public_page Called from a public page
503 public function getPublicMenus($tpl, $public_page = false)
505 $modules = $this->getModules();
506 foreach (array_keys($this->getModules()) as $r) {
507 $menu_path = $this->getTemplatesPath($r) . '/public_menu.tpl';
508 if ($tpl->templateExists($menu_path)) {
509 $name2path = strtolower(
510 str_replace(' ', '_', $modules[$r]['name'])
513 'galette_' . $name2path . '_path',
514 'plugins/' . $r . '/'
520 $tpl->display($menu_path);
526 * Get plugins dashboard entries.
528 * @param Smarty $tpl Smarty template
532 public function getDashboard($tpl)
534 $modules = $this->getModules();
535 foreach (array_keys($this->getModules()) as $r) {
536 $dash_path = $this->getTemplatesPath($r) . '/dashboard.tpl';
537 if ($tpl->templateExists($dash_path)) {
538 $name2path = strtolower(
539 str_replace(' ', '_', $modules[$r]['name'])
541 $tpl->display($dash_path);
547 * Get plugins single member dashboard entries.
549 * @param Smarty $tpl Smarty template
553 public function getMemberDashboard($tpl)
555 $modules = $this->getModules();
556 foreach (array_keys($this->getModules()) as $r) {
557 $dash_path = $this->getTemplatesPath($r) . '/dashboard_member.tpl';
558 if ($tpl->templateExists($dash_path)) {
559 $name2path = strtolower(
560 str_replace(' ', '_', $modules[$r]['name'])
562 $tpl->display($dash_path);
570 * @param array $a A module
571 * @param array $b Another module
573 * @return 1 if a has the highest priority, -1 otherwise
575 private function sortModules($a, $b)
577 if ($a['priority'] == $b['priority']) {
578 return strcasecmp($a['name'], $b['name']);
581 return ($a['priority'] < $b['priority']) ?
-1 : 1;
585 * Get the templates path for a specified module
587 * @param string $id Module's ID
589 * @return Concatenated templates path for requested module
591 public function getTemplatesPath($id)
593 return $this->moduleRoot($id) . '/templates/' . $this->preferences
->pref_theme
;
597 * Get the templates path for a specified module name
599 * @param string $name Module's name
601 * @return Concatenated templates path for requested module
603 public function getTemplatesPathFromName($name)
606 foreach (array_keys($this->getModules()) as $r) {
607 $mod = $this->getModules($r);
608 if ($mod['name'] === $name) {
609 return $this->getTemplatesPath($r);
615 * For each module, returns the headers.tpl full path, if present.
617 * @return array of headers to include for all modules
619 public function getTplHeaders()
622 foreach (array_keys($this->modules
) as $key) {
623 $headers_path = $this->getTemplatesPath($key) . '/headers.tpl';
624 if (file_exists($headers_path)) {
625 $_headers[$key] = $headers_path;
632 * For each module, return the adh_actions.tpl full path, if present.
634 * @return array of adherent actions to include on member list for all modules
636 public function getTplAdhActions()
639 foreach (array_keys($this->modules
) as $key) {
640 $actions_path = $this->getTemplatesPath($key) . '/adh_actions.tpl';
641 if (file_exists($actions_path)) {
642 $_actions['actions_' . $key] = $actions_path;
649 * For each module, return the adh_batch_action.tpl full path, if present.
651 * @return array of adherents batch actions to include on members list
654 public function getTplAdhBatchActions()
657 foreach (array_keys($this->modules
) as $key) {
658 $actions_path = $this->getTemplatesPath($key) . '/adh_batch_action.tpl';
659 if (file_exists($actions_path)) {
660 $_actions['batch_action_' . $key] = $actions_path;
667 * For each module, return the adh_fiche_action.tpl full path, if present.
669 * @return array of adherent actions to include on member detailled view for
672 public function getTplAdhDetailledActions()
675 foreach (array_keys($this->modules
) as $key) {
676 $actions_path = $this->getTemplatesPath($key) . '/adh_fiche_action.tpl';
677 if (file_exists($actions_path)) {
678 $_actions['det_actions_' . $key] = $actions_path;
685 * For each module, gets templates assignements ; and replace some path variables
687 * @return array of Smarty templates assignement for all modules
689 public function getTplAssignments()
692 foreach ($this->modules
as $key => $module) {
693 if (isset($module['tpl_assignments'])) {
694 foreach ($module['tpl_assignments'] as $k => $v) {
697 'plugins/' . $key . '/',
701 '__plugin_include_dir__',
702 'plugins/' . $key . '/includes/',
706 '__plugin_templates_dir__',
707 'plugins/' . $key . '/templates/' .
708 $this->preferences
->pref_theme
. '/',
719 * Does module needs a database?
721 * @param string $id Module's ID
725 public function needsDatabase($id)
727 if (isset($this->modules
[$id])) {
728 $d = $this->modules
[$id]['root'] . '/scripts/';
729 if (file_exists($d)) {
735 throw new \
Exception(_T("Module does not exists!"));
740 * Override preferences from plugin
742 * @param string $id Module ID
746 public function overridePrefs($id)
748 $overridables = ['pref_adhesion_form'];
750 $f = $this->modules
[$id]['root'] . '/_preferences.php';
751 if (file_exists($f)) {
753 if (isset($_preferences)) {
754 foreach ($_preferences as $k => $v) {
755 if (in_array($k, $overridables)) {
756 $this->preferences
->$k = $v;
764 * Get plugins routes ACLs
768 public function getAcls()
771 foreach ($this->modules
as $module) {
772 $acls = array_merge($acls, $module['acls']);
778 * Retrieve a file that should be publically exposed
780 * @param int $id Module id
781 * @param string $path File path
785 public function getFile($id, $path)
787 if (isset($this->modules
[$id])) {
788 $file = $this->modules
[$id]['root'] . '/webroot/' . $path;
789 if (file_exists($file)) {
792 throw new \
RuntimeException(_T("File not found!"));
795 throw new \
Exception(_T("Module does not exists!"));
800 * Set a module as disabled
802 * @param integer $cause Cause (one of Plugins::DISABLED_* constants)
806 private function setDisabled($cause)
808 $this->disabled
[$this->id
] = array(
809 'root' => $this->mroot
,
810 'root_writable' => is_writable($this->mroot
),
818 * Get module namespace
820 * @param integer $id Module ID
824 public function getNamespace($id)
826 return str_replace(' ', '', $this->modules
[$id]['name']);