PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/servers/urlcatcher.org/htdocs/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/behavior/SfPropelBehaviorI18n.php

https://github.com/mrwabu/urlcatcher
PHP | 352 lines | 200 code | 54 blank | 98 comment | 21 complexity | 47d5174259d8f68ac49caf2d0253c728 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Internationalizes Propel models.
  11. *
  12. * This behavior is intended to be applied at the database level.
  13. *
  14. * @package sfPropelPlugin
  15. * @subpackage behavior
  16. * @author Kris Wallsmith <kris.wallsmith@symfony-project.com>
  17. * @version SVN: $Id: SfPropelBehaviorI18n.php 23310 2009-10-24 15:27:41Z Kris.Wallsmith $
  18. */
  19. class SfPropelBehaviorI18n extends SfPropelBehaviorBase
  20. {
  21. protected $parameters = array(
  22. 'i18n_table' => null,
  23. );
  24. /**
  25. * Looks for tables marked as I18N and adds behaviors.
  26. */
  27. public function modifyDatabase()
  28. {
  29. $translationBehavior = sfPropel::importClass($this->getBuildProperty('propel.behavior.symfony_i18n_translation.class'));
  30. foreach ($this->getDatabase()->getTables() as $table)
  31. {
  32. $behaviors = $table->getBehaviors();
  33. if (!isset($behaviors['symfony_i18n']) && 'true' == $table->getAttribute('isI18N'))
  34. {
  35. $i18nTable = $this->getDatabase()->getTable($table->getAttribute('i18nTable'));
  36. // add the current behavior to the translatable model
  37. $behavior = clone $this;
  38. $behavior->setParameters(array('i18n_table' => $i18nTable->getName()));
  39. $table->addBehavior($behavior);
  40. // add the translation behavior to the translation model
  41. $behavior = new $translationBehavior();
  42. $behavior->setName('symfony_i18n_translation');
  43. $behavior->setParameters(array('culture_column' => $this->getCultureColumn($i18nTable)->getName()));
  44. $i18nTable->addBehavior($behavior);
  45. }
  46. }
  47. }
  48. public function modifyTable()
  49. {
  50. if ($this->isDisabled())
  51. {
  52. return;
  53. }
  54. if (count($this->getTable()->getPrimaryKey()) > 1)
  55. {
  56. throw new Exception('i18n support only works with a single primary key');
  57. }
  58. }
  59. public function objectAttributes()
  60. {
  61. if ($this->isDisabled())
  62. {
  63. return;
  64. }
  65. return <<<EOF
  66. /**
  67. * @var string The value for the culture field
  68. */
  69. protected \$culture = null;
  70. /**
  71. * @var array Current I18N objects
  72. */
  73. protected \$current_i18n = array();
  74. EOF;
  75. }
  76. public function objectMethods()
  77. {
  78. if ($this->isDisabled())
  79. {
  80. return;
  81. }
  82. $script = <<<EOF
  83. /**
  84. * Returns the culture.
  85. *
  86. * @return string The culture
  87. */
  88. public function getCulture()
  89. {
  90. return \$this->culture;
  91. }
  92. /**
  93. * Sets the culture.
  94. *
  95. * @param string $culture The culture to set
  96. *
  97. * @return {$this->getTable()->getPhpName()}
  98. */
  99. public function setCulture(\$culture)
  100. {
  101. \$this->culture = \$culture;
  102. return \$this;
  103. }
  104. EOF;
  105. // add accessors and mutators for each of the i18nTable's columns
  106. $foreignKey = $this->getI18nTable()->getBehavior('symfony_i18n_translation')->getForeignKey();
  107. $refPhpName = $foreignKey->getRefPhpName() ? $foreignKey->getRefPhpName() : $this->getI18nTable()->getPhpName();
  108. foreach ($this->getI18nTable()->getColumns() as $column)
  109. {
  110. if ($column->isPrimaryKey())
  111. {
  112. continue;
  113. }
  114. $script .= <<<EOF
  115. /**
  116. * Returns the "{$column->getName()}" value from the current {@link {$this->getI18nTable()->getPhpName()}}.
  117. */
  118. public function get{$column->getPhpName()}(\$culture = null)
  119. {
  120. return \$this->getCurrent{$refPhpName}(\$culture)->get{$column->getPhpName()}();
  121. }
  122. /**
  123. * Sets the "{$column->getName()}" value of the current {@link {$this->getI18nTable()->getPhpName()}}.
  124. *
  125. * @return {$this->getTable()->getPhpName()}
  126. */
  127. public function set{$column->getPhpName()}(\$value, \$culture = null)
  128. {
  129. \$this->getCurrent{$refPhpName}(\$culture)->set{\$column->getPhpName()}(\$value);
  130. return \$this;
  131. }
  132. EOF;
  133. }
  134. $script .= <<<EOF
  135. /**
  136. * Returns the current translation.
  137. *
  138. * @return {$this->getI18nTable()->getPhpName()}
  139. */
  140. public function getCurrent{$refPhpName}(\$culture = null)
  141. {
  142. if (null === \$culture)
  143. {
  144. \$culture = null === \$this->culture ? sfPropel::getDefaultCulture() : \$this->culture;
  145. }
  146. if (!isset(\$this->current_i18n[\$culture]))
  147. {
  148. if (\$object = {$this->getI18nTable()->getPhpName()}Peer::retrieveByPK(\$this->getPrimaryKey(), \$culture))
  149. {
  150. \$this->set{$refPhpName}ForCulture(\$object, \$culture);
  151. }
  152. else
  153. {
  154. \$this->set{$refPhpName}ForCulture(new {$this->getI18nTable()->getPhpName()}(), \$culture);
  155. \$this->current_i18n[\$culture]->set{$this->getI18nTable()->getBehavior('symfony_i18n_translation')->getCultureColumn()->getPhpName()}(\$culture);
  156. }
  157. }
  158. return \$this->current_i18n[\$culture];
  159. }
  160. /**
  161. * Sets the translation object for a culture.
  162. */
  163. public function set{$refPhpName}ForCulture({$this->getI18nTable()->getPhpName()} \$object, \$culture)
  164. {
  165. \$this->current_i18n[\$culture] = \$object;
  166. \$this->add{$refPhpName}(\$object);
  167. }
  168. EOF;
  169. return $script;
  170. }
  171. public function staticMethods()
  172. {
  173. $foreignKey = $this->getI18nTable()->getBehavior('symfony_i18n_translation')->getForeignKey();
  174. $refPhpName = $foreignKey->getRefPhpName() ? $foreignKey->getRefPhpName() : $this->getI18nTable()->getPhpName();
  175. $join = in_array($this->getBuildProperty('propel.useLeftJoinsInDoJoinMethods'), array(true, null), true) ? 'LEFT' : 'INNER';
  176. $behaviors = $this->getTable()->getBehaviors();
  177. $mixerHook = !isset($behaviors['symfony_behaviors']) ? '' : <<<EOF
  178. foreach (sfMixer::getCallables('Base{$this->getTable()->getPhpName()}:doSelectJoin:doSelectJoin') as \$sf_hook)
  179. {
  180. call_user_func(\$sf_hook, '{$this->getTable()->getPhpName()}', \$criteria, \$con);
  181. }
  182. EOF;
  183. return <<<EOF
  184. /**
  185. * Returns the i18n model class name.
  186. *
  187. * @return string The i18n model class name
  188. */
  189. static public function getI18nModel()
  190. {
  191. return '{$this->getI18nTable()->getPhpName()}';
  192. }
  193. /**
  194. * Selects a collection of {@link {$this->getTable()->getPhpName()}} objects with a {@link {$this->getI18nTable()->getPhpName()}} translation populated.
  195. *
  196. * @param Criteria \$criteria
  197. * @param string \$culture
  198. * @param PropelPDO \$con
  199. * @param string \$join_behavior
  200. *
  201. * @return array
  202. */
  203. static public function doSelectWithI18n(Criteria \$criteria, \$culture = null, \$con = null, \$join_behavior = Criteria::{$join}_JOIN)
  204. {
  205. \$criteria = clone \$criteria;
  206. if (null === \$culture)
  207. {
  208. \$culture = sfPropel::getDefaultCulture();
  209. }
  210. // Set the correct dbName if it has not been overridden
  211. if (\$criteria->getDbName() == Propel::getDefaultDB()) {
  212. \$criteria->setDbName(self::DATABASE_NAME);
  213. }
  214. {$this->getTable()->getPhpName()}Peer::addSelectColumns(\$criteria);
  215. \$startcol = ({$this->getTable()->getPhpName()}Peer::NUM_COLUMNS - {$this->getTable()->getPhpName()}Peer::NUM_LAZY_LOAD_COLUMNS);
  216. {$this->getI18nTable()->getPhpName()}Peer::addSelectColumns(\$criteria);
  217. \$criteria->addJoin({$this->getLocalColumn()->getConstantName()}, {$this->getForeignColumn()->getConstantName()}, \$join_behavior);
  218. {$mixerHook}
  219. \$stmt = BasePeer::doSelect(\$criteria, \$con);
  220. \$results = array();
  221. while (\$row = \$stmt->fetch(PDO::FETCH_NUM)) {
  222. \$key1 = {$this->getTable()->getPhpName()}Peer::getPrimaryKeyHashFromRow(\$row, 0);
  223. if (null !== (\$obj1 = {$this->getTable()->getPhpName()}Peer::getInstanceFromPool(\$key1))) {
  224. // We no longer rehydrate the object, since this can cause data loss.
  225. // See http://propel.phpdb.org/trac/ticket/509
  226. // \$obj1->hydrate(\$row, 0, true); // rehydrate
  227. } else {
  228. \$cls = {$this->getTable()->getPhpName()}Peer::getOMClass(false);
  229. \$obj1 = new \$cls();
  230. \$obj1->hydrate(\$row);
  231. {$this->getTable()->getPhpName()}Peer::addInstanceToPool(\$obj1, \$key1);
  232. } // if \$obj1 already loaded
  233. \$key2 = {$this->getI18nTable()->getPhpName()}Peer::getPrimaryKeyHashFromRow(\$row, \$startcol);
  234. if (\$key2 !== null) {
  235. \$obj2 = {$this->getI18nTable()->getPhpName()}Peer::getInstanceFromPool(\$key2);
  236. if (!\$obj2) {
  237. \$cls = {$this->getI18nTable()->getPhpName()}Peer::getOMClass(false);
  238. \$obj2 = new \$cls();
  239. \$obj2->hydrate(\$row, \$startcol);
  240. {$this->getI18nTable()->getPhpName()}Peer::addInstanceToPool(\$obj2, \$key2);
  241. } // if obj2 already loaded
  242. \$obj1->set{$refPhpName}ForCulture(\$obj2, \$culture);
  243. } // if joined row was not null
  244. \$results[] = \$obj1;
  245. }
  246. \$stmt->closeCursor();
  247. return \$results;
  248. }
  249. EOF;
  250. }
  251. /**
  252. * Returns the current table's i18n translation table.
  253. *
  254. * @return Table
  255. */
  256. public function getI18nTable()
  257. {
  258. return $this->getDatabase()->getTable($this->getParameter('i18n_table'));
  259. }
  260. /**
  261. * Finds the supplied translation table's culture column.
  262. *
  263. * @return Column
  264. *
  265. * @throws InvalidArgumentException If there is not a column marked as "isCulture"
  266. */
  267. protected function getCultureColumn(Table $table)
  268. {
  269. foreach ($table->getColumns() as $column)
  270. {
  271. if ('true' == $column->getAttribute('isCulture'))
  272. {
  273. return $column;
  274. }
  275. }
  276. throw new InvalidArgumentException(sprintf('The table "%s" does not have a column marked with the "isCulture" attribute.', $table->getName()));
  277. }
  278. /**
  279. * Returns the column on the current model referenced by the translation model.
  280. *
  281. * @return Column
  282. */
  283. protected function getLocalColumn()
  284. {
  285. $columns = $this->getI18nTable()->getBehavior('symfony_i18n_translation')->getForeignKey()->getForeignColumns();
  286. return $this->getTable()->getColumn($columns[0]);
  287. }
  288. /**
  289. * Returns the column on the translation table the references the current model.
  290. *
  291. * @return Column
  292. */
  293. protected function getForeignColumn()
  294. {
  295. $columns = $this->getI18nTable()->getBehavior('symfony_i18n_translation')->getForeignKey()->getLocalColumns();
  296. return $this->getI18nTable()->getColumn($columns[0]);
  297. }
  298. }