Editing the name of a profile update the name of the associated account
[platal.git] / modules / profile / general.inc.php
index 6967a38..13106c1 100644 (file)
@@ -19,6 +19,8 @@
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
+require_once 'name.func.inc.php';
+
 class ProfileSettingSearchNames implements ProfileSetting
 {
     private $private_name_end;
@@ -38,8 +40,7 @@ class ProfileSettingSearchNames implements ProfileSetting
 
     private function prepareField($value)
     {
-        $value = mb_strtoupper(replace_accent($value));
-        return preg_replace('/[^A-Z]/', ' ', $value);
+        return name_to_basename($value);
     }
 
     private function prepare(ProfilePage &$page, $field, $value, $init, &$success)
@@ -135,8 +136,8 @@ class ProfileSettingSearchNames implements ProfileSetting
             $res = XDB::query("SELECT  s.particle, s.name
                                  FROM  profile_name      AS s
                            INNER JOIN  profile_name_enum AS e ON (e.id = s.typeid)
-                                WHERE  s.pid = {?} AND e.type LIKE '%ini'
-                             ORDER BY  e.type = 'firstname_ini'",
+                                WHERE  s.pid = {?} AND (e.type = 'lastname' OR e.type = 'firstname')
+                             ORDER BY  e.type = 'firstname'",
                              $page->pid());
             $res = $res->fetchAllAssoc();
             $initial = array();
@@ -333,16 +334,15 @@ class ProfileSettingNetworking implements ProfileSetting
     public function value(ProfilePage &$page, $field, $value, &$success)
     {
         if (is_null($value)) {
-            $value = XDB::fetchAllAssoc("SELECT  n.address, n.network_type AS type, n.pub, m.name
+            $value = XDB::fetchAllAssoc("SELECT  n.address, n.pub, n.nwid AS type
                                            FROM  profile_networking AS n
-                                     INNER JOIN  profile_networking_enum AS m ON (n.network_type = m.network_type)
                                           WHERE  n.pid = {?}",
                                          $page->pid());
         }
         if (!is_array($value)) {
             $value = array();
         }
-        $filters = XDB::fetchAllAssoc('type', 'SELECT  filter, network_type AS type
+        $filters = XDB::fetchAllAssoc('type', 'SELECT  filter, nwid AS type, name
                                                  FROM  profile_networking_enum;');
         $success = true;
         foreach($value as $i=>&$network) {
@@ -355,11 +355,12 @@ class ProfileSettingNetworking implements ProfileSetting
                 $network['error'] = false;
                 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
                 $s = true;
-                if ($filters[$network['type']] == 'web') {
+                $network['name'] = $filters[$network['type']]['name'];
+                if ($filters[$network['type']]['filter'] == 'web') {
                     $network['address'] = $this->web->value($page, 'address', $network['address'], $s);
-                } elseif ($filters[$network['type']] == 'email') {
+                } elseif ($filters[$network['type']]['filter'] == 'email') {
                     $network['address'] = $this->email->value($page, 'address', $network['address'], $s);
-                } elseif ($filters[$network['type']] == 'number') {
+                } elseif ($filters[$network['type']]['filter'] == 'number') {
                     $network['address'] = $this->number->value($page, 'address', $network['address'], $s);
                 }
                 if (!$s) {
@@ -381,7 +382,7 @@ class ProfileSettingNetworking implements ProfileSetting
         }
         $insert = array();
         foreach ($value as $id=>$network) {
-            XDB::execute("INSERT INTO  profile_networking (pid, nwid, network_type, address, pub)
+            XDB::execute("INSERT INTO  profile_networking (pid, id, nwid, address, pub)
                                VALUES  ({?}, {?}, {?}, {?}, {?})",
                          $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
         }
@@ -398,7 +399,9 @@ class ProfileSettingPromo implements ProfileSetting
         if ($page->profile->mainEducation() == 'X') {
             $gradYearNew += $page->profile->mainEducationDuration();
         }
-        if ($value == $page->profile->entry_year + $page->profile->mainEducationDuration()) {
+        if (($page->profile->mainEducation() != 'X'
+             && $value == $page->profile->entry_year + $page->profile->mainEducationDuration())
+           || ($page->profile->mainEducation() == 'X' && $value == $page->profile->entry_year)) {
             XDB::execute('UPDATE  profile_display
                              SET  promo = {?}
                            WHERE  pid = {?}',
@@ -407,7 +410,7 @@ class ProfileSettingPromo implements ProfileSetting
                              SET  grad_year = {?}
                            WHERE  pid = {?} AND FIND_IN_SET(\'primary\', flags)',
                          $gradYearNew, $page->profile->id());
-            $page->trigSuccess('Ton statut "orange" a été supprimé.');
+            Platal::page()->trigSuccess('Ton statut « orange » a été supprimé.');
         } else {
             require_once 'validations.inc.php';
 
@@ -421,19 +424,24 @@ class ProfileSettingPromo implements ProfileSetting
     {
         $entryYear = $page->profile->entry_year;
         $gradYear  = $page->profile->grad_year;
+        $yearpromo = $page->profile->grad_year;
+        if ($page->profile->mainEducation() == 'X') {
+            $yearpromo -= $page->profile->mainEducationDuration();
+        }
         $success   = true;
-        if (is_null($value) || $value == $page->profile->yearpromo()) {
+        if (is_null($value) || $value == $yearpromo) {
             if ($gradYear != $entryYear + $page->profile->mainEducationDuration()) {
                 $promoChoice = array();
-                for ($i = $entryYear + $page->profile->mainEducationDuration(); $i <= $gradYear; ++$i) {
-                    $promoChoice[] = $page->profile->mainEducation() . strval($i);
+                for ($i = $entryYear; $i <= $gradYear - $page->profile->mainEducationDuration(); ++$i) {
+                    if ($page->profile->mainEducation() == 'X') {
+                        $promoChoice[] = $page->profile->mainEducation() . strval($i);
+                    } else {
+                        $promoChoice[] = $page->profile->mainEducation() . strval($i + $page->profile->mainEducationDuration());
+                    }
                 }
                 Platal::page()->assign('promo_choice', $promoChoice);
             }
-            if ($page->profile->mainEducation() == 'X') {
-                return $page->profile->grad_year - $page->profile->mainEducationDuration();
-            }
-            return $page->profile->yearpromo();
+            return $yearpromo;
         }
 
         // If this profile belongs to an X, $promoNew needs to be changed to
@@ -560,18 +568,28 @@ class ProfileSettingGeneral extends ProfilePage
                          $this->values['photo_pub'], $this->pid());
         }
         if ($this->changed['yourself']) {
-            XDB::execute('UPDATE  accounts
-                             SET  display_name = {?}
-                           WHERE  uid = {?}',
-                         $this->values['yourself'], $this->owner->id());
+            if ($this->owner) {
+                XDB::execute('UPDATE  accounts
+                                 SET  display_name = {?}
+                               WHERE  uid = {?}',
+                             $this->values['yourself'], $this->owner->id());
+            }
+            XDB::execute('UPDATE  profile_display
+                             SET  yourself = {?}
+                           WHERE  pid = {?}', $this->values['yourself'],
+                         $this->pid());
         }
         if ($this->changed['promo_display']) {
-            if ($this->values['promo_display']{0} == $this->profile->mainEducation()
-                && intval(substr($this->values['promo_display'], 1, 4)) >= $this->profile->entry_year + $this->profile->mainEducationDuration()) {
-                XDB::execute('UPDATE  profile_display
-                                 SET  promo = {?}
-                               WHERE  pid = {?}',
-                             $this->values['promo_display'], $this->pid());
+            if ($this->values['promo_display']{0} == $this->profile->mainEducation()) {
+                if (($this->profile->mainEducation() == 'X'
+                     && intval(substr($this->values['promo_display'], 1, 4)) >= $this->profile->entry_year)
+                    || ($this->profile->mainEducation() != 'X'
+                        && intval(substr($this->values['promo_display'], 1, 4)) >= $this->profile->entry_year + $this->profile->mainEducationDuration())) {
+                    XDB::execute('UPDATE  profile_display
+                                     SET  promo = {?}
+                                   WHERE  pid = {?}',
+                                 $this->values['promo_display'], $this->pid());
+                }
             }
         }
     }
@@ -588,7 +606,7 @@ class ProfileSettingGeneral extends ProfilePage
         require_once "emails.combobox.inc.php";
         fill_email_combobox($page, $this->owner, $this->profile);
 
-        $res = XDB::query("SELECT  nw.network_type AS type, nw.name
+        $res = XDB::query("SELECT  nw.nwid AS type, nw.name
                              FROM  profile_networking_enum AS nw
                          ORDER BY  name");
         $page->assign('network_list', $res->fetchAllAssoc());