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