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