-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax-update-calculated-fields.php
137 lines (119 loc) · 3.67 KB
/
ajax-update-calculated-fields.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
define('PREPEND_PATH', '');
$app_dir = dirname(__FILE__);
include("{$app_dir}/defaultLang.php");
include("{$app_dir}/language.php");
include("{$app_dir}/lib.php");
/*
* calculated fields configuration array, $calc:
* table => [calculated fields], ..
* where calculated fields:
* field => query, ...
*/
$calc = array(
'consultant' => array(
'nom_complet' => 'SELECT CONCAT(`consultant`.`Prenom`,` `,`consultant`.`Nom`) FROM `consultant`
WHERE `consultant`.`id_consultant`=\'%ID%\'',
),
'missions' => array(
),
'competences_individuelles' => array(
),
'client' => array(
),
'competences_ref' => array(
),
'domaine' => array(
),
'filiere' => array(
),
'niveaux_ref' => array(
),
'carriere_consultant' => array(
),
'formation_suivi' => array(
),
'feedback' => array(
),
'emploi_fonctionnel' => array(
),
'tags' => array(
),
);
cleanup_calc_fields($calc);
list($table, $id) = get_params();
if(!$table || !strlen($id))
return_json(array(), 'Access denied or invalid parameters');
if(!isset($calc[$table]))
return_json(array('table' => $table), 'No fields to calculate in this table');
/*
update_calc_fields($table, $id, $calc[$table])
then, for each parent of $table and its parent's $parent_id
stored in record $id of $table:
update_calc_fields($parent_table, $parent_id, $calc[$parent_table])
*/
$caluclations_made = array();
$caluclations_made[] = update_calc_fields($table, $id, $calc[$table]);
// get parents of current table
$parents = get_parent_tables($table);
$pk = getPKFieldName($table);
$safe_id = makeSafe($id);
foreach($parents as $pt => $mlufs /* main lookup fields in child */) {
if(!isset($calc[$pt])) continue; // parent table has no calc fields
foreach($mlufs as $mluf) {
// retrieve parent record ID as stored in lookup field of current table
$pid = sqlValue("SELECT `{$mluf}` FROM `{$table}` WHERE `{$pk}`='{$safe_id}'");
if(!strlen($pid)) continue;
$caluclations_made[] = update_calc_fields($pt, $pid, $calc[$pt]);
}
}
return_json($caluclations_made);
#############################################################
function update_calc_fields($table, $id, $formulas) {
$mi = getMemberInfo();
$pk = getPKFieldName($table);
$safe_id = makeSafe($id);
$eo = array('silentErrors' => true);
$caluclations_made = array();
$replace = array(
'%ID%' => $safe_id,
'%USERNAME%' => makeSafe($mi['username']),
'%GROUPID%' => makeSafe($mi['groupID']),
'%GROUP%' => makeSafe($mi['group'])
);
foreach($formulas as $field => $query) {
$query = str_replace(array_keys($replace), array_values($replace), $query);
$calc_value = sqlValue($query);
if($calc_value === false) continue;
// update calculated field
$safe_calc_value = makeSafe($calc_value);
$update_query = "UPDATE `{$table}` SET `{$field}`='{$safe_calc_value}' " .
"WHERE `{$pk}`='{$safe_id}'";
$res = sql($update_query, $eo);
if($res) $caluclations_made[] = array(
'table' => $table,
'id' => $id,
'field' => $field,
'value' => $calc_value
);
}
return $caluclations_made;
}
/* get and validate params */
function get_params() {
$ret_error = array(false, false);
$table = $_REQUEST['table'];
$id = $_REQUEST['id'];
if(!get_sql_from($table)) return $ret_error;
if(!check_record_permission($table, $id)) return $ret_error;
return array($table, $id);
}
function return_json($data = array(), $error = '') {
@header('Content-type: application/json');
die(json_encode(array('data' => $data, 'error' => $error)));
}
function cleanup_calc_fields(&$calc) {
foreach($calc as $tn => $conf) {
if(!count($conf)) unset($calc[$tn]);
}
}