function __construct($name, $values, Model $model = null) { $this->name = $name; $this->model = $model; if ($model != null) { $properties = Model::getProperties($model); $this->inputs = array(); foreach ($properties as $property) { $propertyName = $property->name; $inputName = $this->name . '[' . $propertyName . ']'; $inputValue = isset($values[$propertyName]) ? $values[$propertyName] : ''; switch ($property->type) { case RecessType::STRING: case RecessType::FLOAT: case RecessType::INTEGER: $this->inputs[$propertyName] = new TextInput($inputName); break; case RecessType::BOOLEAN: $this->inputs[$propertyName] = new BooleanInput($inputName); break; case RecessType::TEXT: $this->inputs[$propertyName] = new TextAreaInput($inputName); break; case RecessType::BLOB: $this->inputs[$propertyName] = new LabelInput($inputName); break; case RecessType::DATE: $this->inputs[$propertyName] = new DateTimeInput($inputName); $this->inputs[$propertyName]->showTime = false; break; case RecessType::DATETIME: $this->inputs[$propertyName] = new DateTimeInput($inputName); $this->inputs[$propertyName]->showTime = false; break; case RecessType::TIME: $this->inputs[$propertyName] = new DateTimeInput($inputName); $this->inputs[$propertyName]->showDate = false; break; case RecessType::TIMESTAMP: $this->inputs[$propertyName] = new DateLabelInput($inputName); break; case RecessType::POINT: $this->inputs[$propertyName] = new PointInput($inputName); break; case RecessType::BOX: $this->inputs[$propertyName] = new BoxInput($inputName); break; default: echo $property->type; } if ($property->isPrimaryKey) { $this->inputs[$propertyName] = new HiddenInput($propertyName); } if (isset($this->inputs[$propertyName]) && isset($values[$propertyName])) { $this->inputs[$propertyName]->setValue($values[$propertyName]); $model->{$propertyName} = $this->inputs[$propertyName]->getValue(); } } } }
/** * Test entity structure */ public function testEntity() { $config = array('db' => array('dsn' => DB_DSN, 'user' => DB_USER, 'pass' => DB_PASS)); $app = new \Duality\App(dirname(__FILE__), $config); $db = $app->call('db'); $entity = new \Model($db); $name = 'dummy'; $expected = array(new Property('id'), new Property($name)); $entity->addPropertiesFromArray(array($name)); $result = $entity->getProperties(); $this->assertEquals($expected, $result); }
/** * Add all of the model properties as columns * * @return string */ protected function addColumns() { $content = ''; foreach ($this->model->getProperties() as $field => $type) { if (!$this->tableHasColumn($field)) { $this->columnsChanged = true; $rule = "\t\t\t"; // Primary key check if ($field === 'id' and $type === 'integer') { $rule .= $this->increment(); } else { $rule .= $this->setColumn($this->model->validTypes[$type], $field); if (!empty($setting)) { $rule .= $this->addColumnOption($setting); } } array_push($this->columnsAdded, $field); $content .= $rule . ";\n"; } } return $content; }
/** * Replace [property] with model's properties * * @param $fileContents * @return mixed */ private function replaceProperties($fileContents) { $lastPos = 0; $needle = "[repeat]"; $endRepeat = "[/repeat]"; while (($lastPos = strpos($fileContents, $needle, $lastPos)) !== false) { $beginning = $lastPos; $lastPos = $lastPos + strlen($needle); $endProp = strpos($fileContents, $endRepeat, $lastPos); $end = $endProp + strlen($endRepeat); $replaceThis = substr($fileContents, $beginning, $end - $beginning); $propertyTemplate = substr($fileContents, $lastPos, $endProp - $lastPos); $properties = ""; foreach ($this->model->getProperties() as $property => $type) { $temp = str_replace("[property]", $property, $propertyTemplate); $temp = str_replace("[Property]", ucfirst($property), $temp); $properties .= $temp; } $properties = trim($properties, ","); $fileContents = str_replace($replaceThis, $properties, $fileContents); } return $fileContents; }
/** !Route GET, $app/model/$model/scaffolding */ public function generateScaffolding($app, $model) { $app = new $app(); if (strpos($app->controllersPrefix, 'recess.apps.') !== false) { $base = $_ENV['dir.recess']; } else { $base = $_ENV['dir.apps']; } Library::import('recess.lang.Inflector'); $controllersDir = $base . str_replace(Library::dotSeparator, Library::pathSeparator, $app->controllersPrefix); $viewsDir = $app->viewsDir; Library::import($app->modelsPrefix . $model); $replacements = array('modelName' => $model, 'modelNameLower' => Inflector::toCamelCaps($model), 'fullyQualifiedModel' => $app->modelsPrefix . $model, 'primaryKey' => Model::primaryKeyName($model), 'viewsPrefix' => Inflector::toCamelCaps($model), 'routesPrefix' => Inflector::toCamelCaps($model)); $this->messages[] = $this->tryGeneratingFile('RESTful ' . $model . ' Controller', $this->application->codeTemplatesDir . 'scaffolding/controllers/ResourceController.template.php', $controllersDir . $model . 'Controller.class.php', $replacements); $indexFieldTemplate = $this->getTemplate($this->application->codeTemplatesDir . 'scaffolding/views/resource/indexField.template.php'); $indexDateFieldTemplate = $this->getTemplate($this->application->codeTemplatesDir . 'scaffolding/views/resource/indexDateField.template.php'); $editFormInputTemplate = $this->getTemplate($this->application->codeTemplatesDir . 'scaffolding/views/resource/editFormInput.template.php'); $indexFields = ''; $formFields = ''; foreach (Model::getProperties($model) as $property) { if ($property->isPrimaryKey) { continue; } $values = array('fieldName' => $property->name, 'primaryKey' => Model::primaryKeyName($model), 'modelName' => $model, 'modelNameLower' => Inflector::toCamelCaps($model), 'fieldNameEnglish' => Inflector::toEnglish($property->name)); switch ($property->type) { case RecessType::DATE: case RecessType::DATETIME: case RecessType::TIME: case RecessType::TIMESTAMP: $template = $indexDateFieldTemplate; break; default: $template = $indexFieldTemplate; break; } $formFields .= $this->fillTemplate($editFormInputTemplate, $values); $indexFields .= $this->fillTemplate($template, $values); } $replacements['fields'] = $indexFields; $replacements['editFields'] = $formFields; $viewsDir = $app->viewsDir . $replacements['viewsPrefix'] . '/'; $this->messages[] = $this->tryCreatingDirectory($viewsDir, $model . ' views dir'); $this->messages[] = $this->tryGeneratingFile('resource layout', $this->application->codeTemplatesDir . 'scaffolding/views/resource/resource.layout.template.php', $viewsDir . '../layouts/' . $replacements['viewsPrefix'] . '.layout.php', $replacements); $this->messages[] = $this->tryGeneratingFile('index view', $this->application->codeTemplatesDir . 'scaffolding/views/resource/index.template.php', $viewsDir . 'index.html.php', $replacements); $this->messages[] = $this->tryGeneratingFile('editForm view', $this->application->codeTemplatesDir . 'scaffolding/views/resource/editForm.template.php', $viewsDir . 'editForm.html.php', $replacements, true); $this->messages[] = $this->tryGeneratingFile('form part', $this->application->codeTemplatesDir . 'scaffolding/views/resource/form.part.template.php', $viewsDir . 'form.part.php', $replacements, true); $this->messages[] = $this->tryGeneratingFile('static details', $this->application->codeTemplatesDir . 'scaffolding/views/resource/details.template.php', $viewsDir . 'details.html.php', $replacements); $this->messages[] = $this->tryGeneratingFile('details part', $this->application->codeTemplatesDir . 'scaffolding/views/resource/details.part.template.php', $viewsDir . 'details.part.php', $replacements); $this->appName = get_class($app); $this->modelName = $model; }