Exemplo n.º 1
0
 /**
  * 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));
 }