/** * 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)); }