/**
  * Displays the map with the availiable screens
  */
 public function actionMap()
 {
     $model = Screen::model()->with('geocodes', 'yeshuv', 'commercials')->findAll();
     $array = array();
     date_default_timezone_set('Asia/Jerusalem');
     $now = new DateTime();
     $now = $now->modify("-10 minutes");
     $id = 0;
     $green = 0;
     foreach ($model as $m) {
         $color = "red";
         $screenAjax = ScreenAjax::model()->findByPk($m->id);
         if ($screenAjax) {
             $last = strtotime($screenAjax->last_date);
             if ($last > $now->getTimestamp()) {
                 $color = "green";
                 $green++;
             }
         }
         if (count($m->geocodes) < 1) {
             $geocode = new Geocode();
             $location = Screen::model()->geocodeLookup($m->name . ',' . $m->yeshuv->name_heb);
             $geocode->lat = $location['lat'];
             $geocode->lng = $location['lng'];
             if ($geocode->save()) {
                 $screenGeocode = new ScreenGeocodeAssignment();
                 $screenGeocode->screen_id = $m->id;
                 $screenGeocode->geocode_id = $geocode->id;
                 $screenGeocode->save();
             }
             $marker = array('id' => $id++, 'name' => $m->name . ', ' . $m->yeshuv->name_heb, 'url' => $m->webkey_nickname, 'lat' => $geocode->lat, 'lng' => $geocode->lng, 'commercial' => $m->commercials[0]->name, 'color' => $color);
             array_push($array, $marker);
         } else {
             $marker = array('id' => $id++, 'name' => $m->name . ', ' . $m->yeshuv->name_heb, 'url' => $m->webkey_nickname, 'lat' => $m->geocodes[0]->lat, 'lng' => $m->geocodes[0]->lng, 'commercial' => $m->commercials[0]->name, 'color' => $color);
             array_push($array, $marker);
         }
     }
     $this->render('map', array('markers' => $array, 'green' => $green));
 }
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     if (!Yii::app()->user->checkAccess('screenUpdate')) {
         throw new CHttpException(403, 'You are not authorized to
         per-form this action.');
     }
     $model = $this->loadModel($id);
     $weather = $model->weatherCodes;
     $commercial = $model->commercials;
     $theme = $model->themes;
     $geocode = $model->geocodes;
     if (count($weather) == 0) {
         $weather = new Weather();
         $screenWeather = null;
     } else {
         $weather = $weather[0];
         $screenWeather = ScreenWeather::model()->findByAttributes(array('screen_id' => $model->id, 'code' => $weather->code));
     }
     if (count($commercial) == 0) {
         $commercial = new Commercial();
         $screenCommercial = null;
     } else {
         $commercial = $commercial[0];
         $screenCommercial = ScreenCommercialAssignment::model()->findByAttributes(array('screen_id' => $model->id, 'commercial_id' => $commercial->id));
     }
     if (count($theme) == 0) {
         $theme = new Theme();
         $screenTheme = null;
     } else {
         $theme = $theme[0];
         $screenTheme = ScreenThemeAssignment::model()->findByAttributes(array('screen_id' => $model->id, 'theme_id' => $theme->id));
     }
     if (count($geocode) == 0) {
         $geocode = new Geocode();
         $screenGeocode = null;
     } else {
         $geocode = $geocode[0];
         $screenGeocode = ScreenGeocodeAssignment::model()->findByAttributes(array('screen_id' => $model->id, 'geocode_id' => $geocode->id));
     }
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Screen'])) {
         $model->attributes = $_POST['Screen'];
         if ($model->save()) {
             if (isset($_POST['Weather'])) {
                 if ($screenWeather == null) {
                     $screenWeather = new ScreenWeather();
                     $screenWeather->screen_id = $model->id;
                 }
                 $screenWeather->attributes = $_POST['Weather'];
                 if ($screenWeather->save()) {
                     Yii::app()->user->setFlash('success', Yii::t('screen', 'Weather code assigned successfully.'));
                 } else {
                     Yii::app()->user->setFlash('error', Yii::t('screen', 'Could not assign the Weather Code to the Screen.'));
                 }
             }
             if (isset($_POST['Commercial'])) {
                 if ($screenCommercial == null) {
                     $screenCommercial = new ScreenCommercialAssignment();
                     $screenCommercial->screen_id = $model->id;
                 }
                 $commercial->attributes = $_POST['Commercial'];
                 $screenCommercial->commercial_id = $commercial->id;
                 if ($screenCommercial->save()) {
                     Yii::app()->user->setFlash('success', Yii::t('screen', 'Commercial Area assigned successfully.'));
                 } else {
                     Yii::app()->user->setFlash('error', Yii::t('screen', 'Could not assign the Commercial Area to the Screen.'));
                 }
             }
             if (isset($_POST['Theme'])) {
                 if ($screenTheme == null) {
                     $screenTheme = new ScreenThemeAssignment();
                     $screenTheme->screen_id = $model->id;
                 }
                 $theme->attributes = $_POST['Theme'];
                 $screenTheme->theme_id = $theme->id;
                 if ($screenTheme->save()) {
                     Yii::app()->user->setFlash('success', Yii::t('screen', 'Theme assigned successfully.'));
                 } else {
                     Yii::app()->user->setFlash('error', Yii::t('screen', 'Could not assign the Theme to the Screen.'));
                 }
             }
             // Create geocode and insert the lng and lat and assign it to the screen
             $location = $model->geocodeLookup($model->name . ',' . $model->yeshuv->name_heb);
             $geocode->lat = $location['lat'];
             $geocode->lng = $location['lng'];
             if ($geocode->save() && $screenGeocode == null) {
                 $screenGeocode = new ScreenGeocodeAssignment();
                 $screenGeocode->screen_id = $model->id;
                 $screenGeocode->geocode_id = $geocode->id;
                 $screenGeocode->save();
             }
             Yii::app()->user->setFlash('success', Yii::t('screen', 'Screen updated successfully.'));
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('update', array('model' => $model, 'weather' => $weather, 'commercial' => $commercial, 'theme' => $theme));
 }