Building out member and info inserts
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 27 Dec 2016 16:23:04 +0000 (11:23 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 27 Dec 2016 16:23:04 +0000 (11:23 -0500)
Pushing to test on another local site.

models/admin/management/csvimport.php
models/admin/management/import/members.php

index 967dcf0..bd6e7b7 100644 (file)
@@ -155,4 +155,235 @@ class GlmMembersAdmin_management_csvimport
 
     }
 
+    public function importMemberData( $data )
+    {
+        // Get default member type
+        $defaultMemberType    = $this->defaultMemberType();
+        $numbMembersActive    = 0;
+        $numbMembersInactive  = 0;
+        $namesInserted        = array();
+        $membImportIssues     = '';
+        $haveMembImportIssues = false;
+        $dupeNames            = 0;
+
+        // Loop through the data array of members.
+        foreach ( $data as $key => $val ) {
+            if ( $val['active'] == '1' ) {
+                $access = $this->config['access_numb']['Moderated'];
+                $numbMembersActive++;
+            } else {
+                // Member is not active, so set to no display no access
+                $access = $this->config['access_numb']['NotDisplayed'];
+                $numbMembersInactive++;
+            }
+
+            // Check for duplicate name
+            $membName = $val['member_name'];
+            if ( isset( $namesInserted[$membName] ) ) {
+                // Bump dupe count and add to name to make this one unique
+                $dupeNames++;
+                $membName            .= ' DUPE-'.$dupeNames;
+                $membImportIssues    .= '<li>Member Duplicate <b>'.$membName.'</b></li>';
+                $haveMembImportIssues = true;
+            }
+
+            // Add main member record
+            $res = $this->wpdb->insert(
+                GLM_MEMBERS_PLUGIN_DB_PREFIX.'members',
+                array(
+                    'access'        => $access,
+                    'member_type'   => $defaultMemberType,
+                    'created'       => date('Y-m-d'),
+                    'name'          => $membName,
+                    'member_slug'   => sanitize_title($val['member_name']),
+                    'old_member_id' => $val['member_id']
+                ),
+                array(
+                    '%d',
+                    '%d',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%d'
+                )
+            );
+            $member[$key]['new_id'] = $membID = $this->wpdb->insert_id;
+
+            // Add this member to the names inserted so we can check for duplicates
+            $namesInserted[$membName] = true;
+
+            // Create truncated short_descr from descriptions - Less tags, html encoded characters, newlines, tabs, etc.
+            $stripped = str_replace(
+                PHP_EOL,
+                '',
+                preg_replace(
+                    '/\t+/',
+                    '',
+                    preg_replace(
+                        "/&#?[a-z0-9]{2,8};/i",
+                        "",
+                        strip_tags( $val['description'] )
+                    )
+                )
+            );
+            $short_descr = implode( ' ', array_slice( explode( ' ', $stripped ), 0, 30 ) );
+            if ( strlen( $short_descr ) < strlen( $stripped ) ) {
+                $short_descr .= ' ...';
+            }
+            // Insert Member Information Record
+            $res = $this->wpdb->insert(
+                GLM_MEMBERS_PLUGIN_DB_PREFIX.'member_info',
+                array(
+                    'member'         => $membID,
+                    'member_name'    => $val['member_name'],
+                    'status'         => $this->config['status_numb']['Active'],
+                    'reference_name' => 'Imported Member Information',
+                    'descr'          => preg_replace('%[\n\r]%', '', $val['description']),
+                    'short_descr'    => $short_descr,
+                    'addr1'          => $val['street'],
+                    'addr2'          => '',
+                    'city'           => ( isset( $val['city'] ) ? $this->getCityId( $val['city'] ) : 0 ),
+                    'state'          => 'MI',
+                    'country'        => 'US',
+                    'zip'            => $val['zip'],
+                    'lat'            => $val['lat'],
+                    'lon'            => $val['lon'],
+                    'region'         => ( isset( $val['region'] ) ? $this->getRegionId( $val['region'] ) : 0 ),
+                    'phone'          => $val['phone'],
+                    'toll_free'      => $val['toll_free'],
+                    'url'            => $val['url'],
+                    'email'          => $val['process_email'],
+                    'logo'           => '',
+                    'cc_type'        => 0,
+                    'notes'          => '',
+                    'create_time'    => $val['create_date'],
+                    'modify_time'    => $val['last_update']
+                ),
+                array(
+                    '%d',
+                    '%s',
+                    '%d',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%d',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%f',
+                    '%f',
+                    '%d',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%d',
+                    '%s',
+                    '%s',
+                    '%s'
+                )
+            );
+            $member[$key]['new_info_id'] = $infoID = $this->wpdb->insert_id;
+        }
+    }
+
+    /**
+     * defaultMemberType
+     *
+     * Grab default member type.
+     * If none is found create default member type.
+     *
+     * @access public
+     * @return void
+     */
+    public function defaultMemberType()
+    {
+        $memberTypeId = $this->wpdb->get_var(
+            $this->wpdb->prepare(
+                "SELECT id
+                   FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_type
+                  WHERE name = %s",
+                'Default'
+            )
+        );
+        if ( $memberTypeId ) {
+            return $memberTypeId;
+        } else {
+            // If not found create it
+            $this->wpdb->insert(
+                GLM_MEMBERS_PLUGIN_DB_PREFIX.'member_type',
+                array(
+                    'name' => 'Default',
+                    'descr' => '',
+                ),
+                array(
+                    '%s',
+                    '%s',
+                )
+            );
+            return $this->wpdb->insert_id;
+        }
+    }
+
+    /**
+     * getCityId
+     *
+     * Find city id from name. If not found create record.
+     *
+     * @param mixed $city
+     * @access public
+     * @return void
+     */
+    public function getCityId( $city )
+    {
+        $cityId = $this->wpdb->get_var(
+            $this->wpdb->prepare(
+                "SELECT id
+                   FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities
+                  WHERE name = %s",
+                $city
+            )
+        );
+        if ( $cityId = filter_var( $cityId, FILTER_VALIDATE_INT ) ) {
+            return $cityId;
+        } else {
+            $this->wpdb->insert(
+                GLM_MEMBERS_PLUGIN_DB_PREFIX . 'cities',
+                array( 'name' => $city ),
+                array( '%s' )
+            );
+            return $this->wpdb->insert_id;
+        }
+    }
+    /**
+     * getRegionId
+     *
+     * @param mixed $region
+     * @access public
+     * @return void
+     */
+    public function getRegionId( $region )
+    {
+        $regionId = $this->wpdb->get_var(
+            $this->wpdb->prepare(
+                "SELECT id
+                   FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "regions
+                  WHERE name = %s",
+                $region
+            )
+        );
+        if ( $regionId = filter_var( $regionId, FILTER_VALIDATE_INT ) ) {
+            return $regionId;
+        } else {
+            $this->wpdb->insert(
+                GLM_MEMBERS_PLUGIN_DB_PREFIX . 'regions',
+                array( 'name' => $region ),
+                array( '%s' )
+            );
+            return $this->wpdb->insert_id;
+        }
+    }
 }
index f573ec2..5d79f35 100644 (file)
@@ -715,15 +715,15 @@ if (!$failure) {
 
     // Member Types
     $res = $this->wpdb->insert(
-            GLM_MEMBERS_PLUGIN_DB_PREFIX.'member_type',
-            array(
-                    'name' => 'Default',
-                    'descr' => '',
-            ),
-            array(
-                    '%s',
-                    '%s',
-            )
+        GLM_MEMBERS_PLUGIN_DB_PREFIX.'member_type',
+        array(
+            'name' => 'Default',
+            'descr' => '',
+        ),
+        array(
+            '%s',
+            '%s',
+        )
     );
     $defaultMemberType = $this->wpdb->insert_id;