Commit 777eb8ffb58bbb4fe4178a8cfbde5396c2d68ceb

Authored by Yarik
1 parent 4921cad4

Добален блог v 0.5 dev commit 2

common/config/.gitignore 0 → 100644
  1 +main-local.php
0 2 \ No newline at end of file
... ...
common/models/Media.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace common\models;
  4 +
  5 +use common\modules\blog\models\Article;
  6 +use common\modules\blog\models\ArticleCategoryMedia;
  7 +use common\modules\blog\models\ArticleMedia;
  8 +use Yii;
  9 +use yii\db\ActiveRecord;
  10 +use yii\web\UploadedFile;
  11 +
  12 +/**
  13 + * This is the model class for table "media".
  14 + *
  15 + * @property integer $id
  16 + * @property string $hash
  17 + *
  18 + * @property ArticleCategoryMedia[] $articleCategoryMedia
  19 + * @property ArticleMedia[] $articleMedia
  20 + */
  21 +class Media extends ActiveRecord
  22 +{
  23 + /**
  24 + * @var UploadedFile[];
  25 + */
  26 + public $imageFile;
  27 +
  28 + /**
  29 + * @inheritdoc
  30 + */
  31 + public static function tableName()
  32 + {
  33 + return 'media';
  34 + }
  35 +
  36 + /**
  37 + * @inheritdoc
  38 + */
  39 + public function rules()
  40 + {
  41 + return [
  42 + [['id'], 'integer'],
  43 + [['hash'], 'string'],
  44 + [['imageFile'], 'file', 'extensions' => 'png, gif, jpeg, jpg', 'skipOnEmpty' => true],
  45 + ];
  46 + }
  47 +
  48 + /**
  49 + * @inheritdoc
  50 + */
  51 + public function attributeLabels()
  52 + {
  53 + return [
  54 + 'id' => Yii::t('app', 'ID'),
  55 + 'hash' => Yii::t('app', 'Hash'),
  56 + ];
  57 + }
  58 +
  59 + /**
  60 + * @return \yii\db\ActiveQuery
  61 + */
  62 + public function getArticleCategoryMedia()
  63 + {
  64 + return $this->hasMany(ArticleCategoryMedia::className(), ['media_id' => 'id']);
  65 + }
  66 +
  67 + /**
  68 + * @return \yii\db\ActiveQuery
  69 + */
  70 + public function getArticleMedia()
  71 + {
  72 + return $this->hasMany(ArticleMedia::className(), ['media_id' => 'id']);
  73 + }
  74 +
  75 + public function getArticle()
  76 + {
  77 + return $this->hasMany(Article::className(), ['id' => 'article_id'])->via('articleMedia');
  78 + }
  79 +
  80 + public function upload()
  81 + {
  82 + if(!empty($this->imageFile) && $this->validate('imageFile'))
  83 + {
  84 + $uploaddir = \Yii::getAlias('@saveImageDir');
  85 + $this->hash = md5_file($this->imageFile->tempName).\Yii::$app->security->generateRandomString(5);
  86 + $this->extension = $this->imageFile->extension;
  87 + if(is_dir($uploaddir.$this->hash)) {
  88 + return false;
  89 + } else {
  90 + if(!mkdir($uploaddir.$this->hash, 0755)) {
  91 + return false;
  92 + }
  93 + }
  94 + $this->imageFile->saveAs($uploaddir.$this->hash.'/original.'.$this->imageFile->extension, false);
  95 + if($this->save(false)) {
  96 + return true;
  97 + } else {
  98 + $this->addError('imageFile', \Yii::t('app', 'Cannot load file'));
  99 + return false;
  100 + }
  101 + } else {
  102 + return false;
  103 + }
  104 + }
  105 +
  106 + public function delete()
  107 + {
  108 + $uploaddir = \Yii::getAlias('@saveImageDir');
  109 + if(is_dir($uploaddir.$this->hash)) {
  110 + $this->removeDir($uploaddir.$this->hash);
  111 + }
  112 + return parent::delete();
  113 + }
  114 +
  115 + public function removeDir($dir)
  116 + {
  117 + if($objs = glob($dir."/*")) {
  118 + foreach($objs as $obj) {
  119 + is_dir($obj) ? removeDir($obj) : unlink($obj);
  120 + }
  121 + }
  122 + rmdir($dir);
  123 + }
  124 +}
