public function RenderContent(WebPage $oPage, $aExtraParams = array()) { if (!isset($aExtraParams['currentId'])) { $sId = $oPage->GetUniqueId(); // Works only if the page is not an Ajax one ! } else { $sId = $aExtraParams['currentId']; } $oPage->add($this->GetRenderContent($oPage, $aExtraParams, $sId)); }
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 RenderContent(WebPage $oPage, $aExtraParams = array()) { if (empty($aExtraParams['currentId'])) { $sId = 'sqlblock_' . $oPage->GetUniqueId(); // Works only if the page is not an Ajax one ! } else { $sId = $aExtraParams['currentId']; } // $oPage->add($this->GetRenderContent($oPage, $aExtraParams, $sId)); $sQuery = $this->BuildQuery(); $res = CMDBSource::Query($sQuery); $aQueryCols = CMDBSource::GetColumns($res); // Prepare column definitions (check + give default values) // foreach ($this->m_aColumns as $sName => $aColumnData) { if (!in_array($sName, $aQueryCols)) { throw new Exception("Unknown column name '{$sName}' in sqlblock column"); } if (!isset($aColumnData['label'])) { $this->m_aColumns[$sName]['label'] = $sName; } if (isset($aColumnData['drilldown']) && !empty($aColumnData['drilldown'])) { // Check if the OQL is valid try { $this->m_aColumns[$sName]['filter'] = DBObjectSearch::FromOQL($aColumnData['drilldown']); } catch (OQLException $e) { unset($aColumnData['drilldown']); } } } if (strlen($this->m_sTitle) > 0) { $oPage->add("<h2>" . Dict::S($this->m_sTitle) . "</h2>\n"); } switch ($this->m_sType) { case 'bars': case 'pie': $aColNames = array_keys($this->m_aColumns); $sXColName = $aColNames[0]; $sYColName = $aColNames[1]; $aData = array(); $aRows = array(); while ($aRow = CMDBSource::FetchArray($res)) { $aData[$aRow[$sXColName]] = $aRow[$sYColName]; $aRows[$aRow[$sXColName]] = $aRow; } $this->RenderChart($oPage, $sId, $aData, $this->m_aColumns[$sYColName]['drilldown'], $aRows); break; default: case 'table': $oAppContext = new ApplicationContext(); $sContext = $oAppContext->GetForLink(); if (!empty($sContext)) { $sContext = '&' . $sContext; } $aDisplayConfig = array(); foreach ($this->m_aColumns as $sName => $aColumnData) { $aDisplayConfig[$sName] = array('label' => $aColumnData['label'], 'description' => ''); } $aDisplayData = array(); while ($aRow = CMDBSource::FetchArray($res)) { $aSQLColNames = array_keys($aRow); $aDisplayRow = array(); foreach ($this->m_aColumns as $sName => $aColumnData) { if (isset($aColumnData['filter'])) { $sFilter = $aColumnData['drilldown']; $sClass = $aColumnData['filter']->GetClass(); $sFilter = str_replace('SELECT ' . $sClass, '', $sFilter); foreach ($aSQLColNames as $sColName) { $sFilter = str_replace(':' . $sColName, "'" . addslashes($aRow[$sColName]) . "'", $sFilter); } $sURL = utils::GetAbsoluteUrlAppRoot() . 'pages/UI.php?operation=search_oql&search_form=0&oql_class=' . $sClass . '&oql_clause=' . urlencode($sFilter) . '&format=html' . $sContext; $aDisplayRow[$sName] = '<a href="' . $sURL . '">' . $aRow[$sName] . "</a>"; } else { $aDisplayRow[$sName] = $aRow[$sName]; } } $aDisplayData[] = $aDisplayRow; } $oPage->table($aDisplayConfig, $aDisplayData); break; } }