Add 'hidden' flag to text descriptions of the 'pub' flag
[platal.git] / modules / profile / general.inc.php
index 08b09ca..f9da015 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -63,25 +63,18 @@ class ProfileSettingSearchNames implements ProfileSetting
 
     public function value(ProfilePage $page, $field, $value, &$success)
     {
+        require_once 'name.func.inc.php';
         $success = true;
 
         if (is_null($value)) {
             $request = NamesReq::getPublicNames($page->pid());
 
             if (!$request) {
-                $value['public_names'] = XDB::fetchOneAssoc('SELECT  particles, lastname_main, lastname_marital, lastname_ordinary,
+                $value['public_names'] = XDB::fetchOneAssoc('SELECT  lastname_main, lastname_marital, lastname_ordinary,
                                                                      firstname_main, firstname_ordinary, pseudonym
                                                                FROM  profile_public_names
                                                               WHERE  pid = {?}',
                                                             $page->pid());
-
-                $flags = new PlFlagSet($value['public_names']['particles']);
-                unset($value['public_names']['particles']);
-                static $suffixes = array('main', 'marital', 'ordinary');
-
-                foreach ($suffixes as $suffix) {
-                    $value['public_names']['particle_' . $suffix] = $flags->hasFlag($suffix);
-                }
             } else {
                 $value['public_names'] = $request;
                 Platal::page()->assign('validation', true);
@@ -94,12 +87,14 @@ class ProfileSettingSearchNames implements ProfileSetting
                                                          $page->pid());
         } else {
             foreach ($value['public_names'] as $key => $name) {
-                $value['public_names'][$key] = trim($name);
+                $value['public_names'][$key] = capitalize_name(trim($name));
             }
-            foreach ($value['private_names'] as $key => $name) {
-                $value['private_names'][$key]['name'] = trim($name['name']);
-                if ($value['private_names'][$key]['name'] == '') {
-                    unset($value['private_names'][$key]);
+            if (isset($value['private_names'])) {
+                foreach ($value['private_names'] as $key => $name) {
+                    $value['private_names'][$key]['name'] = trim($name['name']);
+                    if ($value['private_names'][$key]['name'] == '') {
+                        unset($value['private_names'][$key]);
+                    }
                 }
             }
 
@@ -108,10 +103,14 @@ class ProfileSettingSearchNames implements ProfileSetting
             }
         }
 
-        require_once 'name.func.inc.php';
         $public_name = build_first_name($value['public_names']) . ' ' . build_full_last_name($value['public_names'], $page->profile->isFemale());
-        $private_name_end = build_private_name($value['private_names']);
-        $private_name = $public_name . $private_name_end;
+        if (isset($value['private_names'])) {
+            $private_name_end = build_private_name($value['private_names']);
+            $private_name = $public_name . $private_name_end;
+        } else {
+            $value['private_names'] = array();
+            $private_name = $public_name;
+        }
 
         Platal::page()->assign('public_name', $public_name);
         Platal::page()->assign('private_name', $private_name);
@@ -142,8 +141,10 @@ class ProfileSettingSearchNames implements ProfileSetting
                      $page->pid());
         $values = array();
         $nickname = $lastname = $firstname = 0;
-        foreach ($value['private_names'] as $name) {
-            $values[] = XDB::format('({?}, {?}, {?}, {?})', $page->pid(), $name['type'], $$name['type']++, $name['name']);
+        if (isset($value['private_names'])) {
+            foreach ($value['private_names'] as $name) {
+                $values[] = XDB::format('({?}, {?}, {?}, {?})', $page->pid(), $name['type'], $$name['type']++, $name['name']);
+            }
         }
         if (count($values)) {
             XDB::rawExecute('INSERT INTO  profile_private_names (pid, type, id, name)
@@ -153,7 +154,8 @@ class ProfileSettingSearchNames implements ProfileSetting
         if ($has_diff) {
             update_display_names($page->profile, $old, $value['private_names']);
         } else {
-            update_display_names($page->profile, $value['public_names'], $value['private_names']);
+            update_display_names($page->profile,
+                                 $value['public_names'], (isset($value['private_names']) ? $value['private_names'] : null));
         }
     }
 
@@ -165,7 +167,7 @@ class ProfileSettingSearchNames implements ProfileSetting
             }
         }
 
-        if (count($value['private_names'])) {
+        if (isset($value['private_names']) && count($value['private_names'])) {
             $private_names = array();
             foreach ($value['private_names'] as $name) {
                 $private_names[] = $name['name'];
@@ -198,7 +200,7 @@ class ProfileSettingEdu implements ProfileSetting
             $value = array();
             $value = XDB::fetchAllAssoc("SELECT  eduid, degreeid, fieldid, grad_year, program
                                            FROM  profile_education
-                                          WHERE  pid = {?} AND !FIND_IN_SET('primary', flags)
+                                          WHERE  pid = {?} AND !(FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags))
                                        ORDER BY  id",
                                         $page->pid());
         } else if (!is_array($value)) {
@@ -229,10 +231,11 @@ class ProfileSettingEdu implements ProfileSetting
     public function save(ProfilePage $page, $field, $value)
     {
         XDB::execute("DELETE FROM  profile_education
-                            WHERE  pid = {?} AND !FIND_IN_SET('primary', flags)",
+                            WHERE  pid = {?} AND !(FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags))",
                      $page->pid());
+        $schoolsList = DirEnum::getOptions(DirEnum::EDUSCHOOLS);
         foreach ($value as $eduid=>&$edu) {
-            if ($edu['eduid'] != '') {
+            if ($edu['eduid'] != '' && $schoolsList[$edu['eduid']] != Profile::EDU_X) {
                 $fieldId = ($edu['fieldid'] == 0) ? null : $edu['fieldid'];
                 XDB::execute("INSERT INTO  profile_education
                                       SET  id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
@@ -301,8 +304,8 @@ class ProfileSettingMainEdu implements ProfileSetting
             $value = array();
             $value = XDB::fetchAllAssoc("SELECT  degreeid, fieldid, promo_year, program
                                            FROM  profile_education
-                                          WHERE  pid = {?} AND FIND_IN_SET('primary', flags)
-                                       ORDER BY  degreeid",
+                                          WHERE  pid = {?} AND (FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags))
+                                       ORDER BY  NOT FIND_IN_SET('primary', flags), degreeid",
                                         $page->pid());
 
             foreach ($value as &$item) {
@@ -327,7 +330,7 @@ class ProfileSettingMainEdu implements ProfileSetting
             $fieldId = ($item['fieldid'] == 0) ? null : $item['fieldid'];
             XDB::execute("UPDATE  profile_education
                              SET  fieldid = {?}, program = {?}
-                           WHERE  pid = {?} AND FIND_IN_SET('primary', flags) AND degreeid = {?}",
+                           WHERE  pid = {?} AND (FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags)) AND degreeid = {?}",
                          $fieldId, $item['program'], $page->pid(), $item['degreeid']);
         }
     }
@@ -337,11 +340,11 @@ class ProfileSettingMainEdu implements ProfileSetting
         $educations = array();
         foreach ($value as $item) {
             $details = array($this->cycles[$item['degreeid']]);
-            if ($education['program']) {
-                $details[] = '« ' . $education['program'] . ' »';
+            if ($item['program']) {
+                $details[] = '« ' . $item['program'] . ' »';
             }
-            if ($education['fieldid']) {
-                $details[] = $fieldsList[$education['fieldid']];
+            if ($item['fieldid']) {
+                $details[] = $fieldsList[$item['fieldid']];
             }
         }
         return implode(', ', $educations);
@@ -450,7 +453,7 @@ class ProfileSettingNetworking implements ProfileSetting
     }
 
     public function getText($value) {
-        static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
+        static $pubs = array('public' => 'publique', 'ax' => 'annuaire papier', 'private' => 'privé', 'hidden' => 'administrateurs');
         $networkings = array();
         foreach ($value as $network) {
             $networkings[] = $network['name'] . ' : ' . $network['address'] . ' (affichage ' . $pubs[$network['pub']] . ')';
@@ -459,6 +462,79 @@ class ProfileSettingNetworking implements ProfileSetting
     }
 }
 
+class ProfileSettingHobby implements ProfileSetting
+{
+    private $pub;
+    static private $type = array('Sport', 'Loisir', 'Hobby');
+
+    public function __construct()
+    {
+        $this->pub = new ProfileSettingPub();
+    }
+
+    public function value(ProfilePage $page, $field, $value, &$success)
+    {
+        $success = true;
+        if (is_null($value)) {
+            $value = XDB::fetchAllAssoc('SELECT  type, text, pub
+                                           FROM  profile_hobby
+                                          WHERE  pid = {?}',
+                                         $page->pid());
+        }
+        if (!is_array($value)) {
+            return array();
+        }
+        foreach($value as $i => &$hobby) {
+            $hobby['text'] = trim($hobby['text']);
+            if (!$hobby['text'] ||!in_array($hobby['type'], self::$type)) {
+                unset($value[$i]);
+            } else {
+                if (!isset($hobby['pub'])) {
+                    $hobby['pub'] = 'private';
+                }
+                $s = true;
+                $hobby['pub'] = $this->pub->value($page, 'pub', $hobby['pub'], $s);
+                $success = $success && $s;
+            }
+        }
+        return $value;
+    }
+
+    public function save(ProfilePage $page, $field, $value)
+    {
+        XDB::execute('DELETE FROM profile_hobby
+                            WHERE pid = {?}',
+                     $page->pid());
+        if (!count($value)) {
+            return;
+        }
+        foreach ($value as $id => $hobby) {
+            XDB::execute("INSERT INTO  profile_hobby (pid, id, type, text, pub)
+                               VALUES  ({?}, {?}, {?}, {?}, {?})",
+                         $page->pid(), $id, $hobby['type'], $hobby['text'], $hobby['pub']);
+        }
+    }
+
+    public function getText($value) {
+        static $pubs = array('public' => 'publique', 'ax' => 'annuaire papier', 'private' => 'privé', 'hidden' => 'administrateurs');
+        $hobbies = array();
+        foreach (self::$type as $type) {
+            $hobbies[$type] = array();
+        }
+        foreach ($value as $hobby) {
+            $hobbies[$hobby['type']][] = $hobby['text'] . ' (affichage ' . $pubs[$hobby['pub']] . ')';
+        }
+        $text = array();
+        foreach (self::$type as $type) {
+            if (!empty($hobbies[$type])) {
+                $text[] = $hobbies[$type] . ' : ' . implode(', ' , $hobbies[$type]);
+            }
+        }
+        return implode(', ' , $text);
+    }
+}
+
+
 class ProfileSettingPromo implements ProfileSetting
 {
     public function __construct(){}
@@ -506,7 +582,13 @@ class ProfileSettingPromo implements ProfileSetting
                 }
                 Platal::page()->assign('promo_choice', $promoChoice);
             }
-            return $yearpromo;
+
+            // If this profile belongs to an X, return grad year minus education duration.
+            if ($page->profile->mainEducation() == 'X') {
+                return $gradYear - $page->profile->mainEducationDuration();
+            }
+
+            return $gradYear;
         }
 
         // If this profile belongs to an X, $promoNew needs to be changed to
@@ -539,31 +621,31 @@ class ProfilePageGeneral extends ProfilePage
     public function __construct(PlWizard $wiz)
     {
         parent::__construct($wiz);
-        $this->settings['search_names']
-                                  = new ProfileSettingSearchNames();
-        $this->settings['nationality1']
-                                  = $this->settings['nationality2']
-                                  = $this->settings['nationality3']
-                                  = $this->settings['promo_display']
-                                  = null;
-        $this->settings['email_directory']
-                                  = new ProfileSettingEmail();
-        $this->settings['email_directory_new']
-                                  = new ProfileSettingEmailDirectory();
-        $this->settings['networking'] = new ProfileSettingNetworking();
-        $this->settings['tels']   = new ProfileSettingPhones();
-        $this->settings['edus']   = new ProfileSettingEdu();
+        $this->settings['search_names'] = new ProfileSettingSearchNames();
+        $this->settings['nationality1'] = $this->settings['nationality2']
+                                        = $this->settings['nationality3']
+                                        = $this->settings['promo_display']
+                                        = $this->settings['profile_title']
+                                        = null;
+        $this->settings['email_directory'] = new ProfileSettingEmail();
+        $this->settings['email_directory_new'] = new ProfileSettingEmailDirectory();
+        $this->settings['tels'] = new ProfileSettingPhones();
+        $this->settings['edus'] = new ProfileSettingEdu();
         $this->settings['main_edus'] = new ProfileSettingMainEdu();
         $this->settings['promo']  = new ProfileSettingPromo();
-        $this->watched= array('tels' => true,
-                              'networking' => true, 'edus' => true,
-                              'nationality1' => true, 'nationality2' => true,
-                              'nationality3' => true, 'search_names' => true);
+        $this->settings['networking'] = new ProfileSettingNetworking();
+        $this->settings['hobbies'] = new ProfileSettingHobby();
+        $this->watched = array('tels' => true,
+                               'networking' => true, 'edus' => true,
+                               'nationality1' => true, 'nationality2' => true,
+                               'nationality3' => true, 'search_names' => true);
 
         /* Some fields editable under condition */
         if (!S::user()->isMe($this->owner)) {
             $this->settings['deathdate'] = new ProfileSettingDate(true);
             $this->settings['birthdate'] = new ProfileSettingDate(true);
+            $this->settings['birthdate_ref'] = new ProfileSettingDate(true);
+            $this->settings['axfreetext'] = null;
         } else {
             $this->settings['yourself'] = null;
             $this->settings['birthdate'] = new ProfileSettingDate();
@@ -571,12 +653,12 @@ class ProfilePageGeneral extends ProfilePage
         if (S::user()->checkPerms('directory_private')
             || S::user()->isMyProfile($this->owner)) {
             $this->settings['freetext'] = null;
-            $this->settings['freetext_pub']
-                                      = $this->settings['photo_pub']
-                                      = new ProfileSettingPub();
+            $this->settings['freetext_pub'] = $this->settings['photo_pub']
+                                            = new ProfileSettingPub();
             $this->watched['freetext'] = true;
         }
 
+        Platal::page()->assign('is_registered', ($this->owner->perms ? true : false));
     }
 
     protected function _fetchData()
@@ -584,8 +666,9 @@ class ProfilePageGeneral extends ProfilePage
         // Checkout all data...
         $res = XDB::query("SELECT  p.nationality1, p.nationality2, p.nationality3, IF(p.birthdate = 0, '', p.birthdate) AS birthdate,
                                    p.email_directory as email_directory, pd.promo AS promo_display,
-                                   p.freetext, p.freetext_pub, p.ax_id AS matricule_ax, pd.yourself,
-                                   p.deathdate
+                                   p.freetext, p.freetext_pub, p.axfreetext, p.ax_id AS matricule_ax, pd.yourself,
+                                   p.deathdate, IF(p.birthdate_ref = 0, '', p.birthdate_ref) AS birthdate_ref,
+                                   p.title AS profile_title
                              FROM  profiles              AS p
                        INNER JOIN  profile_display       AS pd ON (pd.pid = p.pid)
                             WHERE  p.pid = {?}", $this->pid());
@@ -616,7 +699,7 @@ class ProfilePageGeneral extends ProfilePage
     {
         if ($this->changed['nationality1'] || $this->changed['nationality2'] || $this->changed['nationality3']
             || $this->changed['birthdate'] || $this->changed['freetext'] || $this->changed['freetext_pub']
-            || $this->changed['email_directory']) {
+            || $this->changed['axfreetext'] || $this->changed['email_directory'] || $this->changed['profile_title']) {
             if ($this->values['nationality3'] == "") {
                 $this->values['nationality3'] = NULL;
             }
@@ -649,11 +732,12 @@ class ProfilePageGeneral extends ProfilePage
 
             XDB::execute("UPDATE  profiles
                              SET  nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
-                                  freetext = {?}, freetext_pub = {?}, email_directory = {?}
+                                  freetext = {?}, freetext_pub = {?}, axfreetext = {?}, email_directory = {?}, title = {?}
                            WHERE  pid = {?}",
                           $this->values['nationality1'], $this->values['nationality2'], $this->values['nationality3'],
                           ProfileSettingDate::toSQLDate($this->values['birthdate']),
-                          $this->values['freetext'], $this->values['freetext_pub'], $new_email, $this->pid());
+                          $this->values['freetext'], $this->values['freetext_pub'], $this->values['axfreetext'], $new_email,
+                          $this->values['profile_title'], $this->pid());
         }
         if ($this->changed['photo_pub']) {
             XDB::execute("UPDATE  profile_photos
@@ -690,6 +774,12 @@ class ProfilePageGeneral extends ProfilePage
                 }
             }
         }
+        if ($this->changed['birthdate_ref'] && S::admin() && !$this->owner->perms) {
+            XDB::execute('UPDATE  profiles
+                             SET  birthdate_ref = {?}
+                           WHERE  pid = {?}',
+                         ProfileSettingDate::toSQLDate($this->values['birthdate_ref']), $this->pid());
+        }
         if (!S::user()->isMe($this->owner) && $this->changed['deathdate']) {
             XDB::execute('UPDATE  profiles
                              SET  deathdate = {?}, deathdate_rec = NOW()
@@ -735,5 +825,5 @@ class ProfilePageGeneral extends ProfilePage
     }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>