Example #1
0
 public static function deletePreviewFile($id)
 {
     global $jlistConfig;
     jimport('joomla.filesystem.folder');
     jimport('joomla.filesystem.file');
     $app = JFactory::getApplication();
     $db = JFactory::getDBO();
     // get path
     $db->SetQuery("SELECT * FROM #__jdownloads_files WHERE file_id = {$id}");
     $file = $db->loadObject();
     if ($file->preview_filename) {
         $filename = $jlistConfig['files.uploaddir'] . DS . $jlistConfig['preview.files.folder.name'] . DS . $file->preview_filename;
         // check whether other downloads use also this preview file
         $db->SetQuery("SELECT count(*) FROM #__jdownloads_files WHERE preview_filename = '{$file->preview_filename}'");
         $result = $db->loadResult();
         if (!jFile::exists($filename) || $result > 1) {
             // file not exist - but we must always clear the data field
             // the same when other downloads used also this file
             $db->SetQuery("UPDATE #__jdownloads_files SET preview_filename = '' WHERE file_id = '{$id}'");
             $db->execute();
             return false;
         } else {
             if (jFile::delete($filename)) {
                 $db->SetQuery("UPDATE #__jdownloads_files SET preview_filename = '' WHERE file_id = '{$id}'");
                 $db->execute();
                 return true;
             } else {
                 // delete error
                 return false;
             }
         }
     } else {
         // preview_filename field empty
         return false;
     }
 }