<commit_before><?php

namespace luya\commands;

use Yii;
use yii\helpers\Console;

class CrudController extends \yii\console\Controller
{
    public function actionCreate()
    {
        $module = $this->prompt('Module Name (e.g. galleryadmin):');
        $modulePre = $new_str = preg_replace('/admin$/', '', $module);
        $modelName = $this->prompt('Model Name (e.g. Album)');
        $apiEndpoint = $this->prompt('Api Endpoint (e.g. api-'.$modulePre.'-'.strtolower($modelName).')');
        $sqlTable = $this->prompt('Database Table name (e.g. '.strtolower($modulePre).'_'.strtolower($modelName).')');

        if (!$this->confirm("Create '$modelName' controller, api & model based on sql table '$sqlTable' in module '$module' for api endpoint '$apiEndpoint'?")) {
            exit(1);
        }

        $shema = Yii::$app->db->getTableSchema($sqlTable);

        if (!$shema) {
            echo 'you have to create a migration script and execute the migration first. the table must exists!';
            exit(1);
        }

        $yiiModule = Yii::$app->getModule($module);

        $basePath = $yiiModule->basePath;

        $ns = $yiiModule->getNamespace();

        $modelName = ucfirst($modelName);

        $modelNs = '\\'.$ns.'\\models\\'.$modelName;
        $data = [
            'api' => [
                'folder' => 'apis',
                'ns' => $ns.'\\apis',
                'file' => $modelName.'Controller.php',
                'class' => $modelName.'Controller',
                'route' => strtolower($module).'-'.strtolower($modelName).'-index',
            ],
            'controller' => [
                'folder' => 'controllers',
                'ns' => $ns.'\\controllers',
                'file' => $modelName.'Controller.php',
                'class' => $modelName.'Controller',
            ],
            'model' => [
                'folder' => 'models',
                'ns' => $ns.'\\models',
                'file' => $modelName.'.php',
                'class' => $modelName,
            ],
        ];
        $apiClass = null;
        foreach ($data as $name => $item) {
            $folder = $basePath.DIRECTORY_SEPARATOR.$item['folder'];

            if (!file_exists($folder)) {
                mkdir($folder);
            }

            if (file_exists($folder.DIRECTORY_SEPARATOR.$item['file'])) {
                echo $this->ansiFormat("Can not create $folder".DIRECTORY_SEPARATOR.$item['file'].', file does already exists!', Console::FG_RED).PHP_EOL;
            } else {
                $content = '<?php'.PHP_EOL.PHP_EOL;
                $content .= 'namespace '.$item['ns'].';'.PHP_EOL.PHP_EOL;
                switch ($name) {

                    case 'api':
                        $content .= 'class '.$item['class'].' extends \admin\ngrest\base\Api'.PHP_EOL;
                        $content .= '{'.PHP_EOL;
                        $content .= '    public $modelClass = \''.$modelNs.'\';'.PHP_EOL;
                        $content .= '}';
                        break;

                    case 'controller':
                        $content .= 'class '.$item['class'].' extends \admin\ngrest\base\Controller'.PHP_EOL;
                        $content .= '{'.PHP_EOL;
                        $content .= '    public $modelClass = \''.$modelNs.'\';'.PHP_EOL;
                        $content .= '}';
                        break;

                    case 'model':

                        $names = [];
                        $ngrest = [
                            'text' => [],
                            'textarea' => [],
                        ];
                        foreach ($shema->columns as $k => $v) {
                            if ($v->phpType == 'string') {
                                $names[] = $v->name;
                                if ($v->type == 'text') {
                                    $ngrest['textarea'][] = $v->name;
                                }
                                if ($v->type == 'string') {
                                    $ngrest['text'][] = $v->name;
                                }
                            }
                        }

                        $content .= 'class '.$item['class'].' extends \admin\ngrest\base\Model'.PHP_EOL;
                        $content .= '{'.PHP_EOL;
                        $content .= '    /* yii model properties */'.PHP_EOL.PHP_EOL;
                        $content .= '    public static function tableName()'.PHP_EOL;
                        $content .= '    {'.PHP_EOL;
                        $content .= '        return \''.$sqlTable.'\';'.PHP_EOL;
                        $content .= '    }'.PHP_EOL.PHP_EOL;
                        $content .= '    public function rules()'.PHP_EOL;
                        $content .= '    {'.PHP_EOL;
                        $content .= '        return ['.PHP_EOL;
                        $content .= '            [[\''.implode("', '", $names).'\'], \'required\'],'.PHP_EOL;
                        $content .= '        ];'.PHP_EOL;
                        $content .= '    }'.PHP_EOL.PHP_EOL;
                        $content .= '    public function scenarios()'.PHP_EOL;
                        $content .= '    {'.PHP_EOL;
                        $content .= '        return ['.PHP_EOL;
                        $content .= '            \'restcreate\' => [\''.implode("', '", $names).'\'],'.PHP_EOL;http://luya.io/
                        $content .= '            \'restupdate\' => [\''.implode("', '", $names).'\'],'.PHP_EOL;
                        $content .= '        ];'.PHP_EOL;
                        $content .= '    }'.PHP_EOL.PHP_EOL;
                        $content .= '    /* ngrest model properties */'.PHP_EOL.PHP_EOL;
                        $content .= '    public function genericSearchFields()'.PHP_EOL;
                        $content .= '    {'.PHP_EOL;
                        $content .= '        return [\''.implode("', '", $names).'\'];'.PHP_EOL;
                        $content .= '    }'.PHP_EOL.PHP_EOL;
                        $content .= '    public function ngRestApiEndpoint()'.PHP_EOL;
                        $content .= '    {'.PHP_EOL;
                        $content .= '        return \''.$apiEndpoint.'\';'.PHP_EOL;
                        $content .= '    }'.PHP_EOL.PHP_EOL;
                        $content .= '    public function ngRestConfig($config)'.PHP_EOL;
                        $content .= '    {'.PHP_EOL;
                        foreach ($ngrest['text'] as $n) {
                            $content .= '        $config->list->field(\''.$n.'\', \''.ucfirst($n).'\')->text();'.PHP_EOL;
                        }
                        foreach ($ngrest['textarea'] as $n) {
                            $content .= '        $config->list->field(\''.$n.'\', \''.ucfirst($n).'\')->textarea();'.PHP_EOL;
                        }
                        $content .= '        $config->create->copyFrom(\'list\');'.PHP_EOL;
                        $content .= '        $config->update->copyFrom(\'list\');'.PHP_EOL;
                        $content .= '        return $config;'.PHP_EOL;
                        $content .= '    }'.PHP_EOL;
                        $content .= '}';
                        break;
                }<commit_msg>add all fields to scenarios in crud/create command.<commit_after><?php

namespace luya\commands;

use Yii;
use yii\helpers\Console;

class CrudController extends \yii\console\Controller
{
    public function actionCreate()
    {
        $module = $this->prompt('Module Name (e.g. galleryadmin):');
        $modulePre = $new_str = preg_replace('/admin$/', '', $module);
        $modelName = $this->prompt('Model Name (e.g. Album)');
        $apiEndpoint = $this->prompt('Api Endpoint (e.g. api-'.$modulePre.'-'.strtolower($modelName).')');
        $sqlTable = $this->prompt('Database Table name (e.g. '.strtolower($modulePre).'_'.strtolower($modelName).')');

        if (!$this->confirm("Create '$modelName' controller, api & model based on sql table '$sqlTable' in module '$module' for api endpoint '$apiEndpoint'?")) {
            exit(1);
        }

        $shema = Yii::$app->db->getTableSchema($sqlTable);

        if (!$shema) {
            echo 'you have to create a migration script and execute the migration first. the table must exists!';
            exit(1);
        }

        $yiiModule = Yii::$app->getModule($module);

        $basePath = $yiiModule->basePath;

        $ns = $yiiModule->getNamespace();

        $modelName = ucfirst($modelName);

        $modelNs = '\\'.$ns.'\\models\\'.$modelName;
        $data = [
            'api' => [
                'folder' => 'apis',
                'ns' => $ns.'\\apis',
                'file' => $modelName.'Controller.php',
                'class' => $modelName.'Controller',
                'route' => strtolower($module).'-'.strtolower($modelName).'-index',
            ],
            'controller' => [
                'folder' => 'controllers',
                'ns' => $ns.'\\controllers',
                'file' => $modelName.'Controller.php',
                'class' => $modelName.'Controller',
            ],
            'model' => [
                'folder' => 'models',
                'ns' => $ns.'\\models',
                'file' => $modelName.'.php',
                'class' => $modelName,
            ],
        ];
        $apiClass = null;
        foreach ($data as $name => $item) {
            $folder = $basePath.DIRECTORY_SEPARATOR.$item['folder'];

            if (!file_exists($folder)) {
                mkdir($folder);
            }

            if (file_exists($folder.DIRECTORY_SEPARATOR.$item['file'])) {
                echo $this->ansiFormat("Can not create $folder".DIRECTORY_SEPARATOR.$item['file'].', file does already exists!', Console::FG_RED).PHP_EOL;
            } else {
                $content = '<?php'.PHP_EOL.PHP_EOL;
                $content .= 'namespace '.$item['ns'].';'.PHP_EOL.PHP_EOL;
                switch ($name) {

                    case 'api':
                        $content .= 'class '.$item['class'].' extends \admin\ngrest\base\Api'.PHP_EOL;
                        $content .= '{'.PHP_EOL;
                        $content .= '    public $modelClass = \''.$modelNs.'\';'.PHP_EOL;
                        $content .= '}';
                        break;

                    case 'controller':
                        $content .= 'class '.$item['class'].' extends \admin\ngrest\base\Controller'.PHP_EOL;
                        $content .= '{'.PHP_EOL;
                        $content .= '    public $modelClass = \''.$modelNs.'\';'.PHP_EOL;
                        $content .= '}';
                        break;

                    case 'model':

                        $names = [];
                        $allfields = [];
                        $ngrest = [
                            'text' => [],
                            'textarea' => [],
                        ];
                        foreach ($shema->columns as $k => $v) {
                            $allfields[] = $v->name;
                            if ($v->phpType == 'string') {
                                $names[] = $v->name;
                                if ($v->type == 'text') {
                                    $ngrest['textarea'][] = $v->name;
                                }
                                if ($v->type == 'string') {
                                    $ngrest['text'][] = $v->name;
                                }
                            }
                        }

                        $content .= 'class '.$item['class'].' extends \admin\ngrest\base\Model'.PHP_EOL;
                        $content .= '{'.PHP_EOL;
                        $content .= '    /* yii model properties */'.PHP_EOL.PHP_EOL;
                        $content .= '    public static function tableName()'.PHP_EOL;
                        $content .= '    {'.PHP_EOL;
                        $content .= '        return \''.$sqlTable.'\';'.PHP_EOL;
                        $content .= '    }'.PHP_EOL.PHP_EOL;
                        $content .= '    public function rules()'.PHP_EOL;
                        $content .= '    {'.PHP_EOL;
                        $content .= '        return ['.PHP_EOL;
                        $content .= '            [[\''.implode("', '", $names).'\'], \'required\'],'.PHP_EOL;
                        $content .= '        ];'.PHP_EOL;
                        $content .= '    }'.PHP_EOL.PHP_EOL;
                        $content .= '    public function scenarios()'.PHP_EOL;
                        $content .= '    {'.PHP_EOL;
                        $content .= '        return ['.PHP_EOL;
                        $content .= '            \'restcreate\' => [\''.implode("', '", $allfields).'\'],'.PHP_EOL;
                        $content .= '            \'restupdate\' => [\''.implode("', '", $allfields).'\'],'.PHP_EOL;
                        $content .= '        ];'.PHP_EOL;
                        $content .= '    }'.PHP_EOL.PHP_EOL;
                        $content .= '    /* ngrest model properties */'.PHP_EOL.PHP_EOL;
                        $content .= '    public function genericSearchFields()'.PHP_EOL;
                        $content .= '    {'.PHP_EOL;
                        $content .= '        return [\''.implode("', '", $names).'\'];'.PHP_EOL;
                        $content .= '    }'.PHP_EOL.PHP_EOL;
                        $content .= '    public function ngRestApiEndpoint()'.PHP_EOL;
                        $content .= '    {'.PHP_EOL;
                        $content .= '        return \''.$apiEndpoint.'\';'.PHP_EOL;
                        $content .= '    }'.PHP_EOL.PHP_EOL;
                        $content .= '    public function ngRestConfig($config)'.PHP_EOL;
                        $content .= '    {'.PHP_EOL;
                        foreach ($ngrest['text'] as $n) {
                            $content .= '        $config->list->field(\''.$n.'\', \''.ucfirst($n).'\')->text();'.PHP_EOL;
                        }
                        foreach ($ngrest['textarea'] as $n) {
                            $content .= '        $config->list->field(\''.$n.'\', \''.ucfirst($n).'\')->textarea();'.PHP_EOL;
                        }
                        $content .= '        $config->create->copyFrom(\'list\');'.PHP_EOL;
                        $content .= '        $config->update->copyFrom(\'list\');'.PHP_EOL;
                        $content .= '        return $config;'.PHP_EOL;
                        $content .= '    }'.PHP_EOL;
                        $content .= '}';
                        break;
                }
{'fraction_non_alphanumeric': 0.17276243468819977, 'fraction_numerical': 0.0002714256632964647, 'mean_word_length': 1.1294610605403843, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 104, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '102844', 'n_tokens_mistral': 4094, 'n_tokens_neox': 3878, 'n_words': 668}