CRM-17609 - Refactor synchronize contacts function into appropriate cms classes
authorColeman Watts <coleman@civicrm.org>
Sun, 22 Nov 2015 00:17:01 +0000 (19:17 -0500)
committerColeman Watts <coleman@civicrm.org>
Sun, 22 Nov 2015 00:20:19 +0000 (19:20 -0500)
CRM/Admin/Form/CMSUser.php
CRM/Core/BAO/CMSUser.php
CRM/Utils/System/Base.php
CRM/Utils/System/Drupal.php
CRM/Utils/System/Drupal6.php
CRM/Utils/System/Joomla.php
CRM/Utils/System/WordPress.php

index b2251817ad5283669c4a26310256dea4c27d6111..e01b76d8d91f3fb5bb843b04bd13c65d606f9801 100644 (file)
@@ -59,7 +59,7 @@ class CRM_Admin_Form_CMSUser extends CRM_Core_Form {
    * Process the form submission.
    */
   public function postProcess() {
-    $result = CRM_Core_BAO_CMSUser::synchronize();
+    $result = CRM_Core_Config::singleton()->userSystem->synchronizeUsers();
 
     $status = ts('Checked one user record.',
         array(
index 6db4cad210237e46ab9bf8b1c24b9fd68102c490..dd45a5a82c4df2a83b0ae1adcc6b78fda729e4b8 100644 (file)
  */
 class CRM_Core_BAO_CMSUser {
 
-  /**
-   * Synchronize CMS users with CiviCRM contacts.
-   *
-   * @return array
-   */
-  public static function synchronize() {
-    $config = CRM_Core_Config::singleton();
-
-    // Build an array of rows from UF users table.
-    $rows = array();
-    if ($config->userSystem->is_drupal == '1') {
-      $id = 'uid';
-      $mail = 'mail';
-      $name = 'name';
-
-      $result = db_query("SELECT uid, mail, name FROM {users} where mail != ''");
-
-      if ($config->userFramework == 'Drupal') {
-        while ($row = $result->fetchAssoc()) {
-          $rows[] = $row;
-        }
-      }
-      elseif ($config->userFramework == 'Drupal6') {
-        while ($row = db_fetch_array($result)) {
-          $rows[] = $row;
-        }
-      }
-    }
-    elseif ($config->userFramework == 'Joomla') {
-      $id = 'id';
-      $mail = 'email';
-      $name = 'name';
-      // TODO: Insert code here to populate $rows for Joomla;
-    }
-    elseif ($config->userFramework == 'WordPress') {
-      $id = 'ID';
-      $mail = 'user_email';
-    }
-    else {
-      CRM_Core_Error::fatal('CMS user creation not supported for this framework');
-    }
-
-    if (PHP_SAPI != 'cli') {
-      set_time_limit(300);
-    }
-
-    if ($config->userSystem->is_drupal == '1') {
-      $user = new StdClass();
-      $uf = $config->userFramework;
-      $contactCount = 0;
-      $contactCreated = 0;
-      $contactMatching = 0;
-      foreach ($rows as $row) {
-        $user->$id = $row[$id];
-        $user->$mail = $row[$mail];
-        $user->$name = $row[$name];
-        $contactCount++;
-        if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row[$id], $row[$mail], $uf, 1, 'Individual', TRUE)) {
-          $contactCreated++;
-        }
-        else {
-          $contactMatching++;
-        }
-        if (is_object($match)) {
-          $match->free();
-        }
-      }
-    }
-    elseif ($config->userFramework == 'Joomla') {
-
-      $JUserTable = &JTable::getInstance('User', 'JTable');
-
-      $db = $JUserTable->getDbo();
-      $query = $db->getQuery(TRUE);
-      $query->select($id . ', ' . $mail . ', ' . $name);
-      $query->from($JUserTable->getTableName());
-      $query->where($mail != '');
-
-      $db->setQuery($query, 0, $limit);
-      $users = $db->loadObjectList();
-
-      $user = new StdClass();
-      $uf = $config->userFramework;
-      $contactCount = 0;
-      $contactCreated = 0;
-      $contactMatching = 0;
-      for ($i = 0; $i < count($users); $i++) {
-        $user->$id = $users[$i]->$id;
-        $user->$mail = $users[$i]->$mail;
-        $user->$name = $users[$i]->$name;
-        $contactCount++;
-        if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user,
-          $users[$i]->$id,
-          $users[$i]->$mail,
-          $uf,
-          1,
-          'Individual',
-          TRUE
-        )
-        ) {
-          $contactCreated++;
-        }
-        else {
-          $contactMatching++;
-        }
-        if (is_object($match)) {
-          $match->free();
-        }
-      }
-    }
-    elseif ($config->userFramework == 'WordPress') {
-      $uf = $config->userFramework;
-      $contactCount = 0;
-      $contactCreated = 0;
-      $contactMatching = 0;
-
-      global $wpdb;
-      $wpUserIds = $wpdb->get_col("SELECT $wpdb->users.ID FROM $wpdb->users");
-
-      foreach ($wpUserIds as $wpUserId) {
-        $wpUserData = get_userdata($wpUserId);
-        $contactCount++;
-        if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($wpUserData,
-          $wpUserData->$id,
-          $wpUserData->$mail,
-          $uf,
-          1,
-          'Individual',
-          TRUE
-        )
-        ) {
-          $contactCreated++;
-        }
-        else {
-          $contactMatching++;
-        }
-        if (is_object($match)) {
-          $match->free();
-        }
-      }
-    }
-
-    return array(
-      'contactCount' => $contactCount,
-      'contactMatching' => $contactMatching,
-      'contactCreated' => $contactCreated,
-    );
-  }
-
   /**
    * Create CMS user using Profile.
    *
index 6e3adf9b00e1163a93db40dcde0f7bdd9c358f6c..2e5619b8640afd563742c7b21f6a7ef362c2aede 100644 (file)
@@ -898,4 +898,15 @@ abstract class CRM_Utils_System_Base {
     header("$name: $value");
   }
 
+  /**
+   * Create CRM contacts for all existing CMS users
+   *
+   * @return array
+   * @throws \Exception
+   */
+  public function synchronizeUsers() {
+    throw new Exception('CMS user creation not supported for this framework');
+    return array();
+  }
+
 }
