1 | <?php |
---|
2 | |
---|
3 | require_once 'propel/engine/builder/om/php5/PHP5ObjectBuilder.php'; |
---|
4 | |
---|
5 | /* |
---|
6 | * This file is part of the symfony package. |
---|
7 | * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> |
---|
8 | * |
---|
9 | * For the full copyright and license information, please view the LICENSE |
---|
10 | * file that was distributed with this source code. |
---|
11 | */ |
---|
12 | |
---|
13 | /** |
---|
14 | * @package symfony |
---|
15 | * @subpackage addon |
---|
16 | * @author Fabien Potencier <fabien.potencier@symfony-project.com> |
---|
17 | * @version SVN: $Id: SfObjectBuilder.php 3493 2007-02-18 09:23:10Z fabien $ |
---|
18 | */ |
---|
19 | class SfObjectBuilder extends PHP5ObjectBuilder |
---|
20 | { |
---|
21 | public function build() |
---|
22 | { |
---|
23 | if (!DataModelBuilder::getBuildProperty('builderAddComments')) |
---|
24 | { |
---|
25 | return sfToolkit::stripComments(parent::build()); |
---|
26 | } |
---|
27 | |
---|
28 | return parent::build(); |
---|
29 | } |
---|
30 | |
---|
31 | protected function addIncludes(&$script) |
---|
32 | { |
---|
33 | if (!DataModelBuilder::getBuildProperty('builderAddIncludes')) |
---|
34 | { |
---|
35 | return; |
---|
36 | } |
---|
37 | |
---|
38 | parent::addIncludes($script); |
---|
39 | |
---|
40 | // include the i18n classes if needed |
---|
41 | if ($this->getTable()->getAttribute('isI18N')) |
---|
42 | { |
---|
43 | $relatedTable = $this->getDatabase()->getTable($this->getTable()->getAttribute('i18nTable')); |
---|
44 | |
---|
45 | $script .= ' |
---|
46 | require_once \''.$this->getFilePath($this->getStubObjectBuilder()->getPackage().'.'.$relatedTable->getPhpName().'Peer').'\'; |
---|
47 | require_once \''.$this->getFilePath($this->getStubObjectBuilder()->getPackage().'.'.$relatedTable->getPhpName()).'\'; |
---|
48 | '; |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | protected function addClassBody(&$script) |
---|
53 | { |
---|
54 | parent::addClassBody($script); |
---|
55 | |
---|
56 | if ($this->getTable()->getAttribute('isI18N')) |
---|
57 | { |
---|
58 | if (count($this->getTable()->getPrimaryKey()) > 1) |
---|
59 | { |
---|
60 | throw new Exception('i18n support only works with a single primary key'); |
---|
61 | } |
---|
62 | |
---|
63 | $this->addCultureAccessorMethod($script); |
---|
64 | $this->addCultureMutatorMethod($script); |
---|
65 | |
---|
66 | $this->addI18nMethods($script); |
---|
67 | } |
---|
68 | |
---|
69 | if (DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
---|
70 | { |
---|
71 | $this->addCall($script); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | protected function addCall(&$script) |
---|
76 | { |
---|
77 | $script .= " |
---|
78 | |
---|
79 | public function __call(\$method, \$arguments) |
---|
80 | { |
---|
81 | if (!\$callable = sfMixer::getCallable('{$this->getClassname()}:'.\$method)) |
---|
82 | { |
---|
83 | throw new sfException(sprintf('Call to undefined method {$this->getClassname()}::%s', \$method)); |
---|
84 | } |
---|
85 | |
---|
86 | array_unshift(\$arguments, \$this); |
---|
87 | |
---|
88 | return call_user_func_array(\$callable, \$arguments); |
---|
89 | } |
---|
90 | |
---|
91 | "; |
---|
92 | } |
---|
93 | |
---|
94 | protected function addAttributes(&$script) |
---|
95 | { |
---|
96 | parent::addAttributes($script); |
---|
97 | |
---|
98 | if ($this->getTable()->getAttribute('isI18N')) |
---|
99 | { |
---|
100 | $script .= ' |
---|
101 | /** |
---|
102 | * The value for the culture field. |
---|
103 | * @var string |
---|
104 | */ |
---|
105 | protected $culture; |
---|
106 | '; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | protected function addCultureAccessorMethod(&$script) |
---|
111 | { |
---|
112 | $script .= ' |
---|
113 | public function getCulture() |
---|
114 | { |
---|
115 | return $this->culture; |
---|
116 | } |
---|
117 | '; |
---|
118 | } |
---|
119 | |
---|
120 | protected function addCultureMutatorMethod(&$script) |
---|
121 | { |
---|
122 | $script .= ' |
---|
123 | public function setCulture($culture) |
---|
124 | { |
---|
125 | $this->culture = $culture; |
---|
126 | } |
---|
127 | '; |
---|
128 | } |
---|
129 | |
---|
130 | protected function addI18nMethods(&$script) |
---|
131 | { |
---|
132 | $table = $this->getTable(); |
---|
133 | $pks = $table->getPrimaryKey(); |
---|
134 | $pk = $pks[0]->getPhpName(); |
---|
135 | |
---|
136 | foreach ($table->getReferrers() as $fk) |
---|
137 | { |
---|
138 | $tblFK = $fk->getTable(); |
---|
139 | if ($tblFK->getName() == $table->getAttribute('i18nTable')) |
---|
140 | { |
---|
141 | $className = $tblFK->getPhpName(); |
---|
142 | $culture = ''; |
---|
143 | $culture_peername = ''; |
---|
144 | foreach ($tblFK->getColumns() as $col) |
---|
145 | { |
---|
146 | if (("true" === strtolower($col->getAttribute('isCulture')))) |
---|
147 | { |
---|
148 | $culture = $col->getPhpName(); |
---|
149 | $culture_peername = PeerBuilder::getColumnName($col, $className); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | foreach ($tblFK->getColumns() as $col) |
---|
154 | { |
---|
155 | if ($col->isPrimaryKey()) continue; |
---|
156 | |
---|
157 | $script .= ' |
---|
158 | public function get'.$col->getPhpName().'() |
---|
159 | { |
---|
160 | $obj = $this->getCurrent'.$className.'(); |
---|
161 | |
---|
162 | return ($obj ? $obj->get'.$col->getPhpName().'() : null); |
---|
163 | } |
---|
164 | |
---|
165 | public function set'.$col->getPhpName().'($value) |
---|
166 | { |
---|
167 | $this->getCurrent'.$className.'()->set'.$col->getPhpName().'($value); |
---|
168 | } |
---|
169 | '; |
---|
170 | } |
---|
171 | |
---|
172 | $script .= ' |
---|
173 | protected $current_i18n = array(); |
---|
174 | |
---|
175 | public function getCurrent'.$className.'() |
---|
176 | { |
---|
177 | if (!isset($this->current_i18n[$this->culture])) |
---|
178 | { |
---|
179 | $obj = '.$className.'Peer::retrieveByPK($this->get'.$pk.'(), $this->culture); |
---|
180 | if ($obj) |
---|
181 | { |
---|
182 | $this->set'.$className.'ForCulture($obj, $this->culture); |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | $this->set'.$className.'ForCulture(new '.$className.'(), $this->culture); |
---|
187 | $this->current_i18n[$this->culture]->set'.$culture.'($this->culture); |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | return $this->current_i18n[$this->culture]; |
---|
192 | } |
---|
193 | |
---|
194 | public function set'.$className.'ForCulture($object, $culture) |
---|
195 | { |
---|
196 | $this->current_i18n[$culture] = $object; |
---|
197 | $this->add'.$className.'($object); |
---|
198 | } |
---|
199 | '; |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | protected function addDoSave(&$script) |
---|
205 | { |
---|
206 | $tmp = ''; |
---|
207 | parent::addDoSave($tmp); |
---|
208 | // add autosave to i18n object even if the base object is not changed |
---|
209 | $tmp = preg_replace_callback('#(\$this\->(.+?)\->isModified\(\))#', array($this, 'i18nDoSaveCallback'), $tmp); |
---|
210 | |
---|
211 | $script .= $tmp; |
---|
212 | } |
---|
213 | |
---|
214 | private function i18nDoSaveCallback($matches) |
---|
215 | { |
---|
216 | $value = $matches[1]; |
---|
217 | |
---|
218 | // get the related class to see if it is a i18n one |
---|
219 | $table = $this->getTable(); |
---|
220 | $column = null; |
---|
221 | foreach ($table->getForeignKeys() as $fk) |
---|
222 | { |
---|
223 | if ($matches[2] == $this->getFKVarName($fk)) |
---|
224 | { |
---|
225 | $column = $fk; |
---|
226 | break; |
---|
227 | } |
---|
228 | } |
---|
229 | $foreign_table = $this->getDatabase()->getTable($fk->getForeignTableName()); |
---|
230 | if ($foreign_table->getAttribute('isI18N')) |
---|
231 | { |
---|
232 | $foreign_tables_i18n_table = $this->getDatabase()->getTable($foreign_table->getAttribute('i18nTable')); |
---|
233 | $value .= ' || $this->'.$matches[2].'->getCurrent'.$foreign_tables_i18n_table->getPhpName().'()->isModified()'; |
---|
234 | } |
---|
235 | |
---|
236 | return $value; |
---|
237 | } |
---|
238 | |
---|
239 | protected function addDelete(&$script) |
---|
240 | { |
---|
241 | $tmp = ''; |
---|
242 | parent::addDelete($tmp); |
---|
243 | |
---|
244 | if (DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
---|
245 | { |
---|
246 | // add sfMixer call |
---|
247 | $pre_mixer_script = " |
---|
248 | |
---|
249 | foreach (sfMixer::getCallables('{$this->getClassname()}:delete:pre') as \$callable) |
---|
250 | { |
---|
251 | \$ret = call_user_func(\$callable, \$this, \$con); |
---|
252 | if (\$ret) |
---|
253 | { |
---|
254 | return; |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | "; |
---|
259 | $post_mixer_script = " |
---|
260 | |
---|
261 | foreach (sfMixer::getCallables('{$this->getClassname()}:delete:post') as \$callable) |
---|
262 | { |
---|
263 | call_user_func(\$callable, \$this, \$con); |
---|
264 | } |
---|
265 | |
---|
266 | "; |
---|
267 | $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1); |
---|
268 | $tmp = preg_replace('/}\s*$/', $post_mixer_script.' }', $tmp); |
---|
269 | } |
---|
270 | |
---|
271 | // update current script |
---|
272 | $script .= $tmp; |
---|
273 | } |
---|
274 | |
---|
275 | protected function addSave(&$script) |
---|
276 | { |
---|
277 | $tmp = ''; |
---|
278 | parent::addSave($tmp); |
---|
279 | |
---|
280 | // add support for created_(at|on) and updated_(at|on) columns |
---|
281 | $date_script = ''; |
---|
282 | $updated = false; |
---|
283 | $created = false; |
---|
284 | foreach ($this->getTable()->getColumns() as $col) |
---|
285 | { |
---|
286 | $clo = strtolower($col->getName()); |
---|
287 | |
---|
288 | if (!$updated && in_array($clo, array('updated_at', 'updated_on'))) |
---|
289 | { |
---|
290 | $updated = true; |
---|
291 | $date_script .= " |
---|
292 | if (\$this->isModified() && !\$this->isColumnModified(".$this->getColumnConstant($col).")) |
---|
293 | { |
---|
294 | \$this->set".$col->getPhpName()."(time()); |
---|
295 | } |
---|
296 | "; |
---|
297 | } |
---|
298 | else if (!$created && in_array($clo, array('created_at', 'created_on'))) |
---|
299 | { |
---|
300 | $created = true; |
---|
301 | $date_script .= " |
---|
302 | if (\$this->isNew() && !\$this->isColumnModified(".$this->getColumnConstant($col).")) |
---|
303 | { |
---|
304 | \$this->set".$col->getPhpName()."(time()); |
---|
305 | } |
---|
306 | "; |
---|
307 | } |
---|
308 | } |
---|
309 | $tmp = preg_replace('/{/', '{'.$date_script, $tmp, 1); |
---|
310 | |
---|
311 | if (DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
---|
312 | { |
---|
313 | // add sfMixer call |
---|
314 | $pre_mixer_script = " |
---|
315 | |
---|
316 | foreach (sfMixer::getCallables('{$this->getClassname()}:save:pre') as \$callable) |
---|
317 | { |
---|
318 | \$affectedRows = call_user_func(\$callable, \$this, \$con); |
---|
319 | if (is_int(\$affectedRows)) |
---|
320 | { |
---|
321 | return \$affectedRows; |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | "; |
---|
326 | $post_mixer_script = <<<EOF |
---|
327 | |
---|
328 | foreach (sfMixer::getCallables('{$this->getClassname()}:save:post') as \$callable) |
---|
329 | { |
---|
330 | call_user_func(\$callable, \$this, \$con, \$affectedRows); |
---|
331 | } |
---|
332 | |
---|
333 | EOF; |
---|
334 | $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1); |
---|
335 | $tmp = preg_replace('/(\$con\->commit\(\);)/', '$1'.$post_mixer_script, $tmp); |
---|
336 | } |
---|
337 | |
---|
338 | // update current script |
---|
339 | $script .= $tmp; |
---|
340 | } |
---|
341 | } |
---|