dashboard.class.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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($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>'.Dict::S($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" value="'.$sLayoutClass.'" 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(
  155. <<<EOF
  156. $('#select_layout').buttonset();
  157. $('#select_layout input').click( function() {
  158. var sLayoutClass = $(this).val();
  159. $(':itop-dashboard').dashboard('option', {layout_class: sLayoutClass});
  160. } );
  161. EOF
  162. );
  163. }
  164. public function RenderDashletsSelection($oPage)
  165. {
  166. // Toolbox/palette to drag and drop dashlets
  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;">Available Dashlets</div>');
  168. $sUrl = utils::GetAbsoluteUrlAppRoot();
  169. $oPage->add('<div id="select_dashlet" style="text-align:center">');
  170. foreach( get_declared_classes() as $sDashletClass)
  171. {
  172. if (is_subclass_of($sDashletClass, 'Dashlet'))
  173. {
  174. $oReflection = new ReflectionClass($sDashletClass);
  175. if (!$oReflection->isAbstract())
  176. {
  177. $aCallSpec = array($sDashletClass, 'IsVisible');
  178. $bVisible = call_user_func($aCallSpec);
  179. if ($bVisible)
  180. {
  181. $aCallSpec = array($sDashletClass, 'GetInfo');
  182. $aInfo = call_user_func($aCallSpec);
  183. $oPage->add('<span dashlet_class="'.$sDashletClass.'" 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>');
  184. }
  185. }
  186. }
  187. }
  188. $oPage->add('</div>');
  189. $oPage->add('</div>');
  190. $oPage->add_ready_script("$('.dashlet_icon').draggable({helper: 'clone', appendTo: 'body', zIndex: 10000, revert:'invalid'});");
  191. $oPage->add_ready_script("$('.layout_cell').droppable({accept:'.dashlet_icon', hoverClass:'dragHover'});");
  192. }
  193. public function RenderDashletsProperties($oPage)
  194. {
  195. // Toolbox/palette to edit the properties of each dashlet
  196. $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>');
  197. $oPage->add('<div id="dashlet_properties" style="text-align:center">');
  198. foreach($this->aDashlets as $oDashlet)
  199. {
  200. $sId = $oDashlet->GetID();
  201. $sClass = get_class($oDashlet);
  202. if ($oDashlet->IsVisible())
  203. {
  204. $oPage->add('<div class="dashlet_properties" id="dashlet_properties_'.$sId.'" style="display:none">');
  205. $oForm = $oDashlet->GetForm();
  206. $this->SetFormParams($oForm);
  207. $oForm->RenderAsPropertySheet($oPage);
  208. $oPage->add('</div>');
  209. }
  210. }
  211. $oPage->add('</div>');
  212. $oPage->add('</div>');
  213. }
  214. abstract protected function SetFormParams($oForm);
  215. }
  216. class RuntimeDashboard extends Dashboard
  217. {
  218. protected function SetFormParams($oForm)
  219. {
  220. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  221. }
  222. public function Save()
  223. {
  224. $sXml = $this->ToXml();
  225. $oUDSearch = new DBObjectSearch('UserDashboard');
  226. $oUDSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  227. $oUDSearch->AddCondition('menu_code', $this->sId, '=');
  228. $oUDSet = new DBObjectSet($oUDSearch);
  229. if ($oUDSet->Count() > 0)
  230. {
  231. // Assuming there is at most one couple {user, menu}!
  232. $oUserDashboard = $oUDSet->Fetch();
  233. $oUserDashboard->Set('contents', $sXml);
  234. $oUserDashboard->DBUpdate();
  235. }
  236. else
  237. {
  238. // No such customized dasboard for the current user, let's create a new record
  239. $oUserDashboard = new UserDashboard();
  240. $oUserDashboard->Set('user_id', UserRights::GetUserId());
  241. $oUserDashboard->Set('menu_code', $this->sId);
  242. $oUserDashboard->Set('contents', $sXml);
  243. $oUserDashboard->DBInsert();
  244. }
  245. }
  246. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  247. {
  248. parent::Render($oPage, $bEditMode, $aExtraParams);
  249. if (!$bEditMode)
  250. {
  251. $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>');
  252. $oPage->add_ready_script("$('#top-bar').prepend('$sEditBtn');");
  253. $oPage->add_script(
  254. <<<EOF
  255. function EditDashboard(sId)
  256. {
  257. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', {operation: 'dashboard_editor', id: sId},
  258. function(data)
  259. {
  260. $('body').append(data);
  261. }
  262. );
  263. return false;
  264. }
  265. EOF
  266. );
  267. }
  268. }
  269. public function RenderEditor($oPage)
  270. {
  271. $oPage->add('<div id="dashboard_editor">');
  272. $oPage->add('<div class="ui-layout-center">');
  273. $this->Render($oPage, true);
  274. $oPage->add('</div>');
  275. $oPage->add('<div class="ui-layout-east">');
  276. $this->RenderProperties($oPage);
  277. $this->RenderDashletsSelection($oPage);
  278. $this->RenderDashletsProperties($oPage);
  279. $oPage->add('</div>');
  280. $oPage->add('<div id="event_bus"/>'); // For exchanging messages between the panes, same as in the designer
  281. $oPage->add('</div>');
  282. $sDialogTitle = 'Dashboard Editor';
  283. $sOkButtonLabel = Dict::S('UI:Button:Ok');
  284. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  285. $sId = addslashes($this->sId);
  286. $sLayoutClass = addslashes($this->sLayoutClass);
  287. $sTitle = addslashes($this->sTitle);
  288. $sUrl = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php';
  289. $oPage->add_ready_script(
  290. <<<EOF
  291. $('#dashboard_editor').dialog({
  292. height: $('body').height() - 50,
  293. width: $('body').width() - 50,
  294. modal: true,
  295. title: '$sDialogTitle',
  296. buttons: [
  297. { text: "$sOkButtonLabel", click: function() {
  298. $('#dashboard_editor .ui-layout-center').dashboard('save'); /* $(this).dialog( "close" ); $(this).remove(); */
  299. } },
  300. { text: "$sCancelButtonLabel", click: function() { $(this).dialog( "close" ); $(this).remove(); } },
  301. ],
  302. close: function() { $(this).remove(); }
  303. });
  304. $('#dashboard_editor .ui-layout-center').dashboard({
  305. dashboard_id: '$sId', layout_class: '$sLayoutClass', title: '$sTitle',
  306. submit_to: '$sUrl', submit_parameters: {operation: 'save_dashboard'},
  307. render_to: '$sUrl', render_parameters: {operation: 'render_dashboard'},
  308. new_dashlet_parameters: {operation: 'new_dashlet'}
  309. });
  310. $('#select_dashlet').droppable({
  311. accept: '.dashlet',
  312. drop: function(event, ui) {
  313. $( this ).find( ".placeholder" ).remove();
  314. var oDashlet = ui.draggable;
  315. oDashlet.remove();
  316. },
  317. });
  318. $('#event_bus').bind('dashlet-selected', function(event, data){
  319. var sDashletId = data.dashlet_id;
  320. var sPropId = 'dashlet_properties_'+sDashletId;
  321. $('.dashlet_properties').each(function() {
  322. var sId = $(this).attr('id');
  323. var bShow = (sId == sPropId);
  324. if (bShow)
  325. {
  326. $(this).show();
  327. }
  328. else
  329. {
  330. $(this).hide();
  331. }
  332. });
  333. });
  334. EOF
  335. );
  336. $oPage->add_ready_script("$('#dashboard_editor').layout({ east__size: 300 });");
  337. }
  338. }