<?php

/*
* This file is part of eSales Media cookieconsent module
*
* (c) Benjamin Roth
*
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL
*/

/**
 * Extend the palettes.
 */
$GLOBALS['TL_DCA']['tl_page']['palettes']['__selector__'][] = 'cookieconsent_enable';
$GLOBALS['TL_DCA']['tl_page']['palettes']['root'] .= ';{cookieconsent_legend},cookieconsent_enable';
if (isset($GLOBALS['TL_DCA']['tl_page']['palettes']['rootfallback'])) {
  $GLOBALS['TL_DCA']['tl_page']['palettes']['rootfallback'] .= ';{cookieconsent_legend},cookieconsent_enable';
}

$GLOBALS['TL_DCA']['tl_page']['subpalettes']['cookieconsent_enable'] = 'cookieconsent_link,cookieconsent_services,cookieconsent_categories_custom,cookieconsent_services_custom';

/*
 * Add the fields
 */
$GLOBALS['TL_DCA']['tl_page']['fields']['cookieconsent_enable'] = array
(
  'label' => &$GLOBALS['TL_LANG']['tl_page']['cookieconsent_enable'],
  'exclude' => true,
  'inputType' => 'checkbox',
  'eval' => array('submitOnChange' => true, 'tl_class' => 'clr'),
  'sql' => "char(1) NOT NULL default ''",
);

$GLOBALS['TL_DCA']['tl_page']['fields']['cookieconsent_link'] = array
(
  'label' => &$GLOBALS['TL_LANG']['tl_page']['cookieconsent_link'],
  'exclude' => true,
  'search' => true,
  'inputType' => 'text',
  'eval' => array('rgxp'=>'url', 'decodeEntities'=>true, 'maxlength'=>255, 'fieldType'=>'radio', 'tl_class'=>'clr w50 wizard'),
  'wizard' => array
  (
    array('tl_content_cookieconsent', 'pagePicker')
  ),
  'sql'=> "varchar(255) NOT NULL default ''"
);

$GLOBALS['TL_DCA']['tl_page']['fields']['cookieconsent_services'] = array
(
  'label' => &$GLOBALS['TL_LANG']['tl_page']['cookieconsent_services'],
  'exclude' => true,
  'inputType' => 'checkbox',
  'options' => array('analytics','matomo'),
  'reference' => &$GLOBALS['TL_LANG']['REF']['tl_page']['cookieconsent_services'],
  'eval' => array('multiple'=>true, 'tl_class' => 'clr'),
  'sql' => "blob NULL",
);

$GLOBALS['TL_DCA']['tl_page']['fields']['cookieconsent_categories_custom'] = array
(
  'label' => &$GLOBALS['TL_LANG']['tl_page']['cookieconsent_categories_custom'],
  'exclude' => true,
  'inputType' => 'textarea',
  'explanation' => 'cookieconsent_categories',
  'eval' => array('allowHtml'=>true, 'class'=>'monospace', 'rte'=>'ace|javascript', 'tl_class'=>'clr', 'helpwizard'=>true),
  'sql'=> "mediumtext NULL"
);

$GLOBALS['TL_DCA']['tl_page']['fields']['cookieconsent_services_custom'] = array
(
  'label' => &$GLOBALS['TL_LANG']['tl_page']['cookieconsent_services_custom'],
  'exclude' => true,
  'inputType' => 'textarea',
  'explanation' => 'cookieconsent_services',
  'eval' => array('allowHtml'=>true, 'class'=>'monospace', 'rte'=>'ace|javascript', 'tl_class'=>'clr', 'helpwizard'=>true),
  'sql'=> "mediumtext NULL"
);

class tl_content_cookieconsent extends Backend
{
  /**
   * Return the link picker wizard
   *
   * @param \DataContainer $dc
   *
   * @return string
   */
  public function pagePicker(\DataContainer $dc)
  {
    return ' <a href="' . (($dc->value == '' || strpos($dc->value, '{{link_url::') !== false) ? 'contao/page.php' : 'contao/file.php') . '?do=' . \Input::get('do') . '&amp;table=' . $dc->table . '&amp;field=' . $dc->field . '&amp;value=' . rawurlencode(str_replace(array('{{link_url::', '}}'), '', $dc->value)) . '&amp;switch=1' . '" title="' . specialchars($GLOBALS['TL_LANG']['MSC']['pagepicker']) . '" onclick="Backend.getScrollOffset();Backend.openModalSelector({\'width\':768,\'title\':\'' . specialchars(str_replace("'", "\\'", $GLOBALS['TL_DCA'][$dc->table]['fields'][$dc->field]['label'][0])) . '\',\'url\':this.href,\'id\':\'' . $dc->field . '\',\'tag\':\'ctrl_'. $dc->field . ((\Input::get('act') == 'editAll') ? '_' . $dc->id : '') . '\',\'self\':this});return false">' . \Image::getHtml('pickpage.gif', $GLOBALS['TL_LANG']['MSC']['pagepicker'], 'style="vertical-align:top;cursor:pointer"') . '</a>';
  }
}