rendererHelper = $rendererHelper;
$this->wrapper = $wrapper;
$this->blockStylesRenderer = $blockStylesRenderer;
$this->wp = $wp;
}
public function render(array $block, array $formSettings, ?int $formId = null): string {
$html = '';
$html .= $this->rendererHelper->renderLabel($block, $formSettings);
$html .= $this->renderDateSelect($formId, $block, $formSettings);
return $this->wrapper->render($block, $html);
}
private function renderDateSelect(?int $formId, array $block = [], $formSettings = []): string {
$html = '';
$fieldName = 'data[' . $this->rendererHelper->getFieldName($block) . ']';
$dateFormats = $this->getDateFormats();
// automatically select first date format
$dateFormat = $dateFormats[$block['params']['date_type']][0];
// set date format if specified
if (
isset($block['params']['date_format'])
&& strlen(trim($block['params']['date_format'])) > 0
) {
$dateFormat = $block['params']['date_format'];
}
// generate an array of selectors based on date format
$dateSelectors = explode('/', $dateFormat);
foreach ($dateSelectors as $dateSelector) {
if ($dateSelector === 'DD') {
$html .= '';
} else if ($dateSelector === 'MM') {
$html .= '';
} else if ($dateSelector === 'YYYY') {
$html .= '';
}
}
$html .= $this->rendererHelper->renderErrorsContainer($block, $formId);
return $html;
}
public function getDateTypes(): array {
return [
'year_month_day' => $this->wp->escHtml(__('Year, month, day', 'mailpoet')),
'year_month' => $this->wp->escHtml(__('Year, month', 'mailpoet')),
'month' => $this->wp->escHtml(__('Month (January, February,...)', 'mailpoet')),
'year' => $this->wp->escHtml(__('Year', 'mailpoet')),
];
}
public function getDateFormats(): array {
return [
'year_month_day' => ['MM/DD/YYYY', 'DD/MM/YYYY', 'YYYY/MM/DD'],
'year_month' => ['MM/YYYY', 'YYYY/MM'],
'year' => ['YYYY'],
'month' => ['MM'],
];
}
public function getMonthNames(): array {
return [__('January', 'mailpoet'), __('February', 'mailpoet'), __('March', 'mailpoet'), __('April', 'mailpoet'),
__('May', 'mailpoet'), __('June', 'mailpoet'), __('July', 'mailpoet'), __('August', 'mailpoet'), __('September', 'mailpoet'),
__('October', 'mailpoet'), __('November', 'mailpoet'), __('December', 'mailpoet'),
];
}
private function getMonths(array $block = []): string {
$defaults = [
'selected' => null,
];
if (!empty($block['params']['value'])) {
$date = CarbonImmutable::createFromFormat('Y-m-d H:i:s', $block['params']['value']);
if ($date instanceof CarbonImmutable) {
$defaults['selected'] = (int)date('m', $date->getTimestamp());
}
} elseif (!empty($block['params']['is_default_today'])) {
// is default today
$defaults['selected'] = (int)date('m');
}
// merge block with defaults
$block = array_merge($defaults, $block);
$monthNames = $this->getMonthNames();
$html = '';
// empty value label
$html .= '';
for ($i = 1; $i < 13; $i++) {
$isSelected = ($i === $block['selected']) ? 'selected="selected"' : '';
$html .= '';
}
return $html;
}
private function getYears(array $block = []): string {
$defaults = [
'selected' => null,
'from' => (int)date('Y') - 100,
'to' => (int)date('Y'),
];
if (!empty($block['params']['value'])) {
$date = CarbonImmutable::createFromFormat('Y-m-d H:i:s', $block['params']['value']);
if ($date instanceof CarbonImmutable) {
$defaults['selected'] = (int)date('Y', $date->getTimestamp());
}
} elseif (!empty($block['params']['is_default_today'])) {
// is default today
$defaults['selected'] = (int)date('Y');
}
// merge block with defaults
$block = array_merge($defaults, $block);
$html = '';
// empty value label
$html .= '';
// return years as an array
for ($i = (int)$block['to']; $i > (int)($block['from'] - 1); $i--) {
$isSelected = ($i === $block['selected']) ? 'selected="selected"' : '';
$html .= '';
}
return $html;
}
private function getDays(array $block = []): string {
$defaults = [
'selected' => null,
];
if (!empty($block['params']['value'])) {
$date = CarbonImmutable::createFromFormat('Y-m-d H:i:s', $block['params']['value']);
if ($date instanceof CarbonImmutable) {
$defaults['selected'] = (int)date('d', $date->getTimestamp());
}
} elseif (!empty($block['params']['is_default_today'])) {
// is default today
$defaults['selected'] = (int)date('d');
}
// merge block with defaults
$block = array_merge($defaults, $block);
$html = '';
// empty value label
$html .= '';
// return days as an array
for ($i = 1; $i < 32; $i++) {
$isSelected = ($i === $block['selected']) ? 'selected="selected"' : '';
$html .= '';
}
return $html;
}
}