1 <?php
2 3 4 5 6 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
19
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 60
61 public static function get_supported_methods(){
62 return self::$supported_methods;
63 }
64
65 66 67 68 69 70 71
72 static function set_supported_methods($methodMap) {
73 self::$supported_methods = $methodMap;
74 }
75
76 77 78 79 80 81 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 91
92 static function get_shipping_methods(){
93 $options = DataObject::get("ShippingMethod", "\"Enabled\" = 1 AND ClassName IN ('".implode("', '", self::$supported_methods)."')");
94
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 146 147 148
149 function ListTitle() {
150 return $this->renderWith('ShippingListTitle');
151 }
152
153 154 155 156 157 158 159 160
161 function calculateRate(ShippingPackage $package, Address $address){
162 user_error('Need to be overwritten in sublcasses!');
163 }
164
165 166 167 168 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.
-