Issues (73)

src/Chuck/PageBlockRepository.php (4 issues)

1
<?php
2
3
namespace Chuckbe\Chuckcms\Chuck;
4
5
use Chuckbe\Chuckcms\Models\Form;
6
use Chuckbe\Chuckcms\Models\PageBlock;
7
use Chuckbe\Chuckcms\Models\Repeater;
8
use Chuckbe\Chuckcms\Models\Resource;
9
use ChuckSite;
10
use View;
11
12
class PageBlockRepository
13
{
14
    protected $pageblock;
15
16
    public function __construct(PageBlock $pageblock)
17
    {
18
        $this->pageblock = $pageblock;
19
    }
20
21
    public function getRenderedByPageBlocks($ogpageblocks)
22
    {
23
        $pageblocks = [];
24
        foreach ($ogpageblocks as $pageblock) {
25
            $body = $pageblock->body;
26
            $findUrlTags = $this->getResources($body, '[%', '%]');
27
            $url = env('APP_URL', ChuckSite::getSetting('domain'));
28
            if (count($findUrlTags) > 0) {
29
                foreach ($findUrlTags as $foundUrlTag) {
30
                    if (strpos($foundUrlTag, 'URL') !== false) {
31
                        $body = str_replace('[%URL%]', $url, $body);
32
                    }
33
                }
34
            }
35
36
            $findThis = $this->getResources($body, '[', ']');
37
38
            // THERE ARE DYNAMICS IN THIS PAGEBLOCK, LET'S RETRIEVE IT
39
            if (count($findThis) > 0) {
40
                //@todo LOOP OVER findThis variable and resolve order for rendering
41
42
                // PAGEBLOCK CONTAINS A LOOP, LET'S RETRIEVE IT
43
                if (strpos($findThis[0], 'LOOP') !== false) {
44
                    $repeater_slug = implode(' ', $this->getResources($body, '[LOOP=', ']'));
45
                    $repeater_body = implode(' ', $this->getResources($body, '[LOOP='.$repeater_slug.']', '[/LOOP]'));
46
47
                    $newbody = str_replace('[LOOP='.$repeater_slug.']'.$repeater_body.'[/LOOP]', $this->getRepeaterContents($repeater_slug, $repeater_body), $body);
48
49
                // THERE IS NO LOOP, CONTINUE
50
                } elseif (strpos($findThis[0], 'FORM') !== false) {// PAGEBLOCK CONTAINS A FORM, LET'S RETRIEVE IT
51
                    $form_slug = implode(' ', $this->getResources($body, '[FORM=', ']'));
52
53
                    $newbody = $this->getFormHtml($form_slug, $body);
54
                } else {// THERE IS NO FORM, SO JUST RETRIEVE THE DYNAMIC CONTENT
55
                    $newbody = $this->getResourceContent($findThis, $pageblock->id, $body); //Maybe write a function in the model?
56
                }
57
            } else {
58
                $newbody = $body;
59
            }
60
            $pageblocks[] = [
61
                'id'      => $pageblock->id,
62
                'page_id' => $pageblock->page_id,
63
                'name'    => $pageblock->name,
64
                'slug'    => $pageblock->slug,
65
                'body'    => $newbody,
66
                'raw'     => $pageblock->body,
67
                'order'   => $pageblock->order,
68
            ];
69
        }
70
71
        return $pageblocks;
72
    }
73
74
    public function getRenderedByPageBlock($pageblock)
75
    {
76
        $body = $pageblock->body;
77
        $findUrlTags = $this->getResources($body, '[%', '%]');
78
        $url = env('APP_URL', ChuckSite::getSetting('domain'));
79
        if (count($findUrlTags) > 0) {
80
            foreach ($findUrlTags as $foundUrlTag) {
81
                if (strpos($foundUrlTag, 'URL') !== false) {
82
                    $body = str_replace('[%URL%]', $url, $body);
83
                }
84
            }
85
        }
86
87
        $findThis = $this->getResources($body, '[', ']');
88
        // THERE ARE DYNAMICS IN THIS PAGEBLOCK, LET'S RETRIEVE IT
89
        if (count($findThis) > 0) {
90
            //@todo LOOP OVER findThis variable and resolve order for rendering
91
92
            // PAGEBLOCK CONTAINS A LOOP, LET'S RETRIEVE IT
93
            if (strpos($findThis[0], 'LOOP') !== false) {
94
                $repeater_slug = implode(' ', $this->getResources($body, '[LOOP=', ']'));
95
                $repeater_body = implode(' ', $this->getResources($body, '[LOOP='.$repeater_slug.']', '[/LOOP]'));
96
97
                $newbody = str_replace('[LOOP='.$repeater_slug.']'.$repeater_body.'[/LOOP]', $this->getRepeaterContents($repeater_slug, $repeater_body), $body);
98
99
            // THERE IS NO LOOP, CONTINUE
100
            } elseif (strpos($findThis[0], 'FORM') !== false) {// PAGEBLOCK CONTAINS A FORM, LET'S RETRIEVE IT
101
                $form_slug = implode(' ', $this->getResources($body, '[FORM=', ']'));
102
103
                $newbody = $this->getFormHtml($form_slug, $body);
104
            } else {// THERE IS NO FORM, SO JUST RETRIEVE THE DYNAMIC CONTENT
105
                $newbody = $this->getResourceContent($findThis, $pageblock->id, $body); //Maybe write a function in the model?
106
            }
107
        } else {
108
            $newbody = $body;
109
        }
110
        $new_pageblock = [
111
            'id'      => $pageblock->id,
112
            'page_id' => $pageblock->page_id,
113
            'name'    => $pageblock->name,
114
            'slug'    => $pageblock->slug,
115
            'body'    => $newbody,
116
            'raw'     => $pageblock->body,
117
            'lang'    => $pageblock->lang,
118
        ];
119
120
        return $new_pageblock;
121
    }
122
123
    public function updateBody($pageblock, $html)
124
    {
125
        $pageblock->body = $html;
126
        $pageblock->update();
127
128
        return $this->getRenderedByPageBlock($pageblock);
129
    }
130
131
    public function getResources($str, $startDelimiter, $endDelimiter)
132
    {
133
        $contents = [];
134
        $startDelimiterLength = strlen($startDelimiter);
135
        $endDelimiterLength = strlen($endDelimiter);
136
        $startFrom = 0;
137
        while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) {
138
            $contentStart += $startDelimiterLength;
139
            $contentEnd = strpos($str, $endDelimiter, $contentStart);
140
            if (false === $contentEnd) {
141
                break;
142
            }
143
            $contents[] = substr($str, $contentStart, $contentEnd - $contentStart);
144
            $startFrom = $contentEnd + $endDelimiterLength;
145
        }
146
147
        return $contents;
148
    }
