public function testGetUsedAttributeNames() { $mappingData = array(array('attributeName' => 'a'), array('attributeName' => 'b'), array('attributeName' => 'c')); $metadata = new DropDownDependencyDerivedAttributeMetadata(); $metadata->setScenario('nonAutoBuild'); $metadata->name = 'someName3'; $metadata->modelClassName = 'Whatever2'; $metadata->serializedMetadata = serialize(array('stuff', 1, 'attributeLabels' => array(), 'mappingData' => $mappingData)); $this->assertTrue($metadata->save()); $usedModelAttributeNames = $metadata->getUsedAttributeNames(); $this->assertEquals(array('a', 'b', 'c'), $usedModelAttributeNames); }
/** * (non-PHPdoc) * @see Element::renderLabel() */ protected function renderLabel() { $content = $this->dropDownDependencyDerivedAttributeMetadata->getLabelByLanguage(Yii::app()->language); if ($this->form === null) { return Yii::app()->format->text($content); } return ZurmoHtml::tag('label', array(), $content); }
public function testGetCustomFieldAttributesNotUsedInOtherDependencyAttributes() { $adapter = new DropDownDependencyToMappingLayoutAdapter('TestDropDownDependencyModel', null, 4); $attributeNames = $adapter->getCustomFieldAttributesNotUsedInOtherDependencyAttributes(); $availableAttributes = array('something1' => 'Something 1', 'something2' => 'Something 2', 'something3' => 'Something 3', 'something4' => 'Something 4'); $this->assertEquals($availableAttributes, $attributeNames); //Now save a drop down dependency. This means it will use up one of the attribute names. $metadata = new DropDownDependencyDerivedAttributeMetadata(); $metadata->setScenario('nonAutoBuild'); $metadata->name = 'someName'; $metadata->modelClassName = 'TestDropDownDependencyModel'; $mappingData = array(array('attributeName' => 'something1')); $metadata->serializedMetadata = serialize(array('stuff', 1, 'attributeLabels' => array(), 'mappingData' => $mappingData)); $this->assertTrue($metadata->save()); //Now requery and see that something1 is not available. $adapter = new DropDownDependencyToMappingLayoutAdapter('TestDropDownDependencyModel', null, 4); $attributeNames = $adapter->getCustomFieldAttributesNotUsedInOtherDependencyAttributes(); $availableAttributes = array('something2' => 'Something 2', 'something3' => 'Something 3', 'something4' => 'Something 4'); $this->assertEquals($availableAttributes, $attributeNames); }
public function getAttributes() { $attributes = array(); $calculatedAttributes = CalculatedDerivedAttributeMetadata::getAllByModelClassName($this->modelClassName); foreach ($calculatedAttributes as $attribute) { ModelAttributeCollectionUtil::populateCollection($attributes, $attribute->name, $attribute->getLabelByLanguage(Yii::app()->language), DerivedAttributeToMixedTypeUtil::getType($this->modelClassName, $attribute->name)); } $dropdownDependencyAttributes = DropDownDependencyDerivedAttributeMetadata::getAllByModelClassName($this->modelClassName); foreach ($dropdownDependencyAttributes as $attribute) { ModelAttributeCollectionUtil::populateCollection($attributes, $attribute->name, $attribute->getLabelByLanguage(Yii::app()->language), DerivedAttributeToMixedTypeUtil::getType($this->modelClassName, $attribute->name)); } return $attributes; }
public function setAttributeMetadataFromForm(AttributeForm $attributeForm) { assert('$attributeForm instanceof DropDownDependencyAttributeForm'); $modelClassName = get_class($this->model); $attributeName = $attributeForm->attributeName; $attributeLabels = $attributeForm->attributeLabels; $elementType = $attributeForm->getAttributeTypeName(); $mappingData = $attributeForm->mappingData; $id = $attributeForm->id; if ($id != null) { $metadata = DropDownDependencyDerivedAttributeMetadata::getById($id); $metadata->setScenario('nonAutoBuild'); } else { $metadata = new DropDownDependencyDerivedAttributeMetadata(); $metadata->setScenario('nonAutoBuild'); } $metadata->name = $attributeName; $metadata->modelClassName = $modelClassName; $metadata->serializedMetadata = serialize(array('mappingData' => $mappingData, 'attributeLabels' => $attributeLabels)); $saved = $metadata->save(); if (!$saved) { throw new NotSupportedException(); } }
/** * Returns the a type that is derived by looking at several different components of an attribute. This * includes the metadata element data, validator data, and relation information. The type returned can * be utilized by different aspects of the application where an attribute type is needed. * @return string type */ public static function getType($modelClassName, $attributeName) { assert('is_string($modelClassName) && $modelClassName != ""'); assert('is_string($attributeName) && $attributeName != ""'); try { $models = CalculatedDerivedAttributeMetadata::getByNameAndModelClassName($attributeName, $modelClassName); if (count($models) == 1) { return 'CalculatedNumber'; } } catch (NotFoundException $e) { } try { $models = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName($attributeName, $modelClassName); if (count($models) == 1) { return 'DropDownDependency'; } } catch (NotFoundException $e) { throw new NotImplementedException($attributeName . 'M' . $modelClassName); } }
/** * @depends testUpdateValueInMappingByOldAndNewValue */ public function testResolveValuesInMappingWhenValueWasRemoved() { //Remove a1 $customFieldDataData = array('a2New', 'a3', 'a4'); DropDownDependencyDerivedAttributeDesignerUtil::resolveValuesInMappingWhenValueWasRemoved('aModelClassName', 'a', $customFieldDataData); //Confirm a1 has been removed from the mapping. $metadata = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName('aName', 'aModelClassName'); $unserializedData = unserialize($metadata->serializedMetadata); $compareData = array(array('attributeName' => 'a'), array('attributeName' => 'b', 'valuesToParentValues' => array('b1' => null, 'b2' => 'a2New', 'b3New' => 'a3', 'b4' => 'a4')), array('attributeName' => 'c', 'valuesToParentValues' => array('c1New' => 'b1', 'c2New' => 'b2', 'c3' => 'b3New', 'c4' => 'b4'))); $this->assertEquals(array('a' => 'b'), $unserializedData['attributeLabels']); $this->assertEquals($compareData, $unserializedData['mappingData']); //Remove b4 $customFieldDataData = array('b1', 'b2', 'b3New'); DropDownDependencyDerivedAttributeDesignerUtil::resolveValuesInMappingWhenValueWasRemoved('aModelClassName', 'b', $customFieldDataData); //Confirm b4 has been removed from the mapping. $metadata = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName('aName', 'aModelClassName'); $unserializedData = unserialize($metadata->serializedMetadata); $compareData = array(array('attributeName' => 'a'), array('attributeName' => 'b', 'valuesToParentValues' => array('b1' => null, 'b2' => 'a2New', 'b3New' => 'a3')), array('attributeName' => 'c', 'valuesToParentValues' => array('c1New' => 'b1', 'c2New' => 'b2', 'c3' => 'b3New', 'c4' => null))); $this->assertEquals(array('a' => 'b'), $unserializedData['attributeLabels']); $this->assertEquals($compareData, $unserializedData['mappingData']); }
protected function areAllPlacedAttributesUnique() { $moduleClassName = $this->moduleClassName; $modelClassName = $moduleClassName::getPrimaryModelName(); foreach ($this->placedDirectAttributeNamesAsElements as $attributeName) { if (isset($this->placeableLayoutAttributes[$attributeName])) { $elementType = $this->placeableLayoutAttributes[$attributeName]['elementType']; if ($elementType == 'DropDownDependency') { $metadata = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName($attributeName, $modelClassName); $usedAttributeNames = $metadata->getUsedAttributeNames(); foreach ($usedAttributeNames as $usedAttribute) { if (in_array($usedAttribute, $this->placedDirectAttributeNamesAsElements)) { $this->message = Zurmo::t('DesignerModule', 'All fields placed must be unique. Two of the placed ' . 'fields: {field1Label} and {field2Label} contain the same fields.', array('{field1Label}' => $this->placeableLayoutAttributes[$attributeName]['attributeLabel'], '{field2Label}' => $this->placeableLayoutAttributes[$usedAttribute]['attributeLabel'])); return false; } } } } else { //Do nothing. This means a real attribute is only placable as part of a derived attribute. } } return true; }
/** * Given an array of customFieldData's data, resolve if any data has been removed and is currently mapped. If it * is mapped remove it. * @param string $modelClassName * @param string $attributeName * @param array $customFieldDataData */ public static function resolveValuesInMappingWhenValueWasRemoved($modelClassName, $attributeName, $customFieldDataData) { assert('is_string($modelClassName)'); assert('is_string($attributeName)'); assert('is_array($customFieldDataData)'); $attributesMetadata = DropDownDependencyDerivedAttributeMetadata::getAllByModelClassName($modelClassName); if (count($attributesMetadata) > 0) { foreach ($attributesMetadata as $metadata) { $saveMetadata = false; $parentPosition = null; if ($metadata->serializedMetadata != null) { $unserializedMetadata = unserialize($metadata->serializedMetadata); if (isset($unserializedMetadata['mappingData'])) { foreach ($unserializedMetadata['mappingData'] as $position => $data) { if ($data['attributeName'] == $attributeName) { $parentPosition = $position; if (isset($data['valuesToParentValues'])) { foreach ($data['valuesToParentValues'] as $value => $parentValue) { if (!in_array($value, $customFieldDataData)) { unset($unserializedMetadata['mappingData'][$position]['valuesToParentValues'][$value]); $saveMetadata = true; } } } } } if ($parentPosition !== null) { $nextPosition = $parentPosition + 1; if (isset($unserializedMetadata['mappingData'][$nextPosition]) && isset($unserializedMetadata['mappingData'][$nextPosition]['valuesToParentValues'])) { foreach ($unserializedMetadata['mappingData'][$nextPosition]['valuesToParentValues'] as $value => $parentValue) { if (!in_array($parentValue, $customFieldDataData)) { $unserializedMetadata['mappingData'][$nextPosition]['valuesToParentValues'][$value] = null; $saveMetadata = true; } } } } } } if ($saveMetadata) { $metadata->serializedMetadata = serialize($unserializedMetadata); $saved = $metadata->save(); if (!$saved) { throw new NotSupportedException(); } } } } }
/** * @depends testSetMetadataFromLayoutWithAndWithOutRequiredDerivedField */ public function testSetMetadataFromLayoutWithAndWithOutOnlyUniqueFields() { Yii::app()->user->userModel = User::getByUsername('super'); //First create a dependency $mappingData = array(array('attributeName' => 'type'), array('attributeName' => 'officePhone', 'valuesToParentValues' => array('b1' => 'a1', 'b2' => 'a2', 'b3' => 'a3', 'b4' => 'a4'))); $metadata = new DropDownDependencyDerivedAttributeMetadata(); $metadata->setScenario('nonAutoBuild'); $metadata->name = 'aName'; $metadata->modelClassName = 'Account'; $metadata->serializedMetadata = serialize(array('attributeLabels' => array('a' => 'b'), 'mappingData' => $mappingData)); $this->assertTrue($metadata->save()); $layout = array('panels' => array(array('rows' => array(array('cells' => array(array('element' => 'aName'))), array('cells' => array(array('element' => 'type'))), array('cells' => array(array('element' => 'officeFax'))), array('cells' => array(array('element' => 'employees'))))))); $model = new Account(); $editableMetadata = AccountEditAndDetailsView::getMetadata(); $modelAttributesAdapter = new ModelAttributesAdapter($model); $attributesLayoutAdapter = AttributesLayoutAdapterUtil::makeAttributesLayoutAdapter($modelAttributesAdapter->getAttributes(), new EditAndDetailsViewDesignerRules(), $editableMetadata); $adapter = new LayoutMetadataAdapter('AccountEditAndDetailsView', 'AccountsModule', $editableMetadata, new EditAndDetailsViewDesignerRules(), $attributesLayoutAdapter->getPlaceableLayoutAttributes(), $attributesLayoutAdapter->getRequiredDerivedLayoutAttributeTypes()); $x = $adapter->setMetadataFromLayout($layout, array()); $this->assertFalse($adapter->setMetadataFromLayout($layout, array())); $this->assertEquals($adapter->getMessage(), 'All required fields must be placed in this layout.'); }
/** * Public for testing only. */ public function getCustomFieldAttributesNotUsedInOtherDependencyAttributes() { $modelClassName = $this->modelClassName; $attributeNames = CustomFieldUtil::getCustomFieldAttributeNames($modelClassName); $dropDownDependencyModels = DropDownDependencyDerivedAttributeMetadata::getAllByModelClassName($this->modelClassName); foreach ($dropDownDependencyModels as $dropDownDependency) { if ($dropDownDependency->name != $this->attributeName) { $usedAttributeNames = $dropDownDependency->getUsedAttributeNames(); foreach ($usedAttributeNames as $usedAttributeName) { if (in_array($usedAttributeName, $attributeNames)) { $key = array_search($usedAttributeName, $attributeNames); unset($attributeNames[$key]); } } } } $attributeNamesAndLabels = array(); foreach ($attributeNames as $attributeName) { $attributeNamesAndLabels[$attributeName] = $modelClassName::getAnAttributeLabel($attributeName); } return $attributeNamesAndLabels; }
/** * (non-PHPdoc) * @see AttributeForm::validateAttributeNameDoesNotExists() */ public function validateAttributeNameDoesNotExists() { assert('$this->modelClassName != null'); try { $models = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName($this->attributeName, $this->modelClassName); $this->addError('attributeName', Zurmo::t('DesignerModule', 'A field with this name is already used.')); } catch (NotFoundException $e) { } }
/** * @depends testUsingTheAdapterAsAWrapperToUpdateValueInMappingByOldAndNewValue */ public function testUsingTheAdapterAsAWrapperToResolveValuesInMappingWhenValueWasRemoved() { Yii::app()->user->userModel = User::getByUsername('super'); $account = new Account(); //Remove a1 $attributeForm = AttributesFormFactory::createAttributeFormByAttributeName($account, 'aaaCstm'); $attributeForm->customFieldDataData = array('a2New', 'a3', 'a4'); $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData(); $adapter = new $modelAttributesAdapterClassName($account); try { $adapter->setAttributeMetadataFromForm($attributeForm); } catch (FailedDatabaseSchemaChangeException $e) { echo $e->getMessage(); $this->fail(); } //Confirm a1 has been removed from the mapping. $metadata = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName('aName', 'Account'); $unserializedData = unserialize($metadata->serializedMetadata); $compareData = array(array('attributeName' => 'aaaCstm'), array('attributeName' => 'bbbCstm', 'valuesToParentValues' => array('b1' => null, 'b2' => 'a2New', 'b3New' => 'a3', 'b4' => 'a4')), array('attributeName' => 'cccCstm', 'valuesToParentValues' => array('c1New' => 'b1', 'c2New' => 'b2', 'c3' => 'b3New', 'c4' => 'b4'))); $this->assertEquals(array('a' => 'b'), $unserializedData['attributeLabels']); $this->assertEquals($compareData, $unserializedData['mappingData']); //Remove b4 $attributeForm = AttributesFormFactory::createAttributeFormByAttributeName($account, 'bbbCstm'); $attributeForm->customFieldDataData = array('b1', 'b2', 'b3New'); $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData(); $adapter = new $modelAttributesAdapterClassName($account); try { $adapter->setAttributeMetadataFromForm($attributeForm); } catch (FailedDatabaseSchemaChangeException $e) { echo $e->getMessage(); $this->fail(); } //Confirm b4 has been removed from the mapping. $metadata = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName('aName', 'Account'); $unserializedData = unserialize($metadata->serializedMetadata); $compareData = array(array('attributeName' => 'aaaCstm'), array('attributeName' => 'bbbCstm', 'valuesToParentValues' => array('b1' => null, 'b2' => 'a2New', 'b3New' => 'a3')), array('attributeName' => 'cccCstm', 'valuesToParentValues' => array('c1New' => 'b1', 'c2New' => 'b2', 'c3' => 'b3New', 'c4' => null))); $this->assertEquals(array('a' => 'b'), $unserializedData['attributeLabels']); $this->assertEquals($compareData, $unserializedData['mappingData']); }