Renames AUTH_MDP to AUTH_PASSWD (Closes #1523).
[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';
128 }
73be4434 129 } elseif ($action == 'integrateSQL') {
cc8ea8b2
PC
130 // intégration des données dans la base MySQL
131 // liste des fichiers sql à exécuter
132 $filesSQL = array(
ddc4c642
SJ
133 0 => 'Activites.sql',
134 1 => 'Adresses.sql',
135 2 => 'Anciens.sql',
136 3 => 'Formations.sql',
0efb08e6
SJ
137 4 => 'Entreprises.sql',
138 5 => 'Formations_MD.sql'
ddc4c642
SJ
139 );
140 if ($file != '') {
cc8ea8b2 141 // récupère le contenu du fichier sql
ddc4c642 142 $queries = explode(';', file_get_contents($modulepath . $filesSQL[$file]));
73be4434
SJ
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
ddc4c642 153 XDB::execute(str_replace('{?}', $spoolpath, $q));
b9ad0878 154 }
b9ad0878 155 }
cc8ea8b2 156 // trouve le prochain fichier à exécuter
ddc4c642 157 $nextfile = $file + 1;
b9ad0878
PC
158 } else {
159 $nextfile = 0;
160 }
0efb08e6 161 if ($nextfile > 5) {
ddc4c642
SJ
162 // tous les fichiers ont été exécutés, on passe à l'étape suivante
163 $next = 'adds1920';
b9ad0878 164 } else {
cc8ea8b2 165 // on passe au fichier suivant
ddc4c642
SJ
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
ddc4c642
SJ
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
00ba8a74
SJ
202 XDB::execute('INSERT INTO profiles (hrpid, xorg_id, ax_id, sex)
203 VALUES ({?}, {?}, {?}, {?})',
ddc4c642
SJ
204 $hrid, $xorgId, $ax_id, $sex);
205 $pid = XDB::insertId();
4caad881 206 XDB::execute('INSERT INTO profile_public_names (pid, lastname_initial, firstname_initial)
00ba8a74 207 VALUES ({?}, {?}, {?})',
4caad881 208 $pid, $lastname, $firstname);
00ba8a74
SJ
209 XDB::execute('INSERT INTO profile_display (pid, yourself, public_name, private_name,
210 directory_name, short_name, sort_name, promo)
211 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
ddc4c642 212 $pid, $firstname, $fullName, $fullName, $directoryName, $fullName, $directoryName, $promo);
00ba8a74
SJ
213 XDB::execute('INSERT INTO profile_education (pid, eduid, degreeid, entry_year, grad_year, flags)
214 VALUES ({?}, {?}, {?}, {?}, {?}, {?})',
ddc4c642 215 $pid, $eduSchools[Profile::EDU_X], $degreeid, $entry_year, $grad_year, 'primary');
ad1c9939
SJ
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);
ddc4c642 219 $uid = XDB::insertId();
00ba8a74
SJ
220 XDB::execute('INSERT INTO account_profiles (uid, pid, perms)
221 VALUES ({?}, {?}, {?})',
ddc4c642 222 $uid, $pid, 'owner');
b9ad0878 223 }
ddc4c642
SJ
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
ad1c9939 231 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)');
ddc4c642 232 $next = 'clean';
73be4434 233 } elseif ($action == 'clean') {
cc8ea8b2 234 // nettoyage du fichier temporaire
0efb08e6 235 //exec('rm -Rf ' . $spoolpath);
ddc4c642 236 $report[] = 'Import finit.';
b9ad0878 237 }
73be4434 238 foreach($report as $t) {
cc8ea8b2 239 // affiche les lignes de report
ddc4c642 240 echo "$('#fusionax').append('" . $t . "<br/>');\n";
73be4434 241 }
b9ad0878 242 if (isset($next)) {
cc8ea8b2 243 // lance le prochain script s'il y en a un
ddc4c642 244 echo "$.getScript('fusionax/import/" . $next . "');";
b9ad0878 245 }
cc8ea8b2 246 // exit pour ne pas afficher la page template par défaut
b9ad0878
PC
247 exit;
248 }
22f043e4 249
26ba053e 250 function handler_view($page, $action = '')
29459c49 251 {
29459c49
SJ
252 $page->changeTpl('fusionax/view.tpl');
253 if ($action == 'create') {
254 XDB::execute('DROP VIEW IF EXISTS fusionax_deceased');
723ea674 255 XDB::execute("CREATE VIEW fusionax_deceased AS
ddc4c642
SJ
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)
723ea674 260 WHERE p.deathdate != a.Date_deces OR (p.deathdate IS NULL AND a.Date_deces != '0000-00-00')");
29459c49
SJ
261 XDB::execute('DROP VIEW IF EXISTS fusionax_promo');
262 XDB::execute('CREATE VIEW fusionax_promo AS
ddc4c642
SJ
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.');
29459c49
SJ
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 */
ddc4c642 276 private static function clear_wrong_in_xorg($pid)
29459c49 277 {
ddc4c642
SJ
278 $res = XDB::execute('UPDATE fusionax_xorg_anciens
279 SET ax_id = NULL
280 WHERE pid = {?}', $pid);
29459c49
SJ
281 if (!$res) {
282 return 0;
283 }
284 return XDB::affectedRows() / 2;
285 }
286
aab2ffdd 287 /* Cherche les les anciens présents dans Xorg avec un matricule_ax ne correspondant à rien dans la base de l'AX
ad1c9939 288 * (mises à part les promo 1921, 1923, 1924, 1925 qui ne figurent pas dans les données de l'AX)*/
29459c49
SJ
289 private static function find_wrong_in_xorg($limit = 10)
290 {
ddc4c642 291 return XDB::iterator('SELECT u.promo, u.pid, u.private_name
29459c49
SJ
292 FROM fusionax_xorg_anciens AS u
293 WHERE NOT EXISTS (SELECT *
294 FROM fusionax_anciens AS f
ddc4c642 295 WHERE f.ax_id = u.ax_id)
ad1c9939 296 AND u.ax_id IS NOT NULL AND promo NOT IN (\'X1921\', \'X1923\', \'X1924\', \'X1925\')');
29459c49
SJ
297 }
298
b65beb64
PC
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
22f043e4 303 */
ddc4c642 304 private static function link_by_ids($pid, $ax_id)
b9ad0878 305 {
ddc4c642 306 $res = XDB::execute('UPDATE fusionax_import AS i
73be4434 307 INNER JOIN fusionax_xorg_anciens AS u
ddc4c642
SJ
308 SET u.ax_id = i.ax_id,
309 i.pid = u.pid,
73be4434 310 i.date_match_id = NOW()
ddc4c642
SJ
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);
73be4434 315 if (!$res) {
b9ad0878
PC
316 return 0;
317 }
73be4434 318 return XDB::affectedRows() / 2;
b9ad0878 319 }
22f043e4 320
b65beb64
PC
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
e8591429 324 * @return un XOrgDBIterator sur les entrées avec display_name, promo,
ddc4c642 325 * pid, ax_id et display_name_ax
22f043e4 326 */
b65beb64 327 private static function find_easy_to_link($limit = 10, $sure = false)
b9ad0878 328 {
e8591429 329 $easy_to_link = XDB::iterator("
ddc4c642 330 SELECT u.private_name, u.promo, u.pid, ax.ax_id,
ad1c9939 331 CONCAT(ax.prenom, ' ', ax.nom_complet, ' (', ax.groupe_promo, ax.promotion_etude, ')') AS display_name_ax,
73be4434 332 COUNT(*) AS nbMatches
29459c49 333 FROM fusionax_anciens AS ax
ddc4c642
SJ
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
ad1c9939 336 AND u.promo = CONCAT(ax.groupe_promo, ax.promotion_etude)
ddc4c642
SJ
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) : ''));
b65beb64
PC
342 if ($easy_to_link->total() > 0 || $sure) {
343 return $easy_to_link;
344 }
73be4434 345 return XDB::iterator("
ddc4c642 346 SELECT u.private_name, u.promo, u.pid, ax.ax_id,
ad1c9939 347 CONCAT(ax.prenom, ' ', ax.nom_complet, ' (', ax.groupe_promo, ax.promotion_etude, ')') AS display_name_ax,
73be4434 348 COUNT(*) AS nbMatches
29459c49 349 FROM fusionax_anciens AS ax
ddc4c642
SJ
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)
ad1c9939
SJ
355 AND u.promo < CONCAT(ax.groupe_promo, ax.promotion_etude + 2)
356 AND u.promo > CONCAT(ax.groupe_promo, ax.promotion_etude - 2))
ddc4c642
SJ
357 GROUP BY u.pid
358 HAVING u.pid IS NOT NULL AND nbMatches = 1" . ($limit ? (' LIMIT ' . $limit) : ''));
b9ad0878 359 }
22f043e4 360
b65beb64 361 /** Module de mise en correspondance les ids */
26ba053e 362 function handler_ids($page, $part = 'main', $pid = null, $ax_id = null)
b9ad0878 363 {
29459c49 364 $nbToLink = 100;
29459c49 365 $page->assign('xorg_title', 'Polytechnique.org - Fusion - Mise en correspondance simple');
ddc4c642 366
73be4434 367 if ($part == 'missingInAX') {
b9ad0878
PC
368 // locate all persons from this database that are not in AX's
369 $page->changeTpl('fusionax/idsMissingInAx.tpl');
ddc4c642 370 $missingInAX = XDB::iterator('SELECT promo, pid, private_name
29459c49 371 FROM fusionax_xorg_anciens
ad1c9939
SJ
372 WHERE ax_id IS NULL
373 ORDER BY promo');
b9ad0878
PC
374 $page->assign('missingInAX', $missingInAX);
375 return;
376 }
73be4434 377 if ($part == 'missingInXorg') {
b9ad0878
PC
378 // locate all persons from AX's database that are not here
379 $page->changeTpl('fusionax/idsMissingInXorg.tpl');
ddc4c642 380 $missingInXorg = XDB::iterator("SELECT CONCAT(a.prenom, ' ', a.Nom_usuel) AS private_name,
ad1c9939 381 CONCAT(a.groupe_promo, a.promotion_etude) AS promo, a.ax_id
73be4434 382 FROM fusionax_import
ddc4c642 383 INNER JOIN fusionax_anciens AS a USING (ax_id)
ad1c9939
SJ
384 WHERE fusionax_import.pid IS NULL
385 ORDER BY promo");
b9ad0878
PC
386 $page->assign('missingInXorg', $missingInXorg);
387 return;
388 }
29459c49
SJ
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()) {
ddc4c642 399 FusionAxModule::clear_wrong_in_xorg($l['pid']);
29459c49
SJ
400 }
401 pl_redirect('fusionax/ids/wrongInXorg');
402 }
403 if ($part == 'lier') {
404 if (Post::has('user_id') && Post::has('matricule_ax')) {
ddc4c642 405 FusionAxModule::link_by_ids(Post::i('pid'), Post::v('ax_id'));
29459c49
SJ
406 }
407 }
73be4434 408 if ($part == 'link') {
ddc4c642 409 FusionAxModule::link_by_ids($pid, $ax_id);
b65beb64 410 exit;
b9ad0878 411 }
73be4434 412 if ($part == 'linknext') {
29459c49 413 $linksToDo = FusionAxModule::find_easy_to_link($nbToLink);
73be4434 414 while ($l = $linksToDo->next()) {
ddc4c642 415 FusionAxModule::link_by_ids($l['pid'], $l['ax_id']);
b9ad0878 416 }
b65beb64 417 pl_redirect('fusionax/ids#autolink');
b9ad0878 418 }
73be4434 419 if ($part == 'linkall') {
b9ad0878 420 $linksToDo = FusionAxModule::find_easy_to_link(0);
73be4434 421 while ($l = $linksToDo->next()) {
ddc4c642 422 FusionAxModule::link_by_ids($l['pid'], $l['ax_id']);
b9ad0878
PC
423 }
424 }
425 {
426 $page->changeTpl('fusionax/ids.tpl');
73be4434 427 $missingInAX = XDB::query('SELECT COUNT(*)
ddc4c642
SJ
428 FROM fusionax_xorg_anciens
429 WHERE ax_id IS NULL');
73be4434 430 if ($missingInAX) {
b9ad0878
PC
431 $page->assign('nbMissingInAX', $missingInAX->fetchOneCell());
432 }
73be4434 433 $missingInXorg = XDB::query('SELECT COUNT(*)
ddc4c642
SJ
434 FROM fusionax_import
435 WHERE pid IS NULL');
73be4434
SJ
436 if ($missingInXorg) {
437 $page->assign('nbMissingInXorg', $missingInXorg->fetchOneCell());
b9ad0878 438 }
29459c49
SJ
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());
b9ad0878
PC
446 $page->assign('easyToLink', $easyToLink);
447 }
448 }
449 }
22f043e4 450
26ba053e 451 function handler_deceased($page, $action = '')
b9ad0878 452 {
29459c49
SJ
453 if ($action == 'updateXorg') {
454 XDB::execute('UPDATE fusionax_deceased
455 SET deces_xorg = deces_ax
22648cc5 456 WHERE deces_xorg IS NULL');
29459c49
SJ
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') {
ddc4c642 464 if (Post::has('pid') && Post::has('date')) {
29459c49
SJ
465 XDB::execute('UPDATE fusionax_deceased
466 SET deces_ax = {?}, deces_xorg = {?}
ddc4c642
SJ
467 WHERE pid = {?}',
468 Post::v('date'), Post::v('date'), Post::i('pid'));
29459c49
SJ
469 }
470 }
471 $page->changeTpl('fusionax/deceased.tpl');
b9ad0878 472 // deceased
73be4434
SJ
473 $deceasedErrorsSql = XDB::query('SELECT COUNT(*) FROM fusionax_deceased');
474 $page->assign('deceasedErrors', $deceasedErrorsSql->fetchOneCell());
ddc4c642
SJ
475 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_ax
476 FROM fusionax_deceased
22648cc5 477 WHERE deces_xorg IS NULL
29459c49
SJ
478 LIMIT 10');
479 $page->assign('nbDeceasedMissingInXorg', $res->total());
480 $page->assign('deceasedMissingInXorg', $res);
ddc4c642
SJ
481 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_xorg
482 FROM fusionax_deceased
483 WHERE deces_ax = "0000-00-00"
29459c49
SJ
484 LIMIT 10');
485 $page->assign('nbDeceasedMissingInAX', $res->total());
486 $page->assign('deceasedMissingInAX', $res);
ddc4c642
SJ
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"');
29459c49
SJ
490 $page->assign('nbDeceasedDifferent', $res->total());
491 $page->assign('deceasedDifferent', $res);
b9ad0878 492 }
c7eac294 493
26ba053e 494 function handler_promo($page, $action = '')
c7eac294
SJ
495 {
496 $page->changeTpl('fusionax/promo.tpl');
ddc4c642 497 $res = XDB::iterator('SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
c7eac294 498 FROM fusionax_promo
a3f12425
SJ
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');
c7eac294 503 $nbMissmatchingPromos = $res->total();
a3f12425
SJ
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();
c7eac294
SJ
512 $page->assign('nbMissmatchingPromos1', $res->total());
513 $page->assign('missmatchingPromos1', $res);
a3f12425 514
ddc4c642 515 $res = XDB::iterator('SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
c7eac294 516 FROM fusionax_promo
a3f12425
SJ
517 WHERE promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 3 = promo_sortie_xorg
518 ORDER BY promo_etude_xorg');
c7eac294
SJ
519 $nbMissmatchingPromos += $res->total();
520 $page->assign('nbMissmatchingPromos2', $res->total());
521 $page->assign('missmatchingPromos2', $res);
a3f12425
SJ
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
26ba053e 534 function handler_names($page, $action = '')
a3f12425
SJ
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
4caad881
SJ
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)");
a3f12425
SJ
549 $page->assign('lastnameIssues', $res->fetchOneCell());
550
4caad881
SJ
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)');
a3f12425
SJ
555 $page->assign('firstnameIssues', $res->fetchOneCell());
556
c7eac294 557 }
12419b5b 558
26ba053e 559 function handler_edu($page, $action = '')
2fad6caa
SJ
560 {
561 $page->changeTpl('fusionax/education.tpl');
562
95b1a6d8 563 $missingEducation = XDB::rawIterator("SELECT DISTINCT(f.Intitule_formation)
2fad6caa 564 FROM fusionax_formations AS f
95b1a6d8
SJ
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)
2fad6caa 569 FROM fusionax_formations AS f
95b1a6d8
SJ
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
2fad6caa 574 FROM fusionax_formations AS f
95b1a6d8
SJ
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)
2fad6caa
SJ
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
26ba053e 590 function handler_corps($page)
551e00c1
SJ
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
26ba053e 611 function handler_issues_deathdate($page, $action = '')
12419b5b
SJ
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
26ba053e 653 function handler_issues_promo($page, $action = '')
12419b5b
SJ
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
26ba053e 700 function handler_issues($page, $action = '')
12419b5b
SJ
701 {
702 static $issueList = array(
703 'name' => 'noms',
704 'phone' => 'téléphones',
705 'education' => 'formations',
706 'address' => 'adresses',
707 'job' => 'emplois'
708 );
d20f50cf
SJ
709 static $typeList = array(
710 'name' => 'general',
711 'phone' => 'general',
712 'education' => 'general',
713 'address' => 'adresses',
714 'job' => 'emploi'
715 );
12419b5b
SJ
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]);
d20f50cf 738 $page->assign('type', $typeList[$action]);
12419b5b
SJ
739 $page->assign('total', $total);
740 }
741 }
b9ad0878 742}
e8591429 743
a3f12425
SJ
744// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
745?>