function create($aData) { if (!isset($aData['FLD_UID'])) { $aData['FLD_UID'] = G::generateUniqueID(); } else { if ($aData['FLD_UID'] == '') { $aData['FLD_UID'] = G::generateUniqueID(); } } $oConnection = Propel::getConnection(FieldsPeer::DATABASE_NAME); try { $oFields = new Fields(); $oFields->fromArray($aData, BasePeer::TYPE_FIELDNAME); if ($oFields->validate()) { $oConnection->begin(); $iResult = $oFields->save(); $oConnection->commit(); return $aData['FLD_UID']; } else { $sMessage = ''; $aValidationFailures = $oFields->getValidationFailures(); foreach ($aValidationFailures as $oValidationFailure) { $sMessage .= $oValidationFailure->getMessage() . '<br />'; } throw new Exception('The registry cannot be created!<br />' . $sMessage); } } catch (Exception $oError) { $oConnection->rollback(); throw $oError; } }
private static function tryAddCol($model, $col, $type) { if (!Fields::model()->findByAttributes(array('fieldName' => $col, 'modelName' => get_class($model)))) { $field = new Fields(); $field->setAttributes(array('modelName' => get_class($model), 'fieldName' => $col, 'attributeLabel' => $col, 'modified' => '1', 'custom' => 1, 'type' => $type, 'required' => '0', 'uniqueConstraint' => '0', 'safe' => '1', 'readOnly' => '0', 'linkType' => NULL, 'searchable' => '0', 'relevance' => 'Low', 'isVirtual' => '0', 'defaultValue' => NULL, 'keyType' => '', 'data' => NULL), false); if (!$field->save()) { AuxLib::debugLogR('$col = '); AuxLib::debugLogR($col); AuxLib::debugLogR($field->getErrors()); } } }
/** * Form/submit action for adding or customizing a field. * * This method allows for the creation of custom fields linked to any customizable * module in X2Engine. This is used by "Manage Fields." It is used to reload the * form via AJAX. * * @param bool $search If set to 1/true, perform a lookup for an existing field * @param bool $save If set to 1/true, attempt to save the model; otherwise just echo the form. */ public function actionCreateUpdateField($search = 0, $save = 0, $override = 0) { $changedType = false; if ($search) { // A field is being looked up, to populate form fields for customizing // an existing field $new = false; if (isset($_POST['Fields'])) { $model = Fields::model()->findByAttributes(array_intersect_key($_POST['Fields'], array_fill_keys(array('modelName', 'fieldName'), null))); } } else { // Requesting the form $new = true; } if (!isset($model) || !(bool) $model) { // If the field model wasn't found, create the object $model = new Fields(); } if (isset($_POST['Fields']) && ($model->isNewRecord || $override)) { $oldType = $model->type; $model->attributes = $_POST['Fields']; // field name exists if model refers to actual db record if ($model->fieldName && $model->type !== $oldType) { $changedType = true; } } $message = ''; $error = false; if (isset($_POST['Fields']) && $save) { $model->attributes = $_POST['Fields']; if (!isset($_POST['Fields']['linkType'])) { // This can be removed if we ever make the linkType attribute more structured // (i.e. field type-specific link type validation rules) $model->linkType = null; } // Set the default value if (isset($_POST['AmorphousModel'])) { $aModel = $_POST['AmorphousModel']; $model->defaultValue = $model->parseValue($aModel['customized_field']); } $new = $model->isNewRecord; $model->modified = 1; // The field has been modified if ($new) { // The field should be marked as custom since the user is adding it $model->custom = 1; } if ($model->save()) { $message = $new ? Yii::t('admin', 'Field added.') : Yii::t('admin', 'Field modified successfully.'); if ($new) { $model = new Fields(); } } else { $error = true; $message = Yii::t('admin', 'Please correct the following errors.'); } } $dummyModel = new AmorphousModel(); $dummyModel->addField($model, 'customized_field'); $dummyModel->setAttribute('customized_field', $model->defaultValue); $this->renderPartial('createUpdateField', array('model' => $model, 'new' => $new, 'dummyModel' => $dummyModel, 'message' => $message, 'error' => $error, 'changedType' => $changedType)); }
/** * Parse the given keys and attributes to ensure required fields are * mapped and new fields are to be created. The verified map will be * stored in the 'importMap' key for the $_SESSION super global. * @param string $model name of the model * @param array $keys * @param array $attributes * @param boolean $createFields whether or not to create new fields */ protected function verifyImportMap($model, $keys, $attributes, $createFields = false) { if (!empty($keys) && !empty($attributes)) { // New import map is the provided data $importMap = array_combine($keys, $attributes); $conflictingFields = array(); $failedFields = array(); // To keep track of fields that were mapped multiple times $mappedValues = array(); $multiMappings = array(); foreach ($importMap as $key => &$value) { if (in_array($value, $mappedValues) && !empty($value) && !in_array($value, $multiMappings)) { // This attribute is mapped to two different fields in X2 $multiMappings[] = $value; } else { if ($value !== 'createNew') { $mappedValues[] = $value; } } // Loop through and figure out if we need to create new fields $origKey = $key; $key = Formatter::deCamelCase($key); $key = preg_replace('/\\[W|_]/', ' ', $key); $key = mb_convert_case($key, MB_CASE_TITLE, "UTF-8"); $key = preg_replace('/\\W/', '', $key); if ($value == 'createNew' && !$createFields) { $importMap[$origKey] = 'c_' . strtolower($key); $fieldLookup = Fields::model()->findByAttributes(array('modelName' => $model, 'fieldName' => $key)); if (isset($fieldLookup)) { $conflictingFields[] = $key; continue; } else { $customFieldLookup = Fields::model()->findByAttributes(array('modelName' => $model, 'fieldName' => $importMap[$origKey])); if (!$customFieldLookup instanceof Fields) { // Create a custom field if one doesn't exist already $columnName = strtolower($key); $field = new Fields(); $field->modelName = $model; $field->type = "varchar"; $field->fieldName = $columnName; $field->required = 0; $field->searchable = 1; $field->relevance = "Medium"; $field->custom = 1; $field->modified = 1; $field->attributeLabel = $field->generateAttributeLabel($key); if (!$field->save()) { $failedFields[] = $key; } } } } } // Check for required attributes that are missing $requiredAttrs = Yii::app()->db->createCommand()->select('fieldName, attributeLabel')->from('x2_fields')->where('modelName = :model AND required = 1', array(':model' => str_replace(' ', '', $model)))->query(); $missingAttrs = array(); foreach ($requiredAttrs as $attr) { // Skip visibility, it can be set for them if (strtolower($attr['fieldName']) == 'visibility') { continue; } // Ignore missing first/last name, this can be inferred from full name if ($model === 'Contacts' && ($attr['fieldName'] === 'firstName' || $attr['fieldName'] === 'lastName') && in_array('name', array_values($importMap))) { continue; } // Otherwise, a required field is missing and should be reported to the user if (!in_array($attr['fieldName'], array_values($importMap))) { $missingAttrs[] = $attr['attributeLabel']; } } if (!empty($conflictingFields)) { echo CJSON::encode(array("2", implode(', ', $conflictingFields))); } else { if (!empty($missingAttrs)) { echo CJSON::encode(array("3", implode(', ', $missingAttrs))); } else { if (!empty($failedFields)) { echo CJSON::encode(array("1", implode(', ', $failedFields))); } else { if (!empty($multiMappings)) { echo CJSON::encode(array("4", implode(', ', $multiMappings))); } else { echo CJSON::encode(array("0")); } } } } $_SESSION['importMap'] = $importMap; } else { echo CJSON::encode(array("0")); $_SESSION['importMap'] = array(); } }
public function actionAddField() { $model = new Fields(); if (isset($_POST['Fields'])) { $model->attributes = $_POST['Fields']; isset($_POST['Fields']['required']) && $_POST['Fields']['required'] == 1 ? $model->required = 1 : ($model->required = 0); isset($_POST['Fields']['searchable']) && $_POST['Fields']['searchable'] == 1 ? $model->searchable = 1 : ($model->searchable = 0); $model->type = $_POST['Fields']['type']; // $model->visible=1; $model->custom = 1; $model->modified = 1; $model->fieldName = strtolower($model->fieldName); $fieldType = $model->type; switch ($fieldType) { case "boolean": $fieldType = "BOOLEAN"; break; case "float": $fieldType = "FLOAT"; break; case "int": $fieldType = "BIGINT"; break; case "text": $fieldType = "TEXT"; break; default: $fieldType = 'VARCHAR(250)'; break; } if ($model->type == 'dropdown') { if (isset($_POST['dropdown'])) { $id = $_POST['dropdown']; $model->linkType = $id; } } if ($model->type == "link") { if (isset($_POST['dropdown'])) { $linkType = $_POST['dropdown']; $model->linkType = ucfirst($linkType); } } $type = strtolower($model->modelName); $field = strtolower($model->fieldName); if (preg_match("/\\s/", $field)) { } else { if ($model->save()) { $sql = "ALTER TABLE x2_{$type} ADD COLUMN {$field} {$fieldType}"; $command = Yii::app()->db->createCommand($sql); $result = $command->query(); } } $this->redirect('manageFields'); } }