Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2 features :1, bulk-delete ; 2, gii types #17

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
665 changes: 665 additions & 0 deletions Generator.php

Large diffs are not rendered by default.

24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## Enhances:

bulk-del brach.
1. add support more fieldtypes ,such as select2,file upload,checkbox,checkboxList,dropdowlist and so.
2. bulk delete on index page

![image](https://raw.githubusercontent.com/zhangyc310/yii2-kartikgii/bulk-del/images/gii-types.png)

![image](https://raw.githubusercontent.com/zhangyc310/yii2-kartikgii/bulk-del/images/bulk-del.png)
=============

I would like to apologize for not following up with this plugin because i has been migrate over to Laravel. However i would like to thanks everyone for using this plugin. Please do let me know is there any nice fork of this project, i will share to link for everyone here.

yii2-kartikgii
Expand Down Expand Up @@ -25,13 +36,15 @@ The preferred way to install this extension is through [composer](http://getcomp
Either run

```
$ php composer.phar require warrence/yii2-kartikgii "dev-master"
$ php composer.phar require zhangyc310/yii2-kartikgii "bulk-del"
//$ php composer.phar require warrence/yii2-kartikgii "dev-master"
```

or add

```
"warrence/yii2-kartikgii": "dev-master"
"zhangyc310/yii2-kartikgii": "bulk-del"
//"warrence/yii2-kartikgii": "dev-master"
```

to the ```require``` section of your `composer.json` file.
Expand All @@ -48,9 +61,14 @@ $config['modules']['gii']['class'] = 'yii\gii\Module';

```php
//Add this into backend/config/main-local.php
//
$config['modules']['gii']['generators'] = [
'kartikgii-crud' => ['class' => 'warrence\kartikgii\crud\Generator'],
'kartikgii-crud' => ['class' => 'zhangyc310\kartikgii\crud\Generator'],
];

// $config['modules']['gii']['generators'] = [
// 'kartikgii-crud' => ['class' => 'warrence\kartikgii\crud\Generator'],
// ];
```

```php
Expand Down
56 changes: 56 additions & 0 deletions _form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use kartik\widgets\FileInput;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;

/**
* @var yii\web\View $this
* @var yii\gii\generators\crud\Generator $generator
*/

/** @var \yii\db\ActiveRecord $model */
$model = new $generator->modelClass;
$safeAttributes = $model->safeAttributes();
if (empty($safeAttributes)) {
$safeAttributes = $model->attributes();
}

echo "<?php\n";

?>

use yii\helpers\Html;
use kartik\widgets\ActiveForm;
use kartik\builder\Form;
use kartik\datecontrol\DateControl;
use kartik\widgets\FileInput
/**
* @var yii\web\View $this
* @var <?=ltrim($generator->modelClass, '\\');?> $model
* @var yii\widgets\ActiveForm $form
*/
?>

<div class="<?=Inflector::camel2id(StringHelper::basename($generator->modelClass));?>-form">

<?="<?php ";?>$form = ActiveForm::begin(['type' => ActiveForm::TYPE_HORIZONTAL]); echo Form::widget([

'model' => $model,
'form' => $form,
'columns' => 1,
'attributes' => [

<?php foreach ($safeAttributes as $attribute): ?>
<?=$generator->generateActiveField($attribute) . "\n\n";?>
<?php endforeach;?>
]

]);

echo Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'),
['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']
);
ActiveForm::end(); ?>

</div>
48 changes: 48 additions & 0 deletions _search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use yii\helpers\Inflector;
use yii\helpers\StringHelper;

/**
* @var yii\web\View $this
* @var yii\gii\generators\crud\Generator $generator
*/

echo "<?php\n";
?>

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/**
* @var yii\web\View $this
* @var <?= ltrim($generator->searchModelClass, '\\') ?> $model
* @var yii\widgets\ActiveForm $form
*/
?>

<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-search">

<?= "<?php " ?>$form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>

<?php
$count = 0;
foreach ($generator->getColumnNames() as $attribute) {
if (++$count < 6) {
echo " <?= " . $generator->generateActiveSearchField($attribute) . " ?>\n\n";
} else {
echo " <?php // echo " . $generator->generateActiveSearchField($attribute) . " ?>\n\n";
}
}
?>
<div class="form-group">
<?= "<?= " ?>Html::submitButton(<?= $generator->generateString('Search') ?>, ['class' => 'btn btn-primary']) ?>
<?= "<?= " ?>Html::resetButton(<?= $generator->generateString('Reset') ?>, ['class' => 'btn btn-default']) ?>
</div>

<?= "<?php " ?>ActiveForm::end(); ?>

</div>
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "warrence/yii2-kartikgii",
"name": "zhangyc310/yii2-kartikgii",
"description": "Gii Generator base on Kartik-V extension https://github.com/kartik-v",
"authors": [
{

"name": "Warrence",
"name": "zhangyc",

"email": "warrence@gmail.com"
"email": "yqf0215@gmail.com"
}
],
"autoload": {
"psr-4": {
"warrence\\kartikgii\\": ""
"zhangyc310\\kartikgii\\": ""
}
},
"require": {
Expand All @@ -22,6 +22,6 @@
"kartik-v/yii2-helpers": "*",
"kartik-v/yii2-builder": "*",
"kartik-v/yii2-detail-view": "*",
"php": ">=5.3.0"
"php": ">=5.6"
}
}
187 changes: 187 additions & 0 deletions controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

use yii\db\ActiveRecordInterface;
use yii\helpers\StringHelper;

/**
* This is the template for generating a CRUD controller class file.
*
* @var yii\web\View $this
* @var yii\gii\generators\crud\Generator $generator
*/

$controllerClass = StringHelper::basename($generator->controllerClass);
$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);
if ($modelClass === $searchModelClass) {
$searchModelAlias = $searchModelClass . 'Search';
}

/** @var ActiveRecordInterface $class */
$class = $generator->modelClass;
$pks = $class::primaryKey();
$urlParams = $generator->generateUrlParams();
$actionParams = $generator->generateActionParams();
$actionParamComments = $generator->generateActionParamComments();

echo "<?php\n";
?>

namespace <?=StringHelper::dirname(ltrim($generator->controllerClass, '\\'));?>;

use Yii;
use <?=ltrim($generator->modelClass, '\\');?>;
<?php if (!empty($generator->searchModelClass)): ?>
use <?=ltrim($generator->searchModelClass, '\\') . (isset($searchModelAlias) ? " as $searchModelAlias" : "");?>;
<?php else: ?>
use yii\data\ActiveDataProvider;
<?php endif;?>
use <?=ltrim($generator->baseControllerClass, '\\');?>;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
* <?=$controllerClass;?> implements the CRUD actions for <?=$modelClass;?> model.
*/
class <?=$controllerClass;?> extends <?=StringHelper::basename($generator->baseControllerClass) . "\n";?>
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}

/**
* Lists all <?=$modelClass;?> models.
* @return mixed
*/
public function actionIndex()
{
<?php if (!empty($generator->searchModelClass)): ?>
$searchModel = new <?=isset($searchModelAlias) ? $searchModelAlias : $searchModelClass;?>;
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());

return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
<?php else: ?>
$dataProvider = new ActiveDataProvider([
'query' => <?=$modelClass;?>::find(),
]);

return $this->render('index', [
'dataProvider' => $dataProvider,
]);
<?php endif;?>
}

public function actionDeleteAll($id)
{


<?=$modelClass;?>::deleteAll('id in (' . $id . ')');

return $this->redirect(Yii::$app->request->referrer);
}

/**
* Displays a single <?=$modelClass;?> model.
* <?=implode("\n * ", $actionParamComments) . "\n";?>
* @return mixed
*/
public function actionView(<?=$actionParams;?>)
{
$model = $this->findModel(<?=$actionParams;?>);

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model-><?=$generator->getTableSchema()->primaryKey[0];?>]);

} else {
return $this->render('view', ['model' => $model]);
}
}

/**
* Creates a new <?=$modelClass;?> model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new <?=$modelClass;?>;

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', <?=$urlParams;?>]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}

/**
* Updates an existing <?=$modelClass;?> model.
* If update is successful, the browser will be redirected to the 'view' page.
* <?=implode("\n * ", $actionParamComments) . "\n";?>
* @return mixed
*/
public function actionUpdate(<?=$actionParams;?>)
{
$model = $this->findModel(<?=$actionParams;?>);

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}

/**
* Deletes an existing <?=$modelClass;?> model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* <?=implode("\n * ", $actionParamComments) . "\n";?>
* @return mixed
*/
public function actionDelete(<?=$actionParams;?>)
{
$this->findModel(<?=$actionParams;?>)->delete();

return $this->redirect(['index']);
}

/**
* Finds the <?=$modelClass;?> model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* <?=implode("\n * ", $actionParamComments) . "\n";?>
* @return <?=$modelClass;?> the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel(<?=$actionParams;?>)
{
<?php
if (count($pks) === 1) {
$condition = '$id';
} else {
$condition = [];
foreach ($pks as $pk) {
$condition[] = "'$pk' => \$$pk";
}
$condition = '[' . implode(', ', $condition) . ']';
}
?>
if (($model = <?=$modelClass;?>::findOne(<?=$condition;?>)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
Loading