Rewrite Profile::getNameTypeId using DirEnum.
[platal.git] / classes / profile.php
index f46eb15..cdd639d 100644 (file)
@@ -130,9 +130,9 @@ class Profile
     {
         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);
+                                 $this->firstName(), $this->lastName(),
+                                 $this->fullName(), $this->shortName(),
+                                 $this->promo()), $format);
     }
 
     public function fullName($with_promo = false)
@@ -195,6 +195,21 @@ class Profile
         return array_unique($vals);
     }
 
+    public function nationalities()
+    {
+        $nats = array();
+        if ($this->nationality1) {
+            $nats[] = $this->nationality1;
+        }
+        if ($this->nationality2) {
+            $nats[] = $this->nationality2;
+        }
+        if ($this->nationality3) {
+            $nats[] = $this->nationality3;
+        }
+        return $nats;
+    }
+
     public function __get($name)
     {
         if (property_exists($this, $name)) {
@@ -213,6 +228,10 @@ class Profile
         return property_exists($this, $name) || isset($this->data[$name]);
     }
 
+    /** Sets the level of visibility of the profile
+     * Sets $this->visibility to a list of valid visibilities.
+     * @param one of the self::VISIBILITY_* values
+     */
     public function setVisibilityLevel($visibility)
     {
         if ($visibility != self::VISIBILITY_PRIVATE
@@ -226,6 +245,14 @@ class Profile
         }
     }
 
+    /** Determine whether an item with visibility $visibility can be displayed
+     * with the current level of visibility of the profile
+     * @param $visibility The level of visibility to be checked
+     */
+    public function isVisible($visibility)
+    {
+        return in_array($visibility, $this->visibility);
+    }
 
     /* Photo
      */
@@ -233,7 +260,7 @@ class Profile
     {
         $cond = '';
         if ($this->visibility) {
-            $cond = ' AND pub IN ' . XDB::formatArray($this->visibility);
+            $cond = XDB::format(' AND pub IN {?}', $this->visibility);
         }
         $res = XDB::query("SELECT  *
                              FROM  profile_photos
@@ -263,7 +290,7 @@ class Profile
             $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)';
         }
         if ($this->visibility) {
-            $where .= ' AND pa.pub IN ' . XDB::formatArray($this->visibility);
+            $where .= XDB::format(' AND pa.pub IN {?}', $this->visibility);
         }
         $type = array();
         if ($flags & self::ADDRESS_PRO) {
@@ -273,7 +300,7 @@ class Profile
             $type[] = 'home';
         }
         if (count($type) > 0) {
-            $where .= ' AND pa.type IN ' . XDB::formatArray($type);
+            $where .= XDB::format(' AND pa.type IN {?}', $type);
         }
         $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit);
         return XDB::iterator('SELECT  pa.text, pa.postalCode, pa.type, pa.latitude, pa.longitude,
@@ -352,7 +379,7 @@ class Profile
             $where .= ' AND pn.network_type = 0'; // XXX hardcoded reference to web site index
         }
         if ($this->visibility) {
-            $where .= ' AND pn.pub IN ' . XDB::formatArray($this->visibility);
+            $where .= XDB::format(' AND pn.pub IN {?}', $this->visibility);
         }
         $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit);
         return XDB::iterator('SELECT  pne.name, pne.icon,
@@ -384,8 +411,8 @@ class Profile
         $where = XDB::format('pj.pid = {?}', $this->id());
         $cond  = 'TRUE';
         if ($this->visibility) {
-            $where .= ' AND pj.pub IN ' . XDB::formatArray($this->visibility);
-            $cond  =  'pj.email_pub IN ' . XDB::formatArray($this->visibility);
+            $where .= XDB::format(' AND pj.pub IN {?}', $this->visibility);
+            $cond  = XDB::format('pj.email_pub IN {?}', $this->visibility);
         }
         $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit);
         return XDB::iterator('SELECT  pje.name, pje.acronym, pje.url, pje.email, pje.NAF_code,
@@ -458,6 +485,7 @@ class Profile
             $order = '';
         }
 
