Added Image Gallery Clone feature
authorChuck Scott <cscott@gaslightmedia.com>
Tue, 14 Jul 2015 14:34:11 +0000 (10:34 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Tue, 14 Jul 2015 14:34:11 +0000 (10:34 -0400)
classes/data/dataImages.php
classes/glmMemberInfoClone.php
lib/GlmDataAbstract/DataAbstract.php
misc/smarty/templates_c/081a36d97cdf30d438a1e104c26a275acc180da0.file.index.html.php
misc/smarty/templates_c/47fb9b803e7138d215645f3c977b0b1dc2a9e718.file.index.html.php
models/admin/ajax/imageUpload.php
models/admin/member/memberInfo.php
views/admin/member/memberInfo.html

index 1b33476..e40e06e 100644 (file)
@@ -115,6 +115,14 @@ class GlmDataImages extends GlmDataAbstract
                         'use' => 'a'
                 ),
 
+                // Original file name
+                'name' => array(
+                        'field' => 'name',
+                        'type' => 'text',
+                        'required' => true,
+                        'use' => 'a'
+                ),
+
                 // Status
                 'status' => array (
                         'field' => 'status',
@@ -276,18 +284,24 @@ class GlmDataImages extends GlmDataAbstract
         // Update image entries with new sort order;
         $pos = 0;
         foreach($order as $i) {
-            $sql = "UPDATE ".GLM_MEMBERS_PLUGIN_DB_PREFIX ."images SET position = $pos WHERE id = $i;";
-            $this->wpdb->query($sql);
-            $pos++;
 
-            $queryError = $this->wpdb->last_error;
-            if ($queryError) {
+            // Do sanity check on image ID
+            if (($i-0) > 0) {
 
-                if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) {
-                    glmMembersAdmin::addNotice("<b>&nbsp;&nbsp;Error setting position order for images.</b><br>$queryError", 'Alert');
-                }
+                // Set the position for this image
+                $sql = "UPDATE ".GLM_MEMBERS_PLUGIN_DB_PREFIX ."images SET position = $pos WHERE id = $i;";
+                $this->wpdb->query($sql);
+                $pos++;
+
+                $queryError = $this->wpdb->last_error;
+                if ($queryError) {
 
-                return false;
+                    if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) {
+                        glmMembersAdmin::addNotice("<b>&nbsp;&nbsp;Error setting position order for images.</b><br>$queryError<br>$sql", 'Alert');
+                    }
+
+                    return false;
+                }
             }
         }
 
@@ -311,62 +325,69 @@ class GlmDataImages extends GlmDataAbstract
         // Re-order image gallery images
         $this->galleryPositionOrder($refType, $refID, $orderField);
 
