Guest User

Untitled

a guest
Jul 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\modules\admin\models;
  4.  
  5. use Yii;
  6. use yii\helpers\ArrayHelper;
  7.  
  8. /**
  9. * This is the model class for table "post".
  10. *
  11. * @property int $id
  12. * @property string $title
  13. * @property string $content
  14. * @property string $thumbnail
  15. * @property string $url
  16. * @property string $author
  17. * @property int $view
  18. * @property int $category
  19. */
  20. class Post extends \yii\db\ActiveRecord
  21. {
  22.  
  23. public $images;
  24.  
  25. public function behaviors()
  26. {
  27. return [
  28. 'image' => [
  29. 'class' => 'rico\yii2images\behaviors\ImageBehave',
  30. ]
  31. ];
  32. }
  33.  
  34. public function getCat()
  35. {
  36. return $this->hasOne(Category::className(), ['id' => 'category']);
  37. }
  38.  
  39. public function getPostTag()
  40. {
  41. return $this->hasMany(PostTag::className(), ['id_post' => 'id']);
  42. }
  43.  
  44. public function getTags()
  45. {
  46. return $this->hasMany(Tag::className(), ['id' => 'id_tag'])->via('postTag');
  47. }
  48.  
  49. public function getLabels()
  50. {
  51. return $this->hasMany(Label::className(), ['id' => 'label_id'])->via('postLabel');
  52. }
  53.  
  54. public function getPostLabel()
  55. {
  56. return $this->hasMany(PostLabel::className(), ['post_id' => 'id']);
  57. }
  58.  
  59. public $newTags;
  60.  
  61. //теги, связанные с этим постом
  62. public $tags_array;
  63.  
  64. //метки, связанные с этим постом
  65. public $labels_array;
  66.  
  67. public function setTags($tagsId)
  68. {
  69. $this->newTags = (array) $tagsId;
  70. }
  71.  
  72. public function beforeDelete()
  73. {
  74. if(parent::beforeDelete())
  75. {
  76. PostTag::deleteAll(['id_post'=>$this->id]);
  77. return true;
  78. }
  79. else
  80. {
  81. return false;
  82. }
  83. }
  84.  
  85. //после загрузки данных поста получаем теги поста
  86. public function afterFind()
  87. {
  88. // $this->newTags = \yii\helpers\ArrayHelper::map($this->tags, 'title', 'title');
  89. parent::afterFind();
  90. $this->tags_array = $this->tags;
  91. $this->labels_array = $this->labels;
  92. }
  93.  
  94. public function getTagsAsString()
  95. {
  96. $arr = ArrayHelper::map($this->tags, 'id', 'title');
  97. return implode(', ', $arr);
  98. }
  99.  
  100. public function getLabelsAsString()
  101. {
  102. $arr = ArrayHelper::map($this->labels, 'id', 'title');
  103. return implode(', ', $arr);
  104. }
  105.  
  106. // public function afterSave($insert, $changedAttributes)
  107. // {
  108. //
  109. // foreach($tags as $t){
  110. // $tag = new Tag();
  111. // $tag->title = $t;
  112. // $tag->save();
  113. // }
  114. // PostTag::deleteAll(['id_post' => $this->id]);
  115. //
  116. // $values = [];
  117. //
  118. // foreach($this->tags as $id)
  119. // {
  120. // $values[] = [$this->id, $id];
  121. // }
  122. // self::getDb()->createCommand()
  123. // ->batchInsert(PostTag::tableName(), ['id_post', 'id_tag'], $values)->execute();
  124. //
  125. // parent::afterSave($insert, $changedAttributes);
  126. // }
  127.  
  128. // public function afterSave($insert, $changedAttributes)
  129. // {
  130. // parent::afterSave($insert, $changedAttributes);
  131. //
  132. // if(is_array($this->newTags)){
  133. // $oldTags = ArrayHelper::map($this->tags, 'title', 'id');
  134. // foreach($this->newTags as $one_new_tag){
  135. // if(isset($oldTags[$one_new_tag])){
  136. // unset($oldTags[$one_new_tag]);
  137. // } else {
  138. // $this->createNewTag($one_new_tag);
  139. // }
  140. // }
  141. // PostTag::deleteAll(['and', ['id_post'=>$this->id], ['id_tag'=>$oldTags]]);
  142. // } else {
  143. // PostTag::deleteAll(['id_post'=>$this->id]);
  144. // }
  145. // }
  146.  
  147. // public function beforeSave($insert)
  148. // {
  149. // parent::beforeSave($insert);
  150. // $all_tags = ArrayHelper::map(Tag::find()->all(), 'id', 'title');
  151. // if($this->tags_array) {
  152. // foreach($this->tags_array as $one)
  153. // {
  154. // if(!in_array($one, $all_tags))
  155. // {
  156. // $model = new Tag();
  157. // $model->title = $one;
  158. // $model->save();
  159. // }
  160. // }
  161. // }
  162.  
  163. // }
  164.  
  165. public function afterSave($insert, $changedAttributes)
  166. {
  167. parent::afterSave($insert, $changedAttributes);
  168.  
  169. ///////////////////////Save Labels
  170.  
  171. // $all_labels =ArrayHelper::map(Label::find()->all(), 'id', 'TITLE') Array
  172. // (
  173. // [69] => 68
  174. // [68] => bobby
  175. // )
  176. $all_labels = ArrayHelper::map(Label::find()->all(), 'id', 'id');
  177. if($this->labels_array) {
  178. foreach($this->labels_array as $one)
  179. {
  180. if(!in_array($one, $all_labels) /*&& !is_int($one)*/) {
  181. $model = new Label();
  182. $model->title = $one;
  183. $model->save();
  184. $postLabel = new PostLabel();
  185. $postLabel->post_id = $this->id;
  186. $postLabel->label_id = $model->id;
  187. $postLabel->save();
  188. }
  189. }
  190. }
  191.  
  192. //создаем массив меток, СВЯЗАННЫХ с постом
  193. $arr_labels = ArrayHelper::map($this->labels, 'id', 'id');
  194. if($this->labels_array) {
  195. foreach($this->labels_array as $one)
  196. {
  197. if(!in_array($one, $arr_labels))
  198. {
  199. $model = new PostLabel();
  200. $model->post_id = $this->id;
  201. $model->label_id = $one;
  202. $model->save();
  203. }
  204. if(isset($arr_labels[$one])) {
  205. unset($arr_labels[$one]);
  206. }
  207. PostLabel::deleteAll(['label_id' => $arr_labels, 'post_id' =>$this->id]);
  208. }
  209. } else {
  210. PostLabel::deleteAll(['label_id' => $arr_labels, 'post_id' =>$this->id]);
  211. }
  212.  
  213.  
  214. ///////////////Save new Tags
  215. $all_tags = ArrayHelper::map(Tag::find()->all(), 'id', 'id');
  216. if($this->tags_array) {
  217. foreach($this->tags_array as $one)
  218. {
  219. if(!in_array($one, $all_tags))
  220. {
  221. $model = new Tag();
  222. $model->title = $one;
  223. $model->save();
  224. $postTag = new PostTag();
  225. $postTag->id_post = $this->id;
  226. $postTag->id_tag = $model->id;
  227. $postTag->save();
  228. }
  229. }
  230. }
  231. /////////////////////
  232.  
  233. //создаем массив тегов, связанных с постом
  234. $arr = ArrayHelper::map($this->tags, 'id', 'id');
  235. if($this->tags_array) {
  236. foreach($this->tags_array as $one) {
  237. if(!in_array($one, $arr)) {
  238. $model = new PostTag();
  239. $model->id_post = $this->id;
  240. $model->id_tag = $one;
  241. $model->save();
  242. }
  243. if(isset($arr[$one])) {
  244. unset($arr[$one]);
  245. }
  246. PostTag::deleteAll(['id_tag' => $arr, 'id_post' =>$this->id]);
  247. }
  248. } else {
  249. PostTag::deleteAll(['id_tag' => $arr, 'id_post' =>$this->id]);
  250. }
  251.  
  252. }
  253.  
  254. public function createNewTag($new_tag) {
  255. if(!$tag = Tag::find()->andWhere(['title'=>$new_tag])->one()) {
  256. $tag = new Tag();
  257. $tag->title = $new_tag;
  258. if(!$tag->save()){
  259. $tag = null;
  260. }
  261. }
  262. if($tag != null){ //if($tag != null)
  263. $post_tag = new PostTag();
  264. $post_tag->id_post = $this->id;
  265. $post_tag->id_tag = $tag->id;
  266. if($post_tag->save())
  267. return $post_tag->id;
  268. }
  269. return false;
  270. }
  271.  
  272. /**
  273. * {@inheritdoc}
  274. */
  275. public static function tableName()
  276. {
  277. return 'post';
  278. }
  279.  
  280. /**
  281. * {@inheritdoc}
  282. */
  283. public function rules()
  284. {
  285. return [
  286. [['title', 'content', 'author'], 'required'],
  287. [['content'], 'string'],
  288. [['view', 'category'], 'integer'],
  289. [['title', 'thumbnail', 'url', 'author'], 'string', 'max' => 255],
  290. [['images'], 'file', 'extensions' => 'png,jpg,jpeg', 'skipOnEmpty' => true, 'maxFiles' => 6],
  291. [['tags_array'], 'safe'],
  292. [['labels_array'], 'safe'],
  293. ];
  294. }
  295.  
  296. /**
  297. * {@inheritdoc}
  298. */
  299. public function attributeLabels()
  300. {
  301. return [
  302. 'id' => 'ID',
  303. 'title' => 'Название',
  304. 'content' => 'Content',
  305. 'url' => 'Url',
  306. 'author' => 'Author',
  307. 'view' => 'View',
  308. 'category' => 'Category',
  309. 'tags_array' => 'Теги',
  310. 'labels_array' => 'Метки',
  311. ];
  312. }
  313.  
  314. public function uploadGallery()
  315. {
  316. if($this->validate())
  317. {
  318. foreach($this->images as $file)
  319. {
  320. $path = 'images/store' . $file->baseName . '.' . $file->extension;
  321. $file->saveAs($path);
  322. $this->attachImage($path, false);
  323. @unlink($path);
  324. }
  325. return true;
  326. }
  327. else
  328. {
  329. return false;
  330. }
  331. }
  332. }
Add Comment
Please, Sign In to add comment