... ...
common/modules/blog/models/Article.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace common\modules\blog\models;
  4 +
  5 +use common\models\Media;
  6 +use common\models\User;
  7 +use common\modules\blog\behaviors\Autocomplete;
  8 +use Yii;
  9 +use yii\db\Query;
  10 +
  11 +/**
  12 + * This is the model class for table "article".
  13 + *
  14 + * @property integer $id
  15 + * @property integer $sort
  16 + * @property string $create_at
  17 + * @property string $update_at
  18 + * @property string $code
  19 + * @property integer $author
  20 + * @property string $tags
  21 + * @property integer $parent_id
  22 + * @property integer $active
  23 + * @property integer $comments
  24 + * @property integer $voting
  25 + *
  26 + * @property Article $parent
  27 + * @property Article[] $articles
  28 + * @property User $author0
  29 + * @property ArticleLang[] $articleLangs
  30 + * @property ArticleMedia[] $articleMedia
  31 + * @property ArticleToCategory[] $articleToCategories
  32 + * @property Media[] $media
  33 + */
  34 +class Article extends \yii\db\ActiveRecord
  35 +{
  36 + /**
  37 + * @inheritdoc
  38 + */
  39 + public static function tableName()
  40 + {
  41 + return 'article';
  42 + }
  43 +
  44 + public function behaviors()
  45 + {
  46 + return [
  47 + [
  48 + 'class' => Autocomplete::className(),
  49 + 'attributes' => [
  50 + 'translit' => ['code'],
  51 + ]
  52 + ]
  53 + ];
  54 + }
  55 + /**
  56 + * @inheritdoc
  57 + */
  58 + public function rules()
  59 + {
  60 + return [
  61 + [['sort', 'parent_id', 'active', 'comments', 'voting'], 'integer'],
  62 + [['create_at', 'update_at'], 'safe'],
  63 + [['code'], 'required'],
  64 + [['code', 'tags'], 'string']
  65 + ];
  66 + }
  67 +
  68 + /**
  69 + * @inheritdoc
  70 + */
  71 + public function attributeLabels()
  72 + {
  73 + return [
  74 + 'id' => Yii::t('app', 'ID'),
  75 + 'sort' => Yii::t('app', 'Sort'),
  76 + 'create_at' => Yii::t('app', 'Create At'),
  77 + 'update_at' => Yii::t('app', 'Update At'),
  78 + 'code' => Yii::t('app', 'Code'),
  79 + 'author' => Yii::t('app', 'Author'),
  80 + 'tags' => Yii::t('app', 'Tags'),
  81 + 'parent_id' => Yii::t('app', 'Parent ID'),
  82 + 'active' => Yii::t('app', 'Active'),
  83 + 'comments' => Yii::t('app', 'Comments'),
  84 + 'voting' => Yii::t('app', 'Voting'),
  85 + ];
  86 + }
  87 +
  88 + /**
  89 + * @return \yii\db\ActiveQuery
  90 + */
  91 + public function getParent()
  92 + {
  93 + return $this->hasOne(Article::className(), ['id' => 'parent_id']);
  94 + }
  95 +
  96 + /**
  97 + * @return \yii\db\ActiveQuery
  98 + */
  99 + public function getArticles()
  100 + {
  101 + return $this->hasMany(Article::className(), ['parent_id' => 'id']);
  102 + }
  103 +
  104 + /**
  105 + * @return \yii\db\ActiveQuery
  106 + */
  107 + public function getAuthor0()
  108 + {
  109 + return $this->hasOne(User::className(), ['id' => 'author']);
  110 + }
  111 +
  112 + /**
  113 + * @return \yii\db\ActiveQuery
  114 + */
  115 + public function getArticleLangs()
  116 + {
  117 + return $this->hasMany(ArticleLang::className(), ['article_id' => 'id']);
  118 + }
  119 +
  120 + /**
  121 + * @return \yii\db\ActiveQuery
  122 + */
  123 + public function getArticleMedia()
  124 + {
  125 + return $this->hasMany(ArticleMedia::className(), ['article_id' => 'id']);
  126 + }
  127 +
  128 + public function getMedia()
  129 + {
  130 + return $this->hasMany(Media::className(), ['id' => 'media_id'])->via('articleMedia');
  131 + }
  132 + /**
  133 + * @return \yii\db\ActiveQuery
  134 + */
  135 + public function getArticleToCategories()
  136 + {
  137 + return $this->hasMany(ArticleToCategory::className(), ['article_id' => 'id']);
  138 + }
  139 +
  140 + public function getArticleCategories()
  141 + {
  142 + return $this->hasMany(ArticleCategory::className(), ['id' => 'category_id'])->viaTable('article_to_category', ['article_id' => 'id']);
  143 + }
  144 +
  145 + public static function findArticleDropdown($id)
  146 + {
  147 + $query = new Query();
  148 + return $query->select(['l.name', 'a.id'])
  149 + ->from(['article a'])
  150 + ->leftJoin(['article_lang l'], 'a.id = l.article_id')
  151 + ->where(['l.lang_id' => 0, 'a.active' => 1])
  152 + ->andWhere(['not', ['a.id' => $id]])
  153 + ->indexBy('a.id')
  154 + ->column();
  155 + }
  156 +
  157 + public function getArticleCategoriesArray()
  158 + {
  159 + return $this->getArticleToCategories()->select('category_id')->column();
  160 + }
  161 +}
