public function updatePhotoCount($id) { $quuery = Photo::find()->select('count(1) as photo_count')->andWhere(['album_id' => $id])->asArray()->one(); $model = Album::findOne($id); $model->photo_count = $quuery['photo_count']; $model->save(); }
/** * Deletes an existing Album model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { try { $this->findModel($id)->delete(); Photo::deleteAll('album_id = :album_id', [':album_id' => $id]); RecommendQueue::deleteAlbumById($id); } catch (\Exception $exp) { echo Json::encode(['statusCode' => '300', 'message' => $exp->getMessage(), 'navTabId' => 'photo_album_index_id', 'forwardUrl' => '', 'callbackType' => '']); exit; } echo Json::encode(['statusCode' => '200', 'message' => '操作成功', 'navTabId' => 'photo_album_index_id', 'forwardUrl' => '', 'callbackType' => '']); exit; }
public function search($params) { $query = (new Query())->select('a.*')->addSelect('b.title as albumTitle')->from(Photo::tableName() . ' as a')->leftJoin(['b' => Album::tableName()], 'a.album_id = b.id'); $this->load($params); if (!$this->validate()) { $query->where('0=1'); } else { $query->where('1=1'); $query->andFilterWhere(['id' => $this->id, 'album_id' => $this->album_id, 'recs_count' => $this->recs_count, 'liked_count' => $this->liked_count, 'prev_photo' => $this->prev_photo, 'next_photo' => $this->next_photo, 'created_by' => $this->created_by, 'created_at' => $this->created_at, 'updated_by' => $this->updated_by, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'alt', $this->alt])->andFilterWhere(['like', 'author', $this->author])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'icon', $this->icon])->andFilterWhere(['like', 'cover', $this->cover])->andFilterWhere(['like', 'thumb', $this->thumb])->andFilterWhere(['like', 'image', $this->image])->andFilterWhere(['like', 'privacy', $this->privacy])->andFilterWhere(['like', 'can_reply', $this->can_reply])->andFilterWhere(['like', 'desc', $this->desc])->andFilterWhere(['like', 'is_album_cover', $this->is_album_cover]); } $sql = $query->createCommand()->getRawSql(); $countQuery = $query->select('count(*)'); $count = $countQuery->createCommand()->queryScalar(); $defaultOrder = ['created_at' => SORT_DESC]; if (!empty($params['orderField'])) { $defaultOrder = [$params['orderField'] => $params['orderDirection'] == 'asc' ? SORT_ASC : SORT_DESC]; } $dataProvider = new SqlDataProvider(['sql' => $sql, 'totalCount' => $count, 'sort' => ['defaultOrder' => $defaultOrder, 'attributes' => array_values($this->attributes())], 'pagination' => ['pageSize' => 20, 'page' => isset($params['pageNum']) ? $params['pageNum'] - 1 : '0']]); return $dataProvider; }
/** * 保存后进行处理 * 写入照片的上一页,下一页 * * @param bool $insert * @param array $changedAttributes */ public static function updatePrevNextAndCover($album_id) { if (!empty($album_id)) { $photos = Photo::find()->andWhere(['album_id' => $album_id])->all(); $albumCoverPhoto = null; if (!empty($photos)) { foreach ($photos as $key => $val) { if ($val->is_album_cover == 1) { $albumCoverPhoto = $val; } $prev_photo = Photo::getPhotoHasPrev($key, $photos); $next_photo = Photo::getPhotoHasNext($key, $photos); if ($prev_photo == $val->prev_photo && $next_photo == $val->next_photo) { continue; } else { $Photo = Photo::findOne($val->id); $Photo->prev_photo = $prev_photo; $Photo->next_photo = $next_photo; $Photo->save(); } } } if (empty($albumCoverPhoto)) { $albumCoverPhoto = array_shift($photos); } $Album = Album::findOne($album_id); $Album->image = $albumCoverPhoto->image; $Album->cover = $albumCoverPhoto->cover; $Album->thumb = $albumCoverPhoto->thumb; $Album->icon = $albumCoverPhoto->icon; $Album->save(); } }
/** * Finds the Photo model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Photo the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Photo::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * 保存批量提交申请 * @param $album_id */ public function actionSaveBatchCreate() { try { $connection = \Yii::$app->db; $transaction = $connection->beginTransaction(); $post = Yii::$app->request->post(); $album_id = $post['Photo']['album_id']; $is_album_cover = isset($post['Photo']['is_album_cover']) ? $post['Photo']['is_album_cover'] : ''; if (!empty($post['Photo']['img_url'])) { foreach ($post['Photo']['img_url'] as $key => $img_url) { if (empty($img_url)) { continue; } //判断传值的图片文件是否存在 $img_path = $_SERVER['DOCUMENT_ROOT'] . $img_url; $full_path = $this->getFullPath(); $absolute_path = $_SERVER['DOCUMENT_ROOT'] . $full_path; if (!file_exists($img_path)) { throw new \Exception('图片文件不存在'); } if (!file_exists($absolute_path) && !mkdir($absolute_path, 0777, true)) { throw new \Exception('ERROR_CREATE_DIR'); } else { if (!is_writeable($absolute_path)) { throw new \Exception('ERROR_DIR_NOT_WRITEABLE'); } } $imageSize = Image::getImagine()->open($img_path)->getSize(); $imageFormat = $this->getFullSize($imageSize->getWidth(), $imageSize->getHeight()); $Photo = new Photo(); $Photo->album_id = $album_id; $Photo->desc = $post['Photo']['desc'][$key]; $Photo->title = $post['Photo']['title'][$key]; if ($is_album_cover == $img_url) { Photo::updateAll(['is_album_cover' => '0'], 'album_id = ' . $album_id); $Photo->is_album_cover = '1'; } if (!empty($imageFormat)) { foreach ($imageFormat as $key => $val) { $full_name = $this->getFullName($this->getFileName($img_path)); //移动图片到指定目录 //生成缩略文件 Image::thumbnail($img_path, $val[0], $val[1])->save($absolute_path . $full_name); $Photo->{$key} = $full_path . $full_name; } } $Photo->save(); } } Photo::updatePrevNextAndCover($album_id); $transaction->commit(); } catch (\Exception $exp) { $transaction->rollBack(); $message = ''; $result = ['statusCode' => '300', 'message' => $exp->getMessage(), 'navTabId' => 'photo_photo_batch_create_id', 'forwardUrl' => '', 'callbackType' => '']; echo Json::encode($result); exit; } $result = ['statusCode' => '200', 'message' => '操作成功', 'navTabId' => 'photo_photo_index_id', 'forwardUrl' => '', 'callbackType' => 'closeCurrent']; echo Json::encode($result); exit; }