plugin.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Base class for plugins
  4. *
  5. * @copyright (c) 2012 University of Geneva
  6. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  7. * @author Laurent Opprecht <laurent@opprecht.info>
  8. */
  9. class Plugin {
  10. protected $version = '';
  11. protected $author = '';
  12. protected $fields = array();
  13. protected function __construct($version, $author, $settings = array()) {
  14. $this->version = $version;
  15. $this->author = $author;
  16. $this->fields = $settings;
  17. global $language_files;
  18. $language_files[] = 'plugin_' . $this->get_name();
  19. }
  20. function get_info() {
  21. $result = array();
  22. $result['title'] = $this->get_title();
  23. $result['comment'] = $this->get_comment();
  24. $result['version'] = $this->get_version();
  25. $result['author'] = $this->get_author();
  26. if ($form = $this->get_settings_form()) {
  27. $result['settings_form'] = $form;
  28. foreach ($this->fields as $name => $type) {
  29. $value = $this->get($name);
  30. $result[$name] = $value;
  31. }
  32. }
  33. return $result;
  34. }
  35. function get_name() {
  36. $result = get_class($this);
  37. $result = str_replace('Plugin', '', $result);
  38. $result = strtolower($result);
  39. return $result;
  40. }
  41. function get_title() {
  42. return $this->get_lang('plugin_title');
  43. }
  44. function get_comment() {
  45. return $this->get_lang('plugin_comment');
  46. }
  47. function get_version() {
  48. return $this->version;
  49. }
  50. function get_author() {
  51. return $this->author;
  52. }
  53. function get_css() {
  54. $name = $this->get_name();
  55. $path = api_get_path(SYS_PLUGIN_PATH)."$name/resources/$name.css";
  56. if (!is_readable($path)) {
  57. return '';
  58. }
  59. $css = array();
  60. $css[] = file_get_contents($path);
  61. $result = implode($css);
  62. return $result;
  63. }
  64. /**
  65. *
  66. * @return FormValidator
  67. */
  68. function get_settings_form() {
  69. $result = new FormValidator($this->get_name());
  70. $defaults = array();
  71. foreach ($this->fields as $name => $type) {
  72. $value = $this->get($name);
  73. $defaults[$name] = $value;
  74. $type = isset($type) ? $type : 'text';
  75. $help = null;
  76. if ($this->get_lang_plugin_exists($name.'_help')) {
  77. $help = $this->get_lang($name.'_help');
  78. }
  79. switch ($type) {
  80. case 'html':
  81. $result->addElement('html', $this->get_lang($name));
  82. break;
  83. case 'wysiwyg':
  84. $result->add_html_editor($name, $this->get_lang($name));
  85. break;
  86. case 'text':
  87. $result->addElement($type, $name, array($this->get_lang($name), $help));
  88. break;
  89. case 'boolean':
  90. $group = array();
  91. $group[] = $result->createElement('radio', $name, '', get_lang('Yes'), 'true');
  92. $group[] = $result->createElement('radio', $name, '', get_lang('No'), 'false');
  93. $result->addGroup($group, null, array($this->get_lang($name), $help));
  94. break;
  95. }
  96. }
  97. $result->setDefaults($defaults);
  98. $result->addElement('style_submit_button', 'submit_button', $this->get_lang('Save'));
  99. return $result;
  100. }
  101. function get($name) {
  102. $settings = $this->get_settings();
  103. foreach ($settings as $setting) {
  104. if ($setting['variable'] == ($this->get_name() . '_' . $name)) {
  105. return $setting['selected_value'];
  106. }
  107. }
  108. return false;
  109. }
  110. private $settings = null;
  111. public function get_settings() {
  112. if (is_null($this->settings)) {
  113. $settings = api_get_settings_params(array("subkey = ? AND category = ? AND type = ? " => array($this->get_name(), 'Plugins', 'setting')));
  114. $this->settings = $settings;
  115. }
  116. return $this->settings;
  117. }
  118. private $strings = null;
  119. public function get_lang_plugin_exists($name) {
  120. return isset($this->strings[$name]);
  121. }
  122. public function get_lang($name) {
  123. if (is_null($this->strings)) {
  124. global $language_interface;
  125. $root = api_get_path(SYS_PLUGIN_PATH);
  126. $plugin_name = $this->get_name();
  127. //1. Loading english if exists
  128. $english_path = $root.$plugin_name."/lang/english.php";
  129. if (is_readable($english_path)) {
  130. include $english_path;
  131. $this->strings = $strings;
  132. }
  133. $path = $root.$plugin_name."/lang/$language_interface.php";
  134. //2. Loading the system language
  135. if (is_readable($path)) {
  136. include $path;
  137. if (!empty($strings)) {
  138. foreach ($strings as $key => $string) {
  139. $this->strings[$key] = $string;
  140. }
  141. }
  142. } else {
  143. $this->strings = array();
  144. }
  145. }
  146. if (isset($this->strings[$name])) {
  147. return $this->strings[$name];
  148. }
  149. return get_lang($name);
  150. }
  151. }