/[drupal]/contributions/modules/taxonomy_image/taxonomy_image.module
ViewVC logotype

Diff of /contributions/modules/taxonomy_image/taxonomy_image.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.12.4.13.2.44, Thu Apr 16 13:33:35 2009 UTC revision 1.12.4.13.2.45, Fri Apr 17 15:54:43 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: taxonomy_image.module,v 1.12.4.13.2.43 2009/03/19 13:44:46 nancyw Exp $  // $Id: taxonomy_image.module,v 1.12.4.13.2.44 2009/04/16 13:33:35 nancyw Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 658  function taxonomy_image_taxonomy($op, $t Line 658  function taxonomy_image_taxonomy($op, $t
658        }        }
659        break;        break;
660      case 'delete':      case 'delete':
661        taxonomy_image_delete($tid);        if (_taxonomy_image_exists($tid)) {
662            taxonomy_image_delete($tid);
663          }
664        break;        break;
665    }    }
666  }  }
# Line 667  function taxonomy_image_taxonomy($op, $t Line 669  function taxonomy_image_taxonomy($op, $t
669   * Helper function for adding an image to a term   * Helper function for adding an image to a term
670   */   */
671  function taxonomy_image_add($tid, $filename) {  function taxonomy_image_add($tid, $filename) {
672      // @TODO: maybe we need to store also the FILE-ID from {files} table, better deletion and shared counts.
673    $count = db_result(db_query('SELECT COUNT(tid) FROM {term_image} WHERE tid=%d', $tid));  
674    if ($count == 1) {    // Delete old image before saving the new one.
675      // Delete old image before saving the new one.    if (_taxonomy_image_exists($tid)) {
676      taxonomy_image_delete($tid);      taxonomy_image_delete($tid);
677    }    }
678    
# Line 683  function taxonomy_image_add($tid, $filen Line 685  function taxonomy_image_add($tid, $filen
685    }    }
686  }  }
687    
688    /**
689     * Deletes the Taxonomy Image associated with the given termid.
690     * @param $tid - the id of the term to delete.
691     * return none - the association is broken, the path is removed from the files table,
692     *   and the image is deleted from the disk, if appropriate.
693     */
694  function taxonomy_image_delete($tid) {  function taxonomy_image_delete($tid) {
695    $old_path = db_result(db_query('SELECT path FROM {term_image} WHERE tid=%d', $tid));    $verbose = variable_get('taxonomy_image_verbose_delete', FALSE);
696    $how_many = db_result(db_query("SELECT COUNT(path) FROM {term_image} WHERE path='%s'", $old_path));    // Is there an image to delete?
697      $path = _taxonomy_image_exists($tid);
698      if (!$path) {
699        // No image, go back now.
700        return;
701      }
702    
703      // See how many terms are using this image.
704      $count = db_result(db_query("SELECT COUNT(path) FROM {term_image} WHERE path='%s'", $path));
705    
706      // Get the term's name.
707      $term = taxonomy_get_term($tid);
708      $term_name = check_plain(taxonomy_image_tt("taxonomy:term:$term->tid:name", $term->name));
709    
710      // Break the term to image association.
711      $del_tid = db_query('DELETE FROM {term_image} WHERE tid=%d', $tid);
712      if ($del_tid) {
713        if ($verbose) {
714          drupal_set_message(t('Term image association removed for !name.', array('!name' => $term_name)), 'status');
715        }
716        // Delete the cached version.
717        cache_clear_all("taxonomy_image:$tid", 'cache_tax_image');
718      }
719      else {
720        drupal_set_message(t('Error deleting %path for !name.', array('%path' => $path, '!name' => $term_name)), 'error');
721        // Don't delete anything else either.
722        return;
723      }
724    
725    // Delete the cached version.    if ($count > 1) {
726    cache_clear_all("taxonomy_image:$tid", 'cache_tax_image');      // This file is shared, don't remove file.
727        if ($verbose) {
728          drupal_set_message(t('%path is used !count other places.', array('%path' => $full_path, '!count' => $count - 1)), 'status');
729        }
730        return;
731      }
732    
733    $taxonomy_image_path = file_directory_path() .'/'. variable_get('taxonomy_image_path', 'category_pictures') .'/';    $taxonomy_image_path = file_directory_path() .'/'. variable_get('taxonomy_image_path', 'category_pictures') .'/';
734    $full_path = $taxonomy_image_path . $old_path;    $full_path = $taxonomy_image_path . $path;
735    if ($how_many == 1) {  
736      // This is the only term using this file, so it is safe to delete it.    // This is the only term using this file, so it is safe to delete it
737      $file_del_ok = db_query("DELETE FROM {files} WHERE filepath='%s'", $full_path);    // The physical file will be deleted only if also removed from {files} table.
738      $file_del = db_query("DELETE FROM {files} WHERE filepath='%s'", $full_path);
739      if ($file_del) {
740        if ($verbose) {
741          drupal_set_message(t('%path deleted from database.', array('%path' => $full_path)), 'status');
742        }
743    }    }
744    else {    else {
745      // Pretend we deleted it.      drupal_set_message(t('Error deleting image %path.', array('%path' => $full_path)), 'error');
746      $file_del_ok = TRUE;      // Don't even try to delete from disk.
747      drupal_set_message(t('Not deleted from the files table because it is use on !count other terms.', array('!count' => $how_many - 1)));      return;
748    }    }
749    
750    $db_del_ok = db_query('DELETE FROM {term_image} WHERE tid=%d', $tid);    $phys_del = file_delete($full_path);
751    if ($file_del_ok && $db_del_ok) {    if ($phys_del) {
752      drupal_set_message(t('!name image removed.', array('!name' => $old_path)));      if ($verbose) {
753          drupal_set_message(t('%path deleted from disk.', array('%path' => $full_path)), 'status');
754        }
755    }    }
756    else {    else {
757      drupal_set_message(t('Image delete failed. File: !file, Db: !db.',      drupal_set_message(t('Error deleting image file %path from disk.', array('%path' => $full_path)), 'warning');
       array('!file' => ($file_del_ok ? 'yes' : 'no'), '!db' => ($db_del_ok ? 'yes' : 'no'))));  
758    }    }
   return;  
759  }  }
760    
761    /**
762     * Check if the given TID has an image associated.
763     *
764     * @param $tid: Term-ID to check
765     * @return the image filename or FALSE
766     */
767  function _taxonomy_image_exists($tid) {  function _taxonomy_image_exists($tid) {
768    if (db_fetch_object(db_query('SELECT path FROM {term_image} WHERE tid=%d', $tid))) {    return db_result(db_query("SELECT path FROM {term_image} WHERE tid=%d", $tid));
     return TRUE;  
   }  
   return FALSE;  
769  }  }
770    
771  /**  /**

Legend:
Removed from v.1.12.4.13.2.44  
changed lines
  Added in v.1.12.4.13.2.45

  ViewVC Help
Powered by ViewVC 1.1.2