88use Cake \Utility \Security ;
99use Cake \Validation \Validation ;
1010use Cake \Mailer \Email ;
11+ use Cake \Http \Exception \BadRequestException ;
1112
1213
1314class FormsController extends RestController {
@@ -37,6 +38,7 @@ public function index() {
3738 $ forms = $ this ->Forms ->find ('all ' ,
3839 [ 'contain ' => FormsController::$ associations ]);
3940
41+ $ forms = $ this ->applyDataDateFilter ($ forms );
4042 $ forms = $ this ->applyFilters ($ forms );
4143
4244 // Check permissions
@@ -56,6 +58,42 @@ public function index() {
5658 $ this ->JSONResponse (ResponseCode::Ok, $ forms );
5759 }
5860
61+ private function applyDataDateFilter ($ query ) {
62+ // PHP converts dots in query parameter names to underscores.
63+ $ date = $ this ->request ->getQuery ('data__data ' );
64+
65+ if ($ date === null ) {
66+ return $ query ;
67+ }
68+
69+ $ parsed = is_string ($ date )
70+ ? \DateTime::createFromFormat ('!Y-m-d ' , $ date )
71+ : false ;
72+
73+ if (!$ parsed || $ parsed ->format ('Y-m-d ' ) !== $ date ) {
74+ throw new BadRequestException (
75+ 'data.data must use the YYYY-MM-DD format '
76+ );
77+ }
78+
79+ return $ query ->where (function ($ exp , $ query ) use ($ date ) {
80+ $ decodedData = $ query ->func ()->json_unquote ([
81+ 'Forms.data ' => 'identifier ' ,
82+ ]);
83+
84+ $ extractedDate = $ query ->func ()->json_extract ([
85+ $ decodedData ,
86+ "'$.data' " => 'literal ' ,
87+ ]);
88+
89+ $ unquotedDate = $ query ->func ()->json_unquote ([
90+ $ extractedDate ,
91+ ]);
92+
93+ return $ exp ->eq ($ unquotedDate , $ date , 'string ' );
94+ });
95+ }
96+
5997 public function get ($ id ) {
6098 try {
6199 $ form = $ this ->Forms ->get ($ id , [ 'contain ' => FormsController::$ associations ]);
@@ -219,6 +257,10 @@ function ($address) {
219257
220258 return $ email ;
221259 }
260+
261+
262+
263+
222264}
223265
224- ?>
266+ ?>
0 commit comments