dashboard.class.inc.php 11 KB

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