Merge fixes due to AX modifications and new M/D data (more updates coming).
[platal.git] / modules / fusionax.php
CommitLineData
b9ad0878
PC
1<?php
2/***************************************************************************
12262f13 3 * Copyright (C) 2003-2011 Polytechnique.org *
b9ad0878
PC
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
c15c0053 22/**
ddc4c642
SJ
23 * Module to merge data from AX database: this will only be used once on
24 * production site and should be removed afterwards.
c15c0053
PC
25 *
26 * Module to import data from another database of alumni that had
27 * different schemas. The organization that used this db is called AX
28 * hence the name of this module.
29 *
ddc4c642 30 * Datas are stored in an export file.
c15c0053 31 */
73be4434
SJ
32class FusionAxModule extends PLModule
33{
b9ad0878
PC
34 function handlers()
35 {
654caec5 36 if (Platal::globals()->merge->state == 'pending') {
12419b5b 37 return array(
bfe9f4c7
SJ
38 'fusionax' => $this->make_hook('index', AUTH_PASSWD, 'admin'),
39 'fusionax/import' => $this->make_hook('import', AUTH_PASSWD, 'admin'),
40 'fusionax/view' => $this->make_hook('view', AUTH_PASSWD, 'admin'),
41 'fusionax/ids' => $this->make_hook('ids', AUTH_PASSWD, 'admin'),
42 'fusionax/deceased' => $this->make_hook('deceased', AUTH_PASSWD, 'admin'),
43 'fusionax/promo' => $this->make_hook('promo', AUTH_PASSWD, 'admin'),
44 'fusionax/names' => $this->make_hook('names', AUTH_PASSWD, 'admin'),
45 'fusionax/edu' => $this->make_hook('edu', AUTH_PASSWD, 'admin'),
46 'fusionax/corps' => $this->make_hook('corps', AUTH_PASSWD, 'admin')
12419b5b 47 );
654caec5 48 } elseif (Platal::globals()->merge->state == 'done') {
12419b5b 49 return array(
bfe9f4c7
SJ
50 'fusionax' => $this->make_hook('index', AUTH_PASSWD, 'admin,edit_directory'),
51 'fusionax/issues' => $this->make_hook('issues', AUTH_PASSWD, 'admin,edit_directory'),
52 'fusionax/issues/deathdate' => $this->make_hook('issues_deathdate', AUTH_PASSWD, 'admin,edit_directory'),
53 'fusionax/issues/promo' => $this->make_hook('issues_promo', AUTH_PASSWD, 'admin,edit_directory'),
12419b5b 54 );
654caec5 55 }
b9ad0878 56 }
c15c0053
PC
57
58
26ba053e 59 function handler_index($page)
b9ad0878 60 {
654caec5
SJ
61 if (Platal::globals()->merge->state == 'pending') {
62 $page->changeTpl('fusionax/index.tpl');
63 } elseif (Platal::globals()->merge->state == 'done') {
12419b5b
SJ
64 $issueList = array(
65 'name' => 'noms',
66 'job' => 'emplois',
67 'address' => 'adresses',
68 'promo' => 'promotions',
69 'deathdate' => 'dates de décès',
70 'phone' => 'téléphones',
71 'education' => 'formations',
72 );
654caec5
SJ
73 $issues = XDB::rawFetchOneAssoc("SELECT COUNT(*) AS total,
74 SUM(FIND_IN_SET('name', issues)) DIV 1 AS name,
75 SUM(FIND_IN_SET('job', issues)) DIV 2 AS job,
76 SUM(FIND_IN_SET('address', issues)) DIV 3 AS address,
77 SUM(FIND_IN_SET('promo', issues)) DIV 4 AS promo,
78 SUM(FIND_IN_SET('deathdate', issues)) DIV 5 AS deathdate,
79 SUM(FIND_IN_SET('phone', issues)) DIV 6 AS phone,
80 SUM(FIND_IN_SET('education', issues)) DIV 7 AS education
81 FROM profile_merge_issues
82 WHERE issues IS NOT NULL OR issues != ''");
654caec5 83 $page->changeTpl('fusionax/issues.tpl');
12419b5b
SJ
84 $page->assign('issues', $issues);
85 $page->assign('issueList', $issueList);
654caec5 86 }
b9ad0878 87 }
22f043e4 88
73be4434 89 /** Import de l'annuaire de l'AX depuis l'export situé dans le home de jacou */
26ba053e 90 function handler_import($page, $action = 'index', $file = '')
b9ad0878
PC
91 {
92 if ($action == 'index') {
93 $page->changeTpl('fusionax/import.tpl');
b9ad0878
PC
94 return;
95 }
22f043e4 96
cc8ea8b2 97 // toutes les actions sont faites en ajax en utilisant jquery
ddc4c642 98 header('Content-type: text/javascript; charset=utf-8');
22f043e4 99
cc8ea8b2
PC
100 // log des actions
101 $report = array();
22f043e4 102
73be4434 103 $modulepath = realpath(dirname(__FILE__) . '/fusionax/') . '/';
ddc4c642 104 $spoolpath = realpath(dirname(__FILE__) . '/../spool/fusionax/') . '/';
22f043e4 105
b9ad0878 106 if ($action == 'launch') {
ddc4c642
SJ
107 if ($file == '') {
108 $report[] = 'Nom de fichier non renseigné.';
109 } elseif (!file_exists(dirname(__FILE__) . '/../spool/fusionax/' . $file)) {
110 $report[] = 'Le fichier ne se situe pas au bon endroit.';
111 } else {
112 // séparation de l'archive en fichiers par tables
113 $file = $spoolpath . $file;
ddc4c642
SJ
114 // Split export into specialised files
115 exec('grep "^AD" ' . $file . ' > ' . $spoolpath . 'Adresses.txt');
116 exec('grep "^AN" ' . $file . ' > ' . $spoolpath . 'Anciens.txt');
0efb08e6
SJ
117 exec('grep "^FO.[0-9]\{4\}[MD][0-9]\{3\}.Etudiant" ' . $file . ' > ' . $spoolpath . 'Formations_MD.txt');
118 exec('grep "^FO.[0-9]\{4\}[MD][0-9]\{3\}.Doct. de" ' . $file . ' >> ' . $spoolpath . 'Formations_MD.txt');
ddc4c642
SJ
119 exec('grep "^FO" ' . $file . ' > ' . $spoolpath . 'Formations.txt');
120 exec('grep "^AC" ' . $file . ' > ' . $spoolpath . 'Activites.txt');
121 exec('grep "^EN" ' . $file . ' > ' . $spoolpath . 'Entreprises.txt');
122 exec($modulepath . 'formation.pl');
123 exec('mv -f ' . $spoolpath . 'Formations_out.txt ' . $spoolpath . 'Formations.txt');
0efb08e6 124 exec('mv -f ' . $spoolpath . 'Formations_MD_out.txt ' . $spoolpath . 'Formations_MD.txt');
ddc4c642
SJ
125 $report[] = 'Fichier parsé.';
126 $report[] = 'Import dans la base en cours...';
127 $next = 'integrateSQL';
9a2876cb
SJ
128 XDB::execute("UPDATE profiles
129 SET ax_id = NULL
130 WHERE ax_id = ''");
ddc4c642 131 }
73be4434 132 } elseif ($action == 'integrateSQL') {
cc8ea8b2
PC
133 // intégration des données dans la base MySQL
134 // liste des fichiers sql à exécuter
135 $filesSQL = array(
ddc4c642
SJ
136 0 => 'Activites.sql',
137 1 => 'Adresses.sql',
138 2 => 'Anciens.sql',
139 3 => 'Formations.sql',
0efb08e6
SJ
140 4 => 'Entreprises.sql',
141 5 => 'Formations_MD.sql'
ddc4c642
SJ
142 );
143 if ($file != '') {
cc8ea8b2 144 // récupère le contenu du fichier sql
ddc4c642 145 $queries = explode(';', file_get_contents($modulepath . $filesSQL[$file]));
73be4434
SJ
146 foreach ($queries as $q) {
147 if (trim($q)) {
148 // coupe le fichier en requêtes individuelles
149 if (substr($q, 0, 2) == '--') {
150 // affiche les commentaires dans le report
151 $lines = explode("\n", $q);
152 $l = $lines[0];
153 $report[] = addslashes($l);
154 }
155 // exécute la requête
ddc4c642 156 XDB::execute(str_replace('{?}', $spoolpath, $q));
b9ad0878 157 }
b9ad0878 158 }
cc8ea8b2 159 // trouve le prochain fichier à exécuter
ddc4c642 160 $nextfile = $file + 1;
b9ad0878
PC
161 } else {
162 $nextfile = 0;
163 }
0efb08e6 164 if ($nextfile > 5) {
ddc4c642
SJ
165 // tous les fichiers ont été exécutés, on passe à l'étape suivante
166 $next = 'adds1920';
b9ad0878 167 } else {
cc8ea8b2 168 // on passe au fichier suivant
ddc4c642
SJ
169 $next = 'integrateSQL/' . $nextfile;
170 }
171 } elseif ($action == 'adds1920') {
172 // Adds promotion 1920 from AX db.
173 $report[] = 'Ajout de la promotion 1920';
174 $res = XDB::iterator('SELECT prenom, Nom_complet, ax_id
175 FROM fusionax_anciens
176 WHERE promotion_etude = 1920;');
177
ddc4c642
SJ
178 $eduSchools = DirEnum::getOptions(DirEnum::EDUSCHOOLS);
179 $eduSchools = array_flip($eduSchools);
180 $eduDegrees = DirEnum::getOptions(DirEnum::EDUDEGREES);
181 $eduDegrees = array_flip($eduDegrees);
182 $degreeid = $eduDegrees[Profile::DEGREE_X];
183 $entry_year = 1920;
184 $grad_year = 1923;
185 $promo = 'X1920';
186 $sex = PlUser::GENDER_MALE;
187 $xorgId = 19200000;
188 $type = 'x';
189
190 while (list($firstname, $lastname, $ax_id) = $res->next()) {
191 $hrid = self::getHrid($firstname, $lastname, $promo);
192 $res1 = XDB::query('SELECT COUNT(*)
193 FROM accounts
194 WHERE hruid = {?}', $hrid);
195 $res2 = XDB::query('SELECT COUNT(*)
196 FROM profiles
197 WHERE hrpid = {?}', $hrid);
198 if (is_null($hrid) || $res1->fetchOneCell() > 0 || $res2->fetchOneCell() > 0) {
199 $report[] = $ax_id . ' non ajouté';
200 }
201 $fullName = $firstname . ' ' . $lastname;
202 $directoryName = $lastname . ' ' . $firstname;
203 ++$xorgId;
204
00ba8a74
SJ
205 XDB::execute('INSERT INTO profiles (hrpid, xorg_id, ax_id, sex)
206 VALUES ({?}, {?}, {?}, {?})',
ddc4c642
SJ
207 $hrid, $xorgId, $ax_id, $sex);
208 $pid = XDB::insertId();
4caad881 209 XDB::execute('INSERT INTO profile_public_names (pid, lastname_initial, firstname_initial)
00ba8a74 210 VALUES ({?}, {?}, {?})',
4caad881 211 $pid, $lastname, $firstname);
00ba8a74
SJ
212 XDB::execute('INSERT INTO profile_display (pid, yourself, public_name, private_name,
213 directory_name, short_name, sort_name, promo)
214 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
ddc4c642 215 $pid, $firstname, $fullName, $fullName, $directoryName, $fullName, $directoryName, $promo);
00ba8a74
SJ
216 XDB::execute('INSERT INTO profile_education (pid, eduid, degreeid, entry_year, grad_year, flags)
217 VALUES ({?}, {?}, {?}, {?}, {?}, {?})',
ddc4c642 218 $pid, $eduSchools[Profile::EDU_X], $degreeid, $entry_year, $grad_year, 'primary');
ad1c9939
SJ
219 XDB::execute('INSERT INTO accounts (hruid, type, is_admin, state, full_name, directory_name, display_name, lastname, firstname, sex)
220 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
221 $hrid, $type, 0, 'active', $fullName, $directoryName, $firstname, $lastname, $firstname, $sex);
ddc4c642 222 $uid = XDB::insertId();
00ba8a74
SJ
223 XDB::execute('INSERT INTO account_profiles (uid, pid, perms)
224 VALUES ({?}, {?}, {?})',
ddc4c642 225 $uid, $pid, 'owner');
b9ad0878 226 }
ddc4c642
SJ
227 $report[] = 'Promo 1920 ajoutée.';
228 $next = 'view';
229 } elseif ($action == 'view') {
230 XDB::execute('CREATE OR REPLACE ALGORITHM=MERGE VIEW fusionax_xorg_anciens AS
231 SELECT p.pid, p.ax_id, pd.promo, pd.private_name, pd.public_name,
232 pd.sort_name, pd.short_name, pd.directory_name
233 FROM profiles AS p
ad1c9939 234 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)');
ddc4c642 235 $next = 'clean';
73be4434 236 } elseif ($action == 'clean') {
cc8ea8b2 237 // nettoyage du fichier temporaire
0efb08e6 238 //exec('rm -Rf ' . $spoolpath);
ddc4c642 239 $report[] = 'Import finit.';
b9ad0878 240 }
73be4434 241 foreach($report as $t) {
cc8ea8b2 242 // affiche les lignes de report
ddc4c642 243 echo "$('#fusionax').append('" . $t . "<br/>');\n";
73be4434 244 }
b9ad0878 245 if (isset($next)) {
cc8ea8b2 246 // lance le prochain script s'il y en a un
ddc4c642 247 echo "$.getScript('fusionax/import/" . $next . "');";
b9ad0878 248 }
cc8ea8b2 249 // exit pour ne pas afficher la page template par défaut
b9ad0878
PC
250 exit;
251 }
22f043e4 252
26ba053e 253 function handler_view($page, $action = '')
29459c49 254 {
29459c49
SJ
255 $page->changeTpl('fusionax/view.tpl');
256 if ($action == 'create') {
257 XDB::execute('DROP VIEW IF EXISTS fusionax_deceased');
723ea674 258 XDB::execute("CREATE VIEW fusionax_deceased AS
ddc4c642
SJ
259 SELECT p.pid, a.ax_id, pd.private_name, pd.promo, p.deathdate AS deces_xorg, a.Date_deces AS deces_ax
260 FROM profiles AS p
261 INNER JOIN profile_display AS pd ON (p.pid = pd.pid)
262 INNER JOIN fusionax_anciens AS a ON (a.ax_id = p.ax_id)
723ea674 263 WHERE p.deathdate != a.Date_deces OR (p.deathdate IS NULL AND a.Date_deces != '0000-00-00')");
29459c49
SJ
264 XDB::execute('DROP VIEW IF EXISTS fusionax_promo');
265 XDB::execute('CREATE VIEW fusionax_promo AS
9a2876cb 266 SELECT p.pid, p.ax_id, pd.private_name, pd.promo, pe.entry_year AS promo_etude_xorg, f.groupe_promo,
ddc4c642
SJ
267 f.promotion_etude AS promo_etude_ax, pe.grad_year AS promo_sortie_xorg
268 FROM profiles AS p
269 INNER JOIN profile_display AS pd ON (p.pid = pd.pid)
270 INNER JOIN profile_education AS pe ON (p.pid = pe.pid)
271 INNER JOIN fusionax_anciens AS f ON (p.ax_id = f.ax_id)
272 WHERE pd.promo != CONCAT(\'X\', f.promotion_etude)
273 AND !(f.promotion_etude = pe.entry_year + 1 AND pe.grad_year = pe.entry_year + 4)');
274 $page->trigSuccess('Les VIEW ont bien été créées.');
29459c49
SJ
275 }
276 }
277
278 /* Mets à NULL le matricule_ax de ces camarades pour marquer le fait qu'ils ne figurent pas dans l'annuaire de l'AX */
ddc4c642 279 private static function clear_wrong_in_xorg($pid)
29459c49 280 {
ddc4c642
SJ
281 $res = XDB::execute('UPDATE fusionax_xorg_anciens
282 SET ax_id = NULL
283 WHERE pid = {?}', $pid);
29459c49
SJ
284 if (!$res) {
285 return 0;
286 }
287 return XDB::affectedRows() / 2;
288 }
289
aab2ffdd 290 /* Cherche les les anciens présents dans Xorg avec un matricule_ax ne correspondant à rien dans la base de l'AX
9a2876cb 291 * (mises à part les promo 1921, 1922, 1923, 1924, 1925, 1927 qui ne figurent pas dans les données de l'AX)*/
29459c49
SJ
292 private static function find_wrong_in_xorg($limit = 10)
293 {
ddc4c642 294 return XDB::iterator('SELECT u.promo, u.pid, u.private_name
29459c49
SJ
295 FROM fusionax_xorg_anciens AS u
296 WHERE NOT EXISTS (SELECT *
297 FROM fusionax_anciens AS f
ddc4c642 298 WHERE f.ax_id = u.ax_id)
9a2876cb 299 AND u.ax_id IS NOT NULL AND promo NOT IN (\'X1921\', \'X1922\', \'X1923\', \'X1924\', \'X1925\', \'X1927\')');
29459c49
SJ
300 }
301
b65beb64
PC
302 /** Lier les identifiants d'un ancien dans les deux annuaires
303 * @param user_id identifiant dans l'annuaire X.org
304 * @param matricule_ax identifiant dans l'annuaire de l'AX
305 * @return 0 si la liaison a échoué, 1 sinon
22f043e4 306 */
ddc4c642 307 private static function link_by_ids($pid, $ax_id)
b9ad0878 308 {
ddc4c642 309 $res = XDB::execute('UPDATE fusionax_import AS i
73be4434 310 INNER JOIN fusionax_xorg_anciens AS u
ddc4c642
SJ
311 SET u.ax_id = i.ax_id,
312 i.pid = u.pid,
73be4434 313 i.date_match_id = NOW()
ddc4c642
SJ
314 WHERE i.ax_id = {?} AND u.pid = {?}
315 AND (u.ax_id != {?} OR u.ax_id IS NULL
316 OR i.pid != {?} OR i.pid IS NULL)',
317 $ax_id, $pid, $ax_id, $pid);
73be4434 318 if (!$res) {
b9ad0878
PC
319 return 0;
320 }
73be4434 321 return XDB::affectedRows() / 2;
b9ad0878 322 }
22f043e4 323
b65beb64
PC
324 /** Recherche automatique d'anciens à lier entre les deux annuaires
325 * @param limit nombre d'anciens à trouver au max
326 * @param sure si true, ne trouve que des anciens qui sont quasi sûrs
e8591429 327 * @return un XOrgDBIterator sur les entrées avec display_name, promo,
ddc4c642 328 * pid, ax_id et display_name_ax
22f043e4 329 */
b65beb64 330 private static function find_easy_to_link($limit = 10, $sure = false)
b9ad0878 331 {
e8591429 332 $easy_to_link = XDB::iterator("
ddc4c642 333 SELECT u.private_name, u.promo, u.pid, ax.ax_id,
ad1c9939 334 CONCAT(ax.prenom, ' ', ax.nom_complet, ' (', ax.groupe_promo, ax.promotion_etude, ')') AS display_name_ax,
73be4434 335 COUNT(*) AS nbMatches
29459c49 336 FROM fusionax_anciens AS ax
ddc4c642
SJ
337 INNER JOIN fusionax_import AS i ON (i.ax_id = ax.ax_id AND i.pid IS NULL)
338 LEFT JOIN fusionax_xorg_anciens AS u ON (u.ax_id IS NULL
ad1c9939 339 AND u.promo = CONCAT(ax.groupe_promo, ax.promotion_etude)
ddc4c642
SJ
340 AND (CONCAT(ax.prenom, ' ', ax.nom_complet) = u.private_name
341 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.public_name
342 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.short_name))
343 GROUP BY u.pid
344 HAVING u.pid IS NOT NULL AND nbMatches = 1" . ($limit ? (' LIMIT ' . $limit) : ''));
b65beb64
PC
345 if ($easy_to_link->total() > 0 || $sure) {
346 return $easy_to_link;
347 }
73be4434 348 return XDB::iterator("
ddc4c642 349 SELECT u.private_name, u.promo, u.pid, ax.ax_id,
ad1c9939 350 CONCAT(ax.prenom, ' ', ax.nom_complet, ' (', ax.groupe_promo, ax.promotion_etude, ')') AS display_name_ax,
73be4434 351 COUNT(*) AS nbMatches
29459c49 352 FROM fusionax_anciens AS ax
ddc4c642
SJ
353 INNER JOIN fusionax_import AS i ON (i.ax_id = ax.ax_id AND i.pid IS NULL)
354 LEFT JOIN fusionax_xorg_anciens AS u ON (u.ax_id IS NULL
355 AND (CONCAT(ax.prenom, ' ', ax.nom_complet) = u.private_name
356 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.public_name
357 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.short_name)
ad1c9939
SJ
358 AND u.promo < CONCAT(ax.groupe_promo, ax.promotion_etude + 2)
359 AND u.promo > CONCAT(ax.groupe_promo, ax.promotion_etude - 2))
ddc4c642
SJ
360 GROUP BY u.pid
361 HAVING u.pid IS NOT NULL AND nbMatches = 1" . ($limit ? (' LIMIT ' . $limit) : ''));
b9ad0878 362 }
22f043e4 363
b65beb64 364 /** Module de mise en correspondance les ids */
26ba053e 365 function handler_ids($page, $part = 'main', $pid = null, $ax_id = null)
b9ad0878 366 {
29459c49 367 $nbToLink = 100;
29459c49 368 $page->assign('xorg_title', 'Polytechnique.org - Fusion - Mise en correspondance simple');
ddc4c642 369
73be4434 370 if ($part == 'missingInAX') {
b9ad0878
PC
371 // locate all persons from this database that are not in AX's
372 $page->changeTpl('fusionax/idsMissingInAx.tpl');
ddc4c642 373 $missingInAX = XDB::iterator('SELECT promo, pid, private_name
29459c49 374 FROM fusionax_xorg_anciens
ad1c9939
SJ
375 WHERE ax_id IS NULL
376 ORDER BY promo');
b9ad0878
PC
377 $page->assign('missingInAX', $missingInAX);
378 return;
379 }
73be4434 380 if ($part == 'missingInXorg') {
b9ad0878
PC
381 // locate all persons from AX's database that are not here
382 $page->changeTpl('fusionax/idsMissingInXorg.tpl');
ddc4c642 383 $missingInXorg = XDB::iterator("SELECT CONCAT(a.prenom, ' ', a.Nom_usuel) AS private_name,
ad1c9939 384 CONCAT(a.groupe_promo, a.promotion_etude) AS promo, a.ax_id
73be4434 385 FROM fusionax_import
ddc4c642 386 INNER JOIN fusionax_anciens AS a USING (ax_id)
ad1c9939
SJ
387 WHERE fusionax_import.pid IS NULL
388 ORDER BY promo");
b9ad0878
PC
389 $page->assign('missingInXorg', $missingInXorg);
390 return;
391 }
29459c49
SJ
392 if ($part == 'wrongInXorg') {
393 // locate all persons from Xorg database that have a bad AX id
394 $page->changeTpl('fusionax/idswrongInXorg.tpl');
395 $wrongInXorg = FusionAxModule::find_wrong_in_xorg($nbToLink);
396 $page->assign('wrongInXorg', $wrongInXorg);
397 return;
398 }
399 if ($part == 'cleanwronginxorg') {
400 $linksToDo = FusionAxModule::find_wrong_in_xorg($nbToLink);
401 while ($l = $linksToDo->next()) {
ddc4c642 402 FusionAxModule::clear_wrong_in_xorg($l['pid']);
29459c49
SJ
403 }
404 pl_redirect('fusionax/ids/wrongInXorg');
405 }
406 if ($part == 'lier') {
407 if (Post::has('user_id') && Post::has('matricule_ax')) {
ddc4c642 408 FusionAxModule::link_by_ids(Post::i('pid'), Post::v('ax_id'));
29459c49
SJ
409 }
410 }
73be4434 411 if ($part == 'link') {
ddc4c642 412 FusionAxModule::link_by_ids($pid, $ax_id);
b65beb64 413 exit;
b9ad0878 414 }
73be4434 415 if ($part == 'linknext') {
29459c49 416 $linksToDo = FusionAxModule::find_easy_to_link($nbToLink);
73be4434 417 while ($l = $linksToDo->next()) {
ddc4c642 418 FusionAxModule::link_by_ids($l['pid'], $l['ax_id']);
b9ad0878 419 }
b65beb64 420 pl_redirect('fusionax/ids#autolink');
b9ad0878 421 }
73be4434 422 if ($part == 'linkall') {
b9ad0878 423 $linksToDo = FusionAxModule::find_easy_to_link(0);
73be4434 424 while ($l = $linksToDo->next()) {
ddc4c642 425 FusionAxModule::link_by_ids($l['pid'], $l['ax_id']);
b9ad0878
PC
426 }
427 }
428 {
429 $page->changeTpl('fusionax/ids.tpl');
73be4434 430 $missingInAX = XDB::query('SELECT COUNT(*)
ddc4c642
SJ
431 FROM fusionax_xorg_anciens
432 WHERE ax_id IS NULL');
73be4434 433 if ($missingInAX) {
b9ad0878
PC
434 $page->assign('nbMissingInAX', $missingInAX->fetchOneCell());
435 }
73be4434 436 $missingInXorg = XDB::query('SELECT COUNT(*)
ddc4c642
SJ
437 FROM fusionax_import
438 WHERE pid IS NULL');
73be4434
SJ
439 if ($missingInXorg) {
440 $page->assign('nbMissingInXorg', $missingInXorg->fetchOneCell());
b9ad0878 441 }
29459c49
SJ
442 $wrongInXorg = FusionAxModule::find_wrong_in_xorg($nbToLink);
443 if ($wrongInXorg->total() > 0) {
444 $page->assign('wrongInXorg', $wrongInXorg->total());
445 }
446 $easyToLink = FusionAxModule::find_easy_to_link($nbToLink);
447 if ($easyToLink->total() > 0) {
448 $page->assign('nbMatch', $easyToLink->total());
b9ad0878
PC
449 $page->assign('easyToLink', $easyToLink);
450 }
451 }
452 }
22f043e4 453
26ba053e 454 function handler_deceased($page, $action = '')
b9ad0878 455 {
29459c49
SJ
456 if ($action == 'updateXorg') {
457 XDB::execute('UPDATE fusionax_deceased
458 SET deces_xorg = deces_ax
22648cc5 459 WHERE deces_xorg IS NULL');
29459c49
SJ
460 }
461 if ($action == 'updateAX') {
462 XDB::execute('UPDATE fusionax_deceased
463 SET deces_ax = deces_xorg
464 WHERE deces_ax = "0000-00-00"');
465 }
466 if ($action == 'update') {
ddc4c642 467 if (Post::has('pid') && Post::has('date')) {
29459c49
SJ
468 XDB::execute('UPDATE fusionax_deceased
469 SET deces_ax = {?}, deces_xorg = {?}
ddc4c642
SJ
470 WHERE pid = {?}',
471 Post::v('date'), Post::v('date'), Post::i('pid'));
29459c49
SJ
472 }
473 }
474 $page->changeTpl('fusionax/deceased.tpl');
b9ad0878 475 // deceased
73be4434
SJ
476 $deceasedErrorsSql = XDB::query('SELECT COUNT(*) FROM fusionax_deceased');
477 $page->assign('deceasedErrors', $deceasedErrorsSql->fetchOneCell());
ddc4c642
SJ
478 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_ax
479 FROM fusionax_deceased
22648cc5 480 WHERE deces_xorg IS NULL
29459c49
SJ
481 LIMIT 10');
482 $page->assign('nbDeceasedMissingInXorg', $res->total());
483 $page->assign('deceasedMissingInXorg', $res);
ddc4c642
SJ
484 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_xorg
485 FROM fusionax_deceased
486 WHERE deces_ax = "0000-00-00"
29459c49
SJ
487 LIMIT 10');
488 $page->assign('nbDeceasedMissingInAX', $res->total());
489 $page->assign('deceasedMissingInAX', $res);
ddc4c642
SJ
490 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_xorg, deces_ax
491 FROM fusionax_deceased
492 WHERE deces_xorg != "0000-00-00" AND deces_ax != "0000-00-00"');
29459c49
SJ
493 $page->assign('nbDeceasedDifferent', $res->total());
494 $page->assign('deceasedDifferent', $res);
b9ad0878 495 }
c7eac294 496
26ba053e 497 function handler_promo($page, $action = '')
c7eac294
SJ
498 {
499 $page->changeTpl('fusionax/promo.tpl');
9a2876cb 500 $res = XDB::iterator("SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
c7eac294 501 FROM fusionax_promo
a3f12425
SJ
502 WHERE !(promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 3 = promo_sortie_xorg)
503 AND !(promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 4 = promo_sortie_xorg)
9a2876cb
SJ
504 AND !(promo_etude_ax = promo_etude_xorg + 1) AND groupe_promo = 'X'
505 ORDER BY promo_etude_xorg");
c7eac294 506 $nbMissmatchingPromos = $res->total();
a3f12425
SJ
507 $page->assign('nbMissmatchingPromos', $res->total());
508 $page->assign('missmatchingPromos', $res);
509
9a2876cb 510 $res = XDB::iterator("SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
a3f12425 511 FROM fusionax_promo
9a2876cb
SJ
512 WHERE promo_etude_ax = promo_etude_xorg + 1 AND groupe_promo = 'X'
513 ORDER BY promo_etude_xorg");
a3f12425 514 $nbMissmatchingPromos += $res->total();
c7eac294
SJ
515 $page->assign('nbMissmatchingPromos1', $res->total());
516 $page->assign('missmatchingPromos1', $res);
a3f12425 517
9a2876cb 518 $res = XDB::iterator("SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
c7eac294 519 FROM fusionax_promo
9a2876cb
SJ
520 WHERE promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 3 = promo_sortie_xorg AND groupe_promo = 'X'
521 ORDER BY promo_etude_xorg");
c7eac294
SJ
522 $nbMissmatchingPromos += $res->total();
523 $page->assign('nbMissmatchingPromos2', $res->total());
524 $page->assign('missmatchingPromos2', $res);
a3f12425 525
9a2876cb 526 $res = XDB::iterator("SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
a3f12425 527 FROM fusionax_promo
9a2876cb
SJ
528 WHERE promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 4 = promo_sortie_xorg AND groupe_promo = 'X'
529 ORDER BY promo_etude_xorg");
a3f12425
SJ
530 $nbMissmatchingPromos += $res->total();
531 $page->assign('nbMissmatchingPromos3', $res->total());
532 $page->assign('missmatchingPromos3', $res);
533
534 $page->assign('nbMissmatchingPromosTotal', $nbMissmatchingPromos);
535 }
536
26ba053e 537 function handler_names($page, $action = '')
a3f12425
SJ
538 {
539 $page->changeTpl('fusionax/names.tpl');
540
541 $res = XDB::query('SELECT COUNT(*)
542 FROM fusionax_anciens AS f
543 INNER JOIN profiles AS p ON (f.ax_id = p.ax_id)');
544 $page->assign('total', $res->fetchOneCell());
545
4caad881
SJ
546 $res = XDB::rawFetchOneCell("SELECT COUNT(*)
547 FROM fusionax_anciens AS f
548 INNER JOIN profiles AS p ON (f.ax_id = p.ax_id)
549 WHERE IF(f.partic_patro, CONCAT(f.partic_patro, CONCAT(' ', f.Nom_patronymique)), f.Nom_patronymique) NOT IN (p.lastname_initial, p.lastname_main, p.lastname_marital, p.lastname_ordinary)
550 OR IF(f.partic_nom, CONCAT(f.partic_nom, CONCAT(' ', f.Nom_usuel)), f.Nom_usuel) NOT IN (p.lastname_initial, p.lastname_main, p.lastname_marital, p.lastname_ordinary)
551 OR f.Nom_complet NOT IN (p.lastname_initial, p.lastname_main, p.lastname_marital, p.lastname_ordinary)");
a3f12425
SJ
552 $page->assign('lastnameIssues', $res->fetchOneCell());
553
4caad881
SJ
554 $res = XDB::rawFetchOneCell('SELECT COUNT(*)
555 FROM fusionax_anciens AS f
556 INNER JOIN profiles AS p ON (f.ax_id = p.ax_id)
557 WHERE f.prenom NOT IN (p.firstname_initial, p.firstname_main, p.firstname_ordinary)');
a3f12425
SJ
558 $page->assign('firstnameIssues', $res->fetchOneCell());
559
c7eac294 560 }
12419b5b 561
26ba053e 562 function handler_edu($page, $action = '')
2fad6caa
SJ
563 {
564 $page->changeTpl('fusionax/education.tpl');
565
95b1a6d8 566 $missingEducation = XDB::rawIterator("SELECT DISTINCT(f.Intitule_formation)
2fad6caa 567 FROM fusionax_formations AS f
95b1a6d8
SJ
568 WHERE f.Intitule_formation != '' AND NOT EXISTS (SELECT *
569 FROM profile_education_enum AS e
570 WHERE f.Intitule_formation = e.name)");
571 $missingDegree = XDB::rawIterator("SELECT DISTINCT(f.Intitule_diplome)
2fad6caa 572 FROM fusionax_formations AS f
95b1a6d8
SJ
573 WHERE f.Intitule_diplome != '' AND NOT EXISTS (SELECT *
574 FROM profile_education_degree_enum AS e
575 WHERE f.Intitule_diplome = e.abbreviation)");
576 $missingCouple = XDB::rawIterator("SELECT DISTINCT(f.Intitule_formation) AS edu, f.Intitule_diplome AS degree, ee.id AS eduid, de.id AS degreeid
2fad6caa 577 FROM fusionax_formations AS f
95b1a6d8
SJ
578 INNER JOIN profile_education_enum AS ee ON (f.Intitule_formation = ee.name)
579 INNER JOIN profile_education_degree_enum AS de ON (f.Intitule_diplome = de.abbreviation)
2fad6caa
SJ
580 WHERE f.Intitule_diplome != '' AND f.Intitule_formation != ''
581 AND NOT EXISTS (SELECT *
582 FROM profile_education_degree AS d
583 WHERE ee.id = d.eduid AND de.id = d.degreeid)");
584
585 $page->assign('missingEducation', $missingEducation);
586 $page->assign('missingDegree', $missingDegree);
587 $page->assign('missingCouple', $missingCouple);
588 $page->assign('missingEducationCount', $missingEducation->total());
589 $page->assign('missingDegreeCount', $missingDegree->total());
590 $page->assign('missingCoupleCount', $missingCouple->total());
591 }
592
26ba053e 593 function handler_corps($page)
551e00c1
SJ
594 {
595 $page->changeTpl('fusionax/corps.tpl');
596
597 $missingCorps = XDB::rawIterator('SELECT DISTINCT(f.corps_sortie) AS name
598 FROM fusionax_anciens AS f
599 WHERE NOT EXISTS (SELECT *
600 FROM profile_corps_enum AS c
601 WHERE f.corps_sortie = c.abbreviation)');
602 $missingGrade = XDB::rawIterator('SELECT DISTINCT(f.grade) AS name
603 FROM fusionax_anciens AS f
604 WHERE NOT EXISTS (SELECT *
605 FROM profile_corps_rank_enum AS c
606 WHERE f.grade = c.name)');
607
608 $page->assign('missingCorps', $missingCorps);
609 $page->assign('missingGrade', $missingGrade);
610 $page->assign('missingCorpsCount', $missingCorps->total());
611 $page->assign('missingGradeCount', $missingGrade->total());
612 }
613
26ba053e 614 function handler_issues_deathdate($page, $action = '')
12419b5b
SJ
615 {
616 $page->changeTpl('fusionax/deathdate_issues.tpl');
617 if ($action == 'edit') {
618 S::assert_xsrf_token();
619
620 $issues = XDB::rawIterRow('SELECT p.pid, pd.directory_name, pd.promo, pm.deathdate_ax, p.deathdate
621 FROM profile_merge_issues AS pm
622 INNER JOIN profiles AS p ON (pm.pid = p.pid)
623 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
624 WHERE FIND_IN_SET(\'deathdate\', pm.issues)
625 ORDER BY pd.directory_name');
626 while (list($pid, $name, $promo, $deathAX, $deathXorg) = $issues->next()) {
627 $choiceAX = Post::has('AX_' . $pid);
628 $choiceXorg = Post::has('XORG_' . $pid);
629 if (!($choiceAX || $choiceXorg)) {
630 continue;
631 }
632
633 if ($choiceAX) {
634 XDB::execute('UPDATE profiles AS p
635 INNER JOIN profile_merge_issues AS pm ON (pm.pid = p.pid)
636 SET p.deathdate = pm.deathdate_ax, p.deathdate_rec = NOW()
637 WHERE p.pid = {?}', $pid);
638 }
639 XDB::execute("UPDATE profile_merge_issues
640 SET issues = REPLACE(issues, 'deathdate', '')
641 WHERE pid = {?}", $pid());
642 $page->trigSuccess("La date de décès de $name ($promo) a bien été corrigée.");
643 }
644 }
645
646 $issues = XDB::rawFetchAllAssoc('SELECT p.pid, p.hrpid, pd.directory_name, pd.promo, pm.deathdate_ax, p.deathdate
647 FROM profile_merge_issues AS pm
648 INNER JOIN profiles AS p ON (pm.pid = p.pid)
649 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
650 WHERE FIND_IN_SET(\'deathdate\', pm.issues)
651 ORDER BY pd.directory_name');
652 $page->assign('issues', $issues);
653 $page->assign('total', count($issues));
654 }
655
26ba053e 656 function handler_issues_promo($page, $action = '')
12419b5b
SJ
657 {
658 $page->changeTpl('fusionax/promo_issues.tpl');
659 if ($action == 'edit') {
660 S::assert_xsrf_token();
661
662 $issues = XDB::rawIterRow('SELECT p.pid, pd.directory_name, pd.promo, pm.entry_year_ax, pe.entry_year, pe.grad_year
663 FROM profile_merge_issues AS pm
664 INNER JOIN profiles AS p ON (pm.pid = p.pid)
665 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
666 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
667 WHERE FIND_IN_SET(\'promo\', pm.issues)
668 ORDER BY pd.directory_name');
669 while (list($pid, $name, $promo, $deathAX, $deathXorgEntry, $deathXorgGrad) = $issues->next()) {
670 $choiceXorg = Post::has('XORG_' . $pid);
671 if (!(Post::has('display_' . $pid) && Post::has('entry_' . $pid) && Post::has('grad_' . $pid))) {
672 continue;
673 }
674
675 $display = Post::i('display_' . $pid);
676 $entry = Post::i('entry_' . $pid);
677 $grad = Post::i('grad_' . $pid);
678 if (!(($grad <= $entry + 5 && $grad >= $entry + 3) && ($display >= $entry && $display <= $grad - 3))) {
679 $page->trigError("La promotion de $name n'a pas été corrigée.");
680 continue;
681 }
682 XDB::execute('UPDATE profile_display
683 SET promo = {?}
684 WHERE pid = {?}', 'X' . $display, $pid);
685 XDB::execute('UPDATE profile_education
686 SET entry_year = {?}, grad_year = {?}
687 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)', $entry, $grad, $pid);
688 $page->trigSuccess("La promotion de $name a bien été corrigée.");
689 }
690 }
691
692 $issues = XDB::rawFetchAllAssoc('SELECT p.pid, p.hrpid, pd.directory_name, pd.promo, pm.entry_year_ax, pe.entry_year, pe.grad_year
693 FROM profile_merge_issues AS pm
694 INNER JOIN profiles AS p ON (pm.pid = p.pid)
695 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
696 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
697 WHERE FIND_IN_SET(\'promo\', pm.issues)
698 ORDER BY pd.directory_name');
699 $page->assign('issues', $issues);
700 $page->assign('total', count($issues));
701 }
702
26ba053e 703 function handler_issues($page, $action = '')
12419b5b
SJ
704 {
705 static $issueList = array(
706 'name' => 'noms',
707 'phone' => 'téléphones',
708 'education' => 'formations',
709 'address' => 'adresses',
710 'job' => 'emplois'
711 );
d20f50cf
SJ
712 static $typeList = array(
713 'name' => 'general',
714 'phone' => 'general',
715 'education' => 'general',
716 'address' => 'adresses',
717 'job' => 'emploi'
718 );
12419b5b
SJ
719
720 if (!array_key_exists($action, $issueList)) {
721 pl_redirect('fusionax');
722 } else {
723 $total = XDB::fetchOneCell('SELECT COUNT(*)
724 FROM profile_merge_issues
725 WHERE FIND_IN_SET({?}, issues)', $action);
726 if ($total == 0) {
727 pl_redirect('fusionax');
728 }
729
730 $issues = XDB::fetchAllAssoc('SELECT p.hrpid, pd.directory_name, pd.promo
731 FROM profile_merge_issues AS pm
732 INNER JOIN profiles AS p ON (pm.pid = p.pid)
733 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
734 WHERE FIND_IN_SET({?}, pm.issues)
735 ORDER BY pd.directory_name
736 LIMIT 100', $action);
737
738 $page->changeTpl('fusionax/other_issues.tpl');
739 $page->assign('issues', $issues);
740 $page->assign('issue', $issueList[$action]);
d20f50cf 741 $page->assign('type', $typeList[$action]);
12419b5b
SJ
742 $page->assign('total', $total);
743 }
744 }
b9ad0878 745}
e8591429 746
a3f12425
SJ
747// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
748?>