]> wagnertech.de Git - timetracker.git/blobdiff - dbinstall.php
Wrote a function to update role_id for all active and inactive users.
[timetracker.git] / dbinstall.php
index 3db74cc54aab811ba2b6b5cd5526a28e44a42579..1648a8277f944eb631eaa33453b46df55ee5150f 100644 (file)
@@ -31,6 +31,7 @@ require_once('WEB-INF/lib/common.lib.php');
 require_once('initialize.php');
 import('ttUserHelper');
 import('ttTaskHelper');
+import('ttUser');
 
 // setChange - executes an sql statement. TODO: rename this function to something better.
 // Better yet, redo the entire thing and make an installer.
@@ -722,7 +723,7 @@ if ($_POST) {
     setChange("ALTER TABLE `tt_log` ADD `paid` tinyint(4) NULL default '0' AFTER `billable`");
   }
 
-  if ($_POST["convert11400to11743"]) {
+  if ($_POST["convert11400to11744"]) {
     setChange("ALTER TABLE `tt_teams` DROP `address`");
     setChange("ALTER TABLE `tt_fav_reports` ADD `report_spec` text default NULL AFTER `user_id`");
     setChange("ALTER TABLE `tt_fav_reports` ADD `paid_status` tinyint(4) default NULL AFTER `invoice`");
@@ -760,6 +761,54 @@ if ($_POST) {
     setChange("update `tt_roles` inner join `tt_site_config` sc on (sc.param_name = 'version_db' and sc.param_value = '1.17.40') set rights = replace(rights, 'on_behalf_data_entry', 'track_time,track_expenses')");
     setChange("update `tt_roles` inner join `tt_site_config` sc on (sc.param_name = 'version_db' and sc.param_value = '1.17.40') set rights = replace(rights, 'data_entry', 'track_own_time,track_own_expenses')");
     setChange("UPDATE `tt_site_config` SET `param_value` = '1.17.43' where param_name = 'version_db'");
+    setChange("update `tt_roles` inner join `tt_site_config` sc on (sc.param_name = 'version_db' and sc.param_value = '1.17.43') set rights = replace(rights, 'override_punch_mode,swap_roles', 'override_punch_mode,override_date_lock,swap_roles')");
+    setChange("UPDATE `tt_site_config` SET `param_value` = '1.17.44' where param_name = 'version_db'");
+  }
+
+  // The update_role_id function assigns a role_id to users, who don't have it.
+  if ($_POST['update_role_id']) {
+    import('I18n');
+
+    $mdb2 = getConnection();
+
+    $sql = "select u.id, u.status from tt_users u inner join `tt_site_config` sc on (sc.param_name = 'version_db' and sc.param_value = '1.17.44') where u.role_id is NULL and u.status is NOT NULL";
+    $res = $mdb2->query($sql);
+    if (is_a($res, 'PEAR_Error')) {
+      die($res->getMessage());
+    }
+
+    $users_updated = 0;
+    // Iterate through users.
+    while ($val = $res->fetchRow()) {
+
+      $user_id = $val['id'];
+
+      // Code only works on active users. Temporarily activate a user.
+      $deactivate = false;
+      if ($val['status'] == 0) {
+        $deactivate = true; // To deactivate later.
+        $sql = "update tt_users set status = 1 where id = $user_id";
+        $mdb2->exec($sql);
+      }
+
+      $user = new ttUser(null, $user_id);
+      $i18n = new I18n();
+      $i18n->load($val['lang']);
+      if ($user->login)
+        $user->migrateLegacyRole();
+
+      if ($deactivate) {
+        // Deactivate temporarily activated user back.
+        $sql = "update tt_users set status = 0 where id = $user_id";
+        $mdb2->exec($sql);
+      }
+
+      unset($user);
+      unset($i18n);
+      $users_updated++;
+      // if ($users_updated >= 1000) break; // TODO: uncomment for large user sets to run multiple times.
+    }
+    print "Updated $users_updated users...<br>\n";
   }
 
   if ($_POST["cleanup"]) {
@@ -805,7 +854,7 @@ if ($_POST) {
 <h2>DB Install</h2>
 <table width="80%" border="1" cellpadding="10" cellspacing="0">
   <tr>
-    <td width="80%"><b>Create database structure (v1.17.43)</b>
+    <td width="80%"><b>Create database structure (v1.17.44)</b>
     <br>(applies only to new installations, do not execute when updating)</br></td><td><input type="submit" name="crstructure" value="Create"></td>
   </tr>
 </table>
@@ -841,8 +890,8 @@ if ($_POST) {
     <td><input type="submit" name="convert1600to11400" value="Update"><br></td>
   </tr>
   <tr valign="top">
-    <td>Update database structure (v1.14 to v1.17.43)</td>
-    <td><input type="submit" name="convert11400to11743" value="Update"><br></td>
+    <td>Update database structure (v1.14 to v1.17.44)</td>
+    <td><input type="submit" name="convert11400to11744" value="Update"><br><input type="submit" name="update_role_id" value="Update role_id"></td>
   </tr>
 </table>