... ...
common/modules/blog/models/ArticleCategory.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace common\modules\blog\models;
  4 +
  5 +use common\modules\blog\behaviors\Autocomplete;
  6 +use Yii;
  7 +use yii\behaviors\TimestampBehavior;
  8 +use yii\db\ActiveRecord;
  9 +use yii\db\Query;
  10 +
  11 +/**
  12 + * This is the model class for table "article_category".
  13 + *
  14 + * @property integer $id
  15 + * @property integer $active
  16 + * @property integer $sort
  17 + * @property string $code
  18 + * @property string $created_at
  19 + * @property string $updated_at
  20 + * @property string $tags
  21 + * @property integer $parent_id
  22 + *
  23 + * @property Article[] $articles
  24 + * @property ArticleCategory $parent
  25 + * @property ArticleCategory[] $articleCategories
  26 + * @property ArticleCategoryLang[] $articleCategoryLangs
  27 + * @property ArticleCategoryMedia[] $articleCategoryMedia
  28 + */
  29 +class ArticleCategory extends ActiveRecord
  30 +{
  31 + /**
  32 + * @inheritdoc
  33 + */
  34 + public static function tableName()
  35 + {
  36 + return 'article_category';
  37 + }
  38 +
  39 + public function behaviors()
  40 + {
  41 + return [
  42 + [
  43 + 'class' => Autocomplete::className(),
  44 + 'attributes' => [
  45 + 'translit' => ['code'],
  46 + ]
  47 + ]
  48 + ];
  49 + }
  50 + /**
  51 + * @inheritdoc
  52 + */
  53 + public function rules()
  54 + {
  55 + return [
  56 + [['active', 'sort', 'parent_id'], 'integer'],
  57 + [['code'], 'required'],
  58 + [['code', 'tags'], 'string'],
  59 + [['created_at', 'updated_at'], 'safe'],
  60 + [['active'], 'boolean'],
  61 + ];
  62 + }
  63 +
  64 + /**
  65 + * @inheritdoc
  66 + */
  67 + public function attributeLabels()
  68 + {
  69 + return [
  70 + 'id' => Yii::t('app', 'ID'),
  71 + 'active' => Yii::t('app', 'Active'),
  72 + 'sort' => Yii::t('app', 'Sort'),
  73 + 'code' => Yii::t('app', 'Code'),
  74 + 'created_at' => Yii::t('app', 'Created At'),
  75 + 'updated_at' => Yii::t('app', 'Updated At'),
  76 + 'tags' => Yii::t('app', 'Tags'),
  77 + 'parent_id' => Yii::t('app', 'Parent ID'),
  78 + ];
  79 + }
  80 +
  81 + /**
  82 + * @return \yii\db\ActiveQuery
  83 + */
  84 + public function getArticles()
  85 + {
  86 + return $this->hasMany(Article::className(), ['category_id' => 'id']);
  87 + }
  88 +
  89 + /**
  90 + * @return \yii\db\ActiveQuery
  91 + */
  92 + public function getParent()
  93 + {
  94 + return $this->hasOne(ArticleCategory::className(), ['id' => 'parent_id']);
  95 + }
  96 +
  97 + /**
  98 + * @return \yii\db\ActiveQuery
  99 + */
  100 + public function getArticleCategories()
  101 + {
  102 + return $this->hasMany(ArticleCategory::className(), ['parent_id' => 'id']);
  103 + }
  104 +
  105 + /**
  106 + * @return \yii\db\ActiveQuery
  107 + */
  108 + public function getArticleCategoryLangs()
  109 + {
  110 + return $this->hasMany(ArticleCategoryLang::className(), ['category_id' => 'id']);
  111 + }
  112 +
  113 + /**
  114 + * @return \yii\db\ActiveQuery
  115 + */
  116 + public function getArticleCategoryMedia()
  117 + {
  118 + return $this->hasMany(ArticleCategoryMedia::className(), ['category_id' => 'id']);
  119 + }
  120 +
  121 + public static function findArticleCategoryDropdown($id)
  122 + {
  123 + $query = new Query();
  124 + return $query->select(['l.name', 'c.id'])
  125 + ->from(['article_category c'])
  126 + ->leftJoin(['article_category_lang l'], 'c.id = l.category_id')
  127 + ->where(['l.lang_id' => 0, 'c.active' => 1])
  128 + ->andWhere(['not', ['c.id' => $id]])
  129 + ->indexBy('id')
  130 + ->column();
  131 + }
  132 +
  133 +}
