/** * Generates set of code based on data. * * @return array */ public function generate() { $this->prepareData($this->data); $columnFields = ['name', 'description', 'label']; $table = $this->describe->getTable($this->data['name']); foreach ($table as $column) { if ($column->isAutoIncrement()) { continue; } $field = strtolower($column->getField()); $method = 'set_' . $field; $this->data['camel'][$field] = lcfirst(camelize($method)); $this->data['underscore'][$field] = underscore($method); array_push($this->data['columns'], $field); if ($column->isForeignKey()) { $referencedTable = Tools::stripTableSchema($column->getReferencedTable()); $this->data['foreignKeys'][$field] = $referencedTable; array_push($this->data['models'], $referencedTable); $dropdown = ['list' => plural($referencedTable), 'table' => $referencedTable, 'field' => $field]; if (!in_array($field, $columnFields)) { $dropdown['field'] = $this->describe->getPrimaryKey($referencedTable); } array_push($this->data['dropdowns'], $dropdown); } } return $this->data; }
/** * Gets the primary keys based on specified field. * * @param array $data * @param string $field * @param string $referencedTable * @return array */ protected function getPrimaryKey(array $data, $field, $referencedTable) { $accessor = 'get_' . $this->describe->getPrimaryKey($referencedTable); $data['primaryKeys'][$field] = $accessor; if ($data['isCamel']) { $camelized = camelize($data['primaryKeys'][$field]); $data['primaryKeys'][$field] = $camelized; } return $data; }
/** * Returns the required data for model. * * @return void */ public function concat(array &$data) { $columns = $this->describe->getTable($data['name']); $counter = 0; $data['labels'] = ''; $data['rules'] = ''; foreach ($columns as $column) { $isBoolean = $column->getDataType() == 'integer' && $column->getLength() == 1; if ($column->isPrimaryKey() || $column->isNull() || $isBoolean || strpos($column->getDataType(), 'blob') !== false) { continue; } $template = '$this->validator->rule(\'required\', \'{name}\');' . "\n"; $keywords = ['{name}' => $column->getField(), '{mutatorName}' => Inflector::camelize('set_' . $column->getField())]; $label = ucfirst(str_replace('_', ' ', Inflector::tableize($column->getField()))); $template = str_replace(array_keys($keywords), array_values($keywords), $template); if ($column->isForeignKey()) { $referencedTable = $this->stripTableSchema($column->getReferencedTable()); $label = ucfirst(str_replace('_', ' ', Inflector::tableize($referencedTable))); } if ($column->getField() != 'datetime_created' && $column->getField() != 'datetime_updated' && $column->getField() != 'password') { $data['labels'] .= '\'' . $column->getField() . '\' => \'' . $label . '\','; $data['rules'] .= $template; } if ($column->getField() == 'password') { $data['labels'] .= '\'' . $column->getField() . '\' => \'' . $label . '\','; $data['labels'] .= "\n " . '\'password_confirmation\' => \'Password confirmation\','; } if ($counter < count($columns) - 1) { if ($column->getField() != 'datetime_created' && $column->getField() != 'datetime_updated') { if ($column->getField() != 'password') { $data['rules'] .= ' '; } $data['labels'] .= "\n" . ' '; } } $counter++; } $data['labels'] = trim($data['labels']); $data['rules'] = trim($data['rules']); foreach ($columns as $column) { if ($column->getField() == 'password') { $data['rules'] .= "\n\n " . 'if (isset($data[\'password\']) && $data[\'password\'] != null || ! isset($data[\'_method\'])) { ' . "\n " . '$this->validator->rule(\'required\', \'password\');' . "\n " . '$this->validator->rule(\'equals\', \'password\', \'password_confirmation\');' . "\n " . '}'; } } }
/** * Generates set of code based on data. * * @return array */ public function generate() { $this->prepareData($this->data); foreach ($this->data['columns'] as $column) { $field = strtolower($column->getField()); $accessor = 'get_' . $field; $mutator = 'set_' . $field; $this->data['camel'][$field] = ['field' => lcfirst(camelize($field)), 'accessor' => lcfirst(camelize($accessor)), 'mutator' => lcfirst(camelize($mutator))]; $this->data['underscore'][$field] = ['field' => lcfirst(underscore($field)), 'accessor' => lcfirst(underscore($accessor)), 'mutator' => lcfirst(underscore($mutator))]; if ($column->isForeignKey()) { $field = $column->getField(); array_push($this->data['indexes'], $field); $this->data['primaryKeys'][$field] = 'get_' . $this->describe->getPrimaryKey($column->getReferencedTable()); if ($this->data['isCamel']) { $this->data['primaryKeys'][$field] = camelize($this->data['primaryKeys'][$field]); } } $column->setReferencedTable(Tools::stripTableSchema($column->getReferencedTable())); } return $this->data; }
/** * Generates set of code based on data. * * @return array */ public function generate() { $this->prepareData($this->data); foreach ($this->data['columns'] as $column) { $field = strtolower($column->getField()); $accessor = 'get_' . $field; $mutator = 'set_' . $field; $this->data['camel'][$field] = ['field' => lcfirst(camelize($field)), 'accessor' => lcfirst(camelize($accessor)), 'mutator' => lcfirst(camelize($mutator))]; $this->data['underscore'][$field] = ['field' => lcfirst(underscore($field)), 'accessor' => lcfirst(underscore($accessor)), 'mutator' => lcfirst(underscore($mutator))]; if ($column->isForeignKey()) { $referencedTable = Tools::stripTableSchema($column->getReferencedTable()); $this->data['foreignKeys'][$field] = plural($referencedTable); $singular = $field . '_singular'; $this->data['foreignKeys'][$singular] = singular($referencedTable); $this->data['primaryKeys'][$field] = 'get_' . $this->describe->getPrimaryKey($referencedTable); if ($this->data['isCamel']) { $this->data['primaryKeys'][$field] = camelize($this->data['primaryKeys'][$field]); } } } return $this->data; }
/** * Returns the required data for model. * * @return void */ public function concat(array &$data) { $config = Configuration::get(); $columns = $this->describe->getTable($data['name']); $data['repository'] = (object) ['compacts' => (object) ['create' => [], 'edit' => ''], 'dropdowns' => '', 'name' => 'repository']; $data['parameters'] = ''; foreach ($columns as $column) { if (strpos($column->getDataType(), 'blob') !== false) { $data['parameters'] = "\n\n " . '$files = request()->getUploadedFiles();'; } } foreach ($columns as $column) { if ($column->isForeignKey()) { $referencedTable = $this->stripTableSchema($column->getReferencedTable()); $keywords = ['{application}' => $config->application->name, '{namespace}' => $config->namespaces->repositories, '{plural}' => Inflector::pluralize($referencedTable), '{singularTitle}' => ucfirst(Inflector::singularize($referencedTable)), '{singular}' => lcfirst(Inflector::classify(Inflector::singularize($referencedTable)))]; $data['repository']->name = lcfirst(Inflector::classify($data['singular'])) . 'Repository'; $compact = ', \'{plural}\''; $dropdown = '${plural} = repository(\'{application}\\{namespace}\\{singularTitle}Repository\')->findAll();' . "\n "; $compact = str_replace(array_keys($keywords), array_values($keywords), $compact); $dropdown = str_replace(array_keys($keywords), array_values($keywords), $dropdown); $data['repository']->compacts->edit .= $compact; $data['repository']->dropdowns .= $dropdown; array_push($data['repository']->compacts->create, "'" . $keywords['{plural}'] . "'"); } if (strpos($column->getDataType(), 'blob') !== false) { $data['parameters'] .= "\n " . '$parameters[\'' . $column->getField() . '\'] = $files[\'' . $column->getField() . '\'];'; } } if (!empty($data['repository']->compacts->create)) { $data['repository']->compacts->create = ', compact(' . implode(', ', $data['repository']->compacts->create) . ')'; } else { $data['repository']->compacts->create = ''; } if (!empty($data['repository']->dropdowns)) { $data['repository']->dropdowns = substr($data['repository']->dropdowns, 0, count($data['repository']->dropdowns) - 9); $data['repository']->dropdowns .= "\n "; } }
/** * Sets specified fields for foreign key values. * * @param \Rougin\Describe\Column $column */ public function setForeignKey(Column $column, array &$data, array &$keywords) { if (!$column->isForeignKey()) { return; } $config = Configuration::get(); $template = $this->foreignColumnTemplate; $data['methods'] .= "\n " . $this->mutatorMethodTemplate . "\n "; $data['methods'] .= $this->accessorMethodTemplate; $referencedTable = $this->stripTableSchema($column->getReferencedTable()); $keywords['{accessorName}'] = Inflector::camelize('get_' . $referencedTable); $keywords['{mutatorName}'] = Inflector::camelize('set_' . $referencedTable); $keywords['{primaryKey}'] = $this->describe->getPrimaryKey($referencedTable); $keywords['{variable}'] = $referencedTable; $keywords['{referencedTable}'] = ucfirst($referencedTable); $keywords['{description}'] = $referencedTable; $keywords['{class}'] = ucfirst($referencedTable) . ' '; $keywords['{datatype}'] = '\\' . $config->application->name . '\\' . $config->namespaces->models . '\\' . ucfirst($referencedTable); $template = str_replace(array_keys($keywords), array_values($keywords), $template); $data['methods'] = str_replace(array_keys($keywords), array_values($keywords), $data['methods']); $data['columns'] .= $template; }
/** * Generates a template based on type. * * @param array $data * @param string $templatesPath * @param string $type * @return string */ public function generate($data, $templatesPath, $type) { $columns = $this->describe->getTable($data['{name}']); $counter = 0; $tableHeading = []; $tableBody = []; $data['{columnForm}'] = ''; foreach ($columns as $column) { if ($column->isPrimaryKey() || $column->getField() == 'datetime_created' || $column->getField() == 'datetime_updated') { continue; } $description = 'name'; $referencedTable = ''; $template = $this->columnFormTemplate; if ($column->isForeignKey()) { $referencedTable = $this->stripTableSchema($column->getReferencedTable()); $foreignColumns = $this->describe->getTable($referencedTable); $template = $this->foreignColumnFormTemplate; foreach ($foreignColumns as $foreignColumn) { if (!$foreignColumn->isPrimaryKey() && !$foreignColumn->isForeignKey()) { $description = $foreignColumn->getField(); break; } } } if ($type == 'edit') { $template = str_replace('session.old.{name}', 'item.{value}', $template); } $columnBody = Inflector::camelize($column->getField()); $columnTitle = ucwords(str_replace('_', ' ', Inflector::tableize($column->getField()))); if ($column->isForeignKey()) { $columnTitle = ucwords(str_replace('_', ' ', Inflector::tableize($referencedTable))); $columnBody = Inflector::singularize($referencedTable) . '.' . $description; } if ($column->getField() != 'password') { array_push($tableHeading, '<td>' . $columnTitle . '</td>'); if (strpos($column->getDataType(), 'blob') !== false) { $image = '<img src="data:image/png;base64,{{ item.' . $columnBody . ' }}" height="50" width="50" />'; array_push($tableBody, '<td>' . $image . '</td>'); } else { array_push($tableBody, '<td>{{ item.' . $columnBody . ' }}</td>'); } } $keywords = ['{columnTitle}' => $columnTitle, '{description}' => $description, '{name}' => $column->getField(), '{plural}' => Inflector::pluralize($referencedTable), '{singular}' => Inflector::singularize($referencedTable)]; if ($type == 'edit') { $keywords['{value}'] = Inflector::camelize($keywords['{name}']); } // column is a boolean (tinyint) if ($column->getDataType() == 'integer' && $column->getLength() == 1) { $template = str_replace(['<input type="text" name="{name}" class="form-control" value="{{ session.old.{name} }}" />', '{columnTitle}'], ['<input type="checkbox" name="{name}" />', '{columnTitle}?'], $template); if ($type == 'edit') { $template = str_replace('<input type="text" name="{name}" class="form-control" value="{{ item.{value} }}" />', '<input type="checkbox" name="{name}" {{ item.{value} ? \'checked\' : \'\' }} />', $template); } } else { if ($column->getDataType() == 'date') { $template = str_replace('type="text"', 'type="date"', $template); } else { if (strpos($column->getDataType(), 'blob') !== false) { $template = str_replace('type="text"', 'type="file"', $template); $template = str_replace(' class="form-control" value="{{ session.old.{name} }}"', '', $template); $template = str_replace(' class="form-control" value="{{ item.{value} }}"', '', $template); } else { if ($column->getField() == 'password') { if ($type == 'edit') { $template = str_replace(' value="{{ item.{value} }}"', '', $template); } $template = str_replace('type="text"', 'type="password"', $template); } } } } $template = str_replace(array_keys($keywords), array_values($keywords), $template); $data['{columnForm}'] .= $template; if ($column->getField() == 'password') { $keywords = ['{columnTitle}' => 'Confirm Password', '{name}' => 'password_confirmation', 'type="text"' => 'type="password"', ' value="{{ session.old.password_confirmation }}"' => '']; $template = str_replace(array_keys($keywords), array_values($keywords), $this->columnFormTemplate); $data['{columnForm}'] .= ' ' . $template; } if ($counter < count($columns) - 1) { $data['{columnForm}'] .= ' '; } $counter++; } $actions = '' . '<td>' . "\n " . '<a href="{{ (\'/' . Inflector::pluralize($data['{name}']) . '/\' ~ item.id ~ \'/edit\') | url }}" class="btn btn-xs btn-info">' . "\n " . '<i class="fa fa-pencil fa-fw"></i> Update' . "\n " . '</a>' . "\n " . '<form action="{{ (\'/' . Inflector::pluralize($data['{name}']) . '/\' ~ item.id) | url }}" style="display: inline-block" method="POST" onsubmit="return confirm(\'Are you sure that you want to delete the selected item?\');">' . "\n " . '<input type="hidden" name="_method" value="DELETE" />' . "\n " . '<button type="submit" class="btn btn-xs btn-danger">' . "\n " . '<i class="fa fa-trash fa-fw"></i> Delete' . "\n " . '</button>' . "\n " . '</form>' . "\n " . '</td>'; array_push($tableHeading, '<td width="200px">Action</td>'); array_push($tableBody, $actions); $data['{columnForm}'] = trim($data['{columnForm}']); $data['{tableHeading}'] = implode("\n ", $tableHeading); $data['{tableBody}'] = implode("\n ", $tableBody); $item = file_get_contents($templatesPath . '/Views/' . $type . '.twig'); $item = str_replace(array_keys($data), array_values($data), $item); return $item; }
/** * Returns the required data for model. * * @return void */ public function concat(array &$data) { $config = Configuration::get(); $columns = $this->describe->getTable($data['name']); $counter = 0; $foreignKeys = 0; $data['singular'] = lcfirst(Inflector::classify($data['singular'])); $data['createColumns'] = ''; $data['updateColumns'] = ''; foreach ($columns as $column) { if ($column->isForeignKey()) { $foreignKeys++; } } foreach ($columns as $column) { if (!$column->isForeignKey()) { continue; } $referencedTable = $this->stripTableSchema($column->getReferencedTable()); $template = $this->foreignVariableTemplate; $keywords = ['{application}' => $config->application->name, '{models}' => $config->namespaces->models, '{name}' => $column->getField(), '{referencedTable}' => $referencedTable, '{table}' => ucfirst($referencedTable)]; $template = str_replace(array_keys($keywords), array_values($keywords), $template); $data['createColumns'] .= $template; $data['updateColumns'] .= $template; if ($counter < $foreignKeys - 1) { $data['createColumns'] .= ' '; $data['updateColumns'] .= ' '; } $counter++; } if ($data['createColumns'] != '') { $data['createColumns'] .= "\n "; $data['updateColumns'] .= "\n "; } $counter = 0; foreach ($columns as $column) { if ($column->isPrimaryKey()) { continue; } $keywords = ['{name}' => $column->getField(), '{mutatorName}' => Inflector::camelize('set_' . $column->getField()), '{singular}' => lcfirst($data['singularTitle'])]; $template = $this->mutatorMethodTemplate; if ($column->isForeignKey()) { $referencedTable = $this->stripTableSchema($column->getReferencedTable()); $template = $this->foreignMutatorMethodTemplate; $keywords['{table}'] = lcfirst(Inflector::classify($referencedTable)); $keywords['{mutatorName}'] = Inflector::camelize('set_' . $referencedTable); } if ($column->getField() == 'datetime_created' || $column->getField() == 'datetime_updated') { $template = str_replace('$data[\'{name}\']', "'now'", $template); } else { if ($column->getDataType() == 'integer' && $column->getLength() == 1) { $template = str_replace('$data[\'{name}\']', 'isset($data[\'{name}\'])', $template); } else { if ($column->getField() == 'password') { $template = str_replace('$data[\'{name}\']', 'md5($data[\'{name}\'])', $template); } else { if ($column->isNull()) { $template = str_replace('$data[\'{name}\']', 'isset($data[\'{name}\']) ? $data[\'{name}\'] : null', $template); } } } } $template = str_replace(array_keys($keywords), array_values($keywords), $template); if ($column->getField() != 'datetime_updated' && strpos($column->getDataType(), 'blob') === false) { $data['createColumns'] .= $template; } if ($column->getField() != 'datetime_created' && $column->getField() != 'password' && strpos($column->getDataType(), 'blob') === false) { $data['updateColumns'] .= $template; } if ($counter < count($columns) - 1) { if ($column->getField() != 'datetime_updated' && strpos($column->getDataType(), 'blob') === false) { $data['createColumns'] .= ' '; } if ($column->getField() != 'datetime_created' && $column->getField() != 'password' && strpos($column->getDataType(), 'blob') === false) { $data['updateColumns'] .= ' '; } } $counter++; } $data['createColumns'] = trim($data['createColumns']); $data['updateColumns'] = trim($data['updateColumns']); foreach ($columns as $column) { if ($column->getField() != 'password' && strpos($column->getDataType(), 'blob') === false) { continue; } if ($column->getField() == 'password') { $table = lcfirst(Inflector::classify($data['singular'])); $mutator = Inflector::camelize('set_' . $column->getField()); $data['updateColumns'] .= "\n\n " . 'if ($data[\'' . $column->getField() . '\']) {' . "\n " . '$' . $table . '->' . $mutator . '(md5($data[\'' . $column->getField() . '\']));' . "\n " . '}'; } if (strpos($column->getDataType(), 'blob') !== false) { $table = lcfirst(Inflector::classify($data['singular'])); $mutator = Inflector::camelize('set_' . $column->getField()); $code = "\n\n " . 'if (! $data[\'' . $column->getField() . '\']->getError()) {' . "\n " . '$' . $column->getField() . ' = $data[\'' . $column->getField() . '\']->getStream()->getContents();' . "\n\n " . '$' . $table . '->' . $mutator . '($' . $column->getField() . ');' . "\n " . '}'; $data['createColumns'] .= $code; $data['updateColumns'] .= $code; } } }