PluginProbe
Depicter — Popup & Slider Builder / 1.5.2
Depicter — Popup & Slider Builder v1.5.2
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Document / Models / Section.php

Section.php in Depicter — Popup & Slider Builder 1.5.2, at app/src/Document/Models/Section.php

422 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Document\Models;
3
4 use Averta\Core\Utility\Arr;
5 use Depicter\Document\CSS\Selector;
6 use Depicter\Document\Helper\Helper;
7 use Depicter\Document\Models\Traits\HasDataSheetTrait;
8 use Depicter\Document\Models\Traits\HasDocumentIdTrait;
9 use Depicter\Html\Html;
10
11 class Section
12 {
13 use HasDocumentIdTrait;
14 use HasDataSheetTrait;
15
16 /**
17 * @var string
18 */
19 public $id;
20
21 /**
22 * @var string
23 */
24 public $name;
25
26 /**
27 * List of belonging element ids (assigns with jsonMapper)
28 *
29 * @var array
30 */
31 public $elements;
32
33 /**
34 * List of belonging elements
35 *
36 * @var array
37 */
38 public $elementObjects;
39
40 /**
41 * @var Common\Size\States
42 */
43 public $wrapperSize;
44
45 /**
46 * @var Common\Background
47 */
48 public $background;
49
50 /**
51 * @var object
52 */
53 public $options;
54
55 /**
56 * @var \Depicter\Document\Models\Common\Animation|null
57 */
58 public $animation;
59
60 /**
61 * @var Common\Parallax|null
62 */
63 public $parallax;
64
65 /**
66 * @var Common\DataSourceOptions|null
67 */
68 public $dataSource = null;
69
70 /**
71 * @var array|null
72 */
73 public $actions;
74
75 /**
76 * @var string
77 */
78 public $className = '';
79
80 /**
81 * @var array
82 */
83 protected $styleList = [];
84
85 /**
86 * @var string
87 */
88 protected $preloadTags = '';
89
90
91 /**
92 * get section ID
93 *
94 * @return string
95 */
96 public function getID() {
97 return Helper::getSectionIdFromSlug( $this->id );
98 }
99
100 /**
101 * get section slug
102 *
103 * @return string
104 */
105 public function getSlug() {
106 return $this->id;
107 }
108
109 /**
110 * Get section name
111 *
112 * @return string
113 */
114 public function getName() {
115 return $this->name ? $this->name : $this->id;
116 }
117
118 /**
119 * Gets belonging element ids
120 *
121 * @return array
122 */
123 public function getElementIds()
124 {
125 return $this->elements;
126 }
127
128 /**
129 * Sets belonging elements
130 *
131 * @param array $elementIds
132 */
133 public function setElementIds( $elementIds )
134 {
135 $this->elements = $elementIds;
136 }
137
138 /**
139 * Sets belonging elements
140 *
141 * @param array $elementObjects
142 */
143 public function setElementObjects( $elementObjects )
144 {
145 $this->elementObjects = $elementObjects;
146 }
147
148 /**
149 * Renders the Section
150 */
151 public function render() {
152 // render section by dataSource if exists
153 if( $this->dataSource && $sectionMarkup = $this->renderDataSource() ){
154 return $sectionMarkup;
155 }
156 return $this->renderStatic();
157 }
158
159 /**
160 * Renders the Section from dataSource
161 */
162 protected function renderDataSource() {
163 $sectionMarkup = '';
164 $dataSourceInstance = \Depicter::dataSource()->getByType( $this->dataSource->type );
165 if( $dataSourceInstance ){
166 $dataSheets = $dataSourceInstance->getRecordsWithTags( $this->dataSource->params );
167 foreach( $dataSheets as $dataSheet ){
168 $this->setDataSheet( $dataSheet );
169 $sectionMarkup .= $this->renderStatic();
170 }
171 }
172 return $sectionMarkup;
173 }
174
175
176 /**
177 * Renders the Section
178 */
179 protected function renderStatic() {
180
181 // default Section properties
182 $args = [
183 'id' => $this->getCssID(),
184 'class' => $this->getClassNames(),
185 'data-name' => $this->getName()
186 ];
187
188 if ( !empty( $this->wrapperSize ) ) {
189 $sectionWidth = $this->wrapperSize->getResponsiveSizes( "width" );
190 if ( ! $this->wrapperSize->hasNoResponsiveSize( $sectionWidth ) ) {
191 $args[ 'data-wrapper-width'] = implode( ',', $sectionWidth );
192 }
193 $sectionHeight = $this->wrapperSize->getResponsiveSizes( "height" );
194 if ( ! $this->wrapperSize->hasNoResponsiveSize( $sectionHeight ) ) {
195 $args['data-wrapper-height'] = implode( ',', $sectionHeight );
196 }
197 }
198
199 if ( !empty( $this->options ) ) {
200 // check for autoplay option
201 if ( !empty( $this->options->slideshowDuration ) ) {
202 $args['data-slideshow-duration'] = $this->options->slideshowDuration;
203 }
204 // check for autoplay option
205 if ( !empty( $this->options->pauseSlideshow ) ) {
206 $args['data-slideshow-pause'] = $this->options->pauseSlideshow ? "true" : "false";
207 }
208 }
209
210 if ( $this->background->hasBackground() && $this->parallax && false !== $parallaxData = $this->parallax->getParallaxAttrs() ) {
211 $args = Arr::merge( $args, $parallaxData );
212 }
213
214 $actions = Helper::getActions( $this->actions );
215 if ( !empty( $actions ) ) {
216 $args = Arr::merge( $args, [
217 'data-actions' => $actions
218 ]);
219 }
220
221 if ( $this->animation && false !== $animationData = $this->animation->getAnimationAttrs() ) {
222 $args = Arr::merge( $args, $animationData );
223 }
224
225 $div = Html::div($args);
226
227 // check if section has link
228 if ( $this->hasEnabledLink() ) {
229 // Section link anchor element should always have target="_black" unless openInNewTab is false.
230 $urlArgs = isset( $this->options->url->openInNewTab ) && ! $this->options->url->openInNewTab ? [] : ['target' => '_blank'];
231 $urlArgs['class'] = Selector::prefixify('section-link');
232
233 if ( $urlPath = $this->getSectionUrl() ) {
234 $div->nest( Html::a( '', $urlPath, $urlArgs ) . "\n" );
235 }
236 }
237 if( $sectionBackground = $this->background->render() ){
238 $this->background->setDataSheet( $this->getDataSheet() );
239 $div->nest( "\n" . $this->background->render() . "\n\n" );
240 }
241
242 $this->styleList = Arr::merge( $this->styleList, $this->getSelectorAndCssList() );
243
244 if ( !empty( $this->elementObjects ) ) {
245 foreach ( $this->elementObjects as $elementObject ) {
246 // if dataSheet is available for current section, assign it elements of this section as well
247 if( $this->getDataSheet() ){
248 $elementObject->prepare()->setDataSheet( $this->getDataSheet() );
249 }
250 // Get element style
251 $this->styleList = Arr::merge( $this->styleList, $elementObject->prepare()->getSelectorAndCssList() );
252
253 $div->nest( "\n" . $elementObject->prepare()->render() );
254
255 // generate and retrieve media files of elements for preloading
256 $this->preloadTags .= $elementObject->prepare()->getPreloadTags();
257 }
258 }
259
260 return "\n" . $div;
261 }
262
263 /**
264 * Whether linking slide ro url is enabled or not
265 *
266 * @return bool
267 */
268 public function hasEnabledLink(){
269 return $this->isLinkedToDataSheet() || !empty( $this->options->url->enable );
270 }
271
272 /**
273 * Retrieves markup for preloading media files belonging to this section
274 *
275 * @return string
276 */
277 public function getPreloadTags(){
278 return $this->preloadTags;
279 }
280
281 /**
282 * Retrieves section url if exists
283 *
284 * @return string
285 */
286 public function getSectionUrl(){
287 if( $this->isLinkedToDataSheet() && $url = $this->getDataSheetUrl() ){
288 return $url;
289 } elseif( ! empty( $this->options->url->path ) ){
290 return $this->options->url->path;
291 }
292 return '';
293 }
294
295 /**
296 * Collect element font and add it to document font service
297 */
298 public function collectElementFonts(){
299 if ( !empty( $this->elementObjects ) ) {
300 foreach ( $this->elementObjects as $elementObject ) {
301 $elementObject->prepare()->getFontsList();
302 }
303 }
304 }
305
306 /**
307 * Get section class names.
308 *
309 * @return string
310 */
311 protected function getClassNames(){
312 $classes = [];
313
314 $classes[] = Selector::prefixify(Selector::SECTION_PREFIX );
315 $classes[] = $this->getSelector();
316 $classes[] = $this->getDataSheetClassName();
317 $classes[] = $this->getCustomClassName();
318
319 return trim( implode(' ', $classes) );
320 }
321
322 /**
323 * Get section CSS ID for data sheet if dataSource is assigned
324 *
325 * @return string
326 */
327 public function getDataSheetClassName() {
328 if( $this->getDataSheetID() ){
329 return $this->getCssID();
330 }
331 return '';
332 }
333
334 /**
335 * Retrieves custom class name of document
336 *
337 * @return string
338 */
339 protected function getCustomClassName(){
340 if ( ! empty( $this->className ) ) {
341 return $this->className;
342 }
343 return '';
344 }
345
346 /**
347 * Retrieves custom styles of document
348 *
349 * @return string
350 */
351 protected function getCustomStyles(){
352 if ( ! empty( $this->options->customStyle ) ) {
353 $customStyles = $this->options->customStyle;
354 // replace "selector" with unique selector of section
355 return str_replace('selector', '.'.$this->getStyleSelector(), $customStyles );
356 }
357 return '';
358 }
359
360 /**
361 * Get Elements Style that presented in this section.
362 *
363 * @return array
364 */
365 public function getCss() {
366 return $this->styleList;
367 }
368
369 /**
370 * Get section CSS ID
371 *
372 * @return string
373 */
374 public function getCssID() {
375 $cssID = Helper::getSectionCssId( $this->getDocumentID(), $this->getID() );
376 if( $dataSheetId = $this->getDataSheetID() ){
377 $cssID .= "-{$dataSheetId}";
378 }
379
380 return $cssID;
381 }
382
383 /**
384 * Get section selector
385 *
386 * @return string
387 */
388 public function getSelector() {
389 return Selector::getUniqueSelector( $this->getDocumentID(), $this->getID(), null, Selector::SECTION_PREFIX );
390 }
391
392 /**
393 * Get section style selector
394 *
395 * @return string
396 */
397 public function getStyleSelector() {
398 return Selector::PREFIX_CSS . " ." . $this->getSelector();
399 }
400
401 /**
402 * Get list of selector and CSS for section
403 *
404 * @return array
405 */
406 protected function getSelectorAndCssList(){
407 $styleList = [];
408
409 $styleList[ '.' . $this->getStyleSelector() . ' .' . $this->background->getContainerClassName() ] = array_merge_recursive(
410 $this->background->getColor(),
411 $this->background->getSectionBackgroundFilter()
412 );
413 $styleList[ '.' . $this->getStyleSelector() . ' .' . $this->background->getContainerClassName() . '::after' ] = $this->background->getOverlayStyles();
414
415 if ( $this->getCustomStyles() ) {
416 $styleList[ '.' . $this->getStyleSelector() ]['customStyle'] = $this->getCustomStyles();
417 }
418
419 return $styleList;
420 }
421 }
422