iframe();
$f ->select('pos', _t('@banners', 'Banner Position'), false, function () use ($positions){
$result = [];
foreach ($positions as $v) {
$result[] = ['id' => $v['id'], 'title' => $v['title'] . ' (' . $v['sizes'] . ')'];
}
return $result;
})
->jsOnChange(function () use ($positions) { ?>
onValidate(function ($param, $field) use ($form) {
/** @var bff\tpl\admin\form\Select $field */
if (empty($param['value'])) {
Errors::set(_t('@banners', 'Please specify a position'), $field->name());
return false;
}
return true;
})
;
$f ->checkboxList('devices', _t('@banners', 'Devices'), function($field) use ($form, $devices){
/** @var bff\tpl\admin\form\CheckboxList $field */
$data = $form->_data();
$raw = $data[$field->name()] ?? [];
if(empty($raw)) {
$raw[] = bff::DEVICE_ANY;
}
$result = [];
$result[] = [
'id' => bff::DEVICE_ANY,
'title' => _t('@', 'All devices'),
'checked' => in_array(bff::DEVICE_ANY, $raw),
];
foreach ($devices as $k => $v) {
$result[] = [
'id' => $k,
'title' => $v['t'],
'checked' => in_array($k, $raw),
];
}
return $result;
}, false, ['attr' => ['class' => 'j-device']])
->beforeFieldRender(function($html, $field) use ($form) { $name = $field->name(); ?>
= $html ?>
rule(TYPE_NOCLEAN)
->onValidate(function($param) use ($form, $devices) {
$data = $param['value'];
$result = [];
foreach ($data as $k => $v) {
if (empty($v)) {
continue;
}
if (! isset($devices[$k])) {
continue;
}
$result[] = $k;
}
if (empty($result)) {
$result = [bff::DEVICE_ANY];
}
$param['value'] = $result;
return true;
})
;
$f ->checkboxList('pages', _t('@banners', 'Website Section'), function ($field) use ($form) {
/** @var bff\tpl\admin\form\CheckboxList $field */
$data = $form->_data();
$raw = $data[$field->name()] ?? [];
if(empty($raw)) {
$raw[] = Banners::PAGE_ANY;
}
$pages = Banners::getPages();
$result = [];
foreach ($pages as $v) {
$result[] = [
'id' => $v['id'],
'title' => $v['title'],
'checked' => in_array($v['id'], $raw),
];
}
return $result;
}, false, ['attr' => ['class' => 'j-pages']])
->beforeFieldRender(function ($html) use ($form) { ?>
= $html ?>
rule(TYPE_NOCLEAN)
->onValidate(function ($param) use ($form) {
$data = $param['value'];
$result = [];
foreach ($data as $k => $v) {
if (empty($v)) continue;
$result[] = $k;
}
$param['value'] = $result;
return true;
})
;
$f ->select('_list_pos', _t('@banners', 'Position Number In the List'))
->noFieldsList()
->option(Banners::LIST_POS_FIRST, _t('@banners', 'First'))
->option(1, _t('@banners', 'Specified'))
->option(Banners::LIST_POS_LAST, _t('@banners', 'Last'))
->onValue(function () use ($form) {
$data = $form->_data();
if (isset($data['list_pos'])) {
if ($data['list_pos'] == Banners::LIST_POS_LAST) {
return Banners::LIST_POS_LAST;
}
if (in_array($data['list_pos'], [Banners::LIST_POS_FIRST, 1])) {
return Banners::LIST_POS_FIRST;
}
}
return 1;
})
->classAdd('j-list-pos')
;
$f ->number('list_pos', '', -1)
->together('_list_pos')
->visibleIf('_list_pos', 1)
->onValidate(function ($param) use ($form) {
$value = & $param['value'];
$result = & $param['result'];
if (empty($value)) {
$value = 1;
}
if (isset($result['_list_pos'])) {
if ($result['_list_pos'] == Banners::LIST_POS_LAST) {
$value = Banners::LIST_POS_LAST;
}
if ($result['_list_pos'] == Banners::LIST_POS_FIRST) {
$value = 1;
}
unset($result['_list_pos']);
}
return true;
})
;
$f ->checkboxList('locale', _t('@banners', 'Localization'), function ($field) use ($form) {
/** @var bff\tpl\admin\form\CheckboxList $field */
$data = $form->_data();
$raw = $data[$field->name()] ?? [];
if(empty($raw)) {
$raw[] = Banners::LOCALE_ALL;
}
$result = [];
$result[] = [
'id' => Banners::LOCALE_ALL,
'title' => _t('@', 'All'),
'checked' => in_array(Banners::LOCALE_ALL, $raw),
];
$locales = Lang::getLanguages(false);
foreach ($locales as $k => $v) {
$result[] = [
'id' => $k,
'title' => $v['title'],
'checked' => in_array($k, $raw),
];
}
return $result;
}, false, ['attr' => ['class' => 'j-locale']])
->beforeFieldRender(function ($html) use ($form) { ?>
= $html ?>
rule(TYPE_NOCLEAN)
->onValidate(function ($param) use ($form) {
$data = $param['value'];
$result = [];
$locales = Lang::getLanguages();
if (sizeof($locales) > 1) {
foreach ($data as $k => $v) {
if (empty($v)) continue;
$result[] = $k;
}
}
if (empty($result)) {
$result[] = Banners::LOCALE_ALL;
}
$param['value'] = $result;
return true;
})
->param('maxHeight', 150)
;
$f ->datepicker('show_start', _t('@banners', 'Start Date of Showing'), tpl::date_format_pub(time(), 'd-m-Y'))
;
$f ->datepicker('show_finish', _t('@banners', 'End Date of Showing'), tpl::date_format_pub(time() + 604800, 'd-m-Y'))
;
$f ->number('show_limit', _t('@banners', 'Limit of Showings'), 0)
->titleRender(function ($html) { ?>= $html ?>
= _t('@banners', '(number)') ?>tip(_t('@banners', '0 - No Limit'))
;
$f ->radio('type', _t('@banners', 'Banner Type'), Banners::TYPE_IMAGE, function () use ($form) {
$types = Banners::types($form->recordID() ? true : false);
$result = [];
foreach ($types as $v) {
$result[$v['id']] = $v['t'];
}
return $result;
},['label' => ['class' => 'radio']])
;
foreach ($devices as $v) {
if (! empty($v['alias'])) {
continue;
}
$f ->custom('img_' . $v['id'], _t('@banners', 'Image'))
->titleRender(function ($html) use($v) { ?>= $html ?>
= $v['t'] ?>getCustom()
->onView(function ($data, $field) use ($form) {
/** @var bff\tpl\admin\form\Custom $field */
$name = $field->name();
$img = false;
if($form->recordID()) {
$data = $form->_data();
if ( ! empty($data['type_data'][$name]['name'])) {
$img = $data['type_data'][$name]['name'];
}
}
if (Admin::themeV2()) {
if ($img) { ?>
; ?>)
end()
->visibleIf('type', Banners::TYPE_IMAGE)
;
}
foreach ($devices as $v) {
if (! empty($v['alias'])) {
continue;
}
$f ->textarea('code_'.$v['id'], _t('@banners', 'Code'), '', false)
->classAdd('j-field-link-'.$v['id'])
->titleRender(function ($html, $field) use ($form, $v) {
$name = $field->name();
$macroses = [
'{query}' => _tejs('@banners', 'Search query text'),
'{click_url}' => _tejs('@banners', 'Conversion Count Link'),
];
$content = '';
foreach ($macroses as $kk => $vv) {
$content .= '';
}
?>
= $html ?>
= $v['t'] ?>
visibleIf('type', Banners::TYPE_CODE)
->onValidate(function ($param, $field) use ($form) {
$name = $field->name();
$value = $param['value'];
$result = & $param['result'];
if ( ! isset($result['type']) || $result['type'] != Banners::TYPE_CODE) {
return false;
}
$res = [$name => ['html' => $value]];
if (isset($result['type_data'])) {
$result['type_data'] = array_merge($result['type_data'], $res);
} else {
$result['type_data'] = $res;
}
return false;
})
->onValue(function ($value, $field) use ($form) {
$name = $field->name();
if ($form->recordID()) {
$data = $form->_data();
if ( ! empty($data['type_data'][$name]['html'])) {
return $data['type_data'][$name]['html'];
}
}
return '';
})
;
}
$f ->text('click_url', _t('@banners', 'Link'), '', false)
->onValidate(function ($param, $field) use ($form) {
/** @var bff\tpl\admin\form\Text $field */
$value = & $param['value'];
$result = $param['result'];
if (! isset($result['type'])) {
Errors::impossible();
return false;
}
if ($result['type'] != Banners::TYPE_CODE) {
if (empty($value) || $value === '#') {
Errors::set(_t('@banners', 'Please specify a correct link'), $field->name());
return false;
} else {
if (preg_match('/^(http|https|ftp):\/\//xisu', $value) !== 1) {
if (mb_stripos($value, 'www.') === 0) {
# корректируем протокол, если отсутствует
$value = 'http://' . $value;
} else {
if ($value[0] !== '/') {
# корректируем относительные ссылки
$value = '/' . $value;
}
}
}
}
}
return true;
})
;
$f ->checkbox('target_blank', '')
->label(_t('@banners', 'Open in New Window'))
->visibleIf('type', Banners::TYPE_IMAGE)
;
$f ->custom('link', _t('@banners', 'Conversion Count Link'))->getCustom()
->onView(function () use ($form) {
$id = $form->recordID();
if (! $id) {
$id = Banners::model()->bannerNextID();
} ?>
end()
->onValidate(function () { return false; })
;
$f ->text('url_match', _t('@banners', 'URL of Showing'), '', false)
->titleRender(function ($html) { ?>= $html ?>
= _t('@banners', '(relative URL)'); ?>tip(_t('@banners', 'The banner will be displayed only on the page with the specified URL.'))
->onValidate(function ($param, $field) use ($form) {
/** @var bff\tpl\admin\form\Text $field */
$value = & $param['value'];
if ( ! empty($value)) {
# приводим к корректной относительной ссылке
if (preg_match('/^(http|https|ftp):\/\//xisu', $value) === 1) {
$value = parse_url($value, PHP_URL_PATH);
} else {
if (strpos($value, 'www.') === 0) {
$value = mb_strcut($value, 4);
}
}
if (!empty($value) && $value[0] !== '/') {
$value = '/' . $value;
}
}
return true;
})
;
$f ->checkbox('url_match_exact', '')
->label(_t('@banners', 'Do Not Include Sub-pages (regarding this URL)'))
;
$f ->text('title', _t('@banners', 'Title'), '', false);
$f ->text('alt', _t('@banners', 'Alt'), '', false);
$f ->textarea('description', _t('@banners', 'Note'), '', false, ['rows' => 3]);
$f ->checkbox('enabled', _t('@', 'Enabled'))
->onValidate(function ($param) use ($form) {
$value = & $param['value'];
$result = $param['result'];
if ($value) {
# Если на данной позиции запрещена ротация нескольких баннеров
if (! Banners::checkPositionRotation($result['pos'])) {
$id = $form->recordID();
if ($id) {
$prev = Banners::model()->banner($id, ['*'], ['regions'])->toArray();
if (! $prev['enabled']) {
$value = 0;
}
} else {
$value = 0;
}
}
}
return true;
})
;
$f ->contentWrapper(function ($html) use ($form) { ?>
= $html ?>