-        // Update text for title and descriptions
-        reset($_REQUEST['galleryImage_caption']);
-        while (list($k, $v) = each($_REQUEST['galleryImage_caption'])) {
-            $id = ($k -0);
+        // If any image gallery data is submitted
+        if (isset($_REQUEST['galleryImage_caption'])
+                && is_array($_REQUEST['galleryImage_caption'])
+                && count($_REQUEST['galleryImage_caption']) > 0) {
 
-            // Sanitize input
-            $caption = sanitize_text_field( $_REQUEST['galleryImage_caption'][$k] );
-            $descr = sanitize_text_field( $_REQUEST['galleryImage_descr'][$k] );
+            // Update text for title and descriptions
+            reset($_REQUEST['galleryImage_caption']);
+            while (list($k, $v) = each($_REQUEST['galleryImage_caption'])) {
+                $id = ($k -0);
 
-            // Update data for this image
-            $sql = "UPDATE ".GLM_MEMBERS_PLUGIN_DB_PREFIX ."images SET caption = '$caption', descr = '$descr' WHERE id = $id;";
-            $this->wpdb->query($sql);
+                // Sanitize input
+                $caption = sanitize_text_field( $_REQUEST['galleryImage_caption'][$k] );
+                $descr = sanitize_text_field( $_REQUEST['galleryImage_descr'][$k] );
 
-        }
+                // Update data for this image
+                $sql = "UPDATE ".GLM_MEMBERS_PLUGIN_DB_PREFIX ."images SET caption = '$caption', descr = '$descr' WHERE id = $id;";
+                $this->wpdb->query($sql);
 
-        // Check for an image deletion
-        if (isset($_REQUEST['galleryImage_delete']) && count($_REQUEST['galleryImage_delete']) > 0) {
+            }
 
-            // For each delete selected
-            reset($_REQUEST['galleryImage_delete']);
-            while (list($k, $v) = each($_REQUEST['galleryImage_delete'])) {
-                $id = ($k -0);
+            // Check for an image deletion
+            if (isset($_REQUEST['galleryImage_delete']) && count($_REQUEST['galleryImage_delete']) > 0) {
 
-                // If a valid key and delete request is in fact set
-                if ($id > 0 && $v == 'on') {
+                // For each delete selected
+                reset($_REQUEST['galleryImage_delete']);
+                while (list($k, $v) = each($_REQUEST['galleryImage_delete'])) {
+                    $id = ($k -0);
 
-                    // Get the data for this image
-                    $sql = "SELECT * FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX ."images WHERE id = $id;";
-                    $imgData = $this->wpdb->get_row($sql, ARRAY_A);
+                    // If a valid key and delete request is in fact set
+                    if ($id > 0 && $v == 'on') {
 
-                    // Do we have data?
-                    if ($imgData != null && trim($imgData['file_name']) != '') {
+                        // Get the data for this image
+                        $sql = "SELECT * FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX ."images WHERE id = $id;";
+                        $imgData = $this->wpdb->get_row($sql, ARRAY_A);
 
-                        // Delete the image from the gallery
-                        $sql = "DELETE FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX ."images WHERE id = $id;";
-                        $this->wpdb->query($sql);
+                        // Do we have data?
+                        if ($imgData != null && trim($imgData['file_name']) != '') {
 
-                        // Also delete all copies of the image
-                        reset($this->config['imageSizes']);
-                        while (list($k, $v) = each($this->config['imageSizes'])) {
+                            // Delete the image from the gallery
+                            $sql = "DELETE FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX ."images WHERE id = $id;";
+                            $this->wpdb->query($sql);
 
-                            if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) {
-                                glmMembersAdmin::addNotice("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Deleting size $k.", 'Process');
-                            }
+                            // Also delete all copies of the image
+                            reset($this->config['imageSizes']);
+                            while (list($k, $v) = each($this->config['imageSizes'])) {
 
-                            // Delete each image size
-                            unlink(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$k.'/'.$imgData['file_name']);
+                                if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) {
+                                    glmMembersAdmin::addNotice("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Deleting size $k.", 'Process');
+                                }
 
-                        }
+                                // Delete each image size
+                                unlink(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$k.'/'.$imgData['file_name']);
 
-                        // Also delete the original
-                        unlink(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/original/'.$imgData['file_name']);
+                            }
+
+                            // Also delete the original
+                            unlink(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/original/'.$imgData['file_name']);
 
+                        }
                     }
                 }
             }
+
         }
 
         // Get updated image gallery
@@ -375,6 +396,91 @@ class GlmDataImages extends GlmDataAbstract
 
     }
 
+
+    /*
+     * Clone an image gallery
+     *
+     * Makes a copy of all images in a gallery and assigns them to another refType and refID
+     *
+     * @param int $refTypeSrc Source Reference Type
+     * @param int $refIDSrc Source Reference entry ID
+     * @param int $refTypeDst Destination Reference Type
+     * @param int $refIDDst Destination Reference entry ID
+     * @param string $orderField Name of SUBMIT field containing sort order.
+     *
+     * @return boolean True if successful
+     *
+     */
+    public function galleryImageDataClone($refTypeSrc, $refIDSrc, $refTypeDst, $refIDDst)
+    {
+
+        // Get Source Image Gallery
+        $imageGallerySrc = $this->getGallery($refTypeSrc, $refIDSrc);
+
+        // Get the destination target table name
+        $dstTable = $this->config['ref_type_table'][$refTypeDst];
+
+        // Get Image sizes and add original
+        $sizes = $this->config['imageSizes'];
+        $sizes['original'] = array();
+
+        // If we have an image gallery with any images
+        if ($imageGallerySrc !== false
+                && is_array($imageGallerySrc)
+                && count($imageGallerySrc) > 0) {
+
+            // for each gallery image
+            foreach ($imageGallerySrc as $i) {
+
+                $srcName = $i['file_name'];
+
+                // Strip the source table name and ID from the file name (part after first '-')
+                $dstName = $dstTable.'_'.$refIDDst.'-'.substr(strchr($i['file_name'], '-'), 1);
+
+                // For each image size
+                reset($sizes);
+                while (list($k, $v) = each($sizes)) {
+
+                    // Copy src file to dst file for this size
+                    copy(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$k.'/'.$srcName, GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$k.'/'.$dstName);
+
+                }
+
+                // Add this image to the images table
+                // Store image name in images table
+                $sql = "
+                    INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX ."images
+                        (
+                            name,
+                            file_name,
+                            descr,
+                            caption,
+                            status,
+                            position,
+                            ref_type,
+                            ref_dest
+                        )
+                    VALUES
+                        (
+                            '".$i['name']."',
+                            '$dstName',
+                            '".addslashes($i['descr'])."',
+                            '".addslashes($i['caption'])."',
+                            ".$i['status']['value'].",
+                            ".$i['position'].",
+                            $refTypeDst,
+                            $refIDDst
+                        );
+                ";
+                $this->wpdb->query($sql);
+                // $queryError = $this->wpdb->last_error;
+
+            }
+
+        }
+
+    }
+
 }
 
 ?>
\ No newline at end of file
index 5531ba0..9c909fb 100644 (file)
@@ -105,6 +105,17 @@ class GlmMemberInfoClone
 
             $this->wpdb->query($sql);
 
+            // Load image gallery class
+            require_once(GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataImages.php');
+            $Images = new GlmDataImages($this->wpdb, $this->config);
+
+            // Get the refType for the member_info table and use if for both src and dst
+            $refType = $this->config['ref_type_numb']['MemberInfo'];
+
+            // Clone the image gallery
+            $Images->galleryImageDataClone($refType, $id, $refType, $newID);
+
+
         }
 
         return $newID;
index a0e26ca..6fb551e 100755 (executable)
@@ -2353,8 +2353,11 @@ abstract class GlmDataAbstract
                     }
                 }
 
