/lib/model/doctrine/SnsTermTable.class.php

https://github.com/ShinichiU/AuthFramework · PHP · 138 lines · 105 code · 26 blank · 7 comment · 13 complexity · 65b79f8db96be75b38407b3a2c3bdf59 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the OpenPNE package.
  4. * (c) OpenPNE Project (http://www.openpne.jp/)
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file and the NOTICE file that were distributed with this source code.
  8. */
  9. class SnsTermTable extends Doctrine_Table implements ArrayAccess
  10. {
  11. protected
  12. $culture = '',
  13. $application = '',
  14. $terms = null;
  15. public function configure($culture = '', $application = '')
  16. {
  17. if ($culture)
  18. {
  19. $this->culture = $culture;
  20. }
  21. if ($application)
  22. {
  23. $this->application = $application;
  24. }
  25. }
  26. public function retrieveByName($name)
  27. {
  28. $terms = $this->getTerms();
  29. $fronting = false;
  30. if (preg_match('/[A-Z]/', $name[0]))
  31. {
  32. $fronting = true;
  33. $name = strtolower($name[0]).substr($name, 1);
  34. }
  35. $result = (isset($terms[$name])) ? $terms[$name] : null;
  36. if ($result && $fronting)
  37. {
  38. $result->fronting();
  39. }
  40. return $result;
  41. }
  42. public function get($name)
  43. {
  44. return (!is_null($term = $this->retrieveByName($name))) ? $term : null;
  45. }
  46. public function set($name, $value, $culture = '', $application = '')
  47. {
  48. if (!$culture)
  49. {
  50. $culture = $this->culture;
  51. }
  52. if (!$application)
  53. {
  54. $application = $this->application;
  55. }
  56. if (!$culture || !$application)
  57. {
  58. return false;
  59. }
  60. $term = $this->createQuery()
  61. ->andWhere('name = ?', $name)
  62. ->andWhere('application = ?', $application)
  63. ->andWhere('id IN (SELECT id FROM SnsTermTranslation WHERE lang = ?)', $culture)
  64. ->fetchOne();
  65. if (!$term)
  66. {
  67. $term = new SnsTerm();
  68. $term->setName($name);
  69. $term->setLang($culture);
  70. $term->setApplication($application);
  71. }
  72. $term->setValue($value);
  73. return $term->save();
  74. }
  75. protected function getTerms()
  76. {
  77. if (is_null($this->terms))
  78. {
  79. $this->terms = array();
  80. $q = $this->createQuery();
  81. if ($this->application)
  82. {
  83. $q->andWhere('application = ?', $this->application);
  84. }
  85. if ($this->culture)
  86. {
  87. $q->leftJoinTranslation('SnsTerm', $this->culture);
  88. }
  89. foreach ($q->execute() as $term)
  90. {
  91. $this->terms[$term->name] = $term;
  92. }
  93. }
  94. return $this->terms;
  95. }
  96. public function offsetExists($offset)
  97. {
  98. return !is_null($this->get($offset));
  99. }
  100. public function offsetGet($offset)
  101. {
  102. return $this->get($offset);
  103. }
  104. public function offsetSet($offset, $value)
  105. {
  106. throw new LogicException('The SnsTermTable class is not writable.');
  107. }
  108. public function offsetUnset($offset)
  109. {
  110. throw new LogicException('The SnsTermTable class is not writable.');
  111. }
  112. }