Example #1
0
 /**
  * @Route("/upload-screenshot/{translationId}", name="upload_screenshot")
  * @Method("POST")
  * @ ParamConverter("project", class="TranslationsBundle:Project", options={"id" = "projectId"})
  */
 public function uploadScrenshotAction(Request $request, $translationId)
 {
     $this->init();
     /** @var Translation $translation */
     $translation = $this->getTranslationRepository()->find($translationId);
     if (!$translation) {
         throw $this->createNotFoundException();
     }
     $projectId = $translation->getProjectId();
     $project = $this->getProjectRepository()->find($projectId);
     $permissions = $this->translationsManager->getPermissionForUserAndProject($this->user, $project);
     // if permissions bla bla bla
     $directory = $this->root . "/web/uploads/{$projectId}/";
     $files = $request->files;
     foreach ($files as $uploadedFile) {
         break;
         // I want only the first file, dont allow to upload more than one
     }
     /** @var UploadedFile $uploadedFile */
     $ext = $uploadedFile->getClientOriginalExtension();
     $baseName = uniqid();
     $name = $baseName . '.' . $ext;
     /** @var \Symfony\Component\HttpFoundation\File\File $file */
     $file = $uploadedFile->move($directory, $name);
     $destFile = $baseName . '.jpg';
     $name = $this->normalize($ext, $directory . $baseName, $directory . $destFile);
     $translation->setScreenshot($destFile);
     $this->dm->persist($translation);
     $this->dm->flush($translation);
     die('OK');
 }
Example #2
0
 /**
  * @Route("/change-user-permissions/{projectId}", name="change_user_permission")
  * @ParamConverter("project", class="TranslationsBundle:Project", options={"id" = "projectId"})
  */
 public function changeUserPermissionsAction(Project $project, Request $request)
 {
     $session = $request->getSession();
     $this->init();
     /** @var Permission $permission */
     $permission = $this->translationsManager->getPermissionForUserAndProject($this->user, $project);
     if (!$permission || $permission->getPermissions(Permission::GENERAL_KEY) != Permission::OWNER) {
         return $this->exception('error.acl.not_enough_permissions_to_manage_this_project');
     }
     $managedLocales = explode(',', $project->getManagedLocales());
     $userId = $request->get('user');
     $locale = $request->get('locale');
     if (!in_array($locale, $managedLocales)) {
         return $this->exception(sprintf('the locale %s is not managed by this project', $locale));
     }
     $newPermission = $request->get('permission');
     if (!in_array($newPermission, Permission::getAllowedLocalePermissions())) {
         return $this->exception(sprintf('the permission %s is not recognized', $newPermission));
     }
     $user = $this->getUserRepository()->find($userId);
     if (!$user) {
         return $this->exception('error.permissions.no_user_found');
     }
     /** @var Permission $permission */
     $permission = $this->getPermissionRepository()->findPermissionForProjectAndUser($project, $user);
     if (!$permission) {
         return $this->exception('error.permissions.no_permissions_for_this_user');
     }
     $permission->addPermission($newPermission, $locale);
     $this->em->persist($permission);
     $this->em->flush($permission);
     return $this->resultOk(array('locale' => $locale, 'user' => $user->getId(), 'permission' => $newPermission));
 }
Example #3
0
 protected function blockSync(Project $project, $catalog, $language, $bundle, $data)
 {
     if (!$bundle || !$data || !$language || !$bundle || !$catalog) {
         return $this->exception('Validation exceptions, missing parameters');
     }
     $result = array();
     /** @var Message[] $localMessages */
     $localMessages = $this->translationsManager->getMessagesForBundleCatalogAndLocale($project, $bundle, $catalog, $language);
     foreach ($localMessages as $message) {
         $key = $message->getKey();
         $remoteMessage = isset($data[$key]) ? $data[$key] : null;
         if ($remoteMessage) {
             $remoteDate = new \DateTime($remoteMessage['updatedAt']);
             if ($message->getUpdatedAt() < $remoteDate) {
                 $message->setMessage($remoteMessage['message']);
                 $message->setUpdatedAt($remoteDate);
                 $this->em->persist($message);
             }
         }
         if ($message->getApproved()) {
             $result[] = $message->asArray();
         }
     }
     $this->em->flush();
     $this->translationsManager->regenerateProjectInfo($project->getId());
     return $this->resultOk($result);
 }
