REPLACE INTO should only be used if the data deletion is wanted.
[platal.git] / modules / profile / page.inc.php
index b754171..ec70a0e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2009 Polytechnique.org                              *
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -36,14 +36,22 @@ interface ProfileSetting
     /** Save the new value for the given field.
      */
     public function save(ProfilePage &$page, $field, $new_value);
+
+    /** Get text from the value.
+     */
+    public function getText($value);
 }
 
 abstract class ProfileNoSave implements ProfileSetting
 {
     public function save(ProfilePage &$page, $field, $new_value) { }
+
+    public function getText($value) {
+        return $value;
+    }
 }
 
-class ProfileWeb extends ProfileNoSave
+class ProfileSettingWeb extends ProfileNoSave
 {
     public function value(ProfilePage &$page, $field, $value, &$success)
     {
@@ -60,7 +68,7 @@ class ProfileWeb extends ProfileNoSave
     }
 }
 
-class ProfileEmail extends ProfileNoSave
+class ProfileSettingEmail extends ProfileNoSave
 {
     public function value(ProfilePage &$page, $field, $value, &$success)
     {
@@ -76,7 +84,7 @@ class ProfileEmail extends ProfileNoSave
     }
 }
 
-class ProfileNumber extends ProfileNoSave
+class ProfileSettingNumber extends ProfileNoSave
 {
     public function value(ProfilePage &$page, $field, $value, &$success)
     {
@@ -92,126 +100,46 @@ class ProfileNumber extends ProfileNoSave
     }
 }
 
-
-class ProfileTel extends ProfileNoSave
+class ProfileSettingPhones implements ProfileSetting
 {
     public function value(ProfilePage &$page, $field, $value, &$success)
     {
-        if (is_null($value)) {
-            return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
-        }
-        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 préfixe international du numéro de téléphone est inconnu. ');
-        }
-        return $value;
-    }
-}
-
-class ProfilePhones implements ProfileSetting
-{
-    private $tel;
-    private $pub;
-    protected $id;
-    protected $link_type;
-    protected $link_id;
-
-    public function __construct($type, $link_id, $id = 0)
-    {
-        if ($id != 0) {
-            $this->id = $id;
-        } else {
-            $this->id = S::i('uid');
-        }
-        $this->tel = new ProfileTel();
-        $this->pub = new ProfilePub();
-        $this->link_type = $type;
-        $this->link_id   = $link_id;
-    }
-
-    public function value(ProfilePage &$page, $field, $value, &$success)
-    {
         $success = true;
+        $phones = array();
+
         if (is_null($value)) {
-            $value = array();
-            $res = XDB::iterator("SELECT  t.display_tel AS tel, t.tel_type AS type, t.pub, t.comment
-                                    FROM  profile_phones AS t
-                                   WHERE  t.uid = {?} AND t.link_type = {?}
-                                ORDER BY  t.tel_id",
-                                 $this->id, $this->link_type);
-            if ($res->numRows() > 0) {
-                $value = $res->fetchAllAssoc();
-            } else {
-                $value = array(
-                        0 => array(
-                            'type'    => 'fixed',
-                            'tel'     => '',
-                            'pub'     => 'private',
-                            'comment' => '',
-                            )
-                        );
+            $it = Phone::iterate(array($page->pid()), array(Phone::LINK_PROFILE), array(0));
+            while ($phone = $it->next()) {
+                $success = ($phone->format() && $success);
+                $phones[] = $phone->toFormArray();
             }
-        }
-        foreach ($value as $key=>&$phone) {
-            if (isset($phone['removed']) && $phone['removed']) {
-                unset($value[$key]);
-            } else {
-                unset($phone['removed']);
-                $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'] = '';
-                }
+            if (count($phones) == 0) {
+                $phone = new Phone();
+                $phones[] = $phone->toFormArray();
             }
-        }
-        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  ({?}, {?}, {?}, {?}, {?},
-                                       {?}, {?}, {?}, {?})",
-                         $this->id, $this->link_type, $this->link_id, $telid, $phone['type'],
-                         format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
+            return $phones;
+        } else {
+            $phones = Phone::formatFormArray($value, $success);
+            if (!$success) {
+                Platal::page()->trigError('Numéro de téléphone invalide');
+            }
+            return $phones;
         }
     }
 
     public function save(ProfilePage &$page, $field, $value)
     {
-        XDB::execute("DELETE FROM  profile_phones
-                            WHERE  uid = {?} AND link_type = {?} AND link_id = {?}",
-                            $this->id, $this->link_type, $this->link_id);
-        $this->saveTels($field, $value);
+        Phone::deletePhones($page->pid(), Phone::LINK_PROFILE);
+        Phone::savePhones($value, $page->pid(), Phone::LINK_PROFILE);
     }
 
-    //Only saves phones without a delete operation
-    public function saveTels($field, $value)
+    public function getText($value)
     {
-        foreach ($value as $telid=>&$phone) {
-            $this->saveTel($telid, $phone);
-        }
+        return Phone::formArrayToString($value);
     }
 }
 
