Exemplo n.º 1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Archivos::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['idarchivo' => $this->idarchivo, 'usuario' => $this->usuario, 'articulo' => $this->articulo, 'fechacreado' => $this->fechacreado, 'fechaactualizado' => $this->fechaactualizado]);
     $query->andFilterWhere(['like', 'nombre', $this->nombre])->andFilterWhere(['like', 'tipo', $this->tipo])->andFilterWhere(['like', 'tamaño', $this->tamaño]);
     return $dataProvider;
 }
Exemplo n.º 2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getArchivos()
 {
     return $this->hasMany(Archivos::className(), ['articulo' => 'idarticulo']);
 }
Exemplo n.º 3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getArchivos()
 {
     return $this->hasMany(Archivos::className(), ['usuario' => 'idusuario']);
 }
Exemplo n.º 4
0
 /**
  * Updates an existing Usuarios model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionActualizar($id)
 {
     $model = $this->findModel($id);
     $articulo = $this->findModel($id);
     $session = Yii::$app->session;
     $idUsuario = Yii::$app->user->getId();
     $claveSession = ImagenHelper::SESSION_IMAGEN_ARTICULO . $idUsuario;
     $files = [];
     $imagenes = [];
     $imagenesEliminar = [];
     if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         return ActiveForm::validate($model);
     }
     if ($session->has($claveSession)) {
         $files = $session->get($claveSession);
         $imagenesAgregar = $files['file'];
         $imagenesEliminar = $files['delete'];
     }
     if ($model->load(Yii::$app->request->post())) {
         $imagenPrincipal = Yii::$app->request->post('dropzone_imagen_principal');
         //actualizar imagen principal
         //guardamos la imagen principal
         if (isset($imagenPrincipal) || $imagenPrincipal == 0) {
             $imagenesArticulo = Archivos::findAll(['articulo' => $id]);
             $principal = '';
             foreach ($imagenesArticulo as $key => $imagen) {
                 if ($key == $imagenPrincipal) {
                     $principal = $imagen->idarchivo;
                 }
             }
             //si existe
             if (!empty($principal)) {
                 //seteamos todas las imagenes a 0
                 Archivos::updateAll(['principal' => Archivos::NO_ES_PRINCIPAL], ['articulo' => $id]);
                 //seteamos todas la principal a 1
                 $imagen = Archivos::findOne(['idarchivo' => $principal]);
                 $imagen->principal = Archivos::ES_PRINCIPAL;
                 $imagen->save();
             }
             $session->remove($claveSession);
             Yii::$app->session->setFlash('toasts', 'Se ha actualizado el articulo con exito');
         }
         if ($model->save() || !empty($files)) {
             //guardamos imagenes nuevas
             if (!empty($imagenesAgregar)) {
                 foreach ($imagenesAgregar as $key => $file) {
                     $file = (object) $file;
                     // the path to save file, you can set an uploadPath
                     $nombre = ImagenHelper::obtenerNombreArchivo(['usuario' => $idUsuario, 'articulo' => $model->idarticulo, 'imagen' => $file->name]);
                     $archivo = new Archivos();
                     $archivo->usuario = $idUsuario;
                     $archivo->articulo = $articulo->idarticulo;
                     $archivo->nombre = $nombre;
                     $archivo->tipo = $file->type;
                     $archivo->peso = $file->size;
                     $archivo->fechacreado = MetodosHelper::fechaHoy();
                     if ($archivo->save()) {
                         file_put_contents(ImagenHelper::rutaImagenesArticulos() . $nombre, base64_decode($file->base64));
                     }
                 }
             }
             //eliminamos imagenes
             if (!empty($imagenesEliminar)) {
                 $listaImagenes = [];
                 foreach ($model->archivos as $key => $imagen) {
                     $listaImagenes[$key] = $imagen->nombre;
                 }
                 $resultado = array_diff($listaImagenes, $imagenesEliminar);
                 if (empty($resultado)) {
                     Archivos::deleteAll(['articulo' => $model->idarticulo, 'usuario' => $idUsuario]);
                 } else {
                     foreach ($listaImagenes as $nombre) {
                         if (in_array($nombre, $imagenesEliminar)) {
                             Archivos::deleteAll(['nombre' => $nombre, 'articulo' => $model->idarticulo, 'usuario' => $idUsuario]);
                             ImagenHelper::eliminarImagen($nombre);
                         }
                     }
                 }
             }
             $session->remove($claveSession);
             Yii::$app->session->setFlash('toasts', 'Se ha actualizado el articulo con exito');
             //Yii::$app->session->setFlash('success', ['toast'=>'Se ha actualizado la categoria con exito'], true);
             return $this->redirect(['detalle', 'id' => $model->idarticulo]);
         }
     }
     $session->remove($claveSession);
     return $this->render('actualizar', ['model' => $model]);
 }
Exemplo n.º 5
0
 /**
  * Editar articulo existente
  * @return bool
  */
 public function editar()
 {
     $idUsuario = Yii::$app->user->getId();
     if ($this->validate()) {
         $model = $this->articulo;
         $model->nombre = ucfirst($this->nombre);
         $model->estado = $this->estado;
         $model->descripcion = ucfirst($this->descripcion);
         $model->categoria = $this->categoria;
         $model->precio = $this->precio;
         $model->transacciones = is_array($this->transacciones) ? implode(',', $this->transacciones) : Articulos::NO_ESPECIFICADO;
         //
         //$archivo = new Archivos();
         if ($model->update(false)) {
             if (!empty($this->imagenes)) {
                 foreach ($this->imagenes as $key => $file) {
                     $file = (object) $file;
                     // the path to save file, you can set an uploadPath
                     $nombre = ImagenHelper::obtenerNombreArchivo(['usuario' => $idUsuario, 'articulo' => $model->idarticulo, 'imagen' => $file->name]);
                     $archivo = new Archivos();
                     $archivo->usuario = $idUsuario;
                     $archivo->articulo = $model->idarticulo;
                     $archivo->nombre = $nombre;
                     $archivo->tipo = $file->type;
                     $archivo->peso = $file->size;
                     $archivo->fechacreado = MetodosHelper::fechaHoy();
                     //guardamos la imagen principal
                     $archivo->principal = count($this->imagenes) == 1 || $key == $this->principal ? 1 : 0;
                     if ($archivo->save()) {
                         file_put_contents(ImagenHelper::rutaImagenesArticulos() . $nombre, base64_decode($file->base64));
                         ImagenHelper::guardarImagenSize($nombre, 'articulos');
                     }
                 }
             }
             return true;
         } else {
             return false;
         }
     }
     return false;
 }
Exemplo n.º 6
0
 /**
  * Finds the Archivos model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Archivos the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Archivos::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }