public function setAttributeMetadataFromForm(AttributeForm $attributeForm)
 {
     assert('$attributeForm instanceof DropDownAttributeForm');
     $modelClassName = get_class($this->model);
     $attributeName = $attributeForm->attributeName;
     $attributeLabels = $attributeForm->attributeLabels;
     $defaultValueOrder = $attributeForm->defaultValueOrder;
     $elementType = $attributeForm->getAttributeTypeName();
     $partialTypeRule = $attributeForm->getModelAttributePartialRule();
     $isRequired = (bool) $attributeForm->isRequired;
     $isAudited = (bool) $attributeForm->isAudited;
     $customFieldDataName = $attributeForm->customFieldDataName;
     if ($customFieldDataName == null) {
         $customFieldDataName = ucfirst(strtolower($attributeForm->attributeName));
         //should we do something else instead?
     }
     $customFieldDataData = $attributeForm->customFieldDataData;
     $customFieldDataLabels = $attributeForm->customFieldDataLabels;
     $defaultValue = DropDownDefaultValueOrderUtil::getDefaultValueFromDefaultValueOrder($defaultValueOrder, $customFieldDataData);
     ModelMetadataUtil::addOrUpdateCustomFieldRelation($modelClassName, $attributeName, $attributeLabels, $defaultValue, $isRequired, $isAudited, $elementType, $customFieldDataName, $customFieldDataData, $customFieldDataLabels);
     if ($attributeForm->getCustomFieldDataId() != null) {
         $oldAndNewValuePairs = array();
         foreach ($attributeForm->customFieldDataData as $order => $newValue) {
             if (isset($attributeForm->customFieldDataDataExistingValues[$order]) && $attributeForm->customFieldDataDataExistingValues[$order] != $newValue) {
                 CustomField::updateValueByDataIdAndOldValueAndNewValue($attributeForm->getCustomFieldDataId(), $attributeForm->customFieldDataDataExistingValues[$order], $newValue);
                 $oldValue = $attributeForm->customFieldDataDataExistingValues[$order];
                 $oldAndNewValuePairs[$oldValue] = $newValue;
             }
         }
         if (count($oldAndNewValuePairs) > 0) {
             DropDownDependencyDerivedAttributeDesignerUtil::updateValueInMappingByOldAndNewValue($modelClassName, $attributeName, $oldAndNewValuePairs, $attributeForm->customFieldDataDataExistingValues[$order], $newValue);
         }
         DropDownDependencyDerivedAttributeDesignerUtil::resolveValuesInMappingWhenValueWasRemoved($modelClassName, $attributeName, $attributeForm->customFieldDataData);
     }
     $this->resolveDatabaseSchemaForModel($modelClassName);
 }
Ejemplo n.º 2
0
 /**
  * @depends testSetAttributesWithPostForCustomField
  */
 public function testUpdateValueOnCustomFieldRows()
 {
     $values = array('A', 'B', 'C');
     $customFieldData = CustomFieldData::getByName('updateItems');
     $customFieldData->serializedData = serialize($values);
     $this->assertTrue($customFieldData->save());
     $id = $customFieldData->id;
     $customField = new CustomField();
     $customField->value = 'A';
     $customField->data = $customFieldData;
     $this->assertTrue($customField->save());
     $customField = new CustomField();
     $customField->value = 'B';
     $customField->data = $customFieldData;
     $this->assertTrue($customField->save());
     $customField = new CustomField();
     $customField->value = 'C';
     $customField->data = $customFieldData;
     $this->assertTrue($customField->save());
     $customField = new CustomField();
     $customField->value = 'C';
     $customField->data = $customFieldData;
     $this->assertTrue($customField->save());
     $quote = DatabaseCompatibilityUtil::getQuote();
     $customFieldTableName = CustomField::getTableName();
     $baseCustomFieldTableName = BaseCustomField::getTableName();
     $valueAttributeColumnName = 'value';
     $dataAttributeColumnName = RedBeanModel::getForeignKeyName('CustomField', 'data');
     $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
     $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
     $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
     $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
     $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id}";
     $ids = ZurmoRedBean::getCol($sql);
     $beans = ZurmoRedBean::batch($customFieldTableName, $ids);
     $customFields = RedBeanModel::makeModels($beans, 'CustomField');
     $this->assertEquals(4, count($customFields));
     $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
     $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
     $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
     $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
     $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} ";
     $sql .= "and {$quote}{$valueAttributeColumnName}{$quote} = 'B'";
     $this->assertEquals(1, count(ZurmoRedBean::getCol($sql)));
     $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
     $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
     $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
     $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
     $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} ";
     $sql .= "and {$quote}{$valueAttributeColumnName}{$quote} = 'C'";
     $this->assertEquals(2, count(ZurmoRedBean::getCol($sql)));
     $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
     $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
     $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
     $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
     $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} ";
     $sql .= "and {$quote}{$valueAttributeColumnName}{$quote} = 'E'";
     $this->assertEquals(0, count(ZurmoRedBean::getCol($sql)));
     CustomField::updateValueByDataIdAndOldValueAndNewValue($id, 'C', 'E');
     $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
     $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
     $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
     $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
     $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} ";
     $sql .= "and {$quote}{$valueAttributeColumnName}{$quote} = 'B'";
     $this->assertEquals(1, count(ZurmoRedBean::getCol($sql)));
     $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
     $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
     $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
     $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
     $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} ";
     $sql .= "and {$quote}{$valueAttributeColumnName}{$quote} = 'C'";
     $this->assertEquals(0, count(ZurmoRedBean::getCol($sql)));
     $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
     $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
     $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
     $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
     $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} ";
     $sql .= "and {$quote}{$valueAttributeColumnName}{$quote} = 'E'";
     $this->assertEquals(2, count(ZurmoRedBean::getCol($sql)));
 }