diff --git a/EMongoMessageSource.php b/EMongoMessageSource.php new file mode 100644 index 00000000..be06d0b4 --- /dev/null +++ b/EMongoMessageSource.php @@ -0,0 +1,121 @@ +translateMessageCollection it is equal + * to i18n by default. + */ +class EMongoMessageSource extends CMessageSource +{ + const CACHE_KEY_PREFIX = "ext.YiiMongoDbSuite.EMongoMessageSource"; + + /** + * @var string + * @desc The id os database connection + */ + public $connectionID = "mongodb"; + + /** + * @var string + * @desc Name of collection with messages and their translations + */ + public $translateMessageCollection = "i18n"; + + /** + * @var int + * @desc The time in seconds that the message can remain a valid cache + * default to 0, meaning that cache is disabled + */ + public $cachingDuration = 0; + + /** + * @var string + * @desc cache id. Simple. Put yours if you don't like this + */ + public $cacheID = 'cache'; + + /** + * @param string $category + * @param string $language + * @return array|null + */ + public function loadMessages($category, $language) + { + if ( + $this->cachingDuration > 0 && + $this->cacheID !== false && + ($cache = Yii::app()->getComponent($this->cacheID)) !== null + ) + { + $key = self::CACHE_KEY_PREFIX.'.messages.'.$category.'.'.$language; + $data = $cache->get($key); + + if ($data !== false) + return unserialize($data); + } + + $messages = $this->loadMessageFromDb($category, $language); + + if (isset($cache)) + $cache->set($key, serialize($messages), $this->cachingDuration); + + return $messages; + } + + /** + * @param $category + * @param $language + * @return array|null + * @desc Method requests translation form mongo and returns request + * result. Variable message contains array: + * $message['someMessage'] = 'Some message in needed language' + */ + protected function loadMessageFromDb($category, $language) + { + //Getting model + $i18n = $this->getMessageModel(); + //Getting translations + $translations = $i18n->getMessages($category, $language)->find(); + + //If we get null instead of object we might face problem trying to get messages + if ($translations instanceof EMongoI18nModel) + return $translations->messages; + + return null; + } + + /** + * @return EMongoI18nModel + * @throws CException + * @desc Checks component and returns model + */ + protected function getMessageModel() + { + $_db = Yii::app()->getComponent($this->connectionID); + + /** + * Checking wether we have correct db component + */ + if (!$_db instanceof EMongoDB) + { + throw new CException(Yii::t('app', 'EMongoMessageSource.connectionId is invalid. Please make sure "{id}" refers to a valid database application component.'), + array("{id}" => $this->connectionID)); + } + + return EMongoI18nModel::model(); + } +} diff --git a/extra/EMongoI18nModel.php b/extra/EMongoI18nModel.php new file mode 100644 index 00000000..9229f482 --- /dev/null +++ b/extra/EMongoI18nModel.php @@ -0,0 +1,69 @@ +getComponent("messages"); + + if (!$messageComponent instanceof EMongoMessageSource) + throw new EMongoException(Yii::t('app', 'Component message is not the instance of EMongoMessageSource.')); + + return $messageComponent->translateMessageCollection; + } + + /** + * @param $category + * @param $language + * @return EMongoI18nModel + * @desc Method adds category and language criterias to the query. + */ + public function getMessages($category, $language) + { + $this->getDbCriteria()->category = $category; + $this->getDbCriteria()->language = $language; + + return $this; + } +}