ui.extkeywidget.class.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 static $iWidgetIndex = 0;
  65. protected $sAttCode;
  66. protected $sNameSuffix;
  67. protected $iId;
  68. protected $sTitle;
  69. protected $oAllowedValues = null;
  70. public function __construct($sAttCode, $sClass, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sNameSuffix = '', $sFieldPrefix = '', $sFormPrefix = '')
  71. {
  72. self::$iWidgetIndex++;
  73. $this->sAttCode = $sAttCode;
  74. $this->sClass = $sClass;
  75. $this->oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  76. $this->sNameSuffix = $sNameSuffix;
  77. $this->iId = $iInputId;
  78. $this->oAllowedValues = $oAllowedValues;
  79. $this->value = $value;
  80. $this->sFieldPrefix = $sFieldPrefix;
  81. $this->sTargetClass = $this->oAttDef->GetTargetClass();
  82. $this->sTitle = $sTitle;
  83. $this->sFormPrefix = $sFormPrefix;
  84. $this->bMandatory = $bMandatory;
  85. }
  86. /**
  87. * Get the HTML fragment corresponding to the linkset editing widget
  88. * @param WebPage $oP The web page used for all the output
  89. * @param Hash $aArgs Extra context arguments
  90. * @return string The HTML fragment to be inserted into the page
  91. */
  92. public function Display(WebPage $oPage, $aArgs = array(), $bSearchMode = false)
  93. {
  94. $oPage->add_linked_script('../js/extkeywidget.js');
  95. $oPage->add_linked_script('../js/forms-json-utils.js');
  96. $bCreate = (!$bSearchMode) && (!MetaModel::IsAbstract($this->sTargetClass)) && (UserRights::IsActionAllowed($this->sTargetClass, UR_ACTION_BULK_MODIFY) && $this->oAttDef->AllowTargetCreation());
  97. $sMessage = Dict::S('UI:Message:EmptyList:UseSearchForm');
  98. $sAttrFieldPrefix = ($bSearchMode) ? '' : 'attr_';
  99. $sHTMLValue = "<span style=\"white-space:nowrap\">"; // no wrap
  100. if($bSearchMode)
  101. {
  102. $sWizHelper = 'null';
  103. }
  104. else
  105. {
  106. $sWizHelper = 'oWizardHelper'.$this->sFormPrefix;
  107. }
  108. if (is_null($this->oAllowedValues))
  109. {
  110. throw new Exception('Implementation: null value for allowed values definition');
  111. }
  112. elseif ($this->oAllowedValues->Count() < $this->oAttDef->GetMaximumComboLength())
  113. {
  114. // Few choices, use a normal 'select'
  115. $sSelectMode = 'true';
  116. $sHelpText = $this->oAttDef->GetHelpOnEdition();
  117. // In case there are no valid values, the select will be empty, thus blocking the user from validating the form
  118. $sHTMLValue = "<select title=\"$sHelpText\" name=\"{$sAttrFieldPrefix}{$this->sFieldPrefix}{$this->sAttCode}{$this->sNameSuffix}\" id=\"$this->iId\">\n";
  119. if ($bSearchMode)
  120. {
  121. $sHTMLValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n";
  122. }
  123. else
  124. {
  125. $sHTMLValue .= "<option value=\"\">".Dict::S('UI:SelectOne')."</option>\n";
  126. }
  127. $this->oAllowedValues->Rewind();
  128. while($oObj = $this->oAllowedValues->Fetch())
  129. {
  130. $key = $oObj->GetKey();
  131. $display_value = $oObj->Get('friendlyname');
  132. if (($this->oAllowedValues->Count() == 1) && ($this->bMandatory == 'true') )
  133. {
  134. // When there is only once choice, select it by default
  135. $sSelected = ' selected';
  136. }
  137. else
  138. {
  139. $sSelected = ($this->value == $key) ? ' selected' : '';
  140. }
  141. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  142. }
  143. $sHTMLValue .= "</select>\n";
  144. $oPage->add_ready_script(
  145. <<<EOF
  146. oACWidget_{$this->iId} = new ExtKeyWidget('{$this->iId}', '{$this->sClass}', '{$this->sAttCode}', '{$this->sNameSuffix}', $sSelectMode, $sWizHelper);
  147. oACWidget_{$this->iId}.emptyHtml = "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>$sMessage</p></div>";
  148. $('#$this->iId').bind('update', function() { oACWidget_{$this->iId}.Update(); } );
  149. EOF
  150. );
  151. }
  152. else
  153. {
  154. // Too many choices, use an autocomplete
  155. $sSelectMode = 'false';
  156. if ($this->oAttDef->IsNull($this->value)) // Null values are displayed as ''
  157. {
  158. $sDisplayValue = '';
  159. }
  160. else
  161. {
  162. $sDisplayValue = $this->GetObjectName($this->value);
  163. }
  164. $sFormPrefix = $this->sFormPrefix;
  165. $iMinChars = $this->oAttDef->GetMinAutoCompleteChars();
  166. $iFieldSize = $this->oAttDef->GetMaxSize();
  167. // the input for the auto-complete
  168. $sHTMLValue = "<input count=\"".$this->oAllowedValues->Count()."\" type=\"text\" id=\"label_$this->iId\" size=\"30\" maxlength=\"$iFieldSize\" value=\"$sDisplayValue\"/>&nbsp;";
  169. $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;";
  170. // another hidden input to store & pass the object's Id
  171. $sHTMLValue .= "<input type=\"hidden\" id=\"$this->iId\" name=\"{$sAttrFieldPrefix}{$this->sFieldPrefix}{$this->sAttCode}{$this->sNameSuffix}\" value=\"$this->value\" />\n";
  172. // Scripts to start the autocomplete and bind some events to it
  173. $oPage->add_ready_script(
  174. <<<EOF
  175. oACWidget_{$this->iId} = new ExtKeyWidget('{$this->iId}', '{$this->sClass}', '{$this->sAttCode}', '{$this->sNameSuffix}', $sSelectMode, $sWizHelper);
  176. oACWidget_{$this->iId}.emptyHtml = "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>$sMessage</p></div>";
  177. $('#label_$this->iId').autocomplete('./ajax.render.php', { scroll:true, minChars:{$iMinChars}, formatItem:formatItem, autoFill:false, matchContains:true, keyHolder:'#{$this->iId}', extraParams:{operation:'autocomplete', sclass:'{$this->sClass}',attCode:'{$this->sAttCode}'}});
  178. $('#label_$this->iId').blur(function() { $(this).search(); } );
  179. $('#label_$this->iId').keyup(function() { if ($(this).val() == '') { $('#$this->iId').val(''); } } ); // Useful for search forms: empty value in the "label", means no value, immediatly !
  180. $('#label_$this->iId').result( function(event, data, formatted) { OnAutoComplete('{$this->iId}', event, data, formatted); } );
  181. $('#$this->iId').bind('update', function() { oACWidget_{$this->iId}.Update(); } );
  182. EOF
  183. );
  184. //$oPage->add_at_the_end($this->GetSearchDialog($oPage)); // To prevent adding forms inside the main form
  185. $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)
  186. }
  187. if ($bCreate)
  188. {
  189. $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;";
  190. $oPage->add_at_the_end('<div id="ajax_'.$this->iId.'"></div>');
  191. }
  192. $sHTMLValue .= "<span id=\"v_{$this->iId}\"></span>";
  193. $sHTMLValue .= "</span>"; // end of no wrap
  194. return $sHTMLValue;
  195. }
  196. public function GetSearchDialog(WebPage $oPage)
  197. {
  198. $sHTML = '<div class="wizContainer" style="vertical-align:top;"><div id="dc_'.$this->iId.'">';
  199. $oFilter = new DBObjectSearch($this->sTargetClass);
  200. $oSet = new CMDBObjectSet($oFilter);
  201. $oBlock = new DisplayBlock($oFilter, 'search', false);
  202. $sHTML .= $oBlock->GetDisplay($oPage, $this->iId, array('open' => true, 'currentId' => $this->iId));
  203. $sHTML .= "<form id=\"fr_{$this->iId}\" OnSubmit=\"return oACWidget_{$this->iId}.DoOk();\">\n";
  204. $sHTML .= "<div id=\"dr_{$this->iId}\" style=\"vertical-align:top;background: #fff;height:100%;overflow:auto;padding:0;border:0;\">\n";
  205. $sHTML .= "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>".Dict::S('UI:Message:EmptyList:UseSearchForm')."</p></div>\n";
  206. $sHTML .= "</div>\n";
  207. $sHTML .= "<input type=\"button\" id=\"btn_cancel_{$this->iId}\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"$('#ac_dlg_{$this->iId}').dialog('close');\">&nbsp;&nbsp;";
  208. $sHTML .= "<input type=\"button\" id=\"btn_ok_{$this->iId}\" value=\"".Dict::S('UI:Button:Ok')."\" onClick=\"oACWidget_{$this->iId}.DoOk();\">";
  209. $sHTML .= "</form>\n";
  210. $sHTML .= '</div></div>';
  211. $sDialogTitle = addslashes($this->sTitle);
  212. $oPage->add_ready_script(
  213. <<<EOF
  214. $('#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 });
  215. $('#fs_{$this->iId}').bind('submit.uiAutocomplete', oACWidget_{$this->iId}.DoSearchObjects);
  216. $('#dc_{$this->iId}').resize(oACWidget_{$this->iId}.UpdateSizes);
  217. EOF
  218. );
  219. $oPage->add($sHTML);
  220. }
  221. /**
  222. * Search for objects to be selected
  223. * @param WebPage $oP The page used for the output (usually an AjaxWebPage)
  224. * @param string $sRemoteClass Name of the "remote" class to perform the search on, must be a derived class of m_sRemoteClass
  225. * @param Array $aAlreadyLinkedIds List of IDs of objects of "remote" class already linked, to be filtered out of the search
  226. */
  227. public function SearchObjectsToSelect(WebPage $oP, $sTargetClass = '')
  228. {
  229. if (is_null($this->oAllowedValues))
  230. {
  231. throw new Exception('Implementation: null value for allowed values definition');
  232. }
  233. $oBlock = new DisplayBlock($this->oAllowedValues->GetFilter(), 'list', false);
  234. $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
  235. }
  236. /**
  237. * Get the display name of the selected object, to fill back the autocomplete
  238. */
  239. public function GetObjectName($iObjId)
  240. {
  241. $oObj = MetaModel::GetObject($this->sTargetClass, $iObjId);
  242. return $oObj->GetName();
  243. }
  244. /**
  245. * Get the form to create a new object of the 'target' class
  246. */
  247. public function GetObjectCreationForm(WebPage $oPage)
  248. {
  249. $sDialogTitle = addslashes($this->sTitle);
  250. $oPage->add('<div id="ac_create_'.$this->iId.'"><div class="wizContainer" style="vertical-align:top;"><div id="dcr_'.$this->iId.'">');
  251. $oPage->add("<h1>".MetaModel::GetClassIcon($this->sTargetClass)."&nbsp;".Dict::Format('UI:CreationTitle_Class', MetaModel::GetName($this->sTargetClass))."</h1>\n");
  252. cmdbAbstractObject::DisplayCreationForm($oPage, $this->sTargetClass, null, array(), array('formPrefix' => $this->iId, 'noRelations' => true));
  253. $oPage->add('</div></div></div>');
  254. // $oPage->add_ready_script("\$('#ac_create_$this->iId').dialog({ width: $(window).width()*0.8, height: 'auto', autoOpen: false, modal: true, title: '$sDialogTitle'});\n");
  255. $oPage->add_ready_script("\$('#ac_create_$this->iId').dialog({ width: 'auto', height: 'auto', autoOpen: false, modal: true, title: '$sDialogTitle'});\n");
  256. $oPage->add_ready_script("$('#dcr_{$this->iId} form').removeAttr('onsubmit');");
  257. $oPage->add_ready_script("$('#dcr_{$this->iId} form').bind('submit.uilinksWizard', oACWidget_{$this->iId}.DoCreateObject);");
  258. }
  259. /**
  260. * Get the form to create a new object of the 'target' class
  261. */
  262. public function DoCreateObject($oPage)
  263. {
  264. $oObj = MetaModel::NewObject($this->sTargetClass);
  265. $aErrors = $oObj->UpdateObject($this->sFormPrefix.$this->iId);
  266. if (count($aErrors) == 0)
  267. {
  268. $oMyChange = MetaModel::NewObject("CMDBChange");
  269. $oMyChange->Set("date", time());
  270. $sUserString = CMDBChange::GetCurrentUserName();
  271. $oMyChange->Set("userinfo", $sUserString);
  272. $iChangeId = $oMyChange->DBInsert();
  273. $oObj->DBInsertTracked($oMyChange);
  274. return array('name' => $oObj->GetName(), 'id' => $oObj->GetKey());
  275. }
  276. else
  277. {
  278. return array('name' => implode(' ', $aErrors), 'id' => 0);
  279. }
  280. }
  281. }
  282. ?>