Fixes module 'admin' (handler 'user') when no login is provided.
[platal.git] / modules / admin.php
index c4f79f5..1caaa6c 100644 (file)
@@ -50,6 +50,7 @@ class AdminModule extends PLModule
             'admin/icons'                  => $this->make_hook('icons', AUTH_MDP, 'admin'),
             'admin/accounts'               => $this->make_hook('accounts', AUTH_MDP, 'admin'),
             'admin/account/types'          => $this->make_hook('account_types', AUTH_MDP, 'admin'),
+            'admin/jobs'                   => $this->make_hook('jobs', AUTH_MDP, 'admin'),
         );
     }
 
@@ -376,6 +377,10 @@ class AdminModule extends PLModule
 
         // Loads the user identity using the environment.
         $user = User::get($login);
+        if (!$user) {
+            return;
+        }
+
         $login = $user->login();
         $registered = ($user->state != 'pending');
 
@@ -418,6 +423,11 @@ class AdminModule extends PLModule
             }
             if (!Post::blank('hashpass')) {
                 $to_update['password'] = Post::s('hashpass');
+                // TODO: Propagate the password update to GoogleApps, when required. Eg:
+                // $account = new GoogleAppsAccount($user);
+                // if ($account->active() && $account->sync_password) {
+                //     $account->set_password($pass_encrypted);
+                // }
             }
             if (!Post::blank('weak_password')) {
                 $to_update['weak_password'] = Post::s('weak_password');
@@ -449,6 +459,8 @@ class AdminModule extends PLModule
             }
         }
         if (!empty($to_update)) {
+            // TODO: fetch the initial values of the fields, and eventually send
+            // a summary of the changes to an admin.
             $set = array();
             foreach ($to_update as $k => $value) {
                 $set[] = XDB::format($k . ' = {?}', $value);
@@ -614,7 +626,6 @@ class AdminModule extends PLModule
             $page->assign('emails', $redirect->emails);
         }
 
-        $page->assign('mr', $mr);
         $page->assign('user', $user);
 
         // Displays forum bans.
@@ -625,13 +636,14 @@ class AdminModule extends PLModule
         $page->assign('bans', $bans);
     }
 
-    function getHruid($line, $key)
+    function getHruid($line, $partial_result, $key)
     {
-        var_dump($line);
-        if (!isset($line['nom']) || !isset($line['prenom']) || !isset($line['promo'])) {
+        if (!isset($partial_result['nom']) ||
+            !isset($partial_result['prenom']) ||
+            !isset($partial_result['promo'])) {
             return null;
         }
-        return make_forlife($line['prenom'], $line['nom'], $line['promo']);
+        return make_forlife($partial_result['prenom'], $partial_result['nom'], $partial_result['promo']);
     }
 
     function getMatricule($line, $key)
@@ -672,9 +684,11 @@ class AdminModule extends PLModule
             $fields = array('hruid', 'nom', 'nom_ini', 'prenom', 'naissance_ini',
                             'prenom_ini', 'promo', 'promo_sortie', 'flags',
                             'matricule', 'matricule_ax', 'perms');
-            $importer->forceValue('hruid', array($this, 'getHruid'));
             $importer->forceValue('promo', $promo);
             $importer->forceValue('promo_sortie', $promo + 3);
+            // The hruid generation callback is set last, so that it is called once 'promo'
+            // has already been computed for that line.
+            $importer->forceValue('hruid', array($this, 'getHruid'));
             break;
           case 'ax':
             $fields = array('matricule', 'matricule_ax');
@@ -1163,6 +1177,68 @@ class AdminModule extends PLModule
                                                 WHERE  a.is_admin
                                              ORDER BY  a.hruid'));
     }
+
+    function handler_jobs(&$page, $id = -1)
+    {
+        $page->changeTpl('admin/jobs.tpl');
+
+        if (Env::has('search')) {
+            $res = XDB::query("SELECT  e.id, e.name, e.acronym
+                                 FROM  profile_job_enum AS e
+                                WHERE  e.name LIKE CONCAT('% ', {?}, '%') OR e.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');
+
+            if (Env::has('change')) {
+                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 {
+                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'), Env::i('AX_code'), Env::i('holdingId'), $id);
+
+                $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
+                                 FROM  profile_job_enum AS e
+                            LEFT JOIN  profile_job_enum AS h ON (e.holdingid = h.id)
+                                WHERE  e.id = {?}',
+                              $id);
+
+            if ($res->numRows() == 0) {
+                $page->trigError('Auncune entreprise ne correspond à cet identifiant.');
+            } else {
+                $page->assign('selectedJob', $res->fetchOneAssoc());
+            }
+        }
+    }
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: