PageRenderTime 53ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/model/om/BaseFunctionI18n.php

https://github.com/mikesname/ehri-ica-atom
PHP | 577 lines | 450 code | 107 blank | 20 comment | 62 complexity | e907f6cdf17fa6bdd9711b2d9edfc651 MD5 | raw file
  1. <?php
  2. abstract class BaseFunctionI18n implements ArrayAccess
  3. {
  4. const
  5. DATABASE_NAME = 'propel',
  6. TABLE_NAME = 'function_i18n',
  7. AUTHORIZED_FORM_OF_NAME = 'function_i18n.AUTHORIZED_FORM_OF_NAME',
  8. CLASSIFICATION = 'function_i18n.CLASSIFICATION',
  9. DATES = 'function_i18n.DATES',
  10. DESCRIPTION = 'function_i18n.DESCRIPTION',
  11. HISTORY = 'function_i18n.HISTORY',
  12. LEGISLATION = 'function_i18n.LEGISLATION',
  13. INSTITUTION_IDENTIFIER = 'function_i18n.INSTITUTION_IDENTIFIER',
  14. REVISION_HISTORY = 'function_i18n.REVISION_HISTORY',
  15. RULES = 'function_i18n.RULES',
  16. SOURCES = 'function_i18n.SOURCES',
  17. ID = 'function_i18n.ID',
  18. CULTURE = 'function_i18n.CULTURE';
  19. public static function addSelectColumns(Criteria $criteria)
  20. {
  21. $criteria->addSelectColumn(QubitFunctionI18n::AUTHORIZED_FORM_OF_NAME);
  22. $criteria->addSelectColumn(QubitFunctionI18n::CLASSIFICATION);
  23. $criteria->addSelectColumn(QubitFunctionI18n::DATES);
  24. $criteria->addSelectColumn(QubitFunctionI18n::DESCRIPTION);
  25. $criteria->addSelectColumn(QubitFunctionI18n::HISTORY);
  26. $criteria->addSelectColumn(QubitFunctionI18n::LEGISLATION);
  27. $criteria->addSelectColumn(QubitFunctionI18n::INSTITUTION_IDENTIFIER);
  28. $criteria->addSelectColumn(QubitFunctionI18n::REVISION_HISTORY);
  29. $criteria->addSelectColumn(QubitFunctionI18n::RULES);
  30. $criteria->addSelectColumn(QubitFunctionI18n::SOURCES);
  31. $criteria->addSelectColumn(QubitFunctionI18n::ID);
  32. $criteria->addSelectColumn(QubitFunctionI18n::CULTURE);
  33. return $criteria;
  34. }
  35. protected static
  36. $functionI18ns = array();
  37. protected
  38. $keys = array(),
  39. $row = array();
  40. public static function getFromRow(array $row)
  41. {
  42. $keys = array();
  43. $keys['id'] = $row[10];
  44. $keys['culture'] = $row[11];
  45. $key = serialize($keys);
  46. if (!isset(self::$functionI18ns[$key]))
  47. {
  48. $functionI18n = new QubitFunctionI18n;
  49. $functionI18n->keys = $keys;
  50. $functionI18n->row = $row;
  51. $functionI18n->new = false;
  52. self::$functionI18ns[$key] = $functionI18n;
  53. }
  54. return self::$functionI18ns[$key];
  55. }
  56. public static function get(Criteria $criteria, array $options = array())
  57. {
  58. if (!isset($options['connection']))
  59. {
  60. $options['connection'] = Propel::getConnection(QubitFunctionI18n::DATABASE_NAME);
  61. }
  62. self::addSelectColumns($criteria);
  63. return QubitQuery::createFromCriteria($criteria, 'QubitFunctionI18n', $options);
  64. }
  65. public static function getAll(array $options = array())
  66. {
  67. return self::get(new Criteria, $options);
  68. }
  69. public static function getOne(Criteria $criteria, array $options = array())
  70. {
  71. $criteria->setLimit(1);
  72. return self::get($criteria, $options)->__get(0, array('defaultValue' => null));
  73. }
  74. public static function getByIdAndCulture($id, $culture, array $options = array())
  75. {
  76. $criteria = new Criteria;
  77. $criteria->add(QubitFunctionI18n::ID, $id);
  78. $criteria->add(QubitFunctionI18n::CULTURE, $culture);
  79. if (1 == count($query = self::get($criteria, $options)))
  80. {
  81. return $query[0];
  82. }
  83. }
  84. public static function doDelete(Criteria $criteria, $connection = null)
  85. {
  86. if (!isset($connection))
  87. {
  88. $connection = QubitTransactionFilter::getConnection(QubitFunctionI18n::DATABASE_NAME);
  89. }
  90. $affectedRows = 0;
  91. $affectedRows += BasePeer::doDelete($criteria, $connection);
  92. return $affectedRows;
  93. }
  94. protected
  95. $tables = array();
  96. public function __construct()
  97. {
  98. $this->tables[] = Propel::getDatabaseMap(QubitFunctionI18n::DATABASE_NAME)->getTable(QubitFunctionI18n::TABLE_NAME);
  99. }
  100. protected
  101. $values = array(),
  102. $refFkValues = array();
  103. protected function rowOffsetGet($name, $offset, $options)
  104. {
  105. if (empty($options['clean']) && array_key_exists($name, $this->values))
  106. {
  107. return $this->values[$name];
  108. }
  109. if (array_key_exists($name, $this->keys))
  110. {
  111. return $this->keys[$name];
  112. }
  113. if (!array_key_exists($offset, $this->row))
  114. {
  115. if ($this->new)
  116. {
  117. return;
  118. }
  119. if (!isset($options['connection']))
  120. {
  121. $options['connection'] = Propel::getConnection(QubitFunctionI18n::DATABASE_NAME);
  122. }
  123. $criteria = new Criteria;
  124. $criteria->add(QubitFunctionI18n::ID, $this->id);
  125. $criteria->add(QubitFunctionI18n::CULTURE, $this->culture);
  126. call_user_func(array(get_class($this), 'addSelectColumns'), $criteria);
  127. $statement = BasePeer::doSelect($criteria, $options['connection']);
  128. $this->row = $statement->fetch();
  129. }
  130. return $this->row[$offset];
  131. }
  132. public function __isset($name)
  133. {
  134. $args = func_get_args();
  135. $options = array();
  136. if (1 < count($args))
  137. {
  138. $options = $args[1];
  139. }
  140. $offset = 0;
  141. foreach ($this->tables as $table)
  142. {
  143. foreach ($table->getColumns() as $column)
  144. {
  145. if ($name == $column->getPhpName())
  146. {
  147. return null !== $this->rowOffsetGet($name, $offset, $options);
  148. }
  149. if ("{$name}Id" == $column->getPhpName())
  150. {
  151. return null !== $this->rowOffsetGet("{$name}Id", $offset, $options);
  152. }
  153. $offset++;
  154. }
  155. }
  156. throw new sfException("Unknown record property \"$name\" on \"".get_class($this).'"');
  157. }
  158. public function offsetExists($offset)
  159. {
  160. $args = func_get_args();
  161. return call_user_func_array(array($this, '__isset'), $args);
  162. }
  163. public function __get($name)
  164. {
  165. $args = func_get_args();
  166. $options = array();
  167. if (1 < count($args))
  168. {
  169. $options = $args[1];
  170. }
  171. $offset = 0;
  172. foreach ($this->tables as $table)
  173. {
  174. foreach ($table->getColumns() as $column)
  175. {
  176. if ($name == $column->getPhpName())
  177. {
  178. return $this->rowOffsetGet($name, $offset, $options);
  179. }
  180. if ("{$name}Id" == $column->getPhpName())
  181. {
  182. $relatedTable = $column->getTable()->getDatabaseMap()->getTable($column->getRelatedTableName());
  183. return call_user_func(array($relatedTable->getClassName(), 'getBy'.ucfirst($relatedTable->getColumn($column->getRelatedColumnName())->getPhpName())), $this->rowOffsetGet("{$name}Id", $offset, $options));
  184. }
  185. $offset++;
  186. }
  187. }
  188. throw new sfException("Unknown record property \"$name\" on \"".get_class($this).'"');
  189. }
  190. public function offsetGet($offset)
  191. {
  192. $args = func_get_args();
  193. return call_user_func_array(array($this, '__get'), $args);
  194. }
  195. public function __set($name, $value)
  196. {
  197. $args = func_get_args();
  198. $options = array();
  199. if (2 < count($args))
  200. {
  201. $options = $args[2];
  202. }
  203. $offset = 0;
  204. foreach ($this->tables as $table)
  205. {
  206. foreach ($table->getColumns() as $column)
  207. {
  208. if ($name == $column->getPhpName())
  209. {
  210. $this->values[$name] = $value;
  211. }
  212. if ("{$name}Id" == $column->getPhpName())
  213. {
  214. $relatedTable = $column->getTable()->getDatabaseMap()->getTable($column->getRelatedTableName());
  215. $this->values["{$name}Id"] = $value->__get($relatedTable->getColumn($column->getRelatedColumnName())->getPhpName(), $options);
  216. }
  217. $offset++;
  218. }
  219. }
  220. return $this;
  221. }
  222. public function offsetSet($offset, $value)
  223. {
  224. $args = func_get_args();
  225. return call_user_func_array(array($this, '__set'), $args);
  226. }
  227. public function __unset($name)
  228. {
  229. $offset = 0;
  230. foreach ($this->tables as $table)
  231. {
  232. foreach ($table->getColumns() as $column)
  233. {
  234. if ($name == $column->getPhpName())
  235. {
  236. $this->values[$name] = null;
  237. }
  238. if ("{$name}Id" == $column->getPhpName())
  239. {
  240. $this->values["{$name}Id"] = null;
  241. }
  242. $offset++;
  243. }
  244. }
  245. return $this;
  246. }
  247. public function offsetUnset($offset)
  248. {
  249. $args = func_get_args();
  250. return call_user_func_array(array($this, '__unset'), $args);
  251. }
  252. public function clear()
  253. {
  254. $this->row = $this->values = array();
  255. return $this;
  256. }
  257. protected
  258. $new = true;
  259. protected
  260. $deleted = false;
  261. public function save($connection = null)
  262. {
  263. if ($this->deleted)
  264. {
  265. throw new PropelException('You cannot save an object that has been deleted.');
  266. }
  267. if ($this->new)
  268. {
  269. $this->insert($connection);
  270. }
  271. else
  272. {
  273. $this->update($connection);
  274. }
  275. $offset = 0;
  276. foreach ($this->tables as $table)
  277. {
  278. foreach ($table->getColumns() as $column)
  279. {
  280. if (array_key_exists($column->getPhpName(), $this->values))
  281. {
  282. $this->row[$offset] = $this->values[$column->getPhpName()];
  283. }
  284. $offset++;
  285. }
  286. }
  287. $this->new = false;
  288. $this->values = array();
  289. return $this;
  290. }
  291. protected function param($column)
  292. {
  293. $value = $this->values[$column->getPhpName()];
  294. // Convert to DateTime or SQL zero special case
  295. if (isset($value) && $column->isTemporal() && !$value instanceof DateTime)
  296. {
  297. // Year only: one or more digits. Convert to SQL zero special case
  298. if (preg_match('/^\d+$/', $value))
  299. {
  300. $value .= '-0-0';
  301. }
  302. // Year and month only: one or more digits, plus separator, plus
  303. // one or more digits. Convert to SQL zero special case
  304. else if (preg_match('/^\d+[-\/]\d+$/', $value))
  305. {
  306. $value .= '-0';
  307. }
  308. // Convert to DateTime if not SQL zero special case: year plus
  309. // separator plus zero to twelve (possibly zero padded) plus
  310. // separator plus one or more zeros
  311. if (!preg_match('/^\d+[-\/]0*(?:1[0-2]|\d)[-\/]0+$/', $value))
  312. {
  313. try
  314. {
  315. $value = new DateTime($value);
  316. }
  317. catch (Exception $e)
  318. {
  319. return null;
  320. }
  321. }
  322. }
  323. return $value;
  324. }
  325. protected function insert($connection = null)
  326. {
  327. if (!isset($connection))
  328. {
  329. $connection = QubitTransactionFilter::getConnection(QubitFunctionI18n::DATABASE_NAME);
  330. }
  331. $offset = 0;
  332. foreach ($this->tables as $table)
  333. {
  334. $criteria = new Criteria;
  335. foreach ($table->getColumns() as $column)
  336. {
  337. if (!array_key_exists($column->getPhpName(), $this->values))
  338. {
  339. if ('createdAt' == $column->getPhpName() || 'updatedAt' == $column->getPhpName())
  340. {
  341. $this->values[$column->getPhpName()] = new DateTime;
  342. }
  343. if ('sourceCulture' == $column->getPhpName())
  344. {
  345. $this->values['sourceCulture'] = sfPropel::getDefaultCulture();
  346. }
  347. }
  348. if (array_key_exists($column->getPhpName(), $this->values))
  349. {
  350. if (null !== $param = $this->param($column))
  351. {
  352. $criteria->add($column->getFullyQualifiedName(), $param);
  353. }
  354. }
  355. $offset++;
  356. }
  357. if (null !== $id = BasePeer::doInsert($criteria, $connection))
  358. {
  359. // Guess that the first primary key of the first table is auto
  360. // incremented
  361. if ($this->tables[0] == $table)
  362. {
  363. $columns = $table->getPrimaryKeyColumns();
  364. $this->values[$columns[0]->getPhpName()] = $this->keys[$columns[0]->getPhpName()] = $id;
  365. }
  366. }
  367. }
  368. return $this;
  369. }
  370. protected function update($connection = null)
  371. {
  372. if (!isset($connection))
  373. {
  374. $connection = QubitTransactionFilter::getConnection(QubitFunctionI18n::DATABASE_NAME);
  375. }
  376. $offset = 0;
  377. foreach ($this->tables as $table)
  378. {
  379. $criteria = new Criteria;
  380. $selectCriteria = new Criteria;
  381. foreach ($table->getColumns() as $column)
  382. {
  383. if (!array_key_exists($column->getPhpName(), $this->values))
  384. {
  385. if ('updatedAt' == $column->getPhpName())
  386. {
  387. $this->values['updatedAt'] = new DateTime;
  388. }
  389. }
  390. if (array_key_exists($column->getPhpName(), $this->values))
  391. {
  392. if ('serialNumber' == $column->getPhpName())
  393. {
  394. $selectCriteria->add($column->getFullyQualifiedName(), $this->values[$column->getPhpName()]++);
  395. }
  396. $criteria->add($column->getFullyQualifiedName(), $this->param($column));
  397. }
  398. if ($column->isPrimaryKey())
  399. {
  400. $selectCriteria->add($column->getFullyQualifiedName(), $this->keys[$column->getPhpName()]);
  401. }
  402. $offset++;
  403. }
  404. if (0 < $criteria->size())
  405. {
  406. BasePeer::doUpdate($selectCriteria, $criteria, $connection);
  407. }
  408. }
  409. return $this;
  410. }
  411. public function delete($connection = null)
  412. {
  413. if ($this->deleted)
  414. {
  415. throw new PropelException('This object has already been deleted.');
  416. }
  417. $criteria = new Criteria;
  418. $criteria->add(QubitFunctionI18n::ID, $this->id);
  419. $criteria->add(QubitFunctionI18n::CULTURE, $this->culture);
  420. self::doDelete($criteria, $connection);
  421. $this->deleted = true;
  422. return $this;
  423. }
  424. /**
  425. * Returns the composite primary key for this object.
  426. * The array elements will be in same order as specified in XML.
  427. * @return array
  428. */
  429. public function getPrimaryKey()
  430. {
  431. $pks = array();
  432. $pks[0] = $this->getid();
  433. $pks[1] = $this->getculture();
  434. return $pks;
  435. }
  436. /**
  437. * Set the [composite] primary key.
  438. *
  439. * @param array $keys The elements of the composite key (order must match the order in XML file).
  440. * @return void
  441. */
  442. public function setPrimaryKey($keys)
  443. {
  444. $this->setid($keys[0]);
  445. $this->setculture($keys[1]);
  446. }
  447. public static function addJoinfunctionCriteria(Criteria $criteria)
  448. {
  449. $criteria->addJoin(QubitFunctionI18n::ID, QubitFunction::ID);
  450. return $criteria;
  451. }
  452. public function __call($name, $args)
  453. {
  454. if ('get' == substr($name, 0, 3) || 'set' == substr($name, 0, 3))
  455. {
  456. $args = array_merge(array(strtolower(substr($name, 3, 1)).substr($name, 4)), $args);
  457. return call_user_func_array(array($this, '__'.substr($name, 0, 3)), $args);
  458. }
  459. throw new sfException('Call to undefined method '.get_class($this)."::$name");
  460. }
  461. }