uilinkswizard.class.inc.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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, UserContext $oContext, $aExtraParams = array())
  78. {
  79. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
  80. $sTargetClass = $oAttDef->GetTargetClass();
  81. $oTargetObj = $oContext->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. $.get( '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').jqmShow();
  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. $.get( 'ajax.render.php', theMap,
  168. function(data)
  169. {
  170. $('#SearchResultsToAdd').html(data);
  171. nb_rows = $('#SearchResultsToAdd table.listResults tr').length;
  172. if(nb_rows > 10)
  173. {
  174. yOffset = $('#ModalDlg').height() - $('#SearchResultsToAdd table.listResults tbody').height();
  175. tbodyHeight = $(document).height() - 100 - yOffset;
  176. if ($('#ModalDlg').height() > ($(document).height() - 100))
  177. {
  178. $('#SearchResultsToAdd table.listResults tbody').attr('height', tbodyHeight);
  179. $('#SearchResultsToAdd .listResults tbody').css('overflow', 'auto');
  180. $('#SearchResultsToAdd .listResults').tablesorter( { headers: { 0:{sorter: false }}, widgets: ['zebra']} ); // sortable and zebra tables
  181. }
  182. }
  183. },
  184. 'html'
  185. );
  186. return false;
  187. }
  188. function DoAddObjects(currentFormId)
  189. {
  190. var theMap = { 'class': '{$this->m_sClass}',
  191. 'linkageAttr': '{$this->m_sLinkageAttr}',
  192. 'linkedClass': '{$this->m_sLinkedClass}',
  193. 'objectId': '{$this->m_iObjectId}'
  194. }
  195. // Gather the parameters from the search form
  196. $('#'+currentFormId+' :input').each(
  197. function(i)
  198. {
  199. if ( (this.name != '') && ((this.type != 'checkbox') || (this.checked)) )
  200. {
  201. //console.log(this.type);
  202. arrayExpr = /\[\]$/;
  203. if (arrayExpr.test(this.name))
  204. {
  205. // Array
  206. if (theMap[this.name] == undefined)
  207. {
  208. theMap[this.name] = new Array();
  209. }
  210. theMap[this.name].push(this.value);
  211. }
  212. else
  213. {
  214. theMap[this.name] = this.value;
  215. }
  216. }
  217. }
  218. );
  219. theMap['operation'] = 'doAddObjects';
  220. // Run the query and display the results
  221. $.get( 'ajax.render.php', theMap,
  222. function(data)
  223. {
  224. //console.log('Data: ' + data);
  225. if (data != '')
  226. {
  227. $('#empty_row').remove();
  228. }
  229. $('.listResults tbody').append(data);
  230. $('.listResults').trigger('update');
  231. $('.listResults').tablesorter( { headers: { 0:{sorter: false }}, widgets: ['zebra']} ); // sortable and zebra tables
  232. },
  233. 'html'
  234. );
  235. $('#ModalDlg').jqmHide();
  236. return false;
  237. }
  238. function InitForm()
  239. {
  240. // make sure that the form is clean
  241. $('.selection').each( function() { this.checked = false; });
  242. $('#btnRemove').attr('disabled','disabled');
  243. $('#linksToRemove').val('');
  244. }
  245. function SubmitHook()
  246. {
  247. var the_form = this;
  248. SearchObjectsToAdd(the_form.id);
  249. return false;
  250. }
  251. EOF
  252. );
  253. $oP->Add("</script>\n");
  254. $oP->add_ready_script("InitForm();");
  255. $oFilter = $oContext->NewFilter($this->m_sClass);
  256. $oFilter->AddCondition($this->m_sLinkageAttr, $this->m_iObjectId, '=');
  257. $oSet = new DBObjectSet($oFilter);
  258. $aForm = array();
  259. while($oCurrentLink = $oSet->Fetch())
  260. {
  261. $aRow = array();
  262. $key = $oCurrentLink->GetKey();
  263. $oLinkedObj = $oContext->GetObject($this->m_sLinkedClass, $oCurrentLink->Get($this->m_sLinkingAttCode));
  264. $aForm[$key] = $this->GetFormRow($oP, $oLinkedObj, $oCurrentLink);
  265. }
  266. //var_dump($aTableLabels);
  267. //var_dump($aForm);
  268. $this->DisplayFormTable($oP, $this->m_aTableConfig, $aForm);
  269. $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();\" >");
  270. $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");
  271. $oP->add("<span style=\"float:right;\"><input id=\"btnCancel\" type=\"button\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"goBack();\">");
  272. $oP->add("&nbsp;&nbsp;&nbsp;<input id=\"btnOk\" type=\"submit\" value=\"".Dict::S('UI:Button:Ok')."\"></span>\n");
  273. $oP->add("<span style=\"clear:both;\"><p>&nbsp;</p></span>\n");
  274. $oP->add("</div>\n");
  275. $oP->add("</form>\n");
  276. if (isset($aExtraParams['StartWithAdd']) && ($aExtraParams['StartWithAdd']))
  277. {
  278. $oP->add_ready_script("AddObjects();");
  279. }
  280. }
  281. protected function GetFormRow($oP, $oLinkedObj, $currentLink = null )
  282. {
  283. $aRow = array();
  284. if(is_object($currentLink))
  285. {
  286. $key = $currentLink->GetKey();
  287. $sNameSuffix = "[$key]"; // To make a tabular form
  288. $aRow['form::checkbox'] = "<input class=\"selection\" type=\"checkbox\" onChange=\"OnSelectChange();\" value=\"$key\">";
  289. $aRow['form::checkbox'] .= "<input type=\"hidden\" name=\"linkId[$key]\" value=\"$key\">";
  290. foreach($this->m_aEditableFields as $sFieldCode)
  291. {
  292. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sFieldCode);
  293. $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sClass, $sFieldCode, $oAttDef, $currentLink->Get($sFieldCode), '' /* DisplayValue */, $key, $sNameSuffix);
  294. }
  295. }
  296. else
  297. {
  298. // form for creating a new record
  299. $sNameSuffix = "[$currentLink]"; // To make a tabular form
  300. $aRow['form::checkbox'] = "<input class=\"selection\" type=\"checkbox\" onChange=\"OnSelectChange();\" value=\"$currentLink\">";
  301. $aRow['form::checkbox'] .= "<input type=\"hidden\" name=\"linkId[$currentLink]\" value=\"$currentLink\">";
  302. foreach($this->m_aEditableFields as $sFieldCode)
  303. {
  304. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $sFieldCode);
  305. $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sClass, $sFieldCode, $oAttDef, '' /* TO DO/ call GetDefaultValue($oObject->ToArgs()) */, '' /* DisplayValue */, '' /* id */, $sNameSuffix);
  306. }
  307. }
  308. foreach(MetaModel::GetZListItems($this->m_sLinkedClass, 'list') as $sFieldCode)
  309. {
  310. $aRow['static::'.$sFieldCode] = $oLinkedObj->GetAsHTML($sFieldCode);
  311. }
  312. return $aRow;
  313. }
  314. protected function DisplayFormTable(WebPage $oP, $aConfig, $aData)
  315. {
  316. $oP->add("<table class=\"listResults\">\n");
  317. // Header
  318. $oP->add("<thead>\n");
  319. $oP->add("<tr>\n");
  320. foreach($aConfig as $sName=>$aDef)
  321. {
  322. $oP->add("<th title=\"".$aDef['description']."\">".$aDef['label']."</th>\n");
  323. }
  324. $oP->add("</tr>\n");
  325. $oP->add("</thead>\n");
  326. // Content
  327. $oP->add("</tbody>\n");
  328. if (count($aData) == 0)
  329. {
  330. $oP->add("<tr id=\"empty_row\"><td colspan=\"".count($aConfig)."\" style=\"text-align:center;\">".Dict::S('UI:Message:EmptyList:UseAdd')."</td></td>");
  331. }
  332. else
  333. {
  334. foreach($aData as $iRowId => $aRow)
  335. {
  336. $this->DisplayFormRow($oP, $aConfig, $aRow, $iRowId);
  337. }
  338. }
  339. $oP->add("</tbody>\n");
  340. // Footer
  341. $oP->add("</table>\n");
  342. }
  343. protected function DisplayFormRow(WebPage $oP, $aConfig, $aRow, $iRowId)
  344. {
  345. $oP->add("<tr id=\"row_$iRowId\">\n");
  346. foreach($aConfig as $sName=>$void)
  347. {
  348. $oP->add("<td>".$aRow[$sName]."</td>\n");
  349. }
  350. $oP->add("</tr>\n");
  351. }
  352. public function DisplayAddForm(WebPage $oP, UserContext $oContext)
  353. {
  354. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
  355. $sTargetClass = $oAttDef->GetTargetClass();
  356. $oTargetObj = $oContext->GetObject($sTargetClass, $this->m_iObjectId);
  357. $oP->add("<div class=\"wizContainer\">\n");
  358. $oP->add("<div class=\"page_header\">\n");
  359. $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");
  360. $oP->add("</div>\n");
  361. $oFilter = $oContext->NewFilter($this->m_sLinkedClass);
  362. $oSet = new CMDBObjectSet($oFilter);
  363. $oBlock = new DisplayBlock($oFilter, 'search', false);
  364. $oBlock->Display($oP, 'SearchFormToAdd', array('open' => true));
  365. $oP->Add("<form id=\"ObjectsAddForm\" OnSubmit=\"return DoAddObjects(this.id);\">\n");
  366. $oP->Add("<div id=\"SearchResultsToAdd\">\n");
  367. $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");
  368. $oP->Add("</div>\n");
  369. $oP->add("<input type=\"button\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"$('#ModalDlg').jqmHide();\">&nbsp;&nbsp;<input type=\"submit\" value=\"".Dict::S('UI:Button:Add')."\">");
  370. $oP->Add("</div>\n");
  371. $oP->Add("</form>\n");
  372. $oP->add_ready_script("$('div#SearchFormToAdd form').bind('submit.uilinksWizard', SubmitHook);");
  373. }
  374. public function SearchObjectsToAdd(WebPage $oP, UserContext $oContext)
  375. {
  376. //$oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
  377. $oFilter = $oContext->NewFilter($this->m_sLinkedClass);
  378. $oSet = new CMDBObjectSet($oFilter);
  379. $oBlock = new DisplayBlock($oFilter, 'list', false);
  380. $oBlock->Display($oP, 'ResultsToAdd', array('menu' => false, 'selection_mode' => true, 'display_limit' => false)); // Don't display the 'Actions' menu on the results
  381. }
  382. public function DoAddObjects(WebPage $oP, UserContext $oContext, $aLinkedObjectIds = array())
  383. {
  384. //$oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sLinkageAttr);
  385. //$sTargetClass = $oAttDef->GetTargetClass();
  386. //$oP->Add("<!-- nb of objects to add: ".count($aLinkedObjectIds)." -->\n"); // Just to make sure it's not empty
  387. $aTable = array();
  388. foreach($aLinkedObjectIds as $iObjectId)
  389. {
  390. $oLinkedObj = $oContext->GetObject($this->m_sLinkedClass, $iObjectId);
  391. if (is_object($oLinkedObj))
  392. {
  393. $aRow = $this->GetFormRow($oP, $oLinkedObj, -$iObjectId ); // Not yet created link get negative Ids
  394. $this->DisplayFormRow($oP, $this->m_aTableConfig, $aRow, -$iObjectId);
  395. }
  396. else
  397. {
  398. echo Dict::Format('UI:Error:Object_Class_Id_NotFound', $this->m_sLinkedClass, $iObjectId);
  399. }
  400. }
  401. //var_dump($aTable);
  402. //$oP->Add("<!-- end of added list -->\n"); // Just to make sure it's not empty
  403. }
  404. }
  405. ?>