/** * Ajax request to submit data * @param array $request the request * @return array ajax format status */ public function submitFormData($request) { $errors = array(); $db = ezcDbInstance::get(); $auth = R3AuthInstance::get(); $this->setMunicipalityForUser($request); if ($this->act != 'add' && !defined('UNIT_TEST_MODE')) { // Security check R3Security::checkBuilding(@$request['id']); } $request['bu_id'] = $request['id']; if (isset($request['mu_name'])) { $request['mu_id'] = R3EcoGisHelper::getMunicipalityIdByName($this->do_id, $request['mu_name'], $this->auth->getParam('mu_id')); } if (isset($request['fr_name'])) { $request['fr_id'] = R3EcoGisHelper::getFractionIdByName($this->do_id, $request['mu_id'], $request['fr_name']); } if (isset($request['st_name'])) { $request['st_id'] = R3EcoGisHelper::getStreetIdByName($this->do_id, $request['mu_id'], $request['st_name']); } if ($this->act != 'del') { $errors = $this->checkFormData($request); if ($auth->getConfigValue('APPLICATION', 'BUILDING_CODE_REQUIRED') == 'T' && trim($request['bu_code']) == '') { $errors['bu_code'] = array('CUSTOM_ERROR' => _('Il campo "Codice edificio" è obbligatorio')); } if (isset($request['mu_name']) && $request['mu_name'] != '' && $request['mu_id'] == '') { $errors['mu_name'] = array('CUSTOM_ERROR' => _('Il comune immesso non è stato trovato')); } if (isset($request['fr_name']) && $request['fr_name'] != '' && $request['fr_id'] == '') { $errors['fr_name'] = array('CUSTOM_ERROR' => _('La frazione immessa non è stata trovata')); } if (isset($request['st_name']) && $request['st_name'] != '' && $request['st_id'] == '') { $errors['st_name'] = array('CUSTOM_ERROR' => _('La strada immessa non è stata trovata')); } if ($request['bu_usage_weeks'] > 52) { $errors['bu_usage_weeks'] = array('CUSTOM_ERROR' => _('Numero di settimane massimo inseribile: 52')); } if ($auth->getConfigValue('APPLICATION', 'BUILDING_CODE_UNIQUE') == 'T' && !$this->checkBuildingCodeUnique($request['mu_id'], $request['bu_code'], $request['id'])) { $errors['bu_code'] = array('CUSTOM_ERROR' => _('Il campo "Codice edificio" non è univoco')); } if ($this->act == 'mod') { // Check compatibility between action and building purpose use $this->checkBuildingPurposeUseCompatibility($request['id'], $request['bpu_id'], $errors); } } if (count($errors) > 0) { return $this->getAjaxErrorResult($errors); } else { $deleteIDs = array(); // Array che contiene l'elenco delle immagini da cancellare (processato dopo il commit per evitare perdita di dati!) $db->beginTransaction(); if ($this->act != 'del') { $id = $this->applyData($request); // Update compatibility between action and building purpose use $this->updateBuildingPurposeUseCompatibility($request['id'], $request['bpu_id']); // Delete photo marked as delete foreach ($this->getOldImagesIDs($id, null) as $photoID) { if (isset($request["photo_{$photoID}_delete"]) || isset($request["label_{$photoID}_delete"]) || isset($request["thermography_{$photoID}_delete"])) { $deleteIDs[] = $photoID; } } // Save images $uploads = array('bu_photo' => 'photo', 'bu_label' => 'label', 'bu_thermography' => 'thermography'); foreach ($uploads as $upload => $kind) { if (isset($_FILES[$upload])) { $tot = count($_FILES[$upload]['name']); for ($i = 0; $i < $tot; $i++) { if ($_FILES[$upload]['error'][$i] == 0) { $deleteIDs = array_merge($deleteIDs, $this->getOldImagesIDs($id, $kind)); $doc_file_id = $this->getDocFileId(); $this->addFile($_FILES[$upload]['name'][$i], 'building', $kind, $doc_file_id, $_FILES[$upload]['tmp_name'][$i]); $doct_id = R3EcoGisHelper::getDocumentTypeIdByCode(strtoupper("BUILDING_{$kind}")); $sql = "INSERT INTO document (doc_object_id, doct_id, doc_file_id, doc_file, doc_date) VALUES ({$id}, {$doct_id}, {$doc_file_id}, " . $db->quote($_FILES[$upload]['name'][$i]) . ", NOW()) "; $db->exec($sql); } } } } // Save map if (isset($request['geometryStatus']) && strtoupper($request['geometryStatus']) == 'CHANGED') { $session_id = session_id(); $sql = "UPDATE building\n SET the_geom=foo.the_geom\n FROM (SELECT MULTI(ST_Force_2d(ST_union(ST_Buffer(the_geom, 0.0)))) AS the_geom FROM edit_tmp_polygon WHERE session_id='{$session_id}') AS foo\n WHERE bu_id={$id}"; $db->exec($sql); } } else { $id = $this->applyData($request); $deleteIDs = $this->getOldImagesIDs($id, null); // Delete old images } $db->commit(); R3EcoGisEventNotifier::notifyDataChanged($this, array('data_changed' => true)); $this->removeOldFilesByIDs(array_unique($deleteIDs)); R3EcoGisCacheHelper::resetMapPreviewCache(null); return array('status' => R3_AJAX_NO_ERROR, 'js' => "submitFormDataDoneBuilding({$id})"); } }