+        require_once 'directory.enums.inc.php';
         return XDB::Iterator('SELECT  p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year,
                                       pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
                                       IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
@@ -470,22 +498,22 @@ class Profile
                                 FROM  profiles AS p
                           INNER JOIN  profile_display AS pd ON (pd.pid = p.pid)
                           INNER JOIN  profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
-                          INNER JOIN  profile_name AS pn_f ON (pn_f.pid = p.pid
-                                                               AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ')
-                          INNER JOIN  profile_name AS pn_l ON (pn_l.pid = p.pid
-                                                               AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ')
-                           LEFT JOIN  profile_name AS pn_uf ON (pn_uf.pid = p.pid
-                                                                AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ')
-                           LEFT JOIN  profile_name AS pn_ul ON (pn_ul.pid = p.pid
-                                                                AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ')
-                           LEFT JOIN  profile_name AS pn_n ON (pn_n.pid = p.pid
-                                                               AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ')
+                          INNER JOIN  profile_name AS pn_f ON (pn_f.pid = p.pid AND pn_f.typeid = {?})
+                          INNER JOIN  profile_name AS pn_l ON (pn_l.pid = p.pid AND pn_l.typeid = {?})
+                           LEFT JOIN  profile_name AS pn_uf ON (pn_uf.pid = p.pid AND pn_uf.typeid = {?})
+                           LEFT JOIN  profile_name AS pn_ul ON (pn_ul.pid = p.pid AND pn_ul.typeid = {?})
+                           LEFT JOIN  profile_name AS pn_n ON (pn_n.pid = p.pid AND pn_n.typeid = {?})
                            LEFT JOIN  profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
                            LEFT JOIN  profile_photos AS ph ON (ph.pid = p.pid)
                            LEFT JOIN  account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
-                               WHERE  p.pid IN ' . XDB::formatArray($pids) . '
+                               WHERE  p.pid IN {?}
                             GROUP BY  p.pid
-                                   ' . $order);
+                                   ' . $order,
+                             DirEnum::getID(DirEnum::NAMETYPES, 'firstname'),
+                             DirEnum::getID(DirEnum::NAMETYPES, 'lastname'),
+                             DirEnum::getID(DirEnum::NAMETYPES, 'firstname_ordinary'),
+                             DirEnum::getID(DirEnum::NAMETYPES, 'lastname_ordinary'),
+                             DirEnum::getID(DirEnum::NAMETYPES, 'nickname'), $pids);
     }
 
     public static function getPID($login)
@@ -516,8 +544,8 @@ class Profile
         return XDB::fetchAllAssoc('uid', 'SELECT  ap.uid, ap.pid
                                             FROM  account_profiles AS ap
                                            WHERE  FIND_IN_SET(\'owner\', ap.perms)
-                                                  AND ap.uid IN ' . XDB::formatArray($uids) .'
-                                               ' . $order);
+                                                  AND ap.uid IN {?}
+                                               ' . $order, $uids);
     }
 
     /** Return the profile associated with the given login.
@@ -587,26 +615,10 @@ class Profile
             || $name == self::DN_SHORT || $name == self::DN_SORT;
     }
 
-    public static function getNameTypeId($type, $for_sql = false)
-    {
-        if (!S::has('name_types')) {
-            $table = XDB::fetchAllAssoc('type', 'SELECT  id, type
-                                                   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];
-        }
-    }
-
     public static function rebuildSearchTokens($pid)
     {
         XDB::execute('DELETE FROM  search_name
-                            WHERE  uid = {?}',
+                            WHERE  pid = {?}',
                      $pid);
         $keys = XDB::iterator("SELECT  CONCAT(n.particle, n.name) AS name, e.score,
                                        FIND_IN_SET('public', e.flags) AS public
@@ -625,9 +637,9 @@ class Profile
             while ($toks) {
                 $token = strtolower(replace_accent(array_pop($toks) . $token));
                 $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10);
-                XDB::execute('REPLACE INTO  search_name (token, uid, soundex, score, flags)
+                XDB::execute('REPLACE INTO  search_name (token, pid, soundex, score, flags)
                                     VALUES  ({?}, {?}, {?}, {?}, {?})',
-                             $token, $uid, soundex_fr($token), $score, $key['public']);
+                             $token, $pid, soundex_fr($token), $score, $key['public']);
                 $first = 0;
             }
         }