public function post() { if ($this->validate()) { $image = new PhotoActiveRecord(); $image->userId = Yii::$app->user->identity->id; $image->name = $this->name . ''; $image->photo = file_get_contents($this->file->tempName); $image->posted = date('Y-m-d H-i-s'); $imagick = new \Imagick(); $imagick->readImageBlob($image->photo); $size = $imagick->getImageGeometry(); if ($size['width'] > 800) { foreach ($imagick as $frame) { $frame->thumbnailImage(800, 0); } } $image->thumbnail = $imagick->getImagesBlob(); if (!$image->save()) { return false; } $tags = split(',', $this->tags); foreach ($tags as $item) { if ($item != '') { $tag = new TagsActiveRecord(); $tag->photo = $image->id; $tag->tag = trim($item); if (!$tag->save()) { return false; } } } return true; } return false; }
public function search() { switch ($this->type) { case 'name': return PhotoActiveRecord::find()->where(['like', 'name', $this->text])->orderBy(['posted' => SORT_DESC])->asArray(); case 'tag': $tags = TagsActiveRecord::find()->where(['like', 'tag', $this->text])->select(['photo'])->asArray(); return PhotoActiveRecord::find()->where(['in', 'id', $tags])->orderBy(['posted' => SORT_DESC])->asArray(); case 'users': return (new Query())->from('user')->where(['like', 'fio', $this->text])->join('left join', 'photo', 'user.photo = photo.id')->select(['id' => 'user.id', 'thumbnail' => 'photo.thumbnail', 'name' => 'fio', 'username', 'pol', 'email', 'registerDate'])->orderBy(['registerDate' => SORT_DESC]); case 'tags': return TagsActiveRecord::find()->where(['like', 'tag', $this->text])->select(['id', 'tag'])->distinct()->asArray(); case 'comments': return (new Query())->from('comments')->join('join', 'user', 'comments.userId = user.id')->select(['id' => 'comments.id', 'photoId', 'body', 'created', 'fio'])->orderBy(['created' => SORT_DESC]); } return null; }
public function actionDeletetag() { if (Yii::$app->request->isPost && !Yii::$app->user->isGuest && Yii::$app->user->identity->role == 'admin') { $tag = Yii::$app->request->post('tag'); if ($tag != null) { $tag = TagsActiveRecord::findAll(['tag' => $tag]); foreach ($tag as $item) { if (!$item->delete()) { return false; } } return true; } } return false; }
public static function photoTags($photoId) { return TagsActiveRecord::find()->select(['tag'])->where(['photo' => $photoId])->asArray()->all(); }