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 |
/**
|
44 |
* __construct
|
45 |
*
|
46 |
* @param string $options
|
47 |
* @return void
|
48 |
*/
|
49 |
public function __construct($options)
|
50 |
{
|
51 |
$this->_options = array_merge($this->_options, $options);
|
52 |
}
|
53 |
|
54 |
/**
|
55 |
* buildDefinition
|
56 |
*
|
57 |
* @param object $Doctrine_Table
|
58 |
* @return void
|
59 |
*/
|
60 |
public function buildDefinition(Doctrine_Table $table)
|
61 |
{
|
62 |
if (empty($this->_options['fields'])) {
|
63 |
throw new Doctrine_I18n_Exception('Fields not set.');
|
64 |
}
|
65 |
|
66 |
$this->_options['className'] = str_replace('%CLASS%',
|
67 |
$this->_options['table']->getComponentName(),
|
68 |
$this->_options['className']);
|
69 |
|
70 |
$name = $table->getComponentName();
|
71 |
|
72 |
if (class_exists($this->_options['className'])) {
|
73 |
return false;
|
74 |
}
|
75 |
|
76 |
$columns = array();
|
77 |
|
78 |
$id = $table->getIdentifier();
|
79 |
|
80 |
$options = array('className' => $this->_options['className']);
|
81 |
|
82 |
$fk = array();
|
83 |
foreach ((array) $id as $column) {
|
84 |
$def = $table->getDefinitionOf($column);
|
85 |
|
86 |
unset($def['autoincrement']);
|
87 |
unset($def['sequence']);
|
88 |
unset($def['unique']);
|
89 |
|
90 |
$fk[$column] = $def;
|
91 |
}
|
92 |
|
93 |
$cols = $table->getColumns();
|
94 |
|
95 |
foreach ($cols as $column => $definition) {
|
96 |
if (in_array($column, $this->_options['fields'])) {
|
97 |
$columns[$column] = $definition;
|
98 |
$table->removeColumn($column);
|
99 |
}
|
100 |
}
|
101 |
|
102 |
$columns['lang'] = array('type' => 'string',
|
103 |
'length' => 2,
|
104 |
'fixed' => true,
|
105 |
'primary' => true);
|
106 |
|
107 |
$local = (count($fk) > 1) ? array_keys($fk) : key($fk);
|
108 |
|
109 |
$relations = array($name => array('local' => $local,
|
110 |
'foreign' => $id,
|
111 |
'onDelete' => 'CASCADE',
|
112 |
'onUpdate' => 'CASCADE'));
|
113 |
|
114 |
|
115 |
$columns += $fk;
|
116 |
|
117 |
$options = array('className' => $this->_options['className'],
|
118 |
'queryParts' => array('indexBy' => 'lang'));
|
119 |
|
120 |
$this->generateClass($options, $columns, $relations);
|
121 |
|
122 |
$this->_options['pluginTable'] = $table->getConnection()->getTable($this->_options['className']);
|
123 |
|
124 |
$this->_options['pluginTable']->bindQueryPart('indexBy', 'lang');
|
125 |
|
126 |
return true;
|
127 |
}
|
128 |
} |