/**
  * Create a new Geometry with a Zone or add a Zone to an existing Geometry
  * @param string $id string representing the integer id of the Geometry
  */
 public function actionCreate($id = null)
 {
     if (!empty($id)) {
         //already exist a geometry
         $geom_model = WaterRequestGeometries::model()->findByPk($id);
         if (!$geom_model) {
             echo CJSON::encode(array('status' => 'error_iniziale'));
             return;
         }
     }
     if (isset($_POST['WaterRequestGeometries']) && isset($_POST['WaterRequestGeometryZones'])) {
         if (!isset($geom_model)) {
             //create new geometry model
             $geom_model = new WaterRequestGeometries();
             //and store post data into model
             $geom_model->attributes = $_POST['WaterRequestGeometries'];
             //$geom_model->geom=new CDbExpression(Geometry::Transform(Geometry::ST_GeomFromText($_POST['WaterRequestGeometries']['geom'])));
             $geom_model->geom = new CDbExpression(Geometry::ST_GeomFromText($_POST['WaterRequestGeometries']['geom'], 900913));
         }
         /*
         if (((isset($geom_model->wr)) && ($geom_model->wr->phase==2)) && ((isset($geom_model->zones)) && (count($geom_model->zones)>1))) {
         	echo CJSON::encode(array('status'=>'cannot add another zone to this geometry'));
         	return;
         }
         */
         //create new geometry zone model
         $geom_zone_model = new WaterRequestGeometryZones();
         $geom_zone_model->attributes = $_POST['WaterRequestGeometryZones'];
         //create new geometry zone property model
         //$geom_zone_property_model=new WaterRequestGeometryZoneProperties;
         //$geom_zone_property_model->attributes=$_POST['WaterRequestGeometryZoneProperties'];
         if (isset($_POST['WaterRequestGeometryZoneProperties'])) {
             if (isset($_POST['WaterRequestGeometryZoneProperties']['ae_choice'])) {
                 $ae = $_POST['WaterRequestGeometryZoneProperties']['ae_choice'];
             } else {
                 $ae = false;
             }
             foreach ($_POST['WaterRequestGeometryZoneProperties'] as $param => $value) {
                 if ($param != 'ae_choice' && !empty($value)) {
                     $new_prop = new WaterRequestGeometryZoneProperties();
                     $new_prop->parameter = $param;
                     $new_prop->value = $value;
                     // the prop is selected
                     if ($ae == $param) {
                         $new_prop->use4ae = true;
                     }
                     // append to array
                     $sent_props[] = $new_prop;
                 }
             }
             if (!isset($sent_props)) {
                 echo CJSON::encode(array('status' => 'error no props sent'));
                 return;
             }
             // TODO: Controllare che la zona necessiti di almeno uno selezionato
             // check if one is selected, otherwise select the first one.
             $choosedProp = -1;
             for ($i = 0; $i < count($sent_props) && $choosedProp < 0; $i++) {
                 if ($sent_props[$i]->use4ae) {
                     $choosedProp = $i;
                 }
             }
             /*
             // 'cp' not found, set the first prop.
             if($choosedProp<0){
             	$sent_props[0]->use4ae = true;
             	$choosedProp=0;  
             }
             
             
             // Check if use4ae is needed to be set to a specific prop
             if($geom_zone_model->zone_name)
             foreach($sent_props as $geom_zone_property_model){
             	$geom_zone_property_model->geometry_zone=$geom_zone_model->id;
             	if ($geom_zone_property_model->()) {
             		//Yii::log("Zone_name $geom_zone_model->zone_name", CLogger::LEVEL_INFO, "LOOP");
             		//$geom_zone_property_model->save();	
             		//echo $geom_zone_property_model->parameter;  // DEBUG
             	}else
             		$_propserror = true;
             }
             */
         } else {
             $sent_props = array();
         }
         //starting new transaction
         $transaction = Yii::app()->db->beginTransaction();
         try {
             $_error = true;
             if ($geom_model->validate()) {
                 //saving geom_model
                 $geom_model->save();
                 //connect geom_zone_model to geom_model through id
                 $geom_zone_model->wr_geometry_id = $geom_model->id;
                 /*
                 					var_dump($geom_zone_model->wr_geometry_id);
                 					$geom_zone_model->geometry=$geom_model;
                 					var_dump($geom_model->id);
                 					var_dump($geom_zone_model->wr_geometry_id);*/
                 //calculate pe and water demand
                 //$geom_zone_model->updatePEAndWD($sent_props[$choosedProp]->parameter, $sent_props[$choosedProp]->value);
                 /*
                 $geom_zone_model->pe = $geom_zone_model->calculatePE($sent_props[$choosedProp]->parameter, $sent_props[$choosedProp]->value);
                 
                 $geom_zone_model->water_demand = $geom_zone_model->calculateWaterDemand();
                 */
                 //$geom_zone_model->updateWD();
                 if ($geom_zone_model->validate()) {
                     //saving geom_zone_model
                     Yii::log('SAVING GEOM ZONE MODEL', CLogger::LEVEL_INFO, 'actionCreate');
                     // DEBUG
                     $geom_zone_model->save();
                     $_propserror = false;
                     $error_status = 'ok';
                     foreach ($sent_props as $geom_zone_property_model) {
                         $geom_zone_property_model->geometry_zone = $geom_zone_model->id;
                         //$geom_zone_property_model->zone = $geom_zone_model;
                         if ($geom_zone_property_model->validate()) {
                             Yii::log('SAVING PROPERTY MODEL ', CLogger::LEVEL_INFO, 'actionCreate');
                             // DEBUG
                             $geom_zone_property_model->save();
                             //echo $geom_zone_property_model->parameter;  // DEBUG
                         } else {
                             //Yii::log('PROPERTY MODEL NON VALIDA' , CLogger::LEVEL_INFO, 'actionCreate');  // DEBUG
                             //Yii::log(print_r($geom_zone_property_model->attributes, true) , CLogger::LEVEL_INFO, 'actionCreate');  // DEBUG
                             //Yii::log(print_r($geom_zone_property_model->getErrors(), true) , CLogger::LEVEL_INFO, 'actionCreate');  // DEBUG
                             $error_status = 'Error: ';
                             foreach ($geom_zone_property_model->getErrors() as $attr => $err_msg) {
                                 $error_status = $error_status . $attr . ': ' . implode(',', $err_msg) . ' ';
                             }
                             $_propserror = true;
                         }
                     }
                     if ($_propserror) {
                         echo CJSON::encode(array('status' => $error_status));
                     }
                     $_error = $_propserror;
                     //echo 'after foreach: '.$_propserror.' ';  // DEBUG
                 } else {
                     $error_status = 'Error: ';
                     foreach ($geom_zone_model->getErrors() as $attr => $err_msg) {
                         $error_status = $error_status . $attr . ': ' . implode(',', $err_msg) . ' ';
                     }
                     echo CJSON::encode(array('status' => $error_status));
                     // DEBUG
                     $_error = true;
                 }
             } else {
                 echo CJSON::encode(array('status' => ' geom_model non valida '));
             }
             // DEBUG
             if ($_error) {
                 $transaction->rollBack();
                 Yii::log('ROLLBACK!', CLogger::LEVEL_INFO);
                 //echo CJSON::encode(array('status'=>'Rollback error == TRUE ('.$_error.')'));
             } else {
                 $transaction->commit();
                 /*
                 $wr = $geom_model->wr;
                 Yii::app()->controller->redirect(array('/waterRequests/update', 'id'=>$wr->id));
                 */
                 echo CJSON::encode(array('status' => 'ok'));
                 Yii::app()->end();
             }
         } catch (Exception $e) {
             $transaction->rollBack();
             echo CJSON::encode(array('status' => 'not inserted ' . $e));
         }
     } else {
         Yii::log('Something went wrong Geom=' . isset($_POST['WaterRequestGeometries']) . ' Zone=' . isset($_POST['WaterRequestGeometryZones']) . ' Property=' . isset($_POST['WaterRequestGeometryZoneProperties']), CLogger::LEVEL_INFO);
     }
     Yii::app()->end();
 }
示例#2
0
 protected function beforeDelete()
 {
     $geom = WaterRequestGeometries::model()->findAll('wr_id = :wr_id', array(':wr_id' => $this->id));
     if ($geom) {
         foreach ($geom as $g) {
             $geom_zone = WaterRequestGeometryZones::model()->findAll('wr_geometry_id = :wr_geometry_id', array(':wr_geometry_id' => $g->id));
             if ($geom_zone) {
                 foreach ($geom_zone as $z) {
                     WaterRequestGeometryZoneProperties::model()->deleteAll('geometry_zone = :geometry_zone', array(':geometry_zone' => $z->id));
                 }
                 WaterRequestGeometryZones::model()->deleteAll('wr_geometry_id = :wr_geometry_id', array(':wr_geometry_id' => $g->id));
             }
         }
         WaterRequestGeometries::model()->deleteAll('wr_id = :wr_id', array(':wr_id' => $this->id));
     }
     return parent::beforeDelete();
 }
 /**
  * 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 the ID of the model to be loaded
  */
 public function loadModel($id)
 {
     $model = WaterRequestGeometries::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }