00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00040 class FilterForReportFormToDataProviderMetadataAdapter
00041 {
00045 protected $filter;
00046
00050 protected $structure;
00051
00055 protected $structureCount = 1;
00056
00060 protected $clauses = array();
00061
00065 protected $modelRelationsAndAttributesToReportAdapter;
00066
00070 public function __construct(FilterForReportForm $filter)
00071 {
00072 $this->filter = $filter;
00073 $this->modelRelationsAndAttributesToReportAdapter = $this->makeModelRelationsAndAttributesToReportAdapter();
00074 }
00075
00079 public function getAdaptedMetadata()
00080 {
00081 $this->resetClausesAndStructure();
00082 $this->resolveClausesAndStructure();
00083 $adaptedMetadata = array();
00084 $adaptedMetadata['clauses'] = $this->clauses;
00085 $adaptedMetadata['structure'] = $this->structure;
00086 if (count($adaptedMetadata['clauses']) > 1)
00087 {
00088 $adaptedMetadata['structure'] = '(' . $adaptedMetadata['structure'] . ')';
00089 }
00090 return $adaptedMetadata;
00091 }
00092
00093 protected function resolveValueForOperator()
00094 {
00095 if ($this->filter->getOperator() == OperatorRules::TYPE_IS_NULL ||
00096 $this->filter->getOperator() == OperatorRules::TYPE_IS_NOT_NULL ||
00097 $this->filter->getOperator() == OperatorRules::TYPE_IS_EMPTY ||
00098 $this->filter->getOperator() == OperatorRules::TYPE_IS_NOT_EMPTY)
00099 {
00100 return null;
00101 }
00102 return $this->filter->value;
00103 }
00104
00105 protected function resolveClausesAndStructure()
00106 {
00107 $attribute = $this->filter->getResolvedAttribute();
00108
00109 if ($this->modelRelationsAndAttributesToReportAdapter->isDynamicallyDerivedAttribute($attribute))
00110 {
00111 if ($this->filter->getValueElementType() == 'MixedLoggedInUserTypesAndUsers')
00112 {
00113 $this->resolveUserAttributeClauseAndStructure();
00114 }
00115 else
00116 {
00117 $this->resolveDynamicallyDerivedAttributeClauseAndStructure();
00118 }
00119 }
00120 elseif ($this->modelRelationsAndAttributesToReportAdapter instanceof
00121 ModelRelationsAndAttributesToSummableReportAdapter &&
00122 $this->modelRelationsAndAttributesToReportAdapter->isAttributeACalculatedGroupByModifier($attribute))
00123 {
00124 $this->clauses[1] = array('attributeName' => $this->getRealAttributeName(),
00125 'operatorType' => $this->filter->getOperator(),
00126 'value' => $this->resolveValueForOperator(),
00127 'modifierType' => $this->modelRelationsAndAttributesToReportAdapter->
00128 getCalculationOrModifierType($attribute));
00129 $this->structure = '1';
00130 }
00131
00132 elseif ($this->modelRelationsAndAttributesToReportAdapter->relationIsReportedAsAttribute($attribute))
00133 {
00134 $this->resolveRelationReportedAsAttributeClauseAndStructure();
00135 }
00136 else
00137 {
00138 $this->resolveNonRelationNonDerivedAttributeClauseAndStructure();
00139 }
00140 }
00141
00142 protected function resolveDynamicallyDerivedAttributeClauseAndStructure()
00143 {
00144 $this->clauses[1] = array('attributeName' => $this->getRealAttributeName(),
00145 'relatedAttributeName' => 'id',
00146 'operatorType' => $this->filter->getOperator(),
00147 'value' => $this->resolveValueForOperator());
00148 $this->structure = '1';
00149 }
00150
00151 protected function resolveRelationReportedAsAttributeClauseAndStructure()
00152 {
00153 if ($this->filter->getValueElementType() == 'MixedCurrencyValueTypes')
00154 {
00155 $this->resolveCurrencyValueAttributeClauseAndStructure();
00156 }
00157 elseif ($this->filter->getValueElementType() == 'StaticDropDownForReport')
00158 {
00159 $this->resolveDropDownVariantAttributeClauseAndStructure();
00160 }
00161 elseif ($this->filter->getValueElementType() == 'MixedLoggedInUserTypesAndUsers')
00162 {
00163 $this->resolveUserAttributeClauseAndStructure();
00164 }
00165 else
00166 {
00167
00168 $this->resolveRelatedIdAttributeClauseAndStructure();
00169 }
00170 }
00171
00172 protected function resolveNonRelationNonDerivedAttributeClauseAndStructure()
00173 {
00174 if ($this->filter->getValueElementType() == 'MixedDateTypesForReport')
00175 {
00176 $this->resolveDateAttributeClauseAndStructure();
00177 }
00178 elseif ($this->filter->getValueElementType() == 'MixedLoggedInUserTypesAndUsers')
00179 {
00180 $this->resolveUserAttributeClauseAndStructure();
00181 }
00182 elseif ($this->filter->getValueElementType() == 'MixedNumberTypes')
00183 {
00184 $this->resolveNumericAttributeClauseAndStructure();
00185 }
00186 elseif ($this->filter->getValueElementType() == 'BooleanForWizardStaticDropDown')
00187 {
00188 $this->resolveBooleanAttributeClauseAndStructure();
00189 }
00190 else
00191 {
00192 $this->resolveTextAttributeClauseAndStructure();
00193 }
00194 }
00195
00196 protected function resolveDateAttributeClauseAndStructure()
00197 {
00198 $rulesClassName = $this->getDateOrDateTimeRulesClassName();
00199 $value = array();
00200 $value['type'] = $this->filter->valueType;
00201 $value['firstDate'] = $this->filter->value;
00202 $value['secondDate'] = $this->filter->secondValue;
00203 $attributesAndRelations = 'resolveEntireMappingByRules';
00204 $rulesClassName::resolveAttributesAndRelations('notUsed__notUsed', $attributesAndRelations, $value);
00205 $count = 1;
00206 foreach ($attributesAndRelations as $attributeAndRelation)
00207 {
00208 $this->clauses[$count] = array('attributeName' => $this->getRealAttributeName(),
00209 'operatorType' => $attributeAndRelation[2],
00210 'value' => $this->resolveForValueByRules($rulesClassName,
00211 $attributeAndRelation, $value));
00212 if ($this->structure == null)
00213 {
00214 $this->structure = $count;
00215 }
00216 else
00217 {
00218 $this->structure .= ' and ' . $count;
00219 }
00220 $count++;
00221 }
00222 }
00223
00228 protected function getDateOrDateTimeRulesClassName()
00229 {
00230 $displayElementType = $this->modelRelationsAndAttributesToReportAdapter->getDisplayElementType(
00231 $this->filter->getResolvedAttribute());
00232 if ($displayElementType == 'Date')
00233 {
00234 return 'MixedDateTypesSearchFormAttributeMappingRules';
00235 }
00236 elseif ($displayElementType == 'DateTime')
00237 {
00238 return 'MixedDateTimeTypesSearchFormAttributeMappingRules';
00239 }
00240 else
00241 {
00242 throw new NotSupportedException();
00243 }
00244 }
00245
00246 protected function resolveUserAttributeClauseAndStructure()
00247 {
00248 $rulesClassName = 'MixedLoggedInUserTypesAndUsersSearchFormAttributeMappingRules';
00249 $value = array();
00250 $value['type'] = $this->filter->valueType;
00251 $value['id'] = $this->filter->value;
00252 $attributesAndRelations = 'resolveEntireMappingByRules';
00253 $rulesClassName::resolveAttributesAndRelations($this->filter->getAttributeIndexOrDerivedType(), $attributesAndRelations, $value);
00254 $count = 1;
00255 foreach ($attributesAndRelations as $attributeAndRelation)
00256 {
00257 $this->clauses[$count] = array('attributeName' => $this->getRealAttributeName(),
00258 'operatorType' => $this->filter->getOperator(),
00259 'relatedAttributeName' => 'id',
00260 'value' => $this->resolveForValueByRules($rulesClassName,
00261 $attributeAndRelation, $value));
00262 if ($this->structure == null)
00263 {
00264 $this->structure = $count;
00265 }
00266 else
00267 {
00268 $this->structure .= ' and ' . $count;
00269 }
00270 $count++;
00271 }
00272 }
00273
00281 protected function resolveForValueByRules($rulesClassName, $attributesAndRelations, $value)
00282 {
00283 if ($attributesAndRelations[3] == 'resolveValueByRules')
00284 {
00285 return $rulesClassName::resolveValueDataIntoUsableValue($value);
00286 }
00287 elseif ($attributesAndRelations[3] != null)
00288 {
00289 return $attributesAndRelations[3];
00290 }
00291 }
00292
00293 protected function resolveNumericAttributeClauseAndStructure()
00294 {
00295 if ($this->filter->getOperator() == OperatorRules::TYPE_BETWEEN)
00296 {
00297 $this->clauses[1] = array('attributeName' => $this->getRealAttributeName(),
00298 'operatorType' => OperatorRules::TYPE_GREATER_THAN_OR_EQUAL_TO,
00299 'value' => $this->resolveValueForOperator());
00300 $this->clauses[2] = array('attributeName' => $this->getRealAttributeName(),
00301 'operatorType' => OperatorRules::TYPE_LESS_THAN_OR_EQUAL_TO,
00302 'value' => $this->filter->secondValue);
00303 $this->structure = '1 and 2';
00304 }
00305 else
00306 {
00307 $this->clauses[1] = array('attributeName' => $this->getRealAttributeName(),
00308 'operatorType' => $this->filter->getOperator(),
00309 'value' => $this->resolveValueForOperator());
00310 $this->structure = '1';
00311 }
00312 }
00313
00314 protected function resolveCurrencyValueAttributeClauseAndStructure()
00315 {
00316 if ($this->filter->getOperator() == OperatorRules::TYPE_BETWEEN)
00317 {
00318 $this->clauses[1] = array('attributeName' => $this->getRealAttributeName(),
00319 'relatedAttributeName' => 'value',
00320 'operatorType' => OperatorRules::TYPE_GREATER_THAN_OR_EQUAL_TO,
00321 'value' => $this->resolveValueForOperator());
00322 $this->clauses[2] = array('attributeName' => $this->getRealAttributeName(),
00323 'relatedAttributeName' => 'value',
00324 'operatorType' => OperatorRules::TYPE_LESS_THAN_OR_EQUAL_TO,
00325 'value' => $this->filter->secondValue);
00326 $this->structure = '1 and 2';
00327 $count = 3;
00328 }
00329 else
00330 {
00331 $this->clauses[1] = array('attributeName' => $this->getRealAttributeName(),
00332 'relatedAttributeName' => 'value',
00333 'operatorType' => $this->filter->getOperator(),
00334 'value' => $this->resolveValueForOperator());
00335 $this->structure = '1';
00336 $count = 2;
00337 }
00338 if ($this->filter->currencyIdForValue != null)
00339 {
00340 $this->clauses[$count] = array('attributeName' => $this->getRealAttributeName(),
00341 'relatedModelData' => array(
00342 'attributeName' => 'currency',
00343 'relatedAttributeName' => 'id',
00344 'operatorType' => OperatorRules::TYPE_EQUALS,
00345 'value' => $this->filter->currencyIdForValue));
00346 $this->structure .= ' and ' . $count;
00347 }
00348 }
00349
00350 protected function resolveDropDownVariantAttributeClauseAndStructure()
00351 {
00352 $relationClassName = $this->modelRelationsAndAttributesToReportAdapter->getModel()->
00353 getRelationModelClassName($this->getRealAttributeName());
00354
00355 if ($relationClassName == 'MultipleValuesCustomField' ||
00356 is_subclass_of ($relationClassName, 'MultipleValuesCustomField'))
00357 {
00358 $relatedAttributeName = 'values';
00359 }
00360 else
00361 {
00362 $relatedAttributeName = 'value';
00363 }
00364 $this->clauses[1] = array('attributeName' => $this->getRealAttributeName(),
00365 'relatedAttributeName' => $relatedAttributeName,
00366 'operatorType' => $this->filter->getOperator(),
00367 'value' => $this->resolveValueForOperator());
00368
00369 $this->structure = '1';
00370 }
00371
00372 protected function resolveTextAttributeClauseAndStructure()
00373 {
00374 $this->clauses[1] = array('attributeName' => $this->getRealAttributeName(),
00375 'operatorType' => $this->filter->getOperator(),
00376 'value' => $this->resolveValueForOperator());
00377 $this->structure = '1';
00378 }
00379
00380 protected function resolveBooleanAttributeClauseAndStructure()
00381 {
00382 if ((bool)$this->resolveValueForOperator() === false)
00383 {
00384 $this->clauses[1] = array( 'attributeName' => $this->getRealAttributeName(),
00385 'operatorType' => OperatorRules::TYPE_EQUALS,
00386 'value' => '0');
00387 $this->clauses[2] = array( 'attributeName' => $this->getRealAttributeName(),
00388 'operatorType' => OperatorRules::TYPE_IS_NULL,
00389 'value' => null);
00390 $this->structure = '1 or 2';
00391 }
00392 else
00393 {
00394 $this->clauses[1] = array('attributeName' => $this->getRealAttributeName(),
00395 'operatorType' => OperatorRules::TYPE_EQUALS,
00396 'value' => true);
00397 $this->structure = '1';
00398 }
00399 }
00400
00401 protected function resolveRelatedIdAttributeClauseAndStructure()
00402 {
00403 $this->clauses[1] = array('attributeName' => $this->getRealAttributeName(),
00404 'relatedAttributeName' => 'id',
00405 'operatorType' => $this->filter->getOperator(),
00406 'value' => $this->resolveValueForOperator());
00407 $this->structure = '1';
00408 }
00409
00413 protected function getRealAttributeName()
00414 {
00415 return $this->modelRelationsAndAttributesToReportAdapter->resolveRealAttributeName(
00416 $this->filter->getResolvedAttribute());
00417 }
00418
00419 protected function resetClausesAndStructure()
00420 {
00421 $this->clauses = array();
00422 $this->structure = null;
00423 }
00424
00428 protected function makeModelRelationsAndAttributesToReportAdapter()
00429 {
00430 return ModelRelationsAndAttributesToReportAdapter::make(
00431 $this->filter->getResolvedAttributeModuleClassName(),
00432 $this->filter->getResolvedAttributeModelClassName(),
00433 $this->filter->getReportType());
00434 }
00435 }