ui.linkswidget.class.inc.php 13 KB

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