PageRenderTime 68ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/behavior/SfPropelBehaviorI18n.php

https://github.com/IDCI-Consulting/WebsiteEval
PHP | 389 lines | 222 code | 59 blank | 108 comment | 24 complexity | 6737d28d8fd70ba4cd8f05fc108f53cf 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 24597 2009-11-30 19:53:50Z 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 = Propel::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. \$object = \$this->isNew() ? null : {$this->getI18nTable()->getPhpName()}Peer::retrieveByPK(\$this->getPrimaryKey(), \$culture);
  149. if (\$object)
  150. {
  151. \$this->set{$refPhpName}ForCulture(\$object, \$culture);
  152. }
  153. else
  154. {
  155. \$this->set{$refPhpName}ForCulture(new {$this->getI18nTable()->getPhpName()}(), \$culture);
  156. \$this->current_i18n[\$culture]->set{$this->getI18nTable()->getBehavior('symfony_i18n_translation')->getCultureColumn()->getPhpName()}(\$culture);
  157. }
  158. }
  159. return \$this->current_i18n[\$culture];
  160. }
  161. /**
  162. * Sets the translation object for a culture.
  163. */
  164. public function set{$refPhpName}ForCulture({$this->getI18nTable()->getPhpName()} \$object, \$culture)
  165. {
  166. \$this->current_i18n[\$culture] = \$object;
  167. \$this->add{$refPhpName}(\$object);
  168. }
  169. EOF;
  170. if (!$this->hasPrimaryString($this->getTable()) && $this->hasPrimaryString($this->getI18nTable()))
  171. {
  172. $script .= <<<EOF
  173. /**
  174. * @see {$this->getI18nTable()->getPhpName()}
  175. */
  176. public function __toString()
  177. {
  178. return (string) \$this->getCurrent{$refPhpName}();
  179. }
  180. EOF;
  181. }
  182. return $script;
  183. }
  184. public function staticMethods()
  185. {
  186. $foreignKey = $this->getI18nTable()->getBehavior('symfony_i18n_translation')->getForeignKey();
  187. $refPhpName = $foreignKey->getRefPhpName() ? $foreignKey->getRefPhpName() : $this->getI18nTable()->getPhpName();
  188. $join = in_array($this->getBuildProperty('propel.useLeftJoinsInDoJoinMethods'), array(true, null), true) ? 'LEFT' : 'INNER';
  189. $behaviors = $this->getTable()->getBehaviors();
  190. $mixerHook = !isset($behaviors['symfony_behaviors']) ? '' : <<<EOF
  191. foreach (sfMixer::getCallables('Base{$this->getTable()->getPhpName()}:doSelectJoin:doSelectJoin') as \$sf_hook)
  192. {
  193. call_user_func(\$sf_hook, '{$this->getTable()->getPhpName()}', \$criteria, \$con);
  194. }
  195. EOF;
  196. return <<<EOF
  197. /**
  198. * Returns the i18n model class name.
  199. *
  200. * @return string The i18n model class name
  201. */
  202. static public function getI18nModel()
  203. {
  204. return '{$this->getI18nTable()->getPhpName()}';
  205. }
  206. /**
  207. * Selects a collection of {@link {$this->getTable()->getPhpName()}} objects with a {@link {$this->getI18nTable()->getPhpName()}} translation populated.
  208. *
  209. * @param Criteria \$criteria
  210. * @param string \$culture
  211. * @param PropelPDO \$con
  212. * @param string \$join_behavior
  213. *
  214. * @return array
  215. */
  216. static public function doSelectWithI18n(Criteria \$criteria, \$culture = null, \$con = null, \$join_behavior = Criteria::{$join}_JOIN)
  217. {
  218. \$criteria = clone \$criteria;
  219. if (null === \$culture)
  220. {
  221. \$culture = sfPropel::getDefaultCulture();
  222. }
  223. // Set the correct dbName if it has not been overridden
  224. if (\$criteria->getDbName() == Propel::getDefaultDB()) {
  225. \$criteria->setDbName(self::DATABASE_NAME);
  226. }
  227. {$this->getTable()->getPhpName()}Peer::addSelectColumns(\$criteria);
  228. \$startcol = ({$this->getTable()->getPhpName()}Peer::NUM_COLUMNS - {$this->getTable()->getPhpName()}Peer::NUM_LAZY_LOAD_COLUMNS);
  229. {$this->getI18nTable()->getPhpName()}Peer::addSelectColumns(\$criteria);
  230. \$criteria->addJoin({$this->getLocalColumn()->getConstantName()}, {$this->getForeignColumn()->getConstantName()}, \$join_behavior);
  231. \$criteria->add({$this->getCultureColumn($this->getI18nTable())->getConstantName()}, \$culture);
  232. {$mixerHook}
  233. \$stmt = BasePeer::doSelect(\$criteria, \$con);
  234. \$results = array();
  235. while (\$row = \$stmt->fetch(PDO::FETCH_NUM)) {
  236. \$key1 = {$this->getTable()->getPhpName()}Peer::getPrimaryKeyHashFromRow(\$row, 0);
  237. if (null !== (\$obj1 = {$this->getTable()->getPhpName()}Peer::getInstanceFromPool(\$key1))) {
  238. // We no longer rehydrate the object, since this can cause data loss.
  239. // See http://propel.phpdb.org/trac/ticket/509
  240. // \$obj1->hydrate(\$row, 0, true); // rehydrate
  241. } else {
  242. \$cls = {$this->getTable()->getPhpName()}Peer::getOMClass(false);
  243. \$obj1 = new \$cls();
  244. \$obj1->hydrate(\$row);
  245. {$this->getTable()->getPhpName()}Peer::addInstanceToPool(\$obj1, \$key1);
  246. } // if \$obj1 already loaded
  247. \$key2 = {$this->getI18nTable()->getPhpName()}Peer::getPrimaryKeyHashFromRow(\$row, \$startcol);
  248. if (\$key2 !== null) {
  249. \$obj2 = {$this->getI18nTable()->getPhpName()}Peer::getInstanceFromPool(\$key2);
  250. if (!\$obj2) {
  251. \$cls = {$this->getI18nTable()->getPhpName()}Peer::getOMClass(false);
  252. \$obj2 = new \$cls();
  253. \$obj2->hydrate(\$row, \$startcol);
  254. {$this->getI18nTable()->getPhpName()}Peer::addInstanceToPool(\$obj2, \$key2);
  255. } // if obj2 already loaded
  256. \$obj1->set{$refPhpName}ForCulture(\$obj2, \$culture);
  257. } // if joined row was not null
  258. \$results[] = \$obj1;
  259. }
  260. \$stmt->closeCursor();
  261. return \$results;
  262. }
  263. EOF;
  264. }
  265. /**
  266. * Returns the current table's i18n translation table.
  267. *
  268. * @return Table
  269. */
  270. public function getI18nTable()
  271. {
  272. return $this->getDatabase()->getTable($this->getParameter('i18n_table'));
  273. }
  274. /**
  275. * Finds the supplied translation table's culture column.
  276. *
  277. * @return Column
  278. *
  279. * @throws InvalidArgumentException If there is not a column marked as "isCulture"
  280. */
  281. protected function getCultureColumn(Table $table)
  282. {
  283. foreach ($table->getColumns() as $column)
  284. {
  285. if ('true' == $column->getAttribute('isCulture'))
  286. {
  287. return $column;
  288. }
  289. }
  290. throw new InvalidArgumentException(sprintf('The table "%s" does not have a column marked with the "isCulture" attribute.', $table->getName()));
  291. }
  292. /**
  293. * Returns the column on the current model referenced by the translation model.
  294. *
  295. * @return Column
  296. */
  297. protected function getLocalColumn()
  298. {
  299. $columns = $this->getI18nTable()->getBehavior('symfony_i18n_translation')->getForeignKey()->getForeignColumns();
  300. return $this->getTable()->getColumn($columns[0]);
  301. }
  302. /**
  303. * Returns the column on the translation table the references the current model.
  304. *
  305. * @return Column
  306. */
  307. protected function getForeignColumn()
  308. {
  309. $columns = $this->getI18nTable()->getBehavior('symfony_i18n_translation')->getForeignKey()->getLocalColumns();
  310. return $this->getI18nTable()->getColumn($columns[0]);
  311. }
  312. /**
  313. * Checks whether the supplied table has a primary string defined.
  314. *
  315. * @param Table $table
  316. *
  317. * @return boolean
  318. */
  319. protected function hasPrimaryString(Table $table)
  320. {
  321. foreach ($table->getColumns() as $column)
  322. {
  323. if ($column->isPrimaryString())
  324. {
  325. return true;
  326. }
  327. }
  328. return false;
  329. }
  330. }