... ...
common/modules/blog/models/ArticleCategoryLang.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace common\modules\blog\models;
  4 +
  5 +use common\modules\blog\behaviors\Autocomplete;
  6 +use Yii;
  7 +
  8 +/**
  9 + * This is the model class for table "article_category_lang".
  10 + *
  11 + * @property integer $id
  12 + * @property integer $lang_id
  13 + * @property integer $category_id
  14 + * @property string $text
  15 + * @property string $preview
  16 + * @property string $seo_url
  17 + * @property string $name
  18 + * @property string $meta_title
  19 + * @property string $meta_descr
  20 + * @property string $meta_keywords
  21 + * @property string $h1_tag
  22 + * @property string $tags
  23 + *
  24 + * @property ArticleCategory $category
  25 + * @property Language $lang
  26 + */
  27 +class ArticleCategoryLang extends \yii\db\ActiveRecord
  28 +{
  29 + /**
  30 + * @inheritdoc
  31 + */
  32 + public static function tableName()
  33 + {
  34 + return 'article_category_lang';
  35 + }
  36 +
  37 + public function behaviors()
  38 + {
  39 + return [
  40 + [
  41 + 'class' => Autocomplete::className(),
  42 + 'attributes' => [
  43 + 'repeat' => [['preview', 'text', false, 5, true, '...']],
  44 + ]
  45 + ]
  46 + ];
  47 + }
  48 + /**
  49 + * @inheritdoc
  50 + */
  51 + public function rules()
  52 + {
  53 + return [
  54 + [['lang_id', 'category_id'], 'integer'],
  55 + [['text', 'name'], 'required'],
  56 + [['text', 'preview', 'seo_url', 'name', 'meta_title', 'meta_descr', 'meta_keywords', 'h1_tag', 'tags'], 'string'],
  57 + ['seo_url', function($attribute, $params) {
  58 + $pattern = "/^[a-zA-Z\d_-]+$/";
  59 + if(!preg_match($pattern, $this->$attribute)) {
  60 + $this->addError($attribute, Yii::t('app', "Pattern doesn't match."));
  61 + }
  62 + }]
  63 + ];
  64 + }
  65 +
  66 + /**
  67 + * @inheritdoc
  68 + */
  69 + public function attributeLabels()
  70 + {
  71 + return [
  72 + 'id' => Yii::t('app', 'ID'),
  73 + 'lang_id' => Yii::t('app', 'Lang ID'),
  74 + 'category_id' => Yii::t('app', 'Category ID'),
  75 + 'text' => Yii::t('app', 'Text'),
  76 + 'preview' => Yii::t('app', 'Preview'),
  77 + 'seo_url' => Yii::t('app', 'Seo Url'),
  78 + 'name' => Yii::t('app', 'Name'),
  79 + 'meta_title' => Yii::t('app', 'Meta Title'),
  80 + 'meta_descr' => Yii::t('app', 'Meta Descr'),
  81 + 'meta_keywords' => Yii::t('app', 'Meta Keywords'),
  82 + 'h1_tag' => Yii::t('app', 'H1 Tag'),
  83 + 'tags' => Yii::t('app', 'Tags'),
  84 + ];
  85 + }
  86 +
  87 + /**
  88 + * @return \yii\db\ActiveQuery
  89 + */
  90 + public function getCategory()
  91 + {
  92 + return $this->hasOne(ArticleCategory::className(), ['id' => 'category_id']);
  93 + }
  94 +
  95 + /**
  96 + * @return \yii\db\ActiveQuery
  97 + */
  98 + public function getLang()
  99 + {
  100 + return $this->hasOne(Language::className(), ['language_id' => 'lang_id']);
  101 + }
  102 +}
