1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Editor;
13
14 use ICanBoogie\Modules\Thumbnailer\Thumbnail;
15 use ICanBoogie\Operation;
16
17 use Brickrouge\Element;
18
19 20 21
22 class RTEEditor implements Editor
23 {
24 25 26 27 28
29 public function serialize($content)
30 {
31 return $content;
32 }
33
34 35 36 37 38
39 public function unserialize($serialized_content)
40 {
41 return $serialized_content;
42 }
43
44 45 46 47 48 49
50 public function render($content)
51 {
52 return preg_replace_callback
53 (
54 '#<img\s+[^>]+>#', function($match)
55 {
56 global $core;
57
58 preg_match_all('#([\w\-]+)\s*=\s*\"([^"]+)#', $match[0], $attributes);
59
60 $attributes = array_combine($attributes[1], $attributes[2]);
61 $attributes = array_map(function($v) { return html_entity_decode($v, ENT_COMPAT, \ICanBoogie\CHARSET); }, $attributes);
62 $attributes += array
63 (
64 'width' => null,
65 'height' => null,
66 'data-nid' => null
67 );
68
69 $w = $attributes['width'];
70 $h = $attributes['height'];
71 $nid = $attributes['data-nid'];
72
73 if ($w && $h && $nid)
74 {
75 $attributes['src'] = Operation::encode('images/' . $nid . '/' . $w . 'x' . $h);
76 }
77 else if (($w || $h) && preg_match('#^/repository/files/image/(\d+)#', $attributes['src'], $matches))
78 {
79 $nid = $matches[1];
80
81 $options = $attributes;
82
83 unset($options['src']);
84
85 $thumbnail = new Thumbnail($core->models['images'][$nid], $options);
86
87 $attributes['src'] = $thumbnail->url;
88 }
89
90 $path = null;
91
92 if (isset($attributes['data-lightbox']) && $nid)
93 {
94 $attributes['src'] = preg_replace('#\&lightbox=true#', '', $attributes['src']);
95 $path = $core->models['images']->select('path')->filter_by_nid($nid)->rc;
96 }
97
98 unset($attributes['data-nid']);
99 unset($attributes['data-lightbox']);
100
101 $rc = (string) new Element('img', $attributes);
102
103 if ($path)
104 {
105 $rc = '<a href="' . \ICanBoogie\escape($path) . '" rel="lightbox[]">' . $rc . '</a>';
106 }
107
108 return $rc;
109 },
110
111 $content
112 );
113 }
114
115 116 117 118 119
120 public function from(array $attributes)
121 {
122 return new RTEEditorElement($attributes);
123 }
124 }