Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| tao_actions_SinglePageModule | |
0.00% |
0 / 29 |
|
0.00% |
0 / 6 |
272 | |
0.00% |
0 / 1 |
| setClientRoute | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setClientParam | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getLayout | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| composeView | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
56 | |||
| setPage | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| defaultData | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * Abstraction for controllers serving single page application. |
| 23 | * |
| 24 | * @author Jean-Sébastien Conan <jean-sebastien@taotesting.com> |
| 25 | * @package oat\tao\actions |
| 26 | * |
| 27 | */ |
| 28 | abstract class tao_actions_SinglePageModule extends \tao_actions_CommonModule |
| 29 | { |
| 30 | /** |
| 31 | * This header is added to the response to inform the client a forward occurs |
| 32 | */ |
| 33 | public const FORWARD_HEADER = 'X-Tao-Forward'; |
| 34 | |
| 35 | /** |
| 36 | * A list of parameters to provide to the client controller |
| 37 | * @var array |
| 38 | */ |
| 39 | protected $clientParams = []; |
| 40 | |
| 41 | /** |
| 42 | * Sets the route to be used by the client controller |
| 43 | * @param string $route |
| 44 | */ |
| 45 | protected function setClientRoute($route) |
| 46 | { |
| 47 | header(self::FORWARD_HEADER . ': ' . $route); |
| 48 | $this->setClientParam('forwardTo', $route); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Add a parameter to provide to the client controller |
| 53 | * @param string $name |
| 54 | * @param mixed $value |
| 55 | * @return $this |
| 56 | */ |
| 57 | protected function setClientParam($name, $value) |
| 58 | { |
| 59 | $this->clientParams[$name] = $value; |
| 60 | return $this; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Gets the path to the layout |
| 65 | * @return array |
| 66 | */ |
| 67 | protected function getLayout() |
| 68 | { |
| 69 | return ['layout.tpl', 'tao']; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Main method to render a view using a particular template. |
| 74 | * Detects whether the client only need JSON content. |
| 75 | * You still need to set the main view, however it must be set |
| 76 | * before to call this method as this view may be overridden. |
| 77 | * |
| 78 | * @param string [$scope] - A CSS class name that scope the view |
| 79 | * @param array [$data] - An optional data set to forward to the view |
| 80 | * @param String [$template] - Defines the path of the view, default to 'pages/index.tpl' |
| 81 | * @param String [$extension] - Defines the extension that should contain the template |
| 82 | * @throws \common_exception_Error |
| 83 | */ |
| 84 | protected function composeView($scope = '', $data = [], $template = '', $extension = '') |
| 85 | { |
| 86 | if (!is_array($data)) { |
| 87 | $data = []; |
| 88 | } |
| 89 | |
| 90 | if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false) { |
| 91 | $this->returnJson([ |
| 92 | 'success' => true, |
| 93 | 'data' => $data, |
| 94 | ]); |
| 95 | } else { |
| 96 | foreach ($data as $key => $value) { |
| 97 | if (is_array($value) || is_object($value)) { |
| 98 | $data[$key] = json_encode($value); |
| 99 | } |
| 100 | } |
| 101 | $this->setData('data', $data); |
| 102 | $this->setPage($scope, $template, $extension); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Assigns the template to render. |
| 108 | * @param string [$scope] - A CSS class name that scope the view |
| 109 | * @param String [$template] - Defines the path of the view, default to 'pages/index.tpl' |
| 110 | * @param String [$extension] - Defines the extension that should contain the template |
| 111 | */ |
| 112 | protected function setPage($scope = '', $template = '', $extension = '') |
| 113 | { |
| 114 | $template = empty($template) ? 'pages/index.tpl' : $template; |
| 115 | $extension = empty($extension) ? \Context::getInstance()->getExtensionName() : $extension; |
| 116 | |
| 117 | $this->defaultData(); |
| 118 | $this->setData('scope', $scope); |
| 119 | |
| 120 | if ($this->isXmlHttpRequest()) { |
| 121 | $this->setView($template, $extension); |
| 122 | } else { |
| 123 | $this->setData('content-template', [$template, $extension]); |
| 124 | |
| 125 | $layout = (array)$this->getLayout(); |
| 126 | $this->setView($layout[0], isset($layout[1]) ? $layout[1] : null); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Retrieve the data from the url and make the base initialization |
| 132 | * |
| 133 | * @return void |
| 134 | */ |
| 135 | protected function defaultData() |
| 136 | { |
| 137 | parent::defaultData(); |
| 138 | |
| 139 | $this->setData('client_params', $this->clientParams); |
| 140 | } |
| 141 | } |