Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * Translation behavior.
13
 *
14
 * @package     sfPropelPlugin
15
 * @subpackage  behavior
16
 * @author      Kris Wallsmith <kris.wallsmith@symfony-project.com>
17
 * @version     SVN: $Id: SfPropelBehaviorI18nTranslation.php 23310 2009-10-24 15:27:41Z Kris.Wallsmith $
18
 */
19
class SfPropelBehaviorI18nTranslation extends SfPropelBehaviorBase
20
{
21
  protected $parameters = array(
22
    'culture_column' => null,
23
  );
24
 
25
  public function objectFilter(& $script)
26
  {
27
    if ($this->isDisabled())
28
    {
29
      return;
30
    }
31
 
32
    $class = new sfClassManipulator($script);
33
    $class->filterMethod('doSave', array($this, 'filterDoSave'));
34
 
35
    $script = $class->getCode();
36
  }
37
 
38
  /**
39
   * Filters each line of the generated doSave method.
40
   *
41
   * @param string $line
42
   *
43
   * @return string
44
   */
45
  public function filterDoSave($line)
46
  {
47
    $foreignKey = $this->getForeignKey();
48
    $phpName = $foreignKey->getPhpName() ? $foreignKey->getPhpName() : $foreignKey->getForeignTable()->getPhpName();
49
    $refPhpName = $foreignKey->getRefPhpName() ? $foreignKey->getRefPhpName() : $this->getTable()->getPhpName();
50
    $search = sprintf('$this->a%s->isModified()', $phpName);
51
    $insert = sprintf(' || ($this->a%s->getCulture() && $this->a%1$s->getCurrent%s()->isModified())', $phpName, $refPhpName);
52
 
53
    if (false !== strpos($line, $search))
54
    {
55
      $line = str_replace($search, $search.$insert, $line);
56
    }
57
 
58
    return $line;
59
  }
60
 
61
  /**
62
   * Returns the foreign key that references the translated model.
63
   *
64
   * @return ForeignKey
65
   *
66
   * @throws LogicException If the foreign key cannot be found
67
   */
68
  public function getForeignKey()
69
  {
70
    foreach ($this->getTable()->getForeignKeys() as $fk)
71
    {
72
      $behaviors = $fk->getForeignTable()->getBehaviors();
73
      if (isset($behaviors['symfony_i18n']))
74
      {
75
        return $fk;
76
      }
77
    }
78
 
79
    throw new Exception('The foreign key that references the I18N model could not be found.');
80
  }
81
 
82
  /**
83
   * Returns the current table's culture column.
84
   *
85
   * @return Column
86
   */
87
  public function getCultureColumn()
88
  {
89
    return $this->getTable()->getColumn($this->getParameter('culture_column'));
90
  }
91
}