From ae09aa1b2858c8b9be026420218e38bcc495deb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dariusz=20G=C3=B3recki?= Date: Fri, 7 Jan 2011 18:43:16 +0100 Subject: [PATCH] Added withoutAdminView Gii CRUD template withoutAdminView CRUD template provides functionality like normal template but produces merge of admin and index view as index --- .../templates/withoutAdminView/_form.php | 39 ++++ .../templates/withoutAdminView/_search.php | 32 ++++ .../templates/withoutAdminView/controller.php | 173 ++++++++++++++++++ .../templates/withoutAdminView/create.php | 23 +++ .../templates/withoutAdminView/index.php | 87 +++++++++ .../templates/withoutAdminView/update.php | 27 +++ .../templates/withoutAdminView/view.php | 35 ++++ 7 files changed, 416 insertions(+) create mode 100644 gii/mongoCRUD/templates/withoutAdminView/_form.php create mode 100644 gii/mongoCRUD/templates/withoutAdminView/_search.php create mode 100644 gii/mongoCRUD/templates/withoutAdminView/controller.php create mode 100644 gii/mongoCRUD/templates/withoutAdminView/create.php create mode 100644 gii/mongoCRUD/templates/withoutAdminView/index.php create mode 100644 gii/mongoCRUD/templates/withoutAdminView/update.php create mode 100644 gii/mongoCRUD/templates/withoutAdminView/view.php diff --git a/gii/mongoCRUD/templates/withoutAdminView/_form.php b/gii/mongoCRUD/templates/withoutAdminView/_form.php new file mode 100644 index 00000000..64dad62b --- /dev/null +++ b/gii/mongoCRUD/templates/withoutAdminView/_form.php @@ -0,0 +1,39 @@ + +
+ +beginWidget('CActiveForm', array( + 'id'=>'".$this->class2id($this->modelClass)."-form', + 'enableAjaxValidation'=>false, +)); ?>\n"; ?> + +

Fields with * are required.

