dashboard.class.inc.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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/dashboardlayout.class.inc.php');
  17. require_once(APPROOT.'application/dashlet.class.inc.php');
  18. /**
  19. * A user editable dashboard page
  20. *
  21. */
  22. abstract class Dashboard
  23. {
  24. protected $sTitle;
  25. protected $sLayoutClass;
  26. protected $aWidgetsData;
  27. protected $oDOMNode;
  28. protected $sId;
  29. public function __construct($sId)
  30. {
  31. $this->sLayoutClass = null;
  32. $this->aDashlets = array();
  33. $this->oDOMNode = null;
  34. $this->sId = $sId;
  35. }
  36. public function FromXml($sXml)
  37. {
  38. $oDoc = new DOMDocument();
  39. $oDoc->loadXML($sXml);
  40. $this->oDOMNode = $oDoc->getElementsByTagName('dashboard')->item(0);
  41. $oLayoutNode = $this->oDOMNode->getElementsByTagName('layout')->item(0);
  42. $this->sLayoutClass = $oLayoutNode->textContent;
  43. $oTitleNode = $this->oDOMNode->getElementsByTagName('title')->item(0);
  44. $this->sTitle = $oTitleNode->textContent;
  45. $oDashletsNode = $this->oDOMNode->getElementsByTagName('dashlets')->item(0);
  46. $oDashletList = $oDashletsNode->getElementsByTagName('dashlet');
  47. foreach($oDashletList as $oDomNode)
  48. {
  49. $sDashletClass = $oDomNode->getAttribute('xsi:type');
  50. $sId = $oDomNode->getAttribute('id');
  51. $oNewDashlet = new $sDashletClass($sId);
  52. $oNewDashlet->FromDOMNode($oDomNode);
  53. $this->aDashlets[] = $oNewDashlet;
  54. }
  55. }
  56. public function ToXml()
  57. {
  58. $oDoc = new DOMDocument();
  59. $oDoc->formatOutput = true; // indent (must be loaded with option LIBXML_NOBLANKS)
  60. $oDoc->preserveWhiteSpace = true; // otherwise the formatOutput option would have no effect
  61. $oMainNode = $oDoc->createElement('dashboard');
  62. $oDoc->appendChild($oMainNode);
  63. $oNode = $oDoc->createElement('layout', $this->sLayoutClass);
  64. $oMainNode->appendChild($oNode);
  65. $oNode = $oDoc->createElement('title', $this->sTitle);
  66. $oMainNode->appendChild($oNode);
  67. $oDashletsNode = $oDoc->createElement('dashlets');
  68. $oMainNode->appendChild($oDashletsNode);
  69. foreach ($this->aDashlets as $oDashlet)
  70. {
  71. $oNode = $oDoc->createElement('dashlet');
  72. $oDashletsNode->appendChild($oNode);
  73. $oNode->setAttribute('id', $oDashlet->GetID());
  74. $oNode->setAttribute('xsi:type', get_class($oDashlet));
  75. $oDashlet->ToDOMNode($oNode);
  76. }
  77. $sXml = $oDoc->saveXML();
  78. return $sXml;
  79. }
  80. public function FromParams($aParams)
  81. {
  82. }
  83. public function Save()
  84. {
  85. }
  86. public function GetLayout()
  87. {
  88. return $this->sLayoutClass;
  89. }
  90. public function SetLayout($sLayoutClass)
  91. {
  92. $this->sLayoutClass = $sLayoutClass;
  93. }
  94. public function GetTitle()
  95. {
  96. return $this->sTitle;
  97. }
  98. public function SetTitle($sTitle)
  99. {
  100. $this->sTitle = $sTitle;
  101. }
  102. public function AddDashlet()
  103. {
  104. }
  105. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  106. {
  107. $oPage->add('<h1>'.$this->sTitle.'</h1>');
  108. $oLayout = new $this->sLayoutClass;
  109. $oLayout->Render($oPage, $this->aDashlets, $bEditMode, $aExtraParams);
  110. if (!$bEditMode)
  111. {
  112. $oPage->add_linked_script('../js/dashlet.js');
  113. $oPage->add_linked_script('../js/property_field.js');
  114. }
  115. }
  116. public function RenderProperties($oPage)
  117. {
  118. // menu to pick a layout and edit other properties of the dashboard
  119. $oPage->add('<div class="ui-widget-content ui-corner-all"><div class="ui-widget-header ui-corner-all" style="text-align:center; padding: 2px;">Dashboard Properties</div>');
  120. $sUrl = utils::GetAbsoluteUrlAppRoot();
  121. $oPage->add('<div style="text-align:center">Layout:</div>');
  122. $oPage->add('<div id="select_layout" style="text-align:center">');
  123. foreach( get_declared_classes() as $sLayoutClass)
  124. {
  125. if (is_subclass_of($sLayoutClass, 'DashboardLayout'))
  126. {
  127. $oReflection = new ReflectionClass($sLayoutClass);
  128. if (!$oReflection->isAbstract())
  129. {
  130. $aCallSpec = array($sLayoutClass, 'GetInfo');
  131. $aInfo = call_user_func($aCallSpec);
  132. $oPage->add('<input type="radio" name="layout_class" id="layout_'.$sLayoutClass.'"><label for="layout_'.$sLayoutClass.'"><img src="'.$sUrl.$aInfo['icon'].'" /></label>'); // title="" on either the img or the label does nothing !
  133. }
  134. }
  135. }
  136. $oPage->add('</div>');
  137. $oPage->add('</div>');
  138. $oPage->add_ready_script("$('#select_layout').buttonset();");
  139. }
  140. public function RenderDashletsSelection($oPage)
  141. {
  142. // Toolbox/palette to drag and drop dashlets
  143. $oPage->add('<div class="ui-widget-content ui-corner-all"><div class="ui-widget-header ui-corner-all" style="text-align:center; padding: 2px;">Available Dashlets</div>');
  144. $sUrl = utils::GetAbsoluteUrlAppRoot();
  145. $oPage->add('<div id="select_dashlet" style="text-align:center">');
  146. foreach( get_declared_classes() as $sDashletClass)
  147. {
  148. if (is_subclass_of($sDashletClass, 'Dashlet'))
  149. {
  150. $oReflection = new ReflectionClass($sDashletClass);
  151. if (!$oReflection->isAbstract())
  152. {
  153. $aCallSpec = array($sDashletClass, 'GetInfo');
  154. $aInfo = call_user_func($aCallSpec);
  155. $oPage->add('<span class="dashlet_icon ui-widget-content ui-corner-all" id="dashlet_'.$sDashletClass.'" title="'.$aInfo['label'].'" style="width:34px; height:34px; display:inline-block; margin:2px;"><img src="'.$sUrl.$aInfo['icon'].'" /></span>');
  156. }
  157. }
  158. }
  159. $oPage->add('</div>');
  160. $oPage->add('</div>');
  161. $oPage->add_ready_script("$('.dashlet_icon').draggable({helper: 'clone', appendTo: 'body', zIndex: 10000, revert:'invalid'});");
  162. $oPage->add_ready_script("$('.layout_cell').droppable({accept:'.dashlet_icon', hoverClass:'dragHover'});");
  163. }
  164. public function RenderDashletsProperties($oPage)
  165. {
  166. // Toolbox/palette to edit the properties of each dashlet
  167. $oPage->add('<div class="ui-widget-content ui-corner-all"><div class="ui-widget-header ui-corner-all" style="text-align:center; padding: 2px;">Dashlet Properties</div>');
  168. $oPage->add('<div id="dashlet_properties" style="text-align:center">');
  169. foreach($this->aDashlets as $oDashlet)
  170. {
  171. $sId = $oDashlet->GetID();
  172. $sClass = get_class($oDashlet);
  173. $oPage->add('<div class="dashlet_properties" id="dashlet_properties_'.$sId.'" style="display:none">');
  174. $oForm = $oDashlet->GetForm();
  175. $this->SetFormParams($oForm);
  176. $oForm->RenderAsPropertySheet($oPage);
  177. $oPage->add('</div>');
  178. }
  179. $oPage->add('</div>');
  180. $oPage->add('</div>');
  181. }
  182. abstract protected function SetFormParams($oForm);
  183. }
  184. class RuntimeDashboard extends Dashboard
  185. {
  186. protected function SetFormParams($oForm)
  187. {
  188. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  189. }
  190. public function Save()
  191. {
  192. }
  193. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  194. {
  195. parent::Render($oPage, $bEditMode, $aExtraParams);
  196. if (!$bEditMode)
  197. {
  198. $sEditBtn = addslashes('<div style="display: inline-block; height: 55px; width:200px;vertical-align:center;line-height:60px;text-align:left;"><button onclick="EditDashboard(\''.$this->sId.'\');">Edit This Page</button></div>');
  199. $oPage->add_ready_script("$('#top-bar').prepend('$sEditBtn');");
  200. $oPage->add_script(
  201. <<<EOF
  202. function EditDashboard(sId)
  203. {
  204. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', {operation: 'dashboard_editor', id: sId},
  205. function(data)
  206. {
  207. $('body').append(data);
  208. }
  209. );
  210. return false;
  211. }
  212. EOF
  213. );
  214. }
  215. }
  216. public function RenderEditor($oPage)
  217. {
  218. $oPage->add('<div id="dashboard_editor">');
  219. $oPage->add('<div class="ui-layout-center">');
  220. $this->Render($oPage, true);
  221. $oPage->add('</div>');
  222. $oPage->add('<div class="ui-layout-east">');
  223. $this->RenderProperties($oPage);
  224. $this->RenderDashletsSelection($oPage);
  225. $this->RenderDashletsProperties($oPage);
  226. $oPage->add('</div>');
  227. $oPage->add('<div id="event_bus"/>'); // For exchanging messages between the panes, same as in the designer
  228. $oPage->add('</div>');
  229. $sDialogTitle = 'Dashboard Editor';
  230. $sOkButtonLabel = Dict::S('UI:Button:Ok');
  231. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  232. $oPage->add_ready_script(
  233. <<<EOF
  234. $('#dashboard_editor').dialog({
  235. height: $('body').height() - 50,
  236. width: $('body').width() - 50,
  237. modal: true,
  238. title: '$sDialogTitle',
  239. buttons: [
  240. { text: "$sOkButtonLabel", click: function() {
  241. $(this).dialog( "close" ); $(this).remove();
  242. } },
  243. { text: "$sCancelButtonLabel", click: function() { $(this).dialog( "close" ); $(this).remove(); } },
  244. ],
  245. close: function() { $(this).remove(); }
  246. });
  247. $('#event_bus').bind('dashlet-selected', function(event, data){
  248. var sDashletId = data.dashlet_id;
  249. var sPropId = 'dashlet_properties_'+sDashletId;
  250. $('.dashlet_properties').each(function() {
  251. var sId = $(this).attr('id');
  252. var bShow = (sId == sPropId);
  253. if (bShow)
  254. {
  255. $(this).show();
  256. }
  257. else
  258. {
  259. $(this).hide();
  260. }
  261. });
  262. });
  263. EOF
  264. );
  265. $oPage->add_ready_script("$('#dashboard_editor').layout();");
  266. }
  267. }