ui.linkswidget.class.inc.php 16 KB

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