+ + errorSummary(\$model); ?>\n"; ?> + +modelObject->attributeNames() as $name) +{ + if($name == '_id') + continue; +?> +
+ generateActiveLabel($this->modelClass,$name)."; ?>\n"; ?> + generateActiveField($this->modelClass,$name)."; ?>\n"; ?> + error(\$model,'{$name}'); ?>\n"; ?> +
+ + +
+ isNewRecord ? 'Create' : 'Save'); ?>\n"; ?> +
+ +endWidget(); ?>\n"; ?> + +
\ No newline at end of file diff --git a/gii/mongoCRUD/templates/withoutAdminView/_search.php b/gii/mongoCRUD/templates/withoutAdminView/_search.php new file mode 100644 index 00000000..39ddfb89 --- /dev/null +++ b/gii/mongoCRUD/templates/withoutAdminView/_search.php @@ -0,0 +1,32 @@ + +
+ +beginWidget('CActiveForm', array( + 'action'=>Yii::app()->createUrl(\$this->route), + 'method'=>'get', +)); ?>\n"; ?> + +modelObject->attributeNames() as $name): ?> +generateInputField($this->modelClass,$name); + if(strpos($field,'password')!==false) + continue; +?> +
+ label(\$model,'{$name}'); ?>\n"; ?> + generateActiveField($this->modelClass,$name)."; ?>\n"; ?> +
+ + +
+ \n"; ?> +
+ +endWidget(); ?>\n"; ?> + +
\ No newline at end of file diff --git a/gii/mongoCRUD/templates/withoutAdminView/controller.php b/gii/mongoCRUD/templates/withoutAdminView/controller.php new file mode 100644 index 00000000..324afba8 --- /dev/null +++ b/gii/mongoCRUD/templates/withoutAdminView/controller.php @@ -0,0 +1,173 @@ + + + +class controllerClass; ?> extends baseControllerClass."\n"; ?> +{ + /** + * @var string the default layout for the views. Defaults to '//layouts/column2', meaning + * using two-column layout. See 'protected/views/layouts/column2.php'. + */ + public $layout='//layouts/column2'; + + /** + * @return array action filters + */ + public function filters() + { + return array( + 'accessControl', // perform access control for CRUD operations + ); + } + + /** + * Specifies the access control rules. + * This method is used by the 'accessControl' filter. + * @return array access control rules + */ + public function accessRules() + { + return array( + array('allow', // allow all users to perform 'index' and 'view' actions + 'actions'=>array('index','view'), + 'users'=>array('*'), + ), + array('allow', // allow authenticated user to perform 'create' and 'update' actions + 'actions'=>array('create','update'), + 'users'=>array('@'), + ), + array('allow', // allow admin user to perform 'admin' and 'delete' actions + 'actions'=>array('admin','delete'), + 'users'=>array('admin'), + ), + array('deny', // deny all users + 'users'=>array('*'), + ), + ); + } + + /** + * Displays a particular model. + * @param integer $id the ID of the model to be displayed + */ + public function actionView($id) + { + $this->render('view',array( + 'model'=>$this->loadModel($id), + )); + } + + /** + * Creates a new model. + * If creation is successful, the browser will be redirected to the 'view' page. + */ + public function actionCreate() + { + $model=new modelClass; ?>; + + // Uncomment the following line if AJAX validation is needed + // $this->performAjaxValidation($model); + + if(isset($_POST['modelClass; ?>'])) + { + $model->attributes=$_POST['modelClass; ?>']; + if($model->save()) + $this->redirect(array('view','id'=>$model->modelObject->primaryKey(); ?>)); + } + + $this->render('create',array( + 'model'=>$model, + )); + } + + /** + * Updates a particular model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id the ID of the model to be updated + */ + public function actionUpdate($id) + { + $model=$this->loadModel($id); + + // Uncomment the following line if AJAX validation is needed + // $this->performAjaxValidation($model); + + if(isset($_POST['modelClass; ?>'])) + { + $model->attributes=$_POST['modelClass; ?>']; + if($model->save()) + $this->redirect(array('view','id'=>$model->modelObject->primaryKey(); ?>)); + } + + $this->render('update',array( + 'model'=>$model, + )); + } + + /** + * Deletes a particular model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id the ID of the model to be deleted + */ + public function actionDelete($id) + { + if(Yii::app()->request->isPostRequest) + { + // we only allow deletion via POST request + $this->loadModel($id)->delete(); + + // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser + if(!isset($_GET['ajax'])) + $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index')); + } + else + throw new CHttpException(400,'Invalid request. Please do not repeat this request again.'); + } + + /** + * Manages all models. + */ + public function actionIndex() + { + $model = new modelClass; ?>('search'); + $model->unsetAttributes(); + + if(isset($_GET['modelClass; ?>'])) + $model->setAttributes($_GET['modelClass; ?>']); + + $this->render('index', array( + 'model'=>$model + )); + } + + /** + * Returns the data model based on the primary key given in the GET variable. + * If the data model is not found, an HTTP exception will be raised. + * @param integer the ID of the model to be loaded + */ + public function loadModel($id) + { + $model=modelClass; ?>::model()->findByPk(modelObject->primaryKey() === '_id') ? 'new MongoId($id)' : '$id'; ?>); + if($model===null) + throw new CHttpException(404,'The requested page does not exist.'); + return $model; + } + + /** + * Performs the AJAX validation. + * @param CModel the model to be validated + */ + protected function performAjaxValidation($model) + { + if(isset($_POST['ajax']) && $_POST['ajax']==='class2id($this->modelClass); ?>-form') + { + echo CActiveForm::validate($model); + Yii::app()->end(); + } + } +} diff --git a/gii/mongoCRUD/templates/withoutAdminView/create.php b/gii/mongoCRUD/templates/withoutAdminView/create.php new file mode 100644 index 00000000..b74969ac --- /dev/null +++ b/gii/mongoCRUD/templates/withoutAdminView/create.php @@ -0,0 +1,23 @@ + +pluralize($this->class2name($this->modelClass)); +echo "\$this->breadcrumbs=array( + '$label'=>array('index'), + 'Create', +);\n"; +?> + +$this->menu=array( + array('label'=>'Manage modelClass; ?>', 'url'=>array('index')), +); +?> + +

Create modelClass; ?>

+ +renderPartial('_form', array('model'=>\$model)); ?>"; ?> \ No newline at end of file diff --git a/gii/mongoCRUD/templates/withoutAdminView/index.php b/gii/mongoCRUD/templates/withoutAdminView/index.php new file mode 100644 index 00000000..c6475c3e --- /dev/null +++ b/gii/mongoCRUD/templates/withoutAdminView/index.php @@ -0,0 +1,87 @@ + +pluralize($this->class2name($this->modelClass)); +echo "\$this->breadcrumbs=array( + '$label'=>array('index'), + 'Manage', +);\n"; +?> + +$this->menu=array( + array('label'=>'List modelClass; ?>', 'url'=>array('index')), + array('label'=>'Create modelClass; ?>', 'url'=>array('create')), +); + +Yii::app()->clientScript->registerScript('search', " +$('.search-button').click(function(){ + $('.search-form').toggle(); + return false; +}); +$('.search-form form').submit(function(){ + $.fn.yiiGridView.update('class2id($this->modelClass); ?>-grid', { + data: $(this).serialize() + }); + return false; +}); +"); + +?> + +

Manage pluralize($this->class2name($this->modelClass)); ?>

+ +

+You may optionally enter a comparison operator (<, <=, >, >=, <> +or =) at the beginning of each of your search values to specify how the comparison should be done. +

+ +'search-button')); ?>"; ?> + + + + $this->widget('zii.widgets.grid.CGridView', array( + 'id'=>'class2id($this->modelClass); ?>-grid', + 'dataProvider'=>new EMongoDocumentDataProvider($model->search(), array( + 'sort'=>array( + 'attributes'=>array( +modelObject->attributeNames() as $name) +{ + if(++$count==7) + echo "\t\t\t\t/*\n"; + echo "\t\t\t\t'".$name."',\n"; +} +if($count>=7) + echo "\t\t\t\t*/\n"; +?> + ), + ), + )), + 'filter'=>$model, + 'columns'=>array( +modelObject->attributeNames() as $name) +{ + if(++$count==7) + echo "\t\t/*\n"; + echo "\t\t'".$name."',\n"; +} +if($count>=7) + echo "\t\t*/\n"; +?> + array( + 'class'=>'CButtonColumn', + ), + ), +)); ?> \ No newline at end of file diff --git a/gii/mongoCRUD/templates/withoutAdminView/update.php b/gii/mongoCRUD/templates/withoutAdminView/update.php new file mode 100644 index 00000000..5660c786 --- /dev/null +++ b/gii/mongoCRUD/templates/withoutAdminView/update.php @@ -0,0 +1,27 @@ + +guessNameColumn($this->modelObject); +$label=$this->pluralize($this->class2name($this->modelClass)); +echo "\$this->breadcrumbs=array( + '$label'=>array('index'), + \$model->{$nameColumn}=>array('view','id'=>\$model->{$this->modelObject->primaryKey()}), + 'Update', +);\n"; +?> + +$this->menu=array( + array('label'=>'Manage modelClass; ?>', 'url'=>array('index')), + array('label'=>'Create modelClass; ?>', 'url'=>array('create')), + array('label'=>'View modelClass; ?>', 'url'=>array('view', 'id'=>$model->modelObject->primaryKey(); ?>)), +); +?> + +

Update modelClass." {$this->modelObject->primaryKey()}; ?>"; ?>

+ +renderPartial('_form', array('model'=>\$model)); ?>"; ?> \ No newline at end of file diff --git a/gii/mongoCRUD/templates/withoutAdminView/view.php b/gii/mongoCRUD/templates/withoutAdminView/view.php new file mode 100644 index 00000000..003a82d8 --- /dev/null +++ b/gii/mongoCRUD/templates/withoutAdminView/view.php @@ -0,0 +1,35 @@ + +guessNameColumn($this->modelObject); +$label=$this->pluralize($this->class2name($this->modelClass)); +echo "\$this->breadcrumbs=array( + '$label'=>array('index'), + \$model->{$nameColumn}, +);\n"; +?> + +$this->menu=array( + array('label'=>'Manage modelClass; ?>', 'url'=>array('index')), + array('label'=>'Create modelClass; ?>', 'url'=>array('create')), + array('label'=>'Update modelClass; ?>', 'url'=>array('update', 'id'=>$model->modelObject->primaryKey(); ?>)), + array('label'=>'Delete modelClass; ?>', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->modelObject->primaryKey(); ?>),'confirm'=>'Are you sure you want to delete this item?')), +); +?> + +

View modelClass." #{$this->modelObject->primaryKey()}; ?>"; ?>

+ + $this->widget('zii.widgets.CDetailView', array( + 'data'=>$model, + 'attributes'=>array( +modelObject->attributeNames() as $name) + echo "\t\t'".$name."',\n"; +?> + ), +)); ?> \ No newline at end of file