Skip to content

Commit

Permalink
Create I18nContent component class, Pass some configuration params th…
Browse files Browse the repository at this point in the history
…rough it

Now you can include `i18ncontent` module in backend, but include it in component
  • Loading branch information
thecodeholic committed Mar 24, 2018
1 parent c3aaa8a commit 7c370b6
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 50 deletions.
53 changes: 53 additions & 0 deletions I18nContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* User: zura
* Date: 3/24/18
* Time: 3:28 PM
*/
namespace centigen\i18ncontent;

use yii\base\Component;
use yii\helpers\Url;

/**
* Class I18nContent
*
* @author Zura Sekhniashvili <[email protected]>
* @package centigen\i18ncontent
*/
class I18nContent extends Component
{

/**
* Identity User class name
*
* @var string
*/
public $userClass = null;

/**
* In texts which may contain <img> or other media object tags (texts which come from WYSIWYG editors)
* `$mediaUrlPrefix` strings are replaced with `$mediaUrlReplacement` string when calling `Html::encodeMediaItemUrls`
* and vice versa when calling `Html::decodeMediaItemUrls`
*
* @author Zura Sekhniashvili <[email protected]>
* @var string
*/
public $mediaUrlPrefix = null;

/**
* See `$mediaUrlPrefix`
*
* @author Zura Sekhniashvili <[email protected]>
* @var string
*/
public $mediaUrlReplacement = '{{media_item_url_prefix}}';

public function init()
{
parent::init();
if (!$this->mediaUrlPrefix){
$this->mediaUrlPrefix = Url::base(true);
}
}
}
32 changes: 0 additions & 32 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
*/
class Module extends \yii\base\Module
{
/**
* Identity User class name
*
* @var string
*/
public $userClass = null;

/**
* Default layout which will be used in all actions
Expand All @@ -24,32 +18,6 @@ class Module extends \yii\base\Module
*/
public $defaultLayout = null;

/**
* In texts which may contain <img> or other media object tags (texts which come from WYSIWYG editors)
* `$mediaUrlPrefix` strings are replaced with `$mediaUrlReplacement` string when calling `Html::encodeMediaItemUrls`
* and vice versa when calling `Html::decodeMediaItemUrls`
*
* @author Zura Sekhniashvili <[email protected]>
* @var string
*/
public $mediaUrlPrefix = null;

/**
* See `$mediaUrlPrefix`
*
* @author Zura Sekhniashvili <[email protected]>
* @var string
*/
public $mediaUrlReplacement = '{{media_item_url_prefix}}';

public function __construct($id, $parent = null, $config = [])
{
parent::__construct($id, $parent, $config);
if (!$this->mediaUrlPrefix){
$this->mediaUrlPrefix = Url::base(true);
}
}

public function missingTranslation()
{
// @todo
Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
yii2-i18ncontent
i18n content module for Yii2 framework
================

yii2-i18ncontent is yii2 module for creating several types of contents in different languages.
Expand All @@ -12,7 +12,7 @@ It support creating the following contents:

### Installation

1. Run `composer require omcrn/yii2-i18ncontent` or add `"omcrn/yii2-i18ncontent": "^1.0.0"` in your projects `composer.json`.
1. Run `composer require omcrn/yii2-i18ncontent` or add `"omcrn/yii2-i18ncontent": "~2.0.0"` in your projects `composer.json`.
2. Make sure you have `user` table in your database with primary key `id`.
3. Run migrations to create tables by `php console/yii migrate --migrationPath=@yii/i18n/migrations` from projects root directory
4. Run migrations to create tables by `php console/yii migrate --migrationPath=@vendor/omcrn/yii2-i18ncontent/migrations` from projects root directory
Expand All @@ -24,13 +24,9 @@ Add the following code in projects configuration file under `modules` section
```php
'i18ncontent' => [
'class' => 'centigen\i18ncontent\Module',
'userClass' => 'common\models\User', //User model class. If you do not have user model, generate it from user table. Make sure this models extends \yii\db\ActiveRecord class
'defaultLayout' => '/admin', //Default layout which will be used for rendering i18ncontent pages
'mediaUrlPrefix' => null, //In texts which may contain <img> or other media object tags (texts which come from WYSIWYG editors)
// `$mediaUrlPrefix` strings are replaced with `$mediaUrlReplacement` string when calling `Html::encodeMediaItemUrls`
// and vice versa when calling `Html::decodeMediaItemUrls`
'mediaUrlReplacement' => '{{media_item_url_prefix}}' //See `$mediaUrlPrefix`
],

```

Add the following code in project's configuration file under `components` section
Expand All @@ -48,6 +44,14 @@ Add the following code in project's configuration file under `components` sectio
'formatter' => [
'class' => 'centigen\base\i18n\Formatter'
],
'i18ncontent' => [
'class' => 'centigen\i18ncontent\I18nContent',
'userClass' => 'common\models\User', //User model class. If you do not have user model, generate it from user table. Make sure this models extends \yii\db\ActiveRecord class
'mediaUrlPrefix' => null, //In texts which may contain <img> or other media object tags (texts which come from WYSIWYG editors)
// `$mediaUrlPrefix` strings are replaced with `$mediaUrlReplacement` string when calling `Html::encodeMediaItemUrls`
// and vice versa when calling `Html::decodeMediaItemUrls`
'mediaUrlReplacement' => '{{media_item_url_prefix}}' //See `$mediaUrlPrefix`
],
```

Add `availableLocales` array to application configuration `params` array.
Expand Down
11 changes: 4 additions & 7 deletions helpers/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public static function encodeMediaItemUrls($text)
if (!$text){
return $text;
}
$module = Yii::$app->getModule('i18ncontent');
return str_replace($module->mediaUrlPrefix, $module->mediaUrlReplacement, $text);
$component = Yii::$app->i18ncontent;
return str_replace($component->mediaUrlPrefix, $component->mediaUrlReplacement, $text);
}

/**
Expand All @@ -48,11 +48,8 @@ public static function decodeMediaItemUrls($text)
if (!$text){
return $text;
}
/**
* @var $module Module
*/
$module = Yii::$app->getModule('i18ncontent');
return str_replace($module->mediaUrlReplacement, $module->mediaUrlPrefix, $text);
$component = Yii::$app->i18ncontent;
return str_replace($component->mediaUrlReplacement, $component->mediaUrlPrefix, $text);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ public function attributeLabels()
*/
public function getAuthor()
{
return $this->hasOne(Yii::$app->getModule('i18ncontent')->userClass, ['id' => 'author_id']);
return $this->hasOne(Yii::$app->i18ncontent->userClass, ['id' => 'author_id']);
}

/**
* @return \yii\db\ActiveQuery
*/
public function getUpdater()
{
return $this->hasOne(Yii::$app->getModule('i18ncontent')->userClass, ['id' => 'updater_id']);
return $this->hasOne(Yii::$app->i18ncontent->userClass, ['id' => 'updater_id']);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ public function attributeLabels()
*/
public function getAuthor()
{
return $this->hasOne(Yii::$app->getModule('i18ncontent')->userClass, ['id' => 'author_id']);
return $this->hasOne(Yii::$app->i18ncontent->userClass, ['id' => 'author_id']);
}

/**
* @return \yii\db\ActiveQuery
*/
public function getUpdater()
{
return $this->hasOne(Yii::$app->getModule('i18ncontent')->userClass, ['id' => 'updater_id']);
return $this->hasOne(Yii::$app->i18ncontent->userClass, ['id' => 'updater_id']);
}

/**
Expand Down

0 comments on commit 7c370b6

Please sign in to comment.