+ $page->assign('disabled', XDB::iterator('SELECT a.hruid, FIND_IN_SET(\'watch\', a.flags) AS watch,
+ a.state = \'disabled\' AS disabled, a.comment
+ FROM accounts AS a
+ WHERE a.state = \'disabled\' OR FIND_IN_SET(\'watch\', a.flags)
+ ORDER BY a.hruid'));
+ $page->assign('admins', XDB::iterator('SELECT a.hruid
+ FROM accounts AS a
+ WHERE a.is_admin
+ ORDER BY a.hruid'));
+ }
+
+ function handler_xnet_without_group($page)
+ {
+ $page->changeTpl('admin/xnet_without_group.tpl');
+ $page->assign('accounts', XDB::iterator('SELECT a.hruid, a.state
+ FROM accounts AS a
+ LEFT JOIN group_members AS m ON (a.uid = m.uid)
+ WHERE a.type = \'xnet\' AND m.uid IS NULL
+ ORDER BY a.state, a.hruid'));
+ }
+
+ function handler_jobs($page, $id = -1)
+ {
+ $page->changeTpl('admin/jobs.tpl');
+
+ if (Env::has('search')) {
+ $res = XDB::query("SELECT id, name, acronym
+ FROM profile_job_enum
+ WHERE name LIKE CONCAT('%', {?}, '%') OR acronym LIKE CONCAT('%', {?}, '%')",
+ Env::t('job'), Env::t('job'));
+
+ if ($res->numRows() <= 20) {
+ $page->assign('jobs', $res->fetchAllAssoc());
+ } else {
+ $page->trigError("Il y a trop d'entreprises correspondant à ton choix. Affine-le !");
+ }
+
+ $page->assign('askedJob', Env::v('job'));
+ return;
+ }
+
+ if (Env::has('edit')) {
+ S::assert_xsrf_token();
+ $selectedJob = Env::has('selectedJob');
+
+ Phone::deletePhones(0, Phone::LINK_COMPANY, $id);
+ Address::deleteAddresses(null, Address::LINK_COMPANY, $id);
+ if (Env::has('change')) {
+ if (Env::has('newJobId') && Env::i('newJobId') > 0) {
+ XDB::execute('UPDATE profile_job
+ SET jobid = {?}
+ WHERE jobid = {?}',
+ Env::i('newJobId'), $id);
+ XDB::execute('DELETE FROM profile_job_enum
+ WHERE id = {?}',
+ $id);
+
+ $page->trigSuccess("L'entreprise a bien été remplacée.");
+ } else {
+ $page->trigError("L'entreprise n'a pas été remplacée car l'identifiant fourni n'est pas valide.");
+ }
+ } else {
+ XDB::execute('UPDATE profile_job_enum
+ SET name = {?}, acronym = {?}, url = {?}, email = {?},
+ NAF_code = {?}, AX_code = {?}, holdingid = {?}
+ WHERE id = {?}',
+ Env::t('name'), Env::t('acronym'), Env::t('url'), Env::t('email'),
+ (Env::t('NAF_code') == 0 ? null : Env::t('NAF_code')),
+ (Env::i('AX_code') == 0 ? null : Env::t('AX_code')),
+ (Env::i('holdingId') == 0 ? null : Env::t('holdingId')), $id);
+
+ $phone = new Phone(array('display' => Env::v('tel'), 'link_id' => $id, 'id' => 0, 'type' => 'fixed',
+ 'link_type' => Phone::LINK_COMPANY, 'pub' => 'public'));
+ $fax = new Phone(array('display' => Env::v('fax'), 'link_id' => $id, 'id' => 1, 'type' => 'fax',
+ 'link_type' => Phone::LINK_COMPANY, 'pub' => 'public'));
+ $address = new Address(array('jobid' => $id, 'type' => Address::LINK_COMPANY, 'text' => Env::t('address')));
+ $phone->save();
+ $fax->save();
+ $address->save();
+
+ $page->trigSuccess("L'entreprise a bien été mise à jour.");
+ }
+ }
+
+ if (!Env::has('change') && $id != -1) {
+ $res = XDB::query("SELECT e.id, e.name, e.acronym, e.url, e.email, e.NAF_code, e.AX_code,
+ h.id AS holdingId, h.name AS holdingName, h.acronym AS holdingAcronym,
+ t.display_tel AS tel, f.display_tel AS fax, a.text AS address
+ FROM profile_job_enum AS e
+ LEFT JOIN profile_job_enum AS h ON (e.holdingid = h.id)
+ LEFT JOIN profile_phones AS t ON (t.pid = e.id AND t.link_type = 'hq' AND t.tel_id = 0)
+ LEFT JOIN profile_phones AS f ON (f.pid = e.id AND f.link_type = 'hq' AND f.tel_id = 1)
+ LEFT JOIN profile_addresses AS a ON (a.jobid = e.id AND a.type = 'hq')
+ WHERE e.id = {?}",
+ $id);
+
+ if ($res->numRows() == 0) {
+ $page->trigError('Auncune entreprise ne correspond à cet identifiant.');
+ } else {
+ $page->assign('selectedJob', $res->fetchOneAssoc());
+ }
+ }
+ }
+
+ function handler_profile($page)
+ {
+ $page->changeTpl('admin/profile.tpl');
+
+ if (Post::has('checked')) {
+ S::assert_xsrf_token();
+ $res = XDB::iterator('SELECT DISTINCT(pm.pid), pd.public_name
+ FROM profile_modifications AS pm
+ INNER JOIN profile_display AS pd ON (pm.pid = pd.pid)
+ WHERE pm.type = \'self\'');
+
+ while ($profile = $res->next()) {
+ if (Post::has('checked_' . $profile['pid'])) {
+ XDB::execute('DELETE FROM profile_modifications
+ WHERE type = \'self\' AND pid = {?}', $profile['pid']);
+
+ $page->trigSuccess('Profil de ' . $profile['public_name'] . ' vérifié.');
+ }
+ }
+ }
+
+ $res = XDB::iterator('SELECT p.hrpid, pm.pid, pd.directory_name, GROUP_CONCAT(pm.field SEPARATOR \', \') AS field
+ FROM profile_modifications AS pm
+ INNER JOIN profiles AS p ON (pm.pid = p.pid)
+ INNER JOIN profile_display AS pd ON (pm.pid = pd.pid)
+ WHERE pm.type = \'self\'
+ GROUP BY pd.directory_name
+ ORDER BY pd.directory_name');
+ $page->assign('updates', $res);
+ }
+
+ function handler_phd($page, $promo = null, $validate = false)
+ {
+ $page->changeTpl('admin/phd.tpl');
+ $eduDegrees = DirEnum::getOptions(DirEnum::EDUDEGREES);
+ $eduDegrees = array_flip($eduDegrees);
+
+ if (is_null($promo)) {
+ $promo_list = XDB::fetchColumn('SELECT DISTINCT(grad_year)
+ FROM profile_education
+ WHERE FIND_IN_SET(\'primary\', flags) AND NOT FIND_IN_SET(\'completed\', flags) AND degreeid = {?}
+ ORDER BY grad_year',
+ $eduDegrees[Profile::DEGREE_D]);
+ $page->assign('promo_list', $promo_list);
+ $page->assign('nothing', count($promo_list) == 0);
+ return;
+ }
+
+ if ($validate) {
+ S::assert_xsrf_token();
+
+ $list = XDB::iterator('SELECT pe.pid, pd.directory_name
+ FROM profile_education AS pe
+ INNER JOIN profile_display AS pd ON (pe.pid = pd.pid)
+ WHERE FIND_IN_SET(\'primary\', pe.flags) AND NOT FIND_IN_SET(\'completed\', pe.flags)
+ AND pe.degreeid = {?} AND pe.grad_year = {?}',
+ $eduDegrees[Profile::DEGREE_D], $promo);
+ while ($res = $list->next()) {
+ $pid = $res['pid'];
+ $name = $res['directory_name'];
+ if (Post::b('completed_' . $pid)) {
+ $grad_year = Post::t('grad_year_' . $pid);
+ XDB::execute('UPDATE profile_education
+ SET flags = \'primary,completed\', grad_year = {?}
+ WHERE FIND_IN_SET(\'primary\', flags) AND pid = {?}',
+ $grad_year, $pid);
+ XDB::execute('UPDATE profile_display
+ SET promo = {?}
+ WHERE pid = {?}',
+ 'D' . $grad_year, $pid);
+ $page->trigSuccess("Promotion de $name validée.");
+ }
+ }
+ }
+
+ $list = XDB::iterator('SELECT pe.pid, pd.directory_name
+ FROM profile_education AS pe
+ INNER JOIN profile_display AS pd ON (pe.pid = pd.pid)
+ WHERE FIND_IN_SET(\'primary\', pe.flags) AND NOT FIND_IN_SET(\'completed\', pe.flags)
+ AND pe.degreeid = {?} AND pe.grad_year = {?}
+ ORDER BY pd.directory_name',
+ $eduDegrees[Profile::DEGREE_D], $promo);
+ $page->assign('list', $list);
+ $page->assign('promo', $promo);
+ }
+
+ function handler_add_secondary_edu($page)
+ {
+ $page->changeTpl('admin/add_secondary_edu.tpl');
+
+ if (!(Post::has('verify') || Post::has('add'))) {
+ return;
+ } elseif (!Post::has('people')) {
+ $page->trigWarning("Aucune information n'a été fournie.");
+ return;
+ }
+
+ require_once 'name.func.inc.php';
+ $lines = explode("\n", Post::t('people'));
+ $separator = Post::t('separator');
+ $degree = Post::v('degree');
+ $promotion = Post::i('promotion');
+ $schoolsList = array_flip(DirEnum::getOptions(DirEnum::EDUSCHOOLS));
+ $degreesList = array_flip(DirEnum::getOptions(DirEnum::EDUDEGREES));
+ $edu_id = $schoolsList[Profile::EDU_X];
+ $degree_id = $degreesList[$degree];
+
+ $res = array(
+ 'incomplete' => array(),
+ 'empty' => array(),
+ 'multiple' => array(),
+ 'already' => array(),
+ 'new' => array()
+ );
+ $old_pids = array();
+ $new_pids = array();
+ foreach ($lines as $line) {
+ $line = trim($line);
+ $line_array = explode($separator, $line);
+ array_walk($line_array, 'trim');
+ if (count($line_array) != 3) {
+ $page->trigError("La ligne « $line » est incomplète.");
+ $res['incomplete'][] = $line;
+ continue;
+ }
+ $cond = new PFC_And(new UFC_NameTokens(split_name_for_search($line_array[0]), array(), false, false, Profile::LASTNAME));
+ $cond->addChild(new UFC_NameTokens(split_name_for_search($line_array[1]), array(), false, false, Profile::FIRSTNAME));
+ $cond->addChild(new UFC_Promo('=', UserFilter::DISPLAY, $line_array[2]));
+ $uf = new UserFilter($cond);
+ $pid = $uf->getPIDs();
+ $count = count($pid);
+ if ($count == 0) {
+ $page->trigError("La ligne « $line » ne correspond à aucun profil existant.");
+ $res['empty'][] = $line;
+ continue;
+ } elseif ($count > 1) {
+ $page->trigError("La ligne « $line » correspond à plusieurs profils existant.");
+ $res['multiple'][] = $line;
+ continue;
+ } else {
+ $count = XDB::fetchOneCell('SELECT COUNT(*) AS count
+ FROM profile_education
+ WHERE pid = {?} AND eduid = {?} AND degreeid = {?}',
+ $pid, $edu_id, $degree_id);
+ if ($count == 1) {
+ $res['already'][] = $line;
+ $old_pids[] = $pid[0];
+ } else {
+ $res['new'][] = $line;
+ $new_pids[] = $pid[0];
+ }
+ }
+ }
+
+ $display = array();
+ foreach ($res as $type => $res_type) {
+ if (count($res_type) > 0) {
+ $display = array_merge($display, array('--------------------' . $type . ':'), $res_type);
+ }
+ }
+ $page->assign('people', implode("\n", $display));
+ $page->assign('promotion', $promotion);
+ $page->assign('degree', $degree);
+
+ if (Post::has('add')) {
+ $entry_year = $promotion - Profile::educationDuration($degree);
+
+ if (Post::b('force_addition')) {
+ $pids = array_unique(array_merge($old_pids, $new_pids));
+ } else {
+ $pids = array_unique($new_pids);
+
+ // Updates years.
+ if (count($old_pids)) {
+ XDB::execute('UPDATE profile_education
+ SET entry_year = {?}, grad_year = {?}, promo_year = {?}
+ WHERE pid IN {?} AND eduid = {?} AND degreeid = {?}',
+ $entry_year, $promotion, $promotion, $old_pids, $edu_id, $degree_id);
+ }
+ }
+
+ // Precomputes values common to all users.
+ $select = XDB::format('MAX(id) + 1, pid, {?}, {?}, {?}, {?}, {?}, \'secondary\'',
+ $edu_id, $degree_id, $entry_year, $promotion, $promotion );
+ XDB::startTransaction();
+ foreach ($pids as $pid) {
+ XDB::execute('INSERT INTO profile_education (id, pid, eduid, degreeid, entry_year, grad_year, promo_year, flags)
+ SELECT ' . $select . '
+ FROM profile_education
+ WHERE pid = {?}
+ GROUP BY pid',
+ $pid);
+ }
+ XDB::commit();
+ }
+