Update the search form and adding the source table entries.
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 12 Jul 2016 19:10:36 +0000 (15:10 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 12 Jul 2016 19:10:36 +0000 (15:10 -0400)
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
models/admin/leads/index.php
setup/adminMenus.php
setup/databaseScripts/create_database_V0.0.1.sql [deleted file]
setup/databaseScripts/create_database_V0.0.2.sql [new file with mode: 0644]
setup/databaseScripts/dbVersions.php
setup/databaseScripts/update_database_V0.0.2.sql [new file with mode: 0644]
setup/frontHooks.php
views/admin/leads/index.html

index 1254036..cd769a3 100644 (file)
--- 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;
index bbea9af..9f03991 100644 (file)
@@ -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 '<pre>$_POST: ' . print_r($_POST, true) . '</pre>';
 
         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,
index 61b2c74..33e395f 100644 (file)
@@ -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.1.sql
deleted file mode 100644 (file)
index a8339e7..0000000
+++ /dev/null
@@ -1,146 +0,0 @@
--- Gaslight Media Members Database - Leads
--- File Created: 12/02/15 15:27:15
--- Database Version: 0.0.1
--- Database Creation Script
---
--- This file is called to create a new set of tables for this
--- add-on for the most receint database version for this add-on.
---
--- There should only be one such file in this directory
---
--- To permit each query below to be executed separately,
--- all queries must be separated by a line with four dashes
-
-
--- Leads
-CREATE TABLE {prefix}leads (
-  id INT NOT NULL AUTO_INCREMENT,
-  email TINYTEXT NULL,                      -- Email address of lead
-  mail_ok BOOLEAN NULL,                     -- OK to send them E-Mail
-  member_ok BOOLEAN NULL,                   -- OK to have members contact lead
-  created DATETIME NULL,                    -- Timestamp lead was first added
-  PRIMARY KEY (id),
-  INDEX (email(20))
-);
-
-----
-
--- Sources - info on form or method via which lead can be submitted
-CREATE TABLE {prefix}sources (
-  id INT NOT NULL AUTO_INCREMENT,
-  title TINYTEXT NULL,                      -- Title/Name of source for reference
-  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
-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
-  lead_id INTEGER NULL,                     -- Pointer to lead - all submissions for same E-Mail address point to same leads entry
-  fname TINYTEXT NULL,                      -- First Name
-  lname TINYTEXT NULL,                      -- Last Name
-  org TINYTEXT NULL,                        -- Organization name
-  addr1 TINYTEXT NULL,                      -- Address line 1
-  addr2 TINYTEXT NULL,                      -- Address line 2
-  city TINYTEXT NULL,                       -- City name
-  state TINYTEXT NULL,                      -- State/Province - as submitted by Gravityforms or whatever (not state code)
-  zip TINYTEXT NULL,                        -- ZIP/Postal code
-  country TINYTEXT NULL,                    -- Country name - as submitted by form (not country code)
-  phone TINYTEXT NULL,                      -- Primary phone #
-  phone2 TINYTEXT NULL,                     -- Alternate phone #
-  fax TINYTEXT NULL,                        -- Fax #
-  how_heard SMALLINT NULL,                  -- Pointer to how_heard table entry
-  visit_date DATE NULL,                     -- Anticipated date of visit
-  date_submitted DATE NULL,                 -- Date this information was submitted
-  user_trace_info TINYTEXT NULL,            -- User IP address and other identifying network info (pos referrer)
-  PRIMARY KEY (id),
-  INDEX (source_id),
-  INDEX (lead_id),
-  INDEX (fname(20)),
-  INDEX (lname(20)),
-  INDEX (visit_date),
-  INDEX (date_submitted)
-);
-
-----
-
--- Interest Groups
-CREATE TABLE {prefix}interest_groups (
-  id INT NOT NULL AUTO_INCREMENT,
-  title TINYTEXT NULL,                      -- Group name
-  PRIMARY KEY (id)
-);
-
-----
-
--- Interests - Used to buil Interest fields in forms - Forms use ID as value
-CREATE TABLE {prefix}interests (
-  id INT NOT NULL AUTO_INCREMENT,
-  title TINYTEXT NULL,                      -- Title
-  group_id SMALLINT NULL,                   -- Pointer to interest group
-  PRIMARY KEY (id),
-  INDEX (group_id)
-);
-
-----
-
--- Lead Interests - many to one links to lead-entry table
-CREATE TABLE {prefix}lead_interests (
-  id INT NOT NULL AUTO_INCREMENT,
-  interest_id SMALLINT NULL,                -- Pointer to Interest table
-  lead_entry_id INTEGER NULL,              -- Pointer to lead_entry table
-  PRIMARY KEY (id),
-  INDEX (interest_id),
-  INDEX (lead_entry_id)
-);
-
-----
-
--- lead_searches - Memorized search configurations
-CREATE TABLE {prefix}searches (
-  id INT NOT NULL AUTO_INCREMENT,
-  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
-  PRIMARY KEY (id)
-);
-
-----
-
--- Lead Stats Date Data - Totals of lead stats for 1 day - Preserved for 2 years
-CREATE TABLE {prefix}lead_stats_date (
-  id INT NOT NULL AUTO_INCREMENT,
-  stat_date DATE NULL,                      -- Date for which these stats are accumulated
-  leads_count INTEGER NULL,                 -- Number of leads
-  PRIMARY KEY (id),
-  INDEX (stat_date)
-);
-
-----
-
--- Lead Stats Week Data - Totals of lead stats for 1 week - Preserved for 2 years - (generated daily?)
-CREATE TABLE {prefix}lead_stats_week (
-  id INT NOT NULL AUTO_INCREMENT,
-  stat_week DATE NULL,                      -- First date of week for which these stats are accumulated
-  leads_count INTEGER NULL,                 -- Number of leads
-  PRIMARY KEY (id),
-  INDEX (stat_week)
-);
-
-----
-
--- Lead Stats Month Data - Totals of lead stats for 1 month - Preserved indefinately - (generated daily?)
-CREATE TABLE {prefix}lead_stats_month (
-  id INT NOT NULL AUTO_INCREMENT,
-  stat_month DATE NULL,                     -- First date of month for which these stats are accumulated
-  leads_count INTEGER NULL,                 -- Number of leads
-  PRIMARY KEY (id),
-  INDEX (stat_month)
-);
diff --git a/setup/databaseScripts/create_database_V0.0.2.sql b/setup/databaseScripts/create_database_V0.0.2.sql
new file mode 100644 (file)
index 0000000..475943c
--- /dev/null
@@ -0,0 +1,146 @@
+-- Gaslight Media Members Database - Leads
+-- File Created: 12/02/15 15:27:15
+-- Database Version: 0.0.1
+-- Database Creation Script
+--
+-- This file is called to create a new set of tables for this
+-- add-on for the most receint database version for this add-on.
+--
+-- There should only be one such file in this directory
+--
+-- To permit each query below to be executed separately,
+-- all queries must be separated by a line with four dashes
+
+
+-- Leads
+CREATE TABLE {prefix}leads (
+  id INT NOT NULL AUTO_INCREMENT,
+  email TINYTEXT NULL,                      -- Email address of lead
+  mail_ok BOOLEAN NULL,                     -- OK to send them E-Mail
+  member_ok BOOLEAN NULL,                   -- OK to have members contact lead
+  created DATETIME NULL,                    -- Timestamp lead was first added
+  PRIMARY KEY (id),
+  INDEX (email(20))
+);
+
+----
+
+-- Sources - info on form or method via which lead can be submitted
+CREATE TABLE {prefix}sources (
+  id INT NOT NULL AUTO_INCREMENT,
+  title TINYTEXT NULL,                      -- Title/Name of source for reference
+  code TINYTEXT NULL,                       -- Code supplied by form to indicate source
+  form_id INT NULL,
+  enabled BOOLEAN NULL,
+  PRIMARY KEY (id),
+  INDEX (code(20))
+);
+
+----
+
+-- 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
+  lead_id INTEGER NULL,                     -- Pointer to lead - all submissions for same E-Mail address point to same leads entry
+  fname TINYTEXT NULL,                      -- First Name
+  lname TINYTEXT NULL,                      -- Last Name
+  org TINYTEXT NULL,                        -- Organization name
+  addr1 TINYTEXT NULL,                      -- Address line 1
+  addr2 TINYTEXT NULL,                      -- Address line 2
+  city TINYTEXT NULL,                       -- City name
+  state TINYTEXT NULL,                      -- State/Province - as submitted by Gravityforms or whatever (not state code)
+  zip TINYTEXT NULL,                        -- ZIP/Postal code
+  country TINYTEXT NULL,                    -- Country name - as submitted by form (not country code)
+  phone TINYTEXT NULL,                      -- Primary phone #
+  phone2 TINYTEXT NULL,                     -- Alternate phone #
+  fax TINYTEXT NULL,                        -- Fax #
+  how_heard SMALLINT NULL,                  -- Pointer to how_heard table entry
+  visit_date DATE NULL,                     -- Anticipated date of visit
+  date_submitted DATE NULL,                 -- Date this information was submitted
+  user_trace_info TINYTEXT NULL,            -- User IP address and other identifying network info (pos referrer)
+  PRIMARY KEY (id),
+  INDEX (source_id),
+  INDEX (lead_id),
+  INDEX (fname(20)),
+  INDEX (lname(20)),
+  INDEX (visit_date),
+  INDEX (date_submitted)
+);
+
+----
+
+-- Interest Groups
+CREATE TABLE {prefix}interest_groups (
+  id INT NOT NULL AUTO_INCREMENT,
+  title TINYTEXT NULL,                      -- Group name
+  PRIMARY KEY (id)
+);
+
+----
+
+-- Interests - Used to buil Interest fields in forms - Forms use ID as value
+CREATE TABLE {prefix}interests (
+  id INT NOT NULL AUTO_INCREMENT,
+  title TINYTEXT NULL,                      -- Title
+  group_id SMALLINT NULL,                   -- Pointer to interest group
+  PRIMARY KEY (id),
+  INDEX (group_id)
+);
+
+----
+
+-- Lead Interests - many to one links to lead-entry table
+CREATE TABLE {prefix}lead_interests (
+  id INT NOT NULL AUTO_INCREMENT,
+  interest_id SMALLINT NULL,                -- Pointer to Interest table
+  lead_entry_id INTEGER NULL,              -- Pointer to lead_entry table
+  PRIMARY KEY (id),
+  INDEX (interest_id),
+  INDEX (lead_entry_id)
+);
+
+----
+
+-- 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
+  PRIMARY KEY (id)
+);
+
+----
+
+-- Lead Stats Date Data - Totals of lead stats for 1 day - Preserved for 2 years
+CREATE TABLE {prefix}lead_stats_date (
+  id INT NOT NULL AUTO_INCREMENT,
+  stat_date DATE NULL,                      -- Date for which these stats are accumulated
+  leads_count INTEGER NULL,                 -- Number of leads
+  PRIMARY KEY (id),
+  INDEX (stat_date)
+);
+
+----
+
+-- Lead Stats Week Data - Totals of lead stats for 1 week - Preserved for 2 years - (generated daily?)
+CREATE TABLE {prefix}lead_stats_week (
+  id INT NOT NULL AUTO_INCREMENT,
+  stat_week DATE NULL,                      -- First date of week for which these stats are accumulated
+  leads_count INTEGER NULL,                 -- Number of leads
+  PRIMARY KEY (id),
+  INDEX (stat_week)
+);
+
+----
+
+-- Lead Stats Month Data - Totals of lead stats for 1 month - Preserved indefinately - (generated daily?)
+CREATE TABLE {prefix}lead_stats_month (
+  id INT NOT NULL AUTO_INCREMENT,
+  stat_month DATE NULL,                     -- First date of month for which these stats are accumulated
+  leads_count INTEGER NULL,                 -- Number of leads
+  PRIMARY KEY (id),
+  INDEX (stat_month)
+);
index bf6b86f..f3dc944 100644 (file)
@@ -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 (file)
index 0000000..6d02d1c
--- /dev/null
@@ -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;
index 760a2b6..6a00168 100644 (file)
@@ -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 '<pre>$groups: ' . print_r($groups, true) . '</pre>';
-            //echo '<pre>$v: ' . print_r($v, true) . '</pre>';
+            $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 '<pre>$interests: ' . print_r($interests, true) . '</pre>';
                         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']   : '',
