SuiteMozaik   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 7
Bugs 7 Features 1
Metric Value
c 7
b 7
f 1
dl 0
loc 249
ccs 0
cts 165
cp 0
rs 10
wmc 26
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentLipsum() 0 3 1
A getContentImageSample() 0 12 2
A getDependenciesHTML() 0 11 1
A getIncludeHTML() 0 7 1
B getElementHTML() 0 59 4
A getAllHTML() 0 10 2
A getRefreshTextareaScript() 0 22 2
B getThumbs() 0 24 5
A getThumbImageHTML() 0 9 2
B __construct() 0 20 6
1
<?php
2
3
class SuiteMozaik {
4
5
    private $mozaikPath = 'include/javascript/mozaik';
6
    private $vendorPath;
7
8
    private static $defaultThumbnails = array(
9
        'headline' => array(
10
            'label' => 'Headline',
11
            //'tpl' => 'tpls/default/headline.html',
12
            'tpl' => 'string:<p><h1>Add your headline here..</h1></p>',
13
            'thumbnail' => 'tpls/default/thumbs/headline.png',
14
        ),
15
        'content' => array(
16
            'label' => 'Content',
17
            'tpl' => 'string:<h2>Title</h2><p>{lipsum}</p>',
18
            'thumbnail' => 'tpls/default/thumbs/content1.png',
19
        ),
20
        'content2' => array(
21
            'label' => 'Content with two columns',
22
            'tpl' => 'string:<table style="width:100%;"><tbody><tr><td><h2>Title</h2></td><td><h2>Title</h2></td></tr><tr><td>{lipsum}</td><td>{lipsum}</td></tr></tbody></table>',
23
            'thumbnail' => 'tpls/default/thumbs/content2.png',
24
        ),
25
        'content3' => array(
26
            'label' => 'Content with three columns',
27
            'tpl' => 'string:<table style="width:100%;"><tbody><tr><td><h2>Title</h2></td><td><h2>Title</h2></td><td><h2>Title</h2></td></tr><tr><td>{lipsum}</td><td>{lipsum}</td><td>{lipsum}</td></tr></tbody></table>',
28
            'thumbnail' => 'tpls/default/thumbs/content3.png',
29
        ),
30
        'image1left' => array(
31
            'label' => 'Content with left image',
32
            'tpl' => 'string:<table style="width:100%;"><tbody><tr><td>{imageSmall}</td><td><h2>Title</h2>{lipsum}</td></tr></tbody></table>',
33
            'thumbnail' => 'tpls/default/thumbs/image1left.png',
34
        ),
35
        'image1right' => array(
36
            'label' => 'Content with right image',
37
            'tpl' => 'string:<table style="width:100%;"><tbody><tr><td><h2>Title</h2>{lipsum}</td><td>{imageSmall}</td></tr></tbody></table>',
38
            'thumbnail' => 'tpls/default/thumbs/image1right.png',
39
        ),
40
        'image2' => array(
41
            'label' => 'Content with two image',
42
            'tpl' => 'string:<table style="width:100%;"><tbody><tr><td>{imageSmall}</td><td><h2>Title</h2>{lipsum}</td><td>{imageSmall}</td><td><h2>Title</h2>{lipsum}</td></tr></tbody></table>',
43
            'thumbnail' => 'tpls/default/thumbs/image2.png',
44
        ),
45
        'image3' => array(
46
            'label' => 'Content with three image',
47
            'tpl' => 'string:<table style="width:100%;"><tbody><tr><td>{image}</td><td>{image}</td><td>{image}</td></tr><tr><td><h2>Title</h2>{lipsum}</td><td><h2>Title</h2>{lipsum}</td><td><h2>Title</h2>{lipsum}</td></tr></tbody></table>',
48
            'thumbnail' => 'tpls/default/thumbs/image3.png',
49
        ),
50
        'footer' => array(
51
            'label' => 'Footer',
52
            //'tpl' => 'tpls/default/footer.html',
53
            'tpl' => 'string:<p class="footer">Take your footer contents and information here..</p>',
54
            'thumbnail' => 'tpls/default/thumbs/footer.png',
55
        ),
56
    );
57
58
    private $thumbsCache = array();
59
60
    private $autoInsertThumbnails = true;
61
62
    private static $devMode = false;
63
64
    public function __construct() {
65
        $this->vendorPath = $this->mozaikPath . '/vendor';
66
        if($this->autoInsertThumbnails) {
67
            if(count($this->getThumbs())==0 || self::$devMode) {
68
                $ord = 0;
69
                foreach(self::$defaultThumbnails as $thumbName => $thumbData) {
70
                    $templateSectionLine = new TemplateSectionLine();
71
                    $templateSectionLine->name = $thumbData['label'];
72
                    $templateSectionLine->description = preg_replace('/^string:/', '', $thumbData['tpl']);
73
                    $templateSectionLine->description = str_replace('{lipsum}', $this->getContentLipsum(), $templateSectionLine->description);
74
                    $templateSectionLine->description = str_replace('{imageSmall}', $this->getContentImageSample(130), $templateSectionLine->description);
75
                    $templateSectionLine->description = str_replace('{image}', $this->getContentImageSample(), $templateSectionLine->description);
76
                    $templateSectionLine->thumbnail = file_exists($this->mozaikPath . '/' . $thumbData['thumbnail']) ? $this->mozaikPath . '/' . $thumbData['thumbnail'] : null;
77
                    $templateSectionLine->ord = ++$ord;
78
                    $templateSectionLine->save();
79
                }
80
            }
81
            $this->thumbsCache = array();
82
        }
83
    }
84
85
    private function getContentLipsum() {
86
        return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam tempus odio ante, in feugiat ex pretium eu. In pharetra tincidunt urna et malesuada. Etiam aliquet auctor justo eu placerat. In nec sollicitudin enim. Nulla facilisi. In viverra velit turpis, et lobortis nunc eleifend id. Curabitur semper tincidunt vulputate. Nullam fermentum pellentesque ullamcorper.';
87
    }
88
89
    private function getContentImageSample($width = null) {
90
        if(is_numeric($width)) {
91
            $width = ' width="' . $width . '"';
92
        }
93
        else {
94
            $width = '';
95
        }
96
        $splits = explode('index.php', $_SERVER['REQUEST_URI']);
97
        $url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . $splits[0];
98
        $image = '<img src="' . $url . $this->mozaikPath . '/tpls/default/images/sample.jpg" ' . $width . ' />';
99
        return $image;
100
    }
101
102
    public function getDependenciesHTML() {
103
        $html = <<<HTML
104
<script src='{$this->vendorPath}/tinymce/tinymce/tinymce.min.js'></script>
105
<script src="{$this->vendorPath}/gymadarasz/imagesloaded/imagesloaded.pkgd.min.js"></script>
106
107
<!-- for color picker plugin -->
108
<link rel="stylesheet" media="screen" type="text/css" href="{$this->vendorPath}/../colorpicker/css/colorpicker.css" />
109
<script type="text/javascript" src="{$this->vendorPath}/../colorpicker/js/colorpicker.js"></script>
110
HTML;
111
        return $html;
112
    }
113
114
    public function getIncludeHTML() {
115
        $html = <<<HTML
116
<link rel="stylesheet" href="{$this->mozaikPath}/jquery.mozaik.css">
117
<script src='{$this->mozaikPath}/jquery.mozaik.js'></script>
118
HTML;
119
        return $html;
120
    }
121
122
    public function getElementHTML($contents = '', $textareaId = null, $elementId = 'mozaik', $width = 'initial', $thumbs = array()) {
123
        if(is_numeric($width)) {
124
            $width .= 'px';
125
        }
126
        if(!$thumbs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $thumbs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
127
            $thumbs = self::$defaultThumbnails;
128
        }
129
        $thumbsJSON = json_encode($thumbs);
130
        $refreshTextareaScript = '';
131
        if($textareaId) {
132
            $refreshTextareaScript = $this->getRefreshTextareaScript($textareaId, $elementId, $width);
133
        }
134
        $html = <<<HTML
135
<style type="text/css">
136
#{$elementId} {position: relative; top: 0; left: 0;}
137
#{$elementId} ul.mozaik-thumbs li.mozaik-thumbnail {padding: 5px 0;}
138
#{$elementId} ul.mozaik-thumbs li.mozaik-thumbnail:hover {background-color: lightgray;}
139
#{$elementId} .mozaik-thumbnail.ui-draggable.ui-draggable-handle {cursor: -webkit-grab;}
140
#{$elementId} .mozaik-thumbnail.ui-draggable.ui-draggable-handle * {cursor: -webkit-grab;}
141
#{$elementId} .mozaik-thumbnail.ui-draggable.ui-draggable-handle.ui-draggable-dragging {cursor: -webkit-grabbing;}
142
#{$elementId} .mozaik-thumbnail.ui-draggable.ui-draggable-handle.ui-draggable-dragging * {cursor: -webkit-grabbing;}
143
#{$elementId} .mozaik-inner a {text-decoration: underline;}
144
</style>
145
<div id="{$elementId}">{$contents}</div>
146
<script type="text/javascript">
147
    $(function() {
148
        // initialize
149
150
        if(typeof window.mozaikSettings == 'undefined') {
151
            window.mozaikSettings = {};
152
        }
153
154
        window.mozaikSettings.{$elementId} = {
155
            base: '{$this->mozaikPath}/',
156
            thumbs: {$thumbsJSON},
157
            editables: 'editable',
158
            style: 'tpls/default/styles/default.css',
159
            namespace: false,
160
            ace: false,
161
            width: '{$width}'
162
        };
163
164
        window.plgBackground.image = '{$this->mozaikPath}/' + window.plgBackground.image;
165
166
        $('#{$elementId}').mozaik(window.mozaikSettings.{$elementId});
167
168
        $(window).mousemove(function(){
169
            var correction = -( ($('#{$elementId}').width()-100) / 2);
170
            $('#{$elementId} .mozaik-thumbnail.ui-draggable-dragging').css('margin-left', correction + 'px');
171
        });
172
173
    });
174
    // refresh textarea
175
    {$refreshTextareaScript}
176
177
</script>
178
HTML;
179
        return $html;
180
    }
181
182
    public function getAllHTML($contents = '', $textareaId = null, $elementId = 'mozaik', $width = 'initial', $group = '') {
183
        if(is_numeric($width)) {
184
            $width .= 'px';
185
        }
186
        $mozaikHTML = $this->getDependenciesHTML();
187
        $mozaikHTML .= $this->getIncludeHTML();
188
        $thumbs = $this->getThumbs($group);
189
        $mozaikHTML .= $this->getElementHTML($contents, $textareaId, $elementId, $width, $thumbs);
190
        return $mozaikHTML;
191
    }
192
193
    private function getRefreshTextareaScript($textareaId, $elementId, $width = 'initial') {
194
        if(is_numeric($width)) {
195
            $width .= 'px';
196
        }
197
        $js = <<<SCRIPT
198
$(window).mouseup(function(){
199
     $('#{$textareaId}').val($('#{$elementId}').getMozaikValue({width: '{$width}'}));
200
201
     // fix table editor panel
202
     var found = false;
203
     $('.mce-tinymce').each(function(i,e){
204
        if(!$(e).hasClass('mce-tinymce-inline-inside') && $(e).css('display') == 'block'){
205
            found = true;
206
        }
207
     });
208
     if(!found) {
209
        $('.mce-tinymce-inline-inside').css('display', 'none');
210
     }
211
});
212
SCRIPT;
213
        return $js;
214
    }
215
216
    private function getThumbs($group = '') {
217
        $cacheGroup = 'cached_' . $group;
218
219
        if(!isset($this->thumbsCache[$cacheGroup])) {
220
            $db = DBManagerFactory::getInstance();
221
            $_group = $db->quote($group);
222
            $templateSectionLineBean = BeanFactory::getBean('TemplateSectionLine');
223
            $thumbBeans = $templateSectionLineBean->get_full_list('ord', "(grp LIKE '$_group' OR grp IS NULL)");
224
            $thumbs = array();
225
            if ($thumbBeans) {
226
                foreach ($thumbBeans as $thumbBean) {
227
                    $thumbs[$thumbBean->name] = array(
228
                        'label' => $thumbBean->thumbnail ? $this->getThumbImageHTML($thumbBean->thumbnail, $thumbBean->name) : $thumbBean->name,
229
                        'tpl' => 'string:' . html_entity_decode($thumbBean->description),
230
                    );
231
                }
232
            }
233
            $this->thumbsCache[$cacheGroup] = $thumbs;
234
        }
235
236
        $thumbs = $this->thumbsCache[$cacheGroup];
237
238
        return $thumbs;
239
    }
240
241
    private function getThumbImageHTML($src, $label) {
242
        if(file_exists($src)) {
243
            $html = '<img src="' . $src. '" alt="' . $label . '">';
244
        }
245
        else {
246
            $html = $label;
247
        }
248
        return $html;
249
    }
250
251
}