-                // Remove original
-//                unlink(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$newFilename);
+                // Move the original to original directory
+                if (!file_exists(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/original')) {
+                    mkdir(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/original');
+                }
+                rename(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$newFilename, GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/original/'.$newFilename);
 
                 $current_img = $newFilename;
 
index f2747bd..5c0c5c3 100644 (file)
@@ -1,4 +1,4 @@
-<?php /* Smarty version Smarty-3.1.21-dev, created on 2015-06-19 11:18:56
+<?php /* Smarty version Smarty-3.1.21-dev, created on 2015-07-13 15:52:12
          compiled from "/var/www/server/wordpress/wp-content/plugins/glm-member-db/views/admin/members/index.html" */ ?>
 <?php /*%%SmartyHeaderCode:43313958054c05ab60b0587-71987387%%*/if(!defined('SMARTY_DIR')) exit('no direct access allowed');
 $_valid = $_smarty_tpl->decodeProperties(array (
@@ -7,7 +7,7 @@ $_valid = $_smarty_tpl->decodeProperties(array (
     '081a36d97cdf30d438a1e104c26a275acc180da0' => 
     array (
       0 => '/var/www/server/wordpress/wp-content/plugins/glm-member-db/views/admin/members/index.html',
-      1 => 1434727132,
+      1 => 1435675689,
       2 => 'file',
     ),
   ),
index 1a98278..fff30a1 100644 (file)
@@ -1,4 +1,4 @@
-<?php /* Smarty version Smarty-3.1.21-dev, created on 2015-06-17 16:40:00
+<?php /* Smarty version Smarty-3.1.21-dev, created on 2015-07-14 09:45:25
          compiled from "/var/www/server/wordpress/wp-content/plugins/glm-member-db/views/admin/configure/index.html" */ ?>
 <?php /*%%SmartyHeaderCode:135960089454c0496da6c5c6-30692976%%*/if(!defined('SMARTY_DIR')) exit('no direct access allowed');
 $_valid = $_smarty_tpl->decodeProperties(array (
@@ -7,7 +7,7 @@ $_valid = $_smarty_tpl->decodeProperties(array (
     '47fb9b803e7138d215645f3c977b0b1dc2a9e718' => 
     array (
       0 => '/var/www/server/wordpress/wp-content/plugins/glm-member-db/views/admin/configure/index.html',
-      1 => 1434554103,
+      1 => 1435675689,
       2 => 'file',
     ),
   ),
index bcba5dd..d765e3e 100644 (file)
@@ -65,35 +65,22 @@ class GlmMembersAdmin_ajax_imageUpload extends GlmDataImages
     /*
      * Perform Model Action
      *
-     * This method does the work for this model and returns any resulting data
+     * This modelAction takes an AJAX image upload and stores the image in the
+     * media/images directory of the plugin.
      *
-     * @return array Status and data array
+     * This model action does not return, it simply does it's work then calls die();
      *
-     * 'status'
+     * Images are stored in various sizes in the media/images/{size} directories
+     * which are created if they don't already exist.
      *
-     * True if successfull and false if there was a fatal failure.
+     * Image names consist of the following...
      *
-     * 'menuItemRedirect'
+     *      {target table name}_{target table id}-{original name}_{timestamp}.{ext}
      *
-     * If not false, provides a menu item the controller should
-     * execute after this one. Normally if this is used, there would also be a
-     * modelRedirect value supplied as well.
-     *
-     * 'modelRedirect'
-     *
-     * If not false, provides an action the controller should execute after
-     * this one.
-     *
-     * 'view'
-     *
-     * A suggested view name that the contoller should use instead of the
-     * default view for this model or false to indicate that the default view
-     * should be used.
-     *
-     * 'data'
-     *
-     * Data that the model is returning for use in merging with the view to
-     * produce output.
+     *      {target table name} The name of the table were the gallery target is located
+     *      {target table id}   The ID of the record in the target table associated with the gallery
+     *      {original name}     The base original name of the file
+     *      {ext}               The file extension related to the type of the image
      *
      */
     public function modelAction ($actionData = false)
@@ -152,7 +139,7 @@ class GlmMembersAdmin_ajax_imageUpload extends GlmDataImages
 
                     // Get the desired file name and add a timestamp to it to ensure that it's unique
                     $fInfo = pathinfo($file['name']);
-                    $newFilename = strtolower($fInfo['filename'].'_'.time().'.'.$fInfo['extension']);
+                    $newFilename = $refTable.'_'.$refDest.'-'.strtolower($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();
index 286afd7..6484044 100644 (file)
@@ -263,7 +263,7 @@ class GlmMembersAdmin_member_memberInfo extends GlmDataMemberInfo
                 // Clone the current member info
                 $memberInfoID = $CloneMemberInfo->cloneMemberInfo($memberInfoID);
 
-                $memberInfo = $this->editEntry($memberInfoID);
+//               $memberInfo = $this->editEntry($memberInfoID);
 
             // Default is to display the currently selected member information record in a form for updates
             default:
@@ -495,6 +495,7 @@ class GlmMembersAdmin_member_memberInfo extends GlmDataMemberInfo
             'haveCategories' => $haveCategories,
             'categories' => $categories,
             'categoryMemberInfo' => $categoryMemberInfo,
+            'haveImageGallery' => $haveImageGallery,
             'imageGallery' => $imageGallery,
             'noActive' => $noActive,
             'time' => time()
index b2186c3..03fab00 100644 (file)
                 <th>Image Gallery</th>
                 <td class="glm-item-container glm-imageGalleryContainer">
                     <input type="hidden" id="galleryPositionOrder" name="galleryPositionOrder" 
-                        value="{foreach $imageGallery as $i name=ig}{if $i.file_name}{$i.id}{if not $smarty.foreach.ig.last},{/if}{/if}{/foreach}" />
+                        value="{if $haveImageGallery}{foreach $imageGallery as $i name=ig}{if $i.file_name}{$i.id}{if not $smarty.foreach.ig.last},{/if}{/if}{/foreach}{/if}" />
                     <div class="glm-imageDropContainer">
                             <!-- All fields with class "glm-imageDrop" are automatically processed by imageUpload.js -->
                         <div class="glm-imageDrop glm-imageItemHidden" 
                     </div>
                     <ul class="glm-galleryImages">
                         <!-- Note that id in li is needed for sorting -->
-        {foreach $imageGallery as $i}
-            {if $i.file_name}
+        {if $haveImageGallery}     
+            {foreach $imageGallery as $i}
+                {if $i.file_name}
                         <li id="{$i.id}" class="glm-galleryContainer">
                             <div class="glm-galleryImageData">
                                 <table class="glm-statusTable">
                                 <img src="{$glmPluginMediaURL}/images/small/{$i.file_name}">
                             </div>
                             <div id="glm-galleryImageLarger_{$i.id}" class="glm-imageDialog"><img src="{$glmPluginMediaURL}/images/large/{$i.file_name}"></div>
-            {/if}       </li>
+                {/if}
+                       </li>
                                          
-        {/foreach}
+            {/foreach}
+        {/if}
                     </div>
                 </td>
             </tr>