dashboard.class.inc.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 FromParams($aParams)
  57. {
  58. }
  59. public function Save()
  60. {
  61. }
  62. public function GetLayout()
  63. {
  64. return $this->sLayoutClass;
  65. }
  66. public function SetLayout($sLayoutClass)
  67. {
  68. $this->sLayoutClass = $sLayoutClass;
  69. }
  70. public function GetTitle()
  71. {
  72. return $this->sTitle;
  73. }
  74. public function SetTitle($sTitle)
  75. {
  76. $this->sTitle = $sTitle;
  77. }
  78. public function AddDashlet()
  79. {
  80. }
  81. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  82. {
  83. $oPage->add('<h1>'.$this->sTitle.'</h1>');
  84. $oLayout = new $this->sLayoutClass;
  85. $oLayout->Render($oPage, $this->aDashlets, $bEditMode, $aExtraParams);
  86. if (!$bEditMode)
  87. {
  88. $oPage->add_linked_script('../js/dashlet.js');
  89. $oPage->add_linked_script('../js/property_field.js');
  90. }
  91. }
  92. public function RenderProperties($oPage)
  93. {
  94. // menu to pick a layout and edit other properties of the dashboard
  95. $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>');
  96. $sUrl = utils::GetAbsoluteUrlAppRoot();
  97. $oPage->add('<div style="text-align:center">Layout:</div>');
  98. $oPage->add('<div id="select_layout" style="text-align:center">');
  99. foreach( get_declared_classes() as $sLayoutClass)
  100. {
  101. if (is_subclass_of($sLayoutClass, 'DashboardLayout'))
  102. {
  103. $oReflection = new ReflectionClass($sLayoutClass);
  104. if (!$oReflection->isAbstract())
  105. {
  106. $aCallSpec = array($sLayoutClass, 'GetInfo');
  107. $aInfo = call_user_func($aCallSpec);
  108. $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 !
  109. }
  110. }
  111. }
  112. $oPage->add('</div>');
  113. $oPage->add('</div>');
  114. $oPage->add_ready_script("$('#select_layout').buttonset();");
  115. }
  116. public function RenderDashletsSelection($oPage)
  117. {
  118. // Toolbox/palette to drag and drop dashlets
  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;">Available Dashlets</div>');
  120. $sUrl = utils::GetAbsoluteUrlAppRoot();
  121. $oPage->add('<div id="select_dashlet" style="text-align:center">');
  122. foreach( get_declared_classes() as $sDashletClass)
  123. {
  124. if (is_subclass_of($sDashletClass, 'Dashlet'))
  125. {
  126. $oReflection = new ReflectionClass($sDashletClass);
  127. if (!$oReflection->isAbstract())
  128. {
  129. $aCallSpec = array($sDashletClass, 'GetInfo');
  130. $aInfo = call_user_func($aCallSpec);
  131. $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>');
  132. }
  133. }
  134. }
  135. $oPage->add('</div>');
  136. $oPage->add('</div>');
  137. $oPage->add_ready_script("$('.dashlet_icon').draggable({helper: 'clone', appendTo: 'body', zIndex: 10000, revert:'invalid'});");
  138. $oPage->add_ready_script("$('.layout_cell').droppable({accept:'.dashlet_icon', hoverClass:'dragHover'});");
  139. }
  140. public function RenderDashletsProperties($oPage)
  141. {
  142. // Toolbox/palette to edit the properties of each dashlet
  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;">Dashlet Properties</div>');
  144. $oPage->add('<div id="dashlet_properties" style="text-align:center">');
  145. foreach($this->aDashlets as $oDashlet)
  146. {
  147. $sId = $oDashlet->GetID();
  148. $sClass = get_class($oDashlet);
  149. $oPage->add('<div class="dashlet_properties" id="dashlet_properties_'.$sId.'" style="display:none">');
  150. $oForm = $oDashlet->GetForm($oPage);
  151. $this->SetFormParams($oForm);
  152. $oForm->RenderAsPropertySheet($oPage);
  153. $oPage->add('</div>');
  154. }
  155. $oPage->add('</div>');
  156. $oPage->add('</div>');
  157. }
  158. abstract protected function SetFormParams($oForm);
  159. }
  160. class RuntimeDashboard extends Dashboard
  161. {
  162. protected function SetFormParams($oForm)
  163. {
  164. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  165. }
  166. public function Save()
  167. {
  168. }
  169. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  170. {
  171. parent::Render($oPage, $bEditMode, $aExtraParams);
  172. if (!$bEditMode)
  173. {
  174. $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>');
  175. $oPage->add_ready_script("$('#top-bar').prepend('$sEditBtn');");
  176. $oPage->add_script(
  177. <<<EOF
  178. function EditDashboard(sId)
  179. {
  180. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', {operation: 'dashboard_editor', id: sId},
  181. function(data)
  182. {
  183. $('body').append(data);
  184. }
  185. );
  186. return false;
  187. }
  188. EOF
  189. );
  190. }
  191. }
  192. public function RenderEditor($oPage)
  193. {
  194. $oPage->add('<div id="dashboard_editor">');
  195. $oPage->add('<div class="ui-layout-center">');
  196. $this->Render($oPage, true);
  197. $oPage->add('</div>');
  198. $oPage->add('<div class="ui-layout-east">');
  199. $this->RenderProperties($oPage);
  200. $this->RenderDashletsSelection($oPage);
  201. $this->RenderDashletsProperties($oPage);
  202. $oPage->add('</div>');
  203. $oPage->add('<div id="event_bus"/>'); // For exchanging messages between the panes, same as in the designer
  204. $oPage->add('</div>');
  205. $sDialogTitle = 'Dashboard Editor';
  206. $sOkButtonLabel = Dict::S('UI:Button:Ok');
  207. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  208. $oPage->add_ready_script(
  209. <<<EOF
  210. $('#dashboard_editor').dialog({
  211. height: $('body').height() - 50,
  212. width: $('body').width() - 50,
  213. modal: true,
  214. title: '$sDialogTitle',
  215. buttons: [
  216. { text: "$sOkButtonLabel", click: function() {
  217. $(this).dialog( "close" ); $(this).remove();
  218. } },
  219. { text: "$sCancelButtonLabel", click: function() { $(this).dialog( "close" ); $(this).remove(); } },
  220. ],
  221. close: function() { $(this).remove(); }
  222. });
  223. $('#event_bus').bind('dashlet-selected', function(event, data){
  224. var sDashletId = data.dashlet_id;
  225. var sPropId = 'dashlet_properties_'+sDashletId;
  226. $('.dashlet_properties').each(function() {
  227. var sId = $(this).attr('id');
  228. var bShow = (sId == sPropId);
  229. if (bShow)
  230. {
  231. $(this).show();
  232. }
  233. else
  234. {
  235. $(this).hide();
  236. }
  237. });
  238. });
  239. EOF
  240. );
  241. $oPage->add_ready_script("$('#dashboard_editor').layout();");
  242. }
  243. }