ui.linkswidget.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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('../application/webpage.class.inc.php');
  25. require_once('../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. public function __construct($sClass, $sAttCode, $iInputId, $sNameSuffix = '')
  33. {
  34. $this->m_sClass = $sClass;
  35. $this->m_sAttCode = $sAttCode;
  36. $this->m_sNameSuffix = $sNameSuffix;
  37. $this->m_iInputId = $iInputId;
  38. }
  39. public function Display(WebPage $oPage, $oCurrentValuesSet = null)
  40. {
  41. $sHTMLValue = '';
  42. $sTargetClass = self::GetTargetClass($this->m_sClass, $this->m_sAttCode);
  43. // #@# todo - add context information, otherwise any value will be authorized for external keys
  44. $aAllowedValues = MetaModel::GetAllowedValues_att($this->m_sClass, $this->m_sAttCode, array(), '');
  45. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sAttCode);
  46. $sExtKeyToRemote = $oAttDef->GetExtKeyToRemote();
  47. $sExtKeyToMe = $oAttDef->GetExtKeyToMe();
  48. $sStateAttCode = MetaModel::GetStateAttributeCode($this->m_sClass);
  49. $sDefaultState = MetaModel::GetDefaultState($this->m_sClass);
  50. $aAttributes = array();
  51. $sLinkedClass = $oAttDef->GetLinkedClass();
  52. foreach(MetaModel::ListAttributeDefs($sLinkedClass) as $sAttCode=>$oAttDef)
  53. {
  54. if ($sStateAttCode == $sAttCode)
  55. {
  56. // State attribute is always hidden from the UI
  57. }
  58. else if (!$oAttDef->IsExternalField() && ($sAttCode != $sExtKeyToMe) && ($sAttCode != $sExtKeyToRemote))
  59. {
  60. $iFlags = MetaModel::GetAttributeFlags($this->m_sClass, $sDefaultState, $sAttCode);
  61. if ( !($iFlags & OPT_ATT_HIDDEN) && !($iFlags & OPT_ATT_READONLY) )
  62. {
  63. $aAttributes[] = $sAttCode;
  64. }
  65. }
  66. }
  67. $sAttributes = "['".implode("','", $aAttributes)."']";
  68. if ($oCurrentValuesSet != null)
  69. {
  70. // Serialize the link set into a JSon object
  71. $aCurrentValues = array();
  72. $oCurrentValuesSet->Rewind(); // Make sure we can iterate through this set...
  73. while($oLinkObj = $oCurrentValuesSet->Fetch())
  74. {
  75. $sRow = '{';
  76. foreach($aAttributes as $sLinkAttCode)
  77. {
  78. $sRow.= "\"$sLinkAttCode\": \"".addslashes($oLinkObj->Get($sLinkAttCode))."\", ";
  79. }
  80. $sRow .= "\"$sExtKeyToRemote\": ".$oLinkObj->Get($sExtKeyToRemote).'}';
  81. $aCurrentValues[] = $sRow;
  82. }
  83. $sJSON = '['.implode(',', $aCurrentValues).']';
  84. }
  85. else
  86. {
  87. $sJSON = '[]'; // Empty array;
  88. //echo "JSON VA IECH<br/>\n";
  89. }
  90. //echo "JASON: $sJSON<br/>\n";;
  91. // Many values (or even a unknown list) display an autocomplete
  92. if ( (count($aAllowedValues) == 0) || (count($aAllowedValues) > 50) )
  93. {
  94. // too many choices, use an autocomplete
  95. // The input for the auto complete
  96. $sTitle = $oAttDef->GetDescription();
  97. $sHTMLValue .= "<script type=\"text/javascript\">\n";
  98. $sHTMLValue .= "oLinkWidget{$this->m_iInputId} = new LinksWidget('{$this->m_iInputId}', '$sLinkedClass', '$sExtKeyToMe', '$sExtKeyToRemote', $sAttributes);\n";
  99. $sHTMLValue .= "</script>\n";
  100. $oPage->add_at_the_end($this->GetObjectPickerDialog($oPage, $sTargetClass, 'oLinkWidget'.$this->m_iInputId.'.OnOk')); // Forms should not be inside forms
  101. $oPage->add_at_the_end($this->GetLinkObjectDialog($oPage, $this->m_iInputId)); // Forms should not be inside forms
  102. $sHTMLValue .= "<input type=\"text\" id=\"ac_{$this->m_iInputId}\" size=\"35\" value=\"\" title=\"".Dict::S('UI:LinksWidget:Autocomplete+')."\"/>";
  103. $sHTMLValue .= "<input type=\"button\" id=\"ac_add_{$this->m_iInputId}\" value=\"".Dict::S('UI:Button:AddObject')."\" class=\"action\" onClick=\"oLinkWidget{$this->m_iInputId}.AddObject();\"/>";
  104. $sHTMLValue .= "&nbsp;<input type=\"button\" value=\"".Dict::S('UI:Button:BrowseObjects')."\" class=\"action\" onClick=\"return ManageObjects('$sTitle', '$sTargetClass', '$this->m_iInputId', '$sExtKeyToRemote');\"/>&nbsp<span id=\"v_{$this->m_iInputId}\"></span>\n";
  105. // another hidden input to store & pass the object's Id
  106. $sHTMLValue .= "<input type=\"hidden\" id=\"id_ac_{$this->m_iInputId}\" onChange=\"EnableAddButton('{$this->m_iInputId}');\"/>\n";
  107. $sHTMLValue .= "<input type=\"hidden\" id=\"{$this->m_iInputId}\" name=\"attr_{$this->m_sAttCode}{$this->m_sNameSuffix}\" value=\"\"/>\n";
  108. $oPage->add_ready_script("\$('#{$this->m_iInputId}').val('$sJSON');\noLinkWidget{$this->m_iInputId}.Init();\n\$('#ac_{$this->m_iInputId}').autocomplete('./ajax.render.php', { scroll:true, minChars:3, onItemSelect:selectItem, onFindValue:findValue, formatItem:formatItem, autoFill:true, keyHolder:'#id_ac_{$this->m_iInputId}', extraParams:{operation:'ui.linkswidget', sclass:'{$this->m_sClass}', attCode:'{$this->m_sAttCode}', max:30}});");
  109. $oPage->add_ready_script("\$('#ac_add_{$this->m_iInputId}').attr('disabled', 'disabled');");
  110. $oPage->add_ready_script("\$('#ac_{$this->m_iInputId}').result( function(event, data, formatted) { if (data) { $('#id_ac_{$this->m_iInputId}').val(data[1]); $('#ac_add_{$this->m_iInputId}').attr('disabled', ''); } else { $('#ac_add_{$this->m_iInputId}').attr('disabled', 'disabled'); } } );");
  111. }
  112. else
  113. {
  114. // Few choices, use a normal 'select'
  115. $sHTMLValue = "<select name=\"attr_{$this->m_sAttCode}\" id=\"{$this->m_iInputId}\">\n";
  116. $sHTMLValue .= "<option value=\"0\">".Dict::S('UI:Combo:SelectValue')."</option>\n";
  117. if (count($aAllowedValues) > 0)
  118. {
  119. foreach($aAllowedValues as $key => $value)
  120. {
  121. $sHTMLValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  122. }
  123. }
  124. $sHTMLValue .= "</select>\n";
  125. }
  126. $sHTMLValue .= "<div id=\"{$this->m_iInputId}_values\">\n";
  127. if ($oCurrentValuesSet != null)
  128. {
  129. // transform the DBObjectSet into a CMDBObjectSet !!!
  130. $aLinkedObjects = $oCurrentValuesSet->ToArray(false);
  131. // Actual values will be displayed asynchronously, no need to display them here
  132. //if (count($aLinkedObjects) > 0)
  133. //{
  134. // $oSet = CMDBObjectSet::FromArray($sLinkedClass, $aLinkedObjects);
  135. // $oDisplayBlock = DisplayBlock::FromObjectSet($oSet, 'list');
  136. // $sHTMLValue .= $oDisplayBlock->GetDisplay($oPage, $this->m_iInputId.'_current', array('linkage' => $sExtKeyToMe, 'menu' => false));
  137. //}
  138. }
  139. $sHTMLValue .= "</div>\n";
  140. return $sHTMLValue;
  141. }
  142. /**
  143. * This static function is called by the Ajax Page when there is a need to fill an autocomplete combo
  144. * @param $oPage WebPage The ajax page used for the put^put (sent back to the browser
  145. * @param $oContext UserContext The context of the user (for limiting the search)
  146. * @param $sClass string The name of the class of the current object being edited
  147. * @param $sAttCode string The name of the attribute being edited
  148. * @param $sName string The partial name that was typed by the user
  149. * @param $iMaxCount integer The maximum number of items to return
  150. * @return void
  151. */
  152. static public function Autocomplete(WebPage $oPage, UserContext $oContext, $sClass, $sAttCode, $sName, $iMaxCount)
  153. {
  154. // #@# todo - add context information, otherwise any value will be authorized for external keys
  155. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, array() /* $aArgs */, $sName);
  156. if ($aAllowedValues != null)
  157. {
  158. $iCount = $iMaxCount;
  159. foreach($aAllowedValues as $key => $value)
  160. {
  161. $oPage->add($value."|".$key."\n");
  162. $iCount--;
  163. if ($iCount == 0) break;
  164. }
  165. }
  166. else // No limitation to the allowed values
  167. {
  168. // Search for all the object of the linked class
  169. $oAttDef = $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  170. $sLinkedClass = $oAttDef->GetLinkedClass();
  171. $sSearchClass = self::GetTargetClass($sClass, $sAttCode);
  172. $oFilter = $oContext->NewFilter($sSearchClass);
  173. $sSearchAttCode = MetaModel::GetNameAttributeCode($sSearchClass);
  174. $oFilter->AddCondition($sSearchAttCode, $sName, 'Begins with');
  175. $oSet = new CMDBObjectSet($oFilter, array($sSearchAttCode => true));
  176. $iCount = 0;
  177. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  178. {
  179. $oPage->add($oObj->GetName()."|".$oObj->GetKey()."\n");
  180. $iCount++;
  181. }
  182. }
  183. }
  184. /**
  185. * This static function is called by the Ajax Page display a set of objects being linked
  186. * to the object being created
  187. * @param $oPage WebPage The ajax page used for the put^put (sent back to the browser
  188. * @param $sClass string The name of the 'linking class' which is the class of the objects to display
  189. * @param $sSet JSON serialized set of objects
  190. * @param $sExtKeyToMe Name of the attribute in sClass that is pointing to a given object
  191. * @param $iObjectId The id of the object $sExtKeyToMe is pointing to
  192. * @return void
  193. */
  194. static public function RenderSet($oPage, $sClass, $sJSONSet, $sExtKeyToMe, $sExtKeyToRemote, $iObjectId)
  195. {
  196. $aSet = json_decode($sJSONSet, true); // true means hash array instead of object
  197. $oSet = CMDBObjectSet::FromScratch($sClass);
  198. foreach($aSet as $aObject)
  199. {
  200. $oObj = MetaModel::NewObject($sClass);
  201. foreach($aObject as $sAttCode => $value)
  202. {
  203. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  204. if ($oAttDef->IsExternalKey())
  205. {
  206. $oTargetObj = MetaModel::GetObject($oAttDef->GetTargetClass(), $value); // @@ optimization, don't do & query per object in the set !
  207. $oObj->Set($sAttCode, $oTargetObj);
  208. }
  209. else
  210. {
  211. $oObj->Set($sAttCode, $value);
  212. }
  213. }
  214. $oSet->AddObject($oObj);
  215. }
  216. $aExtraParams = array();
  217. $aExtraParams['link_attr'] = $sExtKeyToMe;
  218. $aExtraParams['object_id'] = $iObjectId;
  219. $aExtraParams['target_attr'] = $sExtKeyToRemote;
  220. $aExtraParams['menu'] = false;
  221. $aExtraParams['select'] = false;
  222. $aExtraParams['view_link'] = false;
  223. cmdbAbstractObject::DisplaySet($oPage, $oSet, $aExtraParams);
  224. }
  225. protected static function GetTargetClass($sClass, $sAttCode)
  226. {
  227. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  228. $sLinkedClass = $oAttDef->GetLinkedClass();
  229. switch(get_class($oAttDef))
  230. {
  231. case 'AttributeLinkedSetIndirect':
  232. $oLinkingAttDef = MetaModel::GetAttributeDef($sLinkedClass, $oAttDef->GetExtKeyToRemote());
  233. $sTargetClass = $oLinkingAttDef->GetTargetClass();
  234. break;
  235. case 'AttributeLinkedSet':
  236. $sTargetClass = $sLinkedClass;
  237. break;
  238. }
  239. return $sTargetClass;
  240. }
  241. protected function GetObjectPickerDialog($oPage, $sTargetClass, $sOkFunction)
  242. {
  243. $sDialogTitle = Dict::S('UI:ManageObjectsDlg');
  244. $sOkBtnLabel = Dict::S('UI:Button:Ok');
  245. $sCancelBtnLabel = Dict::S('UI:Button:Cancel');
  246. $sAddBtnLabel = Dict::S('UI:Button:AddToList');
  247. $sRemoveBtnLabel = Dict::S('UI:Button:RemoveFromList');
  248. $sFilterBtnLabel = Dict::S('UI:Button:FilterList');
  249. $sLabelSelectedObjects = Dict::S('UI:Label:SelectedObjects');
  250. $sLabelAvailableObjects = Dict::S('UI:Label:AvailableObjects');
  251. $sHTML = <<< EOF
  252. <div style="display:none" title="$sDialogTitle" id="ManageObjectsDlg_{$this->m_iInputId}">
  253. <div class="wizContainer">
  254. <table width="100%">
  255. <tr>
  256. <td>
  257. <p>$sLabelSelectedObjects</p>
  258. <button type="button" class="action" onClick="FilterLeft('$sTargetClass');"><span>$sFilterBtnLabel</span></button>
  259. <p><select id="selected_objects_{$this->m_iInputId}" size="10" multiple onChange="Manage_UpdateButtons('$this->m_iInputId')" style="width:300px;">
  260. </select></p>
  261. </td>
  262. <td style="text-align:center; valign:middle;">
  263. <p><button type="button" id="btn_add_objects_{$this->m_iInputId}" onClick="Manage_AddObjects('$this->m_iInputId');">$sAddBtnLabel</button></p>
  264. <p><button type="button" id="btn_remove_objects_{$this->m_iInputId}" onClick="Manage_RemoveObjects('$this->m_iInputId');">$sRemoveBtnLabel</button></p>
  265. </td>
  266. <td>
  267. <p>$sLabelAvailableObjects</p>
  268. <button type="button" class="action" onClick="FilterRight('$sTargetClass');"><span>$sFilterBtnLabel</span></button>
  269. <p><select id="available_objects_{$this->m_iInputId}" size="10" multiple onChange="Manage_UpdateButtons('$this->m_iInputId')" style="width:300px;">
  270. </select></p>
  271. </td>
  272. </tr>
  273. <tr>
  274. <td colspan="3">
  275. <input type="submit" onClick="$('#ManageObjectsDlg_{$this->m_iInputId}').dialog('close'); $sOkFunction('$sTargetClass', 'selected_objects'); return false;" value="$sOkBtnLabel" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" onClick="$('#ManageObjectsDlg_{$this->m_iInputId}').dialog('close');">$sCancelBtnLabel</button>
  276. </td>
  277. </tr>
  278. </table>
  279. </div>
  280. </div>
  281. EOF;
  282. $oPage->add_ready_script("$('#ManageObjectsDlg_$this->m_iInputId').dialog( {autoOpen: false, modal: true, width: 750, height: 350} );"); // JQuery UI dialog
  283. //$oPage->add_ready_script("UpdateObjectList('$sClass');");
  284. return $sHTML;
  285. }
  286. protected function GetLinkObjectDialog($oPage, $sId)
  287. {
  288. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sAttCode);
  289. $sLinkedClass = $oAttDef->GetLinkedClass();
  290. $sStateAttCode = MetaModel::GetStateAttributeCode($sLinkedClass);
  291. $sDefaultState = MetaModel::GetDefaultState($sLinkedClass);
  292. $oAttDef = MetaModel::GetAttributeDef($this->m_sClass, $this->m_sAttCode);
  293. $sExtKeyToMe = $oAttDef->GetExtKeyToMe();
  294. $sExtKeyToRemote = $oAttDef->GetExtKeyToRemote();
  295. $sHTML = "<div style=\"display:none\" title=\"".Dict::Format('UI:Link_Class_Attributes', MetaModel::GetName($sLinkedClass))."\" id=\"LinkDlg_$sId\">\n";
  296. $sHTML .= "<div class=\"wizContainer\">\n";
  297. $sHTML .= "<form action=\"./UI.php\" onSubmit=\"return oLinkWidget$sId.OnLinkOk();\">\n";
  298. $index = 0;
  299. $aAttrsMap = array();
  300. $aDetails = array();
  301. foreach(MetaModel::ListAttributeDefs($sLinkedClass) as $sAttCode=>$oAttDef)
  302. {
  303. if ($sStateAttCode == $sAttCode)
  304. {
  305. // State attribute is always hidden from the UI
  306. //$sHTMLValue = $this->GetStateLabel();
  307. //$aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  308. }
  309. else if (!$oAttDef->IsExternalField() && ($sAttCode != $sExtKeyToMe) && ($sAttCode != $sExtKeyToRemote))
  310. {
  311. $iFlags = MetaModel::GetAttributeFlags($sLinkedClass, $sDefaultState, $sAttCode);
  312. if ($iFlags & OPT_ATT_HIDDEN)
  313. {
  314. // Attribute is hidden, do nothing
  315. }
  316. else
  317. {
  318. if ($iFlags & OPT_ATT_READONLY)
  319. {
  320. // Attribute is read-only
  321. $sHTMLValue = $this->GetAsHTML($sAttCode);
  322. }
  323. else
  324. {
  325. $sValue = ""; //$this->Get($sAttCode);
  326. $sDisplayValue = ""; //$this->GetEditValue($sAttCode);
  327. $sSubId = $sId.'_'.$index;
  328. $aAttrsMap[$sAttCode] = $sSubId;
  329. $index++;
  330. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sLinkedClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sSubId, $this->m_sAttCode);
  331. }
  332. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  333. }
  334. }
  335. }
  336. $sHTML .= $oPage->GetDetails($aDetails);
  337. $sHTML .= "<input type=\"submit\" onClick=\"oLinkWidget$sId.OnLinkOk(); return false;\" value=\"".Dict::S('UI:Button:Ok')."\" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type=\"button\" onClick=\"oLinkWidget$sId.OnLinkCancel()\">".Dict::S('UI:Button:Cancel')."</button>\n";
  338. $sHTML .= "</form>\n";
  339. $sHTML .= "</div>\n";
  340. $sHTML .= "</div>\n";
  341. $oPage->add_ready_script("$('#LinkDlg_$sId').dialog( {autoOpen: false, modal: true, width: 300 } );"); // jQuery UI dialog
  342. //$oPage->add_ready_script("UpdateObjectList('$sClass');");
  343. return $sHTML;
  344. }
  345. }
  346. ?>