1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
/**
 * This is a standard Product page-type with fields like
 * Price, Weight, Model/Author and basic management of
 * groups.
 * 
 * It also has an associated Product_OrderItem class,
 * an extension of OrderItem, which is the mechanism
 * that links this page type class to the rest of the
 * eCommerce platform. This means you can add an instance
 * of this page type to the shopping cart.
 * 
 * @package ecommerce
 */
class Product extends Page {
	
	public static $db = array(
		'Price' => 'Currency',
		'Weight' => 'Decimal(9,2)',
		'Model' => 'Varchar',
		'FeaturedProduct' => 'Boolean',
		'AllowPurchase' => 'Boolean',
		'Taxable' => 'Boolean',
		'Shipping_Cost' => 'Decimal(9,2)',
		'Int_Shipping_Cost' => 'Decimal(9,2)',
		'InternalItemID' => 'Varchar(30)'
	);
	
	public static $has_one = array(
		'Image' => 'Product_Image'
	);
	
	public static $has_many = array(
		'Variations' => 'ProductVariation'
	);
	
	public static $many_many = array(
		'ProductGroups' => 'ProductGroup'
	);
	
	public static $belongs_many_many = array();
	
	public static $defaults = array(
		'AllowPurchase' => true,
		'Taxable' => true
	);
	
	public static $casting = array();
	
	static $default_parent = 'ProductGroup';
	
	static $add_action = 'a Product Page';
	
	static $icon = 'cms/images/treeicons/book';
	
	function getCMSFields() {
		$fields = parent::getCMSFields();

		// Standard product detail fields
		$fields->addFieldsToTab(
			'Root.Content.Main',
			array(
				new TextField('Weight', _t('Product.WEIGHT', 'Weight (kg)'), '', 12),
				new TextField('Price', _t('Product.PRICE', 'Price'), '', 12),
				new TextField('Shipping_Cost', _t('Product.SHIPPING_COST', 'Shipping Cost'), '', 12),
				new TextField('Int_Shipping_Cost', _t('Product.INT_SHIPPING_COST', 'International Shipping Cost'), '', 12),
				new TextField('Model', _t('Product.MODEL', 'Model/Author'), '', 50),
				new TextField('InternalItemID', _t('Product.CODE', 'Product Code'), '', 7)
			)
		);

		if(!$fields->dataFieldByName('Image')) {
			$fields->addFieldToTab('Root.Content.Images', new ImageField('Image', _t('Product.IMAGE', 'Product Image')));
		}

		// Flags for this product which affect it's behaviour on the site
		$fields->addFieldsToTab(
			'Root.Content.Main',
			array(
				new CheckboxField('FeaturedProduct', _t('Product.FEATURED', 'Featured Product')),
				new CheckboxField('AllowPurchase', _t('Product.ALLOWPURCHASE', 'Allow product to be purchased'), 1),
				new CheckboxField('Taxable', _t('Product.TAXABLE', 'Taxable?')),
			)
		);

		$fields->addFieldsToTab(
			'Root.Content.Variations', 
			array(
				new HeaderField(_t('Product.VARIATIONSSET', 'This product has the following variations set')),
				new LiteralField('VariationsNote', '<p class="message good">If this product has active variations, the price of the product will be the price of the variation added by the member to the shopping cart.</p>'),
				$this->getVariationsTable()
			)
		);

		$fields->addFieldsToTab(
			'Root.Content.Product Groups',
			array(
				new HeaderField(_t('Product.ALSOAPPEARS', 'This product also appears in the following groups')),
				$this->getProductGroupsTable()
			)
		);
		
		return $fields;
	}
	
	function getVariationsTable() {
		$singleton = singleton('ProductVariation');
		$query = $singleton->buildVersionSQL("`ProductID` = '{$this->ID}'");
		$variations = $singleton->buildDataObjectSet($query->execute());
		$filter = $variations ? "`ID` IN ('" . implode("','", $variations->column('RecordID')) . "')" : "`ID` < '0'";
		//$filter = "`ProductID` = '{$this->ID}'";
		
		$tableField = new HasManyComplexTableField(
			$this,
			'Variations',
			'ProductVariation',
			array(
				'Title' => 'Title',
				'Price' => 'Price'
			),
			'getCMSFields_forPopup',
			$filter
		);
		
		if(method_exists($tableField, 'setRelationAutoSetting')) {
			$tableField->setRelationAutoSetting(true);
		}
		
		return $tableField;
	}
	
	protected function getProductGroupsTable() {
		$tableField = new ManyManyComplexTableField(
			$this,
			'ProductGroups',
			'ProductGroup',
			array(
				'Title' => 'Product Group Page Title'
			)
		);
		
		$tableField->setPageSize(30);
		$tableField->setPermissions(array());
		
		return $tableField;
	}
	
	/**
	 * Returns the shopping cart.
	 * @todo Does HTTP::set_cache_age() still need to be set here?
	 * 
	 * @return Order
	 */
	function Cart() {
		HTTP::set_cache_age(0);
		return ShoppingCart::current_order();
	}
	
	/**
	 * Conditions for whether a product can be purchased.
	 * 
	 * If it has the checkbox for 'Allow this product to be purchased',
	 * as well as having a price, it can be purchased. Otherwise a user
	 * can't buy it.
	 * 
	 * @return boolean
	 */
	function AllowPurchase() {
		return $this->AllowPurchase && $this->Price;
	}
	
	/**
	 * Returns if the product is already in the shopping cart.
	 * Note : This function is usable in the Product context because a
	 * Product_OrderItem only has a Product object in attribute
	 * 
	 * @return boolean
	 */
	function IsInCart() {
		return $this->Item() ? true : false;
	}
	
	/**
	 * Returns the order item which contains the product
	 * Note : This function is usable in the Product context because a
	 * Product_OrderItem only has a Product object in attribute
	 */
	function Item() {
		$currentOrder = ShoppingCart::current_order();
		if($items = $currentOrder->Items()) {
			foreach($items as $item) {
				if($item instanceof Product_OrderItem && $itemProduct = $item->Product()) {
					if($itemProduct->ID == $this->ID && $itemProduct->Version == $this->Version) return $item;
				}
			}
		}
	}
	
	/**
	 * Return the currency being used on the site.
	 * @return string Currency code, e.g. "NZD" or "USD"
	 */
	function Currency() {
		if(class_exists('Payment')) {
			return Payment::site_currency();
		}
	}
	
	/**
	 * Return the global tax information of the site.
	 * @return TaxModifier
	 */
	function TaxInfo() {
		$currentOrder = ShoppingCart::current_order();
		return $currentOrder->TaxInfo();
	}
	
	function addLink() {
		return $this->Link() . 'add';
	}

	function addVariationLink($id) {
		return $this->Link() . 'addVariation/' . $id;
	}
	
	/**
	 * When the ecommerce module is first installed, and db/build
	 * is invoked, create some default records in the database.
	 */
	function requireDefaultRecords() {
		parent::requireDefaultRecords();
		
		if(!DataObject::get_one('Product')) {		
			if(!DataObject::get_one('ProductGroup')) singleton('ProductGroup')->requireDefaultRecords();
			if($group = DataObject::get_one('ProductGroup', '', true, '`ParentID` DESC')) {
				$content = '<p>This is a <em>product</em>. It\'s description goes into the Content field as a standard SilverStripe page would have it\'s content. This is an ideal place to describe your product.</p>';
				
				$page1 = new Product();
				$page1->Title = 'Example product';
				$page1->Content = $content . '<p>You may also notice that we have checked it as a featured product and it will be displayed on the main Products page.</p>';
				$page1->URLSegment = 'example-product';
				$page1->ParentID = $group->ID;
				$page1->Price = '15.00';
				$page1->Weight = '0.50';
				$page1->Model = 'Joe Bloggs';
				$page1->FeaturedProduct = true;
				$page1->writeToStage('Stage');
				$page1->publish('Stage', 'Live');
				Database::alteration_message('Product page \'Example product\' created', 'created');
				
				$page2 = new Product();
				$page2->Title = 'Example product 2';
				$page2->Content = $content;
				$page2->URLSegment = 'example-product-2';
				$page2->ParentID = $group->ID;
				$page2->Price = '25.00';
				$page2->Weight = '1.2';
				$page2->Model = 'Jane Bloggs';
				$page2->writeToStage('Stage');
				$page2->publish('Stage', 'Live');
				Database::alteration_message('Product page \'Example product 2\' created', 'created');		
			}
		}
	}
	
}

class Product_Controller extends Page_Controller {
	
	function init() {
		parent::init();
		
		Requirements::themedCSS('Product');
		Requirements::themedCSS('Cart');
	}
	
	function add() {
		if($this->AllowPurchase() && $this->Variations()->Count() == 0) {
			ShoppingCart::add_new_item(new Product_OrderItem($this->dataRecord));
			if(!$this->isAjax()) Director::redirectBack();
		}
	}
	
