Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\modules\admin\models;
- use Yii;
- use yii\helpers\ArrayHelper;
- /**
- * This is the model class for table "post".
- *
- * @property int $id
- * @property string $title
- * @property string $content
- * @property string $thumbnail
- * @property string $url
- * @property string $author
- * @property int $view
- * @property int $category
- */
- class Post extends \yii\db\ActiveRecord
- {
- public $images;
- public function behaviors()
- {
- return [
- 'image' => [
- 'class' => 'rico\yii2images\behaviors\ImageBehave',
- ]
- ];
- }
- public function getCat()
- {
- return $this->hasOne(Category::className(), ['id' => 'category']);
- }
- public function getPostTag()
- {
- return $this->hasMany(PostTag::className(), ['id_post' => 'id']);
- }
- public function getTags()
- {
- return $this->hasMany(Tag::className(), ['id' => 'id_tag'])->via('postTag');
- }
- public function getLabels()
- {
- return $this->hasMany(Label::className(), ['id' => 'label_id'])->via('postLabel');
- }
- public function getPostLabel()
- {
- return $this->hasMany(PostLabel::className(), ['post_id' => 'id']);
- }
- public $newTags;
- //теги, связанные с этим постом
- public $tags_array;
- //метки, связанные с этим постом
- public $labels_array;
- public function setTags($tagsId)
- {
- $this->newTags = (array) $tagsId;
- }
- public function beforeDelete()
- {
- if(parent::beforeDelete())
- {
- PostTag::deleteAll(['id_post'=>$this->id]);
- return true;
- }
- else
- {
- return false;
- }
- }
- //после загрузки данных поста получаем теги поста
- public function afterFind()
- {
- // $this->newTags = \yii\helpers\ArrayHelper::map($this->tags, 'title', 'title');
- parent::afterFind();
- $this->tags_array = $this->tags;
- $this->labels_array = $this->labels;
- }
- public function getTagsAsString()
- {
- $arr = ArrayHelper::map($this->tags, 'id', 'title');
- return implode(', ', $arr);
- }
- public function getLabelsAsString()
- {
- $arr = ArrayHelper::map($this->labels, 'id', 'title');
- return implode(', ', $arr);
- }
- // public function afterSave($insert, $changedAttributes)
- // {
- //
- // foreach($tags as $t){
- // $tag = new Tag();
- // $tag->title = $t;
- // $tag->save();
- // }
- // PostTag::deleteAll(['id_post' => $this->id]);
- //
- // $values = [];
- //
- // foreach($this->tags as $id)
- // {
- // $values[] = [$this->id, $id];
- // }
- // self::getDb()->createCommand()
- // ->batchInsert(PostTag::tableName(), ['id_post', 'id_tag'], $values)->execute();
- //
- // parent::afterSave($insert, $changedAttributes);
- // }
- // public function afterSave($insert, $changedAttributes)
- // {
- // parent::afterSave($insert, $changedAttributes);
- //
- // if(is_array($this->newTags)){
- // $oldTags = ArrayHelper::map($this->tags, 'title', 'id');
- // foreach($this->newTags as $one_new_tag){
- // if(isset($oldTags[$one_new_tag])){
- // unset($oldTags[$one_new_tag]);
- // } else {
- // $this->createNewTag($one_new_tag);
- // }
- // }
- // PostTag::deleteAll(['and', ['id_post'=>$this->id], ['id_tag'=>$oldTags]]);
- // } else {
- // PostTag::deleteAll(['id_post'=>$this->id]);
- // }
- // }
- // public function beforeSave($insert)
- // {
- // parent::beforeSave($insert);
- // $all_tags = ArrayHelper::map(Tag::find()->all(), 'id', 'title');
- // if($this->tags_array) {
- // foreach($this->tags_array as $one)
- // {
- // if(!in_array($one, $all_tags))
- // {
- // $model = new Tag();
- // $model->title = $one;
- // $model->save();
- // }
- // }
- // }
- // }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- ///////////////////////Save Labels
- // $all_labels =ArrayHelper::map(Label::find()->all(), 'id', 'TITLE') Array
- // (
- // [69] => 68
- // [68] => bobby
- // )
- $all_labels = ArrayHelper::map(Label::find()->all(), 'id', 'id');
- if($this->labels_array) {
- foreach($this->labels_array as $one)
- {
- if(!in_array($one, $all_labels) /*&& !is_int($one)*/) {
- $model = new Label();
- $model->title = $one;
- $model->save();
- $postLabel = new PostLabel();
- $postLabel->post_id = $this->id;
- $postLabel->label_id = $model->id;
- $postLabel->save();
- }
- }
- }
- //создаем массив меток, СВЯЗАННЫХ с постом
- $arr_labels = ArrayHelper::map($this->labels, 'id', 'id');
- if($this->labels_array) {
- foreach($this->labels_array as $one)
- {
- if(!in_array($one, $arr_labels))
- {
- $model = new PostLabel();
- $model->post_id = $this->id;
- $model->label_id = $one;
- $model->save();
- }
- if(isset($arr_labels[$one])) {
- unset($arr_labels[$one]);
- }
- PostLabel::deleteAll(['label_id' => $arr_labels, 'post_id' =>$this->id]);
- }
- } else {
- PostLabel::deleteAll(['label_id' => $arr_labels, 'post_id' =>$this->id]);
- }
- ///////////////Save new Tags
- $all_tags = ArrayHelper::map(Tag::find()->all(), 'id', 'id');
- if($this->tags_array) {
- foreach($this->tags_array as $one)
- {
- if(!in_array($one, $all_tags))
- {
- $model = new Tag();
- $model->title = $one;
- $model->save();
- $postTag = new PostTag();
- $postTag->id_post = $this->id;
- $postTag->id_tag = $model->id;
- $postTag->save();
- }
- }
- }
- /////////////////////
- //создаем массив тегов, связанных с постом
- $arr = ArrayHelper::map($this->tags, 'id', 'id');
- if($this->tags_array) {
- foreach($this->tags_array as $one) {
- if(!in_array($one, $arr)) {
- $model = new PostTag();
- $model->id_post = $this->id;
- $model->id_tag = $one;
- $model->save();
- }
- if(isset($arr[$one])) {
- unset($arr[$one]);
- }
- PostTag::deleteAll(['id_tag' => $arr, 'id_post' =>$this->id]);
- }
- } else {
- PostTag::deleteAll(['id_tag' => $arr, 'id_post' =>$this->id]);
- }
- }
- public function createNewTag($new_tag) {
- if(!$tag = Tag::find()->andWhere(['title'=>$new_tag])->one()) {
- $tag = new Tag();
- $tag->title = $new_tag;
- if(!$tag->save()){
- $tag = null;
- }
- }
- if($tag != null){ //if($tag != null)
- $post_tag = new PostTag();
- $post_tag->id_post = $this->id;
- $post_tag->id_tag = $tag->id;
- if($post_tag->save())
- return $post_tag->id;
- }
- return false;
- }
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'post';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['title', 'content', 'author'], 'required'],
- [['content'], 'string'],
- [['view', 'category'], 'integer'],
- [['title', 'thumbnail', 'url', 'author'], 'string', 'max' => 255],
- [['images'], 'file', 'extensions' => 'png,jpg,jpeg', 'skipOnEmpty' => true, 'maxFiles' => 6],
- [['tags_array'], 'safe'],
- [['labels_array'], 'safe'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'title' => 'Название',
- 'content' => 'Content',
- 'url' => 'Url',
- 'author' => 'Author',
- 'view' => 'View',
- 'category' => 'Category',
- 'tags_array' => 'Теги',
- 'labels_array' => 'Метки',
- ];
- }
- public function uploadGallery()
- {
- if($this->validate())
- {
- foreach($this->images as $file)
- {
- $path = 'images/store' . $file->baseName . '.' . $file->extension;
- $file->saveAs($path);
- $this->attachImage($path, false);
- @unlink($path);
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- }
Add Comment
Please, Sign In to add comment