From dc17fd8ea4d0344902abcd93c84b1c0d6a2b7267 Mon Sep 17 00:00:00 2001
From: Joey O. Chua <joey@ict-joey.ceu.edu.ph>
Date: Wed, 16 Mar 2011 10:16:08 +0800
Subject: [PATCH] Issue #364796 by joeycbulk: Added feature automatic delete attached images when node deleted option.

---
 contrib/image_attach/image_attach.module |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/contrib/image_attach/image_attach.module b/contrib/image_attach/image_attach.module
index 87dda1f..fb2b220 100644
--- a/contrib/image_attach/image_attach.module
+++ b/contrib/image_attach/image_attach.module
@@ -176,6 +176,13 @@ function image_attach_form_node_type_form_alter(&$form, $form_state) {
       $link = l(t('Manage fields'), $_GET['q'] . '/fields');
       $form['image_attach']['#description'] = t('Since you installed CCK module, you can change the image weight in the !link page.', array('!link' => $link));
     }
+    $form['image_attach']['image_attach_auto_delete'] = array(
+      '#type' => 'radios',
+      '#title' => t('Auto Delete Attached Images'),
+      '#default_value' => variable_get('image_attach_auto_delete_' . $form['#node_type']->type, 0),
+      '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
+      '#description' => t('This option enables automatic deletion of associated image nodes when parent node is deleted.')
+    );
   }
 }
 
@@ -425,6 +432,13 @@ function image_attach_nodeapi(&$node, $op, $teaser, $page) {
       break;
 
     case 'delete':
+      if (variable_get('image_attach_auto_delete_' . $node->type, 0) == 1) {
+        // delete attached images
+        $nids = db_query("SELECT iid FROM {image_attach} WHERE nid = %d", $node->nid);
+        while ($nid = db_result($nids)) {
+          node_delete($nid);
+        }
+      }
       db_query("DELETE FROM {image_attach} WHERE nid = %d", $node->nid);
       break;
 
-- 
1.7.2.3

