ui.linkswidget.class.inc.php 14 KB

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