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