~fusonic/chive/1.1

« back to all changes in this revision

Viewing changes to yii/cli/views/shell/crud/controller.php

  • Committer: Matthias Burtscher
  • Date: 2010-02-12 09:12:35 UTC
  • Revision ID: matthias.burtscher@fusonic.net-20100212091235-jqxrb62klx872ajc
* Updated Yii to 1.1.0
* Removed CodePress and CodeMirror
* Updated jQuery and some plugins
* Cleaned some code ...

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
?>
10
10
<?php echo "<?php\n"; ?>
11
11
 
12
 
class <?php echo $controllerClass; ?> extends CController
 
12
class <?php echo $controllerClass; ?> extends Controller
13
13
{
14
14
        const PAGE_SIZE=10;
15
15
 
16
16
        /**
17
 
         * @var string specifies the default action to be 'list'.
18
 
         */
19
 
        public $defaultAction='list';
20
 
 
21
 
        /**
22
17
         * @var CActiveRecord the currently loaded data model instance.
23
18
         */
24
19
        private $_model;
41
36
        public function accessRules()
42
37
        {
43
38
                return array(
44
 
                        array('allow',  // allow all users to perform 'list' and 'show' actions
45
 
                                'actions'=>array('list','show'),
 
39
                        array('allow',  // allow all users to perform 'index' and 'view' actions
 
40
                                'actions'=>array('index','view'),
46
41
                                'users'=>array('*'),
47
42
                        ),
48
43
                        array('allow', // allow authenticated user to perform 'create' and 'update' actions
60
55
        }
61
56
 
62
57
        /**
63
 
         * Shows a particular model.
 
58
         * Displays a particular model.
64
59
         */
65
 
        public function actionShow()
 
60
        public function actionView()
66
61
        {
67
 
                $this->render('show',array('model'=>$this->load<?php echo $modelClass; ?>()));
 
62
                $this->render('view',array(
 
63
                        'model'=>$this->loadModel(),
 
64
                ));
68
65
        }
69
66
 
70
67
        /**
71
68
         * Creates a new model.
72
 
         * If creation is successful, the browser will be redirected to the 'show' page.
 
69
         * If creation is successful, the browser will be redirected to the 'view' page.
73
70
         */
74
71
        public function actionCreate()
75
72
        {
78
75
                {
79
76
                        $model->attributes=$_POST['<?php echo $modelClass; ?>'];
80
77
                        if($model->save())
81
 
                                $this->redirect(array('show','id'=>$model-><?php echo $ID; ?>));
 
78
                                $this->redirect(array('view','id'=>$model-><?php echo $ID; ?>));
82
79
                }
83
 
                $this->render('create',array('model'=>$model));
 
80
 
 
81
                $this->render('create',array(
 
82
                        'model'=>$model,
 
83
                ));
84
84
        }
85
85
 
86
86
        /**
87
87
         * Updates a particular model.
88
 
         * If update is successful, the browser will be redirected to the 'show' page.
 
88
         * If update is successful, the browser will be redirected to the 'view' page.
89
89
         */
90
90
        public function actionUpdate()
91
91
        {
92
 
                $model=$this->load<?php echo $modelClass; ?>();
 
92
                $model=$this->loadModel();
93
93
                if(isset($_POST['<?php echo $modelClass; ?>']))
94
94
                {
95
95
                        $model->attributes=$_POST['<?php echo $modelClass; ?>'];
96
96
                        if($model->save())
97
 
                                $this->redirect(array('show','id'=>$model-><?php echo $ID; ?>));
 
97
                                $this->redirect(array('view','id'=>$model-><?php echo $ID; ?>));
98
98
                }
99
 
                $this->render('update',array('model'=>$model));
 
99
 
 
100
                $this->render('update',array(
 
101
                        'model'=>$model,
 
102
                ));
100
103
        }
101
104
 
102
105
        /**
103
106
         * Deletes a particular model.
104
 
         * If deletion is successful, the browser will be redirected to the 'list' page.
 
107
         * If deletion is successful, the browser will be redirected to the 'index' page.
105
108
         */
106
109
        public function actionDelete()
107
110
        {
108
111
                if(Yii::app()->request->isPostRequest)
109
112
                {
110
113
                        // we only allow deletion via POST request
111
 
                        $this->load<?php echo $modelClass; ?>()->delete();
112
 
                        $this->redirect(array('list'));
 
114
                        $this->loadModel()->delete();
 
115
 
 
116
                        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
 
117
                        if(!isset($_POST['ajax']))
 
118
                                $this->redirect(array('index'));
113
119
                }
114
120
                else
115
121
                        throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
118
124
        /**
119
125
         * Lists all models.
120
126
         */
121
 
        public function actionList()
 
127
        public function actionIndex()
122
128
        {
123
 
                $criteria=new CDbCriteria;
124
 
 
125
 
                $pages=new CPagination(<?php echo $modelClass; ?>::model()->count($criteria));
126
 
                $pages->pageSize=self::PAGE_SIZE;
127
 
                $pages->applyLimit($criteria);
128
 
 
129
 
                $models=<?php echo $modelClass; ?>::model()->findAll($criteria);
130
 
 
131
 
                $this->render('list',array(
132
 
                        'models'=>$models,
133
 
                        'pages'=>$pages,
 
129
                $dataProvider=new CActiveDataProvider('<?php echo $modelClass; ?>', array(
 
130
                        'pagination'=>array(
 
131
                                'pageSize'=>self::PAGE_SIZE,
 
132
                        ),
 
133
                ));
 
134
 
 
135
                $this->render('index',array(
 
136
                        'dataProvider'=>$dataProvider,
134
137
                ));
135
138
        }
136
139
 
139
142
         */
140
143
        public function actionAdmin()
141
144
        {
142
 
                $this->processAdminCommand();
143
 
 
144
 
                $criteria=new CDbCriteria;
145
 
 
146
 
                $pages=new CPagination(<?php echo $modelClass; ?>::model()->count($criteria));
147
 
                $pages->pageSize=self::PAGE_SIZE;
148
 
                $pages->applyLimit($criteria);
149
 
 
150
 
                $sort=new CSort('<?php echo $modelClass; ?>');
151
 
                $sort->applyOrder($criteria);
152
 
 
153
 
                $models=<?php echo $modelClass; ?>::model()->findAll($criteria);
 
145
                $dataProvider=new CActiveDataProvider('<?php echo $modelClass; ?>', array(
 
146
                        'pagination'=>array(
 
147
                                'pageSize'=>self::PAGE_SIZE,
 
148
                        ),
 
149
                ));
154
150
 
155
151
                $this->render('admin',array(
156
 
                        'models'=>$models,
157
 
                        'pages'=>$pages,
158
 
                        'sort'=>$sort,
 
152
                        'dataProvider'=>$dataProvider,
159
153
                ));
160
154
        }
161
155
 
162
156
        /**
163
157
         * Returns the data model based on the primary key given in the GET variable.
164
158
         * If the data model is not found, an HTTP exception will be raised.
165
 
         * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable
166
159
         */
167
 
        public function load<?php echo $modelClass; ?>($id=null)
 
160
        public function loadModel()
168
161
        {
169
162
                if($this->_model===null)
170
163
                {
171
 
                        if($id!==null || isset($_GET['id']))
172
 
                                $this->_model=<?php echo $modelClass; ?>::model()->findbyPk($id!==null ? $id : $_GET['id']);
 
164
                        if(isset($_GET['id']))
 
165
                                $this->_model=<?php echo $modelClass; ?>::model()->findbyPk($_GET['id']);
173
166
                        if($this->_model===null)
174
167
                                throw new CHttpException(404,'The requested page does not exist.');
175
168
                }
176
169
                return $this->_model;
177
170
        }
178
 
 
179
 
        /**
180
 
         * Executes any command triggered on the admin page.
181
 
         */
182
 
        protected function processAdminCommand()
183
 
        {
184
 
                if(isset($_POST['command'], $_POST['id']) && $_POST['command']==='delete')
185
 
                {
186
 
                        $this->load<?php echo $modelClass; ?>($_POST['id'])->delete();
187
 
                        // reload the current page to avoid duplicated delete actions
188
 
                        $this->refresh();
189
 
                }
190
 
        }
191
171
}