ui.linkswidget.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 UILinksWidget
  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. 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_sLinkedClass;
  35. protected $m_sRemoteClass;
  36. protected $m_bDuplicatesAllowed;
  37. public function __construct($sClass, $sAttCode, $iInputId, $sNameSuffix = '', $bDuplicatesAllowed = false)
  38. {
  39. $this->m_sClass = $sClass;
  40. $this->m_sAttCode = $sAttCode;
  41. $this->m_sNameSuffix = $sNameSuffix;
  42. $this->m_iInputId = $iInputId;
  43. $this->m_bDuplicatesAllowed = $bDuplicatesAllowed;
  44. $this->m_aEditableFields = array();
  45. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sAttCode);
  46. $this->m_sLinkedClass = $oAttDef->GetLinkedClass();
  47. $this->m_sExtKeyToRemote = $oAttDef->GetExtKeyToRemote();
  48. $oLinkingAttDef = MetaModel::GetAttributeDef($this->m_sLinkedClass, $this->m_sExtKeyToRemote);
  49. $this->m_sRemoteClass = $oLinkingAttDef->GetTargetClass();
  50. $sExtKeyToMe = $oAttDef->GetExtKeyToMe();
  51. $sStateAttCode = MetaModel::GetStateAttributeCode($this->m_sClass);
  52. $sDefaultState = MetaModel::GetDefaultState($this->m_sClass);
  53. $this->m_aEditableFields = array();
  54. $this->m_aTableConfig = array();
  55. $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+'));
  56. foreach(MetaModel::ListAttributeDefs($this->m_sLinkedClass) as $sAttCode=>$oAttDef)
  57. {
  58. if ($sStateAttCode == $sAttCode)
  59. {
  60. // State attribute is always hidden from the UI
  61. }
  62. else if ($oAttDef->IsWritable() && ($sAttCode != $sExtKeyToMe) && ($sAttCode != $this->m_sExtKeyToRemote) && ($sAttCode != 'finalclass'))
  63. {
  64. $iFlags = MetaModel::GetAttributeFlags($this->m_sLinkedClass, $sDefaultState, $sAttCode);
  65. if ( !($iFlags & OPT_ATT_HIDDEN) && !($iFlags & OPT_ATT_READONLY) )
  66. {
  67. $this->m_aEditableFields[] = $sAttCode;
  68. $this->m_aTableConfig[$sAttCode] = array( 'label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  69. }
  70. }
  71. }
  72. $this->m_aTableConfig['static::key'] = array( 'label' => MetaModel::GetName($this->m_sRemoteClass), 'description' => MetaModel::GetClassDescription($this->m_sRemoteClass));
  73. foreach(MetaModel::GetZListItems($this->m_sRemoteClass, 'list') as $sFieldCode)
  74. {
  75. // TO DO: check the state of the attribute: hidden or visible ?
  76. if ($sFieldCode != 'finalclass')
  77. {
  78. $oAttDef = MetaModel::GetAttributeDef($this->m_sRemoteClass, $sFieldCode);
  79. $this->m_aTableConfig['static::'.$sFieldCode] = array( 'label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  80. }
  81. }
  82. }
  83. /**
  84. * A one-row form for editing a link record
  85. * @param WebPage $oP Web page used for the ouput
  86. * @param DBObject $oLinkedObj The object to which all the elements of the linked set refer to
  87. * @param mixed $linkObjOrId Either the object linked or a unique number for new link records to add
  88. * @param Hash $aArgs Extra context arguments
  89. * @return string The HTML fragment of the one-row form
  90. */
  91. protected function GetFormRow(WebPage $oP, DBObject $oLinkedObj, $linkObjOrId = null, $aArgs = array() )
  92. {
  93. $sPrefix = "$this->m_sAttCode{$this->m_sNameSuffix}";
  94. $aRow = array();
  95. if(is_object($linkObjOrId))
  96. {
  97. $key = $linkObjOrId->GetKey();
  98. $sPrefix .= "[$key][";
  99. $sNameSuffix = "]"; // To make a tabular form
  100. $aArgs['prefix'] = $sPrefix;
  101. $aRow['form::checkbox'] = "<input class=\"selection\" type=\"checkbox\" onClick=\"oWidget".$this->m_iInputId.".OnSelectChange();\" value=\"$key\">";
  102. $aRow['form::checkbox'] .= "<input type=\"hidden\" name=\"attr_{$sPrefix}id{$sNameSuffix}\" value=\"$key\">";
  103. foreach($this->m_aEditableFields as $sFieldCode)
  104. {
  105. $sFieldId = $this->m_iInputId.'_'.$sFieldCode.'['.$linkObjOrId->GetKey().']';
  106. $sSafeId = str_replace(array('[',']','-'), '_', $sFieldId);
  107. $oAttDef = MetaModel::GetAttributeDef($this->m_sLinkedClass, $sFieldCode);
  108. $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sLinkedClass, $sFieldCode, $oAttDef, $linkObjOrId->Get($sFieldCode), '' /* DisplayValue */, $sSafeId, $sNameSuffix, 0, $aArgs);
  109. }
  110. }
  111. else
  112. {
  113. // form for creating a new record
  114. $sPrefix .= "[$linkObjOrId][";
  115. $oNewLinkObj = MetaModel::NewObject($this->m_sLinkedClass);
  116. $oNewLinkObj->Set($this->m_sExtKeyToRemote, -$linkObjOrId);
  117. $oNewLinkObj->ComputeValues();
  118. $sNameSuffix = "]"; // To make a tabular form
  119. $aArgs['prefix'] = $sPrefix;
  120. $aRow['form::checkbox'] = "<input class=\"selection\" type=\"checkbox\" onClick=\"oWidget".$this->m_iInputId.".OnSelectChange();\" value=\"$linkObjOrId\">";
  121. $aRow['form::checkbox'] .= "<input type=\"hidden\" name=\"attr_{$sPrefix}id{$sNameSuffix}\" value=\"\">";
  122. foreach($this->m_aEditableFields as $sFieldCode)
  123. {
  124. $sFieldId = $this->m_iInputId.'_'.$sFieldCode.'['.$linkObjOrId.']';
  125. $sSafeId = str_replace(array('[',']','-'), '_', $sFieldId);
  126. $oAttDef = MetaModel::GetAttributeDef($this->m_sLinkedClass, $sFieldCode);
  127. $aRow[$sFieldCode] = cmdbAbstractObject::GetFormElementForField($oP, $this->m_sLinkedClass, $sFieldCode, $oAttDef, $oNewLinkObj->Get($sFieldCode) /* TO DO/ call GetDefaultValue($oObject->ToArgs()) */, '' /* DisplayValue */, $sSafeId /* id */, $sNameSuffix, 0, $aArgs);
  128. }
  129. }
  130. $aRow['static::key'] = $oLinkedObj->GetHyperLink();
  131. foreach(MetaModel::GetZListItems($this->m_sRemoteClass, 'list') as $sFieldCode)
  132. {
  133. $aRow['static::'.$sFieldCode] = $oLinkedObj->GetAsHTML($sFieldCode);
  134. }
  135. return $aRow;
  136. }
  137. /**
  138. * Display one row of the whole form
  139. * @return none
  140. */
  141. protected function DisplayFormRow(WebPage $oP, $aConfig, $aRow, $iRowId)
  142. {
  143. $sHtml = '';
  144. $sHtml .= "<tr id=\"{$this->m_sAttCode}{$this->m_sNameSuffix}_row_$iRowId\">\n";
  145. foreach($aConfig as $sName=>$void)
  146. {
  147. $sHtml .= "<td>".$aRow[$sName]."</td>\n";
  148. }
  149. $sHtml .= "</tr>\n";
  150. return $sHtml;
  151. }
  152. /**
  153. * Display the table with the form for editing all the links at once
  154. * @param WebPage $oP The web page used for the output
  155. * @param Hash $aConfig The table's header configuration
  156. * @param Hash $aData The tabular data to be displayed
  157. * @return string Html fragment representing the form table
  158. */
  159. protected function DisplayFormTable(WebPage $oP, $aConfig, $aData)
  160. {
  161. $sHtml = '';
  162. $sHtml .= "<table class=\"listResults\">\n";
  163. // Header
  164. $sHtml .= "<thead>\n";
  165. $sHtml .= "<tr>\n";
  166. foreach($aConfig as $sName=>$aDef)
  167. {
  168. $sHtml .= "<th title=\"".$aDef['description']."\">".$aDef['label']."</th>\n";
  169. }
  170. $sHtml .= "</tr>\n";
  171. $sHtml .= "</thead>\n";
  172. // Content
  173. $sHtml .= "</tbody>\n";
  174. $sEmptyRowStyle = '';
  175. if (count($aData) != 0)
  176. {
  177. $sEmptyRowStyle = 'style="display:none;"';
  178. }
  179. $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')."<input type=\"hidden\" name=\"attr_{$this->m_sAttCode}{$this->m_sNameSuffix}\" value=\"\"></td></td>";
  180. foreach($aData as $iRowId => $aRow)
  181. {
  182. $sHtml .= $this->DisplayFormRow($oP, $aConfig, $aRow, $iRowId);
  183. }
  184. $sHtml .= "</tbody>\n";
  185. // Footer
  186. $sHtml .= "</table>\n";
  187. return $sHtml;
  188. }
  189. /**
  190. * Get the HTML fragment corresponding to the linkset editing widget
  191. * @param WebPage $oP The web page used for all the output
  192. * @param DBObjectSet The initial value of the linked set
  193. * @param Hash $aArgs Extra context arguments
  194. * @return string The HTML fragment to be inserted into the page
  195. */
  196. public function Display(WebPage $oPage, DBObjectSet $oValue, $aArgs = array())
  197. {
  198. $sHtmlValue = '';
  199. $sTargetClass = self::GetTargetClass($this->m_sClass, $this->m_sAttCode);
  200. $sHtmlValue .= "<div id=\"linkedset_{$this->m_sAttCode}{$this->m_sNameSuffix}\">\n";
  201. $oValue->Rewind();
  202. $aForm = array();
  203. while($oCurrentLink = $oValue->Fetch())
  204. {
  205. $aRow = array();
  206. $oLinkedObj = MetaModel::GetObject($this->m_sRemoteClass, $oCurrentLink->Get($this->m_sExtKeyToRemote));
  207. if ($oCurrentLink->IsNew())
  208. {
  209. $key = -$oLinkedObj->GetKey();
  210. $aForm[$key] = $this->GetFormRow($oPage, $oLinkedObj, $key, $aArgs);
  211. }
  212. else
  213. {
  214. $key = $oCurrentLink->GetKey();
  215. $aForm[$key] = $this->GetFormRow($oPage, $oLinkedObj, $oCurrentLink, $aArgs);
  216. }
  217. }
  218. $sHtmlValue .= $this->DisplayFormTable($oPage, $this->m_aTableConfig, $aForm);
  219. $sDuplicates = ($this->m_bDuplicatesAllowed) ? 'true' : 'false';
  220. $oPage->add_ready_script(<<<EOF
  221. 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);
  222. oWidget{$this->m_iInputId}.Init();
  223. EOF
  224. );
  225. $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();\" >";
  226. $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>\n";
  227. $sHtmlValue .= "<span style=\"clear:both;\"><p>&nbsp;</p></span>\n";
  228. $sHtmlValue .= "</div>\n";
  229. $oPage->add_at_the_end($this->GetObjectPickerDialog($oPage), "dlg_{$this->m_sAttCode}{$this->m_sNameSuffix}"); // To prevent adding forms inside the main form
  230. return $sHtmlValue;
  231. }
  232. protected static function GetTargetClass($sClass, $sAttCode)
  233. {
  234. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  235. $sLinkedClass = $oAttDef->GetLinkedClass();
  236. switch(get_class($oAttDef))
  237. {
  238. case 'AttributeLinkedSetIndirect':
  239. $oLinkingAttDef = MetaModel::GetAttributeDef($sLinkedClass, $oAttDef->GetExtKeyToRemote());
  240. $sTargetClass = $oLinkingAttDef->GetTargetClass();
  241. break;
  242. case 'AttributeLinkedSet':
  243. $sTargetClass = $sLinkedClass;
  244. break;
  245. }
  246. return $sTargetClass;
  247. }
  248. protected function GetObjectPickerDialog($oPage)
  249. {
  250. $sHtml = "<div id=\"dlg_{$this->m_sAttCode}{$this->m_sNameSuffix}\">";
  251. $sHtml .= "<div class=\"wizContainer\" style=\"vertical-align:top;\">\n";
  252. $oFilter = new DBObjectSearch($this->m_sRemoteClass);
  253. $oSet = new CMDBObjectSet($oFilter);
  254. $oBlock = new DisplayBlock($oFilter, 'search', false);
  255. $sHtml .= $oBlock->GetDisplay($oPage, "SearchFormToAdd_{$this->m_sAttCode}{$this->m_sNameSuffix}", array('open' => true));
  256. $sHtml .= "<form id=\"ObjectsAddForm_{$this->m_sAttCode}{$this->m_sNameSuffix}\" OnSubmit=\"return oWidget{$this->m_iInputId}.DoAddObjects(this.id);\">\n";
  257. $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";
  258. $sHtml .= "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>".Dict::S('UI:Message:EmptyList:UseSearchForm')."</p></div>\n";
  259. $sHtml .= "</div>\n";
  260. $sHtml .= "<input type=\"hidden\" id=\"count_{$this->m_sAttCode}{$this->m_sNameSuffix}\" value=\"0\"/>";
  261. $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')."\">";
  262. $sHtml .= "</div>\n";
  263. $sHtml .= "</form>\n";
  264. $sHtml .= "</div>\n";
  265. $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 });");
  266. $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)))."'});");
  267. $oPage->add_ready_script("$('#SearchFormToAdd_{$this->m_sAttCode}{$this->m_sNameSuffix} form').bind('submit.uilinksWizard', oWidget{$this->m_iInputId}.SearchObjectsToAdd);");
  268. $oPage->add_ready_script("$('#SearchFormToAdd_{$this->m_sAttCode}{$this->m_sNameSuffix}').resize(oWidget{$this->m_iInputId}.UpdateSizes);");
  269. return $sHtml;
  270. }
  271. /**
  272. * Search for objects to be linked to the current object (i.e "remote" objects)
  273. * @param WebPage $oP The page used for the output (usually an AjaxWebPage)
  274. * @param string $sRemoteClass Name of the "remote" class to perform the search on, must be a derived class of m_sRemoteClass
  275. * @param Array $aAlreadyLinkedIds List of IDs of objects of "remote" class already linked, to be filtered out of the search
  276. */
  277. public function SearchObjectsToAdd(WebPage $oP, $sRemoteClass = '', $aAlreadyLinkedIds = array())
  278. {
  279. if ($sRemoteClass != '')
  280. {
  281. // assert(MetaModel::IsParentClass($this->m_sRemoteClass, $sRemoteClass));
  282. $oFilter = new DBObjectSearch($sRemoteClass);
  283. }
  284. else
  285. {
  286. // No remote class specified use the one defined in the linkedset
  287. $oFilter = new DBObjectSearch($this->m_sRemoteClass);
  288. }
  289. if (!$this->m_bDuplicatesAllowed && count($aAlreadyLinkedIds) > 0)
  290. {
  291. // Positive IDs correspond to existing link records
  292. // negative IDs correspond to "remote" objects to be linked
  293. $aLinkIds = array();
  294. $aRemoteObjIds = array();
  295. foreach($aAlreadyLinkedIds as $iId)
  296. {
  297. if ($iId > 0)
  298. {
  299. $aLinkIds[] = $iId;
  300. }
  301. else
  302. {
  303. $aRemoteObjIds[] = -$iId;
  304. }
  305. }
  306. if (count($aLinkIds) >0)
  307. {
  308. // Search for the links to find to which "remote" object they are linked
  309. $oLinkFilter = new DBObjectSearch($this->m_sLinkedClass);
  310. $oLinkFilter->AddCondition('id', $aLinkIds, 'IN');
  311. $oLinkSet = new CMDBObjectSet($oLinkFilter);
  312. while($oLink = $oLinkSet->Fetch())
  313. {
  314. $aRemoteObjIds[] = $oLink->Get($this->m_sExtKeyToRemote);
  315. }
  316. }
  317. $oFilter->AddCondition('id', $aRemoteObjIds, 'NOTIN');
  318. }
  319. $oSet = new CMDBObjectSet($oFilter);
  320. $oBlock = new DisplayBlock($oFilter, 'list', false);
  321. $oBlock->Display($oP, "ResultsToAdd_{$this->m_sAttCode}", array('menu' => false, 'cssCount'=> '#count_'.$this->m_sAttCode.$this->m_sNameSuffix , 'selection_mode' => true)); // Don't display the 'Actions' menu on the results
  322. }
  323. public function DoAddObjects(WebPage $oP, $oFullSetFilter)
  324. {
  325. $aLinkedObjectIds = utils::ReadMultipleSelection($oFullSetFilter);
  326. foreach($aLinkedObjectIds as $iObjectId)
  327. {
  328. $oLinkedObj = MetaModel::GetObject($this->m_sRemoteClass, $iObjectId);
  329. if (is_object($oLinkedObj))
  330. {
  331. $aRow = $this->GetFormRow($oP, $oLinkedObj, -$iObjectId ); // Not yet created link get negative Ids
  332. $oP->add($this->DisplayFormRow($oP, $this->m_aTableConfig, $aRow, -$iObjectId));
  333. }
  334. else
  335. {
  336. $oP->p(Dict::Format('UI:Error:Object_Class_Id_NotFound', $this->m_sLinkedClass, $iObjectId));
  337. }
  338. }
  339. }
  340. }
  341. ?>