diff --git a/Document/Entry.php b/Document/Entry.php new file mode 100755 index 0000000..50ff1dc --- /dev/null +++ b/Document/Entry.php @@ -0,0 +1,15 @@ + + */ +class Entry extends AbstractEntry +{ + +} \ No newline at end of file diff --git a/Document/Locale.php b/Document/Locale.php new file mode 100755 index 0000000..bc28fe7 --- /dev/null +++ b/Document/Locale.php @@ -0,0 +1,15 @@ + + */ +class Locale extends AbstractLocale +{ + +} \ No newline at end of file diff --git a/Document/Translation.php b/Document/Translation.php new file mode 100755 index 0000000..eb2b605 --- /dev/null +++ b/Document/Translation.php @@ -0,0 +1,15 @@ + + */ +class Translation extends AbstractTranslation +{ + +} \ No newline at end of file diff --git a/README.md b/README.md index 69ba7c5..4fc9d81 100755 --- a/README.md +++ b/README.md @@ -61,13 +61,20 @@ We recommend that you only enable this bundle for the development environments, The collection parameter allows you to define the collection that will contain the translations for the project, so you can have multiple Symfony2 projects in the same mongodb server. -A sample configuration (in your config_dev.yml): +A sample Doctrine ORM configuration (in your config_dev.yml): server_grove_translation_editor: storage: type: server_grove_translation_editor.storage.orm manager: doctrine.orm.entity_manager +Doctrine MongoDB configuration (in your config_dev.yml): + + server_grove_translation_editor: + storage: + type: server_grove_translation_editor.storage.mongodb + manager: doctrine_mongodb.odm.document_manager + Add the routing configuration to app/config/routing_dev.yml SGTranslationEditorBundle: diff --git a/Resources/config/doctrine/Entry.mongodb.xml b/Resources/config/doctrine/Entry.mongodb.xml new file mode 100755 index 0000000..f74a62a --- /dev/null +++ b/Resources/config/doctrine/Entry.mongodb.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Resources/config/doctrine/Locale.mongodb.xml b/Resources/config/doctrine/Locale.mongodb.xml new file mode 100755 index 0000000..ebfd358 --- /dev/null +++ b/Resources/config/doctrine/Locale.mongodb.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Resources/config/doctrine/Translation.mongodb.xml b/Resources/config/doctrine/Translation.mongodb.xml new file mode 100755 index 0000000..95a88e2 --- /dev/null +++ b/Resources/config/doctrine/Translation.mongodb.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 08bac08..dbbe8c3 100755 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -14,6 +14,10 @@ + + + + %server_grove_translation_editor.storage.manager% diff --git a/Storage/MongoDBStorage.php b/Storage/MongoDBStorage.php new file mode 100755 index 0000000..26c145a --- /dev/null +++ b/Storage/MongoDBStorage.php @@ -0,0 +1,95 @@ + + */ +class MongoDBStorage extends AbstractStorage implements StorageInterface +{ + const CLASS_LOCALE = 'ServerGrove\Bundle\TranslationEditorBundle\Document\Locale'; + const CLASS_ENTRY = 'ServerGrove\Bundle\TranslationEditorBundle\Document\Entry'; + const CLASS_TRANSLATION = 'ServerGrove\Bundle\TranslationEditorBundle\Document\Translation'; + + /** + * {@inheritdoc} + */ + protected function getLocaleClassName() + { + return self::CLASS_LOCALE; + } + + /** + * {@inheritdoc} + */ + protected function getEntryClassName() + { + return self::CLASS_ENTRY; + } + + /** + * {@inheritdoc} + */ + protected function getTranslationClassName() + { + return self::CLASS_TRANSLATION; + } + + /** + * {@inheritdoc} + */ + public function findLocaleList(array $criteria = array()) + { + $builder = $this->manager->createQueryBuilder($this->getLocaleClassName()); + + $this->hydrateCriteria($builder, $criteria); + + return iterator_to_array($builder->getQuery()->execute()); + } + + /** + * {@inheritdoc} + */ + public function findEntryList(array $criteria = array()) + { + $builder = $this->manager->createQueryBuilder($this->getEntryClassName()); + + $this->hydrateCriteria($builder, $criteria); + + return iterator_to_array($builder->getQuery()->execute()); + } + + /** + * {@inheritdoc} + */ + public function findTranslationList(array $criteria = array()) + { + $builder = $this->manager->createQueryBuilder($this->getTranslationClassName()); + + if(isset($criteria['locale']) && $criteria['locale'] instanceof \ServerGrove\Bundle\TranslationEditorBundle\Document\Locale) { + $criteria['locale'] = $criteria['locale']->getId(); + } + if(isset($criteria['entry']) && $criteria['entry'] instanceof \ServerGrove\Bundle\TranslationEditorBundle\Document\Entry) { + $criteria['entry'] = $criteria['entry']->getId(); + } + + $this->hydrateCriteria($builder, $criteria); + + return iterator_to_array($builder->getQuery()->execute()); + } + + /** + * Populate a criteria builder + * + * @param \Doctrine\ODM\MongoDB\Query\Builder $builder + * @param array $criteria + */ + protected function hydrateCriteria($builder, array $criteria = array()) + { + foreach ($criteria as $fieldName => $fieldValue) { + $builder->addOr($builder->expr()->field($fieldName)->equals($fieldValue)); + } + } +}