public function actionIndex() { //Get the wotd $wotd = AppConfig::findOne(1); $allWines = new Wines(); $currentDateTime = new \DateTime("now", new \DateTimeZone('America/Chicago')); $wotdDateTime = new \DateTime($wotd->wotd_dt, new \DateTimeZone('America/Chicago')); $diff = $currentDateTime->diff($wotdDateTime); if ($diff->days > 0) { $wineOfTheDay = $allWines->getRandomWine(Yii::$app->params['wotdMinRating']); $wotd->wotd_id = $wineOfTheDay->id; $wotd->wotd_dt = $currentDateTime->format('Y-m-d H:i:s'); $wotd->save(); } else { $wineOfTheDay = $allWines->findOne($wotd->wotd_id); if (!isset($wineOfTheDay)) { $wineOfTheDay = $allWines->getRandomWine(Yii::$app->params['wotdMinRating']); $wotd->wotd_id = $wineOfTheDay->id; $wotd->wotd_dt = $currentDateTime->format('Y-m-d H:i:s'); $wotd->save(); } } // renders the view file 'protected/views/site/index.php' // using the default layout 'protected/views/layouts/main.php' return $this->render('index', ['wineRecord' => $wineOfTheDay]); }
/** * @return a wine CA record with a random wine selected with an overall * rating of $min_rating or greater */ public function getRandomWine($min_rating) { $wineIDList = (new \yii\db\query())->select(['id'])->from('wines')->all(); $wineID = $wineIDList[rand(0, sizeOf($wineIDList) - 1)]; $wotd = Wines::findOne($wineID); if (!isset($wotd)) { return NULL; } else { return $wotd; } }
/** * Finds the Wines model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Wines the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Wines::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }