1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Views;
13
14 use ICanBoogie\ActiveRecord;
15 use ICanBoogie\Operation;
16 use ICanBoogie\PropertyNotDefined;
17 use ICanBoogie\Route;
18
19 use Icybee\Modules\Cache\Collection as CacheCollection;
20 use Icybee\Modules\Nodes\Node;
21 use Icybee\Modules\Sites\Site;
22 use ICanBoogie\Routing\Pattern;
23
24 class Hooks
25 {
26 27 28
29
30 31 32 33 34 35
36 static public function on_page_save(Operation\ProcessEvent $event, \Icybee\Modules\Pages\SaveOperation $operation)
37 {
38 global $core;
39
40 $request = $event->request;
41 $contents = $request['contents'];
42 $editor_ids = $request['editors'];
43 $nid = $event->response->rc['key'];
44
45 if ($editor_ids)
46 {
47 foreach ($editor_ids as $content_id => $editor_id)
48 {
49 if ($editor_id != 'view')
50 {
51 continue;
52 }
53
54 if (empty($contents[$content_id]))
55 {
56
57
58 continue;
59 }
60
61 $content = $contents[$content_id];
62
63 if (strpos($content, '/') !== false)
64 {
65 $view_target_key = 'views.targets.' . strtr($content, '.', '_');
66
67 $core->site->metas[$view_target_key] = $nid;
68 }
69 }
70 }
71 }
72
73 74 75 76 77 78
79 static public function on_cache_collection_collect(CacheCollection\CollectEvent $event, CacheCollection $collection)
80 {
81 $event->collection['icybee.views'] = new CacheManager;
82 }
83
84 85 86
87
88 static private $pages_model;
89 static private $url_cache_by_siteid = array();
90
91 92 93 94 95 96 97 98
99 static public function url(ActiveRecord $target, $type='view')
100 {
101 global $core;
102
103 if (self::$pages_model === false)
104 {
105
106
107
108
109 return '#';
110 }
111 else
112 {
113 try
114 {
115 self::$pages_model = $core->models['pages'];
116 }
117 catch (\Exception $e)
118 {
119 return '#';
120 }
121 }
122
123 $constructor = isset($target->constructor) ? $target->constructor : $target->model->id;
124 $constructor = strtr($constructor, '.', '_');
125
126 $key = 'views.targets.' . $constructor . '/' . $type;
127 $site_id = !empty($target->siteid) ? $target->siteid : $core->site_id;
128
129 if (isset(self::$url_cache_by_siteid[$site_id][$key]))
130 {
131 $pattern = self::$url_cache_by_siteid[$site_id][$key];
132 }
133 else
134 {
135 $pattern = false;
136 $page_id = null;
137
138 if ($site_id)
139 {
140 $site = $core->models['sites'][$site_id];
141 $page_id = $site->metas[$key];
142
143 if ($page_id)
144 {
145 $pattern = self::$pages_model[$page_id]->url_pattern;
146 }
147 }
148
149 self::$url_cache_by_siteid[$site_id][$key] = $pattern;
150 }
151
152 if (!$pattern)
153 {
154 return '#uknown-target-for:' . $constructor . '/' . $type;
155 }
156
157 return Pattern::from($pattern)->format($target);
158 }
159
160 161 162 163 164
165 static public function get_url(ActiveRecord $node)
166 {
167 return $node->url('view');
168 }
169
170 171 172 173 174 175
176 static public function absolute_url(ActiveRecord $node, $type='view')
177 {
178 global $core;
179
180 try
181 {
182 $site = $node->site ? $node->site : $core->site;
183 }
184 catch (PropertyNotDefined $e)
185 {
186 $site = $core->site;
187 }
188
189 return $site->url . substr($node->url($type), strlen($site->path));
190 }
191
192 193 194 195 196 197 198
199 static public function get_absolute_url(Node $node)
200 {
201 return $node->absolute_url('view');
202 }
203
204 static private $view_target_cache = array();
205
206 207 208 209 210 211 212 213
214 static public function resolve_view_target(Site $site, $viewid)
215 {
216 global $core;
217
218 if (isset(self::$view_target_cache[$viewid]))
219 {
220 return self::$view_target_cache[$viewid];
221 }
222
223 $targetid = $site->metas['views.targets.' . strtr($viewid, '.', '_')];
224
225 return self::$view_target_cache[$viewid] = $targetid ? $core->models['pages'][$targetid] : false;
226 }
227
228 static private $view_url_cache = array();
229
230 231 232 233 234 235 236 237
238 static public function resolve_view_url(Site $site, $viewid)
239 {
240 if (isset(self::$view_url_cache[$viewid]))
241 {
242 return self::$view_url_cache[$viewid];
243 }
244
245 $target = $site->resolve_view_target($viewid);
246
247 return self::$view_url_cache[$viewid] = $target ? $target->url : '#unknown-target-for-view-' . $viewid;
248 }
249
250 251 252
253
254 255 256 257 258 259 260 261 262
263 static public function markup_call_view(array $args, $engine, $template)
264 {
265 global $core;
266
267 return $core->editors['view']->render($args['name'], $engine, $template);
268 }
269 }