|
1
|
|
|
<?php |
|
2
|
|
|
namespace dameter\abstracts\models; |
|
3
|
|
|
|
|
4
|
|
|
use yii\base\InvalidConfigException; |
|
5
|
|
|
use yii\base\Model; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Field describes a column in responses data |
|
9
|
|
|
* |
|
10
|
|
|
* @property string $type technical type for DB |
|
11
|
|
|
* @property string $name fieldName - actual column name in DB |
|
12
|
|
|
* @property string $tokenFieldCollation the suitable collation for token field |
|
13
|
|
|
* |
|
14
|
|
|
* @package dameter\abstracts\models |
|
15
|
|
|
* @author Tõnis Ormisson <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class Field extends Model |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
const TYPE_STRING = 'string'; |
|
21
|
|
|
const TYPE_CHAR = 'char'; |
|
22
|
|
|
const TYPE_INTEGER = 'integer'; |
|
23
|
|
|
const TYPE_DOUBLE = 'double'; |
|
24
|
|
|
const TYPE_DATE = 'date'; |
|
25
|
|
|
const TYPE_DATETIME = 'datetime'; |
|
26
|
|
|
|
|
27
|
|
|
const DEFAULT_STRING_LENGTH = 5; |
|
28
|
|
|
const DEFAULT_DOUBLE_LENGTH = 30; |
|
29
|
|
|
const DEFAULT_DOUBLE_DECIMALS = 10; |
|
30
|
|
|
|
|
31
|
|
|
const SYSFIELD_ID = 'id'; |
|
32
|
|
|
const SYSFIELD_TOKEN = 'token'; |
|
33
|
|
|
const SYSFIELD_SEED = 'seed'; |
|
34
|
|
|
const SYSFIELD_STARTLANGUAGE = 'startlanguage'; |
|
35
|
|
|
const SYSFIELD_STARTDATE = 'startdate'; |
|
36
|
|
|
const SYSFIELD_SUBMITDATE = 'submitdate'; |
|
37
|
|
|
const SYSFIELD_DATESTAMP = 'datestamp'; |
|
38
|
|
|
const SYSFIELD_LASTPAGE = 'lastpage'; |
|
39
|
|
|
const SYSFIELD_REFURL = 'refurl'; |
|
40
|
|
|
const SYSFIELD_IP_ADDRESS = 'ipaddr'; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** @var BaseQuestion */ |
|
44
|
|
|
public $question; |
|
45
|
|
|
|
|
46
|
|
|
/** @var string $systemFieldName Name for non-question field */ |
|
47
|
|
|
public $systemFieldName; |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** @var string */ |
|
51
|
|
|
public $id; |
|
52
|
|
|
|
|
53
|
|
|
/** @var bool $isCommentField */ |
|
54
|
|
|
public $isCommentField = false; |
|
55
|
|
|
|
|
56
|
|
|
/** @var bool $isOtherField */ |
|
57
|
|
|
public $isOtherField = false; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function init() |
|
63
|
|
|
{ |
|
64
|
|
|
parent::init(); |
|
65
|
|
|
if (!($this->question instanceof BaseQuestion)) { |
|
|
|
|
|
|
66
|
|
|
throw new InvalidConfigException(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getType() |
|
76
|
|
|
{ |
|
77
|
|
|
if (!empty($this->question)) { |
|
78
|
|
|
if ($this->isCommentField || $this->isOtherField) { |
|
79
|
|
|
return "text"; |
|
80
|
|
|
} |
|
81
|
|
|
return $this->question->questionType->fieldType; |
|
82
|
|
|
} |
|
83
|
|
|
return $this->systemFieldType(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return string |
|
89
|
|
|
* @throws \Exception |
|
90
|
|
|
*/ |
|
91
|
|
|
private function systemFieldType() |
|
92
|
|
|
{ |
|
93
|
|
|
switch ($this->systemFieldName) { |
|
94
|
|
|
case self::SYSFIELD_ID: |
|
95
|
|
|
return "pk"; |
|
96
|
|
|
break; |
|
|
|
|
|
|
97
|
|
|
case self::SYSFIELD_SEED: |
|
98
|
|
|
return "string(31)"; |
|
99
|
|
|
break; |
|
100
|
|
|
case self::SYSFIELD_STARTLANGUAGE: |
|
101
|
|
|
return "string(20) NOT NULL"; |
|
102
|
|
|
break; |
|
103
|
|
|
case self::SYSFIELD_STARTDATE: |
|
104
|
|
|
case self::SYSFIELD_DATESTAMP: |
|
105
|
|
|
return "datetime NOT NULL"; |
|
106
|
|
|
break; |
|
107
|
|
|
case self::SYSFIELD_SUBMITDATE: |
|
108
|
|
|
return "datetime"; |
|
109
|
|
|
break; |
|
110
|
|
|
case self::SYSFIELD_LASTPAGE: |
|
111
|
|
|
return "integer"; |
|
112
|
|
|
break; |
|
113
|
|
|
case self::SYSFIELD_TOKEN: |
|
114
|
|
|
return "string(36) " . $this->tokenFieldCollation; |
|
115
|
|
|
break; |
|
116
|
|
|
case self::SYSFIELD_REFURL: |
|
117
|
|
|
return "text"; |
|
118
|
|
|
break; |
|
119
|
|
|
case self::SYSFIELD_IP_ADDRESS: |
|
120
|
|
|
return "string(45)"; |
|
121
|
|
|
break; |
|
122
|
|
|
default: |
|
123
|
|
|
throw new \Exception("Undefined system column {$this->systemFieldName}"); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return string |
|
129
|
|
|
* @throws \Exception |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getName() |
|
132
|
|
|
{ |
|
133
|
|
|
if (!empty($this->question)) { |
|
134
|
|
|
return $this->question->code; |
|
135
|
|
|
} elseif (!empty($this->systemFieldName)) { |
|
136
|
|
|
return $this->systemFieldName; |
|
137
|
|
|
} |
|
138
|
|
|
throw new \Exception('Either question or systemFieldName must be defined for Field'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return string |
|
145
|
|
|
* @throws \Exception |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getTokenFieldCollation() |
|
148
|
|
|
{ |
|
149
|
|
|
$driverName = $this->getDriverName(); |
|
150
|
|
|
if ($driverName === 'mysql') { |
|
151
|
|
|
return " COLLATE 'utf8mb4_bin'"; |
|
152
|
|
|
} |
|
153
|
|
|
if ($driverName == 'sqlsrv' || $driverName == 'dblib' || $driverName == 'mssql') { |
|
154
|
|
|
return " COLLATE 'SQL_Latin1_General_CP1_CS_AS'"; |
|
155
|
|
|
} |
|
156
|
|
|
throw new \Exception('Unsupported database engine ' . $driverName); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getDriverName() { |
|
163
|
|
|
return \Yii::$app->db->driverName; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
} |