merge with master
[platal.git] / modules / profile / page.inc.php
index 433947e..b380c3e 100644 (file)
@@ -76,6 +76,23 @@ class ProfileEmail extends ProfileNoSave
     }
 }
 
+class ProfileNumber extends ProfileNoSave
+{
+    public function value(ProfilePage &$page, $field, $value, &$success)
+    {
+        if (is_null($value)) {
+            return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
+        }
+        $value = trim($value);
+        $success = empty($value) || is_numeric($value);
+        if (!$success) {
+            global $page;
+            $page->trigError('Numéro invalide');
+        }
+        return $value;
+    }
+}
+
 
 class ProfileTel extends ProfileNoSave
 {
@@ -84,12 +101,95 @@ class ProfileTel extends ProfileNoSave
         if (is_null($value)) {
             return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
         }
-        $success = !preg_match('/[<>{}@&#~\/:;?,!§*_`\[\]|%$^=]/', $value, $matches);
+        require_once('profil.func.inc.php');
+        $value = format_phone_number($value);
+        if($value == '') {
+            $success = true;
+            return $value;
+        }
+        $value = format_display_number($value,$error);
+        $success = !$error;
         if (!$success) {
-            Platal::page()->trigError('Le numéro de téléphone contient un caractère interdit : ' . pl_entities($matches[0][0]));
+            global $page;
+            $page->trigError('Le préfixe international du numéro de téléphone est inconnu. ');
+        }
+        return $value;
+    }
+}
+
+class ProfilePhones implements ProfileSetting
+{
+    private $tel;
+    private $pub;
+    protected $link_type;
+    protected $link_id;
+
+    public function __construct($type, $id)
+    {
+        $this->tel = new ProfileTel();
+        $this->pub = new ProfilePub();
+        $this->link_type = $type;
+        $this->link_id = $id;
+    }
+
+    public function value(ProfilePage &$page, $field, $value, &$success)
+    {
+        $success = true;
+        if (is_null($value)) {
+            $value = isset($page->values[$field]) ? $page->values[$field] : array();
+        }
+        if (!is_array($value)) {
+            $value = array();
+        }
+        foreach ($value as $key=>&$phone) {
+            if (@$phone['removed']) {
+                unset($value[$key]);
+            } else {
+                $phone['pub'] = $this->pub->value($page, 'pub', $phone['pub'], $s);
+                $phone['tel'] = $this->tel->value($page, 'tel', $phone['tel'], $s);
+                if(!isset($phone['type']) || ($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
+                    $phone['type'] = 'fixed';
+                    $s = false;
+                }
+                if (!$s) {
+                    $phone['error'] = true;
+                    $success = false;
+                }
+                if (!isset($phone['comment'])) {
+                    $phone['comment'] = '';
+                }
+            }
         }
         return $value;
     }
+
+    private function saveTel($telid, array &$phone)
+    {
+        if ($phone['tel'] != '') {
+            XDB::execute("INSERT INTO  profile_phones (uid, link_type, link_id, tel_id, tel_type,
+                                       search_tel, display_tel, pub, comment)
+                               VALUES  ({?}, {?}, {?}, {?}, {?},
+                                       {?}, {?}, {?}, {?})",
+                         S::i('uid'), $this->link_type, $this->link_id, $telid, $phone['type'],
+                        format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
+        }
+    }
+
+    public function save(ProfilePage &$page, $field, $value)
+    {
+        XDB::execute("DELETE FROM  profile_phones
+                            WHERE  uid = {?} AND link_type = {?} AND link_id = {?}",
+                            S::i('uid'), $this->link_type, $this->link_id);
+        $this->saveTels($field, $value);
+    }
+
+    //Only saves phones without a delete operation
+    public function saveTels($field, $value)
+    {
+        foreach ($value as $telid=>&$phone) {
+            $this->saveTel($telid, $phone);
+        }
+    }
 }
 
 class ProfilePub extends ProfileNoSave