1+ <?php
2+
3+ /**
4+ * Copyright 2017 OpenStack Foundation
5+ * Licensed under the Apache License, Version 2.0 (the "License");
6+ * you may not use this file except in compliance with the License.
7+ * You may obtain a copy of the License at
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ **/
15+ class TagsCrudApi extends AbstractRestfulJsonApi
16+ {
17+ const ApiPrefix = 'api/v1/tags ' ;
18+
19+ /**
20+ * @return bool
21+ */
22+ public function checkOwnAjaxRequest ()
23+ {
24+ $ referer = @$ _SERVER ['HTTP_REFERER ' ];
25+ if (empty ($ referer )) {
26+ return false ;
27+ }
28+ //validate
29+ if (!Director::is_ajax ()) {
30+ return false ;
31+ }
32+ return Director::is_site_url ($ referer );
33+ }
34+
35+
36+ /**
37+ * @return bool
38+ */
39+ protected function isApiCall ()
40+ {
41+ $ request = $ this ->getRequest ();
42+ if (is_null ($ request )) {
43+ return false ;
44+ }
45+ return strpos (strtolower ($ request ->getURL ()), self ::ApiPrefix) !== false ;
46+ }
47+
48+ /**
49+ * @return bool
50+ */
51+ protected function authorize ()
52+ {
53+ return true ;
54+ }
55+
56+ /**
57+ * @return bool
58+ */
59+ protected function authenticate ()
60+ {
61+ return Permission::check ('SANGRIA_ACCESS ' );
62+ }
63+
64+ /**
65+ * @var array
66+ */
67+ static $ url_handlers = array (
68+ 'GET ' => 'getTags ' ,
69+ 'PUT $TAG_ID! ' => 'updateTag ' ,
70+ 'POST ' => 'addTag ' ,
71+ 'DELETE $TAG_ID! ' => 'deleteTag ' ,
72+ );
73+
74+ /**
75+ * @var array
76+ */
77+ static $ allowed_actions = array (
78+ 'getTags ' ,
79+ 'updateTag ' ,
80+ 'addTag ' ,
81+ 'deleteTag ' ,
82+ );
83+
84+
85+ /**
86+ * @param SS_HTTPRequest $request
87+ * @return SS_HTTPResponse
88+ */
89+ public function getTags (SS_HTTPRequest $ request ){
90+ $ query_string = $ request ->getVars ();
91+ $ search = (isset ($ query_string ['search ' ])) ? Convert::raw2sql ($ query_string ['search ' ]) : '' ;
92+
93+ try {
94+ $ tags = Tag::get ()->where ("Tag.Tag != '' " );
95+ if (!empty ($ search )) {
96+ $ tags = $ tags ->filter (['Tag:PartialMatch ' => $ search ]);
97+ }
98+ $ tags = $ tags ->sort ('Tag ' ,'ASC ' );
99+
100+ $ data = [];
101+ foreach ($ tags as $ tag ) {
102+
103+ $ data [] = [
104+ 'id ' => $ tag ->ID ,
105+ 'tag ' => $ tag ->Tag
106+ ];
107+ }
108+
109+ return $ this ->ok ($ data );
110+ }
111+ catch (Exception $ ex )
112+ {
113+ SS_Log::log ($ ex ->getMessage (), SS_Log::ERR );
114+ return $ this ->serverError ();
115+ }
116+ }
117+
118+ /**
119+ * @param SS_HTTPRequest $request
120+ * @return SS_HTTPResponse
121+ */
122+ public function updateTag (SS_HTTPRequest $ request ){
123+ $ query_string = $ request ->getVars ();
124+ $ tag_val = Convert::raw2sql ($ query_string ['tag ' ]);
125+ $ tag_id = $ request ->param ('TAG_ID ' );
126+
127+ try {
128+
129+ $ tag = Tag::get ()->byID ($ tag_id );
130+ if (!$ tag )
131+ throw new NotFoundEntityException ('Tag ' );
132+
133+ if (empty ($ tag_val ))
134+ throw new ValidationException ();
135+
136+ $ tag ->Tag = $ tag_val ;
137+ $ tag ->write ();
138+
139+
140+ return $ this ->getTags ($ request );
141+ }
142+ catch (Exception $ ex )
143+ {
144+ SS_Log::log ($ ex ->getMessage (), SS_Log::ERR );
145+ return $ this ->serverError ();
146+ }
147+ }
148+
149+ /**
150+ * @param SS_HTTPRequest $request
151+ * @return SS_HTTPResponse
152+ */
153+ public function addTag (SS_HTTPRequest $ request ){
154+ $ query_string = $ request ->getVars ();
155+ $ tag_val = Convert::raw2sql ($ query_string ['tag ' ]);
156+
157+ try {
158+
159+ if (empty ($ tag_val ))
160+ throw new ValidationException ();
161+
162+ $ tag = new Tag ();
163+ $ tag ->Tag = $ tag_val ;
164+ $ tag ->write ();
165+
166+ return $ this ->getTags ($ request );
167+ }
168+ catch (Exception $ ex )
169+ {
170+ SS_Log::log ($ ex ->getMessage (), SS_Log::ERR );
171+ return $ this ->serverError ();
172+ }
173+ }
174+
175+ /**
176+ * @param SS_HTTPRequest $request
177+ * @return SS_HTTPResponse
178+ */
179+ public function deleteTag (SS_HTTPRequest $ request ){
180+ $ tag_id = $ request ->param ('TAG_ID ' );
181+
182+ try {
183+
184+ $ tag = Tag::get ()->byID ($ tag_id );
185+ if (!$ tag )
186+ throw new NotFoundEntityException ('Tag ' );
187+
188+ $ tag ->delete ();
189+
190+ return $ this ->getTags ($ request );
191+ }
192+ catch (Exception $ ex )
193+ {
194+ SS_Log::log ($ ex ->getMessage (), SS_Log::ERR );
195+ return $ this ->serverError ();
196+ }
197+ }
198+ }
0 commit comments