1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Dashboard;
13
14 use ICanBoogie\I18n;
15
16 use Brickrouge\Element;
17
18 class DashboardBlock extends Element
19 {
20 static protected function add_assets(\Brickrouge\Document $document)
21 {
22 parent::add_assets($document);
23
24 $document->css->add(DIR . 'public/dashboard.css');
25 $document->js->add(DIR . 'public/dashboard.js');
26 }
27
28 29 30 31 32
33 protected $module;
34
35 public function __construct(Module $module)
36 {
37 $this->module = $module;
38
39 parent::__construct('div', array('id' => 'dashboard'));
40 }
41
42 public function render_inner_html()
43 {
44 global $core;
45
46 $panels = $core->configs->synthesize('dashboard', 'merge');
47
48 foreach ($panels as $i => $panel)
49 {
50 $panels[$i] += array
51 (
52 'column' => 0,
53 'weight' => 0
54 );
55 }
56
57 $user_config = $core->user->metas['dashboard.order'];
58
59 if ($user_config)
60 {
61 $user_config = json_decode($user_config);
62
63 foreach ($user_config as $column_index => $user_panels)
64 {
65 foreach ($user_panels as $panel_weight => $panel_id)
66 {
67 $panels[$panel_id]['column'] = $column_index;
68 $panels[$panel_id]['weight'] = $panel_weight;
69 }
70 }
71 }
72
73 uasort($panels, function($a, $b) { return $a['weight'] - $b['weight']; });
74
75 $html = $this->render_panels($panels);
76
77 return <<<EOT
78 <div id="dashboard-panels">
79 $html
80 </div>
81 EOT;
82 }
83
84 protected function render_panels(array $panels)
85 {
86 $colunms = array
87 (
88 array(),
89 array()
90 );
91
92
93
94 foreach ($panels as $id => $descriptor)
95 {
96 try
97 {
98 if (empty($descriptor['callback']))
99 {
100 continue;
101 }
102
103 $contents = call_user_func($descriptor['callback']);
104 }
105 catch (\Exception $e)
106 {
107 $contents = \Brickrouge\render_exception($e);
108 }
109
110 if (!$contents)
111 {
112 continue;
113 }
114
115 $title = I18n\t($id, array(), array('scope' => 'dashboard.title', 'default' => $descriptor['title']));
116
117 $panel = <<<EOT
118 <div class="panel" id="$id">
119 <div class="panel-title">$title</div>
120 <div class="panel-contents">$contents</div>
121 </div>
122 EOT;
123
124 $colunms[$descriptor['column']][] = $panel;
125 }
126
127 $html = '';
128
129 foreach ($colunms as $i => $panels)
130 {
131 $panels = implode(PHP_EOL, $panels);
132
133 $html .= <<<EOT
134 <div class="column">
135 $panels
136 <div class="panel-holder"> </div>
137 </div>
138 EOT;
139 }
140
141 return $html;
142 }
143 }