OSDN Git Service

Collectionableプラグイン導入
[trpgtools-onweb/cake-frame.git] / app / plugins / collectionable / README.md
1 # Collectionable Plugin #
2
3 ## Introduction ##
4 This is a utility plugin for CakePHP. This helps managing find options, virtualFields and validations.
5
6 ## Setup ##
7 - Define $options(such a property name can be modified by configure) for Options Behavior
8 - Define $virtualFieldsCollection(such a property name can be modified by configure) for VirtualFields Behavior
9 - Define 'Validation'(such a config name can be modified by configure) section into Configure for ConfigValidationBehavior
10 - Define $validate{PatternName}, like $validateAdd, same structure with $validate, for MultiValidationBehavior
11
12 ## Sample code ##
13
14 ### OptionsBehavior ###
15
16 Here is a simple Post Model.
17         class Post extends AppModel {
18                 var $hasMany = array('Comment');
19                 var $hasOne = array('Status');
20
21                 var $acsAs = array('Collectionable.options');
22                 var $defaultOption = true; // or string like 'default'
23
24                 var $options =array(
25                         'default' => array(
26                                 'contain' => array(
27                                         'Comment',
28                                         'Status',
29                                 ),
30                         'limit' => 10,
31                         ),
32                         'published' => array(
33                                 'condtiions' => array('Status.published' => true),
34                         ),
35                         'recent' => array(
36                                 'order' => ('Post.updated DESC'),
37                         ),
38                         'rss' => array(
39                                 'limit' => 15,
40                         ),
41                         'unlimited' => array(
42                                 'limit' => null,
43                         ),
44                         'index' => array(
45                                 // You can do sub merging
46                                 'options' => array(
47                                         'published',
48                                         'recent',
49                                 ),
50                         ),
51                 );
52         }
53
54 You can use them by like:
55         class PostsController extends AppController {
56                 function index() {
57                         $this->paginate = $this->Post->options('index');
58                         $this->set('posts', $this->paginate());
59                 }
60
61                 function rss() {
62                         $this->paginate = $this->Post->options('index', 'rss'); // multiple merging at run time;
63                         $this->set('posts', $this->paginate());
64                 }
65
66                 function all_in_one_page() {
67                         // you can use "options" attribute wihtin finding options
68                         $posts = $this->Post->find('all', array('options' => array('index', 'unlimited')));
69                         $this->set(compact('posts'));
70                 }
71         }
72
73 To see more syntax, you would look at [the test case](http://github.com/hiromi2424/Collectionable/blob/master/tests/cases/behaviors/options.test.php) or [the code](http://github.com/hiromi2424/Collectionable/blob/master/models/behaviors/options.php).
74
75 ### VirtualFieldsBehavior ###
76
77 This sample uses [MatchableBehavior](http://github.com/hiromi2424/MatchableBehavior).
78
79         class User extends AppModel {
80                 var $hasMany = array('Post');
81                 var $actsAs = array('Collectionable.VirtualFields', 'Matchable');
82
83                 var $virtualFields = array(
84                         'full_name' => "CONCAT(User.first_name, ' ', User.last_name)",
85                 );
86                 var $virtualFieldsCollection = array(
87                         'posts_count' => 'COUNT(Post.id)',
88                         'amount_used' => 'SUM(Post.volume)',
89                 );
90         }
91
92 You can use them by like:
93
94
95         class UsersController extends AppController {
96                 function admin_index() {
97                         // Hey, you may feel like using OptionsBehavior :P
98                         $jointo = array('Post');
99                         $group = 'User.id';
100                         $virtualFields = array('posts_count', 'amount_used'); // key of collections
101                         $this->paginate = compact('jointo', 'group', 'virtualFields');
102                         $this->set('users', $this->paginate());
103                 }
104
105                 function profile() {
106                         $virtualFields = array('full_name' => false); // disable virtualFields
107                         $user = $this->User->find('first', compact('virtualFields'));
108                         $this->set(compact('user'));
109                 }
110         }
111
112 ### ConfigValidationBehavior ###
113
114
115         class User extends AppModel {
116                 var $actsAs = array('Collectionable.ConfigValidation');
117
118                 var $validate = array(
119                         'nickname' => array(
120                                 'required' => array(
121                                         'rule' => array('notempty'),
122                                 ),
123                                 'min' => array(
124                                         'rule' => array('minlength'),
125                                         'message' => 'I said more than %s!!',
126                                 ),
127                         ),
128                         'email' => array(
129                                 'required' => array(
130                                         'rule' => array('notempty'),
131                                 ),
132                                 'among' => array(
133                                         'rule' => array('between'),
134                                 ),
135                         ),
136                 );
137         }
138
139 You can set validation parameters, messages from Configuration:
140
141
142         Configure::write('Validation', array(
143                 'parameters' => array(
144                         'User' => array(
145                                 'nickname' => array(
146                                         'min' => 3,
147                                 ),
148                                 'email' => array(
149                                         'among' => array(16, 256)
150                                 ),
151                         ),
152                 ),
153                 'messages' => array(
154                         'default' => array(
155                                 'required' => 'you need to enter.',
156                                 'min' => '%s characters needed',
157                         ),
158                         'User' => array(
159                                 'email' => array(
160                                         'required' => 'are you kidding me or misreading?',
161                                 ),
162                         ),
163                 ),
164         ));
165
166
167 Note that priority is "hard coded on your model" > "specifying Model and field" > "default".
168 But if you turn $overwrite property on, "specifying Model and field" forces to overwrite("default" does not).
169
170
171 ### MultiValidationBehavior ###
172
173
174         class User extends AppModel {
175
176                 var $actsAs = array('Collectionable.MultiValidation');
177
178                 var $validate = array(
179                         'password_raw' => array(
180                                 'required' => array(
181                                         'rule' => array('notempty'),
182                                 ),
183                                 'minlength' => array(
184                                         'rule' => array('minlength', 6),
185                                 ),
186                         ),
187                 );
188
189                 // note that $validateprofile is invalid with 'profile'
190                 var $validateProfile = array(
191                         'nickname' => array(
192                                 'required' => array(
193                                         'rule' => array('notempty'),
194                                 ),
195                                 'maxlength' => array(
196                                         'rule' => array('maxlength', 40),
197                                 ),
198                         ),
199                 );
200
201                 var $validateRequireEmail = array(
202                         'email' => array(
203                                 'required' => array(
204                                         'rule' => array('notempty'),
205                                 ),
206                                 'email' => array(
207                                         'rule' => array('email'),
208                                 ),
209                         ),
210                 );
211
212                 var $validatePasswordConfirm = array(
213                         'password_confirm' => array(
214                                 'required' => array(
215                                         'rule' => array('notempty'),
216                                 ),
217                                 'confirm_password' => array(
218                                         'rule' => array('confirm_password'),
219                                 ),
220                         ),
221                 );
222
223                 // You can set validation pattern on demand:
224                 function add($data, $validate = true, $options = array()) {
225                         $this->useValidationSet('requireEmail');
226                         $this->create();
227                         return $this->save($data, $validate, $options);
228                 }
229
230                 // You can dsiable default $validate with second argument as false:
231                 function edit($data, $validate = true, $options = array()) {
232                         $this->useValidationSet('profile', false);
233                         return $this->save($data, $validate, $options);
234                 }
235
236                 // You can specify two and more rule sets. these will be merged
237                 function resetEmail($data) {
238                         $this->useValidationSet(array('requireEmail', 'passwordConfirm'));
239                 }
240
241                 function confirm_password() {
242                         // confirm password
243                 }
244
245         }
246
247
248 ## Thanks ##
249 - [nojimage](http://github.com/nojimage) created [base of this plugin](http://github.com/nojimage/paging)
250
251
252 ## License
253
254 Licensed under The MIT License.
255 Redistributions of files must retain the above copyright notice.
256
257
258 Copyright 2010 hiromi, https://github.com/hiromi2424
259
260 Permission is hereby granted, free of charge, to any person obtaining a copy
261 of this software and associated documentation files (the "Software"), to deal
262 in the Software without restriction, including without limitation the rights
263 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
264 copies of the Software, and to permit persons to whom the Software is
265 furnished to do so, subject to the following conditions:
266
267 The above copyright notice and this permission notice shall be included in
268 all copies or substantial portions of the Software.
269
270 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
271 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
272 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
273 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
274 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
275 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
276 THE SOFTWARE.