Merge branch 'platal-1.0.0'
[platal.git] / modules / admin.php
index caa412a..c98c5bc 100644 (file)
@@ -47,6 +47,7 @@ class AdminModule extends PLModule
             'admin/ipwatch'                => $this->make_hook('ipwatch',                AUTH_MDP, 'admin'),
             'admin/icons'                  => $this->make_hook('icons',                  AUTH_MDP, 'admin'),
             'admin/accounts'               => $this->make_hook('accounts',               AUTH_MDP, 'admin'),
+            'admin/account/watch'          => $this->make_hook('account_watch',          AUTH_MDP, 'admin'),
             'admin/account/types'          => $this->make_hook('account_types',          AUTH_MDP, 'admin'),
             'admin/jobs'                   => $this->make_hook('jobs',                   AUTH_MDP, 'admin'),
         );
@@ -381,7 +382,7 @@ class AdminModule extends PLModule
             $user = User::get($login);
         }
         if (empty($user)) {
-            return;
+            pl_redirect('admin/accounts');
         }
 
         $login = $user->login();
@@ -408,6 +409,15 @@ class AdminModule extends PLModule
             }
         }
 
+        // Handles account deletion.
+        if (Post::has('account_deletion_confirmation')) {
+            $uid = $user->id();
+            $name = $user->fullName();
+            $user->profile()->clear();
+            $user->clear(true);
+            $page->trigSuccess("L'utilisateur $name ($uid) a bien été supprimé.");
+        }
+
         // Account Form {{{
         $to_update = array();
         if (Post::has('disable_weak_access')) {
@@ -1131,6 +1141,40 @@ class AdminModule extends PLModule
         $table_editor->apply($page, $action, $id);
     }
 
+    function handler_accounts(PlPage $page)
+    {
+        $page->changeTpl('admin/accounts.tpl');
+        $page->setTitle('Administration - Comptes');
+        $page->addJsLink('password.js');
+
+        if (Post::has('create_account')) {
+            S::assert_xsrf_token();
+            $firstname = Post::t('firstname');
+            $lastname = strtoupper(Post::t('lastname'));
+            $sex = Post::b('sex') ? User::GENDER_FEMALE : User::GENDER_FEMALE;
+            $email = Post::t('email');
+            $login = PlUser::makeHrid($firstname, $lastname, 'ax');
+            if (!isvalid_email($email)) {
+                $page->trigError("Invalid email address: $email");
+            } else if (strlen(Post::s('pwhash')) != 40) {
+                $page->trigError("Invalid password hash");
+            } else {
+                $full_name = $firstname . ' ' . $lastname;
+                $directory_name = $lastname . ' ' . $firstname;
+                XDB::execute("INSERT INTO  accounts (hruid, type, state, password,
+                                                     registration_date, email, full_name,
+                                                     display_name, sex, directory_name)
+                                   VALUES  ({?}, 'ax', 'active', {?}, NOW(), {?}, {?}, {?}, {?}, {?})",
+                             $login, Post::s('pwhash'), $email, $full_name, $full_name, $sex,
+                             $directory_name);
+            }
+        }
+
+        $uf = new UserFilter(new UFC_AccountType('ax'));
+        $page->assign('users', $uf->iterUsers(new PlLimit(10)));
+
+    }
+
     function handler_account_types(&$page, $action = 'list', $id = null)
     {
         $page->setTitle('Administration - Types de comptes');
@@ -1334,7 +1378,7 @@ class AdminModule extends PLModule
         $page->assign('icons', $icons);
     }
 
-    function handler_accounts(&$page)
+    function handler_account_watch(&$page)
     {
         $page->changeTpl('admin/accounts.tpl');
         $page->assign('disabled', XDB::iterator('SELECT  a.hruid, FIND_IN_SET(\'watch\', a.flags) AS watch,
@@ -1369,34 +1413,29 @@ class AdminModule extends PLModule
         }
 
         if (Env::has('edit')) {
-            // TODO: use address and phone classes to update profile_job_enum and profile_phones once they are done.
+            // TODO: use address class to update profile_job_enum once it is done.
 
             S::assert_xsrf_token();
             $selectedJob = Env::has('selectedJob');
 
-            XDB::execute("DELETE FROM  profile_phones
-                                WHERE  pid = {?} AND link_type = 'hq'",
-                         $id);
+            Phone::deletePhones(0, Phone::LINK_COMPANY, $id);
             XDB::execute("DELETE FROM  profile_addresses
                                 WHERE  jobid = {?} AND type = 'hq'",
                          $id);
-            XDB::execute('DELETE FROM  profile_job_enum
-                                WHERE  id = {?}',
-                         $id);
 
             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 {
-                require_once 'profil.func.inc.php';
                 require_once 'geocoding.inc.php';
 
-                $display_tel = format_display_number(Env::v('tel'), $error_tel);
-                $display_fax = format_display_number(Env::v('fax'), $error_fax);
                 $gmapsGeocoder = new GMapsGeocoder();
                 $address = array('text' => Env::t('address'));
                 $address = $gmapsGeocoder->getGeocodedAddress($address);
@@ -1411,12 +1450,12 @@ class AdminModule extends PLModule
                              Env::t('name'), Env::t('acronym'), Env::t('url'), Env::t('email'),
                              Env::t('NAF_code'), Env::i('AX_code'), Env::i('holdingId'), $id);
 
-                XDB::execute("INSERT INTO  profile_phones (pid, link_type, link_id, tel_id, tel_type,
-                                           search_tel, display_tel, pub)
-                                   VALUES  ({?}, 'hq', 0, 0, 'fixed', {?}, {?}, 'public'),
-                                           ({?}, 'hq', 0, 1, 'fax', {?}, {?}, 'public')",
-                             $id, format_phone_number(Env::v('tel')), $display_tel,
-                             $id, format_phone_number(Env::v('fax')), $display_fax);
+                $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'));
+                $phone->save();
+                $fax->save();
 
                 XDB::execute("INSERT INTO  profile_addresses (jobid, type, id, accuracy,
                                                               text, postalText, postalCode, localityId,