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.phpdoctrine.com>.
|
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.phpdoctrine.com
|
29 |
* @since 1.0
|
30 |
* @version $Revision$
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
32 |
*/
|
33 |
class Doctrine_I18n extends Doctrine_Plugin
|
34 |
{
|
35 |
protected $_options = array(
|
36 |
'className' => '%CLASS%Translation',
|
37 |
'fields' => array(),
|
38 |
'generateFiles' => false,
|
39 |
'table' => false,
|
40 |
'pluginTable' => false,
|
41 |
);
|
42 |
|
43 |
protected $_auditTable;
|
44 |
|
45 |
public function __construct($options)
|
46 |
{
|
47 |
$this->_options = array_merge($this->_options, $options);
|
48 |
}
|
49 |
|
50 |
public function buildDefinition(Doctrine_Table $table)
|
51 |
{
|
52 |
if (empty($this->_options['fields'])) {
|
53 |
throw new Doctrine_I18n_Exception('Fields not set.');
|
54 |
}
|
55 |
|
56 |
$this->_options['className'] = str_replace('%CLASS%',
|
57 |
$this->_options['table']->getComponentName(),
|
58 |
$this->_options['className']);
|
59 |
|
60 |
$name = $table->getComponentName();
|
61 |
|
62 |
if (class_exists($this->_options['className'])) {
|
63 |
return false;
|
64 |
}
|
65 |
|
66 |
$columns = array();
|
67 |
|
68 |
$id = $table->getIdentifier();
|
69 |
|
70 |
$options = array('className' => $this->_options['className']);
|
71 |
|
72 |
$fk = array();
|
73 |
foreach ((array) $id as $column) {
|
74 |
$def = $table->getDefinitionOf($column);
|
75 |
|
76 |
unset($def['autoincrement']);
|
77 |
unset($def['sequence']);
|
78 |
unset($def['unique']);
|
79 |
|
80 |
$fk[$column] = $def;
|
81 |
}
|
82 |
|
83 |
$cols = $table->getColumns();
|
84 |
|
85 |
foreach ($cols as $column => $definition) {
|
86 |
if (in_array($column, $this->_options['fields'])) {
|
87 |
$columns[$column] = $definition;
|
88 |
$table->removeColumn($column);
|
89 |
}
|
90 |
}
|
91 |
|
92 |
$columns['lang'] = array('type' => 'string',
|
93 |
'length' => 2,
|
94 |
'fixed' => true,
|
95 |
'primary' => true);
|
96 |
|
97 |
$local = (count($fk) > 1) ? array_keys($fk) : key($fk);
|
98 |
|
99 |
$relations = array($name => array('local' => $local,
|
100 |
'foreign' => $id,
|
101 |
'onDelete' => 'CASCADE',
|
102 |
'onUpdate' => 'CASCADE'));
|
103 |
|
104 |
|
105 |
$columns += $fk;
|
106 |
|
107 |
$options = array('className' => $this->_options['className'],
|
108 |
'queryParts' => array('indexBy' => 'lang'));
|
109 |
|
110 |
$this->generateClass($options, $columns, $relations);
|
111 |
|
112 |
$this->_options['pluginTable'] = $table->getConnection()->getTable($this->_options['className']);
|
113 |
|
114 |
$this->_options['pluginTable']->bindQueryPart('indexBy', 'lang');
|
115 |
|
116 |
return true;
|
117 |
}
|
118 |
}
|