| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
class CampaignBuilder |
| 6 |
{ |
| 7 |
use CampaignBuilderLegacy; |
| 8 |
|
| 9 |
/** |
| 10 |
* Build campaign Template. |
| 11 |
* |
| 12 |
* @return string |
| 13 |
*/ |
| 14 |
|
| 15 |
/** |
| 16 |
* @var \Hurrytimer\Campaign |
| 17 |
*/ |
| 18 |
protected $campaign; |
| 19 |
|
| 20 |
public function __construct($campaign) |
| 21 |
{ |
| 22 |
$this->campaign = $campaign; |
| 23 |
$this->campaign->loadSettings(); |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Returns built template. |
| 29 |
* |
| 30 |
* @param string |
| 31 |
* @param boolean |
| 32 |
* |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
public function build($content = '', $withConfig = true) |
| 36 |
{ |
| 37 |
$config = $this->getClientConfig(); |
| 38 |
$json = json_encode($config); |
| 39 |
$legacyClass = $this->legacyCampaignClass($this->campaign->getId()); |
| 40 |
if ($this->campaign->enableSticky === C::YES) { |
| 41 |
|
| 42 |
return "<div class='hurrytimer-sticky hurrytimer-sticky-{$this->campaign->getId()}'><div class='hurrytimer-sticky-inner'>" |
| 43 |
. "<div class='{$legacyClass} hurrytimer-campaign hurrytimer-campaign-{$this->campaign->getId()}'" |
| 44 |
. "data-config='{$json}' >{$content}</div></div>" . $this->stickyBarCloseButton() ."</div>"; |
| 45 |
} |
| 46 |
return "<div class='{$legacyClass} hurrytimer-campaign hurrytimer-campaign-{$this->campaign->getId()}'" |
| 47 |
. "data-config='{$json}' >{$content}</div>"; |
| 48 |
} |
| 49 |
|
| 50 |
public function template() |
| 51 |
{ |
| 52 |
return |
| 53 |
($this->campaign->headlinePosition == C::HEADLINE_POSITION_ABOVE_TIMER |
| 54 |
? $this->headline() |
| 55 |
: '') |
| 56 |
. '<div class="' . $this->legacyTimerClass() . ' hurrytimer-timer"></div>' |
| 57 |
. ($this->campaign->headlinePosition == C::HEADLINE_POSITION_BELOW_TIMER |
| 58 |
? $this->headline() : '') |
| 59 |
. $this->callToActionButton(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns common client config. |
| 64 |
* |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
private function commonClientConfig() |
| 68 |
{ |
| 69 |
return [ |
| 70 |
'id' => $this->campaign->getId(), |
| 71 |
'actions' => $this->campaign->actions, |
| 72 |
'template' => $this->timer(), |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
private function evergreenClientConfig() |
| 77 |
{ |
| 78 |
$evergreenCompaign = new EvergreenCampaign( |
| 79 |
$this->campaign->getId(), |
| 80 |
new CookieDetection(), |
| 81 |
new IPDetection() |
| 82 |
); |
| 83 |
|
| 84 |
return [ |
| 85 |
'isRegular' => false, |
| 86 |
'duration' => $this->campaign->getDurationInSeconds(), |
| 87 |
'reset' => $evergreenCompaign->reseting(), |
| 88 |
'restart' => $this->campaign->getRestart(), |
| 89 |
'endDate' => intval($evergreenCompaign->getEndDate()), |
| 90 |
'cookieName' => $evergreenCompaign->cookieName(), |
| 91 |
]; |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns regular config. |
| 97 |
* |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
private function regularClientConfig() |
| 101 |
{ |
| 102 |
|
| 103 |
return [ |
| 104 |
'isRegular' => true, |
| 105 |
'endDate' => date( |
| 106 |
'Y/m/d H:i:s', |
| 107 |
strtotime($this->campaign->getEndDatetime())), |
| 108 |
]; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Returns client config for the compaign. |
| 113 |
* |
| 114 |
* @return array|null |
| 115 |
*/ |
| 116 |
public function getClientConfig() |
| 117 |
{ |
| 118 |
if ($this->campaign->isEvergreen()) { |
| 119 |
return array_merge($this->commonClientConfig(), $this->evergreenClientConfig()); |
| 120 |
|
| 121 |
} else { |
| 122 |
return array_merge($this->commonClientConfig(), $this->regularClientConfig()); |
| 123 |
|
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns timer. |
| 129 |
* |
| 130 |
* @return string |
| 131 |
*/ |
| 132 |
public function timer() |
| 133 |
{ |
| 134 |
$blocks = array_filter([ |
| 135 |
$this->daysBlock(), |
| 136 |
$this->hoursBlock(), |
| 137 |
$this->minutesBlock(), |
| 138 |
$this->secondsBlock(), |
| 139 |
]); |
| 140 |
|
| 141 |
return implode($this->separator(), $blocks); |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Returns separator. |
| 147 |
* |
| 148 |
* @return string |
| 149 |
*/ |
| 150 |
public function separator() |
| 151 |
{ |
| 152 |
return $this->campaign->blockSeparatorVisibility === C::YES |
| 153 |
? '<div class="' . $this->legacySeparatorClass() . ' hurrytimer-timer-sep">:</div>' |
| 154 |
: ''; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Returns days block. |
| 159 |
* |
| 160 |
* @return string |
| 161 |
*/ |
| 162 |
public function daysBlock() |
| 163 |
{ |
| 164 |
return $this->campaign->daysVisibility === C::YES |
| 165 |
? $this->block("%D", $this->campaign->labels['days']) |
| 166 |
: ''; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Returns hours block. |
| 171 |
* |
| 172 |
* @return string |
| 173 |
*/ |
| 174 |
public function hoursBlock() |
| 175 |
{ |
| 176 |
return $this->campaign->hoursVisibility === C::YES |
| 177 |
? $this->block("%H", $this->campaign->labels['hours']) |
| 178 |
: ''; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Returns minutes block. |
| 183 |
* |
| 184 |
* @return string |
| 185 |
*/ |
| 186 |
public function minutesBlock() |
| 187 |
{ |
| 188 |
return $this->campaign->minutesVisibility === C::YES |
| 189 |
? $this->block("%M", $this->campaign->labels['minutes']) |
| 190 |
: ''; |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Returns seconds block. |
| 196 |
* |
| 197 |
* @return string |
| 198 |
*/ |
| 199 |
public function secondsBlock() |
| 200 |
{ |
| 201 |
return $this->campaign->secondsVisibility === C::YES |
| 202 |
? $this->block("%S", $this->campaign->labels['seconds']) |
| 203 |
: ''; |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Returns block. |
| 209 |
* |
| 210 |
* @param $digitFormat |
| 211 |
* @param $label |
| 212 |
* |
| 213 |
* @return string |
| 214 |
*/ |
| 215 |
public function block($digitFormat, $label) |
| 216 |
{ |
| 217 |
return '<div class="hurrytimer-timer-block ' . $this->legacyBlockClass() . '">' |
| 218 |
. $this->digit($digitFormat) |
| 219 |
. $this->label($label) |
| 220 |
. '</div>'; |
| 221 |
} |
| 222 |
|
| 223 |
public function digit($format) |
| 224 |
{ |
| 225 |
return '<div class="hurrytimer-timer-digit ' . $this->legacyDigitClass() . '">' . $format |
| 226 |
. '</div>'; |
| 227 |
} |
| 228 |
|
| 229 |
public function label($text) |
| 230 |
{ |
| 231 |
if ($this->campaign->labelVisibility === "no") { |
| 232 |
return ''; |
| 233 |
} |
| 234 |
|
| 235 |
return '<div class="hurrytimer-timer-label ' . $this->legacyLabelClass() . '" >' |
| 236 |
. __($text, 'hurrytimer') . '</div>'; |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
public function headline() |
| 241 |
{ |
| 242 |
return $this->campaign->headlineVisibility === C::YES |
| 243 |
? '<div class="' . $this->legacyHeadlineClass() . ' hurrytimer-headline">' |
| 244 |
. __($this->campaign->headline, 'hurrytimer') . '</div>' |
| 245 |
: ''; |
| 246 |
} |
| 247 |
|
| 248 |
public function callToActionButton() |
| 249 |
{ |
| 250 |
if ($this->campaign->callToAction['enabled'] === C::NO) { |
| 251 |
return ''; |
| 252 |
} |
| 253 |
|
| 254 |
$target = $this->campaign->callToAction['new_tab'] === C::YES ? '_blank' : '_self'; |
| 255 |
return "<div class='hurrytimer-button-wrap'><a class='hurrytimer-button' target='" . $target . "' href='" . $this->campaign->callToAction['url'] . "' >" . $this->campaign->callToAction['text'] . "</a></div>"; |
| 256 |
} |
| 257 |
|
| 258 |
public function stickyBarCloseButton() |
| 259 |
{ |
| 260 |
if ($this->campaign->stickyBarDismissible === C::NO || |
| 261 |
isset($_COOKIE['_dismissed_sticky_' . $this->campaign->getId()]) |
| 262 |
) { |
| 263 |
return ''; |
| 264 |
} |
| 265 |
|
| 266 |
if ($this->campaign->stickyBarDismissible === C::YES) { |
| 267 |
return '<button type="button" class="hurrytimer-sticky-close"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 357 357"> |
| 268 |
<polygon points="357,35.7 321.3,0 178.5,142.8 35.7,0 0,35.7 142.8,178.5 0,321.3 35.7,357 178.5,214.2 321.3,357 357,321.3 |
| 269 |
214.2,178.5"/></svg></button>'; |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
} |
| 276 |
|