Stores all account modification for active profiles in order to let the secretaries...
authorStéphane Jacob <sj@m4x.org>
Thu, 2 Dec 2010 13:39:18 +0000 (14:39 +0100)
committerStéphane Jacob <sj@m4x.org>
Fri, 3 Dec 2010 15:04:10 +0000 (16:04 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
bin/cron/profile_modification.php
modules/profile/page.inc.php
upgrade/1.0.2/02_modifications.sql [new file with mode: 0644]

index 8d4b4d0..252e392 100755 (executable)
@@ -31,7 +31,8 @@ $res = XDB::iterator('SELECT  p.hrpid, pm.pid, a.full_name, pm.field, pm.oldText
                   INNER JOIN  profile_display       AS pd ON (pm.pid = pd.pid)
                   INNER JOIN  account_profiles      AS ap ON (pm.pid = ap.pid AND FIND_IN_SET(\'owner\', ap.perms))
                   INNER JOIN  aliases               AS al ON (ap.uid = al.uid AND FIND_IN_SET(\'bestalias\', al.flags))
-                    ORDER BY  pm.pid, pm.field');
+                       WHERE  pm.type = \'third_party\'
+                    ORDER BY  pm.pid, pm.field, pm.timestamp');
 
 if ($res->total() > 0) {
     $date = time();
@@ -83,7 +84,8 @@ if ($res->total() > 0) {
     $mailer->assign('date', $date);
     $mailer->send();
 
-    XDB::execute('DELETE FROM  profile_modifications');
+    XDB::execute('DELETE FROM  profile_modifications
+                        WHERE  type = \'third_party\'');
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
index 651bb38..53ffa0e 100644 (file)
@@ -270,13 +270,13 @@ abstract class ProfilePage implements PlWizardPage
             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])),
+                        preg_replace('/(\r\n|\n|\r)/', ' - ', $setting->getText($this->orig[$field])),
+                        preg_replace('/(\r\n|\n|\r)/', ' - ', $setting->getText($this->values[$field])),
                     );
                 } else {
                     $changedFields[$field] = array(
-                        str_replace("\n", " - ", $this->orig[$field]),
-                        str_replace("\n", " - ", $this->values[$field]),
+                        preg_replace('/(\r\n|\n|\r)/', ' - ', $this->orig[$field]),
+                        preg_replace('/(\r\n|\n|\r)/', ' - ', $this->values[$field]),
                     );
                 }
                 if (!is_null($setting)) {
@@ -296,18 +296,20 @@ abstract class ProfilePage implements PlWizardPage
         global $platal;
         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.
+        /** Stores all profile modifications for active users in order to:
+         *  -daily notify the user in case of third party edition,
+         *  -display the modification to the secretaries for verification in
+         *  case of an edition made by the user.
          */
         $owner = $this->profile->owner();
         $user = S::user();
-        if ($owner->isActive() && $owner->id() != $user->id()) {
+        if ($owner->isActive()) {
             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]);
+                XDB::execute('INSERT INTO  profile_modifications (pid, uid, field, oldText, newText, type, timestamp)
+                                   VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, NOW())
+                  ON DUPLICATE KEY UPDATE  uid = VALUES(uid), oldText = IF(VALUES(type) != type, VALUES(oldText), oldText),
+                                           newText = VALUES(newText), type = VALUES(type), timestamp = NOW()',
+                             $this->pid(), $user->id(), $field, $values[0], $values[1], ($owner->id() == $user->id()) ? 'self' : 'third_party');
             }
         }
         return true;
diff --git a/upgrade/1.0.2/02_modifications.sql b/upgrade/1.0.2/02_modifications.sql
new file mode 100644 (file)
index 0000000..f4965a7
--- /dev/null
@@ -0,0 +1,23 @@
+DROP TABLE IF EXISTS tmp_profile_modifications;
+CREATE TEMPORARY TABLE tmp_profile_modifications LIKE profile_modifications;
+INSERT INTO tmp_profile_modifications SELECT * FROM profile_modifications;
+DROP TABLE profile_modifications;
+CREATE TABLE profile_modifications (
+  pid INT(11) UNSIGNED NOT NULL DEFAULT 0,
+  uid INT(11) UNSIGNED NOT NULL DEFAULT 0,
+  field VARCHAR(60) NOT NULL,
+  oldText TEXT NOT NULL,
+  newText TEXT NOT NULL,
+  type ENUM('self', 'third_party') NOT NULL DEFAULT 'self',
+  timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  PRIMARY KEY  (pid, field),
+  KEY uid (uid),
+  FOREIGN KEY (uid) REFERENCES accounts (uid) ON DELETE CASCADE ON UPDATE CASCADE,
+  FOREIGN KEY (pid) REFERENCES profiles (pid) ON DELETE CASCADE ON UPDATE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+INSERT INTO  profile_modifications (pid, uid, field, oldText, newText, type)
+     SELECT  pid, uid, field, oldText, newText, 'third_party'
+       FROM  tmp_profile_modifications;
+DROP TABLE IF EXISTS tmp_profile_modifications;
+
+-- vim:set syntax=mysql: