1 <?php
2 3 4 5 6
7 class BookingService extends DataObject {
8 static $db = array(
9 'Title' => 'Varchar(200)',
10 'Price' => 'Decimal(10, 2)',
11 'Active' => 'Boolean',
12 'Description' => 'HTMLText',
13 'MaxCountType' => "Enum('person, day, count')",
14 'MaxCount' => 'Int',
15 'Repeat' => "Enum('one_time, every_day')",
16 );
17
18 static $has_one = array(
19 'Icon' => 'Image'
20 );
21
22 static $searchable_fields = array('Title', 'Active', 'Price');
23 static $summary_fields = array('Title', 'ActiveTitle', 'Price');
24
25 function fieldLabels($relations = true) {
26 $labels = parent::fieldLabels($relations);
27 $labels['ActiveTitle'] = $labels['Active'];
28 return $labels;
29 }
30
31 function ActiveTitle() {
32 return ($this->Active) ? _t('Boolean.YES') : _t('Boolean.NO');
33 }
34
35 function getCMSFields() {
36 $fields = parent::getCMSFields();
37 $fields->addFieldToTab('Root', new Tab("FullContent", $this->fieldLabel('Description')));
38 $fields->addFieldToTab('Root.FullContent', $fields->dataFieldByName('Description'));
39
40 $values = array();
41 foreach ($this->dbObject('MaxCountType')->enumValues() as $val) {
42 $title = _t('BookingService.MaxCountType_' . $val, $val);
43 $values["$val//$title"] = ($val == 'count') ? $fields->dataFieldByName('MaxCount') : new LabelField('','');
44 }
45 $fields->removeByName('MaxCount');
46 $fields->replaceField('MaxCountType', new SelectionGroup('MaxCountType', $values));
47 $fields->addFieldToTab('Root.Main', new LabelField('labelMaxCountType', $this->fieldLabel('MaxCountType')), 'MaxCountType');
48
49 $values = array();
50 foreach($this->dbObject('Repeat')->enumValues() as $val) {
51 $values[$val] = _t('BookingService.Repeat_' . $val);
52 }
53 $fields->replaceField('Repeat', new OptionsetField('Repeat', $this->fieldLabel('Repeat'), $values));
54
55 return $fields;
56 }
57
58 function getServiceSelector($prefix, $count) {
59 $className = $this->ClassName;
60 $nums = array();
61 $nums[0] = _t('BookingService.SelectText', 'select');
62 for($i=1; $i <= $count; $i++) {
63 $nums[$i] = $i;
64 }
65 $f = new DropdownField("{$prefix}[$this->ID]", '', $nums);
66 $f->addExtraClass("{$className}Selector");
67 $f->addExtraClass($this->MaxCountType);
68 $f->addExtraAttribute('data-service_id', $this->ID);
69
70 return $f;
71 }
72
73
74 function Cost() {
75 if ($this->Repeat == 'one_time') {
76 return (float)$this->Price;
77 }
78 return (float)BookingPage::getFilterDatesPeriodLength() * $this->Price;
79 }
80
81 function onBeforeWrite() {
82 if ($this->isChanged('MaxCountType') && ($this->MaxCountType == 'day')) {
83 $this->Repeat = 'one_time';
84 }
85 parent::onBeforeWrite();
86 }
87 }
[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.
-