public function actionDelete($id) { News::findOne($id)->delete(); // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser if (!isset($_GET['ajax'])) { return $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index')); } }
/** * Finds the News model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return News the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = News::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionEdit($id) { if (User::isAdmin(Yii::$app->user->identity->username)) { $value = News::findOne($id); $model = new News(); if (Yii::$app->request->isPost) { $item = Yii::$app->request->post('News'); if (News::updateAll(['title' => $item['title'], 'description' => $item['description'], 'image' => ''], ['id' => $id])) { return $this->redirect('/news/view'); } else { throw new ForbiddenHttpException('Ошибка обновления новости!', 404); } } return $this->render('edit', compact('model', 'value')); } else { throw new ForbiddenHttpException('У вас нет прав администратора!', 404); } }
public function actionIndex($id, $title) { $this->layout = 'category'; $news = News::findOne($id); if (!$news) { $this->redirect("/"); } $breadCrumbs = []; $breadCrumbs[] = ['label' => 'Главная', 'url' => '/']; $breadCrumbs[] = ['label' => 'Все новости', 'url' => '/category/all']; $breadCrumbs[] = ['label' => $news->title]; Yii::$app->view->title = $news->title; Yii::$app->view->registerMetaTag(['name' => 'keywords', 'content' => 'новости, свежие новости, новости украины, новости сегодня, агрегатор новостей']); Yii::$app->view->registerMetaTag(['name' => 'description', 'content' => Html::encode($news->getShort())], 'description'); Yii::$app->view->registerMetaTag(['name' => 'og:title', 'content' => Yii::$app->view->title], 'og:title'); Yii::$app->view->registerMetaTag(['name' => 'og:image', 'content' => $news->getThumbLink()], 'og:image'); Yii::$app->view->registerMetaTag(['name' => 'og:description', 'content' => Html::encode($news->getShort())], 'og:description'); Yii::$app->view->registerMetaTag(['name' => 'og:url', 'content' => $news->getLink()], 'og:url'); return $this->render('index', ['news' => $news, 'breadcrumbs' => $breadCrumbs]); }
public static function processMessage($msg) { $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); $params = json_decode($msg->body); print_r($params); try { $news = News::findOne(['id' => $params->news_id]); $dirPath = Yii::getAlias('@frontend') . '/web/uploads/' . date("Y", strtotime($news->created_at)) . '/' . date("m", strtotime($news->created_at)) . "/" . date("d", strtotime($news->created_at)) . "/" . $params->news_id . "/"; if (!file_exists($dirPath)) { mkdir($dirPath, 0777, true); } if ($tmpFile = PageLoaderComponent::loadFile($params->src)) { $originFile = $dirPath . "origin"; copy($tmpFile, $originFile); unlink($tmpFile); echo "Origin file: {$originFile}" . PHP_EOL; if ($handle = @fopen($originFile, 'r')) { try { if (file_exists($originFile) && ($data = @getimagesize($originFile))) { $image = new ImageEditor(); $image->load($originFile); foreach (Yii::$app->params['image_sizes'] as $title => $size) { $image->softThumb($size['width'], $size['height'], $dirPath . $title . ".png"); } } else { throw new \Exception("Origin file isn't an image", 400); } } catch (Exception $e) { echo "Error: {$e->getMessage()}" . PHP_EOL; } fclose($handle); } else { throw new \Exception("Can't open origin file", 400); } } } catch (\Exception $e) { echo $e->getMessage(); } }
protected function findNews($id) { if (($model = \common\models\News::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('你访问的页面不存在!'); } }
public function actionError() { if (strpos(Yii::$app->request->url, "uploads")) { $aUrl = explode("/", Yii::$app->request->url); if (isset($aUrl[5])) { if ($news = News::findOne($aUrl[5])) { if ($news->thumb) { $mq = new RabbitMQComponent(); $mq->postMessage("image", "image", json_encode(["news_id" => $news->id, "src" => $news->thumb])); $this->redirect($news->thumb); } else { if ($giData = PageLoaderComponent::load("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" . urlencode($news->title) . "&userip=127.0.0.1&imgsz=large")) { $data = json_decode($giData); if (isset($data->responseData->results[0])) { $news->thumb = $data->responseData->results[0]->unescapedUrl; $news->save(); $mq = new RabbitMQComponent(); $mq->postMessage("image", "image", json_encode(["news_id" => $news->id, "src" => $data->responseData->results[0]->unescapedUrl])); $this->redirect($data->responseData->results[0]->unescapedUrl); } } } } } } $this->redirect("/"); }
protected function checkNewsThumb($news_id, $pn_id) { $news = News::findOne($news_id); if (!$news->thumb) { $pn = PendingNews::findOne($pn_id); $news->thumb = $pn->thumb_src; $news->save(); $mq = new RabbitMQComponent(); $mq->postMessage("image", "image", json_encode(["news_id" => $news->id, "src" => $news->thumb])); } }
public static function processMessage($msg) { $params = json_decode($msg->body); print_r($params); try { $news = News::findOne($params->news_id); $text = mb_substr($news->title, 0, 140 - strlen(Yii::$app->params['domainName'] . $news->getLink()), 'utf-8'); $text .= " " . Yii::$app->params['domainName'] . $news->getLink(); Codebird::setConsumerKey(Yii::$app->params['twitter']['consumer_key'], Yii::$app->params['twitter']['consumer_secret']); $cb = Codebird::getInstance(); $cb->setToken(Yii::$app->params['twitter']['access_token'], Yii::$app->params['twitter']['access_token_secret']); $params = array('status' => $text, 'media[]' => $params->src); $reply = $cb->statuses_updateWithMedia($params); } catch (Exception $e) { echo $e->getMessage(); } $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); }