Adding edit entry screen to admin leads
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 11 Jul 2016 20:55:16 +0000 (16:55 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 11 Jul 2016 20:55:16 +0000 (16:55 -0400)
Working still on the interest edit part.
I have the other fields saving their info.
Using just the data abstract for the edit entry form.

classes/data/dataInterests.php
classes/data/dataLeadEntry.php
models/admin/leads/index.php
setup/validActions.php
views/admin/leads/edit.html [new file with mode: 0644]
views/admin/leads/index.html

index 640e37a..b4c5693 100644 (file)
@@ -140,7 +140,7 @@ class GlmDataInterests extends GlmDataAbstract
 
     }
 
-    /*
+    /**
      * Entry Post Processing Call-Back Method
      *
      * Perform post-processing for all result entries.
index 428f2fa..ef34334 100644 (file)
@@ -228,6 +228,7 @@ class GlmDataLeadEntry extends GlmDataAbstract
                 'type'      => 'text',
                 'required'  => false,
                 'unique'    => false,
+                'view_only' => true,
                 'use'       => 'a',
             ),
 
@@ -236,6 +237,7 @@ class GlmDataLeadEntry extends GlmDataAbstract
                 'type'      => 'text',
                 'required'  => false,
                 'unique'    => false,
+                'view_only' => true,
                 'use'       => 'a',
             ),
 
@@ -243,7 +245,7 @@ class GlmDataLeadEntry extends GlmDataAbstract
 
     }
 
-    /*
+    /**
      * Entry Post Processing Call-Back Method
      *
      * Perform post-processing for all result entries.
index d70b5c8..bbea9af 100644 (file)
@@ -36,6 +36,7 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
      */
     public $config;
 
