1
|
|
|
<?php |
2
|
|
|
namespace App\Model\Table; |
3
|
|
|
|
4
|
|
|
use Cake\ORM\Table; |
5
|
|
|
use Cake\Validation\Validator; |
6
|
|
|
|
7
|
|
|
class BadgesTable extends Table |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Initialize method. |
12
|
|
|
* |
13
|
|
|
* @param array $config The configuration for the Table. |
14
|
|
|
* |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
|
|
public function initialize(array $config) |
18
|
|
|
{ |
19
|
|
|
$this->table('badges'); |
|
|
|
|
20
|
|
|
$this->displayField('name'); |
|
|
|
|
21
|
|
|
$this->primaryKey('id'); |
|
|
|
|
22
|
|
|
|
23
|
|
|
$this->addBehavior('Timestamp'); |
24
|
|
|
$this->addBehavior('Xety/Cake3Upload.Upload', [ |
25
|
|
|
'fields' => [ |
26
|
|
|
'picture' => [ |
27
|
|
|
'path' => 'img/badges/:md5', |
28
|
|
|
'overwrite' => true |
29
|
|
|
] |
30
|
|
|
] |
31
|
|
|
]); |
32
|
|
|
$this->addBehavior('Translate', [ |
33
|
|
|
'fields' => ['name'], |
34
|
|
|
'translationTable' => 'BadgesI18n' |
35
|
|
|
]); |
36
|
|
|
|
37
|
|
|
$this->belongsToMany('Users', [ |
38
|
|
|
'foreignKey' => 'badge_id', |
39
|
|
|
'targetForeignKey' => 'user_id', |
40
|
|
|
'joinTable' => 'badges_users', |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create validation rules. |
46
|
|
|
* |
47
|
|
|
* @param \Cake\Validation\Validator $validator Instance of the validator. |
48
|
|
|
* |
49
|
|
|
* @return \Cake\Validation\Validator |
50
|
|
|
*/ |
51
|
|
|
public function validationCreate(Validator $validator) |
52
|
|
|
{ |
53
|
|
|
$validator |
|
|
|
|
54
|
|
|
->provider('upload', 'App\Model\Validation\UploadValidator') |
55
|
|
|
->notEmpty('name', __("You must select a name.")) |
56
|
|
|
->add('name', 'minLength', [ |
57
|
|
|
'rule' => ['minLength', 5], |
58
|
|
|
'message' => __("Please, {0} characters minimum for the name.", 5) |
59
|
|
|
]) |
60
|
|
|
->allowEmpty('picture_file') |
61
|
|
|
->add('picture_file', [ |
62
|
|
|
'mimeType' => [ |
63
|
|
|
'rule' => ['mimeType', ['image/jpeg', 'image/png']], |
64
|
|
|
'message' => __("The mimeType is not allowed."), |
65
|
|
|
'on' => function ($context) { |
66
|
|
|
return !empty($context['data']['picture_file']['name']); |
67
|
|
|
} |
68
|
|
|
], |
69
|
|
|
'fileExtension' => [ |
70
|
|
|
'rule' => ['extension', ['jpg', 'jpeg', 'png']], |
71
|
|
|
'message' => __("The extension allowed are {0}.", '.jpg, .jpeg and .png'), |
72
|
|
|
'on' => function ($context) { |
73
|
|
|
return !empty($context['data']['picture_file']['name']); |
74
|
|
|
} |
75
|
|
|
], |
76
|
|
|
'fileSize' => [ |
77
|
|
|
'rule' => ['fileSize', '<', '500KB'], |
78
|
|
|
'message' => __("The file exceeded the max allowed size of {0}", '500KB'), |
79
|
|
|
'on' => function ($context) { |
80
|
|
|
return !empty($context['data']['picture_file']['name']); |
81
|
|
|
} |
82
|
|
|
], |
83
|
|
|
'maxDimension' => [ |
84
|
|
|
'rule' => ['maxDimension', 130, 130], |
85
|
|
|
'provider' => 'upload', |
86
|
|
|
'message' => __( |
87
|
|
|
"The file exceeded the max allowed dimension. Max height : {0} Max width : {1}", |
88
|
|
|
130, |
89
|
|
|
130 |
90
|
|
|
), |
91
|
|
|
] |
92
|
|
|
]) |
93
|
|
|
->notEmpty('type', __("You must select a type.")) |
94
|
|
|
->add('type', 'inList', [ |
95
|
|
|
'rule' => ['inList', ['comments', 'registration']], |
96
|
|
|
'message' => __("This type is not allowed. Allowed types : {0}", implode(", ", ['comments', 'registration'])), |
97
|
|
|
]) |
98
|
|
|
->notEmpty('rule', __("You must select a rule.")) |
99
|
|
|
->add('rule', 'numeric', [ |
100
|
|
|
'rule' => 'numeric' |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
return $validator; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.