Version: current

      1 <?php
      2 /**
      3  * ShippingMethod is a base class for providing shipping options to customers.
      4  *
      5  * @package cart_shipping
      6  * @author menedem
      7  */
      8 class ShippingMethod extends DataObject {
      9
     10     static $db = array(
     11         "Name" => "Varchar(255)",
     12         "Description" => "Text",
     13         "Enabled" => "Boolean",
     14         'Sort' => 'Int',
     15         'IconPath' => 'Varchar(255)',
     16         'IconType' => 'Int',
     17
     18         //TODO
     19         //"HandlingFee" => "CatalogPrice", //adds extra handling cost to use this method
     20     );
     21
     22     static $has_one = array(
     23         'Icon' => 'Image'
     24     );
     25
     26     static $defaults = array(
     27         'Sort' => 0,
     28     );
     29
     30     static $summary_fields = array('Name', 'Description', 'Enabled');
     31     static $default_sort = '"Sort"';
     32
     33     static $shippingMethodIcons = array(
     34         'cart_shipping/img/01.png',
     35         'cart_shipping/img/02.png',
     36         'cart_shipping/img/03.png',
     37         'cart_shipping/img/04.png',
     38         'cart_shipping/img/05.png',
     39         'cart_shipping/img/06.png',
     40         'cart_shipping/img/07.png',
     41         'cart_shipping/img/08.png',
     42         'cart_shipping/img/09.png',
     43     );
     44
     45     public $CalculatedRate;
     46
     47     /**
     48      * Список доступных классов для методов доставки
     49      */
     50     protected static $supported_methods = array(
     51         'FixedShippingMethod',
     52         'CostTableShippingMethod',
     53         'WeightTableShippingMethod',
     54     );
     55
     56     /**
     57      *
     58      *
     59      * @return array
     60      */
     61     public static function get_supported_methods(){
     62         return self::$supported_methods;
     63     }
     64
     65     /**
     66      *
     67      *
     68      * @param array $methodMap
     69      *
     70      * @return array
     71      */
     72     static function set_supported_methods($methodMap) {
     73         self::$supported_methods = $methodMap;
     74     }
     75
     76     /**
     77      * Проверяет доступен ли данный клас для создания метода доставки
     78      *
     79      * @param string $method
     80      *
     81      * @return boolean
     82      */
     83     static function method_is_supported($method) {
     84         return (array_search($method, self::$supported_methods) !== false) ? 1 : 0;
     85     }
     86
     87     /**
     88      * Получение всех доступных способов доставки
     89      *
     90      * @return DataObjectSet
     91      */
     92     static function get_shipping_methods(){
     93         $options = DataObject::get("ShippingMethod", "\"Enabled\" = 1 AND ClassName IN ('".implode("', '", self::$supported_methods)."')");
     94         //TODO: restrict options to region / package specs
     95         return $options;
     96     }
     97
     98     public function populateDefaults() {
     99         parent::populateDefaults();
    100         $this->Name = $this->i18n_singular_name();
    101     }
    102
    103     function getCMSFields(){
    104         $fields = parent::getCMSFields();
    105
    106         $iconField = $fields->dataFieldByName('Icon');
    107         $fields->removeByName('Icon');
    108         $fields->removeByName('IconPath');
    109
    110         $options = array();
    111         if (count(self::$shippingMethodIcons)) {
    112             $icons = array();
    113             foreach(self::$shippingMethodIcons as $icon) {
    114                 $icons[$icon] = "<img src=\"{$icon}\">";
    115             }
    116             $options['1//' . _t('ShippingMethod.SelectFromList', 'Select From List')] = new OptionsetField('IconPath', $this->fieldLabel('IconPath'), $icons);
    117         }
    118         $options['2//' . _t('ShippingMethod.UploadIcon', 'Upload Icon')] = $iconField;
    119
    120         $type = new SelectionGroup('IconType', $options);
    121         $fields->insertAfter($type, 'Description');
    122
    123         return $fields;
    124     }
    125
    126     function ShippingIcon() {
    127         $icon = false;
    128         switch ($this->IconType) {
    129             case 1:
    130                 if ($this->IconPath && is_file(BASE_PATH . "/{$this->IconPath}")) {
    131                     $icon = new Image_Cached($this->IconPath);
    132                 }
    133                 break;
    134             case 2:
    135                 if ($this->IconID && ($img = $this->Icon()) && is_file($img->FullPath)) {
    136                     $icon = $img;
    137                 }
    138                 break;
    139         }
    140         return $icon;
    141     }
    142
    143     /*
    144      * Возвращает название способа доставки для формы их выбора.
    145      * В шаблоне ListTitle.ss можно полностью настроить внешний вид
    146      *
    147      * @return html
    148      */
    149     function ListTitle() {
    150         return $this->renderWith('ShippingListTitle');
    151     }
    152
    153     /**
    154      * Рассчитывает стоимость доставки
    155      *
    156      * @param ShippingPackage $package
    157      * @param Address $address
    158      *
    159      * @return null|float
    160      */
    161     function calculateRate(ShippingPackage $package, Address $address){
    162         user_error('Need to be overwritten in sublcasses!');
    163     }
    164
    165     /**
    166      * Стоимость доставки
    167      *
    168      * @return CatalogPrice
    169      */
    170     function getRate(){
    171         $rate = new CatalogPrice('Rate');
    172         $rate->setValue($this->CalculatedRate);
    173         return $rate;
    174     }
    175
    176     function Title(){
    177         $title = implode(" - ",array_filter(array(
    178             $this->Rate->Nice(),
    179             $this->Name,
    180             $this->Description
    181         )));
    182         $this->extend('updateShippingMethodTitle', $title);
    183         return $title;
    184     }
    185
    186     function onBeforeWrite() {
    187         parent::onBeforeWrite();
    188         if(!$this->Sort) {
    189             $this->Sort = DB::query('SELECT MAX("Sort") + 1 FROM "ShippingMethod"')->value();
    190         }
    191     }
    192 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -