vendor/isotope/isotope-core/system/modules/isotope/library/Isotope/Model/Config.php line 329

Open in your IDE?
  1. <?php
  2. /*
  3.  * Isotope eCommerce for Contao Open Source CMS
  4.  *
  5.  * Copyright (C) 2009 - 2019 terminal42 gmbh & Isotope eCommerce Workgroup
  6.  *
  7.  * @link       https://isotopeecommerce.org
  8.  * @license    https://opensource.org/licenses/lgpl-3.0.html
  9.  */
  10. namespace Isotope\Model;
  11. use Contao\Database;
  12. use Contao\Date;
  13. use Contao\FrontendUser;
  14. use Contao\Model;
  15. use Contao\StringUtil;
  16. use Contao\System;
  17. use Isotope\Translation;
  18. /**
  19.  * Isotope\Model\Config represents an Isotope config model
  20.  *
  21.  * @property int    $id
  22.  * @property int    $tstamp
  23.  * @property string $name
  24.  * @property string $label
  25.  * @property bool   $fallback
  26.  * @property string $firstname
  27.  * @property string $lastname
  28.  * @property string $company
  29.  * @property string $vat_no
  30.  * @property string $street_1
  31.  * @property string $street_2
  32.  * @property string $street_3
  33.  * @property string $postal
  34.  * @property string $city
  35.  * @property string $subdivision
  36.  * @property string $country
  37.  * @property string $phone
  38.  * @property string $email
  39.  * @property array  $address_fields
  40.  * @property string $billing_country
  41.  * @property string $shipping_country
  42.  * @property array  $billing_countries
  43.  * @property array  $shipping_countries
  44.  * @property bool   $limitMemberCountries
  45.  * @property array  $vatNoValidators
  46.  * @property string $bankName
  47.  * @property string $bankAccount
  48.  * @property string $bankCode
  49.  * @property string $taxNumber
  50.  * @property string $priceDisplay
  51.  * @property string $currencyFormat
  52.  * @property int    $priceRoundPrecision
  53.  * @property string $priceRoundIncrement
  54.  * @property float  $cartMinSubtotal
  55.  * @property string $currency
  56.  * @property string $currencySymbol
  57.  * @property bool   $currencySpace
  58.  * @property string $currencyPosition
  59.  * @property string $priceCalculateFactor
  60.  * @property string $priceCalculateMode
  61.  * @property bool   $currencyAutomator
  62.  * @property string $currencyOrigin
  63.  * @property string $currencyProvider
  64.  * @property string $orderPrefix
  65.  * @property int    $orderDigits
  66.  * @property int    $orderstatus_new
  67.  * @property int    $orderstatus_error
  68.  * @property string $templateGroup
  69.  * @property array  $newProductPeriod
  70.  * @property bool   $ga_enable
  71.  * @property string $ga_account
  72.  * @property string $ga_member
  73.  */
  74. class Config extends Model
  75. {
  76.     const PRICE_DISPLAY_NET 'net';
  77.     const PRICE_DISPLAY_GROSS 'gross';
  78.     const PRICE_DISPLAY_FIXED 'fixed';
  79.     const PRICE_DISPLAY_LEGACY 'legacy';
  80.     /**
  81.      * Name of the current table
  82.      * @var string
  83.      */
  84.     protected static $strTable 'tl_iso_config';
  85.     /**
  86.      * Cache for additional methods
  87.      * @var array
  88.      */
  89.     protected $arrCache = array();
  90.     private static $priceDisplayGroups null;
  91.     /**
  92.      * Get translated label for the config
  93.      * @return  string
  94.      */
  95.     public function getLabel()
  96.     {
  97.         return Translation::get(($this->label ? : $this->name));
  98.     }
  99.     /**
  100.      * Returns an address model for the shop configuration address data.
  101.      *
  102.      * @return Address
  103.      */
  104.     public function getOwnerAddress()
  105.     {
  106.         $address = new Address();
  107.         $address->company     $this->company;
  108.         $address->firstname   $this->firstname;
  109.         $address->lastname    $this->lastname;
  110.         $address->street_1    $this->street_1;
  111.         $address->street_2    $this->street_2;
  112.         $address->street_3    $this->street_3;
  113.         $address->postal      $this->postal;
  114.         $address->city        $this->city;
  115.         $address->subdivision $this->subdivision;
  116.         $address->country     $this->country;
  117.         $address->email       $this->email;
  118.         $address->phone       $this->phone;
  119.         $address->vat_no      $this->vat_no;
  120.         $address->preventSaving(false);
  121.         return $address;
  122.     }
  123.     /**
  124.      * Get billing address fields
  125.      *
  126.      * @return  array
  127.      */
  128.     public function getBillingFields()
  129.     {
  130.         if (!isset($this->arrCache['billingFields'])) {
  131.             $this->arrCache['billingFields'] = array_filter(array_map(
  132.                 function($field) {
  133.                     return $field['enabled'] ? $field['value'] : null;
  134.                 },
  135.                 $this->getBillingFieldsConfig()
  136.             ));
  137.         }
  138.         return $this->arrCache['billingFields'];
  139.     }
  140.     /**
  141.      * Return raw billing field data
  142.      *
  143.      * @return array
  144.      */
  145.     public function getBillingFieldsConfig()
  146.     {
  147.         if (!isset($this->arrCache['billingFieldsConfig'])) {
  148.             $this->arrCache['billingFieldsConfig'] = array();
  149.             $arrFields                             StringUtil::deserialize($this->address_fields);
  150.             if (\is_array($arrFields)) {
  151.                 foreach ($arrFields as $arrField) {
  152.                     $this->arrCache['billingFieldsConfig'][] = [
  153.                         'value'     => $arrField['name'],
  154.                         'enabled'   => 'disabled' !== $arrField['billing'],
  155.                         'mandatory' => 'mandatory' === $arrField['billing'],
  156.                     ];
  157.                 }
  158.             }
  159.         }
  160.         return $this->arrCache['billingFieldsConfig'];
  161.     }
  162.     /**
  163.      * Get shipping address fields
  164.      *
  165.      * @return array
  166.      */
  167.     public function getShippingFields()
  168.     {
  169.         if (!isset($this->arrCache['shippingFields'])) {
  170.             $this->arrCache['shippingFields'] = array_filter(array_map(
  171.                 function($field) {
  172.                     return $field['enabled'] ? $field['value'] : null;
  173.                 },
  174.                 $this->getShippingFieldsConfig()
  175.             ));
  176.         }
  177.         return $this->arrCache['shippingFields'];
  178.     }
  179.     /**
  180.      * Return raw shipping field data
  181.      *
  182.      * @return array
  183.      */
  184.     public function getShippingFieldsConfig()
  185.     {
  186.         if (!isset($this->arrCache['shippingFieldsConfig'])) {
  187.             $this->arrCache['shippingFieldsConfig'] = array();
  188.             $arrFields                              StringUtil::deserialize($this->address_fields);
  189.             if (\is_array($arrFields)) {
  190.                 foreach ($arrFields as $arrField) {
  191.                     $this->arrCache['shippingFieldsConfig'][] = [
  192.                         'value'     => $arrField['name'],
  193.                         'enabled'   => 'disabled' !== $arrField['shipping'],
  194.                         'mandatory' => 'mandatory' === $arrField['shipping'],
  195.                     ];
  196.                 }
  197.             }
  198.         }
  199.         return $this->arrCache['shippingFieldsConfig'];
  200.     }
  201.     /**
  202.      * Get enabled billing countries
  203.      *
  204.      * @return array
  205.      */
  206.     public function getBillingCountries()
  207.     {
  208.         if (!isset($this->arrCache['billingCountries'])) {
  209.             $arrCountries StringUtil::deserialize($this->billing_countries);
  210.             if (empty($arrCountries) || !\is_array($arrCountries)) {
  211.                 $arrCountries array_keys(System::getCountries());
  212.             }
  213.             $this->arrCache['billingCountries'] = $arrCountries;
  214.         }
  215.         return $this->arrCache['billingCountries'];
  216.     }
  217.     /**
  218.      * Get enabled shipping countries
  219.      *
  220.      * @return array
  221.      */
  222.     public function getShippingCountries()
  223.     {
  224.         if (!isset($this->arrCache['shippingCountries'])) {
  225.             $arrCountries StringUtil::deserialize($this->shipping_countries);
  226.             if (empty($arrCountries) || !\is_array($arrCountries)) {
  227.                 $arrCountries array_keys(System::getCountries());
  228.             }
  229.             $this->arrCache['shippingCountries'] = $arrCountries;
  230.         }
  231.         return $this->arrCache['shippingCountries'];
  232.     }
  233.     /**
  234.      * Get the price display configuration
  235.      *
  236.      * @return string
  237.      */
  238.     public function getPriceDisplay()
  239.     {
  240.         $format $this->priceDisplay;
  241.         if (true === FE_USER_LOGGED_IN) {
  242.             if (null === self::$priceDisplayGroups) {
  243.                 self::$priceDisplayGroups Database::getInstance()
  244.                     ->execute("SELECT id, iso_priceDisplay FROM tl_member_group WHERE iso_priceDisplay!=''")
  245.                     ->fetchEach('iso_priceDisplay')
  246.                 ;
  247.             }
  248.             foreach (FrontendUser::getInstance()->groups as $groupId) {
  249.                 if (isset(self::$priceDisplayGroups[$groupId])) {
  250.                     $format self::$priceDisplayGroups[$groupId];
  251.                     break;
  252.                 }
  253.             }
  254.         }
  255.         // !HOOK: calculate price
  256.         if (isset($GLOBALS['ISO_HOOKS']['priceDisplay']) && \is_array($GLOBALS['ISO_HOOKS']['priceDisplay'])) {
  257.             foreach ($GLOBALS['ISO_HOOKS']['priceDisplay'] as $callback) {
  258.                 $format System::importStatic($callback[0])->{$callback[1]}($format$this);
  259.             }
  260.         }
  261.         return $format;
  262.     }
  263.     /**
  264.      * Get the limit to mark products as new
  265.      *
  266.      * @return int
  267.      */
  268.     public function getNewProductLimit()
  269.     {
  270.         if (!isset($this->arrCache['newProductLimit'])) {
  271.             $arrPeriod StringUtil::deserialize($this->newProductPeriod);
  272.             if (!empty($arrPeriod) && \is_array($arrPeriod) && $arrPeriod['value'] > && $arrPeriod['unit'] != '') {
  273.                 $this->arrCache['newProductLimit'] = strtotime(
  274.                     '-' $arrPeriod['value'] . ' ' $arrPeriod['unit'] . ' 00:00:00'
  275.                 );
  276.             } else {
  277.                 $this->arrCache['newProductLimit'] = Date::floorToMinute();
  278.             }
  279.         }
  280.         return $this->arrCache['newProductLimit'];
  281.     }
  282.     /**
  283.      * Find config set in root page or the fallback
  284.      *
  285.      * @param int   $intRoot
  286.      * @param array $arrOptions
  287.      *
  288.      * @return object|null
  289.      */
  290.     public static function findByRootPageOrFallback($intRoot, array $arrOptions = array())
  291.     {
  292.         $t = static::$strTable;
  293.         $arrOptions array_merge(
  294.             array(
  295.                  'column' => array("($t.id=(SELECT iso_config FROM tl_page WHERE id=?) OR $t.fallback='1')"),
  296.                  'value'  => $intRoot,
  297.                  'order'  => 'fallback',
  298.                  'return' => 'Model'
  299.             ),
  300.             $arrOptions
  301.         );
  302.         return static::find($arrOptions);
  303.     }
  304.     /**
  305.      * Find the fallback config
  306.      *
  307.      * @param array $arrOptions
  308.      *
  309.      * @return object|null
  310.      */
  311.     public static function findByFallback(array $arrOptions = array())
  312.     {
  313.         $arrOptions array_merge(
  314.             array(
  315.                  'column' => 'fallback',
  316.                  'value'  => '1',
  317.                  'return' => 'Model'
  318.             ),
  319.             $arrOptions
  320.         );
  321.         return static::find($arrOptions);
  322.     }
  323. }