index 497a625219da2c633735c6da8389e7ef0a2e292c..765ad3ebe94b2f8e2797596bd38b285aa9efe820 100644 (file)
@@ -789,4 +789,51 @@ AND    u.status = 1
     drupal_add_http_header($name, $value);
   }
 
+  /**
+   * @inheritDoc
+   */
+  public function synchronizeUsers() {
+    $config = CRM_Core_Config::singleton();
+    if (PHP_SAPI != 'cli') {
+      set_time_limit(300);
+    }
+    $rows = array();
+    $id = 'uid';
+    $mail = 'mail';
+    $name = 'name';
+
+    $result = db_query("SELECT uid, mail, name FROM {users} where mail != ''");
+
+    while ($row = $result->fetchAssoc()) {
+      $rows[] = $row;
+    }
+
+    $user = new StdClass();
+    $uf = $config->userFramework;
+    $contactCount = 0;
+    $contactCreated = 0;
+    $contactMatching = 0;
+    foreach ($rows as $row) {
+      $user->$id = $row[$id];
+      $user->$mail = $row[$mail];
+      $user->$name = $row[$name];
+      $contactCount++;
+      if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row[$id], $row[$mail], $uf, 1, 'Individual', TRUE)) {
+        $contactCreated++;
+      }
+      else {
+        $contactMatching++;
+      }
+      if (is_object($match)) {
+        $match->free();
+      }
+    }
+
+    return array(
+      'contactCount' => $contactCount,
+      'contactMatching' => $contactMatching,
+      'contactCreated' => $contactCreated,
+    );
+  }
+
 }
index c11b66b2b490eaa2baf95b12db05fae0a12d9a33..3a08d2e5910200659939b5da8245bccd714799bc 100644 (file)
@@ -751,4 +751,51 @@ class CRM_Utils_System_Drupal6 extends CRM_Utils_System_DrupalBase {
     drupal_set_header("$name: $value");
   }
 
