PluginProbe
Depicter — Popup & Slider Builder / 1.6.2
Depicter — Popup & Slider Builder v1.6.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.6.2, at app/src/Document/Models/Section.php

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