Some ideas for file uploads

Discuss MySQL Ajax Table Editor Paid Version

Some ideas for file uploads

Postby paulr » Thu Apr 22, 2010 11:07 am

It would be nice to have an option to delete the uploaded file(s) associated with a given row when the row is deleted. This is to avoid having orphaned files on the server. I thought about handling this myself with the afterDeleteFun, however I guess as the row is already deleted when the callback fires I wont be able to get the file name, only the row id?

Another nice thing would be to be able add/edit a row and optionally add a file that already exists on the server, not just upload from the client machine. This could perhaps be managed by a separate table having a list of available files.
paulr
 
Posts: 45
Joined: Thu Mar 25, 2010 9:09 am

Re: Some ideas for file uploads

Postby admin » Mon Apr 26, 2010 1:53 pm

You could use a user icon with a callback function to delete both the file and the row in the database.

To choose a file that already exists on the server would require some custom scripting but I believe it could be done.
admin
Site Admin
 
Posts: 1502
Joined: Fri Jul 11, 2008 1:34 am

Re: Some ideas for file uploads

Postby nuloprogramando » Mon May 31, 2010 7:39 pm

Hello,

Thanks for everything, i'm learning a lot with all of your suggestions.
I'd like to know if anyone has tested the upload function in a MAC OX. I have decided to buy the paid version but for me is really important that this function works properly (for a MAC with Safari and/or Mozilla).

Can anyone tell me something?

Thanks in advance,
nuloprogramando
 
Posts: 1
Joined: Mon May 31, 2010 12:52 pm

Re: Some ideas for file uploads

Postby PowerPill » Tue Jun 01, 2010 4:19 am

I'm doing all of my development in OSX (snow) and all works as it should just like it does on my linux box. I haven't received the paid version yet until my echeque clears. But all other ajax/jquery upload systems work fine. In fact that's how I passed my time over the weekend. Unix truly is better in all respects. ;) If by any chance you run into problems, I'd be more than happy to lend a hand or point you in the right direction.

