dashlet.class.inc.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. // Copyright (C) 2012 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. require_once(APPROOT.'application/forms.class.inc.php');
  17. /**
  18. * Base class for all 'dashlets' (i.e. widgets to be inserted into a dashboard)
  19. *
  20. */
  21. abstract class Dashlet
  22. {
  23. protected $sId;
  24. protected $bRedrawNeeded;
  25. protected $bFormRedrawNeeded;
  26. public function __construct($sId)
  27. {
  28. $this->sId = $sId;
  29. $this->bRedrawNeeded = true; // By default: redraw each time a property changes
  30. $this->bFormRedrawNeeded = false; // By default: no need to redraw the form (independent fields)
  31. }
  32. public function FromDOMNode($oDOMNode)
  33. {
  34. }
  35. public function FromXml($sXml)
  36. {
  37. }
  38. public function FromParams($aParams)
  39. {
  40. }
  41. public function DoRender($oPage, $bEditMode = false, $aExtraParams = array())
  42. {
  43. if ($bEditMode)
  44. {
  45. $sId = $this->GetID();
  46. $oPage->add('<div class="dashlet" id="dashlet_'.$sId.'">');
  47. }
  48. else
  49. {
  50. $oPage->add('<div class="dashlet">');
  51. }
  52. $this->Render($oPage, $bEditMode, $aExtraParams);
  53. $oPage->add('</div>');
  54. if ($bEditMode)
  55. {
  56. $sClass = get_class($this);
  57. $oPage->add_ready_script(
  58. <<<EOF
  59. $('#dashlet_$sId').dashlet({dashlet_id: '$sId', dashlet_class: '$sClass'});
  60. EOF
  61. );
  62. }
  63. }
  64. public function GetID()
  65. {
  66. return $this->sId;
  67. }
  68. abstract public function Render($oPage, $bEditMode = false, $aExtraParams = array());
  69. abstract public function GetPropertiesFields(DesignerForm $oForm);
  70. public function ToXml(DOMNode $oContainerNode)
  71. {
  72. }
  73. public function Update($aValues, $aUpdatedFields)
  74. {
  75. }
  76. public function IsRedrawNeeded()
  77. {
  78. return $this->bRedrawNeeded;
  79. }
  80. public function IsFormRedrawNeeded()
  81. {
  82. return $this->bFormRedrawNeeded;
  83. }
  84. static public function GetInfo()
  85. {
  86. return array(
  87. 'label' => '',
  88. 'icon' => '',
  89. 'description' => '',
  90. );
  91. }
  92. public function GetForm($oPage, $bReturnHTML = false)
  93. {
  94. $oForm = new DesignerForm();
  95. $oForm->SetPrefix("dashlet_". $this->GetID());
  96. $oForm->SetParamsContainer('params');
  97. $this->GetPropertiesFields($oForm);
  98. $oDashletClassField = new DesignerHiddenField('dashlet_class', '', get_class($this));
  99. $oForm->AddField($oDashletClassField);
  100. $oDashletIdField = new DesignerHiddenField('dashlet_id', '', $this->GetID());
  101. $oForm->AddField($oDashletIdField);
  102. return $oForm;
  103. }
  104. }
  105. class DashletHelloWorld extends Dashlet
  106. {
  107. protected $sText;
  108. public function __construct($sId)
  109. {
  110. parent::__construct($sId);
  111. $this->sText = 'Hello World';
  112. }
  113. public function FromDOMNode($oDOMNode)
  114. {
  115. //$this->sText = 'Hello World!';
  116. }
  117. public function FromXml($sXml)
  118. {
  119. //$this->sText = 'Hello World!';
  120. }
  121. public function FromParams($aParams)
  122. {
  123. $this->sText = $aParams['text'];
  124. }
  125. public function Update($aValues, $aUpdatedFields)
  126. {
  127. foreach($aUpdatedFields as $sProp)
  128. {
  129. switch($sProp)
  130. {
  131. case 'text':
  132. $this->sText = $aValues['text'];
  133. break;
  134. }
  135. }
  136. }
  137. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  138. {
  139. $oPage->add('<div style="text-align:center; line-height:5em" class="dashlet-content"><span>'.$this->sText.'</span></div>');
  140. }
  141. public function GetPropertiesFields(DesignerForm $oForm)
  142. {
  143. $oField = new DesignerTextField('text', 'Text', $this->sText);
  144. $oForm->AddField($oField);
  145. }
  146. public function ToXml(DOMNode $oContainerNode)
  147. {
  148. $oNewNodeNode = $oContainerNode->ownerDocument->createElement('hello_world', 'test');
  149. $oContainerNode->appendChild($oNewNodeNode);
  150. }
  151. static public function GetInfo()
  152. {
  153. return array(
  154. 'label' => 'Hello World',
  155. 'icon' => 'images/dashlet-text.png',
  156. 'description' => 'Hello World test Dashlet',
  157. );
  158. }
  159. }
  160. class DashletFakeBarChart extends Dashlet
  161. {
  162. public function __construct($sId)
  163. {
  164. parent::__construct($sId);
  165. }
  166. public function FromDOMNode($oDOMNode)
  167. {
  168. }
  169. public function FromXml($sXml)
  170. {
  171. }
  172. public function FromParams($aParams)
  173. {
  174. }
  175. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  176. {
  177. $oPage->add('<div style="text-align:center" class="dashlet-content"><div>Fake Bar Chart</div><divp><img src="../images/fake-bar-chart.png"/></div></div>');
  178. }
  179. public function GetPropertiesFields(DesignerForm $oForm, $oDashlet = null)
  180. {
  181. }
  182. public function ToXml(DOMNode $oContainerNode)
  183. {
  184. $oNewNodeNode = $oContainerNode->ownerDocument->createElement('fake_bar_chart', 'test');
  185. $oContainerNode->appendChild($oNewNodeNode);
  186. }
  187. static public function GetInfo()
  188. {
  189. return array(
  190. 'label' => 'Bar Chart',
  191. 'icon' => 'images/dashlet-bar-chart.png',
  192. 'description' => 'Fake Bar Chart (for testing)',
  193. );
  194. }
  195. }
  196. class DashletFakePieChart extends Dashlet
  197. {
  198. public function __construct($sId)
  199. {
  200. parent::__construct($sId);
  201. }
  202. public function FromDOMNode($oDOMNode)
  203. {
  204. }
  205. public function FromXml($sXml)
  206. {
  207. }
  208. public function FromParams($aParams)
  209. {
  210. }
  211. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  212. {
  213. $oPage->add('<div style="text-align:center" class="dashlet-content"><div>Fake Pie Chart</div><div><img src="../images/fake-pie-chart.png"/></div></div>');
  214. }
  215. public function GetPropertiesFields(DesignerForm $oForm, $oDashlet = null)
  216. {
  217. }
  218. public function ToXml(DOMNode $oContainerNode)
  219. {
  220. $oNewNodeNode = $oContainerNode->ownerDocument->createElement('fake_pie_chart', 'test');
  221. $oContainerNode->appendChild($oNewNodeNode);
  222. }
  223. static public function GetInfo()
  224. {
  225. return array(
  226. 'label' => 'Pie Chart',
  227. 'icon' => 'images/dashlet-pie-chart.png',
  228. 'description' => 'Fake Pie Chart (for testing)',
  229. );
  230. }
  231. }