| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id$
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information, see
|
|
|
19 |
* <http://www.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Doctrine_I18n
|
|
|
24 |
*
|
|
|
25 |
* @package Doctrine
|
|
|
26 |
* @subpackage I18n
|
|
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
28 |
* @link www.doctrine-project.org
|
|
|
29 |
* @since 1.0
|
|
|
30 |
* @version $Revision$
|
|
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
32 |
*/
|
|
|
33 |
class Doctrine_I18n extends Doctrine_Record_Generator
|
|
|
34 |
{
|
|
|
35 |
protected $_options = array(
|
|
|
36 |
'className' => '%CLASS%Translation',
|
|
|
37 |
'tableName' => '%TABLE%_translation',
|
|
|
38 |
'fields' => array(),
|
|
|
39 |
'generateFiles' => false,
|
|
|
40 |
'table' => false,
|
|
|
41 |
'pluginTable' => false,
|
|
|
42 |
'children' => array(),
|
|
|
43 |
'i18nField' => 'lang',
|
|
|
44 |
'type' => 'string',
|
|
|
45 |
'length' => 2,
|
|
|
46 |
'options' => array(),
|
|
|
47 |
'cascadeDelete' => true,
|
|
|
48 |
'appLevelDelete'=> false
|
|
|
49 |
);
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* __construct
|
|
|
53 |
*
|
|
|
54 |
* @param string $options
|
|
|
55 |
* @return void
|
|
|
56 |
*/
|
|
|
57 |
public function __construct($options)
|
|
|
58 |
{
|
|
|
59 |
$this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public function buildRelation()
|
|
|
63 |
{
|
|
|
64 |
$this->buildForeignRelation('Translation');
|
|
|
65 |
$this->buildLocalRelation();
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* buildDefinition
|
|
|
70 |
*
|
|
|
71 |
* @param object $Doctrine_Table
|
|
|
72 |
* @return void
|
|
|
73 |
*/
|
|
|
74 |
public function setTableDefinition()
|
|
|
75 |
{
|
|
|
76 |
if (empty($this->_options['fields'])) {
|
|
|
77 |
throw new Doctrine_I18n_Exception('Fields not set.');
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$options = array('className' => $this->_options['className']);
|
|
|
81 |
|
|
|
82 |
$cols = $this->_options['table']->getColumns();
|
|
|
83 |
|
|
|
84 |
$columns = array();
|
|
|
85 |
foreach ($cols as $column => $definition) {
|
|
|
86 |
$fieldName = $this->_options['table']->getFieldName($column);
|
|
|
87 |
if (in_array($fieldName, $this->_options['fields'])) {
|
|
|
88 |
if ($column != $fieldName) {
|
|
|
89 |
$column .= ' as ' . $fieldName;
|
|
|
90 |
}
|
|
|
91 |
$columns[$column] = $definition;
|
|
|
92 |
$this->_options['table']->removeColumn($fieldName);
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
$this->hasColumns($columns);
|
|
|
97 |
|
|
|
98 |
$defaultOptions = array(
|
|
|
99 |
'fixed' => true,
|
|
|
100 |
'primary' => true
|
|
|
101 |
);
|
|
|
102 |
$options = array_merge($defaultOptions, $this->_options['options']);
|
|
|
103 |
|
|
|
104 |
$this->hasColumn($this->_options['i18nField'], $this->_options['type'], $this->_options['length'], $options);
|
|
|
105 |
|
|
|
106 |
$this->bindQueryParts(array('indexBy' => $this->_options['i18nField']));
|
|
|
107 |
|
|
|
108 |
// Rewrite any relations to our original table
|
|
|
109 |
$originalName = $this->_options['table']->getClassnameToReturn();
|
|
|
110 |
$relations = $this->_options['table']->getRelationParser()->getPendingRelations();
|
|
|
111 |
foreach($relations as $table => $relation) {
|
|
|
112 |
if ($table != $this->_table->getTableName() ) {
|
|
|
113 |
// check that the localColumn is part of the moved col
|
|
|
114 |
if (isset($relation['local']) && in_array($relation['local'], $this->_options['fields'])) {
|
|
|
115 |
// found one, let's rewrite it
|
|
|
116 |
$this->_options['table']->getRelationParser()->unsetPendingRelations($table);
|
|
|
117 |
|
|
|
118 |
// and bind the rewritten one
|
|
|
119 |
$this->_table->getRelationParser()->bind($table, $relation);
|
|
|
120 |
|
|
|
121 |
// now try to get the reverse relation, to rewrite it
|
|
|
122 |
$rp = Doctrine_Core::getTable($table)->getRelationParser();
|
|
|
123 |
$others = $rp->getPendingRelation($originalName);
|
|
|
124 |
if (isset($others)) {
|
|
|
125 |
$others['class'] = $this->_table->getClassnameToReturn();
|
|
|
126 |
$others['alias'] = $this->_table->getClassnameToReturn();
|
|
|
127 |
$rp->unsetPendingRelations($originalName);
|
|
|
128 |
$rp->bind($this->_table->getClassnameToReturn() ,$others);
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
}
|