+  /**
+   * @inheritDoc
+   */
+  public function synchronizeUsers() {
+    $config = CRM_Core_Config::singleton();
+    if (PHP_SAPI != 'cli') {
+      set_time_limit(300);
+    }
+    $rows = array();
+    $id = 'uid';
+    $mail = 'mail';
+    $name = 'name';
+
+    $result = db_query("SELECT uid, mail, name FROM {users} where mail != ''");
+
+    while ($row = db_fetch_array($result)) {
+      $rows[] = $row;
+    }
+
+    $user = new StdClass();
+    $uf = $config->userFramework;
+    $contactCount = 0;
+    $contactCreated = 0;
+    $contactMatching = 0;
+    foreach ($rows as $row) {
+      $user->$id = $row[$id];
+      $user->$mail = $row[$mail];
+      $user->$name = $row[$name];
+      $contactCount++;
+      if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $row[$id], $row[$mail], $uf, 1, 'Individual', TRUE)) {
+        $contactCreated++;
+      }
+      else {
+        $contactMatching++;
+      }
+      if (is_object($match)) {
+        $match->free();
+      }
+    }
+
+    return array(
+      'contactCount' => $contactCount,
+      'contactMatching' => $contactMatching,
+      'contactCreated' => $contactCreated,
+    );
+  }
+
 }
index fa987f8a09a69cbdbe4e5e59050f2369361ec155..76c3c8908e45a750bac6bff8822ad2971b37d286 100644 (file)
@@ -729,4 +729,63 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
     $list[] = 'js/crm.joomla.js';
   }
 
+  /**
+   * @inheritDoc
+   */
+  public function synchronizeUsers() {
+    $config = CRM_Core_Config::singleton();
+    if (PHP_SAPI != 'cli') {
+      set_time_limit(300);
+    }
+    $id = 'id';
+    $mail = 'email';
+    $name = 'name';
+
+    $JUserTable = &JTable::getInstance('User', 'JTable');
+
+    $db = $JUserTable->getDbo();
+    $query = $db->getQuery(TRUE);
+    $query->select($id . ', ' . $mail . ', ' . $name);
+    $query->from($JUserTable->getTableName());
+    $query->where($mail != '');
+
+    $db->setQuery($query);
+    $users = $db->loadObjectList();
+
+    $user = new StdClass();
+    $uf = $config->userFramework;
+    $contactCount = 0;
+    $contactCreated = 0;
+    $contactMatching = 0;
+    for ($i = 0; $i < count($users); $i++) {
+      $user->$id = $users[$i]->$id;
+      $user->$mail = $users[$i]->$mail;
+      $user->$name = $users[$i]->$name;
+      $contactCount++;
+      if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user,
+        $users[$i]->$id,
+        $users[$i]->$mail,
+        $uf,
+        1,
+        'Individual',
+        TRUE
+      )
+      ) {
+        $contactCreated++;
+      }
+      else {
+        $contactMatching++;
+      }
+      if (is_object($match)) {
+        $match->free();
+      }
+    }
+
+    return array(
+      'contactCount' => $contactCount,
+      'contactMatching' => $contactMatching,
+      'contactCreated' => $contactCreated,
+    );
+  }
+
 }
index 0b815191fe7c705811fd3a0642f67bddb14a0896..22ffff0261a5608ef19ec72cd24cc06146fb51b0 100644 (file)
@@ -679,4 +679,52 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
     $list[] = 'js/crm.wordpress.js';
   }
 
+  /**
+   * @inheritDoc
+   */
+  public function synchronizeUsers() {
+    $config = CRM_Core_Config::singleton();
+    if (PHP_SAPI != 'cli') {
+      set_time_limit(300);
+    }
+    $id = 'ID';
+    $mail = 'user_email';
+
+    $uf = $config->userFramework;
+    $contactCount = 0;
+    $contactCreated = 0;
+    $contactMatching = 0;
+
+    global $wpdb;
+    $wpUserIds = $wpdb->get_col("SELECT $wpdb->users.ID FROM $wpdb->users");
+
+    foreach ($wpUserIds as $wpUserId) {
+      $wpUserData = get_userdata($wpUserId);
+      $contactCount++;
+      if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($wpUserData,
+        $wpUserData->$id,
+        $wpUserData->$mail,
+        $uf,
+        1,
+        'Individual',
+        TRUE
+      )
+      ) {
+        $contactCreated++;
+      }
+      else {
+        $contactMatching++;
+      }
+      if (is_object($match)) {
+        $match->free();
+      }
+    }
+
+    return array(
+      'contactCount' => $contactCount,
+      'contactMatching' => $contactMatching,
+      'contactCreated' => $contactCreated,
+    );
+  }
+
 }