... ...
common/modules/blog/models/ArticleCategoryMedia.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace common\modules\blog\models;
  4 +
  5 +use common\models\Media;
  6 +use Yii;
  7 +
  8 +/**
  9 + * This is the model class for table "article_category_media".
  10 + *
  11 + * @property integer $id
  12 + * @property integer $category_id
  13 + * @property integer $media_id
  14 + * @property string $media_alt
  15 + * @property string $media_title
  16 + * @property string $media_caption
  17 + * @property string $type
  18 + *
  19 + * @property ArticleCategory $category
  20 + * @property Media $media
  21 + */
  22 +class ArticleCategoryMedia extends \yii\db\ActiveRecord
  23 +{
  24 + const SCENARIO_FULL = 'full';
  25 + const SCENARIO_PREVIEW = 'preview';
  26 + const SCENARIO_ADDITIONAL = 'additional';
  27 + public $imageFile;
  28 + /**
  29 + * @inheritdoc
  30 + */
  31 + public static function tableName()
  32 + {
  33 + return 'article_category_media';
  34 + }
  35 +
  36 + public function scenarios()
  37 + {
  38 + $scenarios = parent::scenarios();
  39 + $scenarios[self::SCENARIO_FULL] = ['id', 'category_id', 'media_id', 'type', 'media_alt', 'media_title', 'media_caption', 'imageFile'];
  40 + $scenarios[self::SCENARIO_PREVIEW] = ['id', 'category_id', 'media_id', 'type', 'media_alt', 'media_title', 'media_caption', 'imageFile'];
  41 + $scenarios[self::SCENARIO_ADDITIONAL] = ['id', 'category_id', 'media_id', 'type', 'imageFile'];
  42 + return $scenarios;
  43 + }
  44 +
  45 + /**
  46 + * @inheritdoc
  47 + */
  48 + public function rules()
  49 + {
  50 + return [
  51 + [['category_id', 'media_id'], 'required'],
  52 + [['category_id', 'media_id'], 'integer'],
  53 + [['media_alt', 'media_title', 'media_caption'], 'string'],
  54 + [['type'], 'string', 'max' => 10],
  55 + [['imageFile'], 'file', 'extensions' => 'png, gif, jpg, jpeg', 'skipOnEmpty' => true, 'on' => self::SCENARIO_FULL],
  56 + [['imageFile'], 'file', 'extensions' => 'png, gif, jpg, jpeg', 'skipOnEmpty' => true, 'on' => self::SCENARIO_PREVIEW],
  57 + [['imageFile'], 'file', 'extensions' => 'png, gif, jpg, jpeg', 'skipOnEmpty' => true, 'maxFiles' => 10, 'on' => self::SCENARIO_ADDITIONAL]
  58 + ];
  59 + }
  60 +
  61 + /**
  62 + * @inheritdoc
  63 + */
  64 + public function attributeLabels()
  65 + {
  66 + return [
  67 + 'id' => Yii::t('app', 'ID'),
  68 + 'category_id' => Yii::t('app', 'Category ID'),
  69 + 'media_id' => Yii::t('app', 'Media ID'),
  70 + 'media_alt' => Yii::t('app', 'Media Alt'),
  71 + 'media_title' => Yii::t('app', 'Media Title'),
  72 + 'media_caption' => Yii::t('app', 'Media Caption'),
  73 + 'type' => Yii::t('app', 'Type'),
  74 + 'imageFile' => Yii::t('app', 'Image File'),
  75 + ];
  76 + }
  77 +
  78 + /**
  79 + * @return \yii\db\ActiveQuery
  80 + */
  81 + public function getCategory()
  82 + {
  83 + return $this->hasOne(ArticleCategory::className(), ['id' => 'category_id']);
  84 + }
  85 +
  86 + /**
  87 + * @return \yii\db\ActiveQuery
  88 + */
  89 + public function getMedia()
  90 + {
  91 + return $this->hasOne(Media::className(), ['id' => 'media_id']);
  92 + }
  93 +
  94 + public function upload($category_id)
  95 + {
  96 + $this->category_id = $category_id;
  97 + if(is_array($this->imageFile)) {
  98 + $ok = true;
  99 + foreach($this->imageFile as $image) {
  100 + $media_category = clone $this;
  101 + $media = new Media();
  102 + $media->imageFile = $image;
  103 + $media->upload();
  104 + $media_category->media_id = $media->id;
  105 + $ok = $media_category->save() && $ok;
  106 + unset($media_category);
  107 + }
  108 + return $ok;
  109 + } elseif(!empty($this->imageFile)) {
  110 + $media = new Media();
  111 + $media->imageFile = $this->imageFile;
  112 + $media->upload();
  113 + $this->media_id = $media->id;
  114 + return $this->save();
  115 + }
  116 + }
  117 +
  118 + public function replace($category_id, $removeMedia = false)
  119 + {
  120 + $this->category_id = $category_id;
  121 + if($removeMedia) {
  122 + $category_media = ArticleCategoryMedia::find()->select('media_id')->where(['category_id' => $this->category_id, 'type' => $this->type])->column();
  123 + $media = array();
  124 + foreach($category_media as $media_id) {
  125 + $media[] = Media::findOne(['id' => $media_id]);
  126 + }
  127 + $media = array_unique($media);
  128 + foreach($media as $one_media) {
  129 + if($one_media instanceof Media) {
  130 + $one_media->delete();
  131 + }
  132 + }
  133 + unset($media);
  134 + unset($category_media);
  135 + }
  136 + if(is_array($this->imageFile)) {
  137 + $ok = true;
  138 + foreach($this->imageFile as $image) {
  139 + $media_category = clone $this;
  140 + $media = new Media();
  141 + $media->imageFile = $image;
  142 + $media->upload();
  143 + $media_category->media_id = $media->id;
  144 + $ok = $media_category->save() && $ok;
  145 + unset($media_category);
  146 + }
  147 + return $ok;
  148 + } elseif(!empty($this->imageFile)) {
  149 + ArticleCategoryMedia::deleteAll(['category_id' => $this->category_id, 'type' => $this->type]);
  150 + $media = new Media();
  151 + $media->imageFile = $this->imageFile;
  152 + $media->upload();
  153 + $this->media_id = $media->id;
  154 + $this->setIsNewRecord(true);
  155 + return $this->save();
  156 + }
  157 + }
  158 +
  159 +}