I do agree that a beforeDelete option would be useful in many ways (non destructive deletes and now what you've mentioned). - Along with a plethora of other reasons but admin is right, a userButton and or icon with the proper callback can do the same thing.

With a little code though, in your case you could cross reference what files exist against what is currently in the database and then "clean" accordingly using the afterDelete function as well. Just a thought...

Cheers,
PowerPill
 
Posts: 10
Joined: Wed May 19, 2010 8:21 am
Location: Surrey B.C. Canada

Re: Some ideas for file uploads

Postby pq_rar » Mon Jul 19, 2010 2:35 pm

Hi,
I have this issue and I will need some help.
I need "Delete" button for file under EDIT row...like delete button for dates.
Can you provide a sample code.
Pls pls help
Thanks
pq_rar
 
Posts: 89
Joined: Thu May 27, 2010 4:44 am

Re: Some ideas for file uploads

Postby pq_rar » Mon Jul 19, 2010 4:04 pm

anybody?...

I think the "delete" button should come standard in the future.
pq_rar
 
Posts: 89
Joined: Thu May 27, 2010 4:44 am

Re: Some ideas for file uploads

Postby admin » Mon Jul 19, 2010 4:33 pm

Have you tried the afterDeleteFun to delete the file? The problem with this is you will need to be able to identify the file on the hard drive with the primary column value.
admin
Site Admin
 
Posts: 1502
Joined: Fri Jul 11, 2008 1:34 am

Re: Some ideas for file uploads

Postby pq_rar » Mon Jul 19, 2010 7:48 pm

Actually I need something like this in EDIT:
Image
not delete after deleting the row
but I have no idea how to do it; at least to delete it from the DB will be great.
pq_rar
 
Posts: 89
Joined: Thu May 27, 2010 4:44 am

Re: Some ideas for file uploads

Postby admin » Mon Jul 19, 2010 7:56 pm

You can use the format_input_fun option or the editScreenFun to add a delete icon on if a file exists.
admin
Site Admin
 
Posts: 1502
Joined: Fri Jul 11, 2008 1:34 am

Re: Some ideas for file uploads

Postby admin » Tue Jul 20, 2010 3:15 pm

Ok I modifed the UploadToDb.php example that comes with the paid version so it has a delete icon next to the file name on the edit screen. Here is the code.
Code: Select all
<?php
/*
 * Mysql Ajax Table Editor
 *
 * Copyright (c) 2008 Chris Kitchen <info@mysqlajaxtableeditor.com>
 * All rights reserved.
 *
 * See COPYING file for license information.
 *
 * Download the latest version from
 * http://www.mysqlajaxtableeditor.com
 */
require_once('Common.php');
require_once('php/lang/LangVars-en.php');
require_once('php/AjaxTableEditor.php');
class UploadToDb extends Common
{
   var $Editor;
   
   function displayHtml()
   {
      $html = '
         <br />
   
         <div align="left" style="position: relative;"><div id="ajaxLoader1"><img src="images/ajax_loader.gif" alt="Loading..." /></div></div>
         
         <br />

         <div id="information">
         </div>
   
         <div id="titleLayer" style="padding: 2px; font-weight: bold; font-size: 18px; text-align: center;">
         </div>
   
         <div id="tableLayer" align="center">
         </div>
         
         <div id="filterLayer" align="center">
         </div>
         
         <div id="recordLayer" align="center">
         </div>
         
         <div id="searchButtonsLayer" align="center">
         </div>
         
         <br /><br /><br />
         <div align="center"><a href="index.php">Back To Examples</a></div>';
         
      echo $html;
         
      // Set default session configuration variables here
      $defaultSessionData['orderByColumn'] = 'first_name';

      $defaultSessionData = base64_encode($this->Editor->jsonEncode($defaultSessionData));
      
      $javascript = '   
         <script type="text/javascript">
            setAjaxInfo({url: "'.$_SERVER["PHP_SELF"].'", history: true});
            if(ajaxInfo.history == false)
            {
               toAjaxTableEditor("update_html","");
            }
            else if(window.location.hash.length == 0)
            {
               var defaultInfo = {info: "", action: "update_html", sessionData: "'.$defaultSessionData.'"};
               window.location.href = window.location.href+"#"+Base64.encode(Object.toJSON(defaultInfo));
            }
         </script>';
      echo $javascript;
      
      ?>
         <script type="text/javascript">
            function addDeleteIcon()
            {
               var id = $('id').value;
               var html = $('file_data_input_cell').innerHTML;
               var htmlParts = html.split(/<br>/);
               if(htmlParts.length == 2)
               {
                  var newHtml = '<table id="file_name_table"><tr><td style="border: none;">'+htmlParts[0]+'</td><td style="border: none;"><ul class="actions"><li class="delete"><a href="javascript: void(0);" onclick="deleteFile(\''+id+'\');" title="Delete '+htmlParts[0]+'"></a></li></ul></td></tr></table>';
                  newHtml += htmlParts[1];
                  $('file_data_input_cell').innerHTML = newHtml;
               }
            }
            
            function deleteFile(id)
            {
               if(confirm('Are you sure you would like to delete this file?'))
               {
                  toAjaxTableEditor('delete_file',id);
               }
            }
            
         </script>
      <?php
      
   }
   
   function deleteFile($id)
   {
      $query = "update emp_upload_db set file_data = '', file_name = '', file_type = '' where id = '$id'";
      $result = mysql_query($query);
      if($result)
      {
         $this->Editor->retArr[] = array('where' => 'javascript', 'value' => '$("file_name_table").remove();');
      }
   }
   
   function formatFileSize($col,$size,$row)
   {
      $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
      $retstring = '%01.2f %s';
      $lastsizestring = end($sizes);
      foreach ($sizes as $sizestring)
      {
            if ($size < 1024) { break; }
            if ($sizestring != $lastsizestring) { $size /= 1024; }
      }
      if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; }
      return sprintf($retstring, $size, $sizestring);
   }
   
   function formatImage($col,$val,$row)
   {
      $html = '';
      if(strlen($val) > 0)
      {
         $html .= '<a target="_blank" href="DisplayFileFromDb.php?emp_id='.$row['id'].'"><img style="border: none;" src="DisplayFileFromDb.php?emp_id='.$row['id'].'" alt="'.$val.'" width="100" /></a>';
      }
      return $html;
   }
   
   function initiateEditor()
   {
      $tableColumns['id'] = array('display_text' => 'ID', 'perms' => 'ETVQSFXO');
      $tableColumns['first_name'] = array('display_text' => 'First Name', 'perms' => 'EVCTAXQSFHO', 'req' => true);
      $tableColumns['last_name'] = array('display_text' => 'Last Name', 'perms' => 'EVCTAXQSFHO', 'val_fun' => array(&$this,'validateFun'));
      $tableColumns['email'] = array('display_text' => 'Email', 'perms' => 'EVCTAXQSFHO');
      $tableColumns['file_data'] = array('display_text' => 'Image', 'perms' => 'EVCAXTQSFHO',
         'file_upload' => array('type' => 'file_type', 'name' => 'file_name'),
         'display_mask' => 'file_name', 'table_fun' => array(&$this,'formatImage'), 'view_fun' => array(&$this,'formatImage')
      );
      $tableColumns['file_size'] = array('display_text' => 'Size', 'perms' => 'VXTQSFHO',
         'display_mask' => 'octet_length(file_data)', 'table_fun' => array(&$this,'formatFileSize'), 'view_fun' => array(&$this,'formatFileSize')
      );
      $tableColumns['department'] = array('display_text' => 'Department', 'perms' => 'EVCTAXQSFHO',
         'select_array' => array('Accounting' => 'Accounting', 'Marketing' => 'Marketing', 'Sales' => 'Sales', 'Production' => 'Production')
      );
      $tableColumns['file_name'] = array('perms' => 'C');
      //$tableColumns['hire_date'] = array('display_text' => 'Hire Date', 'perms' => 'EVCTAXQSHO', 'display_mask' => 'date_format(hire_date,"%d %M %Y")', 'calendar' => array('format' => '%d %B %Y', 'reset' => true),'col_header_info' => 'style="width: 250px;"');
      
      $tableName = 'emp_upload_db';
      $primaryCol = 'id';
      $errorFun = array(&$this,'logError');
      $permissions = 'EAVDCXHOUFI';
      
      $this->Editor = new AjaxTableEditor($tableName,$primaryCol,$errorFun,$permissions,$tableColumns);
      $this->Editor->setConfig('tableInfo','cellpadding="1" width="1100" class="mateTable"');
      $this->Editor->setConfig('tableTitle','Upload To Database');
      $this->Editor->setConfig('addRowTitle','Add Employee');
      $this->Editor->setConfig('editRowTitle','Edit Employee');
      $this->Editor->setConfig('paginationLinks',true);
      $userActions = array('delete_file' => array(&$this,'deleteFile'));
      $this->Editor->setConfig('userActions',$userActions);
   }
   
   function UploadToDb()
   {
      session_start();
      ob_start();
      $this->mysqlConnect();
      $this->initiateEditor();
      if(isset($_POST['json']))
      {
         if(ini_get('magic_quotes_gpc'))
         {
            $_POST['json'] = stripslashes($_POST['json']);
         }
         $this->Editor->data = $this->Editor->jsonDecode($_POST['json']);
         $this->Editor->setDefaults();
         $this->Editor->main();
         if($this->Editor->data->action == 'edit_row')
         {
            $this->Editor->retArr[] = array('where' => 'javascript', 'value' => 'addDeleteIcon();');
         }
         echo $this->Editor->jsonEncode($this->Editor->retArr);
      }
      else if(isset($_GET['export']))
      {
         $this->Editor->data->sessionData = $_GET['session_data'];
         $this->Editor->setDefaults();
         ob_end_clean();
         header('Cache-Control: no-cache, must-revalidate');
         header('Pragma: no-cache');
         header('Content-type: application/x-msexcel');
         header('Content-Type: text/csv');
         header('Content-Disposition: attachment; filename="'.$this->Editor->tableName.'.csv"');
         // Add utf-8 signature for windows/excel
         echo chr(0xEF).chr(0xBB).chr(0xBF);
         echo $this->Editor->exportInfo();
         exit();
      }
      else if(isset($_POST) && count($_POST) > 0)
      {
         $this->Editor->setDefaults();
         $this->Editor->handleFileUpload();
        }
      else
      {
         $this->displayHeaderHtml();
         $this->displayHtml();
         $this->displayFooterHtml();
      }
   }
}
$lte = new UploadToDb();
?>
admin
Site Admin
 
Posts: 1502
Joined: Fri Jul 11, 2008 1:34 am

Next

Return to Paid Version

Who is online

Users browsing this forum: No registered users and 5 guests

cron