Add 'hidden' flag to text descriptions of the 'pub' flag
[platal.git] / classes / phone.php
index 32eb168..c14a7b2 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -55,21 +55,22 @@ class Phone
     const LINK_ADDRESS = 'address';
     const LINK_PROFILE = 'user';
     const LINK_COMPANY = 'hq';
+    const LINK_GROUP   = 'group';
 
     /** The following fields, but $error, all correspond to the fields of the
      * database table profile_phones.
      */
-    private $id = 0;
-    private $pid = 0;
-    private $search = '';
-    private $link_type = 'user';
-    private $link_id = 0;
+    public $id = 0;
+    public $pid = 0;
+    public $search = '';
+    public $link_type = 'user';
+    public $link_id = 0;
     // The following fields are the fields of the form in the profile edition.
-    private $type = 'fixed';
+    public $type = 'fixed';
     public $display = '';
-    private $pub = 'private';
+    public $pub = 'ax';
     public $comment = '';
-    private $error = false;
+    public $error = false;
 
     public function __construct(array $data = array())
     {
@@ -80,29 +81,9 @@ class Phone
         }
     }
 
-    public function linkType()
+    public function setId($id)
     {
-        return $this->link_type;
-    }
-
-    public function linkId()
-    {
-        return $this->link_id;
-    }
-
-    public function id()
-    {
-        return $this->id;
-    }
-
-    public function pid()
-    {
-        return $this->pid;
-    }
-
-    public function search()
-    {
-        return $this->search;
+        $this->id = $id;
     }
 
     /** Returns the unique ID of a phone.
@@ -206,9 +187,10 @@ class Phone
         if ((!isset($format['phoneprf'])) || ($format['phoneprf'] == '')) {
             $res = XDB::query('SELECT  phonePrefix AS phoneprf, phoneFormat AS format
                                  FROM  geoloc_countries
-                                WHERE  phonePrefix = {?} OR phonePrefix = {?} OR phonePrefix = {?}
+                                WHERE  phonePrefix = SUBSTRING({?}, 1, LENGTH(phonePrefix))
+                             ORDER BY  LENGTH(phonePrefix) DESC
                                 LIMIT  1',
-                              substr($tel, 0, 1), substr($tel, 0, 2), substr($tel, 0, 3));
+                              $tel);
             if ($res->numRows() == 0) {
                 // No country found, does not format more than prepending a '+' sign.
                 $this->error = true;
@@ -281,8 +263,10 @@ class Phone
 
     private function toString()
     {
-        return 'type : ' . $this->type .', numéro : ' . $this->display
-            . ', commentaire : « ' . $this->comment . ' », affichage : ' . $this->pub;
+        static $pubs = array('public' => 'publique', 'ax' => 'annuaire papier', 'private' => 'privé', 'hidden' => 'administrateurs');
+        static $types = array('fax' => 'fax', 'fixed' => 'fixe', 'mobile' => 'mobile');
+        return $this->display . ' (' . $types[$this->type] . (($this->comment) ? ', commentaire : « ' . $this->comment . ' »' : '')
+            . ', affichage ' . $pubs[$this->pub] . ')';
     }
 
     private function isEmpty()
@@ -294,22 +278,29 @@ class Phone
     {
         $this->format();
         if (!$this->isEmpty()) {
-            XDB::execute('INSERT INTO  profile_phones (pid, link_type, link_id, tel_id, tel_type,
-                                                       search_tel, display_tel, pub, comment)
-                               VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
+            XDB::execute('INSERT IGNORE INTO  profile_phones (pid, link_type, link_id, tel_id, tel_type,
+                                                              search_tel, display_tel, pub, comment)
+                                      VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
                          $this->pid, $this->link_type, $this->link_id, $this->id, $this->type,
                          $this->search, $this->display, $this->pub, $this->comment);
         }
     }
 
-    static public function deletePhones($pid, $link_type, $link_id = null)
+    public function delete()
+    {
+        XDB::execute('DELETE FROM  profile_phones
+                            WHERE  pid = {?} AND link_type = {?} AND link_id = {?} AND tel_id = {?}',
+                     $this->pid, $this->link_type, $this->link_id, $this->id);
+    }
+
+    static public function deletePhones($pid, $link_type, $link_id = null, $deletePrivate = true)
     {
         $where = '';
         if (!is_null($link_id)) {
             $where = XDB::format(' AND link_id = {?}', $link_id);
         }
         XDB::execute('DELETE FROM  profile_phones
-                            WHERE  pid = {?} AND link_type = {?}' . $where,
+                            WHERE  pid = {?} AND link_type = {?}' . $where . (($deletePrivate) ? '' : ' AND pub IN (\'public\', \'ax\')'),
                      $pid, $link_type);
     }
 
@@ -335,38 +326,60 @@ class Phone
         }
     }
 
-    static private function formArrayWalk(array $data, $function, &$success = true, $requiresEmptyPhone = false)
+    static private function formArrayWalk(array $data, $function, &$success = true, $requiresEmptyPhone = false, $maxPublicity = null)
     {
         $phones = array();
-        foreach ($data as $item) {
-            $phone = new Phone($item);
-            $success = (!$phone->error && ($phone->format() || $phone->isEmpty()) && $success);
-            if (!$phone->isEmpty()) {
-                $phones[] = call_user_func(array($phone, $function));
+        if (!is_null($data)) {
+            foreach ($data as $item) {
+                $phone = new Phone($item);
+                $success = (!$phone->error && ($phone->format() || $phone->isEmpty()) && $success);
+                if (!$phone->isEmpty()) {
+                    // Restrict phone visibility to $maxPublicity
+                    if (!is_null($maxPublicity) && Visibility::isLessRestrictive($maxPublicity, $phone->pub)) {
+                        $phone->pub = $maxPublicity;
+                    }
+                    $phones[] = call_user_func(array($phone, $function));
+                }
             }
         }
         if (count($phones) == 0 && $requiresEmptyPhone) {
             $phone = new Phone();
+            if (!is_null($maxPublicity) && Visibility::isLessRestrictive($maxPublicity, $phone->pub)) {
+                // Restrict phone visibility to $maxPublicity
+                $phone->pub = $maxPublicity;
+            }
             $phones[] = call_user_func(array($phone, $function));
         }
         return $phones;
     }
 
     // Formats an array of form phones into an array of form formatted phones.
-    static public function formatFormArray(array $data, &$success = true)
+    static public function formatFormArray(array $data, &$success = true, $maxPublicity = null)
     {
-        return self::formArrayWalk($data, 'toFormArray', $success, true);
+        $phones = self::formArrayWalk($data, 'toFormArray', $success, true, $maxPublicity);
+        usort($phones, 'Visibility::comparePublicity');
+        return $phones;
     }
 
     static public function formArrayToString(array $data)
     {
-        return implode(' ; ', self::formArrayWalk($data, 'toString'));
+        return implode(', ', self::formArrayWalk($data, 'toString'));
+    }
+
+    static public function hasPrivate(array $phones)
+    {
+        foreach ($phones as $phone) {
+            if ($phone['pub'] == 'private') {
+                return true;
+            }
+        }
+        return false;
     }
 
     static public function iterate(array $pids = array(), array $link_types = array(),
-                                   array $link_ids = array(), array $pubs = array())
+                                   array $link_ids = array(), $visibility = null)
     {
-        return new PhoneIterator($pids, $link_types, $link_ids, $pubs);
+        return new PhoneIterator($pids, $link_types, $link_ids, $visibility);
     }
 }
 
@@ -381,7 +394,7 @@ class PhoneIterator implements PlIterator
 {
     private $dbiter;
 
-    public function __construct(array $pids, array $link_types, array $link_ids, array $pubs)
+    public function __construct(array $pids, array $link_types, array $link_ids, $visibility)
     {
         $where = array();
         if (count($pids) != 0) {
@@ -393,15 +406,18 @@ class PhoneIterator implements PlIterator
         if (count($link_ids) != 0) {
             $where[] = XDB::format('(link_id IN {?})', $link_ids);
         }
-        if (count($pubs) != 0) {
-            $where[] = XDB::format('(pub IN {?})', $pubs);
+        if ($visibility == null || !($visibility instanceof Visibility)) {
+            $visibility = Visibility::defaultForRead();
         }
+        $where[] = 'pve.best_display_level+0 <= pub+0';
+
         $sql = 'SELECT  search_tel AS search, display_tel AS display, comment, link_id,
                         tel_type AS type, link_type, tel_id AS id, pid, pub
                   FROM  profile_phones
+             LEFT JOIN  profile_visibility_enum AS pve ON (pve.access_level = {?})
                  WHERE  ' . implode(' AND ', $where) . '
-              ORDER BY  link_id, tel_id';
-        $this->dbiter = XDB::iterator($sql);
+              ORDER BY  pid, link_id, tel_id';
+        $this->dbiter = XDB::iterator($sql, $visibility->level());
     }
 
     public function next()
@@ -437,5 +453,5 @@ class PhoneIterator implements PlIterator
     }
 }
 
-// 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:
 ?>