From 3528ed38678b3fbfbd9b8c7a058ded25e14d4695 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 4 Apr 2018 14:58:53 -0400 Subject: [PATCH] Setup import plugin This plugin is for fetching new member data from migcsa ds4 database. For now it is fetching and importing: members member_info member categories --- models/admin/migcsa/index.php | 405 +++++++++++++++++++++++++++ setup/adminMenus.php | 9 +- setup/validActions.php | 5 +- views/admin/migcsa/customFields.html | 5 + views/admin/migcsa/import.html | 5 + views/admin/migcsa/index.html | 6 + 6 files changed, 433 insertions(+), 2 deletions(-) create mode 100644 models/admin/migcsa/index.php create mode 100644 views/admin/migcsa/customFields.html create mode 100644 views/admin/migcsa/import.html create mode 100644 views/admin/migcsa/index.html diff --git a/models/admin/migcsa/index.php b/models/admin/migcsa/index.php new file mode 100644 index 0000000..488af20 --- /dev/null +++ b/models/admin/migcsa/index.php @@ -0,0 +1,405 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release index.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +class GlmMembersAdmin_migcsa_index +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + /** + * Event ID + * + * @var $migcsaID + * @access public + */ + public $migcsaID = false; + + /** + * Constructor + * + * This contructor performs the work for this model. This model returns + * an array containing the following. + * + * 'status' + * + * True if successfull and false if there was a fatal failure. + * + * 'view' + * + * A suggested view name that the contoller should use instead of the + * default view for this model or false to indicate that the default view + * should be used. + * + * 'data' + * + * Data that the model is returning for use in merging with the view to + * produce output. + * + * @wpdb object WordPress database object + * + * @return array Array containing status, suggested view, and any data + */ + public function __construct ($wpdb, $config) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + } + + public function modelAction($actionData = false) + { + + $memberID = false; + $lockedToMember = false; + $response = false; + + // Member classes to member_types + $member_types = array( + 'AA' => 13, + 'A' => 1, + 'SM' => 3, + 'C' => 3, + 'EM' => 4, + 'AFF' => 6, + 'H' => 7, + 'E' => 5, + 'SA' => 8, + 'I' => 10, + 'R' => 9, + 'FM' => 11, + ); + + $dbh = new PDO( + 'pgsql: host=ds4.gaslightmedia.com dbname=migcsa2_update user=nobody', + null, + null, + array( + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC + ) + ); + + $dbh->setAttribute( + PDO::ATTR_ERRMODE, + PDO::ERRMODE_EXCEPTION + ); + + if ( isset( $_REQUEST['option'] ) ) { + $option = $_REQUEST['option']; + } else { + $option = 'list'; + } + + switch( $option ) { + case 'customFields': + $view = 'customFields'; + $response = $this->importCustomFields(); + break; + case 'import': + $view = 'import'; + $response = $this->importNewMembers(); + break; + case 'list': + $view = 'index'; + break; + } + + + // Compile template data + $templateData = array( + 'lockedToMember' => $lockedToMember, + 'response' => $response, + ); + // Return status, any suggested view, and any data to controller + return array( + 'status' => true, + 'modelRedirect' => false, + 'view' => 'admin/migcsa/' . $view . '.html', + 'data' => $templateData + ); + + } + + public function importCustomFields() + { + $response = ''; + $response .= '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + return $response; + } + + public function importNewMembers() + { + $response = ''; + // Looking for all members not already in the new database + $old_members = array(); + $old_member_data = $this->wpdb->get_results( + "SELECT old_member_id + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members + WHERE old_member_id IS NOT NULL", + ARRAY_A + ); + foreach ( $old_member_data as $data ) { + $old_members[] = $data['old_member_id']; + } + // $response .= '
$old_members: ' . print_r( $old_members, true ) . '
'; + + $sql = " + SELECT M.*,S.state_abb + FROM member M LEFT OUTER JOIN state S ON (S.state_id = M.mailing_state_id) + WHERE M.member_id NOT IN (" . implode( ',', $old_members ) . ") + ORDER BY M.member_id"; + $stmt = $dbh->query( $sql ); + $members = $stmt->fetchAll(); + $response .= '
$members: ' . print_r( $members, true ) . '
'; + + // Prepare statement for the member categories + $categorySql = " + SELECT C.name + FROM category C LEFT OUTER JOIN member_category MC ON (C.category_id = MC.category_id) + WHERE MC.member_id = :member_id"; + $getMemberCategories = $dbh->prepare( $categorySql ); + + foreach ( $members as $member ) { + // Initialize $insert_data array. + $insert_data = array(); + $member_name = $member['primary_contact_lname'] . ', ' . $member['primary_contact_fname']; + $access = ( $member['active'] ) ? $this->config['access_numb']['Moderated'] : $this->config['access_numb']['NotDisplayed']; + $member_type = ( $member['member_class'] ) ? $member_types[$member['member_class']] : 0; + // Add the member data + $insert_data['data'] = array( + 'access' => $access, + 'member_type' => $member_type, + 'created' => $member['create_date'], + 'name' => $member_name, + 'member_slug' => sanitize_title( $member_name ), + 'old_member_id' => $member['member_id'], + ); + $insert_data['format'] = array( + '%d',// access + '%d',// member_type + '%s',// created + '%s',// name + '%s',// member_slug + '%d',// old_member_id + ); + $new_member_id = $this->importMemberData( $insert_data ); + if ( $new_member_id ) { + // Add the member contact + $contact['data'] = array( + 'active' => true, + 'primary_contact' => true, + 'access' => $this->config['access_numb']['NotDisplayedModerated'], + 'fname' => $member['primary_contact_fname'], + 'lname' => $member['primary_contact_lname'], + 'contact_type' => $this->config['contact_type_numb']['Personal'], + 'contact_role' => $this->config['contact_role_numb']['EntityManager'], + 'email' => $member['process_email'], + 'username' => $member['member_login'], + 'notes' => 'Imported from old Web site.', + 'create_time' => date( 'Y-m-d H:i:s' ), + 'ref_type' => $this->config['ref_type_numb']['Member'], + 'ref_dest' => $new_member_id, + ); + $contact['format'] = array( + '%d', // active + '%d', // primary_contact + '%d', // access + '%s', // fname + '%s', // lname + '%d', // contact_type + '%d', // contact_role + '%s', // email + '%s', // username + '%s', // notes + '%s', // create_time + '%d', // ref_type + '%d' // ref_dest + ); + $new_contact_id = $this->importMemberContactData( $contact ); + $response .= '
$new_contact_id: ' . print_r( $new_contact_id, true ) . '
'; + // Reset $insert_data array. + $insert_data = array(); + $response .= '
$new_member_id: ' . print_r( $new_member_id, true ) . '
'; + + // Add the member_info record + $city_id = $this->getCityId( $member['city'] ); + $mailing_city_id = $this->getCityId( $member['mailing_city'] ); + $region_id = $this->getRegionId( $member['region'] ); + $insert_data['data'] = array( + 'member' => $new_member_id, + 'member_name' => $member_name, + 'status' => $this->config['status_numb']['Active'], + 'reference_name' => 'Imported Member Information', + 'descr' => preg_replace( '%[\n\r]%', '', $member['description'] ), + 'short_descr' => '', + 'addr1' => '', + 'addr2' => '', + 'city' => $city_id, + 'state' => $member['state'], + 'country' => 'US', + 'zip' => $member['zip'], + 'mailing_addr1' => $member['mailing_address'], + 'mailing_city' => $mailing_city_id, + 'mailing_state' => $member['state_abb'], + 'mailing_zip' => $member['mailing_zip'], + 'lat' => $member['lat'], + 'lon' => $member['lon'], + 'region' => $region_id, + 'phone' => $member['phone'], + 'toll_free' => $member['toll_free'], + 'url' => $member['url'], + 'email' => $member['member_contact_email'], + 'create_time' => $member['create_date'], + ); + $insert_data['format'] = array( + '%d', // member id + '%s', // name + '%d', // status + '%s', // ref name + '%s', // descr + '%s', // short_descr + '%s', // addr1 + '%s', // addr2 + '%d', // city + '%s', // state + '%s', // country + '%s', // zip + '%s', // mailing addr1 + '%d', // mailing city + '%s', // mailing state + '%s', // mailing zip + '%f', // lat + '%f', // lon + '%d', // region + '%s', // phone + '%s', // toll_free + '%s', // url + '%s', // email + '%s' // create_time + ); + $new_member_info_id = $this->importMemberInfoData( $insert_data ); + if ( $new_member_info_id ) { + $response .= '
$new_member_info_id: ' . print_r( $new_member_info_id, true ) . '
'; + + // fetch member categories + $getMemberCategories->bindParam( ':member_id', $member['member_id'], PDO::PARAM_INT ); + $getMemberCategories->execute(); + $categories = $getMemberCategories->fetchAll(); + $response .= '
$categories: ' . print_r( $categories, true ) . '
'; + foreach ( $categories as $category ) { + // Get new category id + $new_category_id = $this->getCategoryId( $category['name'] ); + $this->wpdb->insert( + GLM_MEMBERS_PLUGIN_DB_PREFIX . 'category_member_info', + array( + 'category' => $new_category_id, + 'member_info' => $new_member_info_id + ), + array( + '%d', + '%d' + ) + ); + } + + } + } + } + return $response; + } + + public function getCategoryId( $category_name ) + { + return $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "categories + WHERE name = %s", + $category_name + ) + ); + } + + public function getCityId( $city_name ) + { + return $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities + WHERE name = %s", + $city_name + ) + ); + } + + public function getRegionId( $region_name ) + { + return $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "regions + WHERE name = %s", + $region_name + ) + ); + } + + public function importMemberData( $member ) + { + $this->wpdb->insert( + GLM_MEMBERS_PLUGIN_DB_PREFIX . 'members', + $member['data'], + $member['format'] + ); + return $this->wpdb->insert_id; + } + + public function importMemberInfoData( $member_info ) + { + $this->wpdb->insert( + GLM_MEMBERS_PLUGIN_DB_PREFIX . 'member_info', + $member_info['data'], + $member_info['format'] + ); + return $this->wpdb->insert_id; + } + + public function importMemberContactData( $contact ) + { + $this->wpdb->insert( + GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . 'contacts', + $member_info['data'], + $member_info['format'] + ); + return $this->wpdb->insert_id; + } + + +} diff --git a/setup/adminMenus.php b/setup/adminMenus.php index 74964c8..5c7cb05 100644 --- a/setup/adminMenus.php +++ b/setup/adminMenus.php @@ -49,4 +49,11 @@ * is named the same as the "glm_action" parameter. * */ - +add_submenu_page( + $mainMenuSlug, + 'MiGCSA Import', + 'MiGCSA Import', + 'glm_members_members', + 'glm-members-admin-menu-migcsa-index', + function() {$this->controller('migcsa');} +); diff --git a/setup/validActions.php b/setup/validActions.php index 564046d..8974d31 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -59,9 +59,12 @@ $glmMembersMigcsaAddOnValidActions = array( 'adminActions' => array( + 'migcsa' => array( + 'index' => GLM_MEMBERS_MIGCSA_PLUGIN_SLUG, + ), ), 'frontActions' => array( ) ); -?> \ No newline at end of file +?> diff --git a/views/admin/migcsa/customFields.html b/views/admin/migcsa/customFields.html new file mode 100644 index 0000000..4ac0cff --- /dev/null +++ b/views/admin/migcsa/customFields.html @@ -0,0 +1,5 @@ +

Import for migcsa custom fields

+ +{if $response} + {$response} +{/if} diff --git a/views/admin/migcsa/import.html b/views/admin/migcsa/import.html new file mode 100644 index 0000000..8be1b74 --- /dev/null +++ b/views/admin/migcsa/import.html @@ -0,0 +1,5 @@ +

Import for migcsa members

+ +{if $response} + {$response} +{/if} diff --git a/views/admin/migcsa/index.html b/views/admin/migcsa/index.html new file mode 100644 index 0000000..54b7f54 --- /dev/null +++ b/views/admin/migcsa/index.html @@ -0,0 +1,6 @@ +

MiGCSA Importer

+ + +Import Members +
+Import Custom Fields -- 2.17.1