You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
6.6 KiB

  1. <?php
  2. /**
  3. * @file Widget.php
  4. * This file is part of MOVIM.
  5. *
  6. * @brief A widget interface.
  7. *
  8. * @author Guillaume Pasquet <etenil@etenilsrealm.nl>
  9. *
  10. * @version 1.0
  11. * @date 20 October 2010
  12. *
  13. * Copyright (C)2010 MOVIM Project
  14. *
  15. * See COPYING for licensing information.
  16. */
  17. class WidgetBase
  18. {
  19. protected $js = array(); /*< Contains javascripts. */
  20. protected $css = array(); /*< Contains CSS files. */
  21. protected $external; /*< Boolean: TRUE if not a system widget. */
  22. protected $ajax; /*< Contains ajax client code. */
  23. protected $tpl;
  24. protected $user;
  25. protected $name;
  26. public $events;
  27. protected $cached;
  28. /**
  29. * Initialises Widget stuff.
  30. * @param external is optional, true if the widget is external (an add-on) to Movim.
  31. */
  32. function __construct($external = true)
  33. {
  34. // Put default widget init here.
  35. $this->external = $external;
  36. $this->ajax = AjaxController::getInstance();
  37. $this->user = new User;
  38. $db = modl\Modl::getInstance();
  39. $u = new User();
  40. $db->setUser($u->getLogin());
  41. // Generating ajax calls.
  42. $refl = new ReflectionClass(get_class($this));
  43. $meths = $refl->getMethods();
  44. foreach($meths as $method) {
  45. if(preg_match('#^ajax#', $method->name)) {
  46. $pars = $method->getParameters();
  47. $params = array();
  48. foreach($pars as $param) {
  49. $params[] = $param->name;
  50. }
  51. $this->ajax->defun(get_class($this), $method->name, $params);
  52. }
  53. }
  54. // We load the template engine
  55. $this->view = new RainTPL;
  56. $this->view->configure('tpl_dir', $this->respath('', true));
  57. $this->view->configure('cache_dir', CACHE_PATH);
  58. $this->view->configure('tpl_ext', 'tpl');
  59. $this->view->assign('c', $this);
  60. $this->name = get_class($this);
  61. $this->WidgetLoad();
  62. }
  63. function t() {
  64. return call_user_func_array('t',func_get_args());
  65. }
  66. function route() {
  67. return call_user_func_array('Route::urlize',func_get_args());
  68. }
  69. function WidgetLoad()
  70. {
  71. }
  72. /**
  73. * Generates the widget's HTML code.
  74. */
  75. function build()
  76. {
  77. echo $this->draw();
  78. }
  79. /*
  80. * @desc Preload some sourcecode for the draw method
  81. */
  82. function display()
  83. {
  84. }
  85. /**
  86. * Return the template's HTML code
  87. * @param a specific template name to load (like Ruby partials)
  88. * @param load the parent template, like for WidgetCommon
  89. */
  90. function draw()
  91. {
  92. $this->display();
  93. return trim($this->view->draw(strtolower($this->name), true));
  94. }
  95. protected function tpl() {
  96. $view = new RainTPL;
  97. $view->configure('tpl_dir', APP_PATH.'widgets/'.$this->name.'/');
  98. $view->configure('cache_dir', CACHE_PATH);
  99. $view->configure('tpl_ext', 'tpl');
  100. $view->assign('c', $this);
  101. return $view;
  102. }
  103. /**
  104. * Returns the path to the specified widget file.
  105. * @param file is the file's name to make up the path for.
  106. * @param fspath is optional, returns the OS path if true, the URL by default.
  107. */
  108. protected function respath($file, $fspath = false, $parent = false)
  109. {
  110. if($parent == false)
  111. $folder = get_class($this);
  112. else
  113. $folder = get_parent_class($this);
  114. $path = '';
  115. if(!$this->external) {
  116. $path = 'app/';
  117. }
  118. $path .= 'app/widgets/' . $folder . '/' . $file;
  119. if($fspath) {
  120. $path = DOCUMENT_ROOT . '/'.$path;
  121. } else {
  122. $path = BASE_URI . $path;
  123. }
  124. return $path;
  125. }
  126. public function getName()
  127. {
  128. return $this->name;
  129. }
  130. /**
  131. * Generates and print an ajax call.
  132. */
  133. protected function callAjax($funcname)
  134. {
  135. echo $this->makeCallAjax(func_get_args());
  136. }
  137. /**
  138. * Calls an the ajax function of another widget.
  139. */
  140. protected function callWidget($widgetname, $funcname)
  141. {
  142. $params = func_get_args();
  143. echo $this->makeCallAjax(array_slice($params, 1), $widgetname);
  144. }
  145. /**
  146. * Returns the javascript ajax call.
  147. */
  148. protected function genCallAjax($funcname)
  149. {
  150. return $this->makeCallAjax(func_get_args());
  151. }
  152. /**
  153. * Returns the javascript call to another widget's ajax function.
  154. */
  155. protected function genCallWidget($widgetname, $funcname)
  156. {
  157. $params = func_get_args();
  158. return $this->makeCallAjax(array_slice($params, 1), $widgetname);
  159. }
  160. protected function makeCallAjax($params, $widget=false)
  161. {
  162. if(!$widget) {
  163. $widget = get_class($this);
  164. }
  165. $funcname = array_shift($params);
  166. $args = implode(', ', $params);
  167. return $widget . '_' . $funcname . "(" . $args . ");";
  168. }
  169. /**
  170. * Adds a javascript file to this widget.
  171. */
  172. protected function addjs($filename)
  173. {
  174. $this->js[] = $this->respath($filename);
  175. }
  176. /**
  177. * returns the list of javascript files to be loaded for the widget.
  178. */
  179. public function loadjs()
  180. {
  181. return $this->js;
  182. }
  183. /**
  184. * Adds a javascript file to this widget.
  185. */
  186. protected function addcss($filename)
  187. {
  188. $this->css[] = $this->respath($filename);
  189. }
  190. /**
  191. * Registers an event handler.
  192. */
  193. protected function registerEvent($type, $function)
  194. {
  195. if(!is_array($this->events)
  196. || !array_key_exists($type, $this->events)) {
  197. $this->events[$type] = array($function);
  198. } else {
  199. $this->events[$type][] = $function;
  200. }
  201. }
  202. /**
  203. * Runs all events of a given type.
  204. */
  205. public function runEvents($proto)
  206. {
  207. if(is_array($this->events) && array_key_exists($proto['type'], $this->events)) {
  208. $returns = array();
  209. foreach($this->events[$proto['type']] as $handler) {
  210. $returns[] = call_user_func(array($this, $handler), $proto['data']);
  211. }
  212. return $returns;
  213. }
  214. }
  215. public function isEvents($proto)
  216. {
  217. if(is_array($this->events) &&
  218. array_key_exists($proto['type'], $this->events) &&
  219. $this->cached == true) {
  220. return true;
  221. }
  222. }
  223. /**
  224. * returns the list of javascript files to be loaded for the widget.
  225. */
  226. public function loadcss()
  227. {
  228. return $this->css;
  229. }
  230. }
  231. ?>