Example #4
0
 /**
  *
  * $data[key][locale]
  * {
  *   message,
  *   updatedAt,
  *   bundle,
  *   fileName,
  * }
  *
  */
 protected function receiveKeys(Project $project, $catalog, $data)
 {
     if (!$project || !$catalog || !$data) {
         return $this->exception('Validation exceptions, missing parameters');
     }
     $result = array();
     /** @var Translation[] $messages */
     $messages = $this->getTranslationRepository()->findBy(array('projectId' => $project->getId(), 'catalog' => $catalog));
     if ($this->debug) {
         echo sprintf("found %d in translations\n", count($messages));
     }
     foreach ($messages as $message) {
         $key = $message->getKey();
         $bundle = '';
         $translations = $message->getTranslations();
         $dirty = false;
         if (count($translations)) {
             foreach ($translations as $locale => $translation) {
                 if (isset($data[$key][$locale])) {
                     $current = $data[$key][$locale];
                     $updatedAt = new \DateTime($current['updatedAt']);
                     if ($message->getUpdatedAt()->sec < intval($updatedAt->format("U"))) {
                         $result[$key][$locale] = $current['updatedAt'];
                         $translations[$locale]['message'] = $current['message'];
                         $translations[$locale]['updatedAt'] = $updatedAt;
                         if (isset($current['approved'])) {
                             $translations[$locale]['approved'] = $current['approved'];
                         }
                         $dirty = true;
                     }
                     $translations[$locale]['fileName'] = $current['fileName'];
                     if (!$bundle) {
                         if ($current['bundle']) {
                             $bundle = $current['bundle'];
                         } else {
                             preg_match('/\\/(?<bundle>\\w+Bundle)\\//i', $current['fileName'], $matches);
                             if (isset($matches['bundle'])) {
                                 $bundle = $matches['bundle'];
                             }
                         }
                     }
                     unset($data[$key][$locale]);
                 }
             }
             if ($dirty) {
                 $message->setBundle($bundle);
                 $message->setTranslations($translations);
                 $this->dm->persist($message);
             }
         }
     }
     if ($this->debug) {
         echo sprintf("found %d keys in data\n", count($data));
     }
     foreach ($data as $key => $dataLocale) {
         if (count($dataLocale)) {
             if ($this->debug) {
                 echo sprintf("processing key %s\n", $key);
             }
             $bundle = '';
             $translation = new Translation();
             $translation->setCatalog($catalog);
             $translation->setKey($key);
             $translation->setProjectId($project->getId());
             $translation->setBundle($bundle);
             $translations = array();
             foreach ($dataLocale as $locale => $message) {
                 $translations[$locale] = array('message' => $message['message'], 'updatedAt' => new \DateTime($message['updatedAt']), 'approved' => true, 'fileName' => $message['fileName']);
                 if (!$bundle) {
                     if ($message['bundle']) {
                         $bundle = $message['bundle'];
                     } else {
                         preg_match('/\\/(?<bundle>\\w+Bundle)\\//i', $current['fileName'], $matches);
                         if (isset($matches['bundle'])) {
                             $bundle = $matches['bundle'];
                         }
                     }
                 }
             }
             $translation->setBundle($bundle);
             $translation->setTranslations($translations);
             $this->dm->persist($translation);
         }
     }
     $this->dm->flush();
     // normalize
     $this->translationsManager->regenerateProjectInfo($project->getId());
     return $this->resultOk($result);
 }