Example #1
0
 /**
  * Update the uploaded EPANET file adding thw WaterRequest info and sends it to the client
  * @param string $id string representing the ID of a WaterRequest
  */
 public function actionEpanet($id)
 {
     $this->layout = '//layouts/column1';
     //retrieve water request
     $wr = $this->loadModel($id);
     if (Yii::app()->user->checkAccess('epanetWaterRequest', array('waterRequest' => $wr))) {
         $model = new EpanetForm();
         //parametrized values available
         $replaceable_parameters = array('first_name' => 'First name of water request\'s owner', 'last_name' => 'Last name of water request\'s owner', 'date' => 'Water Request date', 'project' => 'Water Request project name', 'index' => 'Autoincrement index');
         //parametrized values marker
         $replaceable_parameters_marker = '$';
         //form field in which user can insert parametrized values
         $form_elements = array('tag', 'demand_pattern', 'demand_categories', 'emitter_coeff', 'initial_quality', 'source_quality', 'srid', 'other_srid');
         if (isset($_POST['EpanetForm'])) {
             //setting directory to retrieve the uploaded file and to store the new file
             $up_dir = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR . Yii::app()->params['EPANET']['upload_dir'] . DIRECTORY_SEPARATOR;
             $down_dir = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR . Yii::app()->params['EPANET']['download_dir'] . DIRECTORY_SEPARATOR;
             //retrieving information about uploaded file
             $file_info = pathinfo($_POST['EpanetForm']['filename']);
             $filename = $file_info['filename'];
             $ext = $file_info['extension'];
             //setting upload and download directory-filename
             $up_file_dir = $up_dir . $filename . '.' . $ext;
             $down_filename = $filename . Yii::app()->params['EPANET']['download_filename_suffix'];
             $down_file_dir = $down_dir . $down_filename . '.' . $ext;
             //read epanet network from file
             $epanet = new EPANET($up_file_dir);
             //retrieving some parameters from water request
             $first_name = $wr->username;
             $last_name = $wr->username;
             $date = $wr->dateHR;
             $project = $wr->project;
             $wr_id = $wr->id;
             // setting prefix
             $junction_prefix = Yii::app()->params['EPANET']['junction_prefix'];
             if ($junction_prefix !== '') {
                 $junction_prefix = substr(Yii::app()->params['EPANET']['junction_prefix'], 0, 3) . '_';
             }
             $junction_prefix = $junction_prefix . $wr_id;
             $index = 0;
             //loop into geometries
             foreach ($wr->geometries() as $geom) {
                 $water_demand = 0;
                 $description = '';
                 $zones_array = array();
                 //loop into zones to calculate total water demand for each geometry
                 foreach ($geom->zones() as $zone) {
                     $water_demand += $zone->water_demand;
                     if (array_search($zone->zone_name, $zones_array) === false) {
                         array_push($zones_array, $zone->zone_name);
                     }
                 }
                 foreach ($zones_array as $zs) {
                     $description = $description . $zs . ' ';
                 }
                 //substitute, from form elements, water request parameters
                 foreach ($form_elements as $element) {
                     $str = $_POST['EpanetForm'][$element];
                     foreach ($replaceable_parameters as $param => $text) {
                         ${$element} = str_replace($replaceable_parameters_marker . $param, ${$param}, $str);
                         $str = ${$element};
                     }
                 }
                 //be sure to have all parameters before invoking addJunction
                 // junction_id is in config file
                 //if (!isset($junction_id))
                 //	$junction_id = 'a';
                 // La descrizione viene generata a partire dalle Zone della geometria
                 //if (!isset($description))
                 //	$description = 'b';
                 if (!isset($tag)) {
                     $tag = 'c';
                 }
                 if (!isset($demand_pattern)) {
                     $demand_pattern = 'd';
                 }
                 if (!isset($demand_categories)) {
                     $demand_categories = '';
                 }
                 if (!isset($emitter_coeff)) {
                     $emitter_coeff = '';
                 }
                 if (!isset($initial_quality)) {
                     $initial_quality = '';
                 }
                 if (!isset($source_quality)) {
                     $source_quality = '';
                 }
                 //check if srid passed exists
                 if (isset($srid) && $srid == 'other') {
                     if (isset($other_srid) && Geometry::Srid_Exists($other_srid)) {
                         $srid = $other_srid;
                         Yii::app()->user->usedSrid($srid);
                     } else {
                         echo 'Incorrect srid';
                         Yii::app()->end();
                     }
                 }
                 $elevation = $geom->elevation;
                 //Yii::log('Elevation: '.print_r($elevation, true) , CLogger::LEVEL_INFO, 'actionEpanet');  // DEBUG
                 if (isset($srid) && $srid != Yii::app()->params['geoserver']['water_request_geometries_srid']) {
                     $centroid = $geom->getCentroid($srid);
                 } else {
                     $centroid = $geom->centroid;
                 }
                 //$junction_id = $junction_prefix.'_'.$geom->id.'_'.$junction_id;
                 $junction_id = $junction_prefix . '_' . $geom->id;
                 //add geometry centroid and parameters to epanet
                 $epanet->addJunction($junction_id, $centroid['x'], $centroid['y'], $description, $tag, $elevation, $water_demand, $demand_pattern, $demand_categories, $emitter_coeff, $initial_quality, $source_quality);
                 $index++;
             }
             //write epanet network into file
             $epanet->finalize($down_file_dir);
             //send file
             Yii::app()->request->sendFile($down_filename . '.' . $ext, file_get_contents($down_file_dir), $ext, true);
             // Non riesce a fare il redirect e appende al file .inp la pagina wr/view
             //$this->redirect('view',array('id'=>$wr->id));
             return;
         }
         $this->render('epanet', array('wr' => $wr, 'model' => $model, 'replaceable_parameters' => $replaceable_parameters, 'replaceable_parameters_marker' => $replaceable_parameters_marker));
     } else {
         throw new CHttpException(403, Yii::t('http_status', '403'));
     }
 }