/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     if (!Yii::app()->user->checkAccess('giveEvaluation')) {
         //throw new CHttpException(403,Yii::t('http_status', '403'));
         Yii::app()->user->setState('redirect', Yii::app()->request->requestUri);
         $this->redirect(array('/site/login'));
     }
     $model_quality = new WaterQualityOpinions();
     $model_fault = new WaterFaultOpinions();
     if (isset($_POST) && !empty($_POST)) {
         var_dump($_POST);
         if (empty($_POST['geom'])) {
             Yii::app()->user->setFlash('warning', 'ATTENTION: set a point on the map.');
         } else {
             if ($_POST['geom_type'] == 1) {
                 if (!empty($_POST['qualities_list'])) {
                     $model = new WaterQualityOpinions();
                     $model->quality = $_POST['qualities_list'];
                     //$model->geom = new CDbExpression(Geometry::Transform(Geometry::ST_GeomFromText($_POST['geom'])));
                     $model->geom = new CDbExpression(Geometry::Transform(Geometry::ST_GeomFromText($_POST['geom']), 4326));
                     $model->username = Yii::app()->user->id;
                     if ($model->save()) {
                         $this->redirect(array('view'));
                     } else {
                         Yii::app()->user->setFlash('error', 'An error occurred in data storage. Please try again.');
                     }
                 } else {
                     Yii::app()->user->setFlash('warning', 'ATTENTION: to point out the quality of the water service.');
                 }
             } else {
                 if (!empty($_POST['faults_list'])) {
                     $model = new WaterFaultOpinions();
                     $model->fault = $_POST['faults_list'];
                     //$model->geom = new CDbExpression(Geometry::Transform(Geometry::ST_GeomFromText($_POST['geom'])));
                     $model->geom = new CDbExpression(Geometry::Transform(Geometry::ST_GeomFromText($_POST['geom']), 4326));
                     $model->username = Yii::app()->user->id;
                     if ($model->save()) {
                         $this->redirect(array('view'));
                     } else {
                         Yii::app()->user->setFlash('error', 'An error occurred in data storage. Please try again.');
                     }
                 } else {
                     Yii::app()->user->setFlash('warning', 'ATTENTION: to point out the fault of the water network.');
                 }
             }
         }
     }
     $this->render('create');
 }
 /**
  * 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)
 {
     $model = $this->loadModel($id);
     if ($this->deleteIfOrphan($model)) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     // TODO: si potrebbe fare una "getParentWr($model, $destroyOrphan=true)" per evitare la doppia findByPk
     $wr_model = WaterRequests::model()->findByPk($model->wr_id);
     if (!Yii::app()->user->checkAccess('updateWaterRequest', array('waterRequest' => $wr_model))) {
         throw new CHttpException(403, Yii::t('http_status', '403'));
     }
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['WaterRequestGeometries'])) {
         $model->attributes = $_POST['WaterRequestGeometries'];
         $model->geom = new CDbExpression(Geometry::Transform(Geometry::ST_GeomFromText($_POST['WaterRequestGeometries']['geom'])));
         $model->altitude = null;
         if ($model->save()) {
             echo 'ok';
         }
         //$this->redirect(array('view','id'=>$model->id));
     }
     //$this->render('update',array('model'=>$model,));
 }
 /**
  * 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();
 }
示例#4
0
 public static function save_geom($wkt, $wr_id, $proj = 3003)
 {
     $model = new WaterRequestGeometries();
     $model->wr_id = $wr_id;
     $model->name = 'uploaded';
     $model->geom = new CDbExpression(Geometry::Transform(Geometry::ST_GeomFromText($wkt, $proj), Yii::app()->params['geoserver']['water_request_geometries_srid']));
     return array('result' => $model->save(), 'newid' => $model->id);
 }
示例#5
0
文件: Geometry.php 项目: Gnafu/wiz
    /**
     * Calculate service area holding the geometry
     * @param string $wkt Geometria in WellKnownText format
     * @return array the result set (each array element represents a row of data). An empty array will be returned if the result contains no row. NULL is returned if ID is null.
     */
    public static function Get_Service_Area_ByWKT($wkt)
    {
        $wkt_geom = Geometry::Transform(Geometry::ST_GeomFromText($wkt), Yii::app()->params['geoserver']['service_areas_layer_srid']);
        //Yii::log(print_r($wkt_geom, true) , CLogger::LEVEL_INFO, 'Get_City_State_ByWKT');  // DEBUG
        //return;
        $connection = Yii::app()->db;
        $sql = '
		SELECT service_areas.gid, service_areas.area, service_areas.desc_area, service_areas.the_geom
		FROM service_areas
		WHERE st_intersects(
			service_areas.the_geom,
			ST_Centroid(' . $wkt_geom . ')
			);';
        $command = $connection->createCommand($sql);
        $dataReader = $command->query();
        $rows = $dataReader->readAll();
        /*
        if(count($rows)>1)
        	return Geometry::Get_Service_Area_ByWKT_all($wkt);  // Caso MOLTO particolare
        else
        */
        return $rows;
    }