public function __construct(Table $table) { parent::__construct($table); $eol = C::eol(); $t = array(2 => C::tab(2, 4), 3 => C::tab(3, 4), 4 => C::tab(4, 4), 22 => C::tab(2, 2), 32 => C::tab(3, 2), 42 => C::tab(4, 2)); $column_header = $column_select = $pk = $schema = $rel = $fields_form = $namespace_list = $option_list = $date_field = ''; foreach ($table->column as $key => $column) { $column_select .= '{table}.' . $column->Field . ',' . $eol; $column_header .= $t[42] . '<th>{{ @fields.' . $column->Field . ' }}</th>' . $eol; $fields_form .= $this->form($column) . $eol; $schema .= str_replace(array('field', 'label', 'EOL', 'T4', 'T3'), array($column->Field, $column->label, $eol, $t[4], $t[3]), "'field'=>array(EOLT4'label',EOLT4" . $this->filter($column) . "," . 'EOLT4' . $column->defaultContent . "EOLT4),EOLT3"); if ($column->isReferenced) { $namespace_list .= 'use {#model_namespace#}\\' . C::camelhead($column->referencedTable) . ';' . $eol; $option_list .= $eol . $t[2] . '$moe->set(\'' . C::camelcase($column->referencedTable) . '\', ' . C::camelhead($column->referencedTable) . '::instance()->optionList());'; } !$column->isDate || ($date_field .= $t[3] . '$' . $column->Field . ' = $moe->get(\'POST.' . $column->Field . '\');' . $eol . $t[3] . 'krsort($' . $column->Field . ');' . $eol . $t[3] . '$moe->set(\'POST.' . $column->Field . '\', implode(\'-\', $' . $column->Field . '));' . $eol); !$column->isPrimaryKey() || ($pk .= "'{$column->Field}', "); } if ($table->relation) { $this->use_relation = $eol . $t[3] . '->useRelation(\'all\')'; $db = C::get('db'); foreach ($table->relation as $rel) { foreach ($db->table[$rel['table']]->column as $column) { $tname = $db->table[$rel['table']]->name; $column_select .= $tname . '.' . $column->Field . ' as ' . $tname . '_' . $column->Field . ',' . $eol; $column_header .= $t[42] . '<th>' . $column->label . '</th>' . $eol; } } $db = null; unset($db); } $this->controller = 'Crud' . $table->model; $this->model = $table->model; $this->table_name = $table->name; $this->label = $table->label; $this->column_header = trim($column_header); $this->namespace_list = ltrim($namespace_list); $this->option_list = rtrim($option_list); $this->fields_form = ltrim($fields_form); $this->date_field = ltrim($date_field); $this->column_select = trim($column_select, ',' . $eol); $this->schema = rtrim($schema); $this->pk = trim($pk, ' ,'); $this->primary_key = reset($table->primary_key); foreach ($table->relation as $fil1 => $tab) { $this->rel .= str_replace(array('{tab}', '{fil1}', '{fil2}', '{EOL}', '{S3}'), array($tab['table'], $fil1, $tab['field'], $eol, $t[3]), "'{tab}'=>'join {join} on {join}.{fil2} = {table}.{fil1}',{EOL}{S3}"); } $this->rel = trim($this->rel); foreach (C::config('namespaces') ?: array() as $key => $value) { $key2 = $key . '_quoted'; $this->{$key} = $this->replaceToken($value); $this->{$key2} = addslashes($this->{$key}); } $cff = array(); foreach (C::config('files') ?: array() as $key => $value) { C::ext($value['name']) !== 'ini' || array_push($cff, C::name($this->replaceToken($value['name']))); } ksort($cff); $this->config_files = "'" . implode("'," . C::eol() . C::tab(1, 4) . "'", $cff) . "'"; $this->table = $table; }
public function __construct($dir) { $c = C::config(); C::start('Setting project directory to: ' . $dir . '...'); if (file_exists(realpath($dir))) { $dir = realpath($dir); } else { !mkdir($dir, 0755, true) || ($dir = realpath($dir)); } $this->tar = C::fixslashes($dir); is_dir($this->tar) || C::error('invalid dir'); C::finish(); if ($c['server']) { C::start('Setting project link: ...'); isset($c['server']['host'], $c['server']['path']) || C::error('invalid server config'); $this->rlink = C::fixslashes($c['server']['host']) . str_replace(C::fixslashes($c['server']['path']), '', $this->tar); C::finish(); } C::start('Checking config...'); isset($c['generator']['template']) || C::error('no generator.template'); isset($c['database']['name']) || C::error('no database.name'); isset($c['database']['host']) || C::error('no database.host'); isset($c['database']['username']) || C::error('no database.username'); isset($c['database']['password']) || C::error('no database.password'); isset($c['fixed']) || C::error('no fixed'); isset($c['files']) || C::error('no files'); C::finish(); $gen_temp = C::config('generator')['template']; !is_array($gen_temp) || ($gen_temp = end($gen_temp)); C::start('Setting template directory to: ' . $gen_temp . '...'); file_exists($gen_temp) || C::error('fail'); $this->dir = C::fixslashes($gen_temp); C::finish(); C::start('Checking lookup path...'); $paths = C::config('path') ?: array(); foreach ($paths as $key => $value) { $paths[$key] = C::fixslashes($value); } $this->path = $paths; C::config('path', $paths); C::finish(); C::start('Checking files...', 0, 1); ob_start(); $ok = true; $nss = array(); foreach (C::config('files') as $key => $value) { C::start('Checking: ' . $key . '...'); isset($value['name']) || C::error('no key name'); $msg = 'OK'; $path = isset($value['path']) ? C::fixslashes($value['path']) : ''; $e = C::ext($value['name']); $path || !isset($paths[$e]) || ($path = $paths[$e]); if (isset($value['template'])) { $ok = file_exists($this->dir . $value['template']); $ok || ($msg = 'file not exists'); } elseif (isset($value['copyConfig'])) { $ok = isset($c[$key]); $ok || ($msg = 'no ' . $key . ' in config'); } !$path || ($nss[$key . '_namespace'] = rtrim(strtr($path, '/', '\\'), '\\')); C::finish($msg); if ($ok) { continue; } else { break; } } C::config('namespaces', $nss); C::shiftAll(ob_get_clean(), 1); $ok || C::error('Failed'); C::finish(); if (isset($c['composer'], $c['composer']['path'])) { C::start('Checking composer...'); exec($c['composer']['path'] . ' -v', $tmp); isset($tmp[7]) || C::error('Composer was not installed'); $this->cmp = $c['composer']['path']; !isset($c['composer']['command']) || ($this->cmpCmd = $c['composer']['command']); C::finish(); } if (isset($c['generator']['token'])) { $this->token = $c['generator']['token']; } $this->str = $this->dir . $this->str; if (isset($c['generator']['structure'])) { C::start('Checking structure...'); if (file_exists($c['generator']['structure'])) { $this->str = $c['generator']['structure']; } elseif (file_exists($this->dir . $c['generator']['structure'])) { $this->str = $this->dir . $c['generator']['structure']; } else { C::error('Structure doesn\'t exists'); } C::finish(); } C::start('Construct hole database...', 0, 1); ob_start(); C::set('db', new Database($c['database'])); C::shiftAll(ob_get_clean(), 1); C::finish(); $this->tempDir = C::fixslashes(sys_get_temp_dir()) . 'crudgen/'; }