1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* form field to select answer for question. |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class ProductQuestionImageSelectorField extends OptionsetField |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
protected $folderID = 0; |
13
|
|
|
|
14
|
|
|
protected $options = array(); |
15
|
|
|
|
16
|
|
|
protected $objects = null; |
17
|
|
|
|
18
|
|
|
protected $width = 50; |
19
|
|
|
|
20
|
|
|
protected $height = 50; |
21
|
|
|
|
22
|
|
|
public function __construct($name, $title = '', $options = array(), $value = '', $folderID) |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$this->setOptions($options); |
25
|
|
|
$this->setFolderID($folderID); |
26
|
|
|
$this->createObjects(); |
27
|
|
|
parent::__construct($name, $title, $options, $value); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function setOptions($options) |
31
|
|
|
{ |
32
|
|
|
$this->options = $options; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function setFolderID($folderID) |
36
|
|
|
{ |
37
|
|
|
$this->folderID = $folderID; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setWidth($width) |
41
|
|
|
{ |
42
|
|
|
$this->width = $width; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function setHeight($height) |
46
|
|
|
{ |
47
|
|
|
$this->height = $height; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function Field($properties = array()) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$options = ''; |
53
|
|
|
$source = $this->getSource(); |
|
|
|
|
54
|
|
|
$count = 0; |
55
|
|
|
if ($this->objects && $this->objects->count()) { |
56
|
|
|
foreach ($this->objects as $image) { |
57
|
|
|
$key = $image->Key; |
58
|
|
|
$itemID = $this->id() . "_" . $image->ID; |
59
|
|
|
$value = $image->Value; |
60
|
|
|
$labelHTML = $value; |
61
|
|
|
$resizedImageObject = $image->getFormattedImage("CroppedImage", $this->width, $this->height); |
62
|
|
|
if ($resizedImageObject) { |
63
|
|
|
$labelHTML = '<img src="'.$resizedImageObject->Link().'" alt="'.$value.'" />'; |
64
|
|
|
} |
65
|
|
|
if ($key == $this->value/* || $useValue */) { |
66
|
|
|
$useValue = false; |
|
|
|
|
67
|
|
|
$checked = " checked=\"checked\""; |
68
|
|
|
} else { |
69
|
|
|
$checked=""; |
70
|
|
|
} |
71
|
|
|
$odd = ($count + 1) % 2; |
72
|
|
|
$oddEven = $odd ? "odd" : "even"; |
73
|
|
|
$extraClass = " val" . preg_replace('/[^a-zA-Z0-9\-\_]/', '_', $key); |
74
|
|
|
$position = " pos".$count; |
75
|
|
|
$disabled = $this->disabled ? 'disabled="disabled"' : ''; |
76
|
|
|
$options .= "<li class=\"".$oddEven.$extraClass.$position."\"><input id=\"$itemID\" name=\"$this->name\" type=\"radio\" value=\"$key\"$checked $disabled class=\"radio\" /> <label for=\"$itemID\">$labelHTML</label></li>\n"; |
77
|
|
|
$count++; |
78
|
|
|
} |
79
|
|
|
$id = $this->id(); |
80
|
|
|
} |
81
|
|
|
if (empty($id)) { |
82
|
|
|
$id = 0; |
83
|
|
|
} |
84
|
|
|
return " |
85
|
|
|
<ul id=\"$id\" class=\"optionset {$this->extraClass()}\"> |
86
|
|
|
\n$options |
87
|
|
|
</ul>\n"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function createObjects() |
91
|
|
|
{ |
92
|
|
|
$this->objects = new ArrayList(); |
93
|
|
|
if ($this->options && is_array($this->options) && count($this->options)) { |
|
|
|
|
94
|
|
|
foreach ($this->options as $option) { |
95
|
|
|
$imageOptions = ProductQuestion::create_file_array_from_option($option); |
96
|
|
|
$image = DataObject::get_one( |
97
|
|
|
'Image', |
98
|
|
|
array("ParentID" => $this->folderID, "Name" => $imageOptions), |
99
|
|
|
$cacheDataObjectGetOne = false |
100
|
|
|
); |
101
|
|
|
if ($image) { |
102
|
|
|
$image->Key = $option; |
103
|
|
|
$image->Value = $option; |
104
|
|
|
$this->objects->push($image); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.