Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 75 |
|
0.00% |
0 / 22 |
CRAP | |
0.00% |
0 / 1 |
| Action | |
0.00% |
0 / 75 |
|
0.00% |
0 / 22 |
1640 | |
0.00% |
0 / 1 |
| fromSimpleXMLElement | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
156 | |||
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDisplay | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRelativeUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getBinding | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getContext | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getReload | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDisabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getGroup | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getIcon | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isMultiple | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getExtensionId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getController | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getAction | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| inferLegacyIcon | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| hasAccess | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| getRequiredRights | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __toPhpCode | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getWeight | |
0.00% |
0 / 1 |
|
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) 2014 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\model\menu; |
| 24 | |
| 25 | use oat\oatbox\PhpSerializable; |
| 26 | use oat\taoBackOffice\model\menuStructure\Action as iAction; |
| 27 | use oat\tao\helpers\ControllerHelper; |
| 28 | use oat\oatbox\service\ServiceManagerAwareTrait; |
| 29 | use oat\oatbox\service\ServiceManagerAwareInterface; |
| 30 | use tao_models_classes_accessControl_AclProxy; |
| 31 | |
| 32 | class Action implements PhpSerializable, iAction, ServiceManagerAwareInterface |
| 33 | { |
| 34 | use ServiceManagerAwareTrait; |
| 35 | |
| 36 | public const SERIAL_VERSION = 1392821334; |
| 37 | |
| 38 | /** @var array */ |
| 39 | private $data; |
| 40 | |
| 41 | /** |
| 42 | * @param \SimpleXMLElement $node |
| 43 | * @param $structureExtensionId extension of t structures.xml |
| 44 | * @return static |
| 45 | */ |
| 46 | public static function fromSimpleXMLElement(\SimpleXMLElement $node, $structureExtensionId) |
| 47 | { |
| 48 | $url = isset($node['url']) ? (string) $node['url'] : '#'; |
| 49 | if ($url == '#' || empty($url)) { |
| 50 | $extension = null; |
| 51 | $controller = null; |
| 52 | $action = null; |
| 53 | } else { |
| 54 | list($extension, $controller, $action) = explode('/', trim($url, '/')); |
| 55 | } |
| 56 | $data = [ |
| 57 | 'name' => (string) $node['name'], |
| 58 | 'id' => (string) $node['id'], |
| 59 | 'url' => $url, |
| 60 | 'binding' => isset($node['binding']) |
| 61 | ? (string) $node['binding'] |
| 62 | : (isset($node['js']) ? (string) $node['js'] : 'load'), |
| 63 | 'context' => (string) $node['context'], |
| 64 | 'reload' => isset($node['reload']) ? true : false, |
| 65 | 'disabled' => isset($node['disabled']) ? true : false, |
| 66 | 'multiple' => isset($node['multiple']) ? (trim(strtolower($node['multiple'])) == 'true') : false, |
| 67 | 'group' => isset($node['group']) ? (string) $node['group'] : self::GROUP_DEFAULT, |
| 68 | 'extension' => $extension, |
| 69 | 'controller' => $controller, |
| 70 | 'action' => $action, |
| 71 | 'weight' => isset($node['weight']) ? (int) $node['weight'] : self::WEIGHT_DEFAULT |
| 72 | ]; |
| 73 | |
| 74 | if (isset($node->icon)) { |
| 75 | $data['icon'] = Icon::fromSimpleXMLElement($node->icon, $structureExtensionId); |
| 76 | } |
| 77 | |
| 78 | return new static($data); |
| 79 | } |
| 80 | |
| 81 | public function __construct($data, $version = self::SERIAL_VERSION) |
| 82 | { |
| 83 | $this->data = $data; |
| 84 | if (!isset($this->data['icon'])) { |
| 85 | $this->data['icon'] = $this->inferLegacyIcon($data); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public function getName() |
| 90 | { |
| 91 | return $this->data['name']; |
| 92 | } |
| 93 | |
| 94 | public function getId() |
| 95 | { |
| 96 | return $this->data['id']; |
| 97 | } |
| 98 | |
| 99 | public function getDisplay() |
| 100 | { |
| 101 | return $this->data['display']; |
| 102 | } |
| 103 | |
| 104 | public function getUrl() |
| 105 | { |
| 106 | return _url($this->getAction(), $this->getController(), $this->getExtensionId()); |
| 107 | } |
| 108 | |
| 109 | public function getRelativeUrl() |
| 110 | { |
| 111 | return $this->data['url']; |
| 112 | } |
| 113 | |
| 114 | public function getBinding() |
| 115 | { |
| 116 | return $this->data['binding']; |
| 117 | } |
| 118 | |
| 119 | public function getContext() |
| 120 | { |
| 121 | return $this->data['context']; |
| 122 | } |
| 123 | |
| 124 | public function getReload() |
| 125 | { |
| 126 | return $this->data['reload']; |
| 127 | } |
| 128 | |
| 129 | public function getDisabled() |
| 130 | { |
| 131 | return $this->data['disabled']; |
| 132 | } |
| 133 | |
| 134 | public function getGroup() |
| 135 | { |
| 136 | return $this->data['group']; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @return Icon |
| 141 | */ |
| 142 | public function getIcon() |
| 143 | { |
| 144 | return $this->data['icon']; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Is the action available for multiple resources |
| 149 | * @return bool |
| 150 | */ |
| 151 | public function isMultiple() |
| 152 | { |
| 153 | return $this->data['multiple']; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Get the extension id from the action's URL. |
| 158 | * |
| 159 | * @return string the extension id |
| 160 | */ |
| 161 | public function getExtensionId() |
| 162 | { |
| 163 | return array_key_exists('extension', $this->data) ? $this->data['extension'] : null; |
| 164 | } |
| 165 | |
| 166 | public function getController() |
| 167 | { |
| 168 | return array_key_exists('controller', $this->data) ? $this->data['controller'] : null; |
| 169 | } |
| 170 | |
| 171 | public function getAction() |
| 172 | { |
| 173 | return array_key_exists('action', $this->data) ? $this->data['action'] : null; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Try to get the action's icon the old way. |
| 178 | * I/O impact (file_exists) is limited as the results can be serialized. |
| 179 | * |
| 180 | * @return Icon the icon with the src property set to the icon URL. |
| 181 | */ |
| 182 | private function inferLegacyIcon() |
| 183 | { |
| 184 | $ext = $this->getExtensionId(); |
| 185 | $name = strtolower(\tao_helpers_Display::textCleaner($this->data['name'])); |
| 186 | $file = $ext . '/views/img/actions/' . $name . '.png'; |
| 187 | $src = 'actions/' . $name . '.png'; |
| 188 | if (file_exists(ROOT_PATH . $file)) { |
| 189 | return Icon::fromArray(['src' => $src], $ext); |
| 190 | } elseif (file_exists(ROOT_PATH . 'tao/views/img/actions/' . $name . '.png')) { |
| 191 | return Icon::fromArray(['src' => $src], 'tao'); |
| 192 | } else { |
| 193 | return Icon::fromArray( |
| 194 | [ |
| 195 | 'src' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAAAnRSTlMA/1uRIrUAAAA' |
| 196 | . 'KSURBVHjaY/gPAAEBAQAcsIyZAAAAAElFTkSuQmCC' |
| 197 | ], |
| 198 | 'tao' |
| 199 | ); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Check whether the current is allowed to see this action (against ACL). |
| 205 | * @deprecated Wrong layer. Should be called at the level of the controller |
| 206 | * @return bool true if access is granted |
| 207 | */ |
| 208 | public function hasAccess() |
| 209 | { |
| 210 | |
| 211 | \common_Logger::w('Call to deprecated method ' . __METHOD__ . ' in ' . __CLASS__); |
| 212 | |
| 213 | $access = true; |
| 214 | if (!empty($this->data['url'])) { |
| 215 | $access = tao_models_classes_accessControl_AclProxy::hasAccess( |
| 216 | $this->data['action'], |
| 217 | $this->data['controller'], |
| 218 | $this->data['extension'] |
| 219 | ); |
| 220 | } |
| 221 | return $access; |
| 222 | } |
| 223 | |
| 224 | public function getRequiredRights() |
| 225 | { |
| 226 | return $this->getServiceManager()->get(ActionService::SERVICE_ID)->getRequiredRights($this); |
| 227 | } |
| 228 | |
| 229 | public function __toPhpCode() |
| 230 | { |
| 231 | return "new " . __CLASS__ . "(" |
| 232 | . \common_Utils::toPHPVariableString($this->data) . ',' |
| 233 | . \common_Utils::toPHPVariableString(self::SERIAL_VERSION) |
| 234 | . ")"; |
| 235 | } |
| 236 | |
| 237 | public function getWeight() |
| 238 | { |
| 239 | return $this->data['weight']; |
| 240 | } |
| 241 | } |