From 338c8b427a1e41351304d50da4a70b7b6a52f080 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Thu, 7 May 2015 15:19:36 -0400 Subject: [PATCH] Connection import ready for final testing. --- classes/glmMemberImportFromConnections.php | 313 +++++++++++++++++- classes/glmPluginSupport.php | 24 ++ css/admin.css | 4 +- glm-member-db.php | 2 +- ...44e61950c851e98e8906d49.file.list.html.php | 10 +- models/admin/configure/development.php | 228 ++++++++----- views/admin/configure/development.html | 64 ++-- 7 files changed, 525 insertions(+), 120 deletions(-) diff --git a/classes/glmMemberImportFromConnections.php b/classes/glmMemberImportFromConnections.php index 009b2184..411f0259 100644 --- a/classes/glmMemberImportFromConnections.php +++ b/classes/glmMemberImportFromConnections.php @@ -64,6 +64,7 @@ class GlmMemberImportFromConnections */ public function checkConnections() { + // Check if Connections plugin is installed if (!is_plugin_active('connections/connections.php')) { return false; @@ -138,18 +139,328 @@ class GlmMemberImportFromConnections $a = unserialize($v['options']); $list[$k]['options'] = $a; + } + return $list; + } + /* + * Import all members not marked as "Skip" + * + * Looks at "skip" POST parameter (array) for ID's that should be skipped. + * + * @return boolean + * @access public + */ + public function doImport() + { + $list = $this->getList(); + /* + * Create default member type + */ + $memberTypeName = sanitize_text_field($_REQUEST['memberType']); + $sql = " + INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."member_type + ( + name, + descr + ) + VALUES + ( + '$memberTypeName', + '' + ) + ;"; + $this->wpdb->query($sql); + $membTypeID = $this->wpdb->insert_id; + $queryError = $this->wpdb->last_error; + if ($queryError) { + glmMembersAdmin::addNotice('Error when adding the default member type: Check following message.
'.$queryError.'
', 'AdminError'); + } + /* + * Create default region + */ + $regionName = sanitize_text_field($_REQUEST['region']); + $sql = " + INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."regions + ( + name, + descr, + short_descr + ) + VALUES + ( + '$memberTypeName', + '', + '' + ) + ;"; + $this->wpdb->query($sql); + $regionID = $this->wpdb->insert_id; + $queryError = $this->wpdb->last_error; + if ($queryError) { + glmMembersAdmin::addNotice('Error when adding the default region: Check following message.
'.$queryError.'
', 'AdminError'); } - return $list; + /* + * For each member in listing + */ + $membersImported = 0; + $cities = array(); + foreach ($list as $m) { + + // If we're not skipping this entry + if (!isset($_REQUEST['skip'][$m['id']])) { + + echo "123 ".$m['id']; + + + /* + * Create base Member Record + */ + $sql = " + INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."members + ( + access, + member_type, + created, + name + ) + VALUES + ( + ".$this->config['memb_access_numb']['Moderated'].", + $membTypeID, + '".$m['ts']."', + '".$m['organization']."' + ) + ;"; + $this->wpdb->query($sql); + $membID = $this->wpdb->insert_id; + $queryError = $this->wpdb->last_error; + if ($queryError) { + glmMembersAdmin::addNotice('Error when creating member: Check following message.
'.$queryError.'
', 'AdminError'); + } + + /* + * Add a member information record + */ + + // Get address data + reset($m['addresses']); + $addr = current($m['addresses']); + + // Check if city has been created yet + if (!isset($cities[$addr['city']])) { + + // Create City + /* + * Create default region + */ + $sql = " + INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."cities + ( + name + ) + VALUES + ( + '".$addr['city']."' + ) + ;"; + $this->wpdb->query($sql); + $cityID = $this->wpdb->insert_id; + $queryError = $this->wpdb->last_error; + if ($queryError) { + glmMembersAdmin::addNotice('Error when adding the default region: Check following message.
'.$queryError.'
', 'AdminError'); + } + $cities[$addr['city']] = $cityID; + + } + + // Get first link as URL + reset($m['links']); + $link = current($m['links']); + $url = $link['url']; + + // Get phone numbers + $phone = ''; + $toll_free = ''; + foreach ($m['phone_numbers'] as $ph) { + switch($ph['type']) { + case 'workphone': + $phone = $ph['number']; + break; + case 'homephone': + $toll_free = $ph['number']; + break; + case 'workfax': + case 'homefax': + case 'cellphone': + break; + } + } + + // Check for Logo + $logo = ''; + if (isset($m['options']['logo'])) { + $logoPath = $m['options']['logo']['meta']['path']; + $logo = $this->storeImage($logoPath); + if ($logo === false) { + $logo = ''; + } + } + + $t = date('Y-m-d H:i:s', time()); + $sql = " + INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."member_info + ( + member, + member_name, + status, + reference_name, + descr, + short_descr, + addr1, + addr2, + city, + state, + country, + zip, + lat, + lon, + region, + phone, + toll_free, + url, + logo, + cc_type, + notes, + create_time, + modify_time + ) + VALUES + ( + $membID, + '".$m['organization']."', + ".($m['status'] == 'approved' ? + $this->config['status_numb']['Active'] : + $this->config['status_numb']['Inactive'] + ).", + 'Imported from Connections Business Listing', + '".$m['bio']."', + '', + '".$addr['line_1']."', + '".$addr['line_2']."', + $cityID, + '".$addr['state']."', + '".$addr['country']."', + '".$addr['zipcode']."', + ".$addr['latitude'].", + ".$addr['longitude'].", + $regionID, + '$phone', + '$toll_free', + '$url', + '$logo', + 0, + '', + '$t', + '$t' + ) + ;"; + $this->wpdb->query($sql); + $membID = $this->wpdb->insert_id; + $queryError = $this->wpdb->last_error; + if ($queryError) { + glmMembersAdmin::addNotice('Error when creating member: Check following message.
'.$queryError.'
', 'AdminError'); + } + + // Check for additional image +/* + $image = ''; + if (isset($m['options']['image'])) { + $imagePath = $m['options']['image']['meta']['path']; + $image = $this->storeImage($imagePath); + if ($image === false) { + $image = ''; + } + } +*/ + + $membersImported++; + } + + } + + return $membersImported; + } + + /* + * Store image and return image name + * + * @var string $image Source image location (not deleted) + * + * @return string Image name + * @access public + */ + + // If we have a good image + public function storeImage($image) + { + + // Get new image using temporary file name + $newImage = wp_get_image_editor($image); + + // If we have a good image + if ( ! is_wp_error( $newImage ) ) { + + // Get the desired file name and add a timestamp to it to ensure that it's unique + $fInfo = pathinfo($image); + $newFilename = $fInfo['filename'].'_'.time().'.'.$fInfo['extension']; + + // Get image temp file name - Not currently using, but should be using to check for resizing sanity + $size = $newImage->get_size(); + + // Try to store the image using that file name in the 'original' directory + $storedImage = $newImage->save( GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$newFilename ); + + // Now resize the images using $sizeArray + $sizes = $newImage->multi_resize($this->config['imageSizes']); + + // Finally, move the files to the various size directories and rename them back to the correct name + while (list($k, $v) = each($this->config['imageSizes'])) { + + // Check if size directory needs to be made + if (!file_exists(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$k)) { + mkdir(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$k); + } + + // If there's an entry in the $sizes array, it means that the image resized to this size + if (isset($sizes[$k])) { + + // Move resized file to desired direectory + rename(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$sizes[$k]['file'], GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$k.'/'.$newFilename); + + } else { + + // Image didn't resize to this size, probably because it was smaller than the destination size (silly WP Image Editor class) - so just use the original + copy(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$newFilename, GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$k.'/'.$newFilename); + + } + } + + } else { + return false; + } + + return $newFilename; } + } ?> \ No newline at end of file diff --git a/classes/glmPluginSupport.php b/classes/glmPluginSupport.php index 7be41a55..71e599e3 100644 --- a/classes/glmPluginSupport.php +++ b/classes/glmPluginSupport.php @@ -62,6 +62,30 @@ class GlmPluginSupport update_option('glmMembersAdminNoticeDataBlocks', $dataBlocks); break; + case 'AdminNotice': + echo ' +
+

'.$message.'

+
+ '; + break; + + case 'AdminError': + echo ' +
+

'.$message.'

+
+ '; + break; + + case 'AdminNag': + echo ' +
+

'.$message.'

+
+ '; + break; + default; $notices = get_option('glmMembersAdminNotices'); $notices[] = $message; diff --git a/css/admin.css b/css/admin.css index 66cba49b..d8b629ab 100644 --- a/css/admin.css +++ b/css/admin.css @@ -11,7 +11,7 @@ color: red; } .glm-notice { - color: red; + color: green; } .glm-active { color: blue; @@ -23,7 +23,7 @@ float: left; } .glm-indent { - padding-left: 2em; + margin-left: 2em; } .glm-center { text-align: center; diff --git a/glm-member-db.php b/glm-member-db.php index ee289df4..81195ef4 100644 --- a/glm-member-db.php +++ b/glm-member-db.php @@ -273,7 +273,7 @@ if (isset($_REQUEST['glmDebugWindow']) && $_REQUEST['glmDebugWindow'] == true) { * @return void * @access public */ -function glmMembersAdminNotices($windowed = false) +function glmMembersAdminNotices($windowed = true) { $output = ''; diff --git a/misc/smarty/templates_c/4c287ca0e4946b3d644e61950c851e98e8906d49.file.list.html.php b/misc/smarty/templates_c/4c287ca0e4946b3d644e61950c851e98e8906d49.file.list.html.php index 92fce188..249f6b50 100644 --- a/misc/smarty/templates_c/4c287ca0e4946b3d644e61950c851e98e8906d49.file.list.html.php +++ b/misc/smarty/templates_c/4c287ca0e4946b3d644e61950c851e98e8906d49.file.list.html.php @@ -1,4 +1,4 @@ - decodeProperties(array ( @@ -7,7 +7,7 @@ $_valid = $_smarty_tpl->decodeProperties(array ( '4c287ca0e4946b3d644e61950c851e98e8906d49' => array ( 0 => '/var/www/server/wordpress/wp-content/plugins/glm-member-db/views/admin/members/list.html', - 1 => 1426428835, + 1 => 1430858868, 2 => 'file', ), ), @@ -45,7 +45,7 @@ $_valid = $_smarty_tpl->decodeProperties(array (

List of Members

- +
@@ -59,7 +59,9 @@ $_valid = $_smarty_tpl->decodeProperties(array ( tpl_vars['haveMembers']->value) {?> - tpl_vars["i"] = new Smarty_variable("0", null, 0);?> + tpl_vars["i"])) {$_smarty_tpl->tpl_vars["i"] = clone $_smarty_tpl->tpl_vars["i"]; +$_smarty_tpl->tpl_vars["i"]->value = "0"; $_smarty_tpl->tpl_vars["i"]->nocache = null; $_smarty_tpl->tpl_vars["i"]->scope = 0; +} else $_smarty_tpl->tpl_vars["i"] = new Smarty_variable("0", null, 0);?> tpl_vars['m'] = new Smarty_Variable; $_smarty_tpl->tpl_vars['m']->_loop = false; $_from = $_smarty_tpl->tpl_vars['members']->value; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array');} foreach ($_from as $_smarty_tpl->tpl_vars['m']->key => $_smarty_tpl->tpl_vars['m']->value) { diff --git a/models/admin/configure/development.php b/models/admin/configure/development.php index a338fa7d..edf68dc9 100644 --- a/models/admin/configure/development.php +++ b/models/admin/configure/development.php @@ -97,14 +97,14 @@ class GlmMembersAdmin_configure_development $dbVersion = GLM_MEMBERS_PLUGIN_DB_VERSION; settype($dbVersion, 'string'); - $resultMessage = ''; - $importData = array( - 'success' => false, - 'haveNotices' => false, - 'notices' => array(), - 'haveMembers' => false, - 'members' => false - ); + $templateData['resultMessage'] = ''; + $templateData['success'] = false; + $templateData['haveNotices'] = false; + $notices = array(); + $templateData['haveMembers'] = false; + $templateData['import'] = false; + $templateData['importNotice'] = false; + $templateData['haveDatabaseTableList'] = false; // Load database activation class to get access to database version information require_once (GLM_MEMBERS_PLUGIN_PATH . '/activate.php'); @@ -123,107 +123,77 @@ class GlmMembersAdmin_configure_development require_once(GLM_MEMBERS_PLUGIN_CLASS_PATH.'/glmMemberImportFromConnections.php'); $Connections = new GlmMemberImportFromConnections($this->wpdb, $this->config); - // Check if Connections is not installed or not active. If active, gets count of entries. - $c = $Connections->checkConnections(); - if ($c === false) { - $importaData['notices'][] = 'Connections Business Directory plugin is not installed or not active!
'.print_r($c,1); - break; - } - $importData['numbEntries'] = $c; - $importData['notices'][] = 'Connections Business Directory plugin is active.
Number of businesses listed: '.$c.'
'; + if (isset($_REQUEST['import']) && $_REQUEST['import'] == 'now') { - // Get the basic business data for review. - $importData['members'] = $Connections->getList(); - if ($importData['members'] === false) { - $importaData['notices'][] = 'No businesses were retieved.'; - break; - } + // Reset the database + if (!$this->deleteDataTables($dbVersion)) { + glmMembersAdmin::addNotice('Unable to delete the database tables while resetting the database.
', 'AdminError'); + break; + } + if (!$this->createDataTables($dbVersion)) { + glmMembersAdmin::addNotice('Unable to create the database tables while resetting the database.
', 'AdminError'); + break; + } + glmMembersAdmin::addNotice('Database tables have been reset in preparation importing members.
', 'AdminNotice'); - $importData['haveMembers'] = true; + $templateData['import'] = true; + $templateData['membersImported'] = $Connections->doImport(); - if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { - glmMembersAdmin::addNotice($importData['members'], 'DataBlock', 'Base Members Data'); - } + } else { + // Check if Connections is not installed or not active. If active, gets count of entries. + $c = $Connections->checkConnections(); + if ($c === false) { + glmMembersAdmin::addNotice('Connections Business Directory plugin is not installed or not active!
'.print_r($c,1), 'AdminError'); + break; + } + $templateData['numbEntries'] = $c; + $importNotice = 'Connections Business Directory plugin is active.
Number of businesses listed: '.$c.'
'; + + // Get the basic business data for review. + $templateData['members'] = $Connections->getList(); + if ($templateData['members'] === false) { + $importNotice = 'No businesses were retieved.'; + break; + } + + $templateData['haveMembers'] = true; + + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { + glmMembersAdmin::addNotice($templateData['members'], 'DataBlock', 'Base Members Data'); + } + + } break; case 'drop': case 'reset': - // Read in Database deletion script - assumes the current db version. - $sqlFile = GLM_MEMBERS_PLUGIN_DB_SCRIPTS.'/drop_database_V'.$dbVersion.'.sql'; - $sql = file_get_contents($sqlFile); - - // Replace {prefix} with table name prefix - $sql = str_replace('{prefix}', GLM_MEMBERS_PLUGIN_DB_PREFIX, $sql); + $del = $this->deleteDataTables($dbVersion); - if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { - glmMembersAdmin::addNotice('Dropping all database tables', 'Process'); - glmMembersAdmin::addNotice($sql, 'DataBlock', 'Drop Tables SQL'); - } + if ($del) { + glmMembersAdmin::addNotice('New database tables for the '.GLM_MEMBERS_PLUGIN_NAME.' plugin have been deleted.', 'AdminNotice'); - // Removing the tables using the script - $this->wpdb->query($sql); - - // If there's been an error, display in debug Alert - $queryError = $this->wpdb->last_error; - if ($queryError) { - if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { - glmMembersAdmin::addNotice('Error when deleting database:
'.$queryError, 'Alert'); - } } - $resultMessage .= 'Database deleted
'; - if ($option != 'reset') { break; } case 'create': - // Read in Database creation script - $sqlFile = GLM_MEMBERS_PLUGIN_DB_SCRIPTS.'/create_database_V'.$dbVersion.'.sql'; - $sql = file_get_contents($sqlFile); - - // Replace {prefix} with table name prefix - $sql = str_replace('{prefix}', GLM_MEMBERS_PLUGIN_DB_PREFIX, $sql); - - // Split script into separate queries by looking for lines with only "---" - $queries = preg_split('/^----$/m', $sql); - - // Try executing all queries to build database - $qForDebug = ''; - do { - $q = current($queries); - $this->wpdb->query($q); - $queryError = $this->wpdb->last_error; - - if ($queryError) { - glmMembersAdmin::addNotice('Error when creating database:
'.$queryError, 'Alert'); - } - $qForDebug .= $queryError.$q; - - } while ($queryError == '' && next($queries)); - - if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { - glmMembersAdmin::addNotice('Creating database tables', 'Process'); - glmMembersAdmin::addNotice($qForDebug, 'DataBlock', 'Create Tables SQL'); - } + $create = $this->createDataTables($dbVersion); // If there were no errors - if (trim($queryError) == '') { + if ($create) { // Notify the user that the database has been installed - $resultMessage .= 'New database tables installed for the '.GLM_MEMBERS_PLUGIN_NAME.' plugin.
'; + glmMembersAdmin::addNotice('New database tables installed for the '.GLM_MEMBERS_PLUGIN_NAME.' plugin.', 'AdminNotice'); // Save the version of the installed database update_option('glmMembersDatabaseDbVersion', $dbVersion); - - } else { - $resultMessage .= 'Failure installing database tables for the '.GLM_MEMBERS_PLUGIN_NAME.' plugin.
'; - $resultMessage .= 'Database Installation Error: '.$queryError.'
'; } break; @@ -247,6 +217,10 @@ class GlmMembersAdmin_configure_development ", ARRAY_A); if (is_array($existingTables)) { + + $ + + $resultMessage .= 'Current database tables
ID
+ + +
Default member type:
Default region name:
+ +

+ + Skip All   + Import All +

+
+ +
+ @@ -38,17 +59,17 @@ - {foreach $importData.members as $m} + {foreach $members as $m} - + - {assign var="i" value="0"} - {foreach $m.addresses as $a} - {if $i++ gt 1} + {assign var="i" value="0"} + {foreach $m.addresses as $a} + {if $i++ gt 1} - {/if} + {/if} @@ -58,12 +79,13 @@ - {/foreach} - {/foreach} + {/foreach} + {/foreach}
{$m.id} {$m.organization}
   {$a.line_1} {$a.line_2} {$a.line_3}{$a.country} {$a.latitude|number_format:3:".":","}/{$a.longitude|number_format:3:".":","}
-{/if} + {/if} + {/if}

Database

@@ -87,7 +109,7 @@ {/if} -{if $importData.haveMembers} +{if $haveMembers}