-class ProfilePub extends ProfileNoSave
+class ProfileSettingPub extends ProfileNoSave
 {
     public function value(ProfilePage &$page, $field, $value, &$success)
     {
@@ -226,9 +154,13 @@ class ProfilePub extends ProfileNoSave
         }
         return $value;
     }
+
+    public function getText($value) {
+        return $value;
+    }
 }
 
-class ProfileBool extends ProfileNoSave
+class ProfileSettingBool extends ProfileNoSave
 {
     public function value(ProfilePage &$page, $field, $value, &$success)
     {
@@ -240,14 +172,25 @@ class ProfileBool extends ProfileNoSave
     }
 }
 
-class ProfileDate extends ProfileNoSave
+class ProfileSettingDate extends ProfileNoSave
 {
+    private $allowEmpty;
+
+    public function __construct($allowEmpty = false)
+    {
+        $this->allowEmpty = $allowEmpty;
+    }
+
     public function value(ProfilePage &$page, $field, $value, &$success)
     {
         $success = true;
         if (is_null($value)) {
             $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
         } else {
+            $value = trim($value);
+            if (empty($value) && $this->allowEmpty) {
+                return null;
+            }
             $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
             if (!$success) {
                 Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
@@ -263,33 +206,13 @@ class ProfileDate extends ProfileNoSave
         }
         return $value;
     }
-}
 
-abstract class ProfileGeocoding implements ProfileSetting
-{
-    protected function geocodeAddress(array &$address, &$success)
+    public static function toSQLDate($value)
     {
-        require_once 'geocoding.inc.php';
-        $success = true;
-        if (isset($address['changed']) && $address['changed'] == 1) {
-            $gmapsGeocoder = new GMapsGeocoder();
-            $address = $gmapsGeocoder->getGeocodedAddress($address);
-            if (isset($address['geoloc'])) {
-                $success = false;
-            }
-        }
-        if (isset($address['geoloc_choice']) && ($address['geoloc_choice'] == 0)) {
-            $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
-            $mailer->assign('text', $address['text']);
-            $mailer->assign('geoloc', $address['geoloc']);
-            $mailer->send();
-            $gmapsGeocoder = new GMapsGeocoder();
-            $address = $gmapsGeocoder->stripGeocodingFromAddress($address);
-        }
+        return preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $value);
     }
 }
 