... ...
common/modules/blog/models/ArticleLang.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace common\modules\blog\models;
  4 +
  5 +use Yii;
  6 +use common\models\Language;
  7 +
  8 +/**
  9 + * This is the model class for table "article_lang".
  10 + *
  11 + * @property integer $id
  12 + * @property integer $lang_id
  13 + * @property integer $article_id
  14 + * @property string $text
  15 + * @property string $seo_url
  16 + * @property string $name
  17 + * @property string $preview
  18 + * @property string $meta_title
  19 + * @property string $meta_descr
  20 + * @property string $meta_keywords
  21 + * @property string $h1_tag
  22 + * @property string $tags
  23 + *
  24 + * @property Article $article
  25 + * @property Language $lang
  26 + */
  27 +class ArticleLang extends \yii\db\ActiveRecord
  28 +{
  29 + /**
  30 + * @inheritdoc
  31 + */
  32 + public static function tableName()
  33 + {
  34 + return 'article_lang';
  35 + }
  36 +
  37 + /**
  38 + * @inheritdoc
  39 + */
  40 + public function rules()
  41 + {
  42 + return [
  43 + [['lang_id', 'text', 'name'], 'required'],
  44 + [['lang_id', 'article_id'], 'integer'],
  45 + [['text', 'seo_url', 'name', 'preview', 'meta_title', 'meta_descr', 'meta_keywords', 'h1_tag', 'tags'], 'string']
  46 + ];
  47 + }
  48 +
  49 + /**
  50 + * @inheritdoc
  51 + */
  52 + public function attributeLabels()
  53 + {
  54 + return [
  55 + 'id' => Yii::t('app', 'ID'),
  56 + 'lang_id' => Yii::t('app', 'Lang ID'),
  57 + 'article_id' => Yii::t('app', 'Article ID'),
  58 + 'text' => Yii::t('app', 'Text'),
  59 + 'seo_url' => Yii::t('app', 'Seo Url'),
  60 + 'name' => Yii::t('app', 'Name'),
  61 + 'preview' => Yii::t('app', 'Preview'),
  62 + 'meta_title' => Yii::t('app', 'Meta Title'),
  63 + 'meta_descr' => Yii::t('app', 'Meta Descr'),
  64 + 'meta_keywords' => Yii::t('app', 'Meta Keywords'),
  65 + 'h1_tag' => Yii::t('app', 'H1 Tag'),
  66 + 'tags' => Yii::t('app', 'Tags'),
  67 + ];
  68 + }
  69 +
  70 + /**
  71 + * @return \yii\db\ActiveQuery
  72 + */
  73 + public function getArticle()
  74 + {
  75 + return $this->hasOne(Article::className(), ['id' => 'article_id']);
  76 + }
  77 +
  78 + /**
  79 + * @return \yii\db\ActiveQuery
  80 + */
  81 + public function getLang()
  82 + {
  83 + return $this->hasOne(Language::className(), ['language_id' => 'lang_id']);
  84 + }
  85 +}
