Add video import file.
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 24 Jan 2018 19:57:58 +0000 (14:57 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 24 Jan 2018 19:57:58 +0000 (14:57 -0500)
Adding ability to import for the member videos.

models/admin/import/index.php
views/admin/import/header.html
views/admin/import/videos.html [new file with mode: 0644]
views/admin/import/videosProcess.html [new file with mode: 0644]
views/admin/import/videosValidate.html [new file with mode: 0644]

index 2ac696c..49a49fa 100644 (file)
@@ -173,6 +173,10 @@ class GlmMembersAdmin_import_index
      */
     public $processingComplete = false;
 
+    public $totalVideos = 0;
+
+    public $videosProcessed = 0;
+
     /**
      * Constructor
      *
@@ -507,6 +511,77 @@ class GlmMembersAdmin_import_index
             $fileData = '<pre>$_FILES: ' . print_r( $_FILES, true ) . '</pre>';
             $clearData = ( filter_var( $_REQUEST['clear_data'], FILTER_VALIDATE_BOOLEAN ) );
 
+            break;
+
+        case 'videosValidate':
+            if ( isset( $_FILES ) ) {
+                move_uploaded_file( $_FILES['files_file']['tmp_name'], $uploadPath . '/videosData.csv' );
+            }
+            if ( is_file( $uploadPath . '/videosData.csv' ) ) {
+                $fileExists = true;
+            }
+            // validate the header line
+            $fData    = $this->readCSVFileHeaders( $uploadPath . '/videosData.csv' );
+            $validate = array( 'member_id', 'member_name', 'video_url' );
+            $isValid  = ( $fData == $validate );
+            // If the file validates then we're ready to process
+            $readyToProcess = $isValid;
+            // Set the view file
+            $view = 'videosValidate.html';
+            $fileData = '<pre>$_FILES: ' . print_r( $_FILES, true ) . '</pre>';
+            $clearData = ( filter_var( $_REQUEST['clear_data'], FILTER_VALIDATE_BOOLEAN ) );
+            break;
+
+        case 'videosProcess':
+            $view = 'videosProcess.html';
+            // Setup the member_info class
+            require_once GLM_MEMBERS_PLUGIN_PATH .  '/classes/data/dataMemberInfo.php';
+            $memberInfoObj = new GlmDataMemberInfo( $this->wpdb, $this->config );
+
+            if ( is_file( $uploadPath . '/videosData.csv' ) ) {
+                $videoData         = $this->readCSVFile( $uploadPath . '/videosData.csv' );
+                $this->totalVideos = count( $videoData );
+
+                foreach ( $videoData as $video ) {
+                    // Get the member id for this video
+                    $memberId = $this->wpdb->get_var(
+                        $this->wpdb->prepare(
+                            "SELECT id
+                               FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+                              WHERE old_member_id = %s",
+                            $video['member_id']
+                        )
+                    );
+                    if ( !$memberId ) {
+                        var_dump( 'no member id' );
+                        continue;
+                    }
+                    // Now that we have the member id we need to get the
+                    // id for the active member info record
+                    $memberInfoId = $memberInfoObj->getActiveInfoIdForMember( $memberId );
+                    // echo '<pre>$memberInfoId: ' . print_r( $memberInfoId, true ) . '</pre>';
+                    if ( !$memberInfoId ) {
+                        var_dump( 'no member info id' );
+                        continue;
+                    }
+
+                    // check the video url to make sure it is valid
+                    if ( $video_url = filter_var( $video['video_url'], FILTER_VALIDATE_URL ) ) {
+                        $this->videosProcessed++;
+                        $this->wpdb->update(
+                            GLM_MEMBERS_PLUGIN_DB_PREFIX . 'member_info',
+                            array( 'video_url' => $video_url ),
+                            array( 'id' => $memberInfoId ),
+                            array( '%s' ),
+                            array( '%d' )
+                        );
+                        // echo '<pre>' . $video['member_name'] . ': ' . htmlspecialchars( $video['video_url'] ) .'</pre>';
+                    }
+                }
+
+            }
+
+
             break;
 
         case 'photosProcess':
@@ -729,6 +804,27 @@ class GlmMembersAdmin_import_index
             }
             break;
 
+        case 'videos':
+            // Set the view file
+            $view = 'videos.html';
+            unset( $fileData );
+            if ( is_file( $uploadPath . '/videosData.csv' ) ) {
+                $fileExists = true;
+                $fileData['Videos'] = array(
+                    'field'  => 'videos_file',
+                    'name'   => 'videosData.csv',
+                    'exists' => true,
+                    'mtime'  => filemtime( $uploadPath . '/videosData.csv' ),
+                );
+            } else {
+                $fileData['Videos'] = array(
+                    'field'  => 'videos_file',
+                    'name'   => 'videosData.csv',
+                    'exists' => false,
+                );
+            }
+            break;
+
         case 'default':
         default:
             // Set the view file
@@ -757,6 +853,8 @@ class GlmMembersAdmin_import_index
             'totalFiles'      => $this->totalFiles,
             'photosProcessed' => $this->photosProcessed,
             'filesProcessed'  => $this->filesProcessed,
+            'totalVideos'     => $this->totalVideos,
+            'videosProcessed' => $this->videosProcessed,
             'completed'       => $this->processingComplete,
             'data'            => false,
             'fileData'        => $fileData,
index 0de87f7..621570d 100644 (file)
@@ -12,6 +12,9 @@
         <a
             href="{$thisUrl}?page={$thisPage}&glm_action=index&option=files"
             class="nav-tab{if $option==files or $option===filesValidate} nav-tab-active{/if}">Import Files</a>
+        <a
+            href="{$thisUrl}?page={$thisPage}&glm_action=index&option=videos"
+            class="nav-tab{if $option==videos or $option===videosValidate} nav-tab-active{/if}">Import Videos</a>
         {/if}
 
         {foreach $addOnTabs as $a}
diff --git a/views/admin/import/videos.html b/views/admin/import/videos.html
new file mode 100644 (file)
index 0000000..e65dbc7
--- /dev/null
@@ -0,0 +1,45 @@
+{include file='admin/import/header.html'}
+
+    <h2>Data Import: Videos</h2>
+    <form action="{$thisUrl}?page={$thisPage}" method="post" enctype="multipart/form-data">
+        <input type="hidden" name="glm_action" value="index" />
+        <input type="hidden" name="option" value="videosValidate" />
+
+        <table class="glm-admin-table">
+            <tr>
+                <th>File Type</th>
+                <th>New File</th>
+                <th>Sample File</th>
+                <th>Current File</th>
+                <th>Updated</th>
+            </tr>
+            {foreach $fileData as $fileHeader => $file}
+            <tr>
+                <td class="glm-import-td">
+                    {$fileHeader}
+                </td>
+                <td class="glm-import-td">
+                    <input type="file" name="files_file">
+                </td>
+                <td class="glm-import-td">
+                    <a href="{$sampleFileUrl}{$file.name}">Sample {$fileHeader} File</a>
+                </td>
+                <td class="glm-import-td">
+                    {if $file.exists}
+                        {$fileHeader} File
+                    {/if}
+                </td>
+            <td class="glm-import-td">
+                {if $file.exists}
+                    {$file.mtime|date_format:"%D %I:%M %p"}
+                {/if}
+            </td>
+            {/foreach}
+            </tr>
+
+        </table>
+
+        <input type="submit" value="Continue" class="button button-primary submit-import">
+    </form>
+
+{include file='admin/footer.html'}
diff --git a/views/admin/import/videosProcess.html b/views/admin/import/videosProcess.html
new file mode 100644 (file)
index 0000000..69e2cbd
--- /dev/null
@@ -0,0 +1,24 @@
+{include file='admin/import/header.html'}
+
+    <h2>Data Import Step 3: Process Videos File</h2>
+
+    <table class="glm-admin-table">
+        <tr>
+            <th>Total Videos</th>
+            <td>{$totalVideos}</td>
+        </tr>
+        <tr>
+            <th>Processed Videos</th>
+            <td>{$videosProcessed}</td>
+        </tr>
+
+
+        {if $errors}
+            {foreach $errors as $error}
+                {$error}
+            {/foreach}
+        {/if}
+
+    </table>
+
+{include file='admin/footer.html'}
diff --git a/views/admin/import/videosValidate.html b/views/admin/import/videosValidate.html
new file mode 100644 (file)
index 0000000..e9c0b09
--- /dev/null
@@ -0,0 +1,32 @@
+{include file='admin/import/header.html'}
+
+<h2>Data Import: Videos Validation</h2>
+
+<table class="glm-admin-table">
+
+    <tr>
+        <td>
+            {if $fileExists}
+                videosData.csv
+            {/if}
+        </td>
+        <td>
+            {if $isValid}
+                Is Valid
+            {else}
+                Not Valid
+            {/if}
+        </td>
+    </tr>
+
+        {if $readyToProcess}
+        <tr>
+            <td colspan="2">
+                <a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=videosProcess{if $clearData}&clear_data=1{/if}" class="button">Process Files</a>
+            </td>
+        </tr>
+        {/if}
+
+</table>
+
+{include file='admin/footer.html'}