	function addVariation() {
		if($this->AllowPurchase && $this->urlParams['ID']) {
			$variation = DataObject::get_one(
				'ProductVariation', 
				sprintf(
					"`ID` = %d AND `ProductID` = %d",
					(int)$this->urlParams['ID'],
					(int)$this->ID
				)
			);
			if($variation) {
				if($variation->AllowPurchase()) {
					ShoppingCart::add_new_item(new ProductVariation_OrderItem($variation));
					if(!$this->isAjax()) Director::redirectBack();
				}
			}
		}
	}
	
}
class Product_Image extends Image {

	public static $db = array();
	
	public static $has_one = array();
	
	public static $has_many = array();
	
	public static $many_many = array();
	
	public static $belongs_many_many = array();
	
	function generateThumbnail($gd) {
		$gd->setQuality(80);
		return $gd->paddedResize(140,100);
	}
	
	function generateContentImage($gd) {
		$gd->setQuality(90);
		return $gd->resizeByWidth(200);
	}
	
	function generateLargeImage($gd) {
		$gd->setQuality(90);
		return $gd->resizeByWidth(600);
	}
	
}
class Product_OrderItem extends OrderItem {
	
	protected $_productID;
	
	protected $_productVersion;
	
	static $db = array(
		'ProductVersion' => 'Int'
	);
	
	static $has_one = array(
		'Product' => 'Product'
	);
	
	public function __construct($product = null, $quantity = 1) {
		// Case 1: Constructed by getting OrderItem from DB
		if(is_array($product)) {
			$this->ProductID = $this->_productID = $product['ProductID'];
			$this->ProductVersion = $this->_productVersion = $product['ProductVersion'];
		}
		
		// Case 2: Constructed in memory
		if(is_object($product)) {		
			$this->_productID = $product->ID;
 			$this->_productVersion = $product->Version;
 			
		}
		
 		parent::__construct($product, $quantity);
	}
	
	function getProductID() {
		return $this->_productID;
	}
	
	/**
	 * Overloaded Product accessor method.
	 *  
	 * Overloaded from the default has_one accessor to
	 * retrieve a product by it's version, this is extremely
	 * useful because we can set in stone the version of
	 * a product at the time when the user adds the item to
	 * their cart, so if the CMS admin changes the price, it
	 * remains the same for this order.
	 * 
	 * @param boolean $current If set to TRUE, returns the latest published version of the Product,
	 * 								If set to FALSE, returns the set version number of the Product
	 * 						 		(instead of the latest published version)
	 * @return Product object
	 */
	public function Product($current = false) {
		if($current) return DataObject::get_by_id('Product', $this->_productID);
		else return Versioned::get_version('Product', $this->_productID, $this->_productVersion);
	}
	
	function hasSameContent($orderItem) {
		$equals = parent::hasSameContent($orderItem);
		return $equals && $orderItem instanceof Product_OrderItem && $this->_productID == $orderItem->_productID && $this->_productVersion == $orderItem->_productVersion;
	}
	
	function UnitPrice() {
		return $this->Product()->Price;
	}
	
	function UnitTaxable() { 
		if ($this->Product()->Taxable) return $this->Product()->Price;
	}
	
	function UnitShipping_Cost() {
		
		$country = EcommerceRole::findCountry();
			if($country == "United Kingdom"){
				return ($this->Product()->Shipping_Cost) ? $this->Product()->Shipping_Cost : $this->Product()->Int_Shipping_Cost;
			}
			return $this->Product()->Int_Shipping_Cost;
   }   
	
	function TableTitle() {
		return $this->Product()->Title;
	}

	function Link() {
		if($product = $this->Product(true)) return $product->Link();
	}
	
	function addLink() {
		return ShoppingCart_Controller::add_item_link($this->_productID);
	}
	
	function removeLink() {
		return ShoppingCart_Controller::remove_item_link($this->_productID);
	}
	
	function removeallLink() {
		return ShoppingCart_Controller::remove_all_item_link($this->_productID);
	}
	
	function setquantityLink() {
		return ShoppingCart_Controller::set_quantity_item_link($this->_productID);
	}

	function onBeforeWrite() {
		parent::onBeforeWrite();

		$this->ProductID = $this->_productID;
		$this->ProductVersion = $this->_productVersion;
	}
	
	public function debug() {
		$title = $this->TableTitle();
		$productID = $this->_productID;
		$productVersion = $this->_productVersion;
		return parent::debug() .<<<HTML
			<h3>Product_OrderItem class details</h3>
			<p>
				<b>Title : </b>$title<br/>
				<b>Product ID : </b>$productID<br/>
				<b>Product Version : </b>$productVersion
			</p>
HTML;
	}
}
?>