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 ?
260 'root_writable' => is_writable($this->mroot
),
272 public function resetModulesList()
274 $this->modules
= array();
278 * Deactivate specified module
280 * @param string $id Module's ID
282 * @return void|exception
284 public function deactivateModule($id)
286 if (!isset($this->modules
[$id])) {
287 throw new \
Exception(_T("No such module."));
290 if (!$this->modules
[$id]['root_writable']) {
291 throw new \
Exception(_T("Cannot deactivate plugin."));
294 if (@file_put_contents
($this->modules
[$id]['root'] . '/_disabled', '')) {
295 throw new \
Exception(_T("Cannot deactivate plugin."));
300 * Activate specified module
302 * @param string $id Module's ID
304 * @return void|exception
306 public function activateModule($id)
308 if (!isset($this->disabled
[$id])) {
309 throw new \
Exception(_T("No such module."));
312 if (!$this->disabled
[$id]['root_writable']) {
313 throw new \
Exception(_T("Cannot activate plugin."));
316 if (@unlink
($this->disabled
[$id]['root'].'/_disabled') === false) {
317 throw new \
Exception(_T("Cannot activate plugin."));
322 * This method will search for file <var>$file</var> in language
323 * <var>$lang</var> for module <var>$id</var>.
324 * <var>$file</var> should not have any extension.
326 * @param string $id Module ID
327 * @param string $language Language code
331 public function loadModuleL10N($id, $language)
335 if (!$language ||
!isset($this->modules
[$id])) {
340 $this->modules
[$id]['route']
342 foreach ($domains as $domain) {
343 //load translation file for domain
344 $translator->addTranslationFilePattern(
346 $this->modules
[$id]['root'] . '/lang/',
347 '/%s/LC_MESSAGES/' . $domain . '.mo',
351 //check if a local lang file exists and load it
352 $translator->addTranslationFilePattern(
354 $this->modules
[$id]['root'] . '/lang/',
355 $domain . '_%s_local_lang.php',
362 * Loads smarties specific (headers, assigments and so on)
364 * @param string $id Module ID
368 public function loadSmarties($id)
370 $f = $this->modules
[$id]['root'] . '/_smarties.php';
371 if (file_exists($f)) {
373 if (isset($_tpl_assignments)) {
374 $this->modules
[$id]['tpl_assignments'] = $_tpl_assignments;
380 * Loads event provider
382 * @param string $id Module ID
386 public function loadEventProviders($id)
390 $providerClassName = '\\' . $this->getNamespace($id) . '\\' . 'PluginEventProvider';
391 if (class_exists($providerClassName)
392 && method_exists($providerClassName, 'provideListeners')
394 $emitter->useListenerProvider(new $providerClassName());
399 * Returns all modules associative array or only one module if <var>$id</var>
402 * @param string $id Optionnal module ID
404 * @return <b>array</b>
406 public function getModules($id = null)
408 if ($id && isset($this->modules
[$id])) {
409 return $this->modules
[$id];
411 return $this->modules
;
415 * Returns true if the module with ID <var>$id</var> exists.
417 * @param string $id Module ID
419 * @return <b>boolean</b>
421 public function moduleExists($id)
423 return isset($this->modules
[$id]);
427 * Returns all disabled modules in an array
429 * @return <b>array</b>
431 public function getDisabledModules()
433 return $this->disabled
;
437 * Returns root path for module with ID <var>$id</var>.
439 * @param string $id Module ID
441 * @return <b>string</b>
443 public function moduleRoot($id)
445 return $this->moduleInfo($id, 'root');
449 * Returns a module information that could be:
459 * @param string $id Module ID
460 * @param string $info Information to retrieve
462 * @return module's information
464 public function moduleInfo($id, $info)
466 return isset($this->modules
[$id][$info]) ?
$this->modules
[$id][$info] : null;
470 * Search and load menu templates from plugins.
471 * Also sets the web path to the plugin with the var "galette_[plugin-name]_path"
473 * @param Smarty $tpl Smarty template
477 public function getMenus($tpl)
479 $modules = $this->getModules();
480 foreach (array_keys($this->getModules()) as $r) {
481 $menu_path = $this->getTemplatesPath($r) . '/menu.tpl';
482 if ($tpl->templateExists($menu_path)) {
483 $name2path = strtolower(
484 str_replace(' ', '_', $modules[$r]['name'])
487 'galette_' . $name2path . '_path',
488 'plugins/' . $r . '/'
490 $tpl->display($menu_path);
496 * Search and load public menu templates from plugins.
497 * Also sets the web path to the plugin with the var "galette_[plugin-name]_path"
499 * @param Smarty $tpl Smarty template
500 * @param boolean $public_page Called from a public page
504 public function getPublicMenus($tpl, $public_page = false)
506 $modules = $this->getModules();
507 foreach (array_keys($this->getModules()) as $r) {
508 $menu_path = $this->getTemplatesPath($r) . '/public_menu.tpl';
509 if ($tpl->templateExists($menu_path)) {
510 $name2path = strtolower(
511 str_replace(' ', '_', $modules[$r]['name'])
514 'galette_' . $name2path . '_path',
515 'plugins/' . $r . '/'
521 $tpl->display($menu_path);
527 * Get plugins dashboard entries.
529 * @param Smarty $tpl Smarty template
533 public function getDashboard($tpl)
535 $modules = $this->getModules();
536 foreach (array_keys($this->getModules()) as $r) {
537 $dash_path = $this->getTemplatesPath($r) . '/dashboard.tpl';
538 if ($tpl->templateExists($dash_path)) {
539 $name2path = strtolower(
540 str_replace(' ', '_', $modules[$r]['name'])
542 $tpl->display($dash_path);
548 * Get plugins single member dashboard entries.
550 * @param Smarty $tpl Smarty template
554 public function getMemberDashboard($tpl)
556 $modules = $this->getModules();
557 foreach (array_keys($this->getModules()) as $r) {
558 $dash_path = $this->getTemplatesPath($r) . '/dashboard_member.tpl';
559 if ($tpl->templateExists($dash_path)) {
560 $name2path = strtolower(
561 str_replace(' ', '_', $modules[$r]['name'])
563 $tpl->display($dash_path);
571 * @param array $a A module
572 * @param array $b Another module
574 * @return 1 if a has the highest priority, -1 otherwise
576 private function sortModules($a, $b)
578 if ($a['priority'] == $b['priority']) {
579 return strcasecmp($a['name'], $b['name']);
582 return ($a['priority'] < $b['priority']) ?
-1 : 1;
586 * Get the templates path for a specified module
588 * @param string $id Module's ID
590 * @return Concatenated templates path for requested module
592 public function getTemplatesPath($id)
594 return $this->moduleRoot($id) . '/templates/' . $this->preferences
->pref_theme
;
598 * Get the templates path for a specified module name
600 * @param string $name Module's name
602 * @return Concatenated templates path for requested module
604 public function getTemplatesPathFromName($name)
607 foreach (array_keys($this->getModules()) as $r) {
608 $mod = $this->getModules($r);
609 if ($mod['name'] === $name) {
610 return $this->getTemplatesPath($r);
616 * For each module, returns the headers.tpl full path, if present.
618 * @return array of headers to include for all modules
620 public function getTplHeaders()
623 foreach (array_keys($this->modules
) as $key) {
624 $headers_path = $this->getTemplatesPath($key) . '/headers.tpl';
625 if (file_exists($headers_path)) {
626 $_headers[$key] = $headers_path;
633 * For each module, return the adh_actions.tpl full path, if present.
635 * @return array of adherent actions to include on member list for all modules
637 public function getTplAdhActions()
640 foreach (array_keys($this->modules
) as $key) {
641 $actions_path = $this->getTemplatesPath($key) . '/adh_actions.tpl';
642 if (file_exists($actions_path)) {
643 $_actions['actions_' . $key] = $actions_path;
650 * For each module, return the adh_batch_action.tpl full path, if present.
652 * @return array of adherents batch actions to include on members list
655 public function getTplAdhBatchActions()
658 foreach (array_keys($this->modules
) as $key) {
659 $actions_path = $this->getTemplatesPath($key) . '/adh_batch_action.tpl';
660 if (file_exists($actions_path)) {
661 $_actions['batch_action_' . $key] = $actions_path;
668 * For each module, return the adh_fiche_action.tpl full path, if present.
670 * @return array of adherent actions to include on member detailled view for
673 public function getTplAdhDetailledActions()
676 foreach (array_keys($this->modules
) as $key) {
677 $actions_path = $this->getTemplatesPath($key) . '/adh_fiche_action.tpl';
678 if (file_exists($actions_path)) {
679 $_actions['det_actions_' . $key] = $actions_path;
686 * For each module, gets templates assignements ; and replace some path variables
688 * @return array of Smarty templates assignement for all modules
690 public function getTplAssignments()
693 foreach ($this->modules
as $key => $module) {
694 if (isset($module['tpl_assignments'])) {
695 foreach ($module['tpl_assignments'] as $k => $v) {
698 'plugins/' . $key . '/',
702 '__plugin_include_dir__',
703 'plugins/' . $key . '/includes/',
707 '__plugin_templates_dir__',
708 'plugins/' . $key . '/templates/' .
709 $this->preferences
->pref_theme
. '/',
720 * Does module needs a database?
722 * @param string $id Module's ID
726 public function needsDatabase($id)
728 if (isset($this->modules
[$id])) {
729 $d = $this->modules
[$id]['root'] . '/scripts/';
730 if (file_exists($d)) {
736 throw new \
Exception(_T("Module does not exists!"));
741 * Override preferences from plugin
743 * @param string $id Module ID
747 public function overridePrefs($id)
749 $overridables = ['pref_adhesion_form'];
751 $f = $this->modules
[$id]['root'] . '/_preferences.php';
752 if (file_exists($f)) {
754 if (isset($_preferences)) {
755 foreach ($_preferences as $k => $v) {
756 if (in_array($k, $overridables)) {
757 $this->preferences
->$k = $v;
765 * Get plugins routes ACLs
769 public function getAcls()
772 foreach ($this->modules
as $module) {
773 $acls = array_merge($acls, $module['acls']);
779 * Retrieve a file that should be publically exposed
781 * @param int $id Module id
782 * @param string $path File path
786 public function getFile($id, $path)
788 if (isset($this->modules
[$id])) {
789 $file = $this->modules
[$id]['root'] . '/webroot/' . $path;
790 if (file_exists($file)) {
793 throw new \
RuntimeException(_T("File not found!"));
796 throw new \
Exception(_T("Module does not exists!"));
801 * Set a module as disabled
803 * @param integer $cause Cause (one of Plugins::DISABLED_* constants)
807 private function setDisabled($cause)
809 $this->disabled
[$this->id
] = array(
810 'root' => $this->mroot
,
811 'root_writable' => is_writable($this->mroot
),
819 * Get module namespace
821 * @param integer $id Module ID
825 public function getNamespace($id)
827 return str_replace(' ', '', $this->modules
[$id]['name']);