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