Skip to content

Commit

Permalink
Refactored frontend. Most actions are working (except the new transla…
Browse files Browse the repository at this point in the history
…tion).
  • Loading branch information
guilhermeblanco committed Feb 8, 2012
1 parent f331db8 commit af5f151
Show file tree
Hide file tree
Showing 16 changed files with 392 additions and 265 deletions.
Empty file modified CHANGELOG.md
100644 → 100755
Empty file.
Empty file modified Command/Base.php
100644 → 100755
Empty file.
Empty file modified Command/ExportCommand.php
100644 → 100755
Empty file.
133 changes: 54 additions & 79 deletions Controller/EditorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace ServerGrove\Bundle\TranslationEditorBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller,
Symfony\Component\HttpFoundation\Response;

class EditorController extends Controller
{
Expand All @@ -16,71 +17,41 @@ public function listAction()
$storageService = $this->container->get('server_grove_translation_editor.storage');
$defaultLocale = $this->container->getParameter('locale', 'en');

$locales = $storageService->getLocaleList();
$data = $storageService->getEntryList();

echo '<pre>';
\Doctrine\Common\Util\Debug::dump($locales);
echo '</pre>';
die;

$locales = array();
$missing = array();

foreach ($data as $d) {
if ( ! isset($locales[$d['locale']])) {
$locales[$d['locale']] = array(
'entries' => array(),
'data' => array()
);
$localeList = $storageService->findLocaleList();
$defaultLocale = array_filter(
$localeList,
function ($locale) use ($defaultLocale) {
return $locale->equalsTo($defaultLocale);
}
);
$defaultLocale = reset($defaultLocale);

if (is_array($d['entries'])) {
$locales[$d['locale']]['entries'] = array_merge($locales[$d['locale']]['entries'], $d['entries']);
$locales[$d['locale']]['data'][$d['filename']] = $d;
}
}

$keys = array_keys($locales);

foreach ($keys as $locale) {
if ($locale != $default) {
foreach ($locales[$default]['entries'] as $key => $val) {
if (!isset($locales[$locale]['entries'][$key]) || $locales[$locale]['entries'][$key] == $key) {
$missing[$key] = 1;
}
}
}
}
$entryList = $storageService->findEntryList();

return $this->render('ServerGroveTranslationEditorBundle:Editor:list.html.twig', array(
'locales' => $locales,
'default' => $defaultLocale,
'missing' => $missing,
return $this->render(
'ServerGroveTranslationEditorBundle:Editor:list.html.twig',
array(
'localeList' => $localeList,
'entryList' => $entryList,
'defaultLocale' => $defaultLocale,
)
);
}

public function removeAction()
{
$request = $this->getRequest();
$storageService = $this->container->get('server_grove_translation_editor.storage');
$request = $this->getRequest();

if ($request->isXmlHttpRequest()) {
$key = $request->request->get('key');

$values = $this->getCollection()->find();

foreach($values as $data) {
if (isset($data['entries'][$key])) {
unset($data['entries'][$key]);
$this->updateData($data);
}
}

$res = array(
'result' => true,
$id = $request->request->get('id');
$result = array(
'result' => $storageService->deleteEntry($id)
);
return new \Symfony\Component\HttpFoundation\Response(json_encode($res));

return new Response(json_encode($result), 200, array(
'Content-type' => 'application/json'
));
}
}

Expand All @@ -104,7 +75,7 @@ public function addAction()
'result' => false,
'msg' => 'The key already exists. Please update it instead.',
);
return new \Symfony\Component\HttpFoundation\Response(json_encode($res));
return new Response(json_encode($res));
}
}

Expand All @@ -116,48 +87,52 @@ public function addAction()
$this->updateData($data);
}
}

if ($request->isXmlHttpRequest()) {
$res = array(
'result' => true,
);
return new \Symfony\Component\HttpFoundation\Response(json_encode($res));

return new Response(json_encode($res));
}

return new \Symfony\Component\HttpFoundation\RedirectResponse($this->generateUrl('sg_localeditor_list'));
}

