Ejemplo n.º 1
0
 /**
  * Ajax request to submit data
  * @param array $request   the request
  * @return array           ajax format status
  */
 public function submitFormData($request)
 {
     $db = ezcDbInstance::get();
     if ($this->act == 'del') {
         $id = (int) $request['id'];
         $mu_id = $db->query("SELECT mu_id\r\n                                 FROM ecogis.document_data doc\r\n                                 INNER JOIN ecogis.municipality mu ON doc.doc_object_id=mu_id AND doct_code='SEAP'\r\n                                 WHERE do_id={$this->do_id} AND doc_id={$id}")->fetchColumn();
         $db->beginTransaction();
         $this->deleteImportedData($mu_id);
         // Delete document
         $file_info = $this->getDocFileInfo($id);
         $this->removeOldFile($file_info['doc_file'], 'import_seap', '', $file_info['doc_file_id']);
         $request['doc_id'] = $id;
         $id = $this->applyData($request);
         $db->commit();
         die;
     }
     $files = R3EcoGisHelper::getUploadedFile('doc_file');
     $errors = array();
     if ($files['error'] != 0) {
         if ($files['error'] == UPLOAD_ERR_NO_FILE) {
             $errors['doc_file'] = array('CUSTOM_ERROR' => _('Bisogna indicare il file da caricare'));
         } else {
             $errors['doc_file'] = array('CUSTOM_ERROR' => _("Si è veificato un errore durante il caricamento del file. Si prega di riprovare. Errore #") . $files['error']);
         }
     }
     $mu_id = $this->auth->getParam('mu_id') != '' ? $this->auth->getParam('mu_id') : (int) $this->mu_id;
     $istat = $db->query("SELECT mu_istat FROM ecogis.municipality WHERE do_id={$this->do_id} AND mu_id={$mu_id}")->fetchColumn();
     if ($istat == '') {
         $errors['mu_id'] = array('CUSTOM_ERROR' => _("Indicare il comune per cui si intende importare i dati"));
     }
     if (count($errors) > 0) {
         return $this->getAjaxErrorResult($errors);
     }
     $db->beginTransaction();
     $result = $this->importDataFromTemplate($files['tmp_name'], $istat);
     if (!$result['done']) {
         $errors['doc_file'] = array('CUSTOM_ERROR' => $result['log']);
         return $this->getAjaxErrorResult($errors);
     }
     // Insert into document
     // Remove old document
     $doc_id = $db->query("SELECT doc_id\r\n                              FROM ecogis.document_data doc\r\n                              INNER JOIN ecogis.municipality mu ON mu_id=doc_object_id\r\n                              WHERE doct_code='SEAP' AND do_id={$this->do_id} AND doc_object_id={$mu_id}")->fetchColumn();
     if ($doc_id != '') {
         $file_info = $this->getDocFileInfo($doc_id);
         $this->removeOldFile($file_info['doc_file'], 'import_seap', '', $file_info['doc_file_id']);
         $db->exec("DELETE FROM ecogis.document WHERE doc_id={$doc_id}");
     }
     $request['doc_title_1'] = _('Import PAES');
     $request['doc_descr_1'] = json_encode($result['log']);
     $request['doc_object_id'] = $mu_id;
     $request['doc_file'] = $files['name'];
     $request['doc_file_id'] = $this->getDocFileId();
     $request['doct_id'] = R3EcoGisHelper::getDocumentTypeIdByCode('SEAP');
     $request['doc_date'] = date('Y-m-d');
     $this->addFile($request['doc_file'], 'import_seap', '', $request['doc_file_id'], $files['tmp_name']);
     $id = $this->applyData($request);
     $db->commit();
     R3EcoGisEventNotifier::notifyDataChanged($this, array('data_changed' => true));
     return array('status' => R3_AJAX_NO_ERROR, 'js' => "submitFormDataDoneImportSeap({$id})");
 }