Get ready for next version
[platal.git] / modules / fusionax.php
CommitLineData
b9ad0878
PC
1<?php
2/***************************************************************************
ba6ae046 3 * Copyright (C) 2003-2013 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...';
9a2876cb
SJ
127 XDB::execute("UPDATE profiles
128 SET ax_id = NULL
129 WHERE ax_id = ''");
149360d4 130 $next = 'integrateSQL';
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';
149360d4
SJ
186 $hrpromo = '1920';
187 $sex = 'male';
ddc4c642
SJ
188 $xorgId = 19200000;
189 $type = 'x';
190
149360d4
SJ
191 while ($new = $res->next()) {
192 $firstname = $new['prenom'];
193 $lastname = $new['Nom_complet'];
194 $ax_id = $new['ax_id'];
195 $hrid = User::makeHrid($firstname, $lastname, $hrpromo);
ddc4c642
SJ
196 $res1 = XDB::query('SELECT COUNT(*)
197 FROM accounts
198 WHERE hruid = {?}', $hrid);
199 $res2 = XDB::query('SELECT COUNT(*)
200 FROM profiles
201 WHERE hrpid = {?}', $hrid);
202 if (is_null($hrid) || $res1->fetchOneCell() > 0 || $res2->fetchOneCell() > 0) {
203 $report[] = $ax_id . ' non ajouté';
204 }
205 $fullName = $firstname . ' ' . $lastname;
206 $directoryName = $lastname . ' ' . $firstname;
207 ++$xorgId;
208
00ba8a74
SJ
209 XDB::execute('INSERT INTO profiles (hrpid, xorg_id, ax_id, sex)
210 VALUES ({?}, {?}, {?}, {?})',
ddc4c642
SJ
211 $hrid, $xorgId, $ax_id, $sex);
212 $pid = XDB::insertId();
149360d4
SJ
213 XDB::execute('INSERT INTO profile_public_names (pid, lastname_initial, firstname_initial, lastname_main, firstname_main)
214 VALUES ({?}, {?}, {?}, {?}, {?})',
215 $pid, $lastname, $firstname, $lastname, $firstname);
00ba8a74
SJ
216 XDB::execute('INSERT INTO profile_display (pid, yourself, public_name, private_name,
217 directory_name, short_name, sort_name, promo)
218 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
ddc4c642 219 $pid, $firstname, $fullName, $fullName, $directoryName, $fullName, $directoryName, $promo);
00ba8a74
SJ
220 XDB::execute('INSERT INTO profile_education (pid, eduid, degreeid, entry_year, grad_year, flags)
221 VALUES ({?}, {?}, {?}, {?}, {?}, {?})',
ddc4c642 222 $pid, $eduSchools[Profile::EDU_X], $degreeid, $entry_year, $grad_year, 'primary');
ad1c9939 223 XDB::execute('INSERT INTO accounts (hruid, type, is_admin, state, full_name, directory_name, display_name, lastname, firstname, sex)
149360d4
SJ
224 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
225 $hrid, $type, 0, 'pending', $fullName, $directoryName, $firstname, $lastname, $firstname, $sex);
ddc4c642 226 $uid = XDB::insertId();
00ba8a74
SJ
227 XDB::execute('INSERT INTO account_profiles (uid, pid, perms)
228 VALUES ({?}, {?}, {?})',
ddc4c642 229 $uid, $pid, 'owner');
b9ad0878 230 }
ddc4c642 231 $report[] = 'Promo 1920 ajoutée.';
149360d4
SJ
232 $next = 'adds2011';
233 } elseif ($action == 'adds2011') {
234 // Adds promotion 2011 from AX db.
235 $report[] = 'Ajout des élèves manquant de la promotion 2011';
236 $res = XDB::iterator("SELECT prenom, Nom_complet, ax_id, Civilite
237 FROM fusionax_anciens
238 WHERE promotion_etude = 2011 AND groupe_promo = 'X'
239 AND NOT EXISTS (SELECT 1
240 FROM profiles
241 WHERE profiles.ax_id = fusionax_anciens.ax_id)");
242
243 $eduSchools = DirEnum::getOptions(DirEnum::EDUSCHOOLS);
244 $eduSchools = array_flip($eduSchools);
245 $eduDegrees = DirEnum::getOptions(DirEnum::EDUDEGREES);
246 $eduDegrees = array_flip($eduDegrees);
247 $degreeid = $eduDegrees[Profile::DEGREE_X];
248 $entry_year = 2011;
249 $grad_year = 2014;
250 $promo = 'X2011';
251 $hrpromo = '2011';
252 $type = 'x';
253
254 while ($new = $res->next()) {
255 $firstname = $new['prenom'];
256 $lastname = $new['Nom_complet'];
257 $ax_id = $new['ax_id'];
258 $civilite = $new['Civilite'];
259 $hrid = User::makeHrid($firstname, $lastname, $hrpromo);
260 $res1 = XDB::query('SELECT COUNT(*)
261 FROM accounts
262 WHERE hruid = {?}', $hrid);
263 $res2 = XDB::query('SELECT COUNT(*)
264 FROM profiles
265 WHERE hrpid = {?}', $hrid);
266 if (is_null($hrid) || $res1->fetchOneCell() > 0 || $res2->fetchOneCell() > 0) {
267 $report[] = $ax_id . ' non ajouté';
268 }
269 $fullName = $firstname . ' ' . $lastname;
270 $directoryName = $lastname . ' ' . $firstname;
271 if ($civilite == 'M') {
272 $sex = 'male';
273 } else {
274 $sex = 'female';
275 }
276
277 XDB::execute('INSERT INTO profiles (hrpid, ax_id, sex, title)
278 VALUES ({?}, {?}, {?}, {?})',
279 $hrid, $ax_id, $sex, $civilite);
280 $pid = XDB::insertId();
281 XDB::execute('INSERT INTO profile_public_names (pid, lastname_initial, firstname_initial, lastname_main, firstname_main)
282 VALUES ({?}, {?}, {?}, {?}, {?})',
283 $pid, $lastname, $firstname, $lastname, $firstname);
284 XDB::execute('INSERT INTO profile_display (pid, yourself, public_name, private_name,
285 directory_name, short_name, sort_name, promo)
286 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
287 $pid, $firstname, $fullName, $fullName, $directoryName, $fullName, $directoryName, $promo);
288 XDB::execute('INSERT INTO profile_education (pid, eduid, degreeid, entry_year, grad_year, flags)
289 VALUES ({?}, {?}, {?}, {?}, {?}, {?})',
290 $pid, $eduSchools[Profile::EDU_X], $degreeid, $entry_year, $grad_year, 'primary');
291 XDB::execute('INSERT INTO accounts (hruid, type, is_admin, state, full_name, directory_name, display_name, lastname, firstname, sex)
292 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
293 $hrid, $type, 0, 'pending', $fullName, $directoryName, $firstname, $lastname, $firstname, $sex);
294 $uid = XDB::insertId();
295 XDB::execute('INSERT INTO account_profiles (uid, pid, perms)
296 VALUES ({?}, {?}, {?})',
297 $uid, $pid, 'owner');
298 }
299 $report[] = 'Promo 2011 ajoutée.';
300
ddc4c642
SJ
301 $next = 'view';
302 } elseif ($action == 'view') {
303 XDB::execute('CREATE OR REPLACE ALGORITHM=MERGE VIEW fusionax_xorg_anciens AS
304 SELECT p.pid, p.ax_id, pd.promo, pd.private_name, pd.public_name,
305 pd.sort_name, pd.short_name, pd.directory_name
306 FROM profiles AS p
ad1c9939 307 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)');
ddc4c642 308 $next = 'clean';
73be4434 309 } elseif ($action == 'clean') {
cc8ea8b2 310 // nettoyage du fichier temporaire
0efb08e6 311 //exec('rm -Rf ' . $spoolpath);
ddc4c642 312 $report[] = 'Import finit.';
b9ad0878 313 }
73be4434 314 foreach($report as $t) {
cc8ea8b2 315 // affiche les lignes de report
ddc4c642 316 echo "$('#fusionax').append('" . $t . "<br/>');\n";
73be4434 317 }
b9ad0878 318 if (isset($next)) {
cc8ea8b2 319 // lance le prochain script s'il y en a un
ddc4c642 320 echo "$.getScript('fusionax/import/" . $next . "');";
b9ad0878 321 }
cc8ea8b2 322 // exit pour ne pas afficher la page template par défaut
b9ad0878
PC
323 exit;
324 }
22f043e4 325
26ba053e 326 function handler_view($page, $action = '')
29459c49 327 {
29459c49
SJ
328 $page->changeTpl('fusionax/view.tpl');
329 if ($action == 'create') {
330 XDB::execute('DROP VIEW IF EXISTS fusionax_deceased');
723ea674 331 XDB::execute("CREATE VIEW fusionax_deceased AS
ddc4c642
SJ
332 SELECT p.pid, a.ax_id, pd.private_name, pd.promo, p.deathdate AS deces_xorg, a.Date_deces AS deces_ax
333 FROM profiles AS p
334 INNER JOIN profile_display AS pd ON (p.pid = pd.pid)
335 INNER JOIN fusionax_anciens AS a ON (a.ax_id = p.ax_id)
723ea674 336 WHERE p.deathdate != a.Date_deces OR (p.deathdate IS NULL AND a.Date_deces != '0000-00-00')");
29459c49 337 XDB::execute('DROP VIEW IF EXISTS fusionax_promo');
bb375055 338 XDB::execute("CREATE VIEW fusionax_promo AS
9a2876cb 339 SELECT p.pid, p.ax_id, pd.private_name, pd.promo, pe.entry_year AS promo_etude_xorg, f.groupe_promo,
ddc4c642
SJ
340 f.promotion_etude AS promo_etude_ax, pe.grad_year AS promo_sortie_xorg
341 FROM profiles AS p
342 INNER JOIN profile_display AS pd ON (p.pid = pd.pid)
caa04db8 343 INNER JOIN profile_education AS pe ON (p.pid = pe.pid AND FIND_IN_SET('primary', pe.flags))
ddc4c642 344 INNER JOIN fusionax_anciens AS f ON (p.ax_id = f.ax_id)
bb375055 345 WHERE (f.groupe_promo = 'X' AND pd.promo != CONCAT('X', f.promotion_etude)
caa04db8
SJ
346 AND !(f.promotion_etude = pe.entry_year + 1 AND pe.grad_year = pe.entry_year + 4)
347 AND !(f.promotion_etude = pe.entry_year + 2 AND pe.grad_year = pe.entry_year + 5)
348 AND f.promotion_etude != 0)
bb375055
SJ
349 OR (f.groupe_promo = 'D' AND f.promotion_etude != pe.grad_year)
350 OR (f.groupe_promo = 'M' AND f.promotion_etude != pe.entry_year)
351 GROUP BY p.pid");
ddc4c642 352 $page->trigSuccess('Les VIEW ont bien été créées.');
29459c49
SJ
353 }
354 }
355
356 /* Mets à NULL le matricule_ax de ces camarades pour marquer le fait qu'ils ne figurent pas dans l'annuaire de l'AX */
ddc4c642 357 private static function clear_wrong_in_xorg($pid)
29459c49 358 {
ddc4c642
SJ
359 $res = XDB::execute('UPDATE fusionax_xorg_anciens
360 SET ax_id = NULL
361 WHERE pid = {?}', $pid);
29459c49
SJ
362 if (!$res) {
363 return 0;
364 }
365 return XDB::affectedRows() / 2;
366 }
367
aab2ffdd 368 /* Cherche les les anciens présents dans Xorg avec un matricule_ax ne correspondant à rien dans la base de l'AX
9a2876cb 369 * (mises à part les promo 1921, 1922, 1923, 1924, 1925, 1927 qui ne figurent pas dans les données de l'AX)*/
29459c49
SJ
370 private static function find_wrong_in_xorg($limit = 10)
371 {
ddc4c642 372 return XDB::iterator('SELECT u.promo, u.pid, u.private_name
29459c49
SJ
373 FROM fusionax_xorg_anciens AS u
374 WHERE NOT EXISTS (SELECT *
375 FROM fusionax_anciens AS f
ddc4c642 376 WHERE f.ax_id = u.ax_id)
9a2876cb 377 AND u.ax_id IS NOT NULL AND promo NOT IN (\'X1921\', \'X1922\', \'X1923\', \'X1924\', \'X1925\', \'X1927\')');
29459c49
SJ
378 }
379
b65beb64
PC
380 /** Lier les identifiants d'un ancien dans les deux annuaires
381 * @param user_id identifiant dans l'annuaire X.org
382 * @param matricule_ax identifiant dans l'annuaire de l'AX
383 * @return 0 si la liaison a échoué, 1 sinon
22f043e4 384 */
ddc4c642 385 private static function link_by_ids($pid, $ax_id)
b9ad0878 386 {
ddc4c642 387 $res = XDB::execute('UPDATE fusionax_import AS i
73be4434 388 INNER JOIN fusionax_xorg_anciens AS u
ddc4c642
SJ
389 SET u.ax_id = i.ax_id,
390 i.pid = u.pid,
73be4434 391 i.date_match_id = NOW()
ddc4c642
SJ
392 WHERE i.ax_id = {?} AND u.pid = {?}
393 AND (u.ax_id != {?} OR u.ax_id IS NULL
394 OR i.pid != {?} OR i.pid IS NULL)',
395 $ax_id, $pid, $ax_id, $pid);
73be4434 396 if (!$res) {
b9ad0878
PC
397 return 0;
398 }
73be4434 399 return XDB::affectedRows() / 2;
b9ad0878 400 }
22f043e4 401
b65beb64
PC
402 /** Recherche automatique d'anciens à lier entre les deux annuaires
403 * @param limit nombre d'anciens à trouver au max
404 * @param sure si true, ne trouve que des anciens qui sont quasi sûrs
e8591429 405 * @return un XOrgDBIterator sur les entrées avec display_name, promo,
ddc4c642 406 * pid, ax_id et display_name_ax
22f043e4 407 */
b65beb64 408 private static function find_easy_to_link($limit = 10, $sure = false)
b9ad0878 409 {
e8591429 410 $easy_to_link = XDB::iterator("
ddc4c642 411 SELECT u.private_name, u.promo, u.pid, ax.ax_id,
ad1c9939 412 CONCAT(ax.prenom, ' ', ax.nom_complet, ' (', ax.groupe_promo, ax.promotion_etude, ')') AS display_name_ax,
73be4434 413 COUNT(*) AS nbMatches
29459c49 414 FROM fusionax_anciens AS ax
ddc4c642
SJ
415 INNER JOIN fusionax_import AS i ON (i.ax_id = ax.ax_id AND i.pid IS NULL)
416 LEFT JOIN fusionax_xorg_anciens AS u ON (u.ax_id IS NULL
ad1c9939 417 AND u.promo = CONCAT(ax.groupe_promo, ax.promotion_etude)
ddc4c642
SJ
418 AND (CONCAT(ax.prenom, ' ', ax.nom_complet) = u.private_name
419 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.public_name
420 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.short_name))
421 GROUP BY u.pid
422 HAVING u.pid IS NOT NULL AND nbMatches = 1" . ($limit ? (' LIMIT ' . $limit) : ''));
b65beb64
PC
423 if ($easy_to_link->total() > 0 || $sure) {
424 return $easy_to_link;
425 }
73be4434 426 return XDB::iterator("
ddc4c642 427 SELECT u.private_name, u.promo, u.pid, ax.ax_id,
ad1c9939 428 CONCAT(ax.prenom, ' ', ax.nom_complet, ' (', ax.groupe_promo, ax.promotion_etude, ')') AS display_name_ax,
73be4434 429 COUNT(*) AS nbMatches
29459c49 430 FROM fusionax_anciens AS ax
ddc4c642
SJ
431 INNER JOIN fusionax_import AS i ON (i.ax_id = ax.ax_id AND i.pid IS NULL)
432 LEFT JOIN fusionax_xorg_anciens AS u ON (u.ax_id IS NULL
433 AND (CONCAT(ax.prenom, ' ', ax.nom_complet) = u.private_name
434 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.public_name
435 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.short_name)
ad1c9939
SJ
436 AND u.promo < CONCAT(ax.groupe_promo, ax.promotion_etude + 2)
437 AND u.promo > CONCAT(ax.groupe_promo, ax.promotion_etude - 2))
ddc4c642
SJ
438 GROUP BY u.pid
439 HAVING u.pid IS NOT NULL AND nbMatches = 1" . ($limit ? (' LIMIT ' . $limit) : ''));
b9ad0878 440 }
22f043e4 441
b65beb64 442 /** Module de mise en correspondance les ids */
26ba053e 443 function handler_ids($page, $part = 'main', $pid = null, $ax_id = null)
b9ad0878 444 {
29459c49 445 $nbToLink = 100;
29459c49 446 $page->assign('xorg_title', 'Polytechnique.org - Fusion - Mise en correspondance simple');
ddc4c642 447
73be4434 448 if ($part == 'missingInAX') {
b9ad0878
PC
449 // locate all persons from this database that are not in AX's
450 $page->changeTpl('fusionax/idsMissingInAx.tpl');
ddc4c642 451 $missingInAX = XDB::iterator('SELECT promo, pid, private_name
29459c49 452 FROM fusionax_xorg_anciens
ad1c9939
SJ
453 WHERE ax_id IS NULL
454 ORDER BY promo');
b9ad0878
PC
455 $page->assign('missingInAX', $missingInAX);
456 return;
457 }
73be4434 458 if ($part == 'missingInXorg') {
b9ad0878
PC
459 // locate all persons from AX's database that are not here
460 $page->changeTpl('fusionax/idsMissingInXorg.tpl');
ddc4c642 461 $missingInXorg = XDB::iterator("SELECT CONCAT(a.prenom, ' ', a.Nom_usuel) AS private_name,
ad1c9939 462 CONCAT(a.groupe_promo, a.promotion_etude) AS promo, a.ax_id
73be4434 463 FROM fusionax_import
ddc4c642 464 INNER JOIN fusionax_anciens AS a USING (ax_id)
ad1c9939
SJ
465 WHERE fusionax_import.pid IS NULL
466 ORDER BY promo");
b9ad0878
PC
467 $page->assign('missingInXorg', $missingInXorg);
468 return;
469 }
29459c49
SJ
470 if ($part == 'wrongInXorg') {
471 // locate all persons from Xorg database that have a bad AX id
472 $page->changeTpl('fusionax/idswrongInXorg.tpl');
473 $wrongInXorg = FusionAxModule::find_wrong_in_xorg($nbToLink);
474 $page->assign('wrongInXorg', $wrongInXorg);
475 return;
476 }
477 if ($part == 'cleanwronginxorg') {
478 $linksToDo = FusionAxModule::find_wrong_in_xorg($nbToLink);
479 while ($l = $linksToDo->next()) {
ddc4c642 480 FusionAxModule::clear_wrong_in_xorg($l['pid']);
29459c49
SJ
481 }
482 pl_redirect('fusionax/ids/wrongInXorg');
483 }
484 if ($part == 'lier') {
485 if (Post::has('user_id') && Post::has('matricule_ax')) {
ddc4c642 486 FusionAxModule::link_by_ids(Post::i('pid'), Post::v('ax_id'));
29459c49
SJ
487 }
488 }
73be4434 489 if ($part == 'link') {
ddc4c642 490 FusionAxModule::link_by_ids($pid, $ax_id);
b65beb64 491 exit;
b9ad0878 492 }
73be4434 493 if ($part == 'linknext') {
29459c49 494 $linksToDo = FusionAxModule::find_easy_to_link($nbToLink);
73be4434 495 while ($l = $linksToDo->next()) {
ddc4c642 496 FusionAxModule::link_by_ids($l['pid'], $l['ax_id']);
b9ad0878 497 }
b65beb64 498 pl_redirect('fusionax/ids#autolink');
b9ad0878 499 }
73be4434 500 if ($part == 'linkall') {
b9ad0878 501 $linksToDo = FusionAxModule::find_easy_to_link(0);
73be4434 502 while ($l = $linksToDo->next()) {
ddc4c642 503 FusionAxModule::link_by_ids($l['pid'], $l['ax_id']);
b9ad0878
PC
504 }
505 }
506 {
507 $page->changeTpl('fusionax/ids.tpl');
73be4434 508 $missingInAX = XDB::query('SELECT COUNT(*)
ddc4c642
SJ
509 FROM fusionax_xorg_anciens
510 WHERE ax_id IS NULL');
73be4434 511 if ($missingInAX) {
b9ad0878
PC
512 $page->assign('nbMissingInAX', $missingInAX->fetchOneCell());
513 }
73be4434 514 $missingInXorg = XDB::query('SELECT COUNT(*)
ddc4c642
SJ
515 FROM fusionax_import
516 WHERE pid IS NULL');
73be4434
SJ
517 if ($missingInXorg) {
518 $page->assign('nbMissingInXorg', $missingInXorg->fetchOneCell());
b9ad0878 519 }
29459c49
SJ
520 $wrongInXorg = FusionAxModule::find_wrong_in_xorg($nbToLink);
521 if ($wrongInXorg->total() > 0) {
522 $page->assign('wrongInXorg', $wrongInXorg->total());
523 }
524 $easyToLink = FusionAxModule::find_easy_to_link($nbToLink);
525 if ($easyToLink->total() > 0) {
526 $page->assign('nbMatch', $easyToLink->total());
b9ad0878
PC
527 $page->assign('easyToLink', $easyToLink);
528 }
529 }
530 }
22f043e4 531
26ba053e 532 function handler_deceased($page, $action = '')
b9ad0878 533 {
29459c49
SJ
534 if ($action == 'updateXorg') {
535 XDB::execute('UPDATE fusionax_deceased
536 SET deces_xorg = deces_ax
22648cc5 537 WHERE deces_xorg IS NULL');
29459c49
SJ
538 }
539 if ($action == 'updateAX') {
540 XDB::execute('UPDATE fusionax_deceased
541 SET deces_ax = deces_xorg
542 WHERE deces_ax = "0000-00-00"');
543 }
544 if ($action == 'update') {
ddc4c642 545 if (Post::has('pid') && Post::has('date')) {
29459c49
SJ
546 XDB::execute('UPDATE fusionax_deceased
547 SET deces_ax = {?}, deces_xorg = {?}
ddc4c642
SJ
548 WHERE pid = {?}',
549 Post::v('date'), Post::v('date'), Post::i('pid'));
29459c49
SJ
550 }
551 }
552 $page->changeTpl('fusionax/deceased.tpl');
b9ad0878 553 // deceased
73be4434
SJ
554 $deceasedErrorsSql = XDB::query('SELECT COUNT(*) FROM fusionax_deceased');
555 $page->assign('deceasedErrors', $deceasedErrorsSql->fetchOneCell());
ddc4c642
SJ
556 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_ax
557 FROM fusionax_deceased
22648cc5 558 WHERE deces_xorg IS NULL
29459c49
SJ
559 LIMIT 10');
560 $page->assign('nbDeceasedMissingInXorg', $res->total());
561 $page->assign('deceasedMissingInXorg', $res);
ddc4c642
SJ
562 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_xorg
563 FROM fusionax_deceased
564 WHERE deces_ax = "0000-00-00"
29459c49
SJ
565 LIMIT 10');
566 $page->assign('nbDeceasedMissingInAX', $res->total());
567 $page->assign('deceasedMissingInAX', $res);
ddc4c642
SJ
568 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_xorg, deces_ax
569 FROM fusionax_deceased
570 WHERE deces_xorg != "0000-00-00" AND deces_ax != "0000-00-00"');
29459c49
SJ
571 $page->assign('nbDeceasedDifferent', $res->total());
572 $page->assign('deceasedDifferent', $res);
b9ad0878 573 }
c7eac294 574
26ba053e 575 function handler_promo($page, $action = '')
c7eac294
SJ
576 {
577 $page->changeTpl('fusionax/promo.tpl');
9a2876cb 578 $res = XDB::iterator("SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
c8ab27eb 579 FROM fusionax_promo
a3f12425
SJ
580 WHERE !(promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 3 = promo_sortie_xorg)
581 AND !(promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 4 = promo_sortie_xorg)
9a2876cb
SJ
582 AND !(promo_etude_ax = promo_etude_xorg + 1) AND groupe_promo = 'X'
583 ORDER BY promo_etude_xorg");
c7eac294 584 $nbMissmatchingPromos = $res->total();
a3f12425
SJ
585 $page->assign('nbMissmatchingPromos', $res->total());
586 $page->assign('missmatchingPromos', $res);
587
9a2876cb 588 $res = XDB::iterator("SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
a3f12425 589 FROM fusionax_promo
9a2876cb
SJ
590 WHERE promo_etude_ax = promo_etude_xorg + 1 AND groupe_promo = 'X'
591 ORDER BY promo_etude_xorg");
a3f12425 592 $nbMissmatchingPromos += $res->total();
c7eac294
SJ
593 $page->assign('nbMissmatchingPromos1', $res->total());
594 $page->assign('missmatchingPromos1', $res);
a3f12425 595
9a2876cb 596 $res = XDB::iterator("SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
c7eac294 597 FROM fusionax_promo
9a2876cb
SJ
598 WHERE promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 3 = promo_sortie_xorg AND groupe_promo = 'X'
599 ORDER BY promo_etude_xorg");
c7eac294
SJ
600 $nbMissmatchingPromos += $res->total();
601 $page->assign('nbMissmatchingPromos2', $res->total());
602 $page->assign('missmatchingPromos2', $res);
a3f12425 603
9a2876cb 604 $res = XDB::iterator("SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
a3f12425 605 FROM fusionax_promo
9a2876cb
SJ
606 WHERE promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 4 = promo_sortie_xorg AND groupe_promo = 'X'
607 ORDER BY promo_etude_xorg");
a3f12425
SJ
608 $nbMissmatchingPromos += $res->total();
609 $page->assign('nbMissmatchingPromos3', $res->total());
610 $page->assign('missmatchingPromos3', $res);
611
bb375055
SJ
612 $res = XDB::iterator("SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
613 FROM fusionax_promo
614 WHERE groupe_promo = 'M'
615 ORDER BY promo_etude_xorg");
616 $nbMissmatchingPromos += $res->total();
617 $page->assign('nbMissmatchingPromosM', $res->total());
618 $page->assign('missmatchingPromosM', $res);
619
620
621 $res = XDB::iterator("SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
622 FROM fusionax_promo
623 WHERE groupe_promo = 'D'
624 ORDER BY promo_etude_xorg");
625 $nbMissmatchingPromos += $res->total();
626 $page->assign('nbMissmatchingPromosD', $res->total());
627 $page->assign('missmatchingPromosD', $res);
628
a3f12425
SJ
629 $page->assign('nbMissmatchingPromosTotal', $nbMissmatchingPromos);
630 }
631
5bc0014e
SJ
632 private function format($string)
633 {
634 return preg_replace('/(\s+|\-)/', '', $string);
635 }
636
637 private function retrieve_firstnames()
638 {
639 $res = XDB::rawFetchAllAssoc('SELECT p.pid, p.ax_id, p.hrpid,
640 f.prenom, ppn.firstname_initial, ppn.firstname_main, ppn.firstname_ordinary
641 FROM fusionax_anciens AS f
642 INNER JOIN profiles AS p ON (f.ax_id = p.ax_id)
643 INNER JOIN profile_public_names AS ppn ON (p.pid = ppn.pid)
644 WHERE f.prenom NOT IN (ppn.firstname_initial, ppn.firstname_main, ppn.firstname_ordinary)');
645
646 $issues = array();
647 foreach ($res as $item) {
648 if (!($item['firstname_ordinary'] != '' || $item['firstname_main'] != $item['firstname_initial'])) {
649 $ax = $this->format(mb_strtolower(replace_accent($item['prenom'])));
650 $xorg = $this->format(mb_strtolower(replace_accent($item['firstname_main'])));
651 if ($ax != $xorg) {
652 $issues[] = $item;
653 }
654 }
655 }
656
657 return $issues;
658 }
659
a876b1ef 660 function handler_names($page, $action = '', $csv = false)
a3f12425
SJ
661 {
662 $page->changeTpl('fusionax/names.tpl');
663
193affc6 664 if ($action == 'first') {
5bc0014e 665 $res = $this->retrieve_firstnames();
a876b1ef
SJ
666 if ($csv) {
667 pl_cached_content_headers('text/x-csv', 'utf-8', 1, 'firstnames.csv');
668
669 $csv = fopen('php://output', 'w');
670 fputcsv($csv, array('pid', 'ax_id', 'hrpid', 'AX', 'initial', 'principal', 'ordinaire'), ';');
671 foreach ($res as $item) {
672 fputcsv($csv, $item, ';');
673 }
674 fclose($csv);
675 exit();
676 } else {
677 $page->assign('firstnameIssues', $res);
678 }
08aaf92e
SJ
679 } elseif ($action == 'last' || $action == 'last3' || $action == 'last2' || $action == 'last1') {
680 $ax_patro = "(IF(f.partic_patro, CONCAT(f.partic_patro, CONCAT(' ', f.Nom_patronymique)), f.Nom_patronymique) NOT IN (ppn.lastname_initial, ppn.lastname_main, ppn.lastname_marital, ppn.lastname_ordinary))";
681 $ax_ordinary = "(IF(f.partic_nom, CONCAT(f.partic_nom, CONCAT(' ', f.Nom_usuel)), f.Nom_usuel) NOT IN (ppn.lastname_initial, ppn.lastname_main, ppn.lastname_marital, ppn.lastname_ordinary))";
682 $ax_full = "(f.Nom_complet NOT IN (ppn.lastname_initial, ppn.lastname_main, ppn.lastname_marital, ppn.lastname_ordinary))";
683
684 switch ($action) {
685 case 'last':
686 $where = $ax_patro . ' OR ' . $ax_ordinary . ' OR ' . $ax_full;
687 break;
688 case 'last3':
689 $where = $ax_patro . ' AND ' . $ax_ordinary . ' AND ' . $ax_full;
690 break;
691 case 'last2':
692 $where = '(' . $ax_patro . ' AND ' . $ax_ordinary . ' AND NOT ' . $ax_full . ') OR ('
693 . $ax_patro . ' AND NOT ' . $ax_ordinary . ' AND ' . $ax_full . ') OR ('
694 . 'NOT ' . $ax_patro . ' AND ' . $ax_ordinary . ' AND ' . $ax_full . ')';
695 break;
696 case 'last1':
697 $where = '(' . $ax_patro . ' AND NOT ' . $ax_ordinary . ' AND NOT ' . $ax_full . ') OR ('
698 . 'NOT ' . $ax_patro . ' AND NOT ' . $ax_ordinary . ' AND ' . $ax_full . ') OR ('
699 . 'NOT ' . $ax_patro . ' AND ' . $ax_ordinary . ' AND NOT ' . $ax_full . ')';
700 break;
701 }
702
703 $res = XDB::rawFetchAllAssoc('SELECT p.pid, p.ax_id, p.hrpid,
193affc6
SJ
704 f.Nom_patronymique, f.Nom_usuel, f.Nom_complet,
705 ppn.lastname_initial, ppn.lastname_main, ppn.lastname_marital, ppn.lastname_ordinary
706 FROM fusionax_anciens AS f
707 INNER JOIN profiles AS p ON (f.ax_id = p.ax_id)
247ca43b 708 INNER JOIN profile_public_names AS ppn ON (p.pid = ppn.pid)
08aaf92e 709 WHERE ' . $where);
193affc6 710
a876b1ef 711 if ($csv) {
08aaf92e
SJ
712 function format($string)
713 {
714 $string = preg_replace('/\-/', ' ', $string);
715 return preg_replace('/\s+/', ' ', $string);
716 }
717
718
a876b1ef
SJ
719 pl_cached_content_headers('text/x-csv', 'utf-8', 1, 'lastnames.csv');
720
721 $csv = fopen('php://output', 'w');
722 fputcsv($csv, array('pid', 'ax_id', 'hrpid', 'AX patro', 'AX usuel', 'AX complet', 'initial', 'principal', 'marital', 'ordinaire'), ';');
723 foreach ($res as $item) {
08aaf92e
SJ
724 $ax = array(
725 'Nom_patronymique' => format(mb_strtolower(replace_accent($item['Nom_patronymique']))),
726 'Nom_usuel' => format(mb_strtolower(replace_accent($item['Nom_usuel']))),
727 'Nom_complet' => format(mb_strtolower(replace_accent($item['Nom_complet'])))
728 );
729 $xorg = array(
730 'lastname_initial' => format(mb_strtolower(replace_accent($item['lastname_initial']))),
731 'lastname_main' => format(mb_strtolower(replace_accent($item['lastname_main']))),
732 'lastname_ordinary' => format(mb_strtolower(replace_accent($item['lastname_ordinary'])))
733 );
734
735 if (!in_array($ax['Nom_patronymique'], $xorg) || !in_array($ax['Nom_usuel'], $xorg) || !in_array($ax['Nom_complet'], $xorg)) {
736 fputcsv($csv, $item, ';');
737 }
a876b1ef
SJ
738 }
739 fclose($csv);
740 exit();
741 } else {
742 $page->assign('lastnameIssues', $res);
08aaf92e
SJ
743 $page->assign('total', count($res));
744 $page->assign('issuesTypes', array(
745 'last' => "1, 2 ou 3 noms de l'AX manquant",
746 'last1' => "1 nom de l'AX manquant",
747 'last2' => "2 noms de l'AX manquant",
748 'last3' => "3 noms de l'AX manquant"
749 ));
a876b1ef 750 }
193affc6
SJ
751 } else {
752 $res = XDB::query('SELECT COUNT(*)
753 FROM fusionax_anciens AS f
754 INNER JOIN profiles AS p ON (f.ax_id = p.ax_id)');
755 $page->assign('total', $res->fetchOneCell());
756
757 $res = XDB::rawFetchOneCell("SELECT COUNT(*)
758 FROM fusionax_anciens AS f
759 INNER JOIN profiles AS p ON (f.ax_id = p.ax_id)
247ca43b 760 INNER JOIN profile_public_names AS ppn ON (p.pid = ppn.pid)
193affc6 761 WHERE IF(f.partic_patro, CONCAT(f.partic_patro, CONCAT(' ', f.Nom_patronymique)), f.Nom_patronymique) NOT IN (ppn.lastname_initial, ppn.lastname_main, ppn.lastname_marital, ppn.lastname_ordinary)
9a13e83f
SJ
762 OR IF(f.partic_nom, CONCAT(f.partic_nom, CONCAT(' ', f.Nom_usuel)), f.Nom_usuel) NOT IN (ppn.lastname_initial, ppn.lastname_main, ppn.lastname_marital, ppn.lastname_ordinary)
763 OR f.Nom_complet NOT IN (ppn.lastname_initial, ppn.lastname_main, ppn.lastname_marital, ppn.lastname_ordinary)");
193affc6
SJ
764 $page->assign('lastnameIssues', $res);
765
766 $res = XDB::rawFetchOneCell('SELECT COUNT(*)
767 FROM fusionax_anciens AS f
768 INNER JOIN profiles AS p ON (f.ax_id = p.ax_id)
769 INNER JOIN profile_public_names AS ppn ON (p.pid = ppn.pid)
770 WHERE f.prenom NOT IN (ppn.firstname_initial, ppn.firstname_main, ppn.firstname_ordinary)');
5bc0014e 771 $page->assign('firstnameIssues', count($this->retrieve_firstnames()));
193affc6
SJ
772 }
773 $page->assign('action', $action);
c7eac294 774 }
12419b5b 775
26ba053e 776 function handler_edu($page, $action = '')
2fad6caa
SJ
777 {
778 $page->changeTpl('fusionax/education.tpl');
779
95b1a6d8 780 $missingEducation = XDB::rawIterator("SELECT DISTINCT(f.Intitule_formation)
2fad6caa 781 FROM fusionax_formations AS f
95b1a6d8
SJ
782 WHERE f.Intitule_formation != '' AND NOT EXISTS (SELECT *
783 FROM profile_education_enum AS e
784 WHERE f.Intitule_formation = e.name)");
785 $missingDegree = XDB::rawIterator("SELECT DISTINCT(f.Intitule_diplome)
2fad6caa 786 FROM fusionax_formations AS f
95b1a6d8
SJ
787 WHERE f.Intitule_diplome != '' AND NOT EXISTS (SELECT *
788 FROM profile_education_degree_enum AS e
789 WHERE f.Intitule_diplome = e.abbreviation)");
790 $missingCouple = XDB::rawIterator("SELECT DISTINCT(f.Intitule_formation) AS edu, f.Intitule_diplome AS degree, ee.id AS eduid, de.id AS degreeid
2fad6caa 791 FROM fusionax_formations AS f
95b1a6d8
SJ
792 INNER JOIN profile_education_enum AS ee ON (f.Intitule_formation = ee.name)
793 INNER JOIN profile_education_degree_enum AS de ON (f.Intitule_diplome = de.abbreviation)
2fad6caa
SJ
794 WHERE f.Intitule_diplome != '' AND f.Intitule_formation != ''
795 AND NOT EXISTS (SELECT *
796 FROM profile_education_degree AS d
797 WHERE ee.id = d.eduid AND de.id = d.degreeid)");
798
799 $page->assign('missingEducation', $missingEducation);
800 $page->assign('missingDegree', $missingDegree);
801 $page->assign('missingCouple', $missingCouple);
802 $page->assign('missingEducationCount', $missingEducation->total());
803 $page->assign('missingDegreeCount', $missingDegree->total());
804 $page->assign('missingCoupleCount', $missingCouple->total());
805 }
806
26ba053e 807 function handler_corps($page)
551e00c1
SJ
808 {
809 $page->changeTpl('fusionax/corps.tpl');
810
811 $missingCorps = XDB::rawIterator('SELECT DISTINCT(f.corps_sortie) AS name
812 FROM fusionax_anciens AS f
813 WHERE NOT EXISTS (SELECT *
814 FROM profile_corps_enum AS c
815 WHERE f.corps_sortie = c.abbreviation)');
816 $missingGrade = XDB::rawIterator('SELECT DISTINCT(f.grade) AS name
817 FROM fusionax_anciens AS f
818 WHERE NOT EXISTS (SELECT *
819 FROM profile_corps_rank_enum AS c
820 WHERE f.grade = c.name)');
821
822 $page->assign('missingCorps', $missingCorps);
823 $page->assign('missingGrade', $missingGrade);
824 $page->assign('missingCorpsCount', $missingCorps->total());
825 $page->assign('missingGradeCount', $missingGrade->total());
826 }
827
26ba053e 828 function handler_issues_deathdate($page, $action = '')
12419b5b
SJ
829 {
830 $page->changeTpl('fusionax/deathdate_issues.tpl');
831 if ($action == 'edit') {
832 S::assert_xsrf_token();
833
834 $issues = XDB::rawIterRow('SELECT p.pid, pd.directory_name, pd.promo, pm.deathdate_ax, p.deathdate
835 FROM profile_merge_issues AS pm
836 INNER JOIN profiles AS p ON (pm.pid = p.pid)
837 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
838 WHERE FIND_IN_SET(\'deathdate\', pm.issues)
839 ORDER BY pd.directory_name');
840 while (list($pid, $name, $promo, $deathAX, $deathXorg) = $issues->next()) {
841 $choiceAX = Post::has('AX_' . $pid);
842 $choiceXorg = Post::has('XORG_' . $pid);
843 if (!($choiceAX || $choiceXorg)) {
844 continue;
845 }
846
847 if ($choiceAX) {
848 XDB::execute('UPDATE profiles AS p
849 INNER JOIN profile_merge_issues AS pm ON (pm.pid = p.pid)
850 SET p.deathdate = pm.deathdate_ax, p.deathdate_rec = NOW()
851 WHERE p.pid = {?}', $pid);
852 }
853 XDB::execute("UPDATE profile_merge_issues
854 SET issues = REPLACE(issues, 'deathdate', '')
855 WHERE pid = {?}", $pid());
856 $page->trigSuccess("La date de décès de $name ($promo) a bien été corrigée.");
857 }
858 }
859
860 $issues = XDB::rawFetchAllAssoc('SELECT p.pid, p.hrpid, pd.directory_name, pd.promo, pm.deathdate_ax, p.deathdate
861 FROM profile_merge_issues AS pm
862 INNER JOIN profiles AS p ON (pm.pid = p.pid)
863 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
864 WHERE FIND_IN_SET(\'deathdate\', pm.issues)
865 ORDER BY pd.directory_name');
866 $page->assign('issues', $issues);
867 $page->assign('total', count($issues));
868 }
869
26ba053e 870 function handler_issues_promo($page, $action = '')
12419b5b
SJ
871 {
872 $page->changeTpl('fusionax/promo_issues.tpl');
873 if ($action == 'edit') {
874 S::assert_xsrf_token();
875
876 $issues = XDB::rawIterRow('SELECT p.pid, pd.directory_name, pd.promo, pm.entry_year_ax, pe.entry_year, pe.grad_year
877 FROM profile_merge_issues AS pm
878 INNER JOIN profiles AS p ON (pm.pid = p.pid)
879 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
880 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
881 WHERE FIND_IN_SET(\'promo\', pm.issues)
882 ORDER BY pd.directory_name');
883 while (list($pid, $name, $promo, $deathAX, $deathXorgEntry, $deathXorgGrad) = $issues->next()) {
884 $choiceXorg = Post::has('XORG_' . $pid);
885 if (!(Post::has('display_' . $pid) && Post::has('entry_' . $pid) && Post::has('grad_' . $pid))) {
886 continue;
887 }
888
889 $display = Post::i('display_' . $pid);
890 $entry = Post::i('entry_' . $pid);
891 $grad = Post::i('grad_' . $pid);
892 if (!(($grad <= $entry + 5 && $grad >= $entry + 3) && ($display >= $entry && $display <= $grad - 3))) {
893 $page->trigError("La promotion de $name n'a pas été corrigée.");
894 continue;
895 }
896 XDB::execute('UPDATE profile_display
897 SET promo = {?}
898 WHERE pid = {?}', 'X' . $display, $pid);
899 XDB::execute('UPDATE profile_education
900 SET entry_year = {?}, grad_year = {?}
901 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)', $entry, $grad, $pid);
902 $page->trigSuccess("La promotion de $name a bien été corrigée.");
903 }
904 }
905
906 $issues = XDB::rawFetchAllAssoc('SELECT p.pid, p.hrpid, pd.directory_name, pd.promo, pm.entry_year_ax, pe.entry_year, pe.grad_year
907 FROM profile_merge_issues AS pm
908 INNER JOIN profiles AS p ON (pm.pid = p.pid)
909 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
910 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
911 WHERE FIND_IN_SET(\'promo\', pm.issues)
912 ORDER BY pd.directory_name');
913 $page->assign('issues', $issues);
914 $page->assign('total', count($issues));
915 }
916
26ba053e 917 function handler_issues($page, $action = '')
12419b5b
SJ
918 {
919 static $issueList = array(
920 'name' => 'noms',
921 'phone' => 'téléphones',
922 'education' => 'formations',
923 'address' => 'adresses',
924 'job' => 'emplois'
925 );
d20f50cf
SJ
926 static $typeList = array(
927 'name' => 'general',
928 'phone' => 'general',
929 'education' => 'general',
930 'address' => 'adresses',
931 'job' => 'emploi'
932 );
12419b5b
SJ
933
934 if (!array_key_exists($action, $issueList)) {
935 pl_redirect('fusionax');
936 } else {
937 $total = XDB::fetchOneCell('SELECT COUNT(*)
938 FROM profile_merge_issues
939 WHERE FIND_IN_SET({?}, issues)', $action);
940 if ($total == 0) {
941 pl_redirect('fusionax');
942 }
943
944 $issues = XDB::fetchAllAssoc('SELECT p.hrpid, pd.directory_name, pd.promo
945 FROM profile_merge_issues AS pm
946 INNER JOIN profiles AS p ON (pm.pid = p.pid)
947 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
948 WHERE FIND_IN_SET({?}, pm.issues)
949 ORDER BY pd.directory_name
950 LIMIT 100', $action);
951
952 $page->changeTpl('fusionax/other_issues.tpl');
953 $page->assign('issues', $issues);
954 $page->assign('issue', $issueList[$action]);
d20f50cf 955 $page->assign('type', $typeList[$action]);
12419b5b
SJ
956 $page->assign('total', $total);
957 }
958 }
b9ad0878 959}
e8591429 960
a3f12425
SJ
961// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
962?>