ui.linkswidget.class.inc.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. // Copyright (C) 2010-2017 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 UILinksWidget
  20. *
  21. * @copyright Copyright (C) 2010-2017 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once(APPROOT.'/application/webpage.class.inc.php');
  25. require_once(APPROOT.'/application/displayblock.class.inc.php');
  26. class UILinksWidget
  27. {
  28. protected $m_sClass;
  29. protected $m_sAttCode;
  30. protected $m_sNameSuffix;
  31. protected $m_iInputId;
  32. protected $m_aAttributes;
  33. protected $m_sExtKeyToRemote;
  34. protected $m_sExtKeyToMe;
  35. protected $m_sLinkedClass;
  36. protected $m_sRemoteClass;
  37. protected $m_bDuplicatesAllowed;
  38. public function __construct($sClass, $sAttCode, $iInputId, $sNameSuffix = '', $bDuplicatesAllowed = false)
  39. {
  40. $this->m_sClass = $sClass;
  41. $this->m_sAttCode = $sAttCode;
  42. $this->m_sNameSuffix = $sNameSuffix;
  43. $this->m_iInputId = $iInputId;
  44. $this->m_bDuplicatesAllowed = $bDuplicatesAllowed;
  45. $this->m_aEditableFields = array();
  46. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sAttCode);
  47. $this->m_sLinkedClass = $oAttDef->GetLinkedClass();
  48. $this->m_sExtKeyToRemote = $oAttDef->GetExtKeyToRemote();
  49. $this->m_sExtKeyToMe = $oAttDef->GetExtKeyToMe();
  50. $oLinkingAttDef = MetaModel::GetAttributeDef($this->m_sLinkedClass, $this->m_sExtKeyToRemote);
  51. $this->m_sRemoteClass = $oLinkingAttDef->GetTargetClass();
  52. $sExtKeyToMe = $oAttDef->GetExtKeyToMe();
  53. $sStateAttCode = MetaModel::GetStateAttributeCode($this->m_sClass);
  54. $sDefaultState = MetaModel::GetDefaultState($this->m_sClass);
  55. $this->m_aEditableFields = array();
  56. $this->m_aTableConfig = array();
  57. $this->m_aTableConfig['form::checkbox'] = array( 'label' => "<input class=\"select_all\" type=\"checkbox\" value=\"1\" onClick=\"CheckAll('#linkedset_{$this->m_sAttCode}{$this->m_sNameSuffix} .selection', this.checked); oWidget".$this->m_iInputId.".OnSelectChange();\">", 'description' => Dict::S('UI:SelectAllToggle+'));
  58. foreach(MetaModel::FlattenZList(MetaModel::GetZListItems($this->m_sLinkedClass, 'list')) as $sAttCode)
  59. {
  60. $oAttDef = MetaModel::GetAttributeDef($this->m_sLinkedClass, $sAttCode);
  61. if ($sStateAttCode == $sAttCode)
  62. {
  63. // State attribute is always hidden from the UI
  64. }
  65. else if ($oAttDef->IsWritable() && ($sAttCode != $sExtKeyToMe) && ($sAttCode != $this->m_sExtKeyToRemote) && ($sAttCode != 'finalclass'))
  66. {
  67. $iFlags = MetaModel::GetAttributeFlags($this->m_sLinkedClass, $sDefaultState, $sAttCode);
  68. if ( !($iFlags & OPT_ATT_HIDDEN) && !($iFlags & OPT_ATT_READONLY) )
  69. {
  70. $this->m_aEditableFields[] = $sAttCode;
  71. $this->m_aTableConfig[$sAttCode] = array( 'label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  72. }
  73. }
  74. }
  75. $this->m_aTableConfig['static::key'] = array( 'label' => MetaModel::GetName($this->m_sRemoteClass), 'description' => MetaModel::GetClassDescription($this->m_sRemoteClass));
  76. foreach(MetaModel::GetZListItems($this->m_sRemoteClass, 'list') as $sFieldCode)
  77. {
  78. // TO DO: check the state of the attribute: hidden or visible ?
  79. if ($sFieldCode != 'finalclass')
  80. {
  81. $oAttDef = MetaModel::GetAttributeDef($this->m_sRemoteClass, $sFieldCode);
  82. $this->m_aTableConfig['static::'.$sFieldCode] = array( 'label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  83. }
  84. }
  85. }
  86. /**
  87. * A one-row form for editing a link record
  88. * @param WebPage $oP Web page used for the ouput
  89. * @param DBObject $oLinkedObj Remote object
  90. * @param mixed $linkObjOrId Either the object linked or a unique number for new link records to add
  91. * @param array|Hash $aArgs Extra context arguments
  92. * @param $oCurrentObj The object to which all the elements of the linked set refer to
  93. * @param $iUniqueId A unique identifier of new links
  94. * @return string The HTML fragment of the one-row form
  95. */
  96. protected function GetFormRow(WebPage $oP, DBObject $oLinkedObj, $linkObjOrId, $aArgs, $oCurrentObj, $iUniqueId)
  97. {
  98. $sPrefix = "$this->m_sAttCode{$this->m_sNameSuffix}";
  99. $aRow = array();
  100. $aFieldsMap = array();
  101. if(is_object($linkObjOrId) && (!$linkObjOrId->IsNew()))
  102. {
  103. $key = $linkObjOrId->GetKey();
  104. $iRemoteObjKey = $linkObjOrId->Get($this->m_sExtKeyToRemote);
  105. $sPrefix .= "[$key][";
  106. $sNameSuffix = "]"; // To make a tabular form
  107. $aArgs['prefix'] = $sPrefix;
  108. $aArgs['wizHelper'] = "oWizardHelper{$this->m_iInputId}{$key}";
  109. $aArgs['this'] = $linkObjOrId;
  110. $aRow['form::checkbox'] = "<input class=\"selection\" data-remote-id=\"$iRemoteObjKey\" data-link-id=\"$key\" data-unique-id=\"$iUniqueId\" type=\"checkbox\" onClick=\"oWidget".$this->m_iInputId.".OnSelectChange();\" value=\"$key\">";
  111. foreach($this->m_aEditableFields as $sFieldCode)
  112. {
  113. $sFieldId = $this->m_iInputId.'_'.$sFieldCode.'['.$linkObjOrId->GetKey().']';
  114. $sSafeId = utils::GetSafeId($sFieldId);
  115. $sValue = $linkObjOrId->Get($sFieldCode);
  116. $sDisplayValue = $linkObjOrId->GetEditValue($sFieldCode);
  117. $oAttDef = MetaModel::GetAttributeDef($this->m_sLinkedClass, $sFieldCode);
  118. $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sLinkedClass, $sFieldCode, $oAttDef, $sValue, $sDisplayValue, $sSafeId, $sNameSuffix, 0, $aArgs);
  119. $aFieldsMap[$sFieldCode] = $sSafeId;
  120. }
  121. $sState = $linkObjOrId->GetState();
  122. }
  123. else
  124. {
  125. // form for creating a new record
  126. if (is_object($linkObjOrId))
  127. {
  128. // New link existing only in memory
  129. $oNewLinkObj = $linkObjOrId;
  130. $iRemoteObjKey = $oNewLinkObj->Get($this->m_sExtKeyToRemote);
  131. $oNewLinkObj->Set($this->m_sExtKeyToMe, $oCurrentObj); // Setting the extkey with the object also fills the related external fields
  132. }
  133. else
  134. {
  135. $iRemoteObjKey = $linkObjOrId;
  136. $oNewLinkObj = MetaModel::NewObject($this->m_sLinkedClass);
  137. $oRemoteObj = MetaModel::GetObject($this->m_sRemoteClass, $iRemoteObjKey);
  138. $oNewLinkObj->Set($this->m_sExtKeyToRemote, $oRemoteObj); // Setting the extkey with the object alsoo fills the related external fields
  139. $oNewLinkObj->Set($this->m_sExtKeyToMe, $oCurrentObj); // Setting the extkey with the object also fills the related external fields
  140. }
  141. $sPrefix .= "[-$iUniqueId][";
  142. $sNameSuffix = "]"; // To make a tabular form
  143. $aArgs['prefix'] = $sPrefix;
  144. $aArgs['wizHelper'] = "oWizardHelper{$this->m_iInputId}_".$iUniqueId;
  145. $aArgs['this'] = $oNewLinkObj;
  146. $aRow['form::checkbox'] = "<input class=\"selection\" data-remote-id=\"$iRemoteObjKey\" data-link-id=\"\" data-unique-id=\"$iUniqueId\" type=\"checkbox\" onClick=\"oWidget".$this->m_iInputId.".OnSelectChange();\" value=\"-$iUniqueId\">";
  147. foreach($this->m_aEditableFields as $sFieldCode)
  148. {
  149. $sFieldId = $this->m_iInputId.'_'.$sFieldCode.'['.-$iUniqueId.']';
  150. $sSafeId = utils::GetSafeId($sFieldId);
  151. $sValue = $oNewLinkObj->Get($sFieldCode);
  152. $sDisplayValue = $oNewLinkObj->GetEditValue($sFieldCode);
  153. $oAttDef = MetaModel::GetAttributeDef($this->m_sLinkedClass, $sFieldCode);
  154. $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sLinkedClass, $sFieldCode, $oAttDef, $sValue, $sDisplayValue, $sSafeId /* id */, $sNameSuffix, 0, $aArgs);
  155. $aFieldsMap[$sFieldCode] = $sSafeId;
  156. }
  157. $sState = '';
  158. $oP->add_script(
  159. <<<EOF
  160. PrepareWidgets();
  161. oWidget{$this->m_iInputId}.OnLinkAdded($iUniqueId, $iRemoteObjKey);
  162. EOF
  163. );
  164. }
  165. $sExtKeyToMeId = utils::GetSafeId($sPrefix.$this->m_sExtKeyToMe);
  166. $aFieldsMap[$this->m_sExtKeyToMe] = $sExtKeyToMeId;
  167. $aRow['form::checkbox'] .= "<input type=\"hidden\" id=\"$sExtKeyToMeId\" value=\"".$oCurrentObj->GetKey()."\">";
  168. $sExtKeyToRemoteId = utils::GetSafeId($sPrefix.$this->m_sExtKeyToRemote);
  169. $aFieldsMap[$this->m_sExtKeyToRemote] = $sExtKeyToRemoteId;
  170. $aRow['form::checkbox'] .= "<input type=\"hidden\" id=\"$sExtKeyToRemoteId\" value=\"$iRemoteObjKey\">";
  171. $iFieldsCount = count($aFieldsMap);
  172. $sJsonFieldsMap = json_encode($aFieldsMap);
  173. $oP->add_script(
  174. <<<EOF
  175. var {$aArgs['wizHelper']} = new WizardHelper('{$this->m_sLinkedClass}', '', '$sState');
  176. {$aArgs['wizHelper']}.SetFieldsMap($sJsonFieldsMap);
  177. {$aArgs['wizHelper']}.SetFieldsCount($iFieldsCount);
  178. EOF
  179. );
  180. $aRow['static::key'] = $oLinkedObj->GetHyperLink();
  181. foreach(MetaModel::GetZListItems($this->m_sRemoteClass, 'list') as $sFieldCode)
  182. {
  183. $aRow['static::'.$sFieldCode] = $oLinkedObj->GetAsHTML($sFieldCode);
  184. }
  185. return $aRow;
  186. }
  187. /**
  188. * Display one row of the whole form
  189. * @return none
  190. */
  191. protected function DisplayFormRow(WebPage $oP, $aConfig, $aRow, $iRowId)
  192. {
  193. $sHtml = '';
  194. $sHtml .= "<tr id=\"{$this->m_sAttCode}{$this->m_sNameSuffix}_row_$iRowId\">\n";
  195. foreach($aConfig as $sName=>$void)
  196. {
  197. $sHtml .= "<td>".$aRow[$sName]."</td>\n";
  198. }
  199. $sHtml .= "</tr>\n";
  200. return $sHtml;
  201. }
  202. /**
  203. * Display the table with the form for editing all the links at once
  204. * @param WebPage $oP The web page used for the output
  205. * @param Hash $aConfig The table's header configuration
  206. * @param Hash $aData The tabular data to be displayed
  207. * @return string Html fragment representing the form table
  208. */
  209. protected function DisplayFormTable(WebPage $oP, $aConfig, $aData)
  210. {
  211. $sHtml = "<input type=\"hidden\" name=\"attr_{$this->m_sAttCode}{$this->m_sNameSuffix}\" value=\"\">";
  212. $sHtml .= "<table class=\"listResults\">\n";
  213. // Header
  214. $sHtml .= "<thead>\n";
  215. $sHtml .= "<tr>\n";
  216. foreach($aConfig as $sName=>$aDef)
  217. {
  218. $sHtml .= "<th title=\"".$aDef['description']."\">".$aDef['label']."</th>\n";
  219. }
  220. $sHtml .= "</tr>\n";
  221. $sHtml .= "</thead>\n";
  222. // Content
  223. $sHtml .= "</tbody>\n";
  224. $sEmptyRowStyle = '';
  225. if (count($aData) != 0)
  226. {
  227. $sEmptyRowStyle = 'style="display:none;"';
  228. }
  229. foreach($aData as $iRowId => $aRow)
  230. {
  231. $sHtml .= $this->DisplayFormRow($oP, $aConfig, $aRow, $iRowId);
  232. }
  233. $sHtml .= "<tr $sEmptyRowStyle id=\"{$this->m_sAttCode}{$this->m_sNameSuffix}_empty_row\"><td colspan=\"".count($aConfig)."\" style=\"text-align:center;\">".Dict::S('UI:Message:EmptyList:UseAdd')."</td></tr>";
  234. $sHtml .= "</tbody>\n";
  235. // Footer
  236. $sHtml .= "</table>\n";
  237. return $sHtml;
  238. }
  239. /**
  240. * Get the HTML fragment corresponding to the linkset editing widget
  241. * @param WebPage $oP The web page used for all the output
  242. * @param DBObjectSet The initial value of the linked set
  243. * @param Hash $aArgs Extra context arguments
  244. * @param string $sFormPrefix prefix of the fields in the current form
  245. * @param DBObject $oCurrentObj the current object to which the linkset is related
  246. * @return string The HTML fragment to be inserted into the page
  247. */
  248. public function Display(WebPage $oPage, DBObjectSet $oValue, $aArgs = array(), $sFormPrefix, $oCurrentObj)
  249. {
  250. $sHtmlValue = '';
  251. $sTargetClass = self::GetTargetClass($this->m_sClass, $this->m_sAttCode);
  252. $sHtmlValue .= "<div id=\"linkedset_{$this->m_sAttCode}{$this->m_sNameSuffix}\">\n";
  253. $sHtmlValue .= "<input type=\"hidden\" id=\"{$sFormPrefix}{$this->m_iInputId}\">\n";
  254. $oValue->Rewind();
  255. $aForm = array();
  256. $iAddedId = 1; // Unique id for new links
  257. while($oCurrentLink = $oValue->Fetch())
  258. {
  259. $oLinkedObj = MetaModel::GetObject($this->m_sRemoteClass, $oCurrentLink->Get($this->m_sExtKeyToRemote));
  260. if ($oCurrentLink->IsNew())
  261. {
  262. $key = -($iAddedId++);
  263. }
  264. else
  265. {
  266. $key = $oCurrentLink->GetKey();
  267. }
  268. $aForm[$key] = $this->GetFormRow($oPage, $oLinkedObj, $oCurrentLink, $aArgs, $oCurrentObj, $key);
  269. }
  270. $sHtmlValue .= $this->DisplayFormTable($oPage, $this->m_aTableConfig, $aForm);
  271. $sDuplicates = ($this->m_bDuplicatesAllowed) ? 'true' : 'false';
  272. $sWizHelper = 'oWizardHelper'.$sFormPrefix;
  273. $oPage->add_ready_script(<<<EOF
  274. oWidget{$this->m_iInputId} = new LinksWidget('{$this->m_sAttCode}{$this->m_sNameSuffix}', '{$this->m_sClass}', '{$this->m_sAttCode}', '{$this->m_iInputId}', '{$this->m_sNameSuffix}', $sDuplicates, $sWizHelper, '{$this->m_sExtKeyToRemote}');
  275. oWidget{$this->m_iInputId}.Init();
  276. EOF
  277. );
  278. $sHtmlValue .= "<span style=\"float:left;\">&nbsp;&nbsp;&nbsp;<img src=\"../images/tv-item-last.gif\">&nbsp;&nbsp;<input id=\"{$this->m_sAttCode}{$this->m_sNameSuffix}_btnRemove\" type=\"button\" value=\"".Dict::S('UI:RemoveLinkedObjectsOf_Class')."\" onClick=\"oWidget{$this->m_iInputId}.RemoveSelected();\" >";
  279. $sHtmlValue .= "&nbsp;&nbsp;&nbsp;<input id=\"{$this->m_sAttCode}{$this->m_sNameSuffix}_btnAdd\" type=\"button\" value=\"".Dict::Format('UI:AddLinkedObjectsOf_Class', MetaModel::GetName($this->m_sRemoteClass))."\" onClick=\"oWidget{$this->m_iInputId}.AddObjects();\"><span id=\"{$this->m_sAttCode}{$this->m_sNameSuffix}_indicatorAdd\"></span></span>\n";
  280. $sHtmlValue .= "<span style=\"clear:both;\"><p>&nbsp;</p></span>\n";
  281. $sHtmlValue .= "</div>\n";
  282. $oPage->add_at_the_end("<div id=\"dlg_{$this->m_sAttCode}{$this->m_sNameSuffix}\"></div>"); // To prevent adding forms inside the main form
  283. return $sHtmlValue;
  284. }
  285. protected static function GetTargetClass($sClass, $sAttCode)
  286. {
  287. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  288. $sLinkedClass = $oAttDef->GetLinkedClass();
  289. switch(get_class($oAttDef))
  290. {
  291. case 'AttributeLinkedSetIndirect':
  292. $oLinkingAttDef = MetaModel::GetAttributeDef($sLinkedClass, $oAttDef->GetExtKeyToRemote());
  293. $sTargetClass = $oLinkingAttDef->GetTargetClass();
  294. break;
  295. case 'AttributeLinkedSet':
  296. $sTargetClass = $sLinkedClass;
  297. break;
  298. }
  299. return $sTargetClass;
  300. }
  301. public function GetObjectPickerDialog($oPage, $oCurrentObj)
  302. {
  303. $bOpen = MetaModel::GetConfig()->Get('legacy_search_drawer_open');
  304. $sHtml = "<div class=\"wizContainer\" style=\"vertical-align:top;\">\n";
  305. $oFilter = new DBObjectSearch($this->m_sRemoteClass);
  306. $this->SetSearchDefaultFromContext($oCurrentObj, $oFilter);
  307. $oBlock = new DisplayBlock($oFilter, 'search', false);
  308. $sHtml .= $oBlock->GetDisplay($oPage, "SearchFormToAdd_{$this->m_sAttCode}{$this->m_sNameSuffix}", array('open' => $bOpen));
  309. $sHtml .= "<form id=\"ObjectsAddForm_{$this->m_sAttCode}{$this->m_sNameSuffix}\" OnSubmit=\"return oWidget{$this->m_iInputId}.DoAddObjects(this.id);\">\n";
  310. $sHtml .= "<div id=\"SearchResultsToAdd_{$this->m_sAttCode}{$this->m_sNameSuffix}\" style=\"vertical-align:top;background: #fff;height:100%;overflow:auto;padding:0;border:0;\">\n";
  311. $sHtml .= "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>".Dict::S('UI:Message:EmptyList:UseSearchForm')."</p></div>\n";
  312. $sHtml .= "</div>\n";
  313. $sHtml .= "<input type=\"hidden\" id=\"count_{$this->m_sAttCode}{$this->m_sNameSuffix}\" value=\"0\"/>";
  314. $sHtml .= "<input type=\"button\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"$('#dlg_{$this->m_sAttCode}{$this->m_sNameSuffix}').dialog('close');\">&nbsp;&nbsp;<input id=\"btn_ok_{$this->m_sAttCode}{$this->m_sNameSuffix}\" disabled=\"disabled\" type=\"submit\" value=\"".Dict::S('UI:Button:Add')."\">";
  315. $sHtml .= "</div>\n";
  316. $sHtml .= "</form>\n";
  317. $oPage->add($sHtml);
  318. $oPage->add_ready_script("$('#dlg_{$this->m_sAttCode}{$this->m_sNameSuffix}').dialog({ width: $(window).width()*0.8, height: $(window).height()*0.8, autoOpen: false, modal: true, resizeStop: oWidget{$this->m_iInputId}.UpdateSizes });");
  319. $oPage->add_ready_script("$('#dlg_{$this->m_sAttCode}{$this->m_sNameSuffix}').dialog('option', {title:'".addslashes(Dict::Format('UI:AddObjectsOf_Class_LinkedWith_Class', MetaModel::GetName($this->m_sLinkedClass), MetaModel::GetName($this->m_sClass)))."'});");
  320. $oPage->add_ready_script("$('#SearchFormToAdd_{$this->m_sAttCode}{$this->m_sNameSuffix} form').bind('submit.uilinksWizard', oWidget{$this->m_iInputId}.SearchObjectsToAdd);");
  321. $oPage->add_ready_script("$('#SearchFormToAdd_{$this->m_sAttCode}{$this->m_sNameSuffix}').resize(oWidget{$this->m_iInputId}.UpdateSizes);");
  322. }
  323. /**
  324. * Search for objects to be linked to the current object (i.e "remote" objects)
  325. * @param WebPage $oP The page used for the output (usually an AjaxWebPage)
  326. * @param string $sRemoteClass Name of the "remote" class to perform the search on, must be a derived class of m_sRemoteClass
  327. * @param Array $aAlreadyLinkedIds List of IDs of objects of "remote" class already linked, to be filtered out of the search
  328. */
  329. public function SearchObjectsToAdd(WebPage $oP, $sRemoteClass = '', $aAlreadyLinkedIds = array())
  330. {
  331. if ($sRemoteClass != '')
  332. {
  333. // assert(MetaModel::IsParentClass($this->m_sRemoteClass, $sRemoteClass));
  334. $oFilter = new DBObjectSearch($sRemoteClass);
  335. }
  336. else
  337. {
  338. // No remote class specified use the one defined in the linkedset
  339. $oFilter = new DBObjectSearch($this->m_sRemoteClass);
  340. }
  341. if (!$this->m_bDuplicatesAllowed && count($aAlreadyLinkedIds) > 0)
  342. {
  343. $oFilter->AddCondition('id', $aAlreadyLinkedIds, 'NOTIN');
  344. }
  345. $oBlock = new DisplayBlock($oFilter, 'list', false);
  346. $oBlock->Display($oP, "ResultsToAdd_{$this->m_sAttCode}", array('menu' => false, 'cssCount'=> '#count_'.$this->m_sAttCode.$this->m_sNameSuffix , 'selection_mode' => true, 'table_id' => 'add_'.$this->m_sAttCode)); // Don't display the 'Actions' menu on the results
  347. }
  348. public function DoAddObjects(WebPage $oP, $iMaxAddedId, $oFullSetFilter, $oCurrentObj)
  349. {
  350. $aLinkedObjectIds = utils::ReadMultipleSelection($oFullSetFilter);
  351. $iAdditionId = $iMaxAddedId + 1;
  352. foreach($aLinkedObjectIds as $iObjectId)
  353. {
  354. $oLinkedObj = MetaModel::GetObject($this->m_sRemoteClass, $iObjectId, false);
  355. if (is_object($oLinkedObj))
  356. {
  357. $aRow = $this->GetFormRow($oP, $oLinkedObj, $iObjectId, array(), $oCurrentObj, $iAdditionId); // Not yet created link get negative Ids
  358. $oP->add($this->DisplayFormRow($oP, $this->m_aTableConfig, $aRow, -$iAdditionId));
  359. $iAdditionId++;
  360. }
  361. else
  362. {
  363. $oP->p(Dict::Format('UI:Error:Object_Class_Id_NotFound', $this->m_sLinkedClass, $iObjectId));
  364. }
  365. }
  366. }
  367. /**
  368. * Initializes the default search parameters based on 1) a 'current' object and 2) the silos defined by the context
  369. * @param DBObject $oSourceObj
  370. * @param DBSearch $oSearch
  371. */
  372. protected function SetSearchDefaultFromContext($oSourceObj, &$oSearch)
  373. {
  374. $oAppContext = new ApplicationContext();
  375. $sSrcClass = get_class($oSourceObj);
  376. $sDestClass = $oSearch->GetClass();
  377. foreach($oAppContext->GetNames() as $key)
  378. {
  379. // Find the value of the object corresponding to each 'context' parameter
  380. $aCallSpec = array($sSrcClass, 'MapContextParam');
  381. $sAttCode = '';
  382. if (is_callable($aCallSpec))
  383. {
  384. $sAttCode = call_user_func($aCallSpec, $key); // Returns null when there is no mapping for this parameter
  385. }
  386. if (MetaModel::IsValidAttCode($sSrcClass, $sAttCode))
  387. {
  388. $oAttDef = MetaModel::GetAttributeDef($sSrcClass, $sAttCode);
  389. $defaultValue = $oSourceObj->Get($sAttCode);
  390. // Find the attcode for the same 'context' parameter in the destination class
  391. // and sets its value as the default value for the search condition
  392. $aCallSpec = array($sDestClass, 'MapContextParam');
  393. $sAttCode = '';
  394. if (is_callable($aCallSpec))
  395. {
  396. $sAttCode = call_user_func($aCallSpec, $key); // Returns null when there is no mapping for this parameter
  397. }
  398. if (MetaModel::IsValidAttCode($sDestClass, $sAttCode) && !empty($defaultValue))
  399. {
  400. $oSearch->AddCondition($sAttCode, $defaultValue);
  401. }
  402. }
  403. }
  404. }
  405. }