Skip to content

Commit 289dc43

Browse files
author
Christoph Erdmann
committed
Merge remote-tracking branch 'origin/develop'
2 parents e98dba3 + 4da9b48 commit 289dc43

File tree

12 files changed

+943
-1
lines changed

12 files changed

+943
-1
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,36 @@ The following widgets are currently available:
6363
* ActiveForm
6464
* ActiveField
6565
* SwitchButton
66+
* GridView with ActionColumn
67+
* DetailView
6668

6769
These widgets are planned for development:
6870

69-
* GridView with ActionColumn and DataColumn
7071
* Collection
7172
* Pagination
7273
* Modal
7374
* Toast
7475
* Collapsible
76+
77+
## Gii Support
78+
79+
If you are creating your CRUD controller and view files using Gii you can get materialized view files by integrating the adapted Gii templates.
80+
81+
```php
82+
// @app/config/main-local.php
83+
84+
$config['modules']['gii'] = [
85+
'class' => 'yii\gii\Module',
86+
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*', '192.168.178.20'],
87+
'generators' => [
88+
'crud' => [
89+
'class' => 'yii\gii\generators\crud\Generator',
90+
'templates' => [ // setting materializecss templates
91+
'materializecss' => '@vendor/macgyer/yii2-materializecss/src/gii-templates/generators/crud/materializecss',
92+
]
93+
]
94+
],
95+
];
96+
```
97+
98+
You can copy those templates to any location you wish for further customization. Make sure you adapt the path accordingly in your config.
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
/**
3+
* This is the template for generating a CRUD controller class file.
4+
*/
5+
6+
use yii\db\ActiveRecordInterface;
7+
use yii\helpers\StringHelper;
8+
9+
10+
/* @var $this yii\web\View */
11+
/* @var $generator yii\gii\generators\crud\Generator */
12+
13+
$controllerClass = StringHelper::basename($generator->controllerClass);
14+
$modelClass = StringHelper::basename($generator->modelClass);
15+
$searchModelClass = StringHelper::basename($generator->searchModelClass);
16+
if ($modelClass === $searchModelClass) {
17+
$searchModelAlias = $searchModelClass . 'Search';
18+
}
19+
20+
/* @var $class ActiveRecordInterface */
21+
$class = $generator->modelClass;
22+
$pks = $class::primaryKey();
23+
$urlParams = $generator->generateUrlParams();
24+
$actionParams = $generator->generateActionParams();
25+
$actionParamComments = $generator->generateActionParamComments();
26+
27+
echo "<?php\n";
28+
?>
29+
30+
namespace <?= StringHelper::dirname(ltrim($generator->controllerClass, '\\')) ?>;
31+
32+
use Yii;
33+
use <?= ltrim($generator->modelClass, '\\') ?>;
34+
<?php if (!empty($generator->searchModelClass)): ?>
35+
use <?= ltrim($generator->searchModelClass, '\\') . (isset($searchModelAlias) ? " as $searchModelAlias" : "") ?>;
36+
<?php else: ?>
37+
use yii\data\ActiveDataProvider;
38+
<?php endif; ?>
39+
use <?= ltrim($generator->baseControllerClass, '\\') ?>;
40+
use yii\web\NotFoundHttpException;
41+
use yii\filters\VerbFilter;
42+
43+
/**
44+
* <?= $controllerClass ?> implements the CRUD actions for <?= $modelClass ?> model.
45+
*/
46+
class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->baseControllerClass) . "\n" ?>
47+
{
48+
/**
49+
* @inheritdoc
50+
*/
51+
public function behaviors()
52+
{
53+
return [
54+
'verbs' => [
55+
'class' => VerbFilter::className(),
56+
'actions' => [
57+
'delete' => ['POST'],
58+
],
59+
],
60+
];
61+
}
62+
63+
/**
64+
* Lists all <?= $modelClass ?> models.
65+
* @return mixed
66+
*/
67+
public function actionIndex()
68+
{
69+
<?php if (!empty($generator->searchModelClass)): ?>
70+
$searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>();
71+
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
72+
73+
return $this->render('index', [
74+
'searchModel' => $searchModel,
75+
'dataProvider' => $dataProvider,
76+
]);
77+
<?php else: ?>
78+
$dataProvider = new ActiveDataProvider([
79+
'query' => <?= $modelClass ?>::find(),
80+
]);
81+
82+
return $this->render('index', [
83+
'dataProvider' => $dataProvider,
84+
]);
85+
<?php endif; ?>
86+
}
87+
88+
/**
89+
* Displays a single <?= $modelClass ?> model.
90+
* <?= implode("\n * ", $actionParamComments) . "\n" ?>
91+
* @return mixed
92+
*/
93+
public function actionView(<?= $actionParams ?>)
94+
{
95+
return $this->render('view', [
96+
'model' => $this->findModel(<?= $actionParams ?>),
97+
]);
98+
}
99+
100+
/**
101+
* Creates a new <?= $modelClass ?> model.
102+
* If creation is successful, the browser will be redirected to the 'view' page.
103+
* @return mixed
104+
*/
105+
public function actionCreate()
106+
{
107+
$model = new <?= $modelClass ?>();
108+
109+
if ($model->load(Yii::$app->request->post()) && $model->save()) {
110+
return $this->redirect(['view', <?= $urlParams ?>]);
111+
} else {
112+
return $this->render('create', [
113+
'model' => $model,
114+
]);
115+
}
116+
}
117+
118+
/**
119+
* Updates an existing <?= $modelClass ?> model.
120+
* If update is successful, the browser will be redirected to the 'view' page.
121+
* <?= implode("\n * ", $actionParamComments) . "\n" ?>
122+
* @return mixed
123+
*/
124+
public function actionUpdate(<?= $actionParams ?>)
125+
{
126+
$model = $this->findModel(<?= $actionParams ?>);
127+
128+
if ($model->load(Yii::$app->request->post()) && $model->save()) {
129+
return $this->redirect(['view', <?= $urlParams ?>]);
130+
} else {
131+
return $this->render('update', [
132+
'model' => $model,
133+
]);
134+
}
135+
}
136+
137+
/**
138+
* Deletes an existing <?= $modelClass ?> model.
139+
* If deletion is successful, the browser will be redirected to the 'index' page.
140+
* <?= implode("\n * ", $actionParamComments) . "\n" ?>
141+
* @return mixed
142+
*/
143+
public function actionDelete(<?= $actionParams ?>)
144+
{
145+
$this->findModel(<?= $actionParams ?>)->delete();
146+
147+
return $this->redirect(['index']);
148+
}
149+
150+
/**
151+
* Finds the <?= $modelClass ?> model based on its primary key value.
152+
* If the model is not found, a 404 HTTP exception will be thrown.
153+
* <?= implode("\n * ", $actionParamComments) . "\n" ?>
154+
* @return <?= $modelClass ?> the loaded model
155+
* @throws NotFoundHttpException if the model cannot be found
156+
*/
157+
protected function findModel(<?= $actionParams ?>)
158+
{
159+
<?php
160+
if (count($pks) === 1) {
161+
$condition = '$id';
162+
} else {
163+
$condition = [];
164+
foreach ($pks as $pk) {
165+
$condition[] = "'$pk' => \$$pk";
166+
}
167+
$condition = '[' . implode(', ', $condition) . ']';
168+
}
169+
?>
170+
if (($model = <?= $modelClass ?>::findOne(<?= $condition ?>)) !== null) {
171+
return $model;
172+
} else {
173+
throw new NotFoundHttpException('The requested page does not exist.');
174+
}
175+
}
176+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* This is the template for generating CRUD search class of the specified model.
4+
*/
5+
6+
use yii\helpers\StringHelper;
7+
8+
9+
/* @var $this yii\web\View */
10+
/* @var $generator yii\gii\generators\crud\Generator */
11+
12+
$modelClass = StringHelper::basename($generator->modelClass);
13+
$searchModelClass = StringHelper::basename($generator->searchModelClass);
14+
if ($modelClass === $searchModelClass) {
15+
$modelAlias = $modelClass . 'Model';
16+
}
17+
$rules = $generator->generateSearchRules();
18+
$labels = $generator->generateSearchLabels();
19+
$searchAttributes = $generator->getSearchAttributes();
20+
$searchConditions = $generator->generateSearchConditions();
21+
22+
echo "<?php\n";
23+
?>
24+
25+
namespace <?= StringHelper::dirname(ltrim($generator->searchModelClass, '\\')) ?>;
26+
27+
use Yii;
28+
use yii\base\Model;
29+
use yii\data\ActiveDataProvider;
30+
use <?= ltrim($generator->modelClass, '\\') . (isset($modelAlias) ? " as $modelAlias" : "") ?>;
31+
32+
/**
33+
* <?= $searchModelClass ?> represents the model behind the search form about `<?= $generator->modelClass ?>`.
34+
*/
35+
class <?= $searchModelClass ?> extends <?= isset($modelAlias) ? $modelAlias : $modelClass ?>
36+
37+
{
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function rules()
42+
{
43+
return [
44+
<?= implode(",\n ", $rules) ?>,
45+
];
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
public function scenarios()
52+
{
53+
// bypass scenarios() implementation in the parent class
54+
return Model::scenarios();
55+
}
56+
57+
/**
58+
* Creates data provider instance with search query applied
59+
*
60+
* @param array $params
61+
*
62+
* @return ActiveDataProvider
63+
*/
64+
public function search($params)
65+
{
66+
$query = <?= isset($modelAlias) ? $modelAlias : $modelClass ?>::find();
67+
68+
// add conditions that should always apply here
69+
70+
$dataProvider = new ActiveDataProvider([
71+
'query' => $query,
72+
]);
73+
74+
$this->load($params);
75+
76+
if (!$this->validate()) {
77+
// uncomment the following line if you do not want to return any records when validation fails
78+
// $query->where('0=1');
79+
return $dataProvider;
80+
}
81+
82+
// grid filtering conditions
83+
<?= implode("\n ", $searchConditions) ?>
84+
85+
return $dataProvider;
86+
}
87+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use yii\helpers\Inflector;
4+
use yii\helpers\StringHelper;
5+
6+
/* @var $this yii\web\View */
7+
/* @var $generator yii\gii\generators\crud\Generator */
8+
9+
/* @var $model \yii\db\ActiveRecord */
10+
$model = new $generator->modelClass();
11+
$safeAttributes = $model->safeAttributes();
12+
if (empty($safeAttributes)) {
13+
$safeAttributes = $model->attributes();
14+
}
15+
16+
echo "<?php\n";
17+
?>
18+
19+
use macgyer\yii2materializecss\lib\Html;
20+
use macgyer\yii2materializecss\widgets\form\ActiveForm;
21+
22+
/* @var $this yii\web\View */
23+
/* @var $model <?= ltrim($generator->modelClass, '\\') ?> */
24+
/* @var $form macgyer\yii2materializecss\widgets\form\ActiveForm */
25+
?>
26+
27+
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-form">
28+
29+
<?= "<?php " ?>$form = ActiveForm::begin(); ?>
30+
31+
<?php foreach ($generator->getColumnNames() as $attribute) {
32+
if (in_array($attribute, $safeAttributes)) {
33+
echo " <?= " . $generator->generateActiveField($attribute) . " ?>\n\n";
34+
}
35+
} ?>
36+
<div class="form-group">
37+
<?= "<?= " ?>Html::submitButton($model->isNewRecord ? <?= $generator->generateString('Create') ?> : <?= $generator->generateString('Update') ?>, ['class' => 'btn']) ?>
38+
</div>
39+
40+
<?= "<?php " ?>ActiveForm::end(); ?>
41+
42+
</div>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use yii\helpers\Inflector;
4+
use yii\helpers\StringHelper;
5+
6+
/* @var $this yii\web\View */
7+
/* @var $generator yii\gii\generators\crud\Generator */
8+
9+
echo "<?php\n";
10+
?>
11+
12+
use macgyer\yii2materializecss\lib\Html;
13+
use macgyer\yii2materializecss\widgets\form\ActiveForm;
14+
15+
/* @var $this yii\web\View */
16+
/* @var $model <?= ltrim($generator->searchModelClass, '\\') ?> */
17+
/* @var $form macgyer\yii2materializecss\widgets\form\ActiveForm */
18+
?>
19+
20+
<div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-search">
21+
22+
<?= "<?php " ?>$form = ActiveForm::begin([
23+
'action' => ['index'],
24+
'method' => 'get',
25+
]); ?>
26+
27+
<?php
28+
$count = 0;
29+
foreach ($generator->getColumnNames() as $attribute) {
30+
if (++$count < 6) {
31+
echo " <?= " . $generator->generateActiveSearchField($attribute) . " ?>\n\n";
32+
} else {
33+
echo " <?php // echo " . $generator->generateActiveSearchField($attribute) . " ?>\n\n";
34+
}
35+
}
36+
?>
37+
<div class="form-group">
38+
<?= "<?= " ?>Html::submitButton(<?= $generator->generateString('Search') ?>, ['class' => 'btn']) ?>
39+
<?= "<?= " ?>Html::resetButton(<?= $generator->generateString('Reset') ?>, ['class' => 'btn']) ?>
40+
</div>
41+
42+
<?= "<?php " ?>ActiveForm::end(); ?>
43+
44+
</div>

0 commit comments

Comments
 (0)