ui.extkeywidget.class.inc.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 UIExtKeyWidget
  18. * UI wdiget for displaying and editing external keys when
  19. * A simple drop-down list is not enough...
  20. *
  21. * The layout is the following
  22. *
  23. * +-- #label_<id> (input)-------+ +-----------+
  24. * | | | Browse... |
  25. * +-----------------------------+ +-----------+
  26. *
  27. * And the popup dialog has the following layout:
  28. *
  29. * +------------------- ac_dlg_<id> (div)-----------+
  30. * + +--- ds_<id> (div)---------------------------+ |
  31. * | | +------------- fs_<id> (form)------------+ | |
  32. * | | | +--------+---+ | | |
  33. * | | | | Class | V | | | |
  34. * | | | +--------+---+ | | |
  35. * | | | | | |
  36. * | | | S e a r c h F o r m | | |
  37. * | | | +--------+ | | |
  38. * | | | | Search | | | |
  39. * | | | +--------+ | | |
  40. * | | +----------------------------------------+ | |
  41. * | +--------------+-dh_<id>-+--------------------+ |
  42. * | \ Search / |
  43. * | +------+ |
  44. * | +--- fr_<id> (form)--------------------------+ |
  45. * | | +------------ dr_<id> (div)--------------+ | |
  46. * | | | | | |
  47. * | | | S e a r c h R e s u l t s | | |
  48. * | | | | | |
  49. * | | +----------------------------------------+ | |
  50. * | | +--------+ +-----+ | |
  51. * | | | Cancel | | Add | | |
  52. * | | +--------+ +-----+ | |
  53. * | +--------------------------------------------+ |
  54. * +------------------------------------------------+
  55. * @author Erwan Taloc <erwan.taloc@combodo.com>
  56. * @author Romain Quetiez <romain.quetiez@combodo.com>
  57. * @author Denis Flaven <denis.flaven@combodo.com>
  58. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  59. */
  60. require_once(APPROOT.'/application/webpage.class.inc.php');
  61. require_once(APPROOT.'/application/displayblock.class.inc.php');
  62. class UIExtKeyWidget
  63. {
  64. protected $iId;
  65. protected $sTargetClass;
  66. //public function __construct($sAttCode, $sClass, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sNameSuffix = '', $sFieldPrefix = '', $sFormPrefix = '')
  67. static public function DisplayFromAttCode($oPage, $sAttCode, $sClass, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sFieldName = '', $sFormPrefix = '', $aArgs, $bSearchMode = false)
  68. {
  69. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  70. $sTargetClass = $oAttDef->GetTargetClass();
  71. $iMaxComboLength = $oAttDef->GetMaximumComboLength();
  72. $bAllowTargetCreation = $oAttDef->AllowTargetCreation();
  73. if (!$bSearchMode)
  74. {
  75. $sDisplayStyle = $oAttDef->GetDisplayStyle();
  76. }
  77. else
  78. {
  79. $sDisplayStyle = 'select'; // In search mode, always use a drop-down list
  80. }
  81. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  82. return $oWidget->Display($oPage, $iMaxComboLength, $bAllowTargetCreation, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sFieldName, $sFormPrefix, $aArgs, $bSearchMode, $sDisplayStyle);
  83. }
  84. public function __construct($sTargetClass, $iInputId)
  85. {
  86. $this->sTargetClass = $sTargetClass;
  87. $this->iId = $iInputId;
  88. }
  89. /**
  90. * Get the HTML fragment corresponding to the linkset editing widget
  91. * @param WebPage $oP The web page used for all the output
  92. * @param Hash $aArgs Extra context arguments
  93. * @return string The HTML fragment to be inserted into the page
  94. */
  95. public function Display(WebPage $oPage, $iMaxComboLength, $bAllowTargetCreation, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sFieldName, $sFormPrefix = '', $aArgs = array(), $bSearchMode = false, $sDisplayStyle = 'select')
  96. {
  97. $sTitle = addslashes($sTitle);
  98. $oPage->add_linked_script('../js/extkeywidget.js');
  99. $oPage->add_linked_script('../js/forms-json-utils.js');
  100. $bCreate = (!$bSearchMode) && (!MetaModel::IsAbstract($this->sTargetClass)) && (UserRights::IsActionAllowed($this->sTargetClass, UR_ACTION_BULK_MODIFY) && $bAllowTargetCreation);
  101. $bExtensions = true;
  102. $sMessage = Dict::S('UI:Message:EmptyList:UseSearchForm');
  103. $sAttrFieldPrefix = ($bSearchMode) ? '' : 'attr_';
  104. $sHTMLValue = "<span style=\"white-space:nowrap\">"; // no wrap
  105. $sFilter = addslashes($oAllowedValues->GetFilter()->ToOQL());
  106. if($bSearchMode)
  107. {
  108. $sWizHelper = 'null';
  109. $sWizHelperJSON = "''";
  110. }
  111. else
  112. {
  113. $sWizHelper = 'oWizardHelper'.$sFormPrefix;
  114. $sWizHelperJSON = $sWizHelper.'.ToJSON()';
  115. }
  116. if (is_null($oAllowedValues))
  117. {
  118. throw new Exception('Implementation: null value for allowed values definition');
  119. }
  120. elseif ($oAllowedValues->Count() < $iMaxComboLength)
  121. {
  122. // Discrete list of values, use a SELECT or RADIO buttons depending on the config
  123. switch($sDisplayStyle)
  124. {
  125. case 'radio':
  126. case 'radio_horizontal':
  127. case 'radio_vertical':
  128. $sValidationField = "<span id=\"v_{$this->iId}\"></span>";
  129. $sHTMLValue = '';
  130. $bVertical = ($sDisplayStyle != 'radio_horizontal');
  131. $bExtensions = false;
  132. $oAllowedValues->Rewind();
  133. $aAllowedValues = array();
  134. while($oObj = $oAllowedValues->Fetch())
  135. {
  136. $aAllowedValues[$oObj->GetKey()] = $oObj->GetName();
  137. }
  138. $sHTMLValue = $oPage->GetRadioButtons($aAllowedValues, $value, $this->iId, "{$sAttrFieldPrefix}{$sFieldName}", $bMandatory, $bVertical, $sValidationField);
  139. $aEventsList[] ='change';
  140. break;
  141. case 'select':
  142. default:
  143. $sSelectMode = 'true';
  144. $sHelpText = ''; //$this->oAttDef->GetHelpOnEdition();
  145. $sHTMLValue = "<select title=\"$sHelpText\" name=\"{$sAttrFieldPrefix}{$sFieldName}\" id=\"$this->iId\">\n";
  146. if ($bSearchMode)
  147. {
  148. $sDisplayValue = isset($aArgs['sDefaultValue']) ? $aArgs['sDefaultValue'] : Dict::S('UI:SearchValue:Any');
  149. $sHTMLValue .= "<option value=\"\">$sDisplayValue</option>\n";
  150. }
  151. else
  152. {
  153. $sHTMLValue .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>\n";
  154. }
  155. $oAllowedValues->Rewind();
  156. while($oObj = $oAllowedValues->Fetch())
  157. {
  158. $key = $oObj->GetKey();
  159. $display_value = $oObj->GetName();
  160. if (($oAllowedValues->Count() == 1) && ($bMandatory == 'true') )
  161. {
  162. // When there is only once choice, select it by default
  163. $sSelected = ' selected';
  164. }
  165. else
  166. {
  167. $sSelected = ($value == $key) ? ' selected' : '';
  168. }
  169. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  170. }
  171. $sHTMLValue .= "</select>\n";
  172. $oPage->add_ready_script(
  173. <<<EOF
  174. oACWidget_{$this->iId} = new ExtKeyWidget('{$this->iId}', '{$this->sTargetClass}', '$sFilter', '$sTitle', true, $sWizHelper);
  175. oACWidget_{$this->iId}.emptyHtml = "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>$sMessage</p></div>";
  176. $('#$this->iId').bind('update', function() { oACWidget_{$this->iId}.Update(); } );
  177. $('#$this->iId').bind('change', function() { $(this).trigger('extkeychange') } );
  178. EOF
  179. );
  180. } // Switch
  181. }
  182. else
  183. {
  184. // Too many choices, use an autocomplete
  185. $sSelectMode = 'false';
  186. if (is_null($value) || ($value == 0)) // Null values are displayed as ''
  187. {
  188. $sDisplayValue = isset($aArgs['sDefaultValue']) ? $aArgs['sDefaultValue'] : '';
  189. }
  190. else
  191. {
  192. $sDisplayValue = $this->GetObjectName($value);
  193. }
  194. $iMinChars = isset($aArgs['iMinChars']) ? $aArgs['iMinChars'] : 3; //@@@ $this->oAttDef->GetMinAutoCompleteChars();
  195. $iFieldSize = isset($aArgs['iFieldSize']) ? $aArgs['iFieldSize'] : 30; //@@@ $this->oAttDef->GetMaxSize();
  196. // the input for the auto-complete
  197. $sHTMLValue = "<input count=\"".$oAllowedValues->Count()."\" type=\"text\" id=\"label_$this->iId\" size=\"$iFieldSize\" value=\"$sDisplayValue\"/>&nbsp;";
  198. $sHTMLValue .= "<img id=\"mini_search_{$this->iId}\" style=\"border:0;vertical-align:middle;cursor:pointer;\" src=\"../images/mini_search.gif\" onClick=\"oACWidget_{$this->iId}.Search();\"/>&nbsp;";
  199. // another hidden input to store & pass the object's Id
  200. $sHTMLValue .= "<input type=\"hidden\" id=\"$this->iId\" name=\"{$sAttrFieldPrefix}{$sFieldName}\" value=\"$value\" />\n";
  201. // Scripts to start the autocomplete and bind some events to it
  202. $oPage->add_ready_script(
  203. <<<EOF
  204. oACWidget_{$this->iId} = new ExtKeyWidget('{$this->iId}', '{$this->sTargetClass}', '$sFilter', '$sTitle', false, $sWizHelper);
  205. oACWidget_{$this->iId}.emptyHtml = "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>$sMessage</p></div>";
  206. $('#label_$this->iId').autocomplete(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', { scroll:true, minChars:{$iMinChars}, autoFill:false, matchContains:true, mustMatch: true, keyHolder:'#{$this->iId}', extraParams:{operation:'ac_extkey', sTargetClass:'{$this->sTargetClass}',sFilter:'$sFilter', json: function() { return $sWizHelperJSON; } }});
  207. $('#label_$this->iId').keyup(function() { if ($(this).val() == '') { $('#$this->iId').val(''); } } ); // Useful for search forms: empty value in the "label", means no value, immediatly !
  208. $('#label_$this->iId').result( function(event, data, formatted) { OnAutoComplete('{$this->iId}', event, data, formatted); } );
  209. $('#$this->iId').bind('update', function() { oACWidget_{$this->iId}.Update(); } );
  210. if ($('#ac_dlg_{$this->iId}').length == 0)
  211. {
  212. $('body').append('<div id="ac_dlg_{$this->iId}"></div>');
  213. }
  214. EOF
  215. );
  216. }
  217. if ($bExtensions && MetaModel::IsHierarchicalClass($this->sTargetClass) !== false)
  218. {
  219. $sHTMLValue .= "<img id=\"mini_tree_{$this->iId}\" style=\"border:0;vertical-align:middle;cursor:pointer;\" src=\"../images/mini_tree.gif\" onClick=\"oACWidget_{$this->iId}.HKDisplay();\"/>&nbsp;";
  220. $oPage->add_ready_script(
  221. <<<EOF
  222. if ($('#ac_tree_{$this->iId}').length == 0)
  223. {
  224. $('body').append('<div id="ac_tree_{$this->iId}"></div>');
  225. }
  226. EOF
  227. );
  228. }
  229. if ($bCreate && $bExtensions)
  230. {
  231. $sHTMLValue .= "<img id=\"mini_add_{$this->iId}\" style=\"border:0;vertical-align:middle;cursor:pointer;\" src=\"../images/mini_add.gif\" onClick=\"oACWidget_{$this->iId}.CreateObject();\"/>&nbsp;";
  232. $oPage->add_ready_script(
  233. <<<EOF
  234. if ($('#ajax_{$this->iId}').length == 0)
  235. {
  236. $('body').append('<div id="ajax_{$this->iId}"></div>');
  237. }
  238. EOF
  239. );
  240. }
  241. if ($sDisplayStyle == 'select')
  242. {
  243. $sHTMLValue .= "<span id=\"v_{$this->iId}\"></span>";
  244. }
  245. $sHTMLValue .= "</span>"; // end of no wrap
  246. return $sHTMLValue;
  247. }
  248. public function GetSearchDialog(WebPage $oPage, $sTitle)
  249. {
  250. $sHTML = '<div class="wizContainer" style="vertical-align:top;"><div id="dc_'.$this->iId.'">';
  251. $oFilter = new DBObjectSearch($this->sTargetClass);
  252. $oSet = new CMDBObjectSet($oFilter);
  253. $oBlock = new DisplayBlock($oFilter, 'search', false);
  254. $sHTML .= $oBlock->GetDisplay($oPage, $this->iId, array('open' => true, 'currentId' => $this->iId));
  255. $sHTML .= "<form id=\"fr_{$this->iId}\" OnSubmit=\"return oACWidget_{$this->iId}.DoOk();\">\n";
  256. $sHTML .= "<div id=\"dr_{$this->iId}\" style=\"vertical-align:top;background: #fff;height:100%;overflow:auto;padding:0;border:0;\">\n";
  257. $sHTML .= "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>".Dict::S('UI:Message:EmptyList:UseSearchForm')."</p></div>\n";
  258. $sHTML .= "</div>\n";
  259. $sHTML .= "<input type=\"button\" id=\"btn_cancel_{$this->iId}\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"$('#ac_dlg_{$this->iId}').dialog('close');\">&nbsp;&nbsp;";
  260. $sHTML .= "<input type=\"button\" id=\"btn_ok_{$this->iId}\" value=\"".Dict::S('UI:Button:Ok')."\" onClick=\"oACWidget_{$this->iId}.DoOk();\">";
  261. $sHTML .= "<input type=\"hidden\" id=\"count_{$this->iId}\" value=\"0\">";
  262. $sHTML .= "</form>\n";
  263. $sHTML .= '</div></div>';
  264. $sDialogTitle = addslashes($sTitle);
  265. $oPage->add_ready_script(
  266. <<<EOF
  267. $('#ac_dlg_{$this->iId}').dialog({ width: $(window).width()*0.8, height: $(window).height()*0.8, autoOpen: false, modal: true, title: '$sDialogTitle', resizeStop: oACWidget_{$this->iId}.UpdateSizes, close: oACWidget_{$this->iId}.OnClose });
  268. $('#fs_{$this->iId}').bind('submit.uiAutocomplete', oACWidget_{$this->iId}.DoSearchObjects);
  269. $('#dc_{$this->iId}').resize(oACWidget_{$this->iId}.UpdateSizes);
  270. EOF
  271. );
  272. $oPage->add($sHTML);
  273. }
  274. /**
  275. * Search for objects to be selected
  276. * @param WebPage $oP The page used for the output (usually an AjaxWebPage)
  277. * @param string $sRemoteClass Name of the "remote" class to perform the search on, must be a derived class of m_sRemoteClass
  278. * @param Array $aAlreadyLinkedIds List of IDs of objects of "remote" class already linked, to be filtered out of the search
  279. */
  280. public function SearchObjectsToSelect(WebPage $oP, $sFilter, $sRemoteClass = '', $oObj = null)
  281. {
  282. if (is_null($sFilter))
  283. {
  284. throw new Exception('Implementation: null value for allowed values definition');
  285. }
  286. try
  287. {
  288. $oFilter = DBObjectSearch::FromOQL($sFilter);
  289. $oBlock = new DisplayBlock($oFilter, 'list', false);
  290. $oBlock->Display($oP, $this->iId.'_results', array('this' => $oObj, 'cssCount'=> '#count_'.$this->iId, 'menu' => false, 'selection_mode' => true, 'selection_type' => 'single')); // Don't display the 'Actions' menu on the results
  291. }
  292. catch(MissingQueryArgument $e)
  293. {
  294. // When used in a search form the $this parameter may be missing, in this case return all possible values...
  295. // TODO check if we can improve this behavior...
  296. $sOQL = 'SELECT '.$sRemoteClass;
  297. $oFilter = DBObjectSearch::FromOQL($sOQL);
  298. $oBlock = new DisplayBlock($oFilter, 'list', false);
  299. $oBlock->Display($oP, $this->iId.'_results', array('cssCount'=> '#count_'.$this->iId, 'menu' => false, 'selection_mode' => true, 'selection_type' => 'single')); // Don't display the 'Actions' menu on the results
  300. }
  301. }
  302. /**
  303. * Search for objects to be selected
  304. * @param WebPage $oP The page used for the output (usually an AjaxWebPage)
  305. * @param string $sFilter The OQL expression used to define/limit limit the scope of possible values
  306. * @param DBObject $oObj The current object for the OQL context
  307. * @param string $sContains The text of the autocomplete to filter the results
  308. */
  309. public function AutoComplete(WebPage $oP, $sFilter, $oObj = null, $sContains)
  310. {
  311. if (is_null($sFilter))
  312. {
  313. throw new Exception('Implementation: null value for allowed values definition');
  314. }
  315. $oValuesSet = new ValueSetObjects($sFilter, 'friendlyname'); // Bypass GetName() to avoid the encoding by htmlentities
  316. $aValues = $oValuesSet->GetValues(array('this' => $oObj), $sContains);
  317. foreach($aValues as $sKey => $sFriendlyName)
  318. {
  319. $oP->add(trim($sFriendlyName)."\t".$sKey."\n");
  320. }
  321. }
  322. /**
  323. * Get the display name of the selected object, to fill back the autocomplete
  324. */
  325. public function GetObjectName($iObjId)
  326. {
  327. $oObj = MetaModel::GetObject($this->sTargetClass, $iObjId);
  328. return $oObj->GetName();
  329. }
  330. /**
  331. * Get the form to create a new object of the 'target' class
  332. */
  333. public function GetObjectCreationForm(WebPage $oPage)
  334. {
  335. // Set all the default values in an object and clone this "default" object
  336. $oNewObj = MetaModel::NewObject($this->sTargetClass);
  337. // 1st - set context values
  338. $oAppContext = new ApplicationContext();
  339. $oAppContext->InitObjectFromContext($oNewObj);
  340. // 2nd - set values from the page argument 'default'
  341. $oNewObj->UpdateObjectFromArg('default');
  342. $sDialogTitle = addslashes($this->sTitle);
  343. $oPage->add('<div id="ac_create_'.$this->iId.'"><div class="wizContainer" style="vertical-align:top;"><div id="dcr_'.$this->iId.'">');
  344. $oPage->add("<h1>".MetaModel::GetClassIcon($this->sTargetClass)."&nbsp;".Dict::Format('UI:CreationTitle_Class', MetaModel::GetName($this->sTargetClass))."</h1>\n");
  345. cmdbAbstractObject::DisplayCreationForm($oPage, $this->sTargetClass, $oNewObj, array(), array('formPrefix' => $this->iId, 'noRelations' => true));
  346. $oPage->add('</div></div></div>');
  347. // $oPage->add_ready_script("\$('#ac_create_$this->iId').dialog({ width: $(window).width()*0.8, height: 'auto', autoOpen: false, modal: true, title: '$sDialogTitle'});\n");
  348. $oPage->add_ready_script("\$('#ac_create_$this->iId').dialog({ width: 'auto', height: 'auto', autoOpen: false, modal: true, title: '$sDialogTitle'});\n");
  349. $oPage->add_ready_script("$('#dcr_{$this->iId} form').removeAttr('onsubmit');");
  350. $oPage->add_ready_script("$('#dcr_{$this->iId} form').bind('submit.uilinksWizard', oACWidget_{$this->iId}.DoCreateObject);");
  351. }
  352. /**
  353. * Display the hierarchy of the 'target' class
  354. */
  355. public function DisplayHierarchy(WebPage $oPage, $sFilter, $currValue, $oObj)
  356. {
  357. $sDialogTitle = addslashes(Dict::Format('UI:HierarchyOf_Class', MetaModel::GetName($this->sTargetClass)));
  358. $oPage->add('<div id="dlg_tree_'.$this->iId.'"><div class="wizContainer" style="vertical-align:top;"><div style="overflow:auto;background:#fff;margin-bottom:5px;" id="tree_'.$this->iId.'">');
  359. $oPage->add('<table style="width:100%"><tr><td>');
  360. if (is_null($sFilter))
  361. {
  362. throw new Exception('Implementation: null value for allowed values definition');
  363. }
  364. try
  365. {
  366. $oFilter = DBObjectSearch::FromOQL($sFilter);
  367. $oSet = new DBObjectSet($oFilter, array(), array('this' => $oObj));
  368. }
  369. catch(MissingQueryArgument $e)
  370. {
  371. // When used in a search form the $this parameter may be missing, in this case return all possible values...
  372. // TODO check if we can improve this behavior...
  373. $sOQL = 'SELECT '.$this->m_sTargetClass;
  374. $oFilter = DBObjectSearch::FromOQL($sOQL);
  375. $oSet = new DBObjectSet($oFilter);
  376. }
  377. $sHKAttCode = MetaModel::IsHierarchicalClass($this->sTargetClass);
  378. $this->DumpTree($oPage, $oSet, $sHKAttCode, $currValue);
  379. $oPage->add('</td></tr></table>');
  380. $oPage->add('</div>');
  381. $oPage->add("<input type=\"button\" id=\"btn_cancel_{$this->iId}\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"$('#dlg_tree_{$this->iId}').dialog('close');\">&nbsp;&nbsp;");
  382. $oPage->add("<input type=\"button\" id=\"btn_ok_{$this->iId}\" value=\"".Dict::S('UI:Button:Ok')."\" onClick=\"oACWidget_{$this->iId}.DoHKOk();\">");
  383. $oPage->add('</div></div>');
  384. $oPage->add_ready_script("\$('#tree_$this->iId ul').treeview();\n");
  385. $oPage->add_ready_script("\$('#dlg_tree_$this->iId').dialog({ width: 'auto', height: 'auto', autoOpen: true, modal: true, title: '$sDialogTitle', resizeStop: oACWidget_{$this->iId}.OnHKResize, close: oACWidget_{$this->iId}.OnHKClose });\n");
  386. }
  387. /**
  388. * Get the form to create a new object of the 'target' class
  389. */
  390. public function DoCreateObject($oPage)
  391. {
  392. $oObj = MetaModel::NewObject($this->sTargetClass);
  393. $aErrors = $oObj->UpdateObjectFromPostedForm($this->iId);
  394. if (count($aErrors) == 0)
  395. {
  396. $oMyChange = MetaModel::NewObject("CMDBChange");
  397. $oMyChange->Set("date", time());
  398. $sUserString = CMDBChange::GetCurrentUserName();
  399. $oMyChange->Set("userinfo", $sUserString);
  400. $iChangeId = $oMyChange->DBInsert();
  401. $oObj->DBInsertTracked($oMyChange);
  402. return array('name' => $oObj->GetName(), 'id' => $oObj->GetKey());
  403. }
  404. else
  405. {
  406. return array('name' => implode(' ', $aErrors), 'id' => 0);
  407. }
  408. }
  409. function DumpTree($oP, $oSet, $sParentAttCode, $currValue)
  410. {
  411. $aTree = array();
  412. $aNodes = array();
  413. while($oObj = $oSet->Fetch())
  414. {
  415. $iParentId = $oObj->Get($sParentAttCode);
  416. if (!isset($aTree[$iParentId]))
  417. {
  418. $aTree[$iParentId] = array();
  419. }
  420. $aTree[$iParentId][$oObj->GetKey()] = $oObj->GetName();
  421. $aNodes[$oObj->GetKey()] = $oObj;
  422. }
  423. $aParents = array_keys($aTree);
  424. $aRoots = array();
  425. foreach($aParents as $id)
  426. {
  427. if (!array_key_exists($id, $aNodes))
  428. {
  429. $aRoots[] = $id;
  430. }
  431. }
  432. foreach($aRoots as $iRootId)
  433. {
  434. $this->DumpNodes($oP, $iRootId, $aTree, $aNodes, $currValue);
  435. }
  436. }
  437. function DumpNodes($oP, $iRootId, $aTree, $aNodes, $currValue)
  438. {
  439. $bSelect = true;
  440. $bMultiple = false;
  441. $sSelect = '';
  442. if (array_key_exists($iRootId, $aTree))
  443. {
  444. $aSortedRoots = $aTree[$iRootId];
  445. asort($aSortedRoots);
  446. $oP->add("<ul>\n");
  447. foreach($aSortedRoots as $id => $sName)
  448. {
  449. if ($bSelect)
  450. {
  451. $sChecked = ($aNodes[$id]->GetKey() == $currValue) ? 'checked' : '';
  452. if ($bMultiple)
  453. {
  454. $sSelect = '<input type="checkbox" value="'.$aNodes[$id]->GetKey().'" name="selectObject[]" '.$sChecked.'>&nbsp;';
  455. }
  456. else
  457. {
  458. $sSelect = '<input type="radio" value="'.$aNodes[$id]->GetKey().'" name="selectObject" '.$sChecked.'>&nbsp;';
  459. }
  460. }
  461. $oP->add('<li>'.$sSelect.$aNodes[$id]->GetHyperlink());
  462. $this->DumpNodes($oP, $id, $aTree, $aNodes, $currValue);
  463. $oP->add("</li>\n");
  464. }
  465. $oP->add("</ul>\n");
  466. }
  467. }
  468. }
  469. ?>