From b966219fd17c29c1e436484ac09ab7c29459cae7 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Tue, 12 Jul 2016 15:10:36 -0400 Subject: [PATCH] Update the search form and adding the source table entries. Members in the members only will not be able to edit leads. Lead sources not setup so they're entered into the sources table. Also adding new fields for the sources table and the searches table. --- index.php | 26 ++++---- models/admin/leads/index.php | 59 +++++++++++++----- setup/adminMenus.php | 12 ++++ ..._V0.0.1.sql => create_database_V0.0.2.sql} | 4 +- setup/databaseScripts/dbVersions.php | 3 +- .../update_database_V0.0.2.sql | 21 +++++++ setup/frontHooks.php | 62 ++++++++++++++++--- views/admin/leads/index.html | 20 +++++- 8 files changed, 166 insertions(+), 41 deletions(-) rename setup/databaseScripts/{create_database_V0.0.1.sql => create_database_V0.0.2.sql} (97%) create mode 100644 setup/databaseScripts/update_database_V0.0.2.sql diff --git a/index.php b/index.php index 1254036..cd769a3 100644 --- a/index.php +++ b/index.php @@ -38,7 +38,7 @@ * version from this plugin. */ define('GLM_MEMBERS_LEADS_PLUGIN_VERSION', '0.0.1'); -define('GLM_MEMBERS_LEADS_PLUGIN_DB_VERSION', '0.0.1'); +define('GLM_MEMBERS_LEADS_PLUGIN_DB_VERSION', '0.0.2'); // This is the minimum version of the GLM Members DB plugin require for this plugin. define('GLM_MEMBERS_LEADS_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION', '2.0.5'); @@ -151,26 +151,26 @@ unset($leadsManagementSettings['id']); function glmMembersRegisterLeads($addOns) { // Add this add-on to the add-ons array - $addOns[GLM_MEMBERS_LEADS_PLUGIN_SLUG] = array( - 'dir' => GLM_MEMBERS_LEADS_PLUGIN_PATH, - 'name' => GLM_MEMBERS_LEADS_PLUGIN_NAME, - 'short_name' => GLM_MEMBERS_LEADS_PLUGIN_SHORT_NAME, - 'slug' => GLM_MEMBERS_LEADS_PLUGIN_SLUG, - 'actions' => $GLOBALS['glmMembersLeadsAddOnValidActions'], - 'config' => array( + $addOns[GLM_MEMBERS_LEADS_PLUGIN_SLUG] = array( + 'dir' => GLM_MEMBERS_LEADS_PLUGIN_PATH, + 'name' => GLM_MEMBERS_LEADS_PLUGIN_NAME, + 'short_name' => GLM_MEMBERS_LEADS_PLUGIN_SHORT_NAME, + 'slug' => GLM_MEMBERS_LEADS_PLUGIN_SLUG, + 'actions' => $GLOBALS['glmMembersLeadsAddOnValidActions'], + 'config' => array( ), - 'shortcodes' => $GLOBALS['glmMembersLeadsShortcodes'], + 'shortcodes' => $GLOBALS['glmMembersLeadsShortcodes'], 'shortcodesDescription' => $GLOBALS['glmMembersLeadsShortcodesDescription'] ); // If we have database tables for this plugin/addon, provide that data also if (isset($GLOBALS['glmMembersLeadsDbVersions'])) { $addOns[GLM_MEMBERS_LEADS_PLUGIN_SLUG]['database'] = array( - 'dbPrefix' => GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX, - 'dbCurrentVersion' => GLM_MEMBERS_LEADS_PLUGIN_DB_VERSION, + 'dbPrefix' => GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX, + 'dbCurrentVersion' => GLM_MEMBERS_LEADS_PLUGIN_DB_VERSION, 'dbActiveVersionOption' => GLM_MEMBERS_LEADS_PLUGIN_ACTIVE_DB_OPTION, - 'dbScriptPath' => GLM_MEMBERS_LEADS_PLUGIN_DB_SCRIPTS, - 'dbVersions' => $GLOBALS['glmMembersLeadsDbVersions'] + 'dbScriptPath' => GLM_MEMBERS_LEADS_PLUGIN_DB_SCRIPTS, + 'dbVersions' => $GLOBALS['glmMembersLeadsDbVersions'] ); } else { $addOns[GLM_MEMBERS_LEADS_PLUGIN_SLUG]['database'] = false; diff --git a/models/admin/leads/index.php b/models/admin/leads/index.php index bbea9af..9f03991 100644 --- a/models/admin/leads/index.php +++ b/models/admin/leads/index.php @@ -85,6 +85,7 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry public function modelAction($actionData = false) { $option = false; + $user_can_edit_leads = false; // Get any provided option if ( isset( $_REQUEST['option'] ) ) { @@ -136,6 +137,14 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry // build the $where part $where_parts = array(); + if ( $this->config['loggedInUser']['contactUser'] ) { + $where_parts[] = "T.lead_id IN ( + SELECT id + FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "leads + WHERE member_ok = true)"; + } else { + $user_can_edit_leads = true; + } if ( $search_params['company'] ) { $where_parts[] = "org = '" . esc_sql( $search_params['company'] ) . "'"; } @@ -155,16 +164,20 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry $where_parts[] = implode( ' OR ', $name_parts ); } if ( $search_params['interests'] ) { - // Search for the interest. - // Checks the tables for lead_interests. + /* + * Search for the interest. + * Checks the tables for lead_interests. + */ $where_parts[] = " T.id IN ( SELECT lead_entry_id FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_interests WHERE interest_id IN ( " . implode( ", ", array_keys( $search_params['interests'] ) ) . " ) ) "; } if ( $search_params['from_date'] && $search_params['to_date'] ) { - // We get a date from the form in mm/dd/YYYY format. - // Here in mysql we reformat the date using STR_TO_DATE function. + /* + * We get a date from the form in mm/dd/YYYY format. + * Here in mysql we reformat the date using STR_TO_DATE function. + */ $where_parts[] = "T.date_submitted BETWEEN STR_TO_DATE('{$search_params['from_date']}', '%m/%d/%Y') " . "AND STR_TO_DATE('{$search_params['to_date']}', '%m/%d/%Y')"; } else if ( $search_params['from_date'] ) { @@ -195,6 +208,9 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry switch ( $option ) { case 'update': + if ( $this->config['loggedInUser']['contactUser'] ) { + break; + } $entry = $this->updateEntry( $this->entryId ); // Update the Entries Interests. First remove them. @@ -214,26 +230,39 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry ), array( '%d', '%d' ) ); + var_dump( $this->wpdb->insert_id ); } } echo '
$_POST: ' . print_r($_POST, true) . '
'; case 'edit': + if ( $this->config['loggedInUser']['contactUser'] ) { + break; + } + // Get the record for this Entry. $entry = $this->editEntry( $this->entryId ); + // Use the edit view file. $view = 'edit.html'; + // Initialize the $lead_ints array. $lead_ints = array(); + + // Find all lead_interests by the lead_entry_id. $sql = " SELECT * FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_interests WHERE lead_entry_id = " . $this->entryId; $lead_interests = $this->wpdb->get_results( $sql, ARRAY_A ); + + // Sort the lead interests into $lead_ints array if ( $lead_interests ) { foreach ( $lead_interests as $interest ) { - $lead_ints[] = $interest['id']; + $lead_ints[] = $interest['interest_id']; } } + + // Setup the $grouped_interests array for the edit page. if ( $groups ) { foreach ( $groups as $group ) { $intData = new GlmDataInterests( $this->wpdb, $this->config ); @@ -243,10 +272,10 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry $grouped_interests[$group_title] = $intData->getList( "group_id = " . $group['id'], 'title' ); } if ( $grouped_interests ) { - foreach ( $grouped_interests as $group_name => &$interests ) { - foreach ( $interests as $key => &$interest ) { + foreach ( $grouped_interests as $group_name => $interests ) { + foreach ( $interests as $key => $interest ) { $grouped_interests[$group_name][$key]['selected'] - = ( isset( $lead_ints[$key] ) ) ? 1 : 0; + = ( in_array( $key, $lead_ints ) ) ? 1 : 0; } } } @@ -269,8 +298,8 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry $grouped_interests[$group_title] = $intData->getList( "group_id = " . $group['id'], 'title' ); } if ( $grouped_interests ) { - foreach ( $grouped_interests as $group_name => &$interests ) { - foreach ( $interests as $key => &$interest ) { + foreach ( $grouped_interests as $group_name => $interests ) { + foreach ( $interests as $key => $interest ) { $grouped_interests[$group_name][$key]['selected'] = ( isset( $search_params['interests'][$key] ) && $search_params['interests'][$key] ) ? 1 : 0; } @@ -280,15 +309,15 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry $view = 'index.html'; // Compile template data $templateData = array( - 'search_params' => $search_params, - 'request' => print_r( $_REQUEST, true ), - 'leads' => $leads, - 'grouped_interests' => $grouped_interests, + 'user_can_edit_leads' => $user_can_edit_leads, + 'search_params' => $search_params, + 'request' => print_r( $_REQUEST, true ), + 'leads' => $leads, + 'grouped_interests' => $grouped_interests, ); break; } - // Return status, any suggested view, and any data to controller return array( 'status' => true, diff --git a/setup/adminMenus.php b/setup/adminMenus.php index 61b2c74..33e395f 100644 --- a/setup/adminMenus.php +++ b/setup/adminMenus.php @@ -59,3 +59,15 @@ add_submenu_page( 'glm-members-admin-menu-leads-index', function() {$this->controller('leads');} ); + +// If a contact is logged in (ownEntity isn't false), add Contact Profile menu item +if ( $this->config['loggedInUser']['contactUser'] ) { + add_submenu_page( + $mainMenuSlug, + 'Leads', + 'Leads', + 'glm_members_edit_my_entity', + 'glm-members-admin-menu-leads-index', + function() {$this->controller('leads');} + ); +} diff --git a/setup/databaseScripts/create_database_V0.0.1.sql b/setup/databaseScripts/create_database_V0.0.2.sql similarity index 97% rename from setup/databaseScripts/create_database_V0.0.1.sql rename to setup/databaseScripts/create_database_V0.0.2.sql index a8339e7..475943c 100644 --- a/setup/databaseScripts/create_database_V0.0.1.sql +++ b/setup/databaseScripts/create_database_V0.0.2.sql @@ -32,14 +32,13 @@ CREATE TABLE {prefix}sources ( code TINYTEXT NULL, -- Code supplied by form to indicate source form_id INT NULL, enabled BOOLEAN NULL, - groups TEXT NULL, PRIMARY KEY (id), INDEX (code(20)) ); ---- --- Lead Entry - Information on a single submission of lead infomration +-- Lead Entry - Information on a single submission of lead information CREATE TABLE {prefix}lead_entry ( id INT NOT NULL AUTO_INCREMENT, source_id SMALLINT NULL, -- Pointer to sources entry - Form used to submit lead data @@ -106,6 +105,7 @@ CREATE TABLE {prefix}lead_interests ( -- lead_searches - Memorized search configurations CREATE TABLE {prefix}searches ( id INT NOT NULL AUTO_INCREMENT, + user_id INT NOT NULL, -- The wordpress user id title TINYTEXT NULL, -- Title for this search configuration search TEXT NULL, -- Serialized array of search parameters date_created DATE NULL, -- Date the search type was created diff --git a/setup/databaseScripts/dbVersions.php b/setup/databaseScripts/dbVersions.php index bf6b86f..f3dc944 100644 --- a/setup/databaseScripts/dbVersions.php +++ b/setup/databaseScripts/dbVersions.php @@ -14,6 +14,7 @@ */ $glmMembersLeadsDbVersions = array( - '0.0.1' => array('version' => '0.0.1', 'tables' => 10, 'date' => '6/8/2016') + '0.0.1' => array('version' => '0.0.1', 'tables' => 10, 'date' => '6/8/2016'), + '0.0.2' => array('version' => '0.0.2', 'tables' => 10, 'date' => '7/12/2016'), ); diff --git a/setup/databaseScripts/update_database_V0.0.2.sql b/setup/databaseScripts/update_database_V0.0.2.sql new file mode 100644 index 0000000..6d02d1c --- /dev/null +++ b/setup/databaseScripts/update_database_V0.0.2.sql @@ -0,0 +1,21 @@ +-- Gaslight Media Members Database - Events Add-On +-- File Created: 12/09/14 15:27:15 +-- Database Version: 0.0.9 +-- Database Update From Previous Version Script +-- +-- To permit each query below to be executed separately, +-- all queries must be separated by a line with four dashses + + +-- Add form_id +ALTER TABLE {prefix}sources ADD COLUMN form_id INT; + +---- + +-- Add enabled +ALTER TABLE {prefix}sources ADD COLUMN enabled BOOLEAN; + +---- + +-- Add groups +ALTER TABLE {prefix}searches ADD COLUMN user_id INT; diff --git a/setup/frontHooks.php b/setup/frontHooks.php index 760a2b6..6a00168 100644 --- a/setup/frontHooks.php +++ b/setup/frontHooks.php @@ -30,14 +30,26 @@ require_once GLM_MEMBERS_LEADS_PLUGIN_CLASS_PATH.'/data/dataInterestGroups.php'; // Inject things into gravity forms fields - This intercepts all GravityForms before displaying them add_filter( 'gform_pre_render', function( $form ) { + $form_enabled = false; + /* + * Check to see if the form is in the sources table + */ + $result = $this->wpdb->get_row( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "sources + WHERE form_id = %d", + $form['id'] + ), + ARRAY_A + ); foreach ( $form['fields'] as $k => $v ) { $inputs = $choices = array(); if ( $v['type'] == 'leads' ) { + $form_enabled = true; // check the inputName to see if it matches a group - $groupData = new GlmDataInterestGroups( $this->wpdb, $this->config ); - $groups = $groupData->getList(); - //echo '
$groups: ' . print_r($groups, true) . '
'; - //echo '
$v: ' . print_r($v, true) . '
'; + $groupData = new GlmDataInterestGroups( $this->wpdb, $this->config ); + $groups = $groupData->getList(); if ( $groups ) { foreach ( $groups as $group ) { if ( $v['inputName'] == $group['title'] ) { @@ -46,7 +58,6 @@ add_filter( 'gform_pre_render', function( $form ) { "group_id = " . $group['id'], 'title' ); - //echo '
$interests: ' . print_r($interests, true) . '
'; if ( $interests ) { foreach ( $interests as $interest ) { $inputs[] = array( @@ -69,10 +80,47 @@ add_filter( 'gform_pre_render', function( $form ) { } } - + if ( !$result ) { + $this->wpdb->insert( + GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'sources', + array( + 'title' => 'Gravity Form - ' . $form['title'], + 'form_id' => $form['id'], + 'enabled' => $form_enabled + ), + array( '%s', '%d', '%d' ) + ); + } + // Return the form. return $form; }); add_action( 'gform_after_submission', function( $entry, $form ){ + /* + * Get the source id for the form. + */ + $result = $this->wpdb->get_row( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "sources + WHERE form_id = %d", + $form['id'] + ), + ARRAY_A + ); + if ( $result ) { + $source_id = $result['id']; + } else { + $this->wpdb->insert( + GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'sources', + array( + 'title' => 'Gravity Form - ' . $form['title'], + 'form_id' => $form['id'], + 'enabled' => $form_enabled + ), + array( '%s', '%d', '%d' ) + ); + $source_id = $this->wpdb->insert_id; + } $leads_fields = array(); $mapped_keys = array( 'fname', @@ -177,7 +225,7 @@ add_action( 'gform_after_submission', function( $entry, $form ){ $lead_entry = $this->wpdb->insert( GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_entry', // table array( - 'source_id' => $entry['form_id'], + 'source_id' => $source_id, 'lead_id' => $lead_id, 'fname' => ( isset( $glm_leads_entry['fname'] ) ) ? $glm_leads_entry['fname'] : '', 'lname' => ( isset( $glm_leads_entry['lname'] ) ) ? $glm_leads_entry['lname'] : '', diff --git a/views/admin/leads/index.html b/views/admin/leads/index.html index ee944bf..98ea4db 100644 --- a/views/admin/leads/index.html +++ b/views/admin/leads/index.html @@ -16,11 +16,11 @@ From - + To - + {foreach $grouped_interests as $group_name => $group} @@ -39,6 +39,9 @@ {if $leads} +
+ +
@@ -47,7 +50,11 @@ {foreach $leads as $lead} - + @@ -55,4 +62,11 @@
Contact
{$lead.fname} {$lead.lname} + {if $user_can_edit_leads}{/if} + {$lead.fname} {$lead.lname} + {if $user_can_edit_leads}{/if} + {$lead.org} {$lead.date_submitted}
{/if} + + {include file='admin/footer.html'} -- 2.17.1