1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BEdita, API-first content management framework |
4
|
|
|
* Copyright 2017 ChannelWeb Srl, Chialab Srl |
5
|
|
|
* |
6
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace BEdita\Core\Model\Table; |
15
|
|
|
|
16
|
|
|
use Cake\Database\Expression\QueryExpression; |
17
|
|
|
use Cake\ORM\Query; |
18
|
|
|
use Cake\ORM\RulesChecker; |
19
|
|
|
use Cake\ORM\Table; |
20
|
|
|
use Cake\Validation\Validator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* DateRanges Model |
24
|
|
|
* |
25
|
|
|
* @property \Cake\ORM\Association\BelongsTo $Objects |
26
|
|
|
* |
27
|
|
|
* @method \BEdita\Core\Model\Entity\DateRange get($primaryKey, $options = []) |
28
|
|
|
* @method \BEdita\Core\Model\Entity\DateRange newEntity($data = null, array $options = []) |
29
|
|
|
* @method \BEdita\Core\Model\Entity\DateRange[] newEntities(array $data, array $options = []) |
30
|
|
|
* @method \BEdita\Core\Model\Entity\DateRange|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) |
31
|
|
|
* @method \BEdita\Core\Model\Entity\DateRange patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) |
32
|
|
|
* @method \BEdita\Core\Model\Entity\DateRange[] patchEntities($entities, array $data, array $options = []) |
33
|
|
|
* @method \BEdita\Core\Model\Entity\DateRange findOrCreate($search, callable $callback = null, $options = []) |
34
|
|
|
*/ |
35
|
|
|
class DateRangesTable extends Table |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Initialize method |
40
|
|
|
* |
41
|
|
|
* @param array $config The configuration for the Table. |
42
|
|
|
* @return void |
43
|
|
|
* @codeCoverageIgnore |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function initialize(array $config) |
46
|
|
|
{ |
47
|
|
|
parent::initialize($config); |
48
|
|
|
|
49
|
|
|
$this->setTable('date_ranges'); |
50
|
|
|
$this->setDisplayField('id'); |
51
|
|
|
$this->setPrimaryKey('id'); |
52
|
|
|
|
53
|
|
|
$this->belongsTo('Objects', [ |
54
|
|
|
'foreignKey' => 'object_id', |
55
|
|
|
'joinType' => 'INNER', |
56
|
|
|
'className' => 'BEdita/Core.Objects' |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Default validation rules. |
62
|
|
|
* |
63
|
|
|
* @param \Cake\Validation\Validator $validator Validator instance. |
64
|
|
|
* @return \Cake\Validation\Validator |
65
|
|
|
* @codeCoverageIgnore |
66
|
|
|
*/ |
67
|
|
|
public function validationDefault(Validator $validator) |
68
|
|
|
{ |
69
|
|
|
$validator |
70
|
|
|
->integer('id') |
71
|
|
|
->allowEmpty('id', 'create'); |
72
|
|
|
|
73
|
|
|
$validator |
74
|
|
|
->allowEmpty('start_date'); |
75
|
|
|
|
76
|
|
|
$validator |
77
|
|
|
->allowEmpty('end_date'); |
78
|
|
|
|
79
|
|
|
$validator |
80
|
|
|
->allowEmpty('params'); |
81
|
|
|
|
82
|
|
|
return $validator; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns a rules checker object that will be used for validating |
87
|
|
|
* application integrity. |
88
|
|
|
* |
89
|
|
|
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified. |
90
|
|
|
* @return \Cake\ORM\RulesChecker |
91
|
|
|
* @codeCoverageIgnore |
92
|
|
|
*/ |
93
|
|
|
public function buildRules(RulesChecker $rules) |
94
|
|
|
{ |
95
|
|
|
$rules->add($rules->existsIn(['object_id'], 'Objects')); |
96
|
|
|
|
97
|
|
|
return $rules; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Find objects by date range. |
102
|
|
|
* |
103
|
|
|
* Create a query to filter objects using start and end date conditions. |
104
|
|
|
* Accepted options are: |
105
|
|
|
* - 'start_date' or 'end_date' |
106
|
|
|
* - mandatory sub-option must be one of 'gt' (greather than), 'lt' (less than), |
107
|
|
|
* 'ge' (greater or equal), 'le' (less or equal) with a date |
108
|
|
|
* |
109
|
|
|
* Examples |
110
|
|
|
* ``` |
111
|
|
|
* // find events with a start date after '2017-03-01' |
112
|
|
|
* $table->find('dateRanges', ['start_date' => ['gt' => '2017-03-01']]); |
113
|
|
|
* |
114
|
|
|
* // find events with an ending date before '2017-05-01 22:00:00' |
115
|
|
|
* $table->find('dateRanges', ['end_date' => ['lt' => '2017-05-01 22:00:00']]); |
116
|
|
|
* ``` |
117
|
|
|
* |
118
|
|
|
* @param \Cake\ORM\Query $query Query object instance. |
119
|
|
|
* @param array $options Array of acceptable date range conditions. |
120
|
|
|
* @return \Cake\ORM\Query |
121
|
|
|
*/ |
122
|
|
|
public function findDateRanges(Query $query, array $options) |
123
|
|
|
{ |
124
|
|
|
$options = array_intersect_key($options, array_flip(['start_date', 'end_date'])); |
125
|
|
|
|
126
|
|
|
return $query->where(function (QueryExpression $exp) use ($options) { |
127
|
|
|
foreach ($options as $field => $conditions) { |
128
|
|
|
$field = $this->aliasField($field); |
129
|
|
|
|
130
|
|
|
if (!is_array($conditions)) { |
131
|
|
|
$exp = $exp->eq($field, $conditions); |
132
|
|
|
|
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
foreach ($conditions as $operator => $value) { |
137
|
|
|
switch ($operator) { |
138
|
|
|
case 'eq': |
139
|
|
|
case '=': |
140
|
|
|
$exp = $exp->eq($field, $value); |
141
|
|
|
break; |
142
|
|
|
|
143
|
|
|
case 'neq': |
144
|
|
|
case 'ne': |
145
|
|
|
case '!=': |
146
|
|
|
case '<>': |
147
|
|
|
$exp = $exp->notEq($field, $value); |
148
|
|
|
break; |
149
|
|
|
|
150
|
|
|
case 'lt': |
151
|
|
|
case '<': |
152
|
|
|
$exp = $exp->lt($field, $value); |
153
|
|
|
break; |
154
|
|
|
|
155
|
|
|
case 'lte': |
156
|
|
|
case 'le': |
157
|
|
|
case '<=': |
158
|
|
|
$exp = $exp->lte($field, $value); |
159
|
|
|
break; |
160
|
|
|
|
161
|
|
|
case 'gt': |
162
|
|
|
case '>': |
163
|
|
|
$exp = $exp->gt($field, $value); |
164
|
|
|
break; |
165
|
|
|
|
166
|
|
|
case 'gte': |
167
|
|
|
case 'ge': |
168
|
|
|
case '>=': |
169
|
|
|
$exp = $exp->gte($field, $value); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $exp; |
175
|
|
|
}); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|