Merge commit 'origin/fusionax' into account
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Wed, 21 Jan 2009 12:49:40 +0000 (13:49 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Wed, 21 Jan 2009 12:49:40 +0000 (13:49 +0100)
Conflicts:

modules/profile/general.inc.php

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
1  2 
classes/profile.php
include/user.func.inc.php
modules/profile.php
modules/profile/general.inc.php

index 49109fb,0000000..b4843fd
mode 100644,000000..100644
--- /dev/null
@@@ -1,193 -1,0 +1,194 @@@
-                        INNER JOIN  profile_name_search AS pns_f ON (pns_f.pid = p.pid AND pns_f.typeid = ' . self::getNameTypeId('Nom patronymique', true) . ')
-                        INNER JOIN  profile_name_search AS pns_l ON (pns_l.pid = p.pid AND pns_l.typeid = ' . self::getNameTypeId('Prénom', true) . ')
-                         LEFT JOIN  profile_name_search AS pns_uf ON (pns_uf.pid = p.pid AND pns_uf.typeid = ' . self::getNameTypeId('Prénom usuel', true) . ')
-                         LEFT JOIN  profile_name_search AS pns_ul ON (pns_ul.pid = p.pid AND pns_ul.typeid = ' . self::getNameTypeId('Nom usuel', true) . ')
-                         LEFT JOIN  profile_name_search aS pns_n ON (pns_n.pid = p.pid AND pns_n.typeid = ' . self::getNameTypeId('Surnom', true) . ')
 +<?php
 +/***************************************************************************
 + *  Copyright (C) 2003-2008 Polytechnique.org                              *
 + *  http://opensource.polytechnique.org/                                   *
 + *                                                                         *
 + *  This program is free software; you can redistribute it and/or modify   *
 + *  it under the terms of the GNU General Public License as published by   *
 + *  the Free Software Foundation; either version 2 of the License, or      *
 + *  (at your option) any later version.                                    *
 + *                                                                         *
 + *  This program is distributed in the hope that it will be useful,        *
 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 + *  GNU General Public License for more details.                           *
 + *                                                                         *
 + *  You should have received a copy of the GNU General Public License      *
 + *  along with this program; if not, write to the Free Software            *
 + *  Foundation, Inc.,                                                      *
 + *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
 + ***************************************************************************/
 +
 +class Profile
 +{
 +    private $pid;
 +    private $hrpid;
 +    private $data = array();
 +
 +    private function __construct($login)
 +    {
 +        if ($login instanceof PlUser) {
 +            $from  = 'account_profiles AS ap
 +                INNER JOIN profiles AS p ON (p.pid = ap.pid)';
 +            $where = XDB::format('ap.uid = {?} AND FIND_IN_SET(\'owner\', ap.perms)', $login->id());
 +        } else if (is_numeric($login)) {
 +            $from = 'profiles AS p';
 +            $where = XDB::format('p.pid = {?}', $login);
 +        } else {
 +            $from = 'profiles AS p';
 +            $where = XDB::format('p.hrpid = {?}', $login);
 +        }
 +        $res = XDB::query('SELECT  p.*, pe.entry_year, pe.grad_year,
 +                                   pns_f.name AS firstname, pns_l.name AS lastname, pns_n.name AS nickname,
 +                                   IF(pns_uf.name IS NULL, pns_f.name, pns_uf.name) AS firstname_usual,
 +                                   IF(pns_ul.name IS NULL, pns_l.name, pns_ul.name) AS lastname_usual,
 +                                   pd.promo AS promo, pd.short_name, pd.directory_name AS full_name
 +                             FROM  ' . $from . '
 +                       INNER JOIN  profile_display AS pd ON (pd.pid = p.pid)
 +                       INNER JOIN  profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
-                                                    FROM  profile_name_search_enum');
++                       INNER JOIN  profile_name AS pns_f ON (pns_f.pid = p.pid AND pns_f.typeid = ' . self::getNameTypeId('Nom patronymique', true) . ')
++                       INNER JOIN  profile_name AS pns_l ON (pns_l.pid = p.pid AND pns_l.typeid = ' . self::getNameTypeId('Prénom', true) . ')
++                        LEFT JOIN  profile_name AS pns_uf ON (pns_uf.pid = p.pid AND pns_uf.typeid = ' . self::getNameTypeId('Prénom usuel', true) . ')
++                        LEFT JOIN  profile_name AS pns_ul ON (pns_ul.pid = p.pid AND pns_ul.typeid = ' . self::getNameTypeId('Nom usuel', true) . ')
++                        LEFT JOIN  profile_name aS pns_n ON (pns_n.pid = p.pid AND pns_n.typeid = ' . self::getNameTypeId('Surnom', true) . ')
 +                            WHERE  ' . $where);
 +        if ($res->numRows() != 1) {
++            __autoload('PlUser');
 +            throw new UserNotFoundException();
 +        }
 +        $this->data = $res->fetchOneAssoc();
 +        $this->pid = $this->data['pid'];
 +        $this->hrpid = $this->data['hrpid'];
 +    }
 +
 +    public function id()
 +    {
 +        return $this->pid;
 +    }
 +
 +    public function hrid()
 +    {
 +        return $this->hrpid;
 +    }
 +
 +    public function promo()
 +    {
 +        return $this->promo;
 +    }
 +
 +    /** Print a name with the given formatting:
 +     * %s = • for women
 +     * %f = firstname
 +     * %l = lastname
 +     * %F = fullname
 +     * %S = shortname
 +     * %p = promo
 +     */
 +    public function name($format)
 +    {
 +        return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
 +                           array($this->isFemale() ? '•' : '',
 +                                 $this->first_name, $this->last_name,
 +                                 $this->full_name, $this->short_name,
 +                                 $this->promo), $format);
 +    }
 +
 +    public function fullName($with_promo = false)
 +    {
 +        if ($with_promo) {
 +            return $this->full_name . ' (' . $this->promo . ')';
 +        }
 +        return $this->full_name;
 +    }
 +
 +    public function shortName($with_promo = false)
 +    {
 +        if ($with_promo) {
 +            return $this->short_name . ' (' . $this->promo . ')';
 +        }
 +        return $this->short_name;
 +    }
 +
 +    public function firstName()
 +    {
 +        return $this->first_name;
 +    }
 +
 +    public function lastName()
 +    {
 +        return $this->last_name;
 +    }
 +
 +    public function isFemale()
 +    {
 +        return $this->sex == PlUser::GENDER_FEMALE;
 +    }
 +
 +    public function data()
 +    {
 +        $this->first_name;
 +        return $this->data;
 +    }
 +
 +    public function __get($name)
 +    {
 +        if (property_exists($this, $name)) {
 +            return $this->$name;
 +        }
 +
 +        if (isset($this->data[$name])) {
 +            return $this->data[$name];
 +        }
 +
 +        return null;
 +    }
 +
 +    public function __isset($name)
 +    {
 +        return property_exists($this, $name) || isset($this->data[$name]);
 +    }
 +
 +
 +    public function owner()
 +    {
 +        return User::getSilent($this);
 +    }
 +
 +    /** Return the profile associated with the given login.
 +     */
 +    public static function get($login)
 +    {
 +        try {
 +            return new Profile($login);
 +        } catch (UserNotFoundException $e) {
 +            /* Let say we can identify a profile using the identifiers of its owner.
 +             */
 +            if (!($login instanceof PlUser)) {
 +                $user = User::getSilent($login);
 +                if ($user && $user->hasProfile()) {
 +                    return $user->profile();
 +                }
 +            }
 +            return null;
 +        }
 +    }
 +
 +    public static function getNameTypeId($type, $for_sql = false)
 +    {
 +        if (!S::has('name_types')) {
 +            $table = XDB::fetchAllAssoc('name', 'SELECT  id, name
++                                                   FROM  profile_name_enum');
 +            S::set('name_types', $table);
 +        } else {
 +            $table = S::v('name_types');
 +        }
 +        if ($for_sql) {
 +            return XDB::escape($table[$type]);
 +        } else {
 +            return $table[$type];
 +        }
 +    }
 +}
 +
 +// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 +?>
Simple merge
Simple merge
  
  class ProfileSearchNames implements ProfileSetting
  {
-     private $public_name;
-     private $private_name;
-     private $directory_name;
-     private $short_name;
-     private $sort_name;
+     private $private_name_end;
+     private $search_names;
  
 -    private function matchWord($old, $new, $newLen) {
 +    private function matchWord($old, $new, $newLen)
 +    {
          return ($i = strpos($old, $new)) !== false
              && ($i == 0 || $old{$i-1} == ' ')
              && ($i + $newLen == strlen($old) || $old{$i + $newLen} == ' ');
                                               FIND_IN_SET('has_particle', e.flags) AS has_particle,
                                               FIND_IN_SET('always_displayed', e.flags) AS always_displayed,
                                               FIND_IN_SET('public', e.flags) AS pub
-                                        FROM  profile_name_search      AS sn
-                                  INNER JOIN  profile_name_search_enum AS e  ON (e.id = sn.typeid)
+                                        FROM  profile_name      AS sn
+                                  INNER JOIN  profile_name_enum AS e  ON (e.id = sn.typeid)
                                        WHERE  sn.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)
                                     ORDER BY  NOT FIND_IN_SET('always_displayed', e.flags), e.id, sn.name",
 -                                    S::v('uid'));
 +                                     $page->pid());
  
-             $sn_types = XDB::iterator("SELECT  id, name, FIND_IN_SET('has_particle', flags) AS has_particle
-                                          FROM  profile_name_search_enum
+             $sn_types = XDB::iterator("SELECT  id, type, name,
+                                                FIND_IN_SET('has_particle', flags) AS has_particle
+                                          FROM  profile_name_enum
                                          WHERE  NOT FIND_IN_SET('not_displayed', flags)
                                                 AND FIND_IN_SET('always_displayed', flags)
                                       ORDER BY  id");
              while ($sn_type = $sn_types->next()) {
                  if ($sn_type['id'] == $sn['typeid']) {
                      $value[] = $sn;
 -                    $sn = $sn_all->next();
 +                    if ($sn) {
 +                        $sn = $sn_all->next();
 +                    }
                  } else {
-                     $value[] = array('typeid' => $sn_type['id'],
-                                      'type'   => $sn_type['name'],
-                                      'pub'    => 1,
+                     $value[] = array('typeid'    => $sn_type['id'],
+                                      'type'      => $sn_type['type'],
+                                      'type_name' => $sn_type['name'],
+                                      'pub'       => 1,
                                       'has_particle'     => $sn_type['has_particle'],
                                       'always_displayed' => 1);
                  }
              }
-             while ($sn) {
-                 $value[] = $sn;
-                 $sn = $sn_all->next();
+             if ($sn) {
+                 do {
+                     $value[] = $sn;
+                 } while ($sn = $sn_all->next());
              }
          } else {
+             require_once 'name.func.inc.php';
              $res = XDB::query("SELECT  s.particle, s.name
-                                  FROM  profile_name_search      AS s
-                            INNER JOIN  profile_name_search_enum AS e ON (e.id = s.typeid)
-                                 WHERE  s.pid = {?} AND e.name LIKE '%initial'
-                              ORDER BY  e.name = 'Prénom initial'",
+                                  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'",
 -                             S::i('uid'));
 +                             $page->pid());
              $res = $res->fetchAllAssoc();
              $initial = array();
-             $initial['Nom patronymique'] = $res[0]['particle'] . $res[0]['name'];
-             $initial['Prénom'] = $res[1]['name'];
-             $search_names = array();
-             foreach ($value as $key => &$sn) {
+             $initial['lastname'] = $res[0]['particle'] . $res[0]['name'];
+             $initial['firstname'] = $res[1]['name'];
+             $sn_types = build_types();
+             $this->search_names = array();
+             foreach ($value as &$sn) {
                  $sn['name'] = trim($sn['name']);
-                 if ($sn['type'] == 'Prénom' || $sn['type'] == 'Nom patronymique') {
+                 if ($sn['type'] == 'firstname' || $sn['type'] == 'lastname') {
                      $sn['name'] = $this->prepare($page, $sn['type'], $sn['name'],
                                                   $initial[$sn['type']], $success_tmp);
                      $success = $success && $success_tmp;
  
      public function save(ProfilePage &$page, $field, $value)
      {
+         require_once 'name.func.inc.php';
+         $sn_old = build_sn_pub();
          XDB::execute("DELETE FROM  s
-                             USING  profile_name_search      AS s
-                        INNER JOIN  profile_name_search_enum AS e ON (s.typeid = e.id)
+                             USING  profile_name      AS s
+                        INNER JOIN  profile_name_enum AS e ON (s.typeid = e.id)
                              WHERE  s.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)",
 -                     S::i('uid'));
 +                     $page->pid());
-         foreach ($value as $sn) {
-             if ($sn['name'] != '') {
-                 if ($sn['particle']) {
-                     list($particle, $name) = explode(' ', $sn['name'], 2);
-                     $particle = trim($particle) . ' ';
-                     if (!$name) {
-                         list($particle, $name) = explode('\'', $sn['name'], 2);
-                         $particle = trim($particle);
-                     }
-                 } else {
-                     $particle = '';
-                     $name     = $sn['name'];
-                 }
-                 $name = trim($name);
-                 XDB::execute("INSERT INTO  profile_name_search (particle, name, typeid, pid)
-                                    VALUES  ({?}, {?}, {?}, {?})",
-                              $particle, $name, $sn['typeid'], $page->pid());
-             }
+         $has_new = set_alias_names($this->search_names, $sn_old);
+         // Only requires validation if modification in public names
+         if ($has_new) {
+             $new_names = new NamesReq(S::user(), $this->search_names, $this->private_name_end);
+             $new_names->submit();
+             Platal::page()->trigWarning("La demande de modification de tes noms a bien été prises en compte." .
+                                         " Tu recevras un email dès que ces changements auront été effectués.");
+         } else {
+             $display_names = array();
+             build_display_names($display_names, $this->search_names, $this->private_name_end);
+             set_profile_display($display_names);
          }
-         XDB::execute("UPDATE  profile_display
-                          SET  public_name = {?}, private_name = {?},
-                               directory_name = {?}, short_name = {?}, sort_name = {?}
-                        WHERE  pid = {?}",
-                      $this->public_name, $this->private_name, $this->directory_name,
-                      $this->short_name, $this->sort_name, $page->pid());
-         /*require_once('user.func.inc.php');
-         user_reindex(S::v('uid'));*/
      }
  }