ui.linkswidget.class.inc.php 16 KB

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