ui.extkeywidget.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  74. return $oWidget->Display($oPage, $iMaxComboLength, $bAllowTargetCreation, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sFieldName, $sFormPrefix, $aArgs, $bSearchMode);
  75. }
  76. public function __construct($sTargetClass, $iInputId)
  77. {
  78. $this->sTargetClass = $sTargetClass;
  79. $this->iId = $iInputId;
  80. }
  81. /**
  82. * Get the HTML fragment corresponding to the linkset editing widget
  83. * @param WebPage $oP The web page used for all the output
  84. * @param Hash $aArgs Extra context arguments
  85. * @return string The HTML fragment to be inserted into the page
  86. */
  87. public function Display(WebPage $oPage, $iMaxComboLength, $bAllowTargetCreation, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sFieldName, $sFormPrefix = '', $aArgs = array(), $bSearchMode = false)
  88. {
  89. $sTitle = addslashes($sTitle);
  90. $oPage->add_linked_script('../js/extkeywidget.js');
  91. $oPage->add_linked_script('../js/forms-json-utils.js');
  92. $bCreate = (!$bSearchMode) && (!MetaModel::IsAbstract($this->sTargetClass)) && (UserRights::IsActionAllowed($this->sTargetClass, UR_ACTION_BULK_MODIFY) && $bAllowTargetCreation);
  93. $sMessage = Dict::S('UI:Message:EmptyList:UseSearchForm');
  94. $sAttrFieldPrefix = ($bSearchMode) ? '' : 'attr_';
  95. $sHTMLValue = "<span style=\"white-space:nowrap\">"; // no wrap
  96. $sFilter = addslashes($oAllowedValues->GetFilter()->ToOQL());
  97. if($bSearchMode)
  98. {
  99. $sWizHelper = 'null';
  100. $sWizHelperJSON = "''";
  101. }
  102. else
  103. {
  104. $sWizHelper = 'oWizardHelper'.$sFormPrefix;
  105. $sWizHelperJSON = $sWizHelper.'.ToJSON()';
  106. }
  107. if (is_null($oAllowedValues))
  108. {
  109. throw new Exception('Implementation: null value for allowed values definition');
  110. }
  111. elseif ($oAllowedValues->Count() < $iMaxComboLength)
  112. {
  113. // Few choices, use a normal 'select'
  114. $sSelectMode = 'true';
  115. $sHelpText = ''; //$this->oAttDef->GetHelpOnEdition();
  116. $sHTMLValue = "<select title=\"$sHelpText\" name=\"{$sAttrFieldPrefix}{$sFieldName}\" id=\"$this->iId\">\n";
  117. if ($bSearchMode)
  118. {
  119. $sHTMLValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n";
  120. }
  121. else
  122. {
  123. $sHTMLValue .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>\n";
  124. }
  125. $oAllowedValues->Rewind();
  126. while($oObj = $oAllowedValues->Fetch())
  127. {
  128. $key = $oObj->GetKey();
  129. $display_value = $oObj->Get('friendlyname');
  130. if (($oAllowedValues->Count() == 1) && ($bMandatory == 'true') )
  131. {
  132. // When there is only once choice, select it by default
  133. $sSelected = ' selected';
  134. }
  135. else
  136. {
  137. $sSelected = ($value == $key) ? ' selected' : '';
  138. }
  139. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  140. }
  141. $sHTMLValue .= "</select>\n";
  142. $oPage->add_ready_script(
  143. <<<EOF
  144. oACWidget_{$this->iId} = new ExtKeyWidget('{$this->iId}', '{$this->sTargetClass}', '$sFilter', '$sTitle', true, $sWizHelper);
  145. oACWidget_{$this->iId}.emptyHtml = "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>$sMessage</p></div>";
  146. $('#$this->iId').bind('update', function() { oACWidget_{$this->iId}.Update(); } );
  147. EOF
  148. );
  149. }
  150. else
  151. {
  152. // Too many choices, use an autocomplete
  153. $sSelectMode = 'false';
  154. if (is_null($value) || ($value == 0)) // Null values are displayed as ''
  155. {
  156. $sDisplayValue = isset($aArgs['sDefaultValue']) ? $aArgs['sDefaultValue'] : '';
  157. }
  158. else
  159. {
  160. $sDisplayValue = $this->GetObjectName($value);
  161. }
  162. $iMinChars = isset($aArgs['iMinChars']) ? $aArgs['iMinChars'] : 3; //@@@ $this->oAttDef->GetMinAutoCompleteChars();
  163. $iFieldSize = isset($aArgs['iFieldSize']) ? $aArgs['iFieldSize'] : 30; //@@@ $this->oAttDef->GetMaxSize();
  164. $iDropDownSize = 10;
  165. // the input for the auto-complete
  166. $sHTMLValue = "<input count=\"".$oAllowedValues->Count()."\" type=\"text\" id=\"label_$this->iId\" size=\"$iFieldSize\" maxlength=\"$iDropDownSize\" value=\"$sDisplayValue\"/>&nbsp;";
  167. $sHTMLValue .= "<a class=\"no-arrow\" href=\"javascript:oACWidget_{$this->iId}.Search();\"><img id=\"mini_search_{$this->iId}\" style=\"border:0;vertical-align:middle;\" src=\"../images/mini_search.gif\" /></a>&nbsp;";
  168. // another hidden input to store & pass the object's Id
  169. $sHTMLValue .= "<input type=\"hidden\" id=\"$this->iId\" name=\"{$sAttrFieldPrefix}{$sFieldName}\" value=\"$value\" />\n";
  170. // Scripts to start the autocomplete and bind some events to it
  171. $oPage->add_ready_script(
  172. <<<EOF
  173. oACWidget_{$this->iId} = new ExtKeyWidget('{$this->iId}', '{$this->sTargetClass}', '$sFilter', '$sTitle', false, $sWizHelper);
  174. oACWidget_{$this->iId}.emptyHtml = "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>$sMessage</p></div>";
  175. $('#label_$this->iId').autocomplete('./ajax.render.php', { scroll:true, minChars:{$iMinChars}, formatItem:formatItem, autoFill:false, matchContains:true, keyHolder:'#{$this->iId}', extraParams:{operation:'ac_extkey', sTargetClass:'{$this->sTargetClass}',sFilter:'$sFilter', json: $sWizHelperJSON }});
  176. $('#label_$this->iId').blur(function() { $(this).search(); } );
  177. $('#label_$this->iId').keyup(function() { if ($(this).val() == '') { $('#$this->iId').val(''); } } ); // Useful for search forms: empty value in the "label", means no value, immediatly !
  178. $('#label_$this->iId').result( function(event, data, formatted) { OnAutoComplete('{$this->iId}', event, data, formatted); } );
  179. $('#$this->iId').bind('update', function() { oACWidget_{$this->iId}.Update(); } );
  180. if ($('#ac_dlg_{$this->iId}').length == 0)
  181. {
  182. $('body').append('<div id="ac_dlg_{$this->iId}"></div>');
  183. }
  184. EOF
  185. );
  186. //$oPage->add_at_the_end($this->GetSearchDialog($oPage)); // To prevent adding forms inside the main form
  187. //$oPage->add_at_the_end('<div id="ac_dlg_'.$this->iId.'"></div>'); // The place where to download the search dialog is outside of the main form (to prevent nested forms)
  188. }
  189. if ($bCreate)
  190. {
  191. $sHTMLValue .= "<a class=\"no-arrow\" href=\"javascript:oACWidget_{$this->iId}.CreateObject();\"><img id=\"mini_add_{$this->iId}\" style=\"border:0;vertical-align:middle;\" src=\"../images/mini_add.gif\" /></a>&nbsp;";
  192. $oPage->add_ready_script(
  193. <<<EOF
  194. if ($('#ajax_{$this->iId}').length == 0)
  195. {
  196. $('body').append('<div id="ajax_{$this->iId}"></div>');
  197. }
  198. EOF
  199. );
  200. }
  201. $sHTMLValue .= "<span id=\"v_{$this->iId}\"></span>";
  202. $sHTMLValue .= "</span>"; // end of no wrap
  203. return $sHTMLValue;
  204. }
  205. public function GetSearchDialog(WebPage $oPage, $sTitle)
  206. {
  207. $sHTML = '<div class="wizContainer" style="vertical-align:top;"><div id="dc_'.$this->iId.'">';
  208. $oFilter = new DBObjectSearch($this->sTargetClass);
  209. $oSet = new CMDBObjectSet($oFilter);
  210. $oBlock = new DisplayBlock($oFilter, 'search', false);
  211. $sHTML .= $oBlock->GetDisplay($oPage, $this->iId, array('open' => true, 'currentId' => $this->iId));
  212. $sHTML .= "<form id=\"fr_{$this->iId}\" OnSubmit=\"return oACWidget_{$this->iId}.DoOk();\">\n";
  213. $sHTML .= "<div id=\"dr_{$this->iId}\" style=\"vertical-align:top;background: #fff;height:100%;overflow:auto;padding:0;border:0;\">\n";
  214. $sHTML .= "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>".Dict::S('UI:Message:EmptyList:UseSearchForm')."</p></div>\n";
  215. $sHTML .= "</div>\n";
  216. $sHTML .= "<input type=\"button\" id=\"btn_cancel_{$this->iId}\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"$('#ac_dlg_{$this->iId}').dialog('close');\">&nbsp;&nbsp;";
  217. $sHTML .= "<input type=\"button\" id=\"btn_ok_{$this->iId}\" value=\"".Dict::S('UI:Button:Ok')."\" onClick=\"oACWidget_{$this->iId}.DoOk();\">";
  218. $sHTML .= "</form>\n";
  219. $sHTML .= '</div></div>';
  220. $sDialogTitle = addslashes($sTitle);
  221. $oPage->add_ready_script(
  222. <<<EOF
  223. $('#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 });
  224. $('#fs_{$this->iId}').bind('submit.uiAutocomplete', oACWidget_{$this->iId}.DoSearchObjects);
  225. $('#dc_{$this->iId}').resize(oACWidget_{$this->iId}.UpdateSizes);
  226. EOF
  227. );
  228. $oPage->add($sHTML);
  229. }
  230. /**
  231. * Search for objects to be selected
  232. * @param WebPage $oP The page used for the output (usually an AjaxWebPage)
  233. * @param string $sRemoteClass Name of the "remote" class to perform the search on, must be a derived class of m_sRemoteClass
  234. * @param Array $aAlreadyLinkedIds List of IDs of objects of "remote" class already linked, to be filtered out of the search
  235. */
  236. public function SearchObjectsToSelect(WebPage $oP, $sFilter, $sRemoteClass = '', $oObj = null)
  237. {
  238. if (is_null($sFilter))
  239. {
  240. throw new Exception('Implementation: null value for allowed values definition');
  241. }
  242. try
  243. {
  244. $oFilter = DBObjectSearch::FromOQL($sFilter);
  245. $oBlock = new DisplayBlock($oFilter, 'list', false);
  246. $oBlock->Display($oP, $this->iId, array('this' => $oObj, 'menu' => false, 'selection_mode' => true, 'selection_type' => 'single', 'display_limit' => false)); // Don't display the 'Actions' menu on the results
  247. }
  248. catch(MissingQueryArgument $e)
  249. {
  250. // When used in a search form the $this parameter may be missing, in this case return all possible values...
  251. // TODO check if we can improve this behavior...
  252. $sOQL = 'SELECT '.$sRemoteClass;
  253. $oFilter = DBObjectSearch::FromOQL($sOQL);
  254. $oBlock = new DisplayBlock($oFilter, 'list', false);
  255. $oBlock->Display($oP, $this->iId, array('menu' => false, 'selection_mode' => true, 'selection_type' => 'single', 'display_limit' => false)); // Don't display the 'Actions' menu on the results
  256. }
  257. }
  258. /**
  259. * Search for objects to be selected
  260. * @param WebPage $oP The page used for the output (usually an AjaxWebPage)
  261. * @param string $sFilter The OQL expression used to define/limit limit the scope of possible values
  262. * @param DBObject $oObj The current object for the OQL context
  263. * @param string $sContains The text of the autocomplete to filter the results
  264. */
  265. public function AutoComplete(WebPage $oP, $sFilter, $oObj = null, $sContains)
  266. {
  267. if (is_null($sFilter))
  268. {
  269. throw new Exception('Implementation: null value for allowed values definition');
  270. }
  271. $oValuesSet = new ValueSetObjects($sFilter);
  272. $aValues = $oValuesSet->GetValues(array('this' => $oObj), $sContains);
  273. foreach($aValues as $sKey => $sFriendlyName)
  274. {
  275. $oP->add($sFriendlyName."|".$sKey."\n");
  276. }
  277. }
  278. /**
  279. * Get the display name of the selected object, to fill back the autocomplete
  280. */
  281. public function GetObjectName($iObjId)
  282. {
  283. $oObj = MetaModel::GetObject($this->sTargetClass, $iObjId);
  284. return $oObj->GetName();
  285. }
  286. /**
  287. * Get the form to create a new object of the 'target' class
  288. */
  289. public function GetObjectCreationForm(WebPage $oPage)
  290. {
  291. $sDialogTitle = addslashes($this->sTitle);
  292. $oPage->add('<div id="ac_create_'.$this->iId.'"><div class="wizContainer" style="vertical-align:top;"><div id="dcr_'.$this->iId.'">');
  293. $oPage->add("<h1>".MetaModel::GetClassIcon($this->sTargetClass)."&nbsp;".Dict::Format('UI:CreationTitle_Class', MetaModel::GetName($this->sTargetClass))."</h1>\n");
  294. cmdbAbstractObject::DisplayCreationForm($oPage, $this->sTargetClass, null, array(), array('formPrefix' => $this->iId, 'noRelations' => true));
  295. $oPage->add('</div></div></div>');
  296. // $oPage->add_ready_script("\$('#ac_create_$this->iId').dialog({ width: $(window).width()*0.8, height: 'auto', autoOpen: false, modal: true, title: '$sDialogTitle'});\n");
  297. $oPage->add_ready_script("\$('#ac_create_$this->iId').dialog({ width: 'auto', height: 'auto', autoOpen: false, modal: true, title: '$sDialogTitle'});\n");
  298. $oPage->add_ready_script("$('#dcr_{$this->iId} form').removeAttr('onsubmit');");
  299. $oPage->add_ready_script("$('#dcr_{$this->iId} form').bind('submit.uilinksWizard', oACWidget_{$this->iId}.DoCreateObject);");
  300. }
  301. /**
  302. * Get the form to create a new object of the 'target' class
  303. */
  304. public function DoCreateObject($oPage)
  305. {
  306. $oObj = MetaModel::NewObject($this->sTargetClass);
  307. $aErrors = $oObj->UpdateObject($this->iId);
  308. if (count($aErrors) == 0)
  309. {
  310. $oMyChange = MetaModel::NewObject("CMDBChange");
  311. $oMyChange->Set("date", time());
  312. $sUserString = CMDBChange::GetCurrentUserName();
  313. $oMyChange->Set("userinfo", $sUserString);
  314. $iChangeId = $oMyChange->DBInsert();
  315. $oObj->DBInsertTracked($oMyChange);
  316. return array('name' => $oObj->GetName(), 'id' => $oObj->GetKey());
  317. }
  318. else
  319. {
  320. return array('name' => implode(' ', $aErrors), 'id' => 0);
  321. }
  322. }
  323. }
  324. ?>