public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array()) { static $iSearchFormId = 0; $bMultiSelect = false; $oAppContext = new ApplicationContext(); $sHtml = ''; $numCols = 4; $sClassName = $oSet->GetFilter()->GetClass(); // Romain: temporarily removed the tab "OQL query" because it was not finalized // (especially when used to add a link) /* $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul> <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">".Dict::S('UI:OQLQueryTab')."</a></li> <li><a class=\"selected\" href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">".Dict::S('UI:SimpleSearchTab')."</a></li> </ul></div>\n"; */ // Simple search form if (isset($aExtraParams['currentId'])) { $sSearchFormId = $aExtraParams['currentId']; } else { $iSearchFormId = $oPage->GetUniqueId(); $sSearchFormId = 'SimpleSearchForm' . $iSearchFormId; $sHtml .= "<div id=\"ds_{$sSearchFormId}\" class=\"mini_tab{$iSearchFormId}\">\n"; } // Check if the current class has some sub-classes if (isset($aExtraParams['baseClass'])) { $sRootClass = $aExtraParams['baseClass']; } else { $sRootClass = $sClassName; } $aSubClasses = MetaModel::GetSubclasses($sRootClass); if (count($aSubClasses) > 0) { $aOptions = array(); $aOptions[MetaModel::GetName($sRootClass)] = "<option value=\"{$sRootClass}\">" . MetaModel::GetName($sRootClass) . "</options>\n"; foreach ($aSubClasses as $sSubclassName) { $aOptions[MetaModel::GetName($sSubclassName)] = "<option value=\"{$sSubclassName}\">" . MetaModel::GetName($sSubclassName) . "</options>\n"; } $aOptions[MetaModel::GetName($sClassName)] = "<option selected value=\"{$sClassName}\">" . MetaModel::GetName($sClassName) . "</options>\n"; ksort($aOptions); $sContext = $oAppContext->GetForLink(); $sClassesCombo = "<select name=\"class\" onChange=\"ReloadSearchForm('{$sSearchFormId}', this.value, '{$sRootClass}', '{$sContext}')\">\n" . implode('', $aOptions) . "</select>\n"; } else { $sClassesCombo = MetaModel::GetName($sClassName); } $oUnlimitedFilter = new DBObjectSearch($sClassName); $sAction = isset($aExtraParams['action']) ? $aExtraParams['action'] : utils::GetAbsoluteUrlAppRoot() . 'pages/UI.php'; $sHtml .= "<form id=\"fs_{$sSearchFormId}\" action=\"{$sAction}\">\n"; // Don't use $_SERVER['SCRIPT_NAME'] since the form may be called asynchronously (from ajax.php) $sHtml .= "<h2>" . Dict::Format('UI:SearchFor_Class_Objects', $sClassesCombo) . "</h2>\n"; $index = 0; $sHtml .= "<p>\n"; $aMapCriteria = array(); $aList = MetaModel::GetZListItems($sClassName, 'standard_search'); $aConsts = $oSet->ListConstantFields(); // Some fields are constants based on the query/context $sClassAlias = $oSet->GetFilter()->GetClassAlias(); foreach ($aList as $sFilterCode) { //$oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice $sHtml .= '<span style="white-space: nowrap;padding:5px;display:inline-block;">'; $sFilterValue = isset($aConsts[$sClassAlias][$sFilterCode]) ? $aConsts[$sClassAlias][$sFilterCode] : ''; $sFilterValue = utils::ReadParam($sFilterCode, $sFilterValue, false, 'raw_data'); $sFilterOpCode = null; // Use the default 'loose' OpCode if (empty($sFilterValue)) { if (isset($aMapCriteria[$sFilterCode])) { if (count($aMapCriteria[$sFilterCode]) > 1) { $sFilterValue = Dict::S('UI:SearchValue:Mixed'); } else { $sFilterValue = $aMapCriteria[$sFilterCode][0]['value']; $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode']; } // Todo: Investigate... if ($sFilterCode != 'company') { $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode); } } } $oAttDef = MetaModel::GetAttributeDef($sClassName, $sFilterCode); if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE)) { $oKeyAttDef = $oAttDef->GetFinalAttDef(); $sKeyAttClass = $oKeyAttDef->GetHostClass(); $sKeyAttCode = $oKeyAttDef->GetCode(); $sTargetClass = $oKeyAttDef->GetTargetClass(); $oSearch = new DBObjectSearch($sTargetClass); $oSearch->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', true); $oAllowedValues = new DBObjectSet($oSearch); $iFieldSize = $oKeyAttDef->GetMaxSize(); $iMaxComboLength = $oKeyAttDef->GetMaximumComboLength(); $sHtml .= "<label>" . MetaModel::GetFilterLabel($sKeyAttClass, $sKeyAttCode) . ":</label> "; $aExtKeyParams = $aExtraParams; $aExtKeyParams['iFieldSize'] = $oKeyAttDef->GetMaxSize(); $aExtKeyParams['iMinChars'] = $oKeyAttDef->GetMinAutoCompleteChars(); $sHtml .= UIExtKeyWidget::DisplayFromAttCode($oPage, $sKeyAttCode, $sKeyAttClass, $oAttDef->GetLabel(), $oAllowedValues, $sFilterValue, $sSearchFormId . 'search_' . $sFilterCode, false, $sFilterCode, '', $aExtKeyParams, true); } else { $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams); if (is_null($aAllowedValues)) { // Any value is possible, display an input box $sHtml .= "<label>" . MetaModel::GetFilterLabel($sClassName, $sFilterCode) . ":</label> <input class=\"textSearch\" name=\"{$sFilterCode}\" value=\"" . htmlentities($sFilterValue, ENT_QUOTES, 'utf-8') . "\"/>\n"; } else { //Enum field, display a multi-select combo $sValue = "<select class=\"multiselect\" size=\"1\" name=\"{$sFilterCode}[]\" multiple>\n"; $bMultiSelect = true; //$sValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n"; asort($aAllowedValues); foreach ($aAllowedValues as $key => $value) { if (is_array($sFilterValue) && in_array($key, $sFilterValue)) { $sSelected = ' selected'; } else { if ($sFilterValue == $key) { $sSelected = ' selected'; } else { $sSelected = ''; } } $sValue .= "<option value=\"{$key}\"{$sSelected}>{$value}</option>\n"; } $sValue .= "</select>\n"; $sHtml .= "<label>" . MetaModel::GetFilterLabel($sClassName, $sFilterCode) . ":</label> {$sValue}\n"; } } unset($aExtraParams[$sFilterCode]); // Finally, add a tooltip if one is defined for this attribute definition $sTip = $oAttDef->GetHelpOnSmartSearch(); if (strlen($sTip) > 0) { $sTip = addslashes($sTip); $sTip = str_replace(array("\n", "\r"), " ", $sTip); // :input does represent in form visible input (INPUT, SELECT, TEXTAREA) $oPage->add_ready_script("\$('form#fs_{$sSearchFormId} :input[name={$sFilterCode}]').qtip( { content: '{$sTip}', show: 'mouseover', hide: 'mouseout', style: { name: 'dark', tip: 'leftTop' }, position: { corner: { target: 'rightMiddle', tooltip: 'leftTop' }} } );"); } $index++; $sHtml .= '</span> '; } $sHtml .= "</p>\n"; $sHtml .= "<p align=\"right\"><input type=\"submit\" value=\"" . Dict::S('UI:Button:Search') . "\"></p>\n"; if (isset($aExtraParams['table_id'])) { // Rename to avoid collisions... $aExtraParams['_table_id_'] = $aExtraParams['table_id']; unset($aExtraParams['table_id']); } foreach ($aExtraParams as $sName => $sValue) { if (is_scalar($sValue)) { $sHtml .= "<input type=\"hidden\" name=\"{$sName}\" value=\"" . htmlentities($sValue, ENT_QUOTES, 'UTF-8') . "\" />\n"; } } $sHtml .= "<input type=\"hidden\" name=\"class\" value=\"{$sClassName}\" />\n"; $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n"; $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n"; $sHtml .= $oAppContext->GetForForm(); $sHtml .= "</form>\n"; if (!isset($aExtraParams['currentId'])) { $sHtml .= "</div><!-- Simple search form -->\n"; } if ($bMultiSelect) { $aOptions = array('header' => true, 'checkAllText' => Dict::S('UI:SearchValue:CheckAll'), 'uncheckAllText' => Dict::S('UI:SearchValue:UncheckAll'), 'noneSelectedText' => Dict::S('UI:SearchValue:Any'), 'selectedText' => Dict::S('UI:SearchValue:NbSelected'), 'selectedList' => 1); $sJSOptions = json_encode($aOptions); $oPage->add_ready_script("\$('.multiselect').multiselect({$sJSOptions});"); } /* // OQL query builder $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n"; $sHtml .= "<h1>".Dict::S('UI:OQLQueryBuilderTitle')."</h1>\n"; $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n"; $sHtml .= "<td style=\"text-align:right\"><label>SELECT </label><select name=\"oql_class\">"; $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL); $sSelectedClass = utils::ReadParam('oql_class', $sClassName, false, 'class'); $sOQLClause = utils::ReadParam('oql_clause', '', false, 'raw_data'); asort($aClasses); foreach($aClasses as $sChildClass) { $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : ''; $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n"; } $sHtml .= "</select> </td><td>\n"; $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n"; $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Query')."\"></td></tr>\n"; $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n"; foreach($aExtraParams as $sName => $sValue) { if (is_scalar($sValue)) { $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n"; } } $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n"; $sHtml .= $oAppContext->GetForForm(); $sHtml .= "</table></form>\n"; $sHtml .= "</div><!-- OQL query form -->\n"; */ return $sHtml; }
public function GetSiloSelectionForm() { // List of visible Organizations $iCount = 0; $oSet = null; if (MetaModel::IsValidClass('Organization')) { // Display the list of *favorite* organizations... but keeping in mind what is the real number of organizations $aFavoriteOrgs = appUserPreferences::GetPref('favorite_orgs', null); $oSearchFilter = new DBObjectSearch('Organization'); $oSearchFilter->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', true); $oSet = new CMDBObjectSet($oSearchFilter); $iCount = $oSet->Count(); // total number of existing Orgs // Now get the list of Orgs to be displayed in the menu $oSearchFilter = DBObjectSearch::FromOQL(ApplicationMenu::GetFavoriteSiloQuery()); $oSearchFilter->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', true); if (!empty($aFavoriteOrgs)) { $oSearchFilter->AddCondition('id', $aFavoriteOrgs, 'IN'); } $oSet = new CMDBObjectSet($oSearchFilter); // List of favorite orgs } switch ($iCount) { case 0: // No such dimension/silo => nothing to select $sHtml = '<div id="SiloSelection"><!-- nothing to select --></div>'; break; case 1: // Only one possible choice... no selection, but display the value $oOrg = $oSet->Fetch(); $sHtml = '<div id="SiloSelection">' . $oOrg->GetName() . '</div>'; $sHtml .= ''; break; default: $sHtml = ''; $oAppContext = new ApplicationContext(); $iCurrentOrganization = $oAppContext->GetCurrentValue('org_id'); $sHtml = '<div id="SiloSelection">'; $sHtml .= '<form style="display:inline" action="' . utils::GetAbsoluteUrlAppRoot() . 'pages/UI.php">'; //<select class="org_combo" name="c[org_id]" title="Pick an organization" onChange="this.form.submit();">'; $sFavoriteOrgs = ''; $oWidget = new UIExtKeyWidget('Organization', 'org_id', '', true); $sHtml .= $oWidget->Display($this, 50, false, '', $oSet, $iCurrentOrganization, 'org_id', false, 'c[org_id]', '', array('iFieldSize' => 20, 'iMinChars' => MetaModel::GetConfig()->Get('min_autocomplete_chars'), 'sDefaultValue' => Dict::S('UI:AllOrganizations')), null, 'select', false); $this->add_ready_script('$("#org_id").bind("extkeychange", function() { $("#SiloSelection form").submit(); } )'); $this->add_ready_script("\$('#label_org_id').click( function() { \$(this).val(''); \$('#org_id').val(''); return true; } );\n"); // Add other dimensions/context information to this form $oAppContext->Reset('org_id'); // org_id is handled above and we want to be able to change it here ! $oAppContext->Reset('menu'); // don't pass the menu, since a menu may expect more parameters $sHtml .= $oAppContext->GetForForm(); // Pass what remains, if anything... $sHtml .= '</form>'; $sHtml .= '</div>'; } return $sHtml; }
public static function MakeSingleRow($sClass, $iKey, $bMustBeFound = true, $bAllowAllData = false, $aModifierProperties = null) { // Build the query cache signature // $sQuerySign = $sClass; if ($bAllowAllData) { $sQuerySign .= '_all_'; } if (count($aModifierProperties)) { array_multisort($aModifierProperties); $sModifierProperties = json_encode($aModifierProperties); $sQuerySign .= '_all_' . md5($sModifierProperties); } if (!array_key_exists($sQuerySign, self::$aQueryCacheGetObject)) { // NOTE: Quick and VERY dirty caching mechanism which relies on // the fact that the string '987654321' will never appear in the // standard query // This could be simplified a little, relying solely on the query cache, // but this would slow down -by how much time?- the application $oFilter = new DBObjectSearch($sClass); $oFilter->AddCondition('id', 987654321, '='); if ($aModifierProperties) { foreach ($aModifierProperties as $sPluginClass => $aProperties) { foreach ($aProperties as $sProperty => $value) { $oFilter->SetModifierProperty($sPluginClass, $sProperty, $value); } } } if ($bAllowAllData) { $oFilter->AllowAllData(); } $sSQL = $oFilter->MakeSelectQuery(); self::$aQueryCacheGetObject[$sQuerySign] = $sSQL; self::$aQueryCacheGetObjectHits[$sQuerySign] = 0; } else { $sSQL = self::$aQueryCacheGetObject[$sQuerySign]; self::$aQueryCacheGetObjectHits[$sQuerySign] += 1; // echo " -load $sClass/$iKey- ".self::$aQueryCacheGetObjectHits[$sQuerySign]."<br/>\n"; } $sSQL = str_replace(CMDBSource::Quote(987654321), CMDBSource::Quote($iKey), $sSQL); $res = CMDBSource::Query($sSQL); $aRow = CMDBSource::FetchArray($res); CMDBSource::FreeResult($res); if ($bMustBeFound && empty($aRow)) { throw new CoreException("No result for the single row query: '{$sSQL}'"); } return $aRow; }
public function GetSearchDialog(WebPage $oPage, $sTitle, $oCurrObject = null) { $sHTML = '<div class="wizContainer" style="vertical-align:top;"><div id="dc_' . $this->iId . '">'; if ($oCurrObject != null && $this->sAttCode != '') { $oAttDef = MetaModel::GetAttributeDef(get_class($oCurrObject), $this->sAttCode); $aArgs = array('this' => $oCurrObject); $aParams = array('query_params' => $aArgs); $oSet = $oAttDef->GetAllowedValuesAsObjectSet($aArgs); $oFilter = $oSet->GetFilter(); } else { $aParams = array(); $oFilter = new DBObjectSearch($this->sTargetClass); } $oFilter->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', $this->bSearchMode); $oBlock = new DisplayBlock($oFilter, 'search', false, $aParams); $sHTML .= $oBlock->GetDisplay($oPage, $this->iId, array('open' => true, 'currentId' => $this->iId)); $sHTML .= "<form id=\"fr_{$this->iId}\" OnSubmit=\"return oACWidget_{$this->iId}.DoOk();\">\n"; $sHTML .= "<div id=\"dr_{$this->iId}\" style=\"vertical-align:top;background: #fff;height:100%;overflow:auto;padding:0;border:0;\">\n"; $sHTML .= "<div style=\"background: #fff; border:0; text-align:center; vertical-align:middle;\"><p>" . Dict::S('UI:Message:EmptyList:UseSearchForm') . "</p></div>\n"; $sHTML .= "</div>\n"; $sHTML .= "<input type=\"button\" id=\"btn_cancel_{$this->iId}\" value=\"" . Dict::S('UI:Button:Cancel') . "\" onClick=\"\$('#ac_dlg_{$this->iId}').dialog('close');\"> "; $sHTML .= "<input type=\"button\" id=\"btn_ok_{$this->iId}\" value=\"" . Dict::S('UI:Button:Ok') . "\" onClick=\"oACWidget_{$this->iId}.DoOk();\">"; $sHTML .= "<input type=\"hidden\" id=\"count_{$this->iId}\" value=\"0\">"; $sHTML .= "</form>\n"; $sHTML .= '</div></div>'; $sDialogTitle = addslashes($sTitle); $oPage->add_ready_script(<<<EOF \t\t\$('#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 }); \t\t\$('#fs_{$this->iId}').bind('submit.uiAutocomplete', oACWidget_{$this->iId}.DoSearchObjects); \t\t\$('#dc_{$this->iId}').resize(oACWidget_{$this->iId}.UpdateSizes); EOF ); $oPage->add($sHTML); }
/** * Read the context directly in the PHP parameters (either POST or GET) * return nothing */ protected function ReadContext() { if (!isset(self::$aDefaultValues)) { self::$aDefaultValues = array(); $aContext = utils::ReadParam('c', array(), false, 'context_param'); foreach ($this->aNames as $sName) { $sValue = isset($aContext[$sName]) ? $aContext[$sName] : ''; // TO DO: check if some of the context parameters are mandatory (or have default values) if (!empty($sValue)) { self::$aDefaultValues[$sName] = $sValue; } // Hmm, there must be a better (more generic) way to handle the case below: // When there is only one possible (allowed) organization, the context must be // fixed to this org if ($sName == 'org_id') { if (MetaModel::IsValidClass('Organization')) { $oSearchFilter = new DBObjectSearch('Organization'); $oSearchFilter->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', true); $oSet = new CMDBObjectSet($oSearchFilter); $iCount = $oSet->Count(); if ($iCount == 1) { // Only one possible value for org_id, set it in the context $oOrg = $oSet->Fetch(); self::$aDefaultValues[$sName] = $oOrg->GetKey(); } } } } } $this->aValues = self::$aDefaultValues; }