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

441 lines 9.8 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 $args[ 'data-wrapper-width'] = str_replace( 'auto', '', $args[ 'data-wrapper-width'] );
199 if ( $args[ 'data-wrapper-width'] == ',,' ) {
200 unset( $args[ 'data-wrapper-width'] );
201 }
202 }
203 $sectionHeight = $this->wrapperSize->getResponsiveSizes( "height" );
204 if ( ! $this->wrapperSize->hasNoResponsiveSize( $sectionHeight ) ) {
205 $args['data-wrapper-height'] = implode( ',', $sectionHeight );
206 $args[ 'data-wrapper-height'] = str_replace( 'auto', '', $args[ 'data-wrapper-height'] );
207 if ( $args[ 'data-wrapper-height'] == ',,' ) {
208 unset( $args[ 'data-wrapper-height'] );
209 }
210 }
211 }
212
213 if ( !empty( $this->options ) ) {
214 // check for autoplay option
215 if ( !empty( $this->options->slideshowDuration ) ) {
216 $args['data-slideshow-duration'] = $this->options->slideshowDuration;
217 }
218 // check for autoplay option
219 if ( !empty( $this->options->pauseSlideshow ) ) {
220 $args['data-slideshow-pause'] = $this->options->pauseSlideshow ? "true" : "false";
221 }
222 }
223
224 if ( $this->background->hasBackground() && $this->parallax && false !== $parallaxData = $this->parallax->getParallaxAttrs() ) {
225 $args = Arr::merge( $args, $parallaxData );
226 }
227
228 $actions = Helper::getActions( $this->actions );
229 if ( !empty( $actions ) ) {
230 $args = Arr::merge( $args, [
231 'data-actions' => $actions
232 ]);
233 }
234
235 if ( $this->animation && false !== $animationData = $this->animation->getAnimationAttrs() ) {
236 $args = Arr::merge( $args, $animationData );
237 }
238
239 $div = Html::div($args);
240
241 // check if section has link
242 if ( $this->hasEnabledLink() ) {
243 // Section link anchor element should always have target="_black" unless openInNewTab is false.
244 $urlArgs = isset( $this->options->url->openInNewTab ) && ! $this->options->url->openInNewTab ? [] : ['target' => '_blank'];
245 $urlArgs['class'] = Selector::prefixify('section-link');
246
247 if ( $urlPath = $this->getSectionUrl() ) {
248 $div->nest( Html::a( '', $urlPath, $urlArgs ) . "\n" );
249 }
250 }
251 if( $sectionBackground = $this->background->render() ){
252 $this->background->setDataSheet( $this->getDataSheet() );
253
254 if ( $this->kenBurns && false !== $kenBurnsData = $this->kenBurns->getKenBurnsAttrs() ) {
255 $this->background->setKenBurnsData( $kenBurnsData );
256 }
257
258 $div->nest( "\n" . $this->background->render() . "\n\n" );
259 }
260
261 $this->styleList = Arr::merge( $this->styleList, $this->getSelectorAndCssList() );
262
263 if ( !empty( $this->elementObjects ) ) {
264 foreach ( $this->elementObjects as $elementObject ) {
265 // if dataSheet is available for current section, assign it elements of this section as well
266 if( $this->getDataSheet() ){
267 $elementObject->prepare()->setDataSheet( $this->getDataSheet() );
268 }
269 // Get element style
270 $this->styleList = Arr::merge( $this->styleList, $elementObject->prepare()->getSelectorAndCssList() );
271
272 $div->nest( "\n" . $elementObject->prepare()->render() );
273
274 // generate and retrieve media files of elements for preloading
275 $this->preloadTags .= $elementObject->prepare()->getPreloadTags();
276 }
277 }
278
279 return "\n" . $div;
280 }
281
282 /**
283 * Whether linking slide ro url is enabled or not
284 *
285 * @return bool
286 */
287 public function hasEnabledLink(){
288 return $this->isLinkedToDataSheet() || !empty( $this->options->url->enable );
289 }
290
291 /**
292 * Retrieves markup for preloading media files belonging to this section
293 *
294 * @return string
295 */
296 public function getPreloadTags(){
297 return $this->preloadTags;
298 }
299
300 /**
301 * Retrieves section url if exists
302 *
303 * @return string
304 */
305 public function getSectionUrl(){
306 if( $this->isLinkedToDataSheet() && $url = $this->getDataSheetUrl() ){
307 return $url;
308 } elseif( ! empty( $this->options->url->path ) ){
309 return $this->options->url->path;
310 }
311 return '';
312 }
313
314 /**
315 * Collect element font and add it to document font service
316 */
317 public function collectElementFonts(){
318 if ( !empty( $this->elementObjects ) ) {
319 foreach ( $this->elementObjects as $elementObject ) {
320 $elementObject->prepare()->getFontsList();
321 }
322 }
323 }
324
325 /**
326 * Get section class names.
327 *
328 * @return string
329 */
330 protected function getClassNames(){
331 $classes = [];
332
333 $classes[] = Selector::prefixify(Selector::SECTION_PREFIX );
334 $classes[] = $this->getSelector();
335 $classes[] = $this->getDataSheetClassName();
336 $classes[] = $this->getCustomClassName();
337
338 return trim( implode(' ', $classes) );
339 }
340
341 /**
342 * Get section CSS ID for data sheet if dataSource is assigned
343 *
344 * @return string
345 */
346 public function getDataSheetClassName() {
347 if( $this->getDataSheetID() ){
348 return $this->getCssID();
349 }
350 return '';
351 }
352
353 /**
354 * Retrieves custom class name of document
355 *
356 * @return string
357 */
358 protected function getCustomClassName(){
359 if ( ! empty( $this->className ) ) {
360 return $this->className;
361 }
362 return '';
363 }
364
365 /**
366 * Retrieves custom styles of document
367 *
368 * @return string
369 */
370 protected function getCustomStyles(){
371 if ( ! empty( $this->options->customStyle ) ) {
372 $customStyles = $this->options->customStyle;
373 // replace "selector" with unique selector of section
374 return str_replace('selector', '.'.$this->getStyleSelector(), $customStyles );
375 }
376 return '';
377 }
378
379 /**
380 * Get Elements Style that presented in this section.
381 *
382 * @return array
383 */
384 public function getCss() {
385 return $this->styleList;
386 }
387
388 /**
389 * Get section CSS ID
390 *
391 * @return string
392 */
393 public function getCssID() {
394 $cssID = Helper::getSectionCssId( $this->getDocumentID(), $this->getID() );
395 if( $dataSheetId = $this->getDataSheetID() ){
396 $cssID .= "-{$dataSheetId}";
397 }
398
399 return $cssID;
400 }
401
402 /**
403 * Get section selector
404 *
405 * @return string
406 */
407 public function getSelector() {
408 return Selector::getUniqueSelector( $this->getDocumentID(), $this->getID(), null, Selector::SECTION_PREFIX );
409 }
410
411 /**
412 * Get section style selector
413 *
414 * @return string
415 */
416 public function getStyleSelector() {
417 return Selector::PREFIX_CSS . " ." . $this->getSelector();
418 }
419
420 /**
421 * Get list of selector and CSS for section
422 *
423 * @return array
424 */
425 protected function getSelectorAndCssList(){
426 $styleList = [];
427
428 $styleList[ '.' . $this->getStyleSelector() . ' .' . $this->background->getContainerClassName() ] = array_merge_recursive(
429 $this->background->getColor(),
430 $this->background->getSectionBackgroundFilter()
431 );
432 $styleList[ '.' . $this->getStyleSelector() . ' .' . $this->background->getContainerClassName() . '::after' ] = $this->background->getOverlayStyles();
433
434 if ( $this->getCustomStyles() ) {
435 $styleList[ '.' . $this->getStyleSelector() ]['customStyle'] = $this->getCustomStyles();
436 }
437
438 return $styleList;
439 }
440 }
441