public function updateAction()
{
$request = $this->getRequest();
$storageService = $this->container->get('server_grove_translation_editor.storage');
$request = $this->getRequest();

if ($request->isXmlHttpRequest()) {
$locale = $request->request->get('locale');
$key = $request->request->get('key');
$val = $request->request->get('val');
$value = $request->request->get('value');

$values = $this->getCollection()->find(array('locale' => $locale));
$values = iterator_to_array($values);
$localeList = $storageService->findLocaleList(array('id' => $request->request->get('localeId')));
$locale = reset($localeList);

$found = false;
foreach ($values as $data) {
if (isset($data['entries'][$key])) {
$found = true;
break;
}
}
if (!$found) {
$data = array_pop($values);
}
$entryList = $storageService->findEntryList(array('id' => $request->request->get('entryId')));
$entry = reset($entryList);

$data['entries'][$key] = $val;
$this->updateData($data);
$translationList = $storageService->findTranslationList(array('locale' => $locale, 'entry' => $entry));
$translation = reset($translationList);

$res = array(
'result' => true,
'oldata' => $data['entries'][$key],
try {
if ($translation) {
$translation->setValue($value);

);
return new \Symfony\Component\HttpFoundation\Response(json_encode($res));
$storageService->persist($translation);
} else {
$storageService->createTranslation($locale, $entry, $value);
}

$result = array('result' => true);
} catch (\Exception $e) {
$result = array('result' => false);
}

return new Response(json_encode($result), 200, array(
'Content-type' => 'application/json'
));
}
}

Expand Down
Empty file modified LICENSE
100644 → 100755
Empty file.
18 changes: 18 additions & 0 deletions Model/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,22 @@ public function getTranslations()
{
return $this->translations;
}

/**
* Retrieve a Translation of a given Locale
*
* @param Locale $locale
*
* @return Translation
*/
public function getTranslation($locale)
{
foreach ($this->getTranslations() as $translation) {
if ($translation->getLocale() === $locale) {
return $translation;
}
}

return null;
}
}
53 changes: 53 additions & 0 deletions Model/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,57 @@ public function getTranslations()
{
return $this->translations;
}

/**
* Retrieve a Translation of a given Entry
*
* @param Locale $locale
*
* @return Translation
*/
public function getTranslation($entry)
{
foreach ($this->getTranslations() as $translation) {
if ($translation->getEntry() === $entry) {
return $translation;
}
}

return null;
}

/**
* Check if a given locale equals to this Locale instance.
* Accepts either a string or a Locale instance for comparison.
*
* @param mixed $locale
*
* @return boolean
*/
public function equalsTo($locale)
{
if ($locale instanceof self) {
return ($this === $locale);
}

$locale = str_replace('-', '_', $locale);

return ((string) $this === $locale);
}

/**
* Convert Locale to string
*
* @return string
*/
public function __toString()
{
$locale = $this->getLanguage();

if (($country = $this->getCountry()) !== null) {
$locale .= '_' . $country;
}

return $locale;
}
}
Empty file modified MongoStorageManager.php
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
6 changes: 5 additions & 1 deletion Resources/config/doctrine/Entry.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
<one-to-many target-entity="ServerGrove\Bundle\TranslationEditorBundle\Entity\Translation"
field="translations"
mapped-by="entry"
fetch="EXTRA_LAZY"/>
fetch="EXTRA_LAZY">
<cascade>
<cascade-all/>
</cascade>
</one-to-many>

</entity>

Expand Down
6 changes: 5 additions & 1 deletion Resources/config/doctrine/Locale.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
<one-to-many target-entity="ServerGrove\Bundle\TranslationEditorBundle\Entity\Translation"
field="translations"
mapped-by="locale"
fetch="EXTRA_LAZY"/>
fetch="EXTRA_LAZY">
<cascade>
<cascade-all/>
</cascade>
</one-to-many>

</entity>

Expand Down
Loading

0 comments on commit af5f151

Please sign in to comment.