/** * Ajax request to submit data * @param array $request the request * @return array ajax format status */ public function submitFormData($request) { $errors = array(); $db = ezcDbInstance::get(); $request = array_merge(array('ac_object_id' => null, 'gc_id' => null), $request); $request['wo_id'] = null; $request['ac_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 ($this->act != 'del') { $request['ac_green_electricity_purchase'] = forceFloat($request['ac_green_electricity_purchase_mwh'], null, '.') * 1000; // Convert MWh to kWh $request['ac_co2_reduction'] = forceFloat($request['ac_co2_reduction_tco2'], null, '.') * 1000; // Convert tCO2 to kCO2 $tmpGesIdConsumption = array(); $tmpEsIdConsumption = array(); $tmpUdmIdConsumption = array(); $tmpAcExpectedEnergySaving = array(); $tmpAcExpectedEnergySavingMwh = array(); for ($i = 0; $i < count($request['es_id_consumption']); $i++) { if ($request['udm_id_consumption'][$i] != '' || $request['ac_expected_energy_saving'][$i] != '') { $tmpGesIdConsumption[] = $request['ges_id_consumption'][$i]; $tmpEsIdConsumption[] = $request['es_id_consumption'][$i]; $tmpUdmIdConsumption[] = $request['udm_id_consumption'][$i]; $tmpAcExpectedEnergySaving[] = $request['ac_expected_energy_saving'][$i]; $tmpAcExpectedEnergySavingMwh[] = $request['ac_expected_energy_saving_mwh'][$i]; } } $request['ges_id_consumption'] = $tmpGesIdConsumption; $request['es_id_consumption'] = $tmpEsIdConsumption; $request['udm_id_consumption'] = $tmpUdmIdConsumption; $request['ac_expected_energy_saving'] = $tmpAcExpectedEnergySaving; $request['ac_expected_energy_saving_mwh'] = $tmpAcExpectedEnergySavingMwh; $request['esu_id_consumption'] = R3EcoGisHelper::getMultipleEnergySourceUdmID($_SESSION['do_id'], $request['es_id_consumption'], $request['udm_id_consumption'], $request['mu_id']); $request['esu_id_production'] = R3EcoGisHelper::getEnergySourceUdmID($_SESSION['do_id'], $request['es_id_production'], $request['udm_id_production'], $request['mu_id'], true); $request['emo_id'] = self::getEnergyMeterObjectByID($request['mu_id'], $request['ac_object_id'], $request['gc_id']); 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['gc_id_parent']) || $request['gc_id_parent'] == '') { $errors['gc_id_parent'] = array('CUSTOM_ERROR' => _('Il campo "Macro-settore" è obbligatorio')); } $errors = $this->checkFormData($request, $errors); $selectedRelatedActions = array(); if (isset($request['related_required_action_id'])) { for ($i = 0; $i < count($request['related_required_action_id']); $i++) { if ($request['related_required_action_id'][$i] > 0 && in_array($request['related_required_action_id'][$i], $selectedRelatedActions)) { $errors['related_required_action_' . $i] = array('CUSTOM_ERROR' => _("L'azione ") . $this->getActionName($request['related_required_action_id'][$i]) . _(" è già stata selezionata")); } array_push($selectedRelatedActions, $request['related_required_action_id'][$i]); } } if (isset($request['related_action_id'])) { for ($i = 0; $i < count($request['related_action_id']); $i++) { if ($request['related_action_id'][$i] > 0 && in_array($request['related_action_id'][$i], $selectedRelatedActions)) { $errors['related_action_' . $i] = array('CUSTOM_ERROR' => _("L'azione ") . $this->getActionName($request['related_action_id'][$i]) . _(" è già stata selezionata")); } array_push($selectedRelatedActions, $request['related_action_id'][$i]); } } if (isset($request['related_excluded_action_id'])) { for ($i = 0; $i < count($request['related_excluded_action_id']); $i++) { if ($request['related_excluded_action_id'][$i] > 0 && in_array($request['related_excluded_action_id'][$i], $selectedRelatedActions)) { $errors['related_required_action_' . $i] = array('CUSTOM_ERROR' => _("L'azione ") . $this->getActionName($request['related_excluded_action_id'][$i]) . _(" è già stata selezionata")); } array_push($selectedRelatedActions, $request['related_excluded_action_id'][$i]); } } if (isset($request['enable_benefit_year']) && $request['enable_benefit_year'] == 'T' && isset($request['benefit_year'])) { $startBenefitYear = (int) substr($request['ac_benefit_start_date'], 0, 4); $endBenefitYear = (int) substr($request['ac_benefit_end_date'], 0, 4); $lastBenefitPerc = 0; for ($i = 0; $i < count($request['benefit_year']); $i++) { if ($request['benefit_year'][$i] != '' && $request['benefit_year'][$i] < $startBenefitYear) { $errors['benefit_year_' . $i] = array('CUSTOM_ERROR' => sprintf(_("L'anno del beneficio \"%s\" è antecedente al %s (anno inizio beneficio)"), $request['benefit_year'][$i], $startBenefitYear)); } else { if ($request['benefit_year'][$i] != '' && $request['benefit_year'][$i] > $endBenefitYear) { $errors['benefit_year_' . $i] = array('CUSTOM_ERROR' => sprintf(_("L'anno del beneficio \"%s\" è oltre al %s (anno fine beneficio)"), $request['benefit_year'][$i], $endBenefitYear)); } } if ($request['benefit_benefit'][$i] != '' && $request['benefit_benefit'][$i] < 0 || $request['benefit_benefit'][$i] != '' && $request['benefit_benefit'][$i] > 100) { $errors['benefit_benefit_' . $i] = array('CUSTOM_ERROR' => _("Il valore del beneficio deve essere compreso tra 0 e 100")); } } } } if (count($errors) > 0) { return $this->getAjaxErrorResult($errors); } else { setLocale(LC_ALL, 'C'); $db->beginTransaction(); $id = $this->applyData($request); $sql = "DELETE FROM ecogis.action_catalog_energy WHERE ac_id=" . $db->quote($id, PDO::PARAM_INT); $db->exec($sql); if ($this->act != 'del') { for ($i = 0; $i < count($request['esu_id_consumption']); $i++) { $sql = "INSERT INTO ecogis.action_catalog_energy (ac_id, esu_id, ace_expected_energy_saving) " . " VALUES (" . $db->quote($id, PDO::PARAM_INT) . ", " . $db->quote($request['esu_id_consumption'][$i], PDO::PARAM_INT) . ", " . $db->quote($request['ac_expected_energy_saving'][$i], PDO::PARAM_INT) . ")"; $db->exec($sql); } } $sql = "DELETE FROM ecogis.action_catalog_dependencies WHERE ac_id=" . $db->quote($id, PDO::PARAM_INT); $db->exec($sql); if ($this->act != 'del') { $relatedActions = array(); if (isset($request['related_action_id'])) { for ($i = 0; $i < count($request['related_action_id']); $i++) { array_push($relatedActions, array('related_action_id' => $request['related_action_id'][$i], 'acd_required' => 'D')); } } if (isset($request['related_required_action_id'])) { for ($i = 0; $i < count($request['related_required_action_id']); $i++) { array_push($relatedActions, array('related_action_id' => $request['related_required_action_id'][$i], 'acd_required' => 'R')); } } if (isset($request['related_excluded_action_id'])) { for ($i = 0; $i < count($request['related_excluded_action_id']); $i++) { array_push($relatedActions, array('related_action_id' => $request['related_excluded_action_id'][$i], 'acd_required' => 'E')); } } foreach ($relatedActions as $relatedAction) { $sql = "INSERT INTO ecogis.action_catalog_dependencies (ac_id, ac_related_id, acd_type) " . " VALUES (" . $db->quote($id, PDO::PARAM_INT) . ", " . $db->quote($relatedAction['related_action_id'], PDO::PARAM_INT) . ", " . $db->quote($relatedAction['acd_required'], PDO::PARAM_BOOL) . ")"; if ($relatedAction['related_action_id'] > 0) { $db->exec($sql); } } } $sql = "DELETE FROM ecogis.action_catalog_benefit_year WHERE ac_id=" . $db->quote($id); $db->exec($sql); if ($this->act != 'del') { if (isset($request['enable_benefit_year']) && $request['enable_benefit_year'] == 'T') { for ($i = 0; $i < count($request['benefit_year']); $i++) { $year = forceInteger($request['benefit_year'][$i]); $benefit = forceFloat($request['benefit_benefit'][$i], null, '.'); if ($year > 1970 && $request['benefit_benefit'][$i] != '') { $sql = "INSERT INTO ecogis.action_catalog_benefit_year (ac_id, acby_year, acby_benefit) " . "VALUES ({$id}, {$year}, {$benefit})"; $db->exec($sql); } } } } $db->commit(); R3EcoGisCacheHelper::resetMapPreviewCache(null); R3EcoGisEventNotifier::notifyDataChanged($this, array('data_changed' => true)); if ($this->bu_id == '') { return array('status' => R3_AJAX_NO_ERROR, 'js' => "submitFormDataDoneActionCatalog({$id})"); } else { return array('status' => R3_AJAX_NO_ERROR, 'js' => "submitFormDataDoneActionCatalogFromBuilding({$id})"); } } }