index ee944bf..98ea4db 100644 (file)
         </tr>
         <tr>
             <td>From</td>
-            <td><input type="text" name="from_date" value="{$search_params.from_date}"></td>
+            <td><input type="text" id="from_date" name="from_date" value="{$search_params.from_date}"></td>
         </tr>
         <tr>
             <td>To</td>
-            <td><input type="text" name="to_date" value="{$search_params.to_date}"></td>
+            <td><input type="text" id="to_date" name="to_date" value="{$search_params.to_date}"></td>
         </tr>
         {foreach $grouped_interests as $group_name => $group}
             <tr>
@@ -39,6 +39,9 @@
 </form>
 
 {if $leads}
+    <form action="{$thisUrl}?page={$thisPage}">
+        <input type="submit" value="Download" />
+    </form>
     <table class="glm-admin-table">
         <tr>
             <th>Contact</th>
         </tr>
         {foreach $leads as $lead}
         <tr>
-            <td> <a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=edit&entry={$lead.id}">{$lead.fname} {$lead.lname}</a></td>
+            <td>
+                {if $user_can_edit_leads}<a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=edit&entry={$lead.id}">{/if}
+                    {$lead.fname} {$lead.lname}
+                {if $user_can_edit_leads}</a>{/if}
+            </td>
             <td> {$lead.org} </td>
             <td> {$lead.date_submitted} </td>
         </tr>
     </table>
 {/if}
 
+<script>
+jQuery(document).ready(function($){
+    $('#from_date').datepicker();
+    $('#to_date').datepicker();
+});
+</script>
+
 {include file='admin/footer.html'}