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