From 29177ec86bbbac314cbbd7a40edf18419d80e50f Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 27 Sep 2018 14:37:50 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B0917:=20New=20AttributeQueryAttCodeSet=20?= =?UTF-8?q?and=20enhanced=20Query=20phrase=20book?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/query.class.inc.php | 2 +- core/attributedef.class.inc.php | 183 +++++++++++++++++++++++++++++++- 2 files changed, 183 insertions(+), 2 deletions(-) diff --git a/application/query.class.inc.php b/application/query.class.inc.php index cfe8f640b..917abb411 100644 --- a/application/query.class.inc.php +++ b/application/query.class.inc.php @@ -47,7 +47,7 @@ abstract class Query extends cmdbAbstractObject MetaModel::Init_AddAttribute(new AttributeString("name", array("allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array()))); MetaModel::Init_AddAttribute(new AttributeText("description", array("allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array()))); - MetaModel::Init_AddAttribute(new AttributeText("fields", array("allowed_values"=>null, "sql"=>"fields", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array()))); + MetaModel::Init_AddAttribute(new AttributeQueryAttCodeSet("fields", array("allowed_values"=>null,"max_items" => 1000, "query_field" => "oql", "sql"=>"fields", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array('oql')))); // Display lists MetaModel::Init_SetZListItems('details', array('name', 'description', 'fields')); // Attributes to be displayed for the complete details diff --git a/core/attributedef.class.inc.php b/core/attributedef.class.inc.php index 7a690f2f4..4ca44c9f1 100644 --- a/core/attributedef.class.inc.php +++ b/core/attributedef.class.inc.php @@ -9367,7 +9367,7 @@ class AttributePropertySet extends AttributeTable * * Class AttributeSet */ -class AttributeSet extends AttributeDBFieldVoid +abstract class AttributeSet extends AttributeDBFieldVoid { const SEARCH_WIDGET_TYPE = self::SEARCH_WIDGET_TYPE_SET; @@ -9856,6 +9856,187 @@ class AttributeClassAttCodeSet extends AttributeSet } } +class AttributeQueryAttCodeSet extends AttributeSet +{ + static public function ListExpectedParams() + { + return array_merge(parent::ListExpectedParams(), array('query_field')); + } + + protected function GetSQLCol($bFullSpec = false) + { + return "TEXT".CMDBSource::GetSqlStringColumnDefinition(); + } + + public function GetMaxSize() + { + return 65535; + } + + /** + * Get a class array indexed by alias + * @param $oHostObj + * + * @return array + */ + private function GetClassList($oHostObj) + { + try + { + $sQueryField = $this->Get('query_field'); + $sQuery = $oHostObj->Get($sQueryField); + if (empty($sQuery)) + { + return array(); + } + $oFilter = DBSearch::FromOQL($sQuery); + return $oFilter->GetSelectedClasses(); + + } catch (OQLException $e) + { + IssueLog::Warning($e->getMessage()); + } + return array(); + } + + public function GetAllowedValues($aArgs = array(), $sContains = '') + { + if (isset($aArgs['this'])) + { + $oHostObj = $aArgs['this']; + $aClasses = $this->GetClassList($oHostObj); + + $aAllowedAttributes = array(); + $aAllAttributes = array(); + if (is_array($aClasses)) + { + if (!empty($aClasses)) + { + ksort($aClasses); + foreach($aClasses as $sAlias => $sClass) + { + $aAttributes = MetaModel::GetAttributesList($sClass); + foreach($aAttributes as $sAttCode) + { + $aAllAttributes[] = array('alias' => $sAlias, 'class' => $sClass, 'att_code' => $sAttCode); + } + } + } + foreach($aAllAttributes as $aFullAttCode) + { + $sAttCode = $aFullAttCode['alias'].'.'.$aFullAttCode['att_code']; + $sClass = $aFullAttCode['class']; + $sLabel = $aFullAttCode['alias'].'.'.MetaModel::GetLabel($sClass, $aFullAttCode['att_code']); + $aAllowedAttributes[$sAttCode] = $sLabel; + } + } + else + { + $sClass = "$aClasses"; + $aAttributes = MetaModel::GetAttributesList($sClass); + foreach($aAttributes as $sAttCode) + { + $aAllowedAttributes[$sAttCode] = MetaModel::GetLabel($sClass, $sAttCode); + } + } + return $aAllowedAttributes; + } + + return null; + } + + /** + * force an allowed value (type conversion and possibly forces a value as mySQL would do upon writing! + * + * @param $proposedValue + * @param \DBObject $oHostObj + * + * @param bool $bIgnoreErrors + * + * @return mixed + * @throws \CoreException + * @throws \CoreUnexpectedValue + * @throws \OQLException + * @throws \Exception + */ + public function MakeRealValue($proposedValue, $oHostObj, $bIgnoreErrors = false) + { + $oSet = new ormSet(MetaModel::GetAttributeOrigin($this->GetHostClass(), $this->GetCode()), $this->GetCode(), $this->GetMaxItems()); + $aArgs = array(); + if (!empty($oHostObj)) + { + $aArgs['this'] = $oHostObj; + } + $aAllowedAttributes = $this->GetAllowedValues($aArgs); + $aInvalidAttCodes = array(); + if (is_string($proposedValue) && !empty($proposedValue)) + { + $proposedValue = trim($proposedValue); + $aValues = array(); + foreach(explode(',', $proposedValue) as $sValue) + { + $sAttCode = trim($sValue); + if (empty($aAllowedAttributes) || isset($aAllowedAttributes[$sAttCode])) + { + $aValues[$sAttCode] = $sAttCode; + } + else + { + $aInvalidAttCodes[] = $sAttCode; + } + } + $oSet->SetValues($aValues); + } + elseif ($proposedValue instanceof ormSet) + { + $oSet = $proposedValue; + } + if (!empty($aInvalidAttCodes) && !$bIgnoreErrors) + { + throw new CoreUnexpectedValue("The attribute(s) ".implode(', ', $aInvalidAttCodes)." are invalid"); + } + + return $oSet; + } + + /** + * @param $value + * @param \DBObject $oHostObject + * @param bool $bLocalize + * + * @return string|null + * + * @throws \Exception + */ + public function GetAsHTML($value, $oHostObject = null, $bLocalize = true) + { + if ($value instanceof ormSet) + { + $value = $value->GetValues(); + } + if (is_array($value)) + { + if (!empty($oHostObject) && $bLocalize) + { + $aArgs['this'] = $oHostObject; + $aAllowedAttributes = $this->GetAllowedValues($aArgs); + + $aLocalizedValues = array(); + foreach($value as $sAttCode) + { + if (isset($aAllowedAttributes[$sAttCode])) + { + $aLocalizedValues[] = $aAllowedAttributes[$sAttCode]; + } + } + $value = $aLocalizedValues; + } + return implode(', ', $value); + } + return $value; + } +} + /** * The attribute dedicated to the friendly name automatic attribute (not written) *