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