ui.autocompletewidget.class.inc.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 UIAutoCompleteWidget
  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('../application/webpage.class.inc.php');
  61. require_once('../application/displayblock.class.inc.php');
  62. class UIAutoCompleteWidget
  63. {
  64. protected static $iWidgetIndex = 0;
  65. protected $sAttCode;
  66. protected $sNameSuffix;
  67. protected $iId;
  68. protected $sTitle;
  69. public function __construct($sAttCode, $sClass, $sTitle, $aAllowedValues, $value, $iInputId, $sNameSuffix = '', $sFieldPrefix = '')
  70. {
  71. self::$iWidgetIndex++;
  72. $this->sAttCode = $sAttCode;
  73. $this->sClass = $sClass;
  74. $this->oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  75. $this->sNameSuffix = $sNameSuffix;
  76. $this->iId = $iInputId;
  77. $this->aAllowedValues = $aAllowedValues;
  78. $this->value = $value;
  79. $this->sFieldPrefix = $sFieldPrefix;
  80. $this->sTargetClass = $this->oAttDef->GetTargetClass();
  81. $this->sTitle = $sTitle;
  82. }
  83. /**
  84. * Get the HTML fragment corresponding to the linkset editing widget
  85. * @param WebPage $oP The web page used for all the output
  86. * @param Hash $aArgs Extra context arguments
  87. * @return string The HTML fragment to be inserted into the page
  88. */
  89. public function Display(WebPage $oPage, $aArgs = array())
  90. {
  91. if ($this->oAttDef->IsNull($this->value)) // Null values are displayed as ''
  92. {
  93. $sDisplayValue = '';
  94. }
  95. else
  96. {
  97. $sDisplayValue = $this->GetObjectName($this->value);
  98. }
  99. $sMessage = Dict::S('UI:Message:EmptyList:UseSearchForm');
  100. $oPage->add_ready_script(
  101. <<<EOF
  102. oACWidget_{$this->iId} = new AutocompleteWidget('$this->iId', '$this->sClass', '$this->sAttCode', '$this->sNameSuffix');
  103. oACWidget_{$this->iId}.emptyHtml = "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>$sMessage</p></div>";
  104. EOF
  105. );
  106. $iMinChars = $this->oAttDef->GetMinAutoCompleteChars();
  107. // the input for the auto-complete
  108. $sHTMLValue = "<input count=\"".count($this->aAllowedValues)."\" type=\"text\" id=\"label_$this->iId\" size=\"30\" maxlength=\"$iFieldSize\" value=\"$sDisplayValue\"/>&nbsp;<a class=\"no-arrow\" href=\"javascript:oACWidget_{$this->iId}.Search();\"><img style=\"border:0;vertical-align:middle;\" src=\"../images/mini_search.gif\" /></a>&nbsp;<span id=\"v_{$this->iId}\"></span>";
  109. // another hidden input to store & pass the object's Id
  110. $sHTMLValue .= "<input type=\"hidden\" id=\"$this->iId\" name=\"attr_{$this->sFieldPrefix}{$this->sAttCode}{$this->sNameSuffix}\" value=\"$this->value\" />\n";
  111. // Scripts to start the autocomplete and bind some events to it
  112. $oPage->add_ready_script("\$('#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."'}});");
  113. $oPage->add_ready_script("\$('#label_$this->iId').blur(function() { $(this).search(); } );");
  114. $oPage->add_ready_script("\$('#label_$this->iId').result( function(event, data, formatted) { OnAutoComplete('$this->iId', event, data, formatted); } );");
  115. $oPage->add_ready_script("\$('#ac_dlg_$this->iId').dialog({ width: $(window).width()*0.8, height: $(window).height()*0.8, autoOpen: false, modal: true, title: '$this->sTitle', resizeStop: oACWidget_{$this->iId}.UpdateSizes, close: oACWidget_{$this->iId}.OnClose });\n");
  116. $oPage->add_at_the_end($this->GetSearchDialog($oPage)); // To prevent adding forms inside the main form
  117. return $sHTMLValue;
  118. }
  119. protected function GetSearchDialog(WebPage $oPage)
  120. {
  121. $sHTML = '<div id="ac_dlg_'.$this->iId.'"><div class="wizContainer" style="vertical-align:top;"><div id="dc_'.$this->iId.'">';
  122. $oFilter = new DBObjectSearch($this->sTargetClass);
  123. $oSet = new CMDBObjectSet($oFilter);
  124. $oBlock = new DisplayBlock($oFilter, 'search', false);
  125. $sHTML .= $oBlock->GetDisplay($oPage, $this->iId, array('open' => true, 'currentId' => $this->iId));
  126. $sHTML .= "<form id=\"fr_{$this->iId}\" OnSubmit=\"return oACWidget_{$this->iId}.DoOk();\">\n";
  127. $sHTML .= "<div id=\"dr_{$this->iId}\" style=\"vertical-align:top;background: #fff;height:100%;overflow:auto;padding:0;border:0;\">\n";
  128. $sHTML .= "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>".Dict::S('UI:Message:EmptyList:UseSearchForm')."</p></div>\n";
  129. $sHTML .= "</div>\n";
  130. $sHTML .= "<input type=\"button\" id=\"btn_cancel_{$this->iId}\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"$('#ac_dlg_{$this->iId}').dialog('close');\">&nbsp;&nbsp;";
  131. $sHTML .= "<input type=\"button\" id=\"btn_ok_{$this->iId}\" value=\"".Dict::S('UI:Button:Ok')."\" onClick=\"oACWidget_{$this->iId}.DoOk();\">";
  132. $sHTML .= "</div>\n";
  133. $sHTML .= "</form>\n";
  134. $sHTML .= '</div></div></div>';
  135. $oPage->add_ready_script("$('#fs_{$this->iId}').bind('submit.uilinksWizard', oACWidget_{$this->iId}.DoSearchObjects);");
  136. $oPage->add_ready_script("$('#dc_{$this->iId}').resize(oACWidget_{$this->iId}.UpdateSizes);");
  137. return $sHTML;
  138. }
  139. /**
  140. * Search for objects to be selected
  141. * @param WebPage $oP The page used for the output (usually an AjaxWebPage)
  142. * @param string $sRemoteClass Name of the "remote" class to perform the search on, must be a derived class of m_sRemoteClass
  143. * @param Array $aAlreadyLinkedIds List of IDs of objects of "remote" class already linked, to be filtered out of the search
  144. */
  145. public function SearchObjectsToSelect(WebPage $oP, $sTargetClass = '')
  146. {
  147. if ($sTargetClass != '')
  148. {
  149. // assert(MetaModel::IsParentClass($this->m_sRemoteClass, $sRemoteClass));
  150. $oFilter = new DBObjectSearch($sTargetClass);
  151. }
  152. else
  153. {
  154. // No remote class specified use the one defined in the linkedset
  155. $oFilter = new DBObjectSearch($this->sTargetClass);
  156. }
  157. $oFilter->AddCondition('id', array_keys($this->aAllowedValues), 'IN');
  158. $oSet = new CMDBObjectSet($oFilter);
  159. $oBlock = new DisplayBlock($oFilter, 'list', false);
  160. $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
  161. }
  162. /**
  163. * Get the display name of the selected object, to fill back the autocomplete
  164. */
  165. public function GetObjectName($iObjId)
  166. {
  167. $oObj = MetaModel::GetObject($this->sTargetClass, $iObjId);
  168. return $oObj->GetName();
  169. }
  170. }
  171. ?>