<?php

/**
 * News notification extension for Contao
 *
 * Copyright (c) 2016 Benjamin Roth
 *
 * @link    http://www.esales-media.de
 * @license commercial
 */

/**
 * Palettes
 */

$GLOBALS['TL_DCA']['tl_member']['palettes']['default'] = str_replace(array(',email,','email;'),array(',email,nc_news_subscribe,',',email,nc_news_subscribe;'),$GLOBALS['TL_DCA']['tl_member']['palettes']['default']);


/**
 * Fields
 */

$GLOBALS['TL_DCA']['tl_member']['fields']['nc_news_subscribe'] = array
(
  'label'                   => &$GLOBALS['TL_LANG']['tl_member']['nc_news_subscribe'],
  'exclude'                 => true,
  'inputType'               => 'checkbox',
  'eval'                    => array('tl_class'=>'w50 m12', 'doNotCopy'=>true, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'contact'),
  'save_callback'           => array
  (
    array('tl_member_eSM_nc_news', 'setNcNewsChangedate'),
  ),
  'sql'                     => "char(1) NOT NULL default '1'"
);

$GLOBALS['TL_DCA']['tl_member']['fields']['nc_news_subscribe_changed'] = array
(
  'label'                   => &$GLOBALS['TL_LANG']['tl_member']['nc_news_subscribe_changed'],
  'eval'                    => array('rgxp'=>'datim'),
  'sql'                     => "int(10) unsigned NOT NULL default '0'"
);


/**
 * Provide miscellaneous methods that are used by the data configuration array.
 *
 * @author Benjamin Roth <http://www.esales-media.de>
 */
class tl_member_eSM_nc_news extends \Backend
{

  public function __construct()
  {
    parent::__construct();
    $this->import('BackendUser', 'User');
  }

  /**
   * Reset field
   *
   * @return array
   */
  public function setNcNewsChangedate($varValue, DataContainer $dc)
  {
    if ($varValue != $dc->activeRecord->nc_news_subscribe)
    {
      \Database::getInstance()->prepare("UPDATE tl_member SET nc_news_subscribe_changed=? WHERE id=?")->execute(array(time(),$dc->id));
    }

    return $varValue;
  }
}