uilinkswizard.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Class UILinksWizard
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class UILinksWizard
  25. {
  26. protected $m_sClass;
  27. protected $m_sLinkageAttr;
  28. protected $m_iObjectId;
  29. protected $m_sLinkedClass;
  30. protected $m_sLinkingAttCode;
  31. protected $m_aEditableFields;
  32. protected $m_aTableConfig;
  33. public function __construct($sClass, $sLinkageAttr, $iObjectId, $sLinkedClass = '')
  34. {
  35. $this->m_sClass = $sClass;
  36. $this->m_sLinkageAttr = $sLinkageAttr;
  37. $this->m_iObjectId = $iObjectId;
  38. $this->m_sLinkedClass = $sLinkedClass; // Will try to guess below, if it's empty
  39. $this->m_sLinkingAttCode = ''; // Will be filled once we've found the attribute corresponding to the linked class
  40. $this->m_aEditableFields = array();
  41. $this->m_aTableConfig = array();
  42. $this->m_aTableConfig['form::checkbox'] = array( 'label' => "<input class=\"select_all\" type=\"checkbox\" value=\"1\" onChange=\"var value = this.checked; $('.selection').each( function() { this.checked = value; } );OnSelectChange();\">", 'description' => Dict::S('UI:SelectAllToggle+'));
  43. foreach(MetaModel::GetAttributesList($this->m_sClass) as $sAttCode)
  44. {
  45. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sAttCode);
  46. if ($oAttDef->IsExternalKey() && ($sAttCode != $this->m_sLinkageAttr))
  47. {
  48. if (empty($this->m_sLinkedClass))
  49. {
  50. // This is a class of objects we can manage !
  51. // Since nothing was specify, any class will do !
  52. $this->m_sLinkedClass = $oAttDef->GetTargetClass();
  53. $this->m_sLinkingAttCode = $sAttCode;
  54. }
  55. else if ($this->m_sLinkedClass == $oAttDef->GetTargetClass())
  56. {
  57. // This is the class of objects we want to manage !
  58. $this->m_sLinkingAttCode = $sAttCode;
  59. }
  60. }
  61. else if ( (!$oAttDef->IsExternalKey()) && (!$oAttDef->IsExternalField()))
  62. {
  63. $this->m_aEditableFields[] = $sAttCode;
  64. $this->m_aTableConfig[$sAttCode] = array( 'label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  65. }
  66. }
  67. if (empty($this->m_sLinkedClass))
  68. {
  69. throw( new Exception(Dict::Format('UI:Error:IncorrectLinkDefinition_LinkedClass_Class', $sLinkedClass, $sClass)));
  70. }
  71. foreach(MetaModel::GetZListItems($this->m_sLinkedClass, 'list') as $sFieldCode)
  72. {
  73. $oAttDef = MetaModel::GetAttributeDef($this->m_sLinkedClass, $sFieldCode);
  74. $this->m_aTableConfig['static::'.$sFieldCode] = array( 'label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  75. }
  76. }
  77. public function Display(WebPage $oP, $aExtraParams = array())
  78. {
  79. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
  80. $sTargetClass = $oAttDef->GetTargetClass();
  81. $oTargetObj = MetaModel::GetObject($sTargetClass, $this->m_iObjectId);
  82. $oP->set_title("iTop - ".MetaModel::GetName($this->m_sLinkedClass)." objects linked with ".MetaModel::GetName(get_class($oTargetObj)).": ".$oTargetObj->GetRawName());
  83. $oP->add("<div class=\"wizContainer\">\n");
  84. $oP->add("<form method=\"post\">\n");
  85. $oP->add("<div class=\"page_header\">\n");
  86. $oP->add("<input type=\"hidden\" id=\"linksToRemove\" name=\"linksToRemove\" value=\"\">\n");
  87. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"do_modify_links\">\n");
  88. $oP->add("<input type=\"hidden\" name=\"class\" value=\"{$this->m_sClass}\">\n");
  89. $oP->add("<input type=\"hidden\" name=\"linkage\" value=\"{$this->m_sLinkageAttr}\">\n");
  90. $oP->add("<input type=\"hidden\" name=\"object_id\" value=\"{$this->m_iObjectId}\">\n");
  91. $oP->add("<input type=\"hidden\" name=\"linking_attcode\" value=\"{$this->m_sLinkingAttCode}\">\n");
  92. $oP->add("<h1>".Dict::Format('UI:ManageObjectsOf_Class_LinkedWith_Class_Instance', MetaModel::GetName($this->m_sLinkedClass), MetaModel::GetName(get_class($oTargetObj)), "<span class=\"hilite\">".$oTargetObj->GetHyperlink()."</span>")."</h1>\n");
  93. $oP->add("</div>\n");
  94. $oP->add_script(
  95. <<<EOF
  96. function OnSelectChange()
  97. {
  98. var nbChecked = $('.selection:checked').length;
  99. if (nbChecked > 0)
  100. {
  101. $('#btnRemove').removeAttr('disabled');
  102. }
  103. else
  104. {
  105. $('#btnRemove').attr('disabled','disabled');
  106. }
  107. }
  108. function RemoveSelected()
  109. {
  110. $('.selection:checked').each(
  111. function()
  112. {
  113. $('#linksToRemove').val($('#linksToRemove').val() + ' ' + this.value);
  114. $('#row_'+this.value).remove();
  115. }
  116. );
  117. // Disable the button since all the selected items have been removed
  118. $('#btnRemove').attr('disabled','disabled');
  119. // Re-run the zebra plugin to properly highlight the remaining lines
  120. $('.listResults').trigger('update');
  121. }
  122. function AddObjects()
  123. {
  124. // TO DO: compute the list of objects already linked with the current Object
  125. $.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', { 'operation': 'addObjects',
  126. 'class': '{$this->m_sClass}',
  127. 'linkageAttr': '{$this->m_sLinkageAttr}',
  128. 'linkedClass': '{$this->m_sLinkedClass}',
  129. 'objectId': '{$this->m_iObjectId}'
  130. },
  131. function(data)
  132. {
  133. $('#ModalDlg').html(data);
  134. dlgWidth = $(document).width() - 100;
  135. $('#ModalDlg').css('width', dlgWidth);
  136. $('#ModalDlg').css('left', 50);
  137. $('#ModalDlg').css('top', 50);
  138. $('#ModalDlg').dialog( 'open' );
  139. },
  140. 'html'
  141. );
  142. }
  143. function SearchObjectsToAdd(currentFormId)
  144. {
  145. var theMap = { 'class': '{$this->m_sClass}',
  146. 'linkageAttr': '{$this->m_sLinkageAttr}',
  147. 'linkedClass': '{$this->m_sLinkedClass}',
  148. 'objectId': '{$this->m_iObjectId}'
  149. }
  150. if ($('#'+currentFormId+' :input[name=class]').val() != undefined)
  151. {
  152. theMap.linkedClass = $('#'+currentFormId+' :input[name=class]').val();
  153. }
  154. // Gather the parameters from the search form
  155. $('#'+currentFormId+' :input').each(
  156. function(i)
  157. {
  158. if (this.name != '')
  159. {
  160. theMap[this.name] = this.value;
  161. }
  162. }
  163. );
  164. theMap['operation'] = 'searchObjectsToAdd';
  165. // Run the query and display the results
  166. $.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', theMap,
  167. function(data)
  168. {
  169. $('#SearchResultsToAdd').html(data);
  170. $('#SearchResultsToAdd .listResults').tablesorter( { headers: {0: false}}, widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  171. },
  172. 'html'
  173. );
  174. return false;
  175. }
  176. function DoAddObjects(currentFormId)
  177. {
  178. var theMap = { 'class': '{$this->m_sClass}',
  179. 'linkageAttr': '{$this->m_sLinkageAttr}',
  180. 'linkedClass': '{$this->m_sLinkedClass}',
  181. 'objectId': '{$this->m_iObjectId}'
  182. }
  183. // Gather the parameters from the search form
  184. $('#'+currentFormId+' :input').each(
  185. function(i)
  186. {
  187. if ( (this.name != '') && ((this.type != 'checkbox') || (this.checked)) )
  188. {
  189. //console.log(this.type);
  190. arrayExpr = /\[\]$/;
  191. if (arrayExpr.test(this.name))
  192. {
  193. // Array
  194. if (theMap[this.name] == undefined)
  195. {
  196. theMap[this.name] = new Array();
  197. }
  198. theMap[this.name].push(this.value);
  199. }
  200. else
  201. {
  202. theMap[this.name] = this.value;
  203. }
  204. }
  205. }
  206. );
  207. theMap['operation'] = 'doAddObjects';
  208. // Run the query and display the results
  209. $.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', theMap,
  210. function(data)
  211. {
  212. //console.log('Data: ' + data);
  213. if (data != '')
  214. {
  215. $('#empty_row').remove();
  216. }
  217. $('.listResults tbody').append(data);
  218. $('.listResults').trigger('update');
  219. $('.listResults').tablesorter( { headers: {0: false}}, widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  220. },
  221. 'html'
  222. );
  223. $('#ModalDlg').dialog('close');
  224. return false;
  225. }
  226. function InitForm()
  227. {
  228. // make sure that the form is clean
  229. $('.selection').each( function() { this.checked = false; });
  230. $('#btnRemove').attr('disabled','disabled');
  231. $('#linksToRemove').val('');
  232. }
  233. function SubmitHook()
  234. {
  235. var the_form = this;
  236. SearchObjectsToAdd(the_form.id);
  237. return false;
  238. }
  239. EOF
  240. );
  241. $oP->add_ready_script("InitForm();");
  242. $oFilter = new DBObjectSearch($this->m_sClass);
  243. $oFilter->AddCondition($this->m_sLinkageAttr, $this->m_iObjectId, '=');
  244. $oSet = new DBObjectSet($oFilter);
  245. $aForm = array();
  246. while($oCurrentLink = $oSet->Fetch())
  247. {
  248. $aRow = array();
  249. $key = $oCurrentLink->GetKey();
  250. $oLinkedObj = MetaModel::GetObject($this->m_sLinkedClass, $oCurrentLink->Get($this->m_sLinkingAttCode));
  251. $aForm[$key] = $this->GetFormRow($oP, $oLinkedObj, $oCurrentLink);
  252. }
  253. //var_dump($aTableLabels);
  254. //var_dump($aForm);
  255. $this->DisplayFormTable($oP, $this->m_aTableConfig, $aForm);
  256. $oP->add("<span style=\"float:left;\">&nbsp;&nbsp;&nbsp;<img src=\"../images/tv-item-last.gif\">&nbsp;&nbsp;<input id=\"btnRemove\" type=\"button\" value=\"".Dict::S('UI:RemoveLinkedObjectsOf_Class')."\" onClick=\"RemoveSelected();\" >");
  257. $oP->add("&nbsp;&nbsp;&nbsp;<input id=\"btnAdd\" type=\"button\" value=\"".Dict::Format('UI:AddLinkedObjectsOf_Class', MetaModel::GetName($this->m_sLinkedClass))."\" onClick=\"AddObjects();\"></span>\n");
  258. $oP->add("<span style=\"float:right;\"><input id=\"btnCancel\" type=\"button\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"BackToDetails('".$sTargetClass."', ".$this->m_iObjectId.", '', '');\">");
  259. $oP->add("&nbsp;&nbsp;&nbsp;<input id=\"btnOk\" type=\"submit\" value=\"".Dict::S('UI:Button:Ok')."\"></span>\n");
  260. $oP->add("<span style=\"clear:both;\"><p>&nbsp;</p></span>\n");
  261. $oP->add("</div>\n");
  262. $oP->add("</form>\n");
  263. if (isset($aExtraParams['StartWithAdd']) && ($aExtraParams['StartWithAdd']))
  264. {
  265. $oP->add_ready_script("AddObjects();");
  266. }
  267. }
  268. protected function GetFormRow($oP, $oLinkedObj, $currentLink = null )
  269. {
  270. $aRow = array();
  271. if(is_object($currentLink))
  272. {
  273. $key = $currentLink->GetKey();
  274. $sNameSuffix = "[$key]"; // To make a tabular form
  275. $aRow['form::checkbox'] = "<input class=\"selection\" type=\"checkbox\" onChange=\"OnSelectChange();\" value=\"$key\">";
  276. $aRow['form::checkbox'] .= "<input type=\"hidden\" name=\"linkId[$key]\" value=\"$key\">";
  277. foreach($this->m_aEditableFields as $sFieldCode)
  278. {
  279. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sFieldCode);
  280. $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sClass, $sFieldCode, $oAttDef, $currentLink->Get($sFieldCode), '' /* DisplayValue */, $key, $sNameSuffix);
  281. }
  282. }
  283. else
  284. {
  285. // form for creating a new record
  286. $sNameSuffix = "[$currentLink]"; // To make a tabular form
  287. $aRow['form::checkbox'] = "<input class=\"selection\" type=\"checkbox\" onChange=\"OnSelectChange();\" value=\"$currentLink\">";
  288. $aRow['form::checkbox'] .= "<input type=\"hidden\" name=\"linkId[$currentLink]\" value=\"$currentLink\">";
  289. foreach($this->m_aEditableFields as $sFieldCode)
  290. {
  291. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sFieldCode);
  292. $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sClass, $sFieldCode, $oAttDef, '' /* TO DO/ call GetDefaultValue($oObject->ToArgs()) */, '' /* DisplayValue */, '' /* id */, $sNameSuffix);
  293. }
  294. }
  295. foreach(MetaModel::GetZListItems($this->m_sLinkedClass, 'list') as $sFieldCode)
  296. {
  297. $aRow['static::'.$sFieldCode] = $oLinkedObj->GetAsHTML($sFieldCode);
  298. }
  299. return $aRow;
  300. }
  301. protected function DisplayFormTable(WebPage $oP, $aConfig, $aData)
  302. {
  303. $oP->add("<table class=\"listResults\">\n");
  304. // Header
  305. $oP->add("<thead>\n");
  306. $oP->add("<tr>\n");
  307. foreach($aConfig as $sName=>$aDef)
  308. {
  309. $oP->add("<th title=\"".$aDef['description']."\">".$aDef['label']."</th>\n");
  310. }
  311. $oP->add("</tr>\n");
  312. $oP->add("</thead>\n");
  313. // Content
  314. $oP->add("</tbody>\n");
  315. if (count($aData) == 0)
  316. {
  317. $oP->add("<tr id=\"empty_row\"><td colspan=\"".count($aConfig)."\" style=\"text-align:center;\">".Dict::S('UI:Message:EmptyList:UseAdd')."</td></td>");
  318. }
  319. else
  320. {
  321. foreach($aData as $iRowId => $aRow)
  322. {
  323. $this->DisplayFormRow($oP, $aConfig, $aRow, $iRowId);
  324. }
  325. }
  326. $oP->add("</tbody>\n");
  327. // Footer
  328. $oP->add("</table>\n");
  329. }
  330. protected function DisplayFormRow(WebPage $oP, $aConfig, $aRow, $iRowId)
  331. {
  332. $oP->add("<tr id=\"row_$iRowId\">\n");
  333. foreach($aConfig as $sName=>$void)
  334. {
  335. $oP->add("<td>".$aRow[$sName]."</td>\n");
  336. }
  337. $oP->add("</tr>\n");
  338. }
  339. public function DisplayAddForm(WebPage $oP)
  340. {
  341. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
  342. $sTargetClass = $oAttDef->GetTargetClass();
  343. $oTargetObj = MetaModel::GetObject($sTargetClass, $this->m_iObjectId);
  344. $oP->add("<div class=\"wizContainer\">\n");
  345. //$oP->add("<div class=\"page_header\">\n");
  346. //$oP->add("<h1>".Dict::Format('UI:AddObjectsOf_Class_LinkedWith_Class_Instance', MetaModel::GetName($this->m_sLinkedClass), MetaModel::GetName(get_class($oTargetObj)), "<span class=\"hilite\">".$oTargetObj->GetHyperlink()."</span>")."</h1>\n");
  347. //$oP->add("</div>\n");
  348. $oFilter = new DBObjectSearch($this->m_sLinkedClass);
  349. $oSet = new CMDBObjectSet($oFilter);
  350. $oBlock = new DisplayBlock($oFilter, 'search', false);
  351. $oBlock->Display($oP, 'SearchFormToAdd', array('open' => true));
  352. $oP->Add("<form id=\"ObjectsAddForm\" OnSubmit=\"return DoAddObjects(this.id);\">\n");
  353. $oP->Add("<div id=\"SearchResultsToAdd\">\n");
  354. $oP->Add("<div style=\"height: 100px; background: #fff;border-color:#F6F6F1 #E6E6E1 #E6E6E1 #F6F6F1; border-style:solid; border-width:3px; text-align: center; vertical-align: center;\"><p>".Dict::S('UI:Message:EmptyList:UseSearchForm')."</p></div>\n");
  355. $oP->Add("</div>\n");
  356. $oP->add("<input type=\"button\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"$('#ModalDlg').dialog('close');\">&nbsp;&nbsp;<input type=\"submit\" value=\"".Dict::S('UI:Button:Add')."\">");
  357. $oP->Add("</div>\n");
  358. $oP->Add("</form>\n");
  359. $oP->add_ready_script("$('#ModalDlg').dialog('option', {title:'".Dict::Format('UI:AddObjectsOf_Class_LinkedWith_Class_Instance', MetaModel::GetName($this->m_sLinkedClass), MetaModel::GetName(get_class($oTargetObj)), "<span class=\"hilite\">".$oTargetObj->GetHyperlink()."</span>")."'});");
  360. $oP->add_ready_script("$('div#SearchFormToAdd form').bind('submit.uilinksWizard', SubmitHook);");
  361. }
  362. public function SearchObjectsToAdd(WebPage $oP)
  363. {
  364. //$oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
  365. $oFilter = new DBObjectSearch($this->m_sLinkedClass);
  366. $oSet = new CMDBObjectSet($oFilter);
  367. $oBlock = new DisplayBlock($oFilter, 'list', false);
  368. $oBlock->Display($oP, 'ResultsToAdd', array('menu' => false, 'selection_mode' => true)); // Don't display the 'Actions' menu on the results
  369. }
  370. public function DoAddObjects(WebPage $oP, $aLinkedObjectIds = array())
  371. {
  372. //$oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
  373. //$sTargetClass = $oAttDef->GetTargetClass();
  374. //$oP->Add("<!-- nb of objects to add: ".count($aLinkedObjectIds)." -->\n"); // Just to make sure it's not empty
  375. $aTable = array();
  376. foreach($aLinkedObjectIds as $iObjectId)
  377. {
  378. $oLinkedObj = MetaModel::GetObject($this->m_sLinkedClass, $iObjectId);
  379. if (is_object($oLinkedObj))
  380. {
  381. $aRow = $this->GetFormRow($oP, $oLinkedObj, -$iObjectId ); // Not yet created link get negative Ids
  382. $this->DisplayFormRow($oP, $this->m_aTableConfig, $aRow, -$iObjectId);
  383. }
  384. else
  385. {
  386. echo Dict::Format('UI:Error:Object_Class_Id_NotFound', $this->m_sLinkedClass, $iObjectId);
  387. }
  388. }
  389. //var_dump($aTable);
  390. //$oP->Add("<!-- end of added list -->\n"); // Just to make sure it's not empty
  391. }
  392. }
  393. ?>