forked from openpne/OpenPNE3
/
opDoctrineRecord.class.php
228 lines (186 loc) · 6.14 KB
/
opDoctrineRecord.class.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
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
<?php
/**
* This file is part of the OpenPNE package.
* (c) OpenPNE Project (http://www.openpne.jp/)
*
* For the full copyright and license information, please view the LICENSE
* file and the NOTICE file that were distributed with this source code.
*/
/**
* opDoctrineRecord
*
* @package OpenPNE
* @subpackage util
* @author Kousuke Ebihara <ebihara@tejimaya.com>
*/
abstract class opDoctrineRecord extends sfDoctrineRecord implements Zend_Acl_Resource_Interface
{
/**
* UNDEFINED_DATETIME
*
* It is used as NULL in a datetime column by getter and setter.
* We can't use real NULL value for this use, because NULL is ambiguous among RDBMSs.
*
* MySQL prepares "0000-00-00 00:00:00" for this use. But it can't be used in PostgreSQL.
* PostgreSQL's behavior is like ISO 8601. It accepts "0000-01-01" (it is the year 1 B.C.).
* In definition of Standard SQL, TIMESTAMP accepts "0001-01-01 00:00:00".
*
* OpenPNE will support many type of RDBMS. So we should select acceptable format of every RDBMSs.
* As you can see, it is "0001-01-01 00:00:00".
*
* I referred to hnw's entry: http://openlab.dino.co.jp/2007/11/10/170436147.html [ja]
*/
const UNDEFINED_DATETIME = '0001-01-01 00:00:00';
/**
* UNDEFINED_DATETIME_BC
*/
const UNDEFINED_DATETIME_BC = '0000-00-00 00:00:00';
const MAX_NESTING_LEVEL = 50;
protected
$roleList = array(),
$nestingLevelCheckClasses = array(
'Album',
),
$nested = array();
public function save(Doctrine_Connection $conn = null)
{
if (is_null($conn))
{
$conn = opDoctrineQuery::chooseConnection(true);
}
parent::save($conn);
}
public function hasColumn($name, $type = null, $length = null, $options = array())
{
// Temporary coping with the problem reported in http://trac.symfony-project.org/ticket/6873
if ('string' === $type && is_null($length))
{
$length = 2147483647;
}
return parent::hasColumn($name, $type, $length, $options);
}
protected function checkIsDatetimeField($fieldName)
{
$definition = $this->_table->getColumnDefinition($fieldName);
return 'datetime' === $definition['type'];
}
protected function checkIsDatetimeOrTimestamp($fieldName)
{
$definition = $this->_table->getColumnDefinition($fieldName);
return ('datetime' === $definition['type'] || 'timestamp' === $definition['type']);
}
protected function _set($fieldName, $value, $load = true)
{
// In setter, empty value must be handled as opDoctrineRecord::UNDEFINED_DATETIME
if ($this->checkIsDatetimeField($fieldName) && empty($value))
{
$value = self::UNDEFINED_DATETIME;
}
elseif ($this->checkIsDatetimeOrTimestamp($fieldName) && $time = strtotime($value))
{
$timezone = date_default_timezone_get();
$defaultTimezone = sfConfig::get('op_default_timezone', 'Asia/Tokyo');
if ($timezone !== $defaultTimezone)
{
date_default_timezone_set($defaultTimezone);
$value = date('Y-m-d H:i:s', $time);
date_default_timezone_set($timezone);
}
}
return parent::_set($fieldName, $value, $load);
}
public function get($fieldName, $load = true)
{
if (!in_array(get_class($this), $this->nestingLevelCheckClasses))
{
return parent::get($fieldName, $load);
}
if (empty($this->nested[$fieldName]))
{
$this->nested[$fieldName] = 0;
}
$this->nested[$fieldName]++;
if ($this->nested[$fieldName] > self::MAX_NESTING_LEVEL)
{
return $this->_get($fieldName, $load);
}
return parent::get($fieldName, $load);
}
public function _get($fieldName, $load = true)
{
$value = parent::_get($fieldName, $load);
// In getter, opDoctrineRecord::UNDEFINED_DATETIME must be handled as null
if ($this->checkIsDatetimeField($fieldName) && in_array($value, array(self::UNDEFINED_DATETIME, self::UNDEFINED_DATETIME_BC), true))
{
return null;
}
elseif ($this->checkIsDatetimeOrTimestamp($fieldName) && $value)
{
$timezone = date_default_timezone_get();
$defaultTimezone = sfConfig::get('op_default_timezone', 'Asia/Tokyo');
if ($timezone !== $defaultTimezone)
{
date_default_timezone_set($defaultTimezone);
$time = strtotime($value);
date_default_timezone_set($timezone);
$value = date('Y-m-d H:i:s', $time);
}
}
return $value;
}
public function getResourceId()
{
$tableName = $this->getTable()->getTableName();
$identifier = array_values($this->identifier());
$identifier = array_shift($identifier);
return $tableName.'.'.$identifier;
}
public function getRoleId(Member $member)
{
$this->checkReadyForAcl();
if (empty($this->roleList[$member->id]))
{
$this->roleList[$member->id] = $this->generateRoleId($member);
}
return $this->roleList[$member->id];
}
public function clearRoleList()
{
$this->checkReadyForAcl();
$this->roleList = array();
}
public function isAllowed(Member $member, $privilege)
{
$this->checkReadyForAcl();
$acl = $this->getTable()->getAcl($this);
return $acl->isAllowed($this->getRoleId($member), $this, $privilege);
}
public function checkReadyForAcl()
{
if (!($this instanceof opAccessControlRecordInterface))
{
throw new LogicException(sprintf('%s must implement the opAccessControlRecordInterface for access controll.', get_class($this)));
}
if (!($this->getTable() instanceof opAccessControlDoctrineTable))
{
throw new LogicException(sprintf('%s must be subclass of the opAccessControlDoctrineTable for access controll.', get_class($this->getTable())));
}
}
public function setUp()
{
parent::setUp();
if (!($this->getTable()->getConnection()->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_CONSTRAINTS))
{
$this->addListener(new opApplicationLevelCascadingListener());
}
$this->addListener(new opDoctrineEventNotifier());
}
public function setTableName($tableName)
{
if (sfConfig::get('op_table_prefix'))
{
$tableName = sfConfig::get('op_table_prefix').$tableName;
}
parent::setTableName($tableName);
}
}