Merge branch 'xorg/maint' into xorg/1.0.2/master
[platal.git] / include / name.func.inc.php
index 7b761da..7fd7623 100644 (file)
@@ -126,7 +126,7 @@ function build_private_name(&$search_names, &$sn_types)
                 $i++;
             }
             if (isset($search_names[$sn_types['name_other']])
-                || isset($search_names[$sn_types['name_other']])) {
+                || isset($search_names[$sn_types['firstname_other']])) {
                 $name .= ", ";
             }
         }
@@ -189,7 +189,7 @@ function build_sort_name(&$search_names, &$sn_types)
     return $name;
 }
 
-function set_profile_display(&$display_names, $pid)
+function set_profile_display(&$display_names, Profile $profile)
 {
     XDB::execute("UPDATE  profile_display
                      SET  public_name = {?}, private_name = {?},
@@ -197,7 +197,15 @@ function set_profile_display(&$display_names, $pid)
                    WHERE  pid = {?}",
                  $display_names['public_name'], $display_names['private_name'],
                  $display_names['directory_name'], $display_names['short_name'],
-                 $display_names['sort_name'], $pid);
+                 $display_names['sort_name'], $profile->id());
+
+    $owner = $profile->owner();
+    if ($owner) {
+        XDB::execute('UPDATE  accounts
+                         SET  full_name = {?}, directory_name = {?}
+                       WHERE  uid = {?}',
+                     $display_names['public_name'], $display_names['directory_name'], $owner->id());
+    }
 }
 
 function build_sn_pub($pid)
@@ -220,23 +228,36 @@ function build_sn_pub($pid)
     return $sn_old;
 }
 
+/** Transform a name to its canonical value so it can be compared
+ * to another form (different case, with accents or with - instead
+ * of blanks).
+ * @see compare_basename to compare
+ */
+function name_to_basename($value) {
+    $value = mb_strtoupper(replace_accent($value));
+    return preg_replace('/[^A-Z]/', ' ', $value);
+}
+
+/** Compares two strings and check if they are two forms of the
+ * same name (different case, with accents or with - instead of
+ * blanks).
+ * @see name_to_basename to retreive the compared string
+ */
+function compare_basename($a, $b) {
+    return name_to_basename($a) == name_to_basename($b);
+}
+
 function set_alias_names(&$sn_new, $sn_old, $pid, $uid, $update_new = false, $new_alias = null)
 {
     $has_new = false;
     foreach ($sn_new as $typeid => $sn) {
         if (isset($sn['pub'])) {
-            if (isset($sn_old[$typeid]) && ($sn_old[$typeid]['fullname'] == $sn['fullname'] && $update_new)) {
+            if (isset($sn_old[$typeid]) && $update_new) {
                 XDB::execute("UPDATE  profile_name
                                  SET  particle = {?}, name = {?}, typeid = {?}
                                WHERE  id = {?} AND pid = {?}",
                              $sn['particle'], $sn['name'], $typeid, $sn_old[$typeid]['id'], $pid);
                 unset($sn_old[$typeid]);
-            } elseif ($update_new
-                      || (isset($sn_old[$typeid]) && $sn_old[$typeid]['fullname'] == $sn['fullname'])) {
-                XDB::execute("INSERT INTO  profile_name (particle, name, typeid, pid)
-                                   VALUES  ({?}, {?}, {?}, {?})",
-                             $sn['particle'], $sn['name'], $typeid, $pid);
-                unset($sn_old[$typeid]);
             } else {
                 $has_new = true;
             }
@@ -281,7 +302,7 @@ function set_alias_names(&$sn_new, $sn_old, $pid, $uid, $update_new = false, $ne
                            VALUES  ({?}, 'alias', 'usage', {?})",
                      $new_alias, $uid);
     }
-    Profile::rebuildSearchTokens($pid);
+    Profile::rebuildSearchTokens($pid, false);
     return $has_new;
 }