3 // Copyright (c) 2011 Blue Static
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
17 namespace hoplite\data
;
18 use \hoplite\http
as http
;
20 require_once HOPLITE_ROOT
. '/http/response_code.php';
21 require_once HOPLITE_ROOT
. '/http/rest_action.php';
24 A Controller is a RESTful http\Action that is used to bind a data\Model to a
25 web interface. Subclass this in order to perform business logic such as
26 validation and authentication.
28 This class is semi-abstract in that it cannot be used directly. At minimum,
29 FilterRequest() needs to be overridden to select the Model to use.
31 class Controller
extends http\RestAction
33 /*! @var hoplite\data\Model The object that will be operated on. */
34 protected $model = NULL
;
36 /*! Selects the Model object. */
37 public function FilterRequest(http\Request
$request, http\Response
$response)
40 // $this->model = new webapp\models\User();
41 throw new ControllerException('Model not selected');
44 /*! Gets the data from the model. */
45 public function DoGet(http\Request
$request, http\Response
$response)
47 $this->model
->SetFrom(array_merge($request->data
, $request->data
['_GET']));
49 $response->data
= $this->model
->Fetch();
50 } catch (ModelException
$e) {
51 $response->body
= $e->GetMessage();
52 $response->response_code
= http\ResponseCode
::NOT_FOUND
;
53 } catch (\PDOException
$e) {
54 $response->body
= $e->GetMessage();
55 $response->response_code
= http\ResponseCode
::INTERNAL_SERVER_ERROR
;
59 /*! Updates an object in the store. */
60 public function DoPost(http\Request
$request, http\Response
$response)
62 $this->model
->SetFrom(array_merge($request->data
, $request->data
['_POST']));
64 $this->model
->Update();
65 $response->data
= $this->model
->Fetch();
66 } catch (ModelException
$e) {
67 $response->body
= $e->GetMessage();
68 $response->response_code
= http\ResponseCode
::NOT_FOUND
;
69 } catch (\PDOException
$e) {
70 $response->body
= $e->GetMessage();
71 $response->response_code
= http\ResponseCode
::INTERNAL_SERVER_ERROR
;
75 /*! Deletes the object from the store. */
76 public function DoDelete(http\Request
$request, http\Response
$response)
78 $this->model
->SetFrom(array_merge($request->data
, $request->data
['_POST']));
80 $this->model
->Delete();
81 } catch (ModelException
$e) {
82 $response->body
= $e->GetMessage();
83 $response->response_code
= http\ResponseCode
::BAD_REQUEST
;
84 } catch (\PDOException
$e) {
85 $response->body
= $e->GetMessage();
86 $response->response_code
= http\ResponseCode
::INTERNAL_SERVER_ERROR
;
90 /*! Updates an object in the store. */
91 public function DoPut(http\Request
$request, http\Response
$response)
93 $this->model
->SetFrom(array_merge($request->data
, $request->data
['_POST']));
95 $this->model
->Insert();
96 $response->data
= $this->model
->Fetch();
97 } catch (ModelException
$e) {
98 $response->body
= $e->GetMessage();
99 $response->response_code
= http\ResponseCode
::BAD_REQUEST
;
100 } catch (\PDOException
$e) {
101 $response->body
= $e->GetMessage();
102 $response->response_code
= http\ResponseCode
::INTERNAL_SERVER_ERROR
;
107 class ControllerException
extends \Exception
{}