149
150
    public function getResourceContent($resources, $page_block_id, $page_block_body)
0 ignored issues
show
The parameter $page_block_id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

150
    public function getResourceContent($resources, /** @scrutinizer ignore-unused */ $page_block_id, $page_block_body)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
151
    {
152
        $body = $page_block_body;
153
        foreach ($resources as $resource) {
154
            $res_arr = explode('+', $resource);
155
            $res_slug = (string) $res_arr[0];
156
            $res_json = (string) $res_arr[1];
157
            $res_object = Resource::where('slug', $res_slug)->first();
158
            $json = $res_object->json[app()->getLocale()];
159
            $match = $json[$res_json];
160
            $body = str_replace('['.$res_slug.'+'.$res_json.']', $match, $body);
161
        }
162
163
        return $body;
164
    }
165
166
    public function getFormHtml($form_slug, $page_block_body)
167
    {
168
        $slug = $form_slug;
169
        $form = Form::where('slug', $slug)->first();
170
        $render = View::make('chuckcms::backend.forms.render', ['form' => $form])->render();
171
        $html = str_replace('[FORM='.$slug.']', $render, $page_block_body);
172
173
        return $html;
174
    }
175
176
    public function getRepeaterContents($repeater_slug, $page_block_body)
177
    {
178
        $slug = $repeater_slug;
179
        if (strpos($repeater_slug, ' LIMIT=') !== false) {
180
            $explode = explode(' LIMIT=', $repeater_slug);
181
            $slug = $explode[0];
182
            $limit = (int) $explode[1];
183
            $contents = Repeater::where('slug', $slug)->take($limit)->get();
184
        } else {
185
            $contents = Repeater::where('slug', $slug)->get();
186
        }
187
        $resources = $this->getResources($page_block_body, '['.$slug.'+', ']');
188
        $new_body = [];
189
        $i = 0;
190
        foreach ($contents as $content) {
191
            $body = $page_block_body;
192
            foreach ($resources as $resource) {
193
                $json = $content->json;
194
                $match = $json[$resource];
195
                $body = str_replace('['.$slug.'+'.$resource.']', $match, $body);
196
            }
197
            $new_body[] = $body;
198
            $i++;
199
        }
200
        $str_body = implode(' ', $new_body);
201
202
        return $str_body;
203
    }
204
205
    public function moveUpById($id)
206
    {
207
        $pageblock = $this->pageblock->find($id);
208
        $og_order = $pageblock->order;
209
        $target_pb = $this->pageblock->where('page_id', $pageblock->page_id)->where('lang', $pageblock->lang)->where('order', ($og_order - 1))->first();
210
211
        $pageblock->order = $og_order - 1;
212
        $target_pb->order = $og_order;
213
214
        $pageblock->update();
215
        $target_pb->update();
216
217
        return $this->getRenderedByPageBlock($pageblock);
218
    }
219
220
    public function moveDownById($id)
221
    {
222
        $pageblock = $this->pageblock->find($id);
223
        $og_order = $pageblock->order;
224
        $target_pb = $this->pageblock->where('page_id', $pageblock->page_id)->where('lang', $pageblock->lang)->where('order', ($og_order + 1))->first();
225
226
        $pageblock->order = $og_order + 1;
227
        $target_pb->order = $og_order;
228
229
        $pageblock->update();
230
        $target_pb->update();
231
232
        return $this->getRenderedByPageBlock($pageblock);
233
    }
234
235
    public function moveOrderDownByPageId($id)
236
    {
237
        $pageblocks = $this->where('page_id', $id)->increment('order');
0 ignored issues
show
The assignment to $pageblocks is dead and can be removed.
Loading history...
238
    }
239
240
    public function moveOrderUpByPageId($id)
241
    {
242
        $pageblocks = $this->where('page_id', $id)->decrement('order');
0 ignored issues
show
The assignment to $pageblocks is dead and can be removed.
Loading history...
243
    }
244
245
    public function deleteById($id)
246
    {
247
        $pageblock = $this->pageblock->find($id);
248
        if ($pageblock) {
249
            $og_order = $pageblock->order;
250
            //Decrement order of following pageblocks
251
            $pageblocks = $this->pageblock->where('page_id', $pageblock->page_id)->where('lang', $pageblock->lang)->where('order', '>', $og_order)->decrement('order');
0 ignored issues
show
The assignment to $pageblocks is dead and can be removed.
Loading history...
252
            //Delete pageblock
253
            if ($pageblock->delete()) {
254
                return 'success';
255
            } else {
256
                return 'error';
257
            }
258
        } else {
259
            return 'false';
260
        }
261
    }
262
}
263