/**
  * Update translations
  *
  * @param mixed $id Id of text that need to be translated
  * @param mixed $data Data
  *
  * @return mixed|\Zend\Stdlib\ResponseInterface|JsonModel
  * @throws \Exception
  */
 public function update($id, $data)
 {
     if (!$this->rcmIsAllowed('translations', 'update')) {
         $response = $this->getResponse();
         $response->setStatusCode(Response::STATUS_CODE_401);
         $response->setContent($response->renderStatusLine());
         return $response;
     }
     $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
     $messageRepo = $em->getRepository('RcmI18n\\Entity\\Message');
     $locale = $this->params()->fromRoute('locale');
     /**
      * White-list local and default test to make sure nothing funny is
      * making its way to the DB. All default text's used must already exist
      * in the en_US locale
      */
     $usMessage = $messageRepo->findOneBy(['locale' => 'en_US', 'defaultText' => $data['defaultText']]);
     if (!$this->getServiceLocator()->get('RcmI18n\\Model\\Locales')->localeIsValid($locale)) {
         return $this->buildBadRequestResponse('invalid locale');
     } elseif (!$usMessage instanceof Message) {
         return $this->buildBadRequestResponse('invalid defaultText');
     }
     /**
      * Purify text to make sure nothing funny is making its way to the DB
      */
     $cleanText = $this->rcmHtmlPurify($data['text']);
     $message = $messageRepo->findOneBy(['locale' => $locale, 'messageId' => $id]);
     if ($message instanceof Message) {
         $message->setText($cleanText);
     } else {
         $message = new Message();
         $message->setLocale($locale);
         $message->setDefaultText($usMessage->getDefaultText());
         $message->setText($cleanText);
         $em->persist($message);
     }
     $em->flush();
     return new JsonModel($message);
 }
Beispiel #2
0
 /**
  * @covers RcmI18n\Entity\Message
  */
 public function testSetGetKey()
 {
     $value = 'Bob';
     $this->unit->setDefaultText($value);
     $this->assertEquals($value, $this->unit->getDefaultText());
 }
 /**
  * addMissingDefaultTranslation
  *
  * @param EventInterface $event
  *
  * @return void
  */
 public function addMissingDefaultTranslation($event)
 {
     $params = $event->getParams();
     $defaultLocale = $this->getDefaultLocale();
     // Ignore if not translate
     if ($params['text_domain'] === self::DO_NOT_TRANSLATE) {
         return;
     }
     // Only adding if we are the default locale
     if ($params['locale'] !== $defaultLocale) {
         return;
     }
     $em = $this->getEntityManager();
     try {
         $defaultMessage = $em->getRepository('RcmI18n\\Entity\\Message')->findOneBy(['locale' => $defaultLocale, 'defaultText' => $params['message']]);
     } catch (\Exception $e) {
         $defaultMessage = null;
     }
     if (empty($defaultMessage)) {
         $newMessage = new Message();
         $newMessage->setLocale($defaultLocale);
         $newMessage->setDefaultText($params['message']);
         $newMessage->setText($params['message']);
         $em->persist($newMessage);
         $em->flush($newMessage);
     }
 }