-
 abstract class ProfilePage implements PlWizardPage
 {
     protected $wizard;
@@ -301,10 +224,14 @@ abstract class ProfilePage implements PlWizardPage
 
     public $orig     = array();
     public $values   = array();
+    public $profile  = null;
+    public $owner    = null;
 
     public function __construct(PlWizard &$wiz)
     {
         $this->wizard =& $wiz;
+        $this->profile = $this->wizard->getUserData('profile');
+        $this->owner   = $this->wizard->getUserData('owner');
     }
 
     protected function _fetchData()
@@ -335,27 +262,55 @@ abstract class ProfilePage implements PlWizardPage
     {
     }
 
-    protected function saveData()
+    public function saveData()
     {
         require_once 'notifs.inc.php';
+        $changedFields = array();
         foreach ($this->settings as $field=>&$setting) {
-            if (!is_null($setting) && $this->changed[$field]) {
-                $setting->save($this, $field, $this->values[$field]);
-            }
-            if ($this->changed[$field] && @$this->watched[$field]) {
-                register_profile_update(S::i('uid'), $field);
+            if ($this->changed[$field]) {
+                if (!is_null($setting)) {
+                    $changedFields[$field] = array(
+                        str_replace("\n", " - ", $setting->getText($this->orig[$field])),
+                        str_replace("\n", " - ", $setting->getText($this->values[$field])),
+                    );
+                } else {
+                    $changedFields[$field] = array(
+                        str_replace("\n", " - ", $this->orig[$field]),
+                        str_replace("\n", " - ", $this->values[$field]),
+                    );
+                }
+                if (!is_null($setting)) {
+                    $setting->save($this, $field, $this->values[$field]);
+                }
+                if (isset($this->watched[$field]) && $this->watched[$field]) {
+                    WatchProfileUpdate::register($this->profile, $field);
+                }
             }
         }
         $this->_saveData();
 
         // Update the last modification date
-        XDB::execute('REPLACE INTO  user_changes
-                               SET  user_id = {?}', S::v('uid'));
-        if (!S::has('suid')) {
-            register_watch_op(S::i('uid'), WATCH_FICHE);
-        }
+        XDB::execute('UPDATE  profiles
+                         SET  last_change = NOW()
+                       WHERE  pid = {?}', $this->pid());
         global $platal;
-        S::logger()->log('profil', $platal->pl_self(1));
+        S::logger()->log('profil', $platal->pl_self(2));
+
+        /** If the update was made by a third party and the profile corresponds
+         * to a registered user, stores both former and new text.
+         * This will be daily sent to the user.
+         */
+        $owner = $this->profile->owner();
+        $user = S::user();
+        if ($owner->isActive() && $owner->id() != $user->id()) {
+            foreach ($changedFields as $field => $values) {
+                XDB::execute('INSERT INTO  profile_modifications (pid, uid, field, oldText, newText)
+                                   VALUES  ({?}, {?}, {?}, {?}, {?})
+                  ON DUPLICATE KEY UPDATE  oldText = VALUES(oldText), newText = VALUES(newText)',
+                             $this->pid(), $user->id(), $field, $values[0], $values[1]);
+            }
+        }
+        return true;
     }
 
     protected function checkChanges()
@@ -385,6 +340,16 @@ abstract class ProfilePage implements PlWizardPage
         return 'profile/base.tpl';
     }
 
+    public function pid()
+    {
+        return $this->profile->id();
+    }
+
+    public function hrpid()
+    {
+        return $this->profile->hrpid();
+    }
+
     protected function _prepare(PlPage &$page, $id)
     {
     }
@@ -398,11 +363,13 @@ abstract class ProfilePage implements PlWizardPage
             $page->assign($field, $value);
         }
         $this->_prepare($page, $id);
+        $page->assign('profile', $this->profile);
+        $page->assign('owner', $this->owner);
         $page->assign('profile_page', $this->pg_template);
         $page->assign('errors', $this->errors);
     }
 
-    public function process()
+    public function process(&$global_success)
     {
         $global_success = true;
         $this->fetchData();
@@ -419,15 +386,42 @@ abstract class ProfilePage implements PlWizardPage
         }
         if ($global_success) {
             if ($this->checkChanges()) {
-                $this->saveData();
+                /* Save changes atomically to avoid inconsistent state
+                 * in case of error.
+                 */
+                if (!XDB::runTransaction(array($this, 'saveData'))) {
+                    $global_success = false;
+                    return PlWizard::CURRENT_PAGE;
+                }
                 $this->markChange();
             }
+            // XXX: removes this code once all merge related issues have been fixed.
+            static $issues = array(0 => array('name', 'promo', 'phone', 'education'), 1 => array('address'), 2 => array('job'));
+            if (isset($issues[Post::i('valid_page')])) {
+                foreach ($issues[Post::i('valid_page')] as $issue) {
+                    XDB::execute("UPDATE  profile_merge_issues
+                                     SET  issues = REPLACE(issues, {?}, '')
+                                   WHERE  pid = {?}",
+                                 $issue, $this->pid());
+                }
+            }
             return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
         }
-        Platal::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
-                                . "de ton profil et de revalider ta demande");
+        $text = "Certains champs n'ont pas pu être validés, merci de corriger les informations "
+              . (S::user()->isMe($this->owner) ? "de ton profil et de revalider ta demande."
+                                               : "du profil et de revalider ta demande.");
+        Platal::page()->trigError($text);
         return PlWizard::CURRENT_PAGE;
     }
+
+    public function success()
+    {
+        if (S::user()->isMe($this->owner)) {
+            return 'Ton profil a bien été mis à jour.';
+        } else {
+            return 'Le profil a bien été mis à jour.';
+        }
+    }
 }
 
 require_once dirname(__FILE__) . '/general.inc.php';