FRED™  3.0
FRED™: Framework for Rapid and Easy Development
View.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred\Controller;
4 
5 class View extends \Rsi\Fred\Component{
6 
7  public $convert = false;
8  public $minifiedPath = null;
9  public $numericCheck = false; //!< Convert numeric strings to numbers.
10  public $maxInt = 0x7fffffff; //!< Higher numbers (integers) will not be converted (from string).
11  public $menuCaptionId = 'menu-*'; //!< String ID for the menu item caption (an asterisk will be replaced with the controller
12  // name).
13 
14  protected $_title = null;
15 
16  protected $_controller = null;
17 
18  protected function init(){
19  parent::init();
20  $this->publish('title');
21  }
22 
23  public function jsonEncode($data){
24  if($this->numericCheck) array_walk_recursive($data,function(&$value){ //JSON_NUMERIC_CHECK removes leading zeros and plus signs
25  if(\Rsi\Str::numeric($value) && (strpos('.',$value) || (abs($value) < $this->maxInt))) $value += 0;
26  });
27  return json_encode($data,$this->_fred->debug ? JSON_PRETTY_PRINT : 0);
28  }
29  /**
30  * Filter menu items on user rights.
31  * @param array $menu Array with menu items. Each item is an array with (optional) keys:
32  * - caption: caption for the menu item (when not provided the global menuCaptionId is used).
33  * - controller: controller (page) for this item.
34  * - link: link for this page (overrides reverse controller name route, but can also be used in absence of controller).
35  * - right: right the user must have to view this page, and thus the menu item (controller default right is also checked).
36  * - items: array with sub-menu items.
37  * @param string $type View type (empty = default; true = current).
38  * @return array Nested array with only viewable items, or items with sub-items. Key = caption. value = array with:
39  * - link: link for the item.
40  * - items: sub items (always present; might be empty).
41  */
42  public function menu($menu,$type = null){
43  extract($this->components('front','router','trans','user'));
44  if($type === true) $type = $router->viewtype;
45  $result = [];
46  if($menu) foreach($menu as $item){
47  if($controller = $item['controller'] ?? null) $controller = $front->controllerFactory($controller);
48  $right = $item['right'] ?? null;
49  if((!$controller || $front->controllerAuthorized($controller->name)) && (!$right || $user->authorized($right))){
50  $link = ($item['link'] ?? null) ?: ($controller ? $router->reverse($controller->name,$type) : null);
51  $items = $this->menu($item['items'] ?? null);
52  if($link || $items) $result[$item['caption'] ?? $trans->id(str_replace('*',$controller->name,$this->menuCaptionId))] =
53  compact('link','items');
54  }
55  }
56  return $result;
57  }
58 
59  public function render(){
60  if(!headers_sent()) header('Content-type: application/json; charset=utf-8');
61  print($this->jsonEncode($data = $this->data));
62  if($error = json_last_error())
63  $this->component('log')->error('JSON encoding error: ' . json_last_error_msg(),__FILE__,__LINE__,compact('data','error'));
64  }
65 
66  protected function getData(){
67  $request = $this->component('request');
68  $data = [
69  'data' => $request->data,
70  'result' => $request->result,
71  ];
72  if(!$request->action) $data['config'] = array_merge(
73  $this->_fred->clientConfig(),
74  ['controller' => $this->_controller->clientConfig()]
75  );
76  if($request->errors) $data['errors'] = $request->errors;
77  if($request->redir) $data['redir'] = $request->redir;
78  elseif($messages = $this->component('message')->retrieve()) $data['messages'] = $messages;
79  return $data;
80  }
81 
82 }
Rsi\Fred\Controller\View
Definition: View.php:5
Rsi\Fred\Controller\View\$minifiedPath
$minifiedPath
Definition: View.php:8
Rsi\Fred\Controller
Rsi
Rsi\Fred\Component\components
components(... $names)
Get multiple components in an array.
Definition: Component.php:90
Rsi\Fred\Controller\View\jsonEncode
jsonEncode($data)
Definition: View.php:23
Rsi\Fred\Controller\View\getData
getData()
Definition: View.php:66
Rsi\Fred\Component
Basic component class.
Definition: Component.php:8
Rsi\Fred\Controller\View\init
init()
Definition: View.php:18
Rsi\Fred\Controller\View\$_controller
$_controller
Definition: View.php:16
Rsi\Fred\Component\component
component($name)
Get a component (local or default).
Definition: Component.php:81
Rsi\Fred\Controller\View\menu
menu($menu, $type=null)
Filter menu items on user rights.
Definition: View.php:42
Rsi\Thing\publish
publish($property, $visibility=self::READABLE)
Publish a property (or hide it again).
Definition: Thing.php:28
Rsi\Fred\Controller\View\$_title
$_title
Definition: View.php:14
Rsi\Fred\Controller\View\$maxInt
$maxInt
Higher numbers (integers) will not be converted (from string).
Definition: View.php:10
Rsi\Fred\Controller\View\$numericCheck
$numericCheck
Convert numeric strings to numbers.
Definition: View.php:9
Rsi\Fred\Controller\View\render
render()
Definition: View.php:59
Rsi\Fred\Controller\View\$convert
$convert
Definition: View.php:7
Rsi\Fred\Controller\View\$menuCaptionId
$menuCaptionId
String ID for the menu item caption (an asterisk will be replaced with the controller.
Definition: View.php:11