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