<?php

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

$GLOBALS['TL_DCA']['tl_iso_product_collection']['config']['onload_callback'][] = array('tl_iso_product_collection_eSM_isotope_shipmenttracking','registerSubPalettes');

$GLOBALS['TL_DCA']['tl_iso_product_collection']['palettes']['default'] = str_replace(array('{status_legend}','{status_legend:hide}'),array('{status_legend},shipping_tracking','{status_legend:hide},shipping_tracking'),$GLOBALS['TL_DCA']['tl_iso_product_collection']['palettes']['default']);
$GLOBALS['TL_DCA']['tl_iso_product_collection']['palettes']['default'] = str_replace('date_paid','shipping_annotation,date_paid',$GLOBALS['TL_DCA']['tl_iso_product_collection']['palettes']['default']);
$GLOBALS['TL_DCA']['tl_iso_product_collection']['palettes']['__selector__'][] = 'shipping_tracking';

$GLOBALS['TL_DCA']['tl_iso_product_collection']['subpalettes']['shipping_tracking'] = 'tracking_number';


$GLOBALS['TL_DCA']['tl_iso_product_collection']['fields']['shipping_tracking'] = array
(
  'label'                     => &$GLOBALS['TL_LANG']['tl_iso_product_collection']['shipping_tracking'],
  'exclude'                   => true,
  'inputType'                 => 'select',
  'foreignKey'                => 'tl_iso_shipmenttracking.name',
  'save_callback'             => array
  (
    array('tl_iso_product_collection_eSM_isotope_shipmenttracking','resetTracking')
  ),
  'eval'                      => array('includeBlankOption'=>true,'submitOnChange'=>true,'tl_class'=>'clr w50'),
  'sql'                       => "int(10) unsigned NOT NULL default '0'",
  'relation'                  => array('type'=>'hasMany', 'load'=>'lazy'),
);

$GLOBALS['TL_DCA']['tl_iso_product_collection']['fields']['tracking_number'] = array
(
  'label'                     => &$GLOBALS['TL_LANG']['tl_iso_product_collection']['tracking_number'],
  'exclude'                   => true,
  'inputType'                 => 'text',
  'eval'                      => array('mandatory'=>true, 'maxlength'=>255, 'tl_class'=>'w50', 'decodeEntities'=>true),
  'sql'                       => "varchar(255) NOT NULL default ''",
);
$GLOBALS['TL_DCA']['tl_iso_product_collection']['fields']['shipping_annotation'] = array
(
  'label'                     => &$GLOBALS['TL_LANG']['tl_iso_product_collection']['shipping_annotation'],
  'exclude'                   => true,
  'inputType'                 => 'textarea',
  'eval'                      => array('style'=>'height:80px;', 'tl_class'=>'clr'),
  'sql'                       => "mediumtext NULL",
);

$GLOBALS['TL_DCA']['tl_iso_product_collection']['fields']['order_status']['eval']['tl_class'] = 'clr';


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

  /**
   * Dynamically add subpalettes on runtime
   *
   * @param DataContainer $dc
   */
  public function registerSubPalettes(\DataContainer $dc)
  {
    $Tracking = \eSM_isotope_shipmenttracking\Model\ShipmentTrackingModel::findAll();

    if ($Tracking === null)
    {
      return;
    }

    while ($Tracking->next())
    {
      if ($Tracking->trackingUrl)
      {
        $GLOBALS['TL_DCA']['tl_iso_product_collection']['subpalettes']['shipping_tracking_' . $Tracking->id] = 'tracking_number';
      }
    }

  }

  public function resetTracking($varValue, \DataContainer $dc)
  {
    if (!$varValue) {
      $t = \Isotope\Model\ProductCollection::getTable();
      \Database::getInstance()->prepare("UPDATE $t SET tracking_number = '' WHERE id = ?")->execute($dc->id);
    }
    return $varValue;
  }
}