236 lines
8.7 KiB
PHP
Executable File
236 lines
8.7 KiB
PHP
Executable File
<?php
|
|
|
|
if (!defined('sugarEntry') || !sugarEntry)
|
|
die('-1');
|
|
|
|
if (!$_REQUEST ['job'] || $_REQUEST ['job'] == '')
|
|
die('-1');
|
|
|
|
switch ($_REQUEST ['job']) {
|
|
case 'getCodeTemplate':
|
|
getCodeTemplate($_REQUEST['id']);
|
|
break;
|
|
case 'saveCodeTemplate':
|
|
saveCodeTemplate($_REQUEST['id_template'], $_REQUEST['html_template'], $_REQUEST['aditional_fields'], $_REQUEST['margin_top'], $_REQUEST['margin_bottom'], $_REQUEST['margin_left'], $_REQUEST['margin_right']);
|
|
break;
|
|
case 'addSaveCodeTemplate':
|
|
addSaveCodeTemplate($_REQUEST['label']);
|
|
break;
|
|
case 'removeCodeTemplate':
|
|
removeCodeTemplate($_REQUEST['id_template']);
|
|
break;
|
|
case 'editLabelCodeTemplate':
|
|
editLabelCodeTemplate($_REQUEST['label'],$_REQUEST['id_template']);
|
|
break;
|
|
case 'getFilledCodeTemplate':
|
|
getFilledCodeTemplate($_REQUEST['id'], $_REQUEST['data']);
|
|
break;
|
|
case 'getListCodeTemplate':
|
|
getListCodeTemplate();
|
|
break;
|
|
case 'getParseFields':
|
|
getParseFields($_REQUEST['id_template']);
|
|
break;
|
|
default:
|
|
print 'Nie ma takiej akcji';
|
|
break;
|
|
}
|
|
|
|
function editLabelCodeTemplate($label, $id_template){
|
|
$db = $GLOBALS ['db'];
|
|
$update_query = "UPDATE operating_values SET value0='" . ($label). "' WHERE id ='" . $id_template . "'";
|
|
$db->query($update_query);
|
|
print ("Pomyślnie zmieniono nazwę szablonu.");
|
|
}
|
|
|
|
function getParseFields($id_template){
|
|
$db = $GLOBALS ['db'];
|
|
$parser_fields = EcmAgreement::get_parse_fields_for_pdf();
|
|
$query = "select * from operating_values where name ='AgreementTemplatesAditionalFields' AND module_name='EcmAgreements' and (value1 =0 OR value1 IS NULL) AND value2='" . $id_template. "'";
|
|
$result = $db->query($query);
|
|
while($row = $db->fetchByAssoc($result)){
|
|
$parser_fields[$row['value0']] = $row['value0'];
|
|
}
|
|
print json_encode($parser_fields);
|
|
}
|
|
|
|
function getCodeTemplate($id) {
|
|
$db = $GLOBALS ['db'];
|
|
$q = 'SELECT text0 as pdf_template,value6 as margin_top, value7 as margin_bottom, value8 as margin_left,value9 as margin_right FROM operating_values WHERE operating_values.name="AgreementTemplates" AND module_name="EcmAgreements" AND id="' . $id . '"';
|
|
$rs = $db->query($q);
|
|
$row = $db->fetchByAssoc($rs);
|
|
$row['pdf_template'] = html_entity_decode($row['pdf_template']);
|
|
$query = "select * from operating_values where name ='AgreementTemplatesAditionalFields' AND module_name='EcmAgreements' and (value1 =0 OR value1 IS NULL) AND value2='" . $id. "'";
|
|
$result = $db->query($query);
|
|
//$row['aditional_fields'] = $query;
|
|
while($roww = $db->fetchByAssoc($result)){
|
|
$row['aditional_fields'][] = $roww;
|
|
}
|
|
|
|
print json_encode($row);
|
|
}
|
|
|
|
function getListCodeTemplate(){
|
|
$db = $GLOBALS ['db'];
|
|
$get_saved_templates_query = "SELECT id AS id,value0 AS template_name FROM operating_values WHERE name='AgreementTemplates' AND module_name='EcmAgreements' AND value1=0 ORDER BY value0";
|
|
$get_saved_templates_result = $db->query($get_saved_templates_query);
|
|
$get_saved_templates_array = array();
|
|
while($row = $db->fetchByAssoc($get_saved_templates_result)){
|
|
$get_saved_templates_array[$row['id']] = $row['template_name'];
|
|
}
|
|
|
|
print json_encode($get_saved_templates_array);
|
|
}
|
|
|
|
function getFilledCodeTemplate($id, $fill_data){
|
|
global $current_language;
|
|
$app_list_strings = return_app_list_strings_language ( $current_language );
|
|
$db = $GLOBALS ['db'];
|
|
include_once ("modules/kwota.php");
|
|
$kwota=new KwotaSlownie();
|
|
$q = 'SELECT text0, value6 as margin_top, value7 as margin_bottom, value8 as margin_left,value9 as margin_right FROM operating_values WHERE operating_values.name="AgreementTemplates" AND module_name="EcmAgreements" AND id="' . $id . '"';
|
|
$rs = $db->query($q);
|
|
$margins = $db->fetchByAssoc($rs);
|
|
$template = $margins['text0'];
|
|
$parser_fields = EcmAgreement::get_parse_fields_for_pdf();
|
|
|
|
foreach($parser_fields as $key => $value){
|
|
$pos = strpos($template, '$' . $key .'$');
|
|
if ($pos !== false) {
|
|
switch($key){
|
|
case 'toStringAmount':
|
|
if($fill_data['amount']==''){
|
|
break;
|
|
}
|
|
$str = str_replace(".","",$fill_data['amount']);
|
|
$str = str_replace(",",".",$str);
|
|
$str = $kwota->convertPrice(floatval ($str));
|
|
$template = str_replace('$' . $key .'$', $str, $template );
|
|
break;
|
|
case 'type':
|
|
$template = str_replace('$' . $key .'$', $app_list_strings['ecmagreements_type_dom'][$fill_data['type']], $template );
|
|
break;
|
|
case 'status':
|
|
$template = str_replace('$' . $key .'$', $app_list_strings['ecmagreements_status_dom'][$fill_data['status']], $template );
|
|
break;
|
|
case 'pagebreak':
|
|
$template = str_replace('$' . $key .'$', '<pagebreak />', $template );
|
|
break;
|
|
default:
|
|
$template = str_replace('$' . $key .'$', $fill_data[$key],$template );
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
$aditional_fields_query = 'SELECT value0 FROM operating_values WHERE operating_values.name="AgreementTemplatesAditionalFields" AND operating_values.module_name="EcmAgreements" AND operating_values.value2="' . $id . '"';
|
|
|
|
$aditional_fields_rs = $db->query($aditional_fields_query);
|
|
while($row = $db->fetchByAssoc($aditional_fields_rs)){
|
|
$pos = strpos($template, '$' . $row['value0'] .'$');
|
|
if ($pos !== false) {
|
|
switch($key){
|
|
default:
|
|
$template = str_replace('$' . $row['value0'] .'$', $fill_data[$row['value0']],$template );
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
print html_entity_decode($template);
|
|
print $template;
|
|
|
|
include_once ("include/MPDF57/mpdf.php");
|
|
$p = new mPDF ( '', 'A4', null, 'helvetica', $margins['margin_left'], $margins['margin_right'], $margins['margin_top'], $margins['margin_bottom'], 5, 5 );
|
|
$p->mirrorMargins = 1;
|
|
$p->WriteHTML ($template);
|
|
$p->Output ('Agreement.pdf', 'F');
|
|
}
|
|
|
|
function saveCodeTemplate($id_template, $html_template, $aditional_fields, $margin_top, $margin_bottom, $margin_left, $margin_right){
|
|
$db = $GLOBALS ['db'];
|
|
$update_query = "UPDATE operating_values SET text0='" . ($html_template). "', value6='" . $margin_top ."', value7='" . $margin_bottom ."', value8='" . $margin_left ."', value9='" . $margin_right ."' WHERE id ='" . $id_template . "'";
|
|
$db->query($update_query);
|
|
|
|
foreach($aditional_fields as $key => $field){
|
|
foreach ($field as $k => $v){
|
|
if($v==''){
|
|
unset($field[$k]);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
$saved_records_query = "SELECT * FROM operating_values WHERE name='AgreementTemplatesAditionalFields' AND value2='" . $id_template . "'";
|
|
$saved_records_results = $db->query($saved_records_query);
|
|
$saved_records = array();
|
|
while($row = $db->fetchByAssoc($saved_records_results)){
|
|
$saved_records[$row['id']] = $row;
|
|
}
|
|
//Zapis nowych rekordow do bazy
|
|
$allQuery = '';
|
|
foreach($aditional_fields as $indeks => $value){
|
|
|
|
|
|
//Sprwadzam czy jest już może w bazie
|
|
if(isset($saved_records[$value['id']])){
|
|
// jak tak to robie update
|
|
$query = "UPDATE operating_values SET value0 = '" . $value['value0'] . "'";
|
|
$query .= ", value3 = '" . $value['value3'] . "' WHERE id = '" . $value['id'] . "';";
|
|
// i usuwam z listy do usuniecia
|
|
$allQuery .= $query;
|
|
unset($saved_records[$value['id']]);
|
|
}else{
|
|
//jak nie to dodaje
|
|
$value['value1'] = '0';
|
|
$keys = array_keys ($value);
|
|
$values = array_values($value);
|
|
|
|
$query = "INSERT INTO operating_values (" . implode(",", $keys) . ") VALUES ('" . implode("','", $values) ."');";
|
|
$allQuery .= $query;
|
|
}
|
|
//zapis do bazy
|
|
$db->query($query);
|
|
}
|
|
//usuwamy niepotrzebne rekordy z bazy
|
|
if(count($saved_records)>0){
|
|
$delete_query = "UPDATE operating_values SET value1 = '1' WHERE id IN ('";
|
|
$delete_id_array = null;
|
|
foreach($saved_records as $indeks => $value){
|
|
$delete_id_array[] = $value['id'];
|
|
}
|
|
$values = array_values ($delete_id_array);
|
|
$delete_query .= implode("','" ,$values) . "');";
|
|
$allQuery .= $delete_query;
|
|
$db->query($delete_query);
|
|
}
|
|
print ($allQuery);
|
|
}
|
|
|
|
function addSaveCodeTemplate($label){
|
|
$db = $GLOBALS ['db'];
|
|
$count_query = "SELECT count(*) as count FROM operating_values WHERE value0='" . ($label). "' AND operating_values.name='AgreementTemplates' AND module_name='EcmAgreements' AND value1=0";
|
|
$rs =$db->query($count_query);
|
|
$row = $db->fetchByAssoc($rs);
|
|
if($row['count'] > 0){
|
|
print 1;
|
|
return;
|
|
}
|
|
$insert_query = "INSERT INTO operating_values (name,module_name,value0,value1,value6,value7,value8,value9) VALUES ('AgreementTemplates', 'EcmAgreements','" .$label ."', 0,20,20,20,20)";
|
|
$rs =$db->query($insert_query);
|
|
$id_query = "SELECT id FROM operating_values WHERE name='AgreementTemplates' AND module_name='EcmAgreements' AND value1=0 AND value0='" .$label ."'";
|
|
$rs =$db->query($id_query);
|
|
$row = $db->fetchByAssoc($rs);
|
|
print($row['id']);
|
|
return;
|
|
}
|
|
|
|
function removeCodeTemplate($id_template){
|
|
$db = $GLOBALS ['db'];
|
|
$delete_query = "UPDATE operating_values SET value1=1 WHERE id='" . ($id_template). "'";
|
|
$rs =$db->query($delete_query);
|
|
print("Usunięto szablon.");
|
|
return;
|
|
}
|
|
|
|
?>
|