uilinkswizard.class.inc.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. // Copyright (C) 2010 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. /**
  17. * Class UILinksWizard
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  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->GetName());
  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 type=\"text/javascript\">\n");
  95. $oP->add(
  96. <<<EOF
  97. function OnSelectChange()
  98. {
  99. var nbChecked = $('.selection:checked').length;
  100. if (nbChecked > 0)
  101. {
  102. $('#btnRemove').attr('disabled','');
  103. }
  104. else
  105. {
  106. $('#btnRemove').attr('disabled','disabled');
  107. }
  108. }
  109. function RemoveSelected()
  110. {
  111. $('.selection:checked').each(
  112. function()
  113. {
  114. $('#linksToRemove').val($('#linksToRemove').val() + ' ' + this.value);
  115. $('#row_'+this.value).remove();
  116. }
  117. );
  118. // Disable the button since all the selected items have been removed
  119. $('#btnRemove').attr('disabled','disabled');
  120. // Re-run the zebra plugin to properly highlight the remaining lines
  121. $('.listResults').trigger('update');
  122. }
  123. function AddObjects()
  124. {
  125. // TO DO: compute the list of objects already linked with the current Object
  126. $.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', { 'operation': 'addObjects',
  127. 'class': '{$this->m_sClass}',
  128. 'linkageAttr': '{$this->m_sLinkageAttr}',
  129. 'linkedClass': '{$this->m_sLinkedClass}',
  130. 'objectId': '{$this->m_iObjectId}'
  131. },
  132. function(data)
  133. {
  134. $('#ModalDlg').html(data);
  135. dlgWidth = $(document).width() - 100;
  136. $('#ModalDlg').css('width', dlgWidth);
  137. $('#ModalDlg').css('left', 50);
  138. $('#ModalDlg').css('top', 50);
  139. $('#ModalDlg').dialog( 'open' );
  140. },
  141. 'html'
  142. );
  143. }
  144. function SearchObjectsToAdd(currentFormId)
  145. {
  146. var theMap = { 'class': '{$this->m_sClass}',
  147. 'linkageAttr': '{$this->m_sLinkageAttr}',
  148. 'linkedClass': '{$this->m_sLinkedClass}',
  149. 'objectId': '{$this->m_iObjectId}'
  150. }
  151. if ($('#'+currentFormId+' :input[name=class]').val() != undefined)
  152. {
  153. theMap.linkedClass = $('#'+currentFormId+' :input[name=class]').val();
  154. }
  155. // Gather the parameters from the search form
  156. $('#'+currentFormId+' :input').each(
  157. function(i)
  158. {
  159. if (this.name != '')
  160. {
  161. theMap[this.name] = this.value;
  162. }
  163. }
  164. );
  165. theMap['operation'] = 'searchObjectsToAdd';
  166. // Run the query and display the results
  167. $.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', theMap,
  168. function(data)
  169. {
  170. $('#SearchResultsToAdd').html(data);
  171. $('#SearchResultsToAdd .listResults').tablesorter( { headers: {0: false}}, widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  172. },
  173. 'html'
  174. );
  175. return false;
  176. }
  177. function DoAddObjects(currentFormId)
  178. {
  179. var theMap = { 'class': '{$this->m_sClass}',
  180. 'linkageAttr': '{$this->m_sLinkageAttr}',
  181. 'linkedClass': '{$this->m_sLinkedClass}',
  182. 'objectId': '{$this->m_iObjectId}'
  183. }
  184. // Gather the parameters from the search form
  185. $('#'+currentFormId+' :input').each(
  186. function(i)
  187. {
  188. if ( (this.name != '') && ((this.type != 'checkbox') || (this.checked)) )
  189. {
  190. //console.log(this.type);
  191. arrayExpr = /\[\]$/;
  192. if (arrayExpr.test(this.name))
  193. {
  194. // Array
  195. if (theMap[this.name] == undefined)
  196. {
  197. theMap[this.name] = new Array();
  198. }
  199. theMap[this.name].push(this.value);
  200. }
  201. else
  202. {
  203. theMap[this.name] = this.value;
  204. }
  205. }
  206. }
  207. );
  208. theMap['operation'] = 'doAddObjects';
  209. // Run the query and display the results
  210. $.post( GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', theMap,
  211. function(data)
  212. {
  213. //console.log('Data: ' + data);
  214. if (data != '')
  215. {
  216. $('#empty_row').remove();
  217. }
  218. $('.listResults tbody').append(data);
  219. $('.listResults').trigger('update');
  220. $('.listResults').tablesorter( { headers: {0: false}}, widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  221. },
  222. 'html'
  223. );
  224. $('#ModalDlg').dialog('close');
  225. return false;
  226. }
  227. function InitForm()
  228. {
  229. // make sure that the form is clean
  230. $('.selection').each( function() { this.checked = false; });
  231. $('#btnRemove').attr('disabled','disabled');
  232. $('#linksToRemove').val('');
  233. }
  234. function SubmitHook()
  235. {
  236. var the_form = this;
  237. SearchObjectsToAdd(the_form.id);
  238. return false;
  239. }
  240. EOF
  241. );
  242. $oP->Add("</script>\n");
  243. $oP->add_ready_script("InitForm();");
  244. $oFilter = new DBObjectSearch($this->m_sClass);
  245. $oFilter->AddCondition($this->m_sLinkageAttr, $this->m_iObjectId, '=');
  246. $oSet = new DBObjectSet($oFilter);
  247. $aForm = array();
  248. while($oCurrentLink = $oSet->Fetch())
  249. {
  250. $aRow = array();
  251. $key = $oCurrentLink->GetKey();
  252. $oLinkedObj = MetaModel::GetObject($this->m_sLinkedClass, $oCurrentLink->Get($this->m_sLinkingAttCode));
  253. $aForm[$key] = $this->GetFormRow($oP, $oLinkedObj, $oCurrentLink);
  254. }
  255. //var_dump($aTableLabels);
  256. //var_dump($aForm);
  257. $this->DisplayFormTable($oP, $this->m_aTableConfig, $aForm);
  258. $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();\" >");
  259. $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");
  260. $oP->add("<span style=\"float:right;\"><input id=\"btnCancel\" type=\"button\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"BackToDetails('".$sTargetClass."', ".$this->m_iObjectId.");\">");
  261. $oP->add("&nbsp;&nbsp;&nbsp;<input id=\"btnOk\" type=\"submit\" value=\"".Dict::S('UI:Button:Ok')."\"></span>\n");
  262. $oP->add("<span style=\"clear:both;\"><p>&nbsp;</p></span>\n");
  263. $oP->add("</div>\n");
  264. $oP->add("</form>\n");
  265. if (isset($aExtraParams['StartWithAdd']) && ($aExtraParams['StartWithAdd']))
  266. {
  267. $oP->add_ready_script("AddObjects();");
  268. }
  269. }
  270. protected function GetFormRow($oP, $oLinkedObj, $currentLink = null )
  271. {
  272. $aRow = array();
  273. if(is_object($currentLink))
  274. {
  275. $key = $currentLink->GetKey();
  276. $sNameSuffix = "[$key]"; // To make a tabular form
  277. $aRow['form::checkbox'] = "<input class=\"selection\" type=\"checkbox\" onChange=\"OnSelectChange();\" value=\"$key\">";
  278. $aRow['form::checkbox'] .= "<input type=\"hidden\" name=\"linkId[$key]\" value=\"$key\">";
  279. foreach($this->m_aEditableFields as $sFieldCode)
  280. {
  281. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sFieldCode);
  282. $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sClass, $sFieldCode, $oAttDef, $currentLink->Get($sFieldCode), '' /* DisplayValue */, $key, $sNameSuffix);
  283. }
  284. }
  285. else
  286. {
  287. // form for creating a new record
  288. $sNameSuffix = "[$currentLink]"; // To make a tabular form
  289. $aRow['form::checkbox'] = "<input class=\"selection\" type=\"checkbox\" onChange=\"OnSelectChange();\" value=\"$currentLink\">";
  290. $aRow['form::checkbox'] .= "<input type=\"hidden\" name=\"linkId[$currentLink]\" value=\"$currentLink\">";
  291. foreach($this->m_aEditableFields as $sFieldCode)
  292. {
  293. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sFieldCode);
  294. $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sClass, $sFieldCode, $oAttDef, '' /* TO DO/ call GetDefaultValue($oObject->ToArgs()) */, '' /* DisplayValue */, '' /* id */, $sNameSuffix);
  295. }
  296. }
  297. foreach(MetaModel::GetZListItems($this->m_sLinkedClass, 'list') as $sFieldCode)
  298. {
  299. $aRow['static::'.$sFieldCode] = $oLinkedObj->GetAsHTML($sFieldCode);
  300. }
  301. return $aRow;
  302. }
  303. protected function DisplayFormTable(WebPage $oP, $aConfig, $aData)
  304. {
  305. $oP->add("<table class=\"listResults\">\n");
  306. // Header
  307. $oP->add("<thead>\n");
  308. $oP->add("<tr>\n");
  309. foreach($aConfig as $sName=>$aDef)
  310. {
  311. $oP->add("<th title=\"".$aDef['description']."\">".$aDef['label']."</th>\n");
  312. }
  313. $oP->add("</tr>\n");
  314. $oP->add("</thead>\n");
  315. // Content
  316. $oP->add("</tbody>\n");
  317. if (count($aData) == 0)
  318. {
  319. $oP->add("<tr id=\"empty_row\"><td colspan=\"".count($aConfig)."\" style=\"text-align:center;\">".Dict::S('UI:Message:EmptyList:UseAdd')."</td></td>");
  320. }
  321. else
  322. {
  323. foreach($aData as $iRowId => $aRow)
  324. {
  325. $this->DisplayFormRow($oP, $aConfig, $aRow, $iRowId);
  326. }
  327. }
  328. $oP->add("</tbody>\n");
  329. // Footer
  330. $oP->add("</table>\n");
  331. }
  332. protected function DisplayFormRow(WebPage $oP, $aConfig, $aRow, $iRowId)
  333. {
  334. $oP->add("<tr id=\"row_$iRowId\">\n");
  335. foreach($aConfig as $sName=>$void)
  336. {
  337. $oP->add("<td>".$aRow[$sName]."</td>\n");
  338. }
  339. $oP->add("</tr>\n");
  340. }
  341. public function DisplayAddForm(WebPage $oP)
  342. {
  343. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
  344. $sTargetClass = $oAttDef->GetTargetClass();
  345. $oTargetObj = MetaModel::GetObject($sTargetClass, $this->m_iObjectId);
  346. $oP->add("<div class=\"wizContainer\">\n");
  347. //$oP->add("<div class=\"page_header\">\n");
  348. //$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");
  349. //$oP->add("</div>\n");
  350. $oFilter = new DBObjectSearch($this->m_sLinkedClass);
  351. $oSet = new CMDBObjectSet($oFilter);
  352. $oBlock = new DisplayBlock($oFilter, 'search', false);
  353. $oBlock->Display($oP, 'SearchFormToAdd', array('open' => true));
  354. $oP->Add("<form id=\"ObjectsAddForm\" OnSubmit=\"return DoAddObjects(this.id);\">\n");
  355. $oP->Add("<div id=\"SearchResultsToAdd\">\n");
  356. $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");
  357. $oP->Add("</div>\n");
  358. $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')."\">");
  359. $oP->Add("</div>\n");
  360. $oP->Add("</form>\n");
  361. $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>")."'});");
  362. $oP->add_ready_script("$('div#SearchFormToAdd form').bind('submit.uilinksWizard', SubmitHook);");
  363. }
  364. public function SearchObjectsToAdd(WebPage $oP)
  365. {
  366. //$oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
  367. $oFilter = new DBObjectSearch($this->m_sLinkedClass);
  368. $oSet = new CMDBObjectSet($oFilter);
  369. $oBlock = new DisplayBlock($oFilter, 'list', false);
  370. $oBlock->Display($oP, 'ResultsToAdd', array('menu' => false, 'selection_mode' => true, 'display_limit' => false)); // Don't display the 'Actions' menu on the results
  371. }
  372. public function DoAddObjects(WebPage $oP, $aLinkedObjectIds = array())
  373. {
  374. //$oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
  375. //$sTargetClass = $oAttDef->GetTargetClass();
  376. //$oP->Add("<!-- nb of objects to add: ".count($aLinkedObjectIds)." -->\n"); // Just to make sure it's not empty
  377. $aTable = array();
  378. foreach($aLinkedObjectIds as $iObjectId)
  379. {
  380. $oLinkedObj = MetaModel::GetObject($this->m_sLinkedClass, $iObjectId);
  381. if (is_object($oLinkedObj))
  382. {
  383. $aRow = $this->GetFormRow($oP, $oLinkedObj, -$iObjectId ); // Not yet created link get negative Ids
  384. $this->DisplayFormRow($oP, $this->m_aTableConfig, $aRow, -$iObjectId);
  385. }
  386. else
  387. {
  388. echo Dict::Format('UI:Error:Object_Class_Id_NotFound', $this->m_sLinkedClass, $iObjectId);
  389. }
  390. }
  391. //var_dump($aTable);
  392. //$oP->Add("<!-- end of added list -->\n"); // Just to make sure it's not empty
  393. }
  394. }
  395. ?>