/**
  * Creates a new Comercios model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Comercios();
     if ($model->load(Yii::$app->request->post())) {
         $cantProd = count($model->productos);
         $productos = ArrayHelper::map(Productos::find()->all(), 'idProd', 'nombre');
         /*se crea el stock inicial para el comercio*/
         for ($i = 0; $i < $cantProd; $i++) {
             $id = $model->productos[$i];
             $modelStock = new Stock();
             $modelStock->nombreComercio = $model->nombre;
             $modelStock->cantidadEnStock = 0;
             $modelStock->nombreProducto = $productos[$id];
             $modelStock->save();
         }
         $address = urlencode($model->direccion);
         $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=" . $address;
         $response = file_get_contents($url);
         $json = json_decode($response, true);
         if ($json['status'] == 'ZERO_RESULTS') {
             return array();
         }
         $model->latitud = json_encode($json['results'][0]['geometry']['location']['lat']);
         $model->longitud = json_encode($json['results'][0]['geometry']['location']['lng']);
         if ($model->save()) {
             return $this->redirect(['view', 'id' => $model->idComercio]);
         }
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemple #2
0
 /**
  * Creates a new Comercios model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Comercios();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }