ui.linkswidget.class.inc.php 13 KB

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