array('index','view','activo'), 'users'=>array('@'), ), array('allow', // allow authenticated user to perform 'create' and 'update' actions 'actions'=>array('create','update','GeneratePdf','GenerateExcel'), 'users'=>array('@'), ), array('allow', // allow admin user to perform 'admin' and 'delete' actions 'actions'=>array('admin','delete'), 'users'=>array('admin'), ), array('deny', // deny all users 'users'=>array('*'), ), ); } /** * Displays a particular model. * @param integer $id the ID of the model to be displayed */ public function actionView($id) { $this->render('view',array( 'model'=>$this->loadModel($id), )); } /** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate($id) { $model=new Diccionarios; $model->id_encuesta = $id; // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if(isset($_POST['Diccionarios'])) { $model->attributes=$_POST['Diccionarios']; $model->tabla = 'dicc_'.Yii::app()->funciones->cleanText($model->nombre).'_'.date('ymdHis'); if($model->save()){ if($_FILES['archivo']['error'] == UPLOAD_ERR_OK){ $fila = 1; $nombre_tmp = $_FILES['archivo']['tmp_name']; $handle = fopen($nombre_tmp, "r"); $connect = Yii::app()->db; while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { if($fila == 1){ $columnas = $data; foreach ($columnas as $key => $value) { $value = Yii::app()->funciones->cleanText($value); $columnas[$key] = $value; } $columna = implode(' VARCHAR(100) NOT NULL COLLATE utf8_spanish_ci, ', $columnas); $connect->createCommand('CREATE TABLE IF NOT EXISTS '.$model->tabla.'('.$columna.' VARCHAR(100) NOT NULL COLLATE utf8_spanish_ci)')->execute(); }else{ foreach ($data as $key => $value) { $value = utf8_encode($value); $data[$key] = $value; } $valores = implode('", "', $data); $connect->createCommand('INSERT INTO '.$model->tabla.' VALUES("'.$valores.'")')->execute(); } $fila++; } fclose($handle); } $this->redirect(array('index','id'=>$model->id_encuesta)); } } $this->render('create',array( 'model'=>$model, )); } /** * 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); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if(isset($_POST['Diccionarios'])) { $model->attributes=$_POST['Diccionarios']; if($model->save()){ if($_FILES['archivo']['error'] == UPLOAD_ERR_OK){ Yii::app()->funciones->deleteBD($model->tabla); $fila = 1; $nombre_tmp = $_FILES['archivo']['tmp_name']; $handle = fopen($nombre_tmp, "r"); $connect = Yii::app()->db; while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { if($fila == 1){ $columnas = $data; foreach ($columnas as $key => $value) { $value = Yii::app()->funciones->cleanText($value); $columnas[$key] = $value; } $columna = implode(' VARCHAR(100) NOT NULL COLLATE utf8_spanish_ci, ', $columnas); $connect->createCommand('CREATE TABLE IF NOT EXISTS '.$model->tabla.'('.$columna.' VARCHAR(100) NOT NULL COLLATE utf8_spanish_ci)')->execute(); }else{ foreach ($data as $key => $value) { $value = utf8_encode($value); $data[$key] = $value; } $valores = implode('", "', $data); $connect->createCommand('INSERT INTO '.$model->tabla.' VALUES("'.$valores.'")')->execute(); } $fila++; } fclose($handle); } $this->redirect(array('index','id'=>$model->id_encuesta)); } } $this->render('update',array( 'model'=>$model, )); } /** * Deletes a particular model. * If deletion is successful, the browser will be redirected to the 'admin' page. * @param integer $id the ID of the model to be deleted */ public function actionDelete($id) { //if(!isset($_GET['ajax']))return false; $data=array("check"=>"0"); $model = $this->loadModel($id); Yii::app()->funciones->deleteBD($model->tabla); if($model->delete())$data=array("check"=>"1"); echo json_encode($data); exit(); } public function actionActivo($id) { $model=$this->loadModel($id); //Importante tener declarado el campo activo //en el modelo de base de datos $activo=$model->activo; if($model->activo){ $model->activo=0; }else{ $model->activo=1; } $arrResp=array(); if($model->saveAttributes(array('activo'=>$model->activo))){ $arrResp=array( 'check'=>1, 'activo'=>$model->activo); }else{ $arrResp=array( 'check'=>0, 'activo'=>$activo); } echo json_encode($arrResp); Yii::App()->end(); } /** * Lists all models. */ public function actionIndex($id) { $model = Diccionarios::model()->findAllByAttributes(array('id_encuesta'=>$id)); if($id != 0){ $encuesta = BasesEncuestas::model()->findByPk($id); $nombre = $encuesta->encuesta; }else{ $nombre = 'Generales'; } $this->render('index',array('model'=>$model, 'nombre'=>$nombre, 'id'=>$id)); } /** * Manages all models. */ public function actionAdmin() { $model=new Diccionarios('search'); $model->unsetAttributes(); // clear any default values if(isset($_GET['Diccionarios'])) $model->attributes=$_GET['Diccionarios']; $this->render('admin',array( 'model'=>$model, )); } /** * 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=Diccionarios::model()->findByPk($id); if($model===null) throw new CHttpException(404,'The requested page does not exist.'); return $model; } /** * Performs the AJAX validation. * @param CModel the model to be validated */ protected function performAjaxValidation($model) { if(isset($_POST['ajax']) && $_POST['ajax']==='diccionarios-form') { echo CActiveForm::validate($model); Yii::app()->end(); } } }