Vaskovsky Web Application  3.17.0306
CatalogPage.php
1 <?php
2 // Copyright © 2017 Alexey Vaskovsky.
3 //
4 // This file is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 3.0 of the License, or (at your option) any later version.
8 //
9 // This file is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this file. If not, see
16 // <http://www.gnu.org/licenses/>
18 /**
19  * Catalog page pattern.
20  *
21  * @class AVaskovsky::WebApplication::CatalogPage
22  * @extends AVaskovsky::WebApplication::AbstractPage
23  *
24  * @author Alexey Vaskovsky
25  */
26 /**
27  * Returns an associative array that contains an initial model data; never null.
28  *
29  * @protected @fn create()
30  * @memberof AVaskovsky::WebApplication::CatalogPage
31  */
32 /**
33  * Sanitizes a model data.
34  *
35  * @protected @fn sanitize($x)
36  * @memberof AVaskovsky::WebApplication::CatalogPage
37  *
38  * @param object $x
39  * is an object that contains a model data; never null.
40  *
41  * @return an associative array that contains the sanitized data; never null.
42  */
43 /**
44  * Validates a model data to update model.
45  *
46  * @protected @fn validateUpdate($x)
47  * @memberof AVaskovsky::WebApplication::CatalogPage
48  *
49  * @param object $x
50  * is an object that contains a sanitized model data; never null.
51  *
52  * @return a string that contains a validation error or nothing if the model is
53  * valid.
54  */
55 /**
56  * Validates a model data to insert model.
57  *
58  * The default implementation uses `$this->validateUpdate($x)`.
59  *
60  * @protected @fn validateInsert($x)
61  * @memberof AVaskovsky::WebApplication::CatalogPage
62  *
63  * @param object $x
64  * is an object that contains a sanitized model data; never null.
65  *
66  * @return a string that contains a validation error or nothing if the model is
67  * valid.
68  */
69 /**
70  * Validates primary key in a model.
71  *
72  * @protected @fn validateKey($x)
73  * @memberof AVaskovsky::WebApplication::CatalogPage
74  *
75  * @param object $x
76  * is an object that contains a sanitized model data; never null.
77  *
78  * @return a string that contains a validation error
79  * or nothing if the model is valid.
80  */
81 /**
82  * Returns an iterable list of objects that contains the selected models; never
83  * null.
84  *
85  * @protected @fn select($x)
86  * @memberof AVaskovsky::WebApplication::CatalogPage
87  *
88  * @param object $x
89  * is an object that contains a sanitized model data; never null.
90  */
91 /**
92  * Returns an object that contains the model data or null if the model with the
93  * specified key is not found.
94  *
95  * @protected @fn get($x)
96  * @memberof AVaskovsky::WebApplication::CatalogPage
97  *
98  * @param object $x
99  * is an object that contains a sanitized model data with the valid
100  * primary key; never null.
101  */
102 /**
103  * Inserts a new model in the database.
104  *
105  * @protected @fn insert($x)
106  * @memberof AVaskovsky::WebApplication::CatalogPage
107  *
108  * @param object $x
109  * is an object that contains a valid model data; never null.
110  */
111 /**
112  * Updates a model in the database.
113  *
114  * @protected @fn update($x)
115  * @memberof AVaskovsky::WebApplication::CatalogPage
116  *
117  * @param object $x
118  * is an object that contains a valid model data; never null.
119  */
120 /**
121  * Deletes a model from the database.
122  *
123  * @protected @fn delete($x)
124  * @memberof AVaskovsky::WebApplication::CatalogPage
125  *
126  * @param object $x
127  * is an object that contains a sanitized model data with the valid
128  * primary key; never null.
129  */
130 /**
131  * The SELECT action.
132  *
133  * @public @fn doSelect()
134  * @memberof AVaskovsky::WebApplication::CatalogPage
135  *
136  * @return a string; never null.
137  */
138 /**
139  * The GET action.
140  *
141  * @public @fn doGet()
142  * @memberof AVaskovsky::WebApplication::CatalogPage
143  *
144  * @return a string; never null.
145  */
146 /**
147  * The ADD action.
148  *
149  * @public @fn doAdd()
150  * @memberof AVaskovsky::WebApplication::CatalogPage
151  *
152  * @return a string; never null.
153  */
154 /**
155  * The INSERT action.
156  *
157  * @public @fn doInsert()
158  * @memberof AVaskovsky::WebApplication::CatalogPage
159  *
160  * @return a string; never null.
161  */
162 /**
163  * The UPDATE action.
164  *
165  * @public @fn doUpdate()
166  * @memberof AVaskovsky::WebApplication::CatalogPage
167  *
168  * @return a string; never null.
169  */
170 /**
171  * The DELETE action.
172  *
173  * @public @fn doDelete()
174  * @memberof AVaskovsky::WebApplication::CatalogPage
175  *
176  * @return a string; never null.
177  */
178 trait CatalogPage {
179  abstract protected function create();
180  abstract protected function sanitize($x);
181  abstract protected function select($x);
182  abstract protected function get($x);
183  abstract protected function insert($x);
184  abstract protected function update($x);
185  abstract protected function delete($x);
186  abstract protected function validateUpdate($x);
187  protected function validateInsert($x)
188  {
189  return $this->validateUpdate($x);
190  }
191  protected function validateKey($x)
192  {
193  // x: -null
194  if (! is_object($x)) {
195  throw new \InvalidArgumentException();
196  }
197  //
198  if (empty($x->id))
199  return _("Empty ID");
200  }
201  public function doSelect()
202  {
203  //
204  $req = (object) $this->sanitize((object) $_REQUEST);
205  $list = $this->select($req);
206  $model_name = (new \ReflectionClass($this))->getShortName();
207  return $this->render(
208  "{$model_name}Index",
209  array(
210  "request" => $req,
211  "list" => $list
212  ));
213  }
214  public function doGet()
215  {
216  //
217  $req = (object) $this->sanitize((object) $_REQUEST);
218  $err = $this->validateKey($req);
219  if (! empty($err))
220  return $this->doSelect();
221  $data = $this->get($req);
222  if (empty($data))
223  return $this->renderError(_("Not found"), 404);
224  $model_name = (new \ReflectionClass($this))->getShortName();
225  return $this->render(
226  "{$model_name}Editor",
227  array(
228  "request" => $req,
229  "model" => $data,
230  "permissions" => (object) [
231  "can_insert" => false,
232  "can_update" => true,
233  "can_delete" => true
234  ]
235  ));
236  }
237  public function doAdd()
238  {
239  //
240  $req = (object) $this->sanitize((object) $_REQUEST);
241  $data = (object) $this->create();
242  $model_name = (new \ReflectionClass($this))->getShortName();
243  return $this->render(
244  "{$model_name}Editor",
245  array(
246  "request" => $req,
247  "model" => $data,
248  "permissions" => (object) [
249  "can_insert" => true,
250  "can_update" => false,
251  "can_delete" => false
252  ]
253  ));
254  }
255  public function doInsert()
256  {
257  //
258  $req = (object) $this->sanitize((object) $_REQUEST);
259  $err = $this->validateInsert($req);
260  if (! empty($err))
261  return $this->renderError($err, 400);
262  $this->insert($req);
263  return $this->doSelect();
264  }
265  public function doUpdate()
266  {
267  //
268  $req = (object) $this->sanitize((object) $_REQUEST);
269  $err = $this->validateKey($req);
270  if (! empty($err))
271  return $this->renderError($err, 400);
272  $err = $this->validateUpdate($req);
273  if (! empty($err))
274  return $this->renderError($err, 400);
275  $this->update($req);
276  return $this->doSelect();
277  }
278  public function doDelete()
279  {
280  //
281  $req = (object) $this->sanitize((object) $_REQUEST);
282  $err = $this->validateKey($req);
283  if (! empty($err))
284  return $this->renderError($err, 400);
285  $this->delete($req);
286  return $this->doSelect();
287  }
288 }