-
Notifications
You must be signed in to change notification settings - Fork 453
/
option.php
121 lines (106 loc) · 4.21 KB
/
option.php
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
<?php
namespace Concrete\Attribute\Select;
use Gettext\Translations;
use Loader;
use \Concrete\Core\Foundation\Object;
class Option extends Object {
public function __construct($ID, $value, $displayOrder, $usageCount = false, $akID = null) {
$this->ID = $ID;
$this->value = $value;
$this->th = Loader::helper('text');
$this->displayOrder = $displayOrder;
$this->usageCount = $usageCount;
$this->akID = $akID;
}
public function getAttributeKeyID()
{
return $this->akID;
}
public function getSelectAttributeOptionID() {return $this->ID;}
public function getSelectAttributeOptionUsageCount() {return $this->usageCount;}
public function getSelectAttributeOptionValue($sanitize = true) {
if (!$sanitize) {
return $this->value;
} else {
return $this->th->specialchars($this->value);
}
}
/** Returns the display name for this select option value (localized and escaped accordingly to $format)
* @param string $format = 'html'
* Escape the result in html format (if $format is 'html').
* If $format is 'text' or any other value, the display name won't be escaped.
* @return string
*/
public function getSelectAttributeOptionDisplayValue($format = 'html') {
$value = tc('SelectAttributeValue', $this->getSelectAttributeOptionValue(false));
switch($format) {
case 'html':
return h($value);
case 'text':
default:
return $value;
}
}
public function getSelectAttributeOptionDisplayOrder() {return $this->displayOrder;}
public function getSelectAttributeOptionTemporaryID() {return $this->tempID;}
public function __toString() {return $this->value;}
public static function add($ak, $option, $isEndUserAdded = 0) {
$db = Loader::db();
$th = Loader::helper('text');
// this works because displayorder starts at zero. So if there are three items, for example, the display order of the NEXT item will be 3.
$displayOrder = $db->GetOne('select count(ID) from atSelectOptions where akID = ?', array($ak->getAttributeKeyID()));
$v = array($ak->getAttributeKeyID(), $displayOrder, $th->sanitize($option), $isEndUserAdded);
$db->Execute('insert into atSelectOptions (akID, displayOrder, value, isEndUserAdded) values (?, ?, ?, ?)', $v);
return Option::getByID($db->Insert_ID());
}
public function setDisplayOrder($num) {
$db = Loader::db();
$db->Execute('update atSelectOptions set displayOrder = ? where ID = ?', array($num, $this->ID));
}
public static function getByID($id) {
$db = Loader::db();
$row = $db->GetRow("select ID, displayOrder, value, akID from atSelectOptions where ID = ?", array($id));
if (isset($row['ID'])) {
$obj = new Option($row['ID'], $row['value'], $row['displayOrder'], null, $row['akID']);
return $obj;
}
}
public static function getByValue($value, $ak = false) {
$db = Loader::db();
if (is_object($ak)) {
$row = $db->GetRow("select ID, displayOrder, akID, value from atSelectOptions where value = ? and akID = ?", array($value, $ak->getAttributeKeyID()));
} else {
$row = $db->GetRow("select ID, displayOrder, akID, value from atSelectOptions where value = ?", array($value));
}
if (isset($row['ID'])) {
$obj = new Option($row['ID'], $row['value'], $row['displayOrder'], null, $row['akID']);
return $obj;
}
}
public function delete() {
$db = Loader::db();
$db->Execute('delete from atSelectOptions where ID = ?', array($this->ID));
$db->Execute('delete from atSelectOptionsSelected where atSelectOptionID = ?', array($this->ID));
}
public function saveOrCreate($ak) {
if ($this->tempID != false || $this->ID==0) {
return Option::add($ak, $this->value);
} else {
$db = Loader::db();
$th = Loader::helper('text');
$db->Execute('update atSelectOptions set value = ? where ID = ?', array($th->sanitize($this->value), $this->ID));
return Option::getByID($this->ID);
}
}
public static function exportTranslations()
{
$translations = new Translations();
$db = \Database::get();
$r = $db->Execute('select ID from atSelectOptions order by ID asc');
while ($row = $r->FetchRow()) {
$opt = static::getByID($row['ID']);
$translations->insert('SelectAttributeValue', $opt->getSelectAttributeOptionValue(false));
}
return $translations;
}
}