symphonycms /
symphony-2
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @package toolkit |
||
| 4 | */ |
||
| 5 | /** |
||
| 6 | * JSONPage extends the Page class to provide an object representation |
||
| 7 | * of a Symphony backend JSON page. |
||
| 8 | */ |
||
| 9 | |||
| 10 | abstract class JSONPage extends TextPage |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The root node for the response of the JSONPage |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected $_Result; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The constructor for `JSONPage`. This sets the page status to `Page::HTTP_STATUS_OK`, |
||
| 20 | * the default content type to `application/json` and initialises `$this->_Result` |
||
| 21 | * with an `array`. The constructor also starts the Profiler for this |
||
| 22 | * page template. |
||
| 23 | * |
||
| 24 | * @see toolkit.Profiler |
||
| 25 | */ |
||
| 26 | public function __construct() |
||
| 27 | { |
||
| 28 | $this->_Result = array(); |
||
| 29 | |||
| 30 | $this->setHttpStatus(self::HTTP_STATUS_OK); |
||
| 31 | $this->addHeaderToPage('Content-Type', 'application/json'); |
||
| 32 | |||
| 33 | Symphony::Profiler()->sample('Page template created', PROFILE_LAP); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * This function is called by Administration class when a user is not authenticated |
||
| 38 | * to the Symphony backend. It sets the status of this page to |
||
| 39 | * `Page::HTTP_STATUS_UNAUTHORIZED` and appends a message for generation |
||
| 40 | */ |
||
| 41 | public function handleFailedAuthorisation() |
||
| 42 | { |
||
| 43 | $this->setHttpStatus(self::HTTP_STATUS_UNAUTHORIZED); |
||
| 44 | $this->_Result = array('status' => __('You are not authorised to access this page.')); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The generate functions outputs the correct headers for |
||
| 49 | * this `JSONPage`, adds `$this->getHttpStatusCode()` code to the root attribute |
||
| 50 | * before calling the parent generate function and generating |
||
| 51 | * the `$this->_Result` json string |
||
| 52 | * |
||
| 53 | * @param null $page |
||
|
0 ignored issues
–
show
|
|||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function generate($page = null) |
||
|
0 ignored issues
–
show
|
|||
| 57 | { |
||
| 58 | // Set the actual status code in the xml response |
||
| 59 | $this->_Result['status'] = $this->getHttpStatusCode(); |
||
| 60 | |||
| 61 | parent::generate($page); |
||
| 62 | |||
| 63 | return json_encode($this->_Result); |
||
| 64 | } |
||
| 65 | } |
||
| 66 |