/** * This is the default 'index' action that is invoked * when an action is not explicitly requested by users. */ public function actionIndex() { // lets get the Wine Of The Day... // instantiate a wines model, // get the sysconfig // see if we already picked a wotd for today // if we did, just set the wotd to the value in sysConfig // if not, select a random wine // pass the wotd to the index view $sysConfig = new Systemconfig(); $currentSys = $sysConfig->findByPk(1); Yii::app()->session['version'] = $currentSys->version; $allWines = new Wines(); $currentDateTime = new DateTime("now", new DateTimeZone('America/Chicago')); $wotdDateTime = new DateTime($currentSys->wineOfTheDay_dt, new DateTimeZone('America/Chicago')); $diff = $currentDateTime->diff($wotdDateTime); if ($diff->days > 0) { $wineOfTheDay = $allWines->getRandomWine(); $currentSys->wineOfTheDay_id = $wineOfTheDay->id; $currentSys->wineOfTheDay_dt = $currentDateTime->format('Y-m-d H:i:s'); $currentSys->save(); } else { $wineOfTheDay = $allWines->findByPk($currentSys->wineOfTheDay_id); if (!isset($wineOfTheDay)) { $wineOfTheDay = $allWines->getRandomWine(); $currentSys->wineOfTheDay_id = $wineOfTheDay->id; $currentSys->wineOfTheDay_dt = $currentDateTime->format('Y-m-d H:i:s'); $currentSys->save(); } } // renders the view file 'protected/views/site/index.php' // using the default layout 'protected/views/layouts/main.php' $this->render('index', array('wineRecord' => $wineOfTheDay)); }
/** * Default view all wineries */ public function actionCreate() { $wineList = Wines::model()->findByPk($id); $this->renderOutput($wineList); }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return Wines the loaded model * @throws CHttpException */ public function loadModel($id) { $model = Wines::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
/** * @return string the associated database table name */ public function refreshRating($wine_id) { $newRatingAvg = 0; $currWine = Wines::model()->findByPk($wine_id); $wineInCellars = new CellarWines(); $this->overall_rating = $newRatingAvg; // propogate the result of saving to the calling code return $currWine->save(); }
/** * Add an existing wine into the cellar * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the wine to be added */ public function actionAddWine($wine_id) { $model = new CellarWines(); $currentDateTime = new DateTime("now", new DateTimeZone('America/Chicago')); if (isset($_POST['CellarWines'])) { $model->attributes = $_POST['CellarWines']; $model->cellar_id = Yii::app()->session['cellar_id']; $model->wine_id = $wine_id; $model->create_dt = $currentDateTime->format('Y-m-d H:i:s'); $model->update_dt = $currentDateTime->format('Y-m-d H:i:s'); if ($model->save()) { $this->redirect(array('index', 'cellar_id' => $model->cellar_id)); } } else { $selectedWine = Wines::model()->findByPk($wine_id); $model->cellar_id = Yii::app()->session['cellar_id']; $model->wine_id = $wine_id; $model->quantity = 0; $model->rating = $selectedWine->overall_rating; $model->cellar_loc_id = 1; $model->create_dt = $currentDateTime->format('Y-m-d H:i:s'); $model->update_dt = $currentDateTime->format('Y-m-d H:i:s'); } $this->render('update', array('model' => $model)); // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser // if (!isset($_GET['ajax'])) // { // $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('update', 'id' => $model->id)); // } }