From 03d5592acc5bf7d1984b6284c0f835212512293d Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 21 Nov 2015 19:17:01 -0500 Subject: [PATCH] CRM-17609 - Refactor synchronize contacts function into appropriate cms classes --- CRM/Admin/Form/CMSUser.php | 2 +- CRM/Core/BAO/CMSUser.php | 149 --------------------------------- CRM/Utils/System/Base.php | 11 +++ CRM/Utils/System/Drupal.php | 47 +++++++++++ CRM/Utils/System/Drupal6.php | 47 +++++++++++ CRM/Utils/System/Joomla.php | 59 +++++++++++++ CRM/Utils/System/WordPress.php | 48 +++++++++++ 7 files changed, 213 insertions(+), 150 deletions(-) diff --git a/CRM/Admin/Form/CMSUser.php b/CRM/Admin/Form/CMSUser.php index b2251817ad..e01b76d8d9 100644 --- a/CRM/Admin/Form/CMSUser.php +++ b/CRM/Admin/Form/CMSUser.php @@ -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( diff --git a/CRM/Core/BAO/CMSUser.php b/CRM/Core/BAO/CMSUser.php index 6db4cad210..dd45a5a82c 100644 --- a/CRM/Core/BAO/CMSUser.php +++ b/CRM/Core/BAO/CMSUser.php @@ -40,155 +40,6 @@ */ 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. * diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index 6e3adf9b00..2e5619b864 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -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(); + } + } diff --git a/CRM/Utils/System/Drupal.php b/CRM/Utils/System/Drupal.php index 497a625219..765ad3ebe9 100644 --- a/CRM/Utils/System/Drupal.php +++ b/CRM/Utils/System/Drupal.php @@ -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, + ); + } + } diff --git a/CRM/Utils/System/Drupal6.php b/CRM/Utils/System/Drupal6.php index c11b66b2b4..3a08d2e591 100644 --- a/CRM/Utils/System/Drupal6.php +++ b/CRM/Utils/System/Drupal6.php @@ -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, + ); + } + } diff --git a/CRM/Utils/System/Joomla.php b/CRM/Utils/System/Joomla.php index fa987f8a09..76c3c8908e 100644 --- a/CRM/Utils/System/Joomla.php +++ b/CRM/Utils/System/Joomla.php @@ -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, + ); + } + } diff --git a/CRM/Utils/System/WordPress.php b/CRM/Utils/System/WordPress.php index 0b815191fe..22ffff0261 100644 --- a/CRM/Utils/System/WordPress.php +++ b/CRM/Utils/System/WordPress.php @@ -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, + ); + } + } -- 2.25.1