PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/modules/persona/controllers/PersonaController.php

https://code.google.com/
PHP | 440 lines | 284 code | 50 blank | 106 comment | 17 complexity | c2902a2dc521ee7db5506374a8d7b061 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. class PersonaController extends Controller
  3. {
  4. /**
  5. * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
  6. * using two-column layout. See 'protected/views/layouts/column2.php'.
  7. */
  8. public $layout='//layouts/column2';
  9. /**
  10. * @return array action filters
  11. */
  12. public function filters()
  13. {
  14. return array(
  15. //'accessControl', // perform access control for CRUD operations
  16. 'rights',
  17. );
  18. }
  19. /**
  20. * Specifies the access control rules.
  21. * This method is used by the 'accessControl' filter.
  22. * @return array access control rules
  23. */
  24. /*
  25. public function accessRules()
  26. {
  27. return array(
  28. array('allow', // allow all users to perform 'index' and 'view' actions
  29. 'actions'=>array('index','view'),
  30. 'users'=>array('*'),
  31. ),
  32. array('allow', // allow authenticated user to perform 'create' and 'update' actions
  33. 'actions'=>array('create','update','HTMLPerson','PersonSearch','admin','delete'),
  34. 'users'=>array('@'),
  35. ),
  36. array('allow', // allow admin user to perform 'admin' and 'delete' actions
  37. 'actions'=>array('admin','delete'),
  38. 'users'=>array('admin'),
  39. ),
  40. array('deny', // deny all users
  41. 'users'=>array('*'),
  42. ),
  43. );
  44. }
  45. */
  46. //public function allowedActions() { return 'index'; }
  47. /**
  48. * Displays a particular model.
  49. * @param integer $id the ID of the model to be displayed
  50. */
  51. public function actionView($persona_numerodocumento, $persona_tipodocumento_id)
  52. {
  53. $this->render('view',array(
  54. 'model'=>$this->loadModel($persona_numerodocumento, $persona_tipodocumento_id),
  55. ));
  56. }
  57. /**
  58. *
  59. * Enter description here ...
  60. * @param Person $model
  61. */
  62. private function parseData(Persona $model)
  63. {
  64. if(isset($_POST['Persona']))
  65. {
  66. $model->attributes=$_POST['Persona'];
  67. //var_dump($model->persona_fechanacimiento);
  68. /*
  69. if(trim($model->persona_fechanacimiento)!= '')
  70. {
  71. $model->persona_fechanacimiento = Yii::app()->dateFormatter->format('yyyy-MM-dd', CDateTimeParser::parse($model->persona_fechanacimiento, Yii::app()->locale->getDateFormat('calendar_short')));
  72. }
  73. */
  74. //var_dump($_POST['Persona']);
  75. //var_dump($model->persona_fechanacimiento);exit;
  76. $direccion = new Direccion();
  77. $direccion->attributes=$_POST['Direccion'];
  78. if($model->isNewRecord)//nuevo
  79. {
  80. $model->persona_estado_id = AplicacionVariables::ESTADO_ACTIVO_ID;
  81. $direccion->direccion_creado_usuario_id = user()->getId();
  82. $direccion->direccion_creado = new CDbExpression('NOW()');
  83. }
  84. else
  85. {
  86. $direccion->direccion_modificado_usuario_id = user()->getId();
  87. $direccion->direccion_modificado = new CDbExpression('NOW()');
  88. }
  89. $direccion->direccion_estado_id= AplicacionVariables::ESTADO_ACTIVO_ID;
  90. $tmpDireccion= array();
  91. array_push($tmpDireccion, $direccion);
  92. $model->direcciones = $tmpDireccion;
  93. $comunicacion = new Comunicacion();
  94. $comunicacion->attributes=$_POST['Comunicacion'];
  95. $tmpComunicacion = array();
  96. array_push($tmpComunicacion, $comunicacion);
  97. $model->comunicaciones = $tmpComunicacion;
  98. if($model->validate() &&
  99. $direccion->validate() &&
  100. $comunicacion->validate())
  101. {
  102. $transaction=$model->dbConnection->beginTransaction();
  103. try
  104. {
  105. $model->save();
  106. $transaction->commit();
  107. $this->redirect(array('view','persona_tipodocumento_id'=>$model->persona_tipodocumento_id, 'persona_numerodocumento'=>$model->persona_numerodocumento));
  108. }
  109. catch(Exception $e)
  110. {
  111. //var_dump($e);
  112. $transaction->rollBack();
  113. }
  114. }
  115. else //hay arrores
  116. {
  117. $model->addErrors($direccion->getErrors());
  118. $model->addErrors($comunicacion->getErrors());
  119. /*
  120. if(trim($model->persona_fechanacimiento)!= '')
  121. {
  122. $model->persona_fechanacimiento = Yii::app()->dateFormatter->format('dd/MM/yyyy', $model->persona_fechanacimiento);
  123. }
  124. */
  125. }
  126. }
  127. return $model;
  128. }
  129. /**
  130. * Creates a new model.
  131. * If creation is successful, the browser will be redirected to the 'view' page.
  132. */
  133. public function actionCreate()
  134. {
  135. $model= new Persona();
  136. $model = $this->parseData($model);
  137. // Uncomment the following line if AJAX validation is needed
  138. // $this->performAjaxValidation($model);
  139. $this->render('create',array(
  140. 'model'=>$model,
  141. ));
  142. }
  143. /**
  144. * Updates a particular model.
  145. * If update is successful, the browser will be redirected to the 'view' page.
  146. * @param integer $id the ID of the model to be updated
  147. */
  148. public function actionUpdate($persona_numerodocumento, $persona_tipodocumento_id)
  149. {
  150. $model=$this->loadModel($persona_numerodocumento,$persona_tipodocumento_id);
  151. $model = $this->parseData($model);
  152. $this->render('update',array(
  153. 'model'=>$model,
  154. ));
  155. }
  156. /**
  157. * Deletes a particular model.
  158. * If deletion is successful, the browser will be redirected to the 'index' page.
  159. * @param integer $id the ID of the model to be deleted
  160. */
  161. public function actionDelete($persona_numerodocumento, $persona_tipodocumento_id)
  162. {
  163. if(Yii::app()->request->isPostRequest)
  164. {
  165. // we only allow deletion via POST request
  166. $this->loadModel($persona_numerodocumento, $persona_tipodocumento_id)->delete();
  167. // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  168. if(!isset($_GET['ajax']))
  169. $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
  170. }
  171. else
  172. throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
  173. }
  174. /**
  175. * Lists all models.
  176. */
  177. public function actionIndex()
  178. {
  179. $this->actionAdmin();
  180. }
  181. /**
  182. * Manages all models.
  183. */
  184. public function actionOwner()
  185. {
  186. //$criteria = new CDbCriteria();
  187. //$criteria->join = "join tbl_rel_person_property rpp on t.person_id = rpp.pepr_property_id";
  188. //$model = Person::model()->findAll($criteria);
  189. //var_dump($model);exit;
  190. $model=new Person('search');
  191. $model->unsetAttributes(); // clear any default values
  192. if(isset($_GET['Person']))
  193. $model->attributes=$_GET['Person'];
  194. $this->render('admin',array(
  195. 'model'=>$model,
  196. 'filterByTypePerson'=>'owner'
  197. ));
  198. }
  199. /**
  200. * Manages all models.
  201. */
  202. public function actionAdmin()
  203. {
  204. $model=new Persona('search');
  205. $model->unsetAttributes(); // clear any default values
  206. if(isset($_GET['Person']))
  207. $model->attributes=$_GET['Person'];
  208. $this->render('admin',array(
  209. 'model'=>$model,
  210. ));
  211. }
  212. /**
  213. * Returns the data model based on the primary key given in the GET variable.
  214. * If the data model is not found, an HTTP exception will be raised.
  215. * @param integer the ID of the model to be loaded
  216. */
  217. public function loadModel($persona_numerodocumento,$persona_tipodocumento_id)
  218. {
  219. $criteria = new CDbCriteria();
  220. $criteria->condition = 'persona_numerodocumento = :numDOC AND persona_tipodocumento_id = :tipoDOC';
  221. $criteria->params = array(':numDOC'=>$persona_numerodocumento,':tipoDOC'=>$persona_tipodocumento_id);
  222. $model=Persona::model()->find($criteria);//FIXME se puede mejorar a lo mejor usardo findByPK
  223. if($model===null)
  224. throw new CHttpException(404,'The requested page does not exist.');
  225. return $model;
  226. }
  227. /**
  228. * Performs the AJAX validation.
  229. * @param CModel the model to be validated
  230. */
  231. protected function performAjaxValidation($model)
  232. {
  233. if(isset($_POST['ajax']) && $_POST['ajax']==='person-form')
  234. {
  235. echo CActiveForm::validate($model);
  236. Yii::app()->end();
  237. }
  238. }
  239. /**
  240. * get Person as html.
  241. * If creation is successful, the browser will be redirected to the 'view' page.
  242. */
  243. public function actionHTMLPerson()
  244. {
  245. //fixme es un asco esto mejorar
  246. if(isset($_POST['Person']))
  247. {
  248. $typeDocument = $_POST['Person']['person_typedocument_id'];
  249. $numberDocument = $_POST['Person']['person_numberdocument'];
  250. }
  251. $model = Person::model()->getPersonActives($typeDocument, $numberDocument);
  252. if(count($model) != 0)
  253. {
  254. echo CHtml::activeHiddenField($model[0], 'person_id');
  255. $this->widget('zii.widgets.CDetailView', array(
  256. 'data'=>$model[0],
  257. 'attributes'=>array(
  258. 'person_firstname',
  259. 'person_lastname',
  260. 'person_numberdocument',
  261. 'typeDocument.typedocument_name', ),
  262. ));
  263. }
  264. else
  265. {
  266. echo "<p>the person doesn't exist</p>";
  267. }
  268. }
  269. /**
  270. * mejorar este asco :)
  271. * Enter description here ...
  272. * @param Array $arrayCommunication
  273. */
  274. private function parseCommunication($arrayCommunication)
  275. {
  276. //var_dump($arrayCommunication);exit;
  277. $communications = array();
  278. foreach ($arrayCommunication['communication_data'] as $key => $value)
  279. {
  280. $modelCommunication = new Communication();
  281. $modelCommunication->communication_typecommunication_id = $arrayCommunication['communication_typecommunication_id'][$key];
  282. $modelCommunication->communication_categorycommunication_id = $arrayCommunication['communication_categorycommunication_id'][$key];
  283. $modelCommunication->communication_data = $value;
  284. $modelCommunication->validate();
  285. array_push($communications, $modelCommunication);
  286. }
  287. return $communications;
  288. }
  289. private function parseCommunicationErrors($communications){
  290. $return = true;
  291. foreach ($communications as $communication)
  292. {
  293. $communication->validate();
  294. if($communication->hasErrors())
  295. {
  296. $return = false;
  297. }
  298. }
  299. return $return;
  300. }
  301. public function actionAutocomplete()
  302. {
  303. $res =array();
  304. if (isset($_GET['term']))
  305. {
  306. //CONCAT(per.persona_numerodocumento, '-',tdo.tipodocumento_abreviacion) as id,
  307. $qtxt ="SELECT
  308. per.persona_tipodocumento_id,
  309. per.persona_numerodocumento,
  310. CONCAT(tdo.tipodocumento_abreviacion, ' ',per.persona_numerodocumento , ' ', per.persona_apellido, ' ', per.persona_nombres) as value
  311. FROM tbl_persona per
  312. JOIN tbl_tipodocumento tdo ON per.persona_tipodocumento_id = tdo.tipodocumento_id
  313. WHERE CONCAT(tdo.tipodocumento_abreviacion , ' ',per.persona_numerodocumento, ' ', per.persona_apellido, ' ', per.persona_nombres) LIKE :term
  314. AND per.persona_estado_id = :estado";
  315. $command =Yii::app()->db->createCommand($qtxt);
  316. $command->bindValue(":term", '%'.trim($_GET['term']).'%', PDO::PARAM_STR);
  317. $command->bindValue(":estado", AplicacionVariables::ESTADO_ACTIVO_ID , PDO::PARAM_INT);
  318. $res =$command->queryAll();
  319. }
  320. echo CJSON::encode($res);
  321. Yii::app()->end();
  322. }
  323. public function actionSearch()
  324. {
  325. $resultado = '';
  326. if(
  327. isset($_POST['Persona']['persona_tipodocumento_id'])
  328. && trim($_POST['Persona']['persona_tipodocumento_id']) != ''
  329. && isset($_POST['Persona']['persona_numerodocumento'])
  330. && trim($_POST['Persona']['persona_numerodocumento']) != ''
  331. )
  332. {
  333. $model = new Persona();
  334. $model->persona_tipodocumento_id = $_POST['Persona']['persona_tipodocumento_id'];
  335. $model->persona_numerodocumento = $_POST['Persona']['persona_numerodocumento'];
  336. $criteria = new CDbCriteria();
  337. $criteria->condition = "persona_estado_id = :estado AND persona_tipodocumento_id = :tipoDocumento AND persona_numerodocumento = :numeroDocumento";
  338. $criteria->params = array(
  339. ':estado'=>AplicacionVariables::ESTADO_ACTIVO_ID,
  340. ':tipoDocumento'=>$model->persona_tipodocumento_id,
  341. ':numeroDocumento'=>$model->persona_numerodocumento
  342. );
  343. $tmpPerson = $model->find($criteria);
  344. if(!is_null($tmpPerson))
  345. {
  346. Yii::import('application.extensions.json.CArJSON');
  347. $json = new CArJSON();
  348. $relations = array('direcciones','comunicaciones','tipoDocumento','estudios');
  349. $attributes = array(
  350. 'root' => null,//all
  351. 'tipoDocumento' => array( 'tipodocumento_id', 'tipodocumento_abreviacion' ),
  352. );
  353. $resultado = $json->toJSON($tmpPerson, $relations, $attributes);
  354. }
  355. else
  356. {
  357. $resultado = '{"error": {"message": "'.t('Persona no existe','persona_etiqueta').'"}}';
  358. }
  359. }
  360. else
  361. {
  362. $resultado = '{"error": {"message": "'.t('Faltan Datos','persona_etiqueta').'"}}';
  363. }
  364. header('Content-type: application/json');
  365. echo $resultado;
  366. Yii::app()->end();
  367. }
  368. }