... ...
common/modules/blog/models/ArticleMedia.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace common\modules\blog\models;
  4 +
  5 +use common\models\Media;
  6 +use Yii;
  7 +use yii\web\UploadedFile;
  8 +
  9 +/**
  10 + * This is the model class for table "article_media".
  11 + *
  12 + * @property integer $id
  13 + * @property integer $article_id
  14 + * @property integer $media_id
  15 + * @property string $type
  16 + * @property string $media_alt
  17 + * @property string $media_title
  18 + * @property string $media_caption
  19 + *
  20 + * @property Article $article
  21 + * @property Media $media
  22 + */
  23 +class ArticleMedia extends \yii\db\ActiveRecord
  24 +{
  25 + const SCENARIO_FULL = 'full';
  26 + const SCENARIO_PREVIEW = 'preview';
  27 + const SCENARIO_ADDITIONAL = 'additional';
  28 + public $imageFile;
  29 + /**
  30 + * @inheritdoc
  31 + */
  32 + public static function tableName()
  33 + {
  34 + return 'article_media';
  35 + }
  36 +
  37 + public function scenarios()
  38 + {
  39 + $scenarios = parent::scenarios();
  40 + $scenarios[self::SCENARIO_FULL] = ['id', 'article_id', 'media_id', 'type', 'media_alt', 'media_title', 'media_caption', 'imageFile'];
  41 + $scenarios[self::SCENARIO_PREVIEW] = ['id', 'article_id', 'media_id', 'type', 'media_alt', 'media_title', 'media_caption', 'imageFile'];
  42 + $scenarios[self::SCENARIO_ADDITIONAL] = ['id', 'article_id', 'media_id', 'type', 'imageFile'];
  43 + return $scenarios;
  44 + }
  45 +
  46 + /**
  47 + * @inheritdoc
  48 + */
  49 + public function rules()
  50 + {
  51 + return [
  52 + [['article_id', 'media_id'], 'required'],
  53 + [['article_id', 'media_id'], 'integer'],
  54 + [['media_alt', 'media_title', 'media_caption'], 'string'],
  55 + [['type'], 'string', 'max' => 10],
  56 + [['imageFile'], 'file', 'extensions' => 'png, gif, jpg, jpeg', 'skipOnEmpty' => true, 'on' => self::SCENARIO_FULL],
  57 + [['imageFile'], 'file', 'extensions' => 'png, gif, jpg, jpeg', 'skipOnEmpty' => true, 'on' => self::SCENARIO_PREVIEW],
  58 + [['imageFile'], 'file', 'extensions' => 'png, gif, jpg, jpeg', 'skipOnEmpty' => true, 'maxFiles' => 10, 'on' => self::SCENARIO_ADDITIONAL]
  59 + ];
  60 + }
  61 +
  62 + /**
  63 + * @inheritdoc
  64 + */
  65 + public function attributeLabels()
  66 + {
  67 + return [
  68 + 'id' => Yii::t('app', 'ID'),
  69 + 'article_id' => Yii::t('app', 'Article ID'),
  70 + 'media_id' => Yii::t('app', 'Media ID'),
  71 + 'type' => Yii::t('app', 'Type'),
  72 + 'media_alt' => Yii::t('app', 'Media Alt'),
  73 + 'media_title' => Yii::t('app', 'Media Title'),
  74 + 'media_caption' => Yii::t('app', 'Media Caption'),
  75 + 'imageFile' => Yii::t('app', 'Image File'),
  76 + ];
  77 + }
  78 +
  79 + /**
  80 + * @return \yii\db\ActiveQuery
  81 + */
  82 + public function getArticle()
  83 + {
  84 + return $this->hasOne(Article::className(), ['id' => 'article_id']);
  85 + }
  86 +
  87 + /**
  88 + * @return \yii\db\ActiveQuery
  89 + */
  90 + public function getMedia()
  91 + {
  92 + return $this->hasOne(Media::className(), ['id' => 'media_id']);
  93 + }
  94 +
  95 + public function upload($article_id)
  96 + {
  97 + $this->article_id = $article_id;
  98 + if(is_array($this->imageFile)) {
  99 + $ok = true;
  100 + foreach($this->imageFile as $image) {
  101 + $media_article = clone $this;
  102 + $media = new Media();
  103 + $media->imageFile = $image;
  104 + $media->upload();
  105 + $media_article->media_id = $media->id;
  106 + $ok = $media_article->save() && $ok;
  107 + unset($media_article);
  108 + }
  109 + return $ok;
  110 + } elseif(!empty($this->imageFile)) {
  111 + $media = new Media();
  112 + $media->imageFile = $this->imageFile;
  113 + $media->upload();
  114 + $this->media_id = $media->id;
  115 + return $this->save();
  116 + }
  117 + }
  118 +
  119 + public function replace($article_id, $removeMedia = false)
  120 + {
  121 + $this->article_id = $article_id;
  122 + if($removeMedia) {
  123 + $article_media = ArticleMedia::find()->select('media_id')->where(['article_id' => $this->article_id, 'type' => $this->type])->column();
  124 + $media = array();
  125 + foreach($article_media as $media_id) {
  126 + $media[] = Media::findOne(['id' => $media_id]);
  127 + }
  128 + $media = array_unique($media);
  129 + foreach($media as $one_media) {
  130 + if($one_media instanceof Media) {
  131 + $one_media->delete();
  132 + }
  133 + }
  134 + unset($media);
  135 + unset($article_media);
  136 + }
  137 + if(is_array($this->imageFile)) {
  138 + $ok = true;
  139 + foreach($this->imageFile as $image) {
  140 + $media_article = clone $this;
  141 + $media = new Media();
  142 + $media->imageFile = $image;
  143 + $media->upload();
  144 + $media_article->media_id = $media->id;
  145 + $ok = $media_article->save() && $ok;
  146 + unset($media_article);
  147 + }
  148 + return $ok;
  149 + } elseif(!empty($this->imageFile)) {
  150 + ArticleMedia::deleteAll(['article_id' => $this->article_id, 'type' => $this->type]);
  151 + $media = new Media();
  152 + $media->imageFile = $this->imageFile;
  153 + $media->upload();
  154 + $this->media_id = $media->id;
  155 + $this->setIsNewRecord(true);
  156 + return $this->save();
  157 + }
  158 + }
  159 +
  160 +}
... ...
common/modules/blog/models/ArticleToCategory.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace common\modules\blog\models;
  4 +
  5 +use Yii;
  6 +
  7 +/**
  8 + * This is the model class for table "article_to_category".
  9 + *
  10 + * @property integer $article_id
  11 + * @property integer $category_id
  12 + *
  13 + * @property Article $article
  14 + * @property ArticleCategory $category
  15 + */
  16 +class ArticleToCategory extends \yii\db\ActiveRecord
  17 +{
  18 + /**
  19 + * @inheritdoc
  20 + */
  21 + public static function tableName()
  22 + {
  23 + return 'article_to_category';
  24 + }
  25 +
  26 + /**
  27 + * @inheritdoc
  28 + */
  29 + public function rules()
  30 + {
  31 + return [
  32 + [['article_id', 'category_id'], 'integer']
  33 + ];
  34 + }
  35 +
  36 + /**
  37 + * @inheritdoc
  38 + */
  39 + public function attributeLabels()
  40 + {
  41 + return [
  42 + 'article_id' => Yii::t('app', 'Article ID'),
  43 + 'category_id' => Yii::t('app', 'Category ID'),
  44 + ];
  45 + }
  46 +
  47 + /**
  48 + * @return \yii\db\ActiveQuery
  49 + */
  50 + public function getArticle()
  51 + {
  52 + return $this->hasOne(Article::className(), ['id' => 'article_id']);
  53 + }
  54 +
  55 + /**
  56 + * @return \yii\db\ActiveQuery
  57 + */
  58 + public function getCategory()
  59 + {
  60 + return $this->hasOne(ArticleCategory::className(), ['id' => 'category_id']);
  61 + }
  62 +}
... ...
db-migration/yarik.zip 0 → 100644
No preview for this file type
frontend/web/images/upload/06021c27debe77630c03fb0e28a419fcaRUpU/original.jpg 0 → 100644

225 KB

frontend/web/images/upload/1a0cbb7d512737e6257dab6319c8760dzucy5/original.png 0 → 100644

19 KB

frontend/web/images/upload/40de6d6d4d8155e28570cc6a605c312dvaQ3X/original.png 0 → 100644

17.1 KB

frontend/web/images/upload/535ef2c058ba1c87101af958ecb70dd31Mdw0/original.jpg 0 → 100644

3.83 KB

frontend/web/images/upload/5ee447f553cd4b615e0107772e72469ci_PpP/original.jpg 0 → 100644

45.3 KB

frontend/web/images/upload/68ea5e978068f52504ba9f2f10a3eca8df4wM/original.jpg 0 → 100644

46.1 KB

frontend/web/images/upload/7c0beac302dbe743c2fec948bd232d7doP_3v/original.jpg 0 → 100644

14.9 KB

frontend/web/images/upload/bdaa304bee2504b65fffa5d1cb15fbf4Cdp4t/original.jpg 0 → 100644

138 KB

frontend/web/images/upload/da89edbbf4c7d0addb126fe4169800f8VJaw2/original.jpg 0 → 100644

57.4 KB

frontend/web/images/upload/fe37052e2ca69e4db311946dc24eab40a5hso/original.jpg 0 → 100644

736 KB