bslinkedsetfieldrenderer.class.inc.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. // Copyright (C) 2010-2016 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. namespace Combodo\iTop\Renderer\Bootstrap\FieldRenderer;
  19. use \utils;
  20. use \Dict;
  21. use \UserRights;
  22. use \InlineImage;
  23. use \DBObjectSet;
  24. use \MetaModel;
  25. use \Combodo\iTop\Renderer\FieldRenderer;
  26. use \Combodo\iTop\Renderer\RenderingOutput;
  27. use \Combodo\iTop\Form\Field\LinkedSetField;
  28. /**
  29. * Description of BsLinkedSetFieldRenderer
  30. *
  31. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  32. */
  33. class BsLinkedSetFieldRenderer extends FieldRenderer
  34. {
  35. /**
  36. * Returns a RenderingOutput for the FieldRenderer's Field
  37. *
  38. * @return \Combodo\iTop\Renderer\RenderingOutput
  39. */
  40. public function Render()
  41. {
  42. $oOutput = new RenderingOutput();
  43. $sFieldMandatoryClass = ($this->oField->GetMandatory()) ? 'form_mandatory' : '';
  44. // Vars to build the table
  45. $sAttributesToDisplayAsJson = json_encode($this->oField->GetAttributesToDisplay());
  46. $sAttCodesToDisplayAsJson = json_encode($this->oField->GetAttributesToDisplay(true));
  47. $aItems = array();
  48. $aItemIds = array();
  49. $this->PrepareItems($aItems, $aItemIds);
  50. $sItemsAsJson = json_encode($aItems);
  51. $sItemIdsAsJson = htmlentities(json_encode($aItemIds), ENT_QUOTES, 'UTF-8');
  52. if (!$this->oField->GetHidden())
  53. {
  54. // Rendering field
  55. $sIsEditable = ($this->oField->GetReadOnly()) ? 'false' : 'true';
  56. $sCollapseTogglerVisibleClass = 'glyphicon-menu-down';
  57. $sCollapseTogglerHiddenClass = 'glyphicon-menu-down collapsed';
  58. $sCollapseTogglerId = 'form_linkedset_toggler_' . $this->oField->GetGlobalId();
  59. $sFieldWrapperId = 'form_linkedset_wrapper_' . $this->oField->GetGlobalId();
  60. $oOutput->AddHtml('<div class="form-group ' . $sFieldMandatoryClass . '">');
  61. if ($this->oField->GetLabel() !== '')
  62. {
  63. $oOutput->AddHtml('<label for="' . $this->oField->GetGlobalId() . '" class="control-label">')
  64. ->AddHtml($this->oField->GetLabel(), true)
  65. ->AddHtml('<a id="' . $sCollapseTogglerId . '" class="form_linkedset_toggler" data-toggle="collapse" href="#' . $sFieldWrapperId . '" aria-expanded="false" aria-controls="' . $sFieldWrapperId . '">')
  66. ->AddHtml('<span class="text">' . count($aItemIds) . '</span>')
  67. ->AddHtml('<span class="glyphicon ' . $sCollapseTogglerHiddenClass . '"></>')
  68. ->AddHtml('</a>')
  69. ->AddHtml('</label>');
  70. }
  71. $oOutput->AddHtml('<div class="help-block"></div>');
  72. // Rendering table
  73. // - Vars
  74. $sTableId = 'table_' . $this->oField->GetGlobalId();
  75. // - Output
  76. $oOutput->AddHtml(
  77. <<<EOF
  78. <div class="form_linkedset_wrapper collapse" id="{$sFieldWrapperId}">
  79. <div class="row">
  80. <div class="col-xs-12">
  81. <input type="hidden" id="{$this->oField->GetGlobalId()}" name="{$this->oField->GetId()}" value="{$sItemIdsAsJson}" />
  82. <table id="{$sTableId}" data-field-id="{$this->oField->GetId()}" class="table table-striped table-bordered responsive" cellspacing="0" width="100%">
  83. <tbody>
  84. </tbody>
  85. </table>
  86. </div>
  87. </div>
  88. EOF
  89. );
  90. // Rendering table widget
  91. // - Vars
  92. $sEmptyTableLabel = htmlentities(Dict::S(($this->oField->GetReadOnly()) ? 'Portal:Datatables:Language:EmptyTable' : 'UI:Message:EmptyList:UseAdd'), ENT_QUOTES, 'UTF-8');
  93. $sLabelGeneralCheckbox = htmlentities(Dict::S('Core:BulkExport:CheckAll') . ' / ' . Dict::S('Core:BulkExport:UncheckAll'), ENT_QUOTES, 'UTF-8');
  94. $sSelectionOptionHtml = ($this->oField->GetReadOnly()) ? 'false' : '{"style": "multi"}';
  95. $sSelectionInputGlobalHtml = ($this->oField->GetReadOnly()) ? '' : '<span class="row_input"><input type="checkbox" id="' . $this->oField->GetGlobalId() . '_check_all" name="' . $this->oField->GetGlobalId() . '_check_all" title="' . $sLabelGeneralCheckbox . '" /></span>';
  96. $sSelectionInputHtml = ($this->oField->GetReadOnly()) ? '' : '<span class="row_input"><input type="checkbox" name="' . $this->oField->GetGlobalId() . '" /></span>';
  97. // - Output
  98. $oOutput->AddJs(
  99. <<<EOF
  100. // Collapse handlers
  101. // - Collapsing by default to optimize form space
  102. // It would be better to be able to construct the widget as collapsed, but in this ase, datatables thinks the container is very small and therefore renders the table as if it was in microbox.
  103. $('#{$sFieldWrapperId}').collapse({toggle: false});
  104. // - Change toggle icon class
  105. $('#{$sFieldWrapperId}').on('shown.bs.collapse', function(){
  106. // Creating the table if null (first expand). If we create it on start, it will be displayed as if it was in a micro screen due to the div being "display: none;"
  107. if(oTable_{$this->oField->GetGlobalId()} === undefined)
  108. {
  109. buildTable_{$this->oField->GetGlobalId()}();
  110. }
  111. })
  112. .on('show.bs.collapse', function(){
  113. $('#{$sCollapseTogglerId} > span.glyphicon').removeClass('{$sCollapseTogglerHiddenClass}').addClass('{$sCollapseTogglerVisibleClass}');
  114. })
  115. .on('hide.bs.collapse', function(){
  116. $('#{$sCollapseTogglerId} > span.glyphicon').removeClass('{$sCollapseTogglerVisibleClass}').addClass('{$sCollapseTogglerHiddenClass}');
  117. });
  118. // Places a loader in the empty datatables
  119. $('#{$sTableId} > tbody').html('<tr><td class="datatables_overlay" colspan="100">' + $('#page_overlay').html() + '</td></tr>');
  120. // Prepares data for datatables
  121. var oColumnProperties_{$this->oField->GetGlobalId()} = {$sAttributesToDisplayAsJson};
  122. var oRawDatas_{$this->oField->GetGlobalId()} = {$sItemsAsJson};
  123. var oTable_{$this->oField->GetGlobalId()};
  124. var oSelectedItems_{$this->oField->GetGlobalId()} = {};
  125. var getColumnsDefinition_{$this->oField->GetGlobalId()} = function()
  126. {
  127. var aColumnsDefinition = [];
  128. if({$sIsEditable})
  129. {
  130. aColumnsDefinition.push({
  131. "width": "auto",
  132. "searchable": false,
  133. "sortable": false,
  134. "title": '{$sSelectionInputGlobalHtml}',
  135. "type": "html",
  136. "data": "",
  137. "render": function(data, type, row)
  138. {
  139. var oCheckboxElem = $('{$sSelectionInputHtml}');
  140. oCheckboxElem.find(':input').attr('data-object-id', row.id).attr('data-target-object-id', row.target_id);
  141. return oCheckboxElem.prop('outerHTML');
  142. }
  143. });
  144. }
  145. for(sKey in oColumnProperties_{$this->oField->GetGlobalId()})
  146. {
  147. // Level main column
  148. aColumnsDefinition.push({
  149. "width": "auto",
  150. "searchable": true,
  151. "sortable": true,
  152. "title": oColumnProperties_{$this->oField->GetGlobalId()}[sKey],
  153. "defaultContent": "",
  154. "type": "html",
  155. "data": "attributes."+sKey+".att_code",
  156. "render": function(data, type, row){
  157. var cellElem;
  158. // Preparing the cell data
  159. if(row.attributes[data].url !== undefined)
  160. {
  161. cellElem = $('<a></a>');
  162. cellElem.attr('target', '_blank').attr('href', row.attributes[data].url);
  163. }
  164. else
  165. {
  166. cellElem = $('<span></span>');
  167. }
  168. cellElem.html('<span>' + row.attributes[data].value + '</span>');
  169. return cellElem.prop('outerHTML');
  170. },
  171. });
  172. }
  173. return aColumnsDefinition;
  174. };
  175. // Helper to build the datatable
  176. // Note : Those options should be externalized in an library so we can use them on any DataTables for the portal.
  177. // We would just have to override / complete the necessary elements
  178. var buildTable_{$this->oField->GetGlobalId()} = function()
  179. {
  180. var iDefaultOrderColumnIndex = ({$sIsEditable}) ? 1 : 0;
  181. // Instanciates datatables
  182. oTable_{$this->oField->GetGlobalId()} = $('#{$sTableId}').DataTable({
  183. "language": {
  184. "emptyTable": "{$sEmptyTableLabel}"
  185. },
  186. "displayLength": -1,
  187. "scrollY": "300px",
  188. "scrollCollapse": true,
  189. "order": [[iDefaultOrderColumnIndex, "asc"]],
  190. "dom": 't',
  191. "columns": getColumnsDefinition_{$this->oField->GetGlobalId()}(),
  192. "select": {$sSelectionOptionHtml},
  193. "rowId": "id",
  194. "data": oRawDatas_{$this->oField->GetGlobalId()},
  195. });
  196. // Handles items selection/deselection
  197. // - Directly on the table
  198. oTable_{$this->oField->GetGlobalId()}.off('select').on('select', function(oEvent, dt, type, indexes){
  199. var aData = oTable_{$this->oField->GetGlobalId()}.rows(indexes).data().toArray();
  200. // Checking input
  201. $('#{$sTableId} tbody tr[role="row"].selected td:first-child input').prop('checked', true);
  202. // Saving values in temp array
  203. for(var i in aData)
  204. {
  205. var iItemId = aData[i].id;
  206. if(!(iItemId in oSelectedItems_{$this->oField->GetGlobalId()}))
  207. {
  208. oSelectedItems_{$this->oField->GetGlobalId()}[iItemId] = aData[i].name;
  209. }
  210. }
  211. // Updating remove button
  212. updateRemoveButtonState_{$this->oField->GetGlobalId()}();
  213. });
  214. oTable_{$this->oField->GetGlobalId()}.off('deselect').on('deselect', function(oEvent, dt, type, indexes){
  215. var aData = oTable_{$this->oField->GetGlobalId()}.rows(indexes).data().toArray();
  216. // Checking input
  217. $('#{$sTableId} tbody tr[role="row"]:not(.selected) td:first-child input').prop('checked', false);
  218. // Saving values in temp array
  219. for(var i in aData)
  220. {
  221. var iItemId = aData[i].id;
  222. if(iItemId in oSelectedItems_{$this->oField->GetGlobalId()})
  223. {
  224. delete oSelectedItems_{$this->oField->GetGlobalId()}[iItemId];
  225. }
  226. }
  227. // Unchecking global checkbox
  228. $('#{$this->oField->GetGlobalId()}_check_all').prop('checked', false);
  229. // Updating remove button
  230. updateRemoveButtonState_{$this->oField->GetGlobalId()}();
  231. });
  232. // - From the global button
  233. $('#{$this->oField->GetGlobalId()}_check_all').off('click').on('click', function(oEvent){
  234. if($(this).prop('checked'))
  235. {
  236. oTable_{$this->oField->GetGlobalId()}.rows().select();
  237. }
  238. else
  239. {
  240. oTable_{$this->oField->GetGlobalId()}.rows().deselect();
  241. }
  242. updateRemoveButtonState_{$this->oField->GetGlobalId()}();
  243. });
  244. };
  245. EOF
  246. );
  247. // Attaching JS widget
  248. $sObjectInformationsUrl = $this->oField->GetInformationEndpoint();
  249. $oOutput->AddJs(
  250. <<<EOF
  251. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").portal_form_field({
  252. 'validators': {$this->GetValidatorsAsJson()},
  253. 'get_current_value_callback': function(me, oEvent, oData){
  254. var value = null;
  255. // Retrieving JSON value as a string and not an object
  256. //
  257. // Note : The value is passed as a string instead of an array because the attribute would not be included in the posted data when empty.
  258. // Which was an issue when deleting all objects from linkedset
  259. //
  260. // Old code : value = JSON.parse(me.element.find('#{$this->oField->GetGlobalId()}').val());
  261. value = me.element.find('#{$this->oField->GetGlobalId()}').val();
  262. return value;
  263. },
  264. 'set_current_value_callback': function(me, oEvent, oData){
  265. // When we have data (meaning that we picked objects from search)
  266. if(oData !== undefined && Object.keys(oData.values).length > 0)
  267. {
  268. // Showing loader while retrieving informations
  269. $('#page_overlay').fadeIn(200);
  270. // Retrieving new rows ids
  271. var aObjectIds = Object.keys(oData.values);
  272. // Retrieving rows informations so we can add them
  273. $.post(
  274. '{$sObjectInformationsUrl}',
  275. {
  276. sObjectClass: '{$this->oField->GetTargetClass()}',
  277. aObjectIds: aObjectIds,
  278. aObjectAttCodes: $sAttCodesToDisplayAsJson
  279. },
  280. function(oData){
  281. // Updating datatables
  282. if(oData.items !== undefined)
  283. {
  284. for(var i in oData.items)
  285. {
  286. // Adding target item id information
  287. oData.items[i].target_id = oData.items[i].id;
  288. // Adding item to table only if it's not already there
  289. if($('#{$sTableId} tr[role="row"] > td input[data-target-object-id="' + oData.items[i].target_id + '"], #{$sTableId} tr[role="row"] > td input[data-target-object-id="' + (oData.items[i].target_id*-1) + '"]').length === 0)
  290. {
  291. // Making id negative in order to recognize it when persisting
  292. oData.items[i].id = -1 * parseInt(oData.items[i].id);
  293. oTable_{$this->oField->GetGlobalId()}.row.add(oData.items[i]);
  294. }
  295. }
  296. oTable_{$this->oField->GetGlobalId()}.draw();
  297. }
  298. }
  299. )
  300. .done(function(oData){
  301. // Updating hidden field
  302. var aData = oTable_{$this->oField->GetGlobalId()}.rows().data().toArray();
  303. var aObjectIds = [];
  304. for(var i in aData)
  305. {
  306. aObjectIds.push({id: aData[i].id});
  307. }
  308. $('#{$this->oField->GetGlobalId()}').val(JSON.stringify(aObjectIds));
  309. // Updating items count
  310. updateItemCount();
  311. // Updating global checkbox
  312. $('#{$this->oField->GetGlobalId()}_check_all').prop('checked', false);
  313. })
  314. .always(function(oData){
  315. // Hiding loader
  316. $('#page_overlay').fadeOut(200);
  317. });
  318. }
  319. // We come from a button
  320. else
  321. {
  322. // Updating hidden field
  323. var aData = oTable_{$this->oField->GetGlobalId()}.rows().data().toArray();
  324. var aObjectIds = [];
  325. for(var i in aData)
  326. {
  327. aObjectIds.push({id: aData[i].id});
  328. }
  329. $('#{$this->oField->GetGlobalId()}').val(JSON.stringify(aObjectIds));
  330. // Updating items count
  331. updateItemCount();
  332. // Updating global checkbox
  333. $('#{$this->oField->GetGlobalId()}_check_all').prop('checked', false);
  334. }
  335. }
  336. });
  337. EOF
  338. );
  339. // Additional features if in edition mode
  340. if (!$this->oField->GetReadOnly())
  341. {
  342. // Rendering table
  343. // - Vars
  344. $sButtonRemoveId = 'btn_remove_' . $this->oField->GetGlobalId();
  345. $sButtonAddId = 'btn_add_' . $this->oField->GetGlobalId();
  346. $sLabelRemove = Dict::S('UI:Button:Remove');
  347. $sLabelAdd = Dict::S('UI:Button:AddObject');
  348. // - Output
  349. $oOutput->AddHtml(
  350. <<<EOF
  351. <div class="row">
  352. <div class="col-xs-12">
  353. <div class="btn-group" role="group">
  354. <button type="button" class="btn btn-sm btn-danger" id="{$sButtonRemoveId}" title="{$sLabelRemove}" disabled><span class="glyphicon glyphicon-minus"></span></button>
  355. <button type="button" class="btn btn-sm btn-default" id="{$sButtonAddId}" title="{$sLabelAdd}"><span class="glyphicon glyphicon-plus"></span></button>
  356. </div>
  357. </div>
  358. </div>
  359. EOF
  360. );
  361. // Rendering table widget
  362. // - Vars
  363. $sAddButtonEndpoint = str_replace('-sMode-', 'from-attribute', $this->oField->GetSearchEndpoint());
  364. // - Output
  365. $oOutput->AddJs(
  366. <<<EOF
  367. // Handles items selection/deselection
  368. // - Remove button state handler
  369. var updateRemoveButtonState_{$this->oField->GetGlobalId()} = function()
  370. {
  371. var bIsDisabled = (Object.keys(oSelectedItems_{$this->oField->GetGlobalId()}).length == 0);
  372. $('#{$sButtonRemoveId}').prop('disabled', bIsDisabled);
  373. };
  374. // - Item count state handler
  375. var updateItemCount = function()
  376. {
  377. $('#{$sCollapseTogglerId} > .text').text( oTable_{$this->oField->GetGlobalId()}.rows().count() );
  378. };
  379. // Handles items remove/add
  380. $('#{$sButtonRemoveId}').off('click').on('click', function(){
  381. // Removing items from table
  382. oTable_{$this->oField->GetGlobalId()}.rows({selected: true}).remove().draw();
  383. // Resetting selected items
  384. oSelectedItems_{$this->oField->GetGlobalId()} = {};
  385. // Updating form value
  386. $("[data-field-id='{$this->oField->GetId()}'][data-form-path='{$this->oField->GetFormPath()}']").triggerHandler('set_current_value');
  387. // Updating global checkbox state
  388. $('#{$this->oField->GetGlobalId()}_check_all').prop('checked', false);
  389. // Updating remove button
  390. updateRemoveButtonState_{$this->oField->GetGlobalId()}();
  391. });
  392. $('#{$sButtonAddId}').off('click').on('click', function(){
  393. // Preparing current values
  394. var aObjectIdsToIgnore = [];
  395. $('#{$sTableId} tr[role="row"] > td input[data-target-object-id]').each(function(iIndex, oElem){
  396. aObjectIdsToIgnore.push( $(oElem).attr('data-target-object-id') );
  397. });
  398. // Creating a new modal
  399. var oModalElem;
  400. if($('.modal[data-source-element="{$sButtonAddId}"]').length === 0)
  401. {
  402. oModalElem = $('#modal-for-all').clone();
  403. oModalElem.attr('id', '').attr('data-source-element', '{$sButtonAddId}').appendTo('body');
  404. }
  405. else
  406. {
  407. oModalElem = $('.modal[data-source-element="{$sButtonAddId}"]').first();
  408. }
  409. // Resizing to small modal
  410. oModalElem.find('.modal-dialog').removeClass('modal-sm').addClass('modal-lg');
  411. // Loading content
  412. oModalElem.find('.modal-content').html($('#page_overlay .overlay_content').html());
  413. oModalElem.find('.modal-content').load(
  414. '{$sAddButtonEndpoint}',
  415. {
  416. sFormPath: '{$this->oField->GetFormPath()}',
  417. sFieldId: '{$this->oField->GetId()}',
  418. aObjectIdsToIgnore : aObjectIdsToIgnore
  419. }
  420. );
  421. oModalElem.modal('show');
  422. });
  423. EOF
  424. );
  425. }
  426. }
  427. // ... and in hidden mode
  428. else
  429. {
  430. $oOutput->AddHtml('<input type="hidden" id="' . $this->oField->GetGlobalId() . '" name="' . $this->oField->GetId() . '" value="' . $sItemIdsAsJson . '" />');
  431. }
  432. // End of table rendering
  433. $oOutput->AddHtml('</div>');
  434. $oOutput->AddHtml('</div>');
  435. return $oOutput;
  436. }
  437. protected function PrepareItems(&$aItems, &$aItemIds)
  438. {
  439. $oValueSet = $this->oField->GetCurrentValue();
  440. $oValueSet->OptimizeColumnLoad(array($this->oField->GetTargetClass() => $this->oField->GetAttributesToDisplay(true)));
  441. while ($oItem = $oValueSet->Fetch())
  442. {
  443. // In case of indirect linked set, we must retrieve the remote object
  444. if ($this->oField->IsIndirect())
  445. {
  446. $oRemoteItem = MetaModel::GetObject($this->oField->GetTargetClass(), $oItem->Get($this->oField->GetExtKeyToRemote()));
  447. }
  448. else
  449. {
  450. $oRemoteItem = $oItem;
  451. }
  452. $aItemProperties = array(
  453. 'id' => $oItem->GetKey(),
  454. 'target_id' => $oRemoteItem->GetKey(),
  455. 'name' => $oItem->GetName(),
  456. 'attributes' => array()
  457. );
  458. // Target object others attributes
  459. foreach ($this->oField->GetAttributesToDisplay(true) as $sAttCode)
  460. {
  461. if ($sAttCode !== 'id')
  462. {
  463. $aAttProperties = array(
  464. 'att_code' => $sAttCode
  465. );
  466. $oAttDef = MetaModel::GetAttributeDef($this->oField->GetTargetClass(), $sAttCode);
  467. if ($oAttDef->IsExternalKey())
  468. {
  469. $aAttProperties['value'] = $oRemoteItem->Get($sAttCode . '_friendlyname');
  470. }
  471. else
  472. {
  473. $aAttProperties['value'] = $oAttDef->GetValueLabel($oRemoteItem->Get($sAttCode));
  474. }
  475. $aItemProperties['attributes'][$sAttCode] = $aAttProperties;
  476. }
  477. }
  478. $aItems[] = $aItemProperties;
  479. $aItemIds[] = array('id' => $oItem->GetKey());
  480. }
  481. }
  482. }