+    public $entryId = false;
     /*
      * Constructor
      *
@@ -83,6 +84,24 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
 
     public function modelAction($actionData = false)
     {
+        $option = false;
+
+        // Get any provided option
+        if ( isset( $_REQUEST['option'] ) ) {
+            $option = $_REQUEST['option'];
+        }
+
+        // Get event ID if supplied
+        if (isset($_REQUEST['entry'])) {
+
+            // Make sure it's numeric
+            $this->entryId = ( $_REQUEST['entry'] - 0 );
+
+            if ( $this->entryId <= 0 ) {
+                $this->entryId = false;
+            }
+
+        }
         // Check for a search being done.
         if ( isset( $_REQUEST['search'] ) ) {
             $search = filter_var( $_REQUEST['search'], FILTER_VALIDATE_BOOLEAN);
@@ -149,7 +168,9 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
                 $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'] ) {
+                $where_parts[] = "T.date_submitted >= STR_TO_DATE('{$search_params['from_date']}', '%m/%d/%Y')";
             } else  if ( $search_params['to_date'] ) {
+                $where_parts[] = "T.date_submitted <= STR_TO_DATE('{$search_params['to_date']}', '%m/%d/%Y')";
             }
             $where = implode( ' AND ', $where_parts );
             //echo '<pre>$where: ' . print_r( $where, true ) . '</pre>';
@@ -166,42 +187,113 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             );
         }
 
+        $interests = array();
         $groupData = new GlmDataInterestGroups( $this->wpdb, $this->config );
         $groups    = $groupData->getList();
 
         $grouped_interests = array();
 
-        if ( $groups ) {
-            foreach ( $groups as $group ) {
-                $intData     = new GlmDataInterests( $this->wpdb, $this->config );
-                $group_parts = explode( '_', $group['title'] );
-                $group_parts = array_map( function($item){ return ucfirst( $item ); }, $group_parts );
-                $group_title = implode( ' ', $group_parts );
-                $grouped_interests[$group_title] = $intData->getList( "group_id = " . $group['id'], 'title' );
+        switch ( $option ) {
+        case 'update':
+            $entry = $this->updateEntry( $this->entryId );
+
+            // Update the Entries Interests. First remove them.
+            $this->wpdb->delete(
+                GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_interests",
+                array( 'lead_entry_id' => $this->entryId ),
+                array( '%d' )
+            );
+            // then add the ones needed.
+            if ( isset( $_POST['interests'] ) && !empty( $_POST['interests'] ) ) {
+                foreach ( $_POST['interests'] as $interest_id => $new_interest ) {
+                    $this->wpdb->insert(
+                        GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_interests",
+                        array(
+                            'lead_entry_id' => $this->entryId,
+                            'interest_id'   => $interest_id
+                        ),
+                        array( '%d', '%d' )
+                    );
+                }
             }
-            if ( $grouped_interests ) {
-                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;
+            echo '<pre>$_POST: ' . print_r($_POST, true) . '</pre>';
+
+        case 'edit':
+            $entry = $this->editEntry( $this->entryId );
+
+            $view = 'edit.html';
+
+            $lead_ints = array();
+            $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 );
+            if ( $lead_interests ) {
+                foreach ( $lead_interests as $interest ) {
+                    $lead_ints[] = $interest['id'];
+                }
+            }
+            if ( $groups ) {
+                foreach ( $groups as $group ) {
+                    $intData     = new GlmDataInterests( $this->wpdb, $this->config );
+                    $group_parts = explode( '_', $group['title'] );
+                    $group_parts = array_map( function($item){ return ucfirst( $item ); }, $group_parts );
+                    $group_title = implode( ' ', $group_parts );
+                    $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 ) {
+                            $grouped_interests[$group_name][$key]['selected']
+                                = ( isset( $lead_ints[$key] ) ) ? 1 : 0;
+                        }
+                    }
+                }
+            }
+            // Compile template data
+            $templateData = array(
+                'entry'             => $entry,
+                'lead_interests'    => $lead_interests,
+                'grouped_interests' => $grouped_interests,
+            );
+            break;
+
+        default:
+            if ( $groups ) {
+                foreach ( $groups as $group ) {
+                    $intData     = new GlmDataInterests( $this->wpdb, $this->config );
+                    $group_parts = explode( '_', $group['title'] );
+                    $group_parts = array_map( function($item){ return ucfirst( $item ); }, $group_parts );
+                    $group_title = implode( ' ', $group_parts );
+                    $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 ) {
+                            $grouped_interests[$group_name][$key]['selected']
+                                = ( isset( $search_params['interests'][$key] ) && $search_params['interests'][$key] ) ? 1 : 0;
+                        }
                     }
                 }
             }
+            $view = 'index.html';
+            // Compile template data
+            $templateData = array(
+                'search_params'     => $search_params,
+                'request'           => print_r( $_REQUEST, true ),
+                'leads'             => $leads,
+                'grouped_interests' => $grouped_interests,
+            );
+            break;
         }
 
-        // Compile template data
-        $templateData = array(
-            'search_params'     => $search_params,
-            'request'           => print_r( $_REQUEST, true ),
-            'leads'             => $leads,
-            'grouped_interests' => $grouped_interests,
-        );
 
         // Return status, any suggested view, and any data to controller
         return array(
                 'status'        => true,
                 'modelRedirect' => false,
-                'view'          => 'admin/leads/index.html',
+                'view'          => 'admin/leads/' . $view,
                 'data'          => $templateData
         );
 
index bb4a1be..33b89fb 100644 (file)
 $glmMembersLeadsAddOnValidActions = array(
     'adminActions' => array(
         'leads' => array(
-            'index' => GLM_MEMBERS_LEADS_PLUGIN_SLUG
+            'index' => GLM_MEMBERS_LEADS_PLUGIN_SLUG,
+            'edit'  => GLM_MEMBERS_LEADS_PLUGIN_SLUG,
         ),
         'management' => array(
-            'leads' => GLM_MEMBERS_LEADS_PLUGIN_SLUG
+            'leads' => GLM_MEMBERS_LEADS_PLUGIN_SLUG,
         ),
         'settings' => array(
-            'leads' => GLM_MEMBERS_LEADS_PLUGIN_SLUG
-        )
+            'leads' => GLM_MEMBERS_LEADS_PLUGIN_SLUG,
+        ),
     ),
 );
 
-?>
\ No newline at end of file
diff --git a/views/admin/leads/edit.html b/views/admin/leads/edit.html
new file mode 100644 (file)
index 0000000..e900b77
--- /dev/null
@@ -0,0 +1,79 @@
+{include file='admin/leads/header.html'}
+
+{if $entry.status}
+<form action="{$thisUrl}?page={$thisPage}" method="post">
+    <input type="hidden" name="glm_action" value="{$thisAction}" />
+    <input type="hidden" name="option" value="update" />
+    <input type="hidden" name="entry" value="{$entry.fieldData.id}" />
+    <input type="hidden" name="lead_id" value="{$entry.fieldData.lead_id.value}" />
+    <table class="glm-admin-table glm-lead-table">
+        <tr>
+            <th>Company</th>
+            <td><input type="text" name="org" value="{$entry.fieldData.org}" /></td>
+        </tr>
+        <tr>
+            <th>First Name</th>
+            <td><input type="text" name="fname" value="{$entry.fieldData.fname}" /></td>
+        </tr>
+        <tr>
+            <th>Last Name</th>
+            <td><input type="text" name="lname" value="{$entry.fieldData.lname}" /></td>
+        </tr>
+        <tr>
+            <th>Address 1</th>
+            <td><input type="text" name="addr1" value="{$entry.fieldData.addr1}" /></td>
+        </tr>
+        <tr>
+            <th>Address 2</th>
+            <td><input type="text" name="addr2" value="{$entry.fieldData.addr2}" /></td>
+        </tr>
+        <tr>
+            <th>City</th>
+            <td><input type="text" name="city" value="{$entry.fieldData.city}" /></td>
+        </tr>
+        <tr>
+            <th>State</th>
+            <td><input type="text" name="state" value="{$entry.fieldData.state}" /></td>
+        </tr>
+        <tr>
+            <th>ZIP</th>
+            <td><input type="text" name="zip" value="{$entry.fieldData.zip}" /></td>
+        </tr>
+        <tr>
+            <th>Country</th>
+            <td><input type="text" name="country" value="{$entry.fieldData.country}" /></td>
+        </tr>
+        <tr>
+            <th>Phone</th>
+            <td><input type="text" name="phone" value="{$entry.fieldData.phone}" /></td>
+        </tr>
+        <tr>
+            <th>Phone 2</th>
+            <td><input type="text" name="phone2" value="{$entry.fieldData.phone2}" /></td>
+        </tr>
+        <tr>
+            <th>Fax</th>
+            <td><input type="text" name="fax" value="{$entry.fieldData.fax}" /></td>
+        </tr>
+        <tr>
+            <td colspan="2">
+                {foreach $grouped_interests as $group_name => $group}
+                <div>
+                    <strong>{$group_name}</strong>
+                    {foreach $group as $interest}
+                    <div>
+                        <label><input type="checkbox" name="interests[{$interest.id}]" {if $interest.selected} checked{/if}>{$interest.title}</label>
+                    </div>
+                    {/foreach}
+                </div>
+                {/foreach}
+            </td>
+        </tr>
+        <tr>
+            <td colspan="2"><input type="submit" value="Update Lead"></td>
+        </tr>
+    </table>
+</form>
+{/if}
+
+{include file='admin/footer.html'}
index f2e5d42..ee944bf 100644 (file)
 {if $leads}
     <table class="glm-admin-table">
         <tr>
-            <th>Company</th>
             <th>Contact</th>
+            <th>Company</th>
             <th>Submitted</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> {$lead.org} </td>
-            <td> {$lead.fname} {$lead.lname} </td>
             <td> {$lead.date_submitted} </td>
         </tr>
        {/foreach}