protected function _addModelLabel($id) { $defaults = array('label' => ucfirst(Garp_Spawn_Util::underscored2readable(Garp_Spawn_Util::camelcased2underscored($this['id'])))); foreach ($defaults as $prop => $defaultValue) { if (!array_key_exists($prop, $this) || is_null($this[$prop])) { $this[$prop] = $defaultValue; } } }
/** * @return Garp_Spawn_Config_Model_Binding */ protected function _getBindingModelConfig() { $relation = $this->getRelation(); $habtmModelId = $this->_getBindingModelName(); $rules = $this->_getRules(); $config = $this->_getBindingModelParams(); if ($relation->weighable) { $weightCol1 = Garp_Spawn_Util::camelcased2underscored($rules[0] . $rules[1]) . '_weight'; $weightCol2 = Garp_Spawn_Util::camelcased2underscored($rules[1] . $rules[0]) . '_weight'; $config['inputs'][$weightCol1] = array('type' => 'numeric'); $config['inputs'][$weightCol2] = array('type' => 'numeric'); } $bindingModelConfig = new Garp_Spawn_Config_Model_Binding($habtmModelId, new Garp_Spawn_Config_Storage_PhpArray(array($habtmModelId => $config)), new Garp_Spawn_Config_Format_PhpArray()); return $bindingModelConfig; }
public function getImageFieldId($columnName, $locale = false) { return Garp_Spawn_Util::underscored2camelcased($columnName) . ($locale ? '_' . $locale : ''); }
protected function _addWeighableRelationFields() { $weighableRels = $this->_model->relations->getRelations('weighable', true); $fieldConfig = array('type' => 'numeric', 'required' => false, 'default' => 0, 'editable' => false, 'visible' => false); foreach ($weighableRels as $relName => $rel) { $this->add('relation', Garp_Spawn_Util::camelcased2underscored($relName . '_weight'), $fieldConfig); } }
/** * @param String $modelId Model identifier * @return String The model namespace, to be used in front of a column name, i.e. blog_post */ protected function _getModelNamespace($modelId) { $modelNamespace = Garp_Spawn_Util::camelcased2underscored($modelId); return $modelNamespace; }
/** * Adds the weighable behavior, for user defined sorting of related objects. * Can only be initialized after the relations for this model are set. * * @return void */ protected function _addWeighableBehavior() { $model = $this->getModel(); $weighableRels = $model->relations->getRelations('weighable', true); if (!$weighableRels) { return; } $weighableConfig = array(); foreach ($weighableRels as $relName => $rel) { $weightColumn = Garp_Spawn_Util::camelcased2underscored($relName) . '_weight'; $weighableConfig[$relName] = array('foreignKeyColumn' => $rel->column, 'weightColumn' => $weightColumn); } $this->_add('relation', 'Weighable', $weighableConfig); }
protected function _deleteNoLongerConfiguredColumn(Garp_Spawn_MySql_Column $targetColumn) { $source = $this->getSource(); $target = $this->getTarget(); $progress = Garp_Cli_Ui_ProgressBar::getInstance(); if ($source->columnExists($targetColumn->name)) { return; } if ($this->getFeedback()->isInteractive()) { $progress->display("Delete column {$target->name}.{$targetColumn->name}? "); if (!Garp_Spawn_Util::confirm()) { return; } } $target->deleteColumn($targetColumn); }
/** * @param String $modelName Name of the model. * @param Int $index Optional index of this column, in case of a homosexual relation. * @return String Database column / field name that corresponds to this model. E.g.: 'Users' -> 'users_id' */ public static function getRelationColumn($modelName, $index = null) { if (!is_string($modelName)) { $error = sprintf(self::ERROR_MODELNAME_IS_NO_STRING, __METHOD__); throw new Exception($error); } $modelNamespace = Garp_Spawn_Util::camelcased2underscored($modelName); $relationColumn = $modelNamespace . (is_null($index) ? '' : (string) $index) . '_id'; return $relationColumn; }
protected function _removeUniqueKeys(array $keysToRemove) { $progress = $this->getFeedback(); $tableName = $this->getSource()->getTableName(); foreach ($keysToRemove as $key) { $fields = $this->_model->fields->getFields('name', $key->column); $field = current($fields); $columnsOutput = implode(', ', (array) $key->column); if ($progress->isInteractive()) { $progress->display("Make {$this->_model->id}.{$columnsOutput} no longer unique? "); if (!Garp_Spawn_Util::confirm()) { continue; } } if (!Garp_Spawn_MySql_UniqueKey::delete($tableName, $key)) { throw new Exception("Could not set column '{$columnsOutput}' to non-unique."); } } }
protected function _renderDefaultOrder() { $model = $this->getModel(); $commaInOrder = strpos($model->order, ",") !== false; $noOpeningParenthesisInOrder = strpos($model->order, "(") === false; $orderFieldNames = explode(", ", $model->order); $orderFieldNamesStatement = Garp_Spawn_Util::array2phpStatement($orderFieldNames); $orderValueStatement = $commaInOrder && $noOpeningParenthesisInOrder ? $orderFieldNamesStatement : "'{$model->order}'"; $orderStatement = "protected \$_defaultOrder = {$orderValueStatement};"; $out = $this->_rl($orderStatement, 1, 2); return $out; }
protected function _getConfirmationMessage(array $diffProperties, Garp_Spawn_MySql_Column $newColumn) { if (count($diffProperties) === 1 && $diffProperties[0] === 'nullable') { return "Make {$this->name}.{$newColumn->name} " . ($newColumn->nullable ? 'no longer ' : '') . 'required? '; } else { $readableDiffPropsList = Garp_Spawn_Util::humanList($diffProperties, "'"); return "Change " . $readableDiffPropsList . " of {$this->name}.{$newColumn->name}? "; } }
/** * Throw a warning when a field is set to unique, while its maxLength is too large. */ protected function _validateUniqueKeyLength(ArrayObject $config) { $restrictedFieldTypes = array('text', 'html'); if (array_key_exists('inputs', $config)) { foreach ($config['inputs'] as $inputName => $input) { if (array_key_exists('unique', $input) && $input['unique'] && (!array_key_exists('type', $input) && !Garp_Spawn_Util::stringEndsIn('email', $inputName) && !Garp_Spawn_Util::stringEndsIn('url', $inputName) && !Garp_Spawn_Util::stringEndsIn('id', $inputName) && !Garp_Spawn_Util::stringEndsIn('date', $inputName) && !Garp_Spawn_Util::stringEndsIn('time', $inputName) || array_key_exists('type', $input) && in_array($input['type'], $restrictedFieldTypes)) && (!array_key_exists('maxLength', $input) || $input['maxLength'] > 255)) { throw new Exception("You've set {$config['id']}.{$inputName} to unique, but this type of field has to have a maxLength of 255 at most to be made unique."); } } } }
protected function _setConditionalDefaults(array $config) { if (!array_key_exists('type', $config)) { foreach ($this->_defaultTypeByNameEnding as $ending => $type) { if (Garp_Spawn_Util::stringEndsIn($ending, $this->name)) { $this->type = $type; } } } if (!array_key_exists('maxLength', $config)) { switch ($this->name) { case 'name': case 'subtitle': $this->maxLength = self::TEXTFIELD_MAX_LENGTH; break; case 'id': $this->maxLength = 8; break; case 'email': $this->maxLength = 50; break; default: if (Garp_Spawn_Util::stringEndsIn('name', $this->name)) { $this->maxLength = self::TEXTFIELD_MAX_LENGTH; } } } if (!array_key_exists('multiline', $config) && $this->isTextual()) { $this->multiline = !$this->maxLength || $this->maxLength > self::TEXTFIELD_MAX_LENGTH; } if ($this->type === 'checkbox') { $this->required = false; } if (!array_key_exists('label', $config) || !$config['label']) { $this->label = Garp_Spawn_Util::underscored2readable(Garp_Spawn_Util::stringEndsIn('_id', $this->name) ? substr($this->name, 0, -3) : $this->name); } else { $this->label = ucfirst($this->label); } $this->required = (bool) $this->required; }