PageRenderTime 24ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/reaktor/modules/sfTransUnit/actions/actions.class.php

https://github.com/digibib/reaktorcms
PHP | 220 lines | 159 code | 30 blank | 31 comment | 18 complexity | ab52885971949b2ea7a7898deed81477 MD5 | raw file
  1. <?php
  2. /**
  3. * trans_unit actions.
  4. *
  5. * @package symfony
  6. * @subpackage plugin
  7. * @author Gareth James <symfony@bemused.org>
  8. * @version SVN: $Id$
  9. */
  10. require_once(dirname(__FILE__).'/../lib/BasesfTransUnitActions.class.php');
  11. class sfTransUnitActions extends BasesfTransUnitActions
  12. {
  13. public function executeEdit()
  14. {
  15. $this->trans_unit = $this->getTransUnitOrCreate();
  16. if ($this->getRequest()->getMethod() == sfRequest::POST)
  17. {
  18. $catalogues = CataloguePeer::getCatalogues();
  19. foreach ($catalogues as $catalogue) {
  20. $trans_unit_string = 'trans_unit_' . $catalogue->getCatId();
  21. $c = new Criteria();
  22. if($this->trans_unit->getSource())
  23. {
  24. // Ticket 23733 - in some cases LIKE operator matches to much
  25. // $c->add(TransUnitPeer::SOURCE, $this->trans_unit->getSource(), ' LIKE BINARY ');
  26. $c->add(TransUnitPeer::SOURCE, $this->trans_unit->getSource(), ' = BINARY ');
  27. }
  28. else
  29. {
  30. $c->add(TransUnitPeer::SOURCE, $this->getRequestParameter("${trans_unit_string}[target]"),Criteria::EQUAL);
  31. }
  32. $c->add(TransUnitPeer::CAT_ID, $catalogue->getCatId());
  33. $trans_unit_cat = TransUnitPeer::doSelectOne($c);
  34. if ($trans_unit_cat) {
  35. $this->$trans_unit_string = $this->getTransUnitByMsgIdOrCreate($trans_unit_cat->getMsgId());
  36. } else {
  37. // If translation is missing create the object
  38. $this->$trans_unit_string = $this->getTransUnitByMsgIdOrCreate();
  39. $this->$trans_unit_string->setSource($this->trans_unit->getSource());
  40. $this->$trans_unit_string->setFilename($this->trans_unit->getFilename());
  41. $this->$trans_unit_string->setModule($this->trans_unit->getModule());
  42. $this->$trans_unit_string->setId($this->trans_unit->getId());
  43. }
  44. }
  45. foreach ($catalogues as $catalogue) {
  46. $this->updateTransUnitCatIdFromRequest($catalogue->getCatId());
  47. $trans_unit_string = 'trans_unit_' . $catalogue->getCatId();
  48. $this->$trans_unit_string->setCatId($catalogue->getCatId());
  49. $this->saveTransUnit($this->$trans_unit_string);
  50. }
  51. $this->setFlash('notice', 'Your modifications have been saved');
  52. if ($this->getRequestParameter('save_and_add'))
  53. {
  54. return $this->redirect('sfTransUnit/create');
  55. }
  56. else if ($this->getRequestParameter('save_and_list'))
  57. {
  58. return $this->redirect('sfTransUnit/list');
  59. }
  60. else
  61. {
  62. if ($this->trans_unit->getMsgId())
  63. {
  64. return $this->redirect('sfTransUnit/edit?msg_id='.$this->trans_unit->getMsgId());
  65. }
  66. else
  67. {
  68. return $this->redirect('sfTransUnit/list');
  69. }
  70. }
  71. }
  72. else
  73. {
  74. $this->labels = $this->getLabels();
  75. }
  76. }
  77. function hex2bin($h)
  78. {
  79. if (!is_string($h)) return null;
  80. $r='';
  81. for ($a=0; $a<strlen($h); $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); }
  82. return $r;
  83. }
  84. public function executeSetCulture()
  85. {
  86. $redirect = $this->getRequestParameter('ref');
  87. $redirect = $this->hex2bin($redirect);
  88. $newCulture = $this->getRequestParameter("lang");
  89. $cult = strpos($redirect, "sf_culture=");
  90. if ($cult !== false) {
  91. // 10 = sf_culture, +1 = =, 2 == no/en/
  92. $redirect = substr_replace($redirect, $newCulture, $cult+10+1, 2);
  93. }
  94. else
  95. {
  96. if (strpos($redirect, "?") !== false) {
  97. $redirect .= "&";
  98. }
  99. else
  100. {
  101. $redirect .= "?";
  102. }
  103. $redirect .= "sf_culture=" . $newCulture;
  104. }
  105. if ($this->getUser()->isAuthenticated())
  106. {
  107. $this->getUser()->getGuardUser()->setCulture($newCulture);
  108. $this->getUser()->getGuardUser()->save();
  109. }
  110. else
  111. {
  112. sfContext::getInstance()->getResponse()->setCookie(sfConfig::get('app_sf_guard_plugin_lang_cookie_name', 'lang'),
  113. $newCulture, time()+60*60*24*10);
  114. }
  115. $this->redirect($redirect);
  116. }
  117. public function executeNextString()
  118. {
  119. $msg_id = $this->getRequestParameter('msg_id') + 1;
  120. $this->redirect('@trans_edit?msg_id='.$msg_id);
  121. }
  122. public function executePreviousString()
  123. {
  124. $msg_id = $this->getRequestParameter('msg_id') - 1;
  125. if ($msg_id <= 0) $msg_id = 1;
  126. $this->redirect('@trans_edit?msg_id='.$msg_id);
  127. }
  128. /**
  129. * Process the new translation form - if we are here then the validation must have passed
  130. *
  131. * @return void - the user is redirected
  132. */
  133. public function executeNewTranslation()
  134. {
  135. $languages = $this->languages = CataloguePeer::getCatalogues(true);
  136. $translateObject = $this->getrequestParameter("translateObject");
  137. $translateField = $this->getrequestParameter("translateField");
  138. if (!class_exists($translateObject))
  139. {
  140. throw new exception ("Need a class to translate");
  141. }
  142. else
  143. {
  144. $trans = new $translateObject;
  145. }
  146. $trans->setBasename($this->getrequestParameter("basename"));
  147. $trans->save();
  148. foreach ($languages as $key => $language)
  149. {
  150. $trans->setCulture($key);
  151. $trans->{"set".$translateField}($this->getrequestParameter($key));
  152. $trans->save();
  153. }
  154. // Send the user back to the page they want to go to
  155. $this->redirect($this->getrequestParameter("redirect"));
  156. }
  157. /**
  158. * Validator for adding new translation
  159. * Here and not in the yaml file because number of fields can change
  160. * Basename is validated in yaml - the languages here
  161. *
  162. * @return void
  163. */
  164. public function validateNewTranslation()
  165. {
  166. $languages = $this->languages = CataloguePeer::getCatalogues(true);
  167. foreach ($languages as $key => $language)
  168. {
  169. if (!$this->getrequestParameter($key))
  170. {
  171. $this->getRequest()->setError($key, $this->getContext()->getI18n()->__("Required"));
  172. }
  173. }
  174. if ($this->getRequest()->hasErrors())
  175. {
  176. return false;
  177. }
  178. return true;
  179. }
  180. /**
  181. * Handle errors for new translations if there are any
  182. *
  183. * @return void
  184. */
  185. public function handleErrorNewTranslation()
  186. {
  187. // Send the response back to the calling module, with errors intact
  188. $this->forward($this->getrequestParameter("referingModule"), $this->getrequestParameter("referingAction"));
  189. }
  190. }