<?php

/**
 * Shipment tracking for Isotope eCommerce
 *
 * Copyright (c) 2017 Benjamin Roth
 *
 * @license commercial
 */

/**
 * Table tl_iso_shipmenttracking
 */
$GLOBALS['TL_DCA']['tl_iso_shipmenttracking'] = array
(

	// Config
	'config' => array
	(
		'dataContainer'             => 'Table',
    'enableVersioning'          => true,
    'closed'                    => true,
    'onload_callback' => array
    (
      array('\Isotope\Backend', 'initializeSetupModule'),
      array('tl_iso_shipmenttracking', 'checkPermission'),
      array('tl_iso_shipmenttracking', 'mandatoryCheck'),
    ),
    'sql' => array
    (
      'keys' => array
      (
        'id' => 'primary',
      )
    ),
	),

	// List
	'list' => array
	(
		'sorting' => array
		(
      'mode'                  => 1,
      'fields'                => array('name'),
      'flag'                  => 1,
      'panelLayout'           => 'sort,filter;search,limit',
		),
		'label' => array
    (
      'fields'                => array('name'),
      'format'                => '%s',
    ),
		'global_operations' => array
		(
      'back' => array
      (
        'label'             => &$GLOBALS['TL_LANG']['MSC']['backBT'],
        'href'              => 'mod=&table=',
        'class'             => 'header_back',
        'attributes'        => 'onclick="Backend.getScrollOffset();"',
      ),
      'new' => array
      (
        'label'             => &$GLOBALS['TL_LANG']['tl_iso_shipping']['new'],
        'href'              => 'act=create',
        'class'             => 'header_new',
        'attributes'        => 'onclick="Backend.getScrollOffset();"',
      ),
      'all' => array
      (
        'label'                 => &$GLOBALS['TL_LANG']['MSC']['all'],
        'href'                  => 'act=select',
        'class'                 => 'header_edit_all',
        'attributes'            => 'onclick="Backend.getScrollOffset()" accesskey="e"'
      )
		),
		'operations' => array
		(
      'edit' => array
      (
        'label'               => &$GLOBALS['TL_LANG']['tl_iso_shipmenttracking']['edit'],
        'href'                => 'act=edit',
        'icon'                => 'edit.gif',
      ),
      'copy' => array
      (
        'label'                 => &$GLOBALS['TL_LANG']['tl_iso_shipmenttracking']['copy'],
        'href'                  => 'act=copy',
        'icon'                  => 'copy.gif'
      ),
      'delete' => array
      (
        'label'               => &$GLOBALS['TL_LANG']['tl_iso_shipmenttracking']['delete'],
        'href'                => 'act=delete',
        'icon'                => 'delete.gif',
      ),
      'toggle' => array
      (
        'label'               => &$GLOBALS['TL_LANG']['tl_iso_shipmenttracking']['toggle'],
        'icon'                => 'visible.gif',
        'attributes'          => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"',
//        'button_callback'     => array('tl_iso_shipmenttracking', 'toggleIcon')
      ),
      'show' => array
      (
        'label'               => &$GLOBALS['TL_LANG']['tl_iso_shipmenttracking']['show'],
        'icon'                => 'show.gif',
      ),
		)
	),

	// Palettes
	'palettes' => array
	(
	  'default'                   => '{title_legend},name;{note_legend},note;{tracking_legend},trackingUrl'
	),

	// Subpalettes
	'subpalettes' => array
	(
	),

	// Fields
	'fields' => array
	(
		'id' => array
		(
			'sql'                     => "int(10) unsigned NOT NULL auto_increment"
		),
		'tstamp' => array
		(
			'sql'                     => "int(10) unsigned NOT NULL default '0'"
		),
    'name' => array
    (
      'exclude'                 => true,
      'label'                   => &$GLOBALS['TL_LANG']['tl_iso_shipmenttracking']['name'],
      'sorting'                 => true,
      'search'                  => true,
      'flag'                    => 1,
      'inputType'               => 'text',
      'eval'                    => array('mandatory'=>true, 'maxlength'=>255),
      'sql'                     => "varchar(255) NOT NULL default ''",
    ),
    'note' => array
    (
      'label'                   => &$GLOBALS['TL_LANG']['tl_iso_shipmenttracking']['note'],
      'exclude'                 => true,
      'inputType'               => 'textarea',
      'explanation'             => 'iso_shipmenttracking_placeholder',
      'eval'                    => array('mandatory'=>true,'rte'=>'tinyMCE', 'decodeEntities'=>true, 'helpwizard'=>true),
      'sql'                     => "text NULL",
    ),
    'trackingUrl' => array
    (
      'exclude'                 => true,
      'label'                   => &$GLOBALS['TL_LANG']['tl_iso_shipmenttracking']['trackingUrl'],
      'inputType'               => 'text',
      'eval'                    => array('mandatory'=>false, 'rgxp'=>'url', 'maxlength'=>255, 'decodeEntities'=>true),
      'sql'                     => "varchar(255) NOT NULL default ''",
    ),
	)
);


class tl_iso_shipmenttracking extends \Backend
{
  /**
   * Import the back end user object
   */
  public function __construct()
  {
    parent::__construct();
    $this->import('BackendUser', 'User');
  }

  /**
   * Check permissions to edit table tl_iso_shipping
   * @return void
   */
  public function checkPermission()
  {
    // Check permissions to add shipping modules
    if (!\BackendUser::getInstance()->hasAccess(\Input::get('mod'), 'iso_modules')) {
      $GLOBALS['TL_DCA']['tl_iso_shipmenttracking']['config']['closed'] = true;
      unset($GLOBALS['TL_DCA']['tl_iso_shipping']['list']['global_operations']['new']);
    }
  }

  /**
   * Set fields mandatory if needed
   * @param DataContainer $dc
   * @return void
   */
  public function mandatoryCheck(\DataContainer $dc)
  {
    if (\Input::get('act') == 'edit')
    {
      $data = \Database::getInstance()->execute("SELECT note FROM $dc->table WHERE id = $dc->id");
      if ($data->numRows && preg_match('/(%%TRACKING[^%]+%%)/', $data->note))
      {
        $GLOBALS['TL_DCA']['tl_iso_shipmenttracking']['fields']['trackingUrl']['eval']['mandatory'] = true;
      }
    }
  }

}