dashboard.class.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. $oMainNode->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");
  63. $oDoc->appendChild($oMainNode);
  64. $oNode = $oDoc->createElement('layout', $this->sLayoutClass);
  65. $oMainNode->appendChild($oNode);
  66. $oNode = $oDoc->createElement('title', $this->sTitle);
  67. $oMainNode->appendChild($oNode);
  68. $oDashletsNode = $oDoc->createElement('dashlets');
  69. $oMainNode->appendChild($oDashletsNode);
  70. foreach ($this->aDashlets as $oDashlet)
  71. {
  72. $oNode = $oDoc->createElement('dashlet');
  73. $oDashletsNode->appendChild($oNode);
  74. $oNode->setAttribute('id', $oDashlet->GetID());
  75. $oNode->setAttribute('xsi:type', get_class($oDashlet));
  76. $oDashlet->ToDOMNode($oNode);
  77. }
  78. $sXml = $oDoc->saveXML();
  79. return $sXml;
  80. }
  81. public function FromParams($aParams)
  82. {
  83. $this->sLayoutClass = $aParams['layout_class'];
  84. $this->sTitle = $aParams['title'];
  85. foreach($aParams['dashlets'] as $aDashletParams)
  86. {
  87. $sDashletClass = $aDashletParams['dashlet_class'];
  88. $sId = $aDashletParams['dashlet_id'];
  89. $oNewDashlet = new $sDashletClass($sId);
  90. $oForm = $oNewDashlet->GetForm();
  91. $oForm->SetParamsContainer('dashlet_'.$sId);
  92. $oForm->SetPrefix('');
  93. $aValues = $oForm->ReadParams();
  94. $oNewDashlet->FromParams($aValues);
  95. $this->aDashlets[] = $oNewDashlet;
  96. }
  97. }
  98. public function Save()
  99. {
  100. }
  101. public function GetLayout()
  102. {
  103. return $this->sLayoutClass;
  104. }
  105. public function SetLayout($sLayoutClass)
  106. {
  107. $this->sLayoutClass = $sLayoutClass;
  108. }
  109. public function GetTitle()
  110. {
  111. return $this->sTitle;
  112. }
  113. public function SetTitle($sTitle)
  114. {
  115. $this->sTitle = $sTitle;
  116. }
  117. public function AddDashlet()
  118. {
  119. }
  120. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  121. {
  122. $oPage->add('<h1>'.$this->sTitle.'</h1>');
  123. $oLayout = new $this->sLayoutClass;
  124. $oLayout->Render($oPage, $this->aDashlets, $bEditMode, $aExtraParams);
  125. if (!$bEditMode)
  126. {
  127. $oPage->add_linked_script('../js/dashlet.js');
  128. $oPage->add_linked_script('../js/dashboard.js');
  129. $oPage->add_linked_script('../js/property_field.js');
  130. }
  131. }
  132. public function RenderProperties($oPage)
  133. {
  134. // menu to pick a layout and edit other properties of the dashboard
  135. $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>');
  136. $sUrl = utils::GetAbsoluteUrlAppRoot();
  137. $oPage->add('<div style="text-align:center">Layout:</div>');
  138. $oPage->add('<div id="select_layout" style="text-align:center">');
  139. foreach( get_declared_classes() as $sLayoutClass)
  140. {
  141. if (is_subclass_of($sLayoutClass, 'DashboardLayout'))
  142. {
  143. $oReflection = new ReflectionClass($sLayoutClass);
  144. if (!$oReflection->isAbstract())
  145. {
  146. $aCallSpec = array($sLayoutClass, 'GetInfo');
  147. $aInfo = call_user_func($aCallSpec);
  148. $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 !
  149. }
  150. }
  151. }
  152. $oPage->add('</div>');
  153. $oPage->add('</div>');
  154. $oPage->add_ready_script("$('#select_layout').buttonset();");
  155. }
  156. public function RenderDashletsSelection($oPage)
  157. {
  158. // Toolbox/palette to drag and drop dashlets
  159. $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>');
  160. $sUrl = utils::GetAbsoluteUrlAppRoot();
  161. $oPage->add('<div id="select_dashlet" style="text-align:center">');
  162. foreach( get_declared_classes() as $sDashletClass)
  163. {
  164. if (is_subclass_of($sDashletClass, 'Dashlet'))
  165. {
  166. $oReflection = new ReflectionClass($sDashletClass);
  167. if (!$oReflection->isAbstract())
  168. {
  169. $aCallSpec = array($sDashletClass, 'GetInfo');
  170. $aInfo = call_user_func($aCallSpec);
  171. $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>');
  172. }
  173. }
  174. }
  175. $oPage->add('</div>');
  176. $oPage->add('</div>');
  177. $oPage->add_ready_script("$('.dashlet_icon').draggable({helper: 'clone', appendTo: 'body', zIndex: 10000, revert:'invalid'});");
  178. $oPage->add_ready_script("$('.layout_cell').droppable({accept:'.dashlet_icon', hoverClass:'dragHover'});");
  179. }
  180. public function RenderDashletsProperties($oPage)
  181. {
  182. // Toolbox/palette to edit the properties of each dashlet
  183. $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>');
  184. $oPage->add('<div id="dashlet_properties" style="text-align:center">');
  185. foreach($this->aDashlets as $oDashlet)
  186. {
  187. $sId = $oDashlet->GetID();
  188. $sClass = get_class($oDashlet);
  189. $oPage->add('<div class="dashlet_properties" id="dashlet_properties_'.$sId.'" style="display:none">');
  190. $oForm = $oDashlet->GetForm();
  191. $this->SetFormParams($oForm);
  192. $oForm->RenderAsPropertySheet($oPage);
  193. $oPage->add('</div>');
  194. }
  195. $oPage->add('</div>');
  196. $oPage->add('</div>');
  197. }
  198. abstract protected function SetFormParams($oForm);
  199. }
  200. class RuntimeDashboard extends Dashboard
  201. {
  202. protected function SetFormParams($oForm)
  203. {
  204. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  205. }
  206. public function Save()
  207. {
  208. $sXml = $this->ToXml();
  209. $oUDSearch = new DBObjectSearch('UserDashboard');
  210. $oUDSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  211. $oUDSearch->AddCondition('menu_code', $this->sId, '=');
  212. $oUDSet = new DBObjectSet($oUDSearch);
  213. if ($oUDSet->Count() > 0)
  214. {
  215. // Assuming there is at most one couple {user, menu}!
  216. $oUserDashboard = $oUDSet->Fetch();
  217. $oUserDashboard->Set('contents', $sXml);
  218. $oUserDashboard->DBUpdate();
  219. }
  220. else
  221. {
  222. // No such customized dasboard for the current user, let's create a new record
  223. $oUserDashboard = new UserDashboard();
  224. $oUserDashboard->Set('user_id', UserRights::GetUserId());
  225. $oUserDashboard->Set('menu_code', $this->sId);
  226. $oUserDashboard->Set('contents', $sXml);
  227. $oUserDashboard->DBInsert();
  228. }
  229. }
  230. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  231. {
  232. parent::Render($oPage, $bEditMode, $aExtraParams);
  233. if (!$bEditMode)
  234. {
  235. $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>');
  236. $oPage->add_ready_script("$('#top-bar').prepend('$sEditBtn');");
  237. $oPage->add_script(
  238. <<<EOF
  239. function EditDashboard(sId)
  240. {
  241. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', {operation: 'dashboard_editor', id: sId},
  242. function(data)
  243. {
  244. $('body').append(data);
  245. }
  246. );
  247. return false;
  248. }
  249. EOF
  250. );
  251. }
  252. }
  253. public function RenderEditor($oPage)
  254. {
  255. $oPage->add('<div id="dashboard_editor">');
  256. $oPage->add('<div class="ui-layout-center">');
  257. $this->Render($oPage, true);
  258. $oPage->add('</div>');
  259. $oPage->add('<div class="ui-layout-east">');
  260. $this->RenderProperties($oPage);
  261. $this->RenderDashletsSelection($oPage);
  262. $this->RenderDashletsProperties($oPage);
  263. $oPage->add('</div>');
  264. $oPage->add('<div id="event_bus"/>'); // For exchanging messages between the panes, same as in the designer
  265. $oPage->add('</div>');
  266. $sDialogTitle = 'Dashboard Editor';
  267. $sOkButtonLabel = Dict::S('UI:Button:Ok');
  268. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  269. $sId = addslashes($this->sId);
  270. $sLayoutClass = addslashes($this->sLayoutClass);
  271. $sTitle = addslashes($this->sTitle);
  272. $sUrl = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php';
  273. $oPage->add_ready_script(
  274. <<<EOF
  275. $('#dashboard_editor').dialog({
  276. height: $('body').height() - 50,
  277. width: $('body').width() - 50,
  278. modal: true,
  279. title: '$sDialogTitle',
  280. buttons: [
  281. { text: "$sOkButtonLabel", click: function() {
  282. $('#dashboard_editor .ui-layout-center').dashboard('save'); /* $(this).dialog( "close" ); $(this).remove(); */
  283. } },
  284. { text: "$sCancelButtonLabel", click: function() { $(this).dialog( "close" ); $(this).remove(); } },
  285. ],
  286. close: function() { $(this).remove(); }
  287. });
  288. $('#dashboard_editor .ui-layout-center').dashboard({ dashboard_id: '$sId', layout_class: '$sLayoutClass', title: '$sTitle', submit_to: '$sUrl', submit_parameters: {operation: 'save_dashboard'} });
  289. $('#event_bus').bind('dashlet-selected', function(event, data){
  290. var sDashletId = data.dashlet_id;
  291. var sPropId = 'dashlet_properties_'+sDashletId;
  292. $('.dashlet_properties').each(function() {
  293. var sId = $(this).attr('id');
  294. var bShow = (sId == sPropId);
  295. if (bShow)
  296. {
  297. $(this).show();
  298. }
  299. else
  300. {
  301. $(this).hide();
  302. }
  303. });
  304. });
  305. EOF
  306. );
  307. $oPage->add_ready_script("$('#dashboard_editor').layout({ east__size: 300 });");
  308. }
  309. }