del_item.php

Go to the documentation of this file.
00001 <?php
00002 
00005 
00006 require_once 'Smarty.class.php';
00007 require_once 'sql.php';
00008 require_once 'error.php';
00009 require_once 'util.php';
00010 require_once 'redirect.php';
00011 require_once 'const.php';
00012 require_once 'config.php';
00013 require_once 'upload.php';
00014 
00028 
00029 function del_item($smarty, $INPUT, $db) {
00030 global $_SESSION, $validation_info;
00031 
00032 
00033 $default = array(
00034 );
00035 
00036 $INPUT = array_merge($default, $INPUT);
00037 $errors = check_input($INPUT, $validation_info); 
00038 
00039 if ($debug_level > 10) {
00040          print "<hr><pre>Input: ";
00041         print_r($INPUT);
00042         print "</pre><hr>";
00043 }
00044 
00045 if (!empty($errors)) {
00046         user_error("Missing or malformed input parameter(s): " . join($errors, ", "),
00047                 E_USER_ERROR); 
00048 }
00049 
00050 
00051 if (!empty($INPUT['file'])) {
00052 
00053 
00054         # just delete an attached file
00055         delete_file($INPUT['item'], $INPUT['id'], $INPUT['file']);
00056 
00057 
00058 } else {
00059 
00060         if (!may_delete_item($INPUT['item'], $INPUT['id'], $db)) {
00061                 do_template($smarty, 'no_delete.tpl' , $INPUT, FALSE);
00062                 exit(0);
00063         }
00064 
00065         # delete the whole item
00066         do_delete_item($INPUT['item'], $INPUT['id'], $db);
00067 
00068 }
00069 
00070 }
00071 
00081 
00082 function do_delete_item($item, $id, $db) {
00083 
00084         $sql_table = array (
00085                 "article"    => "document",
00086                 "book"       => "document",
00087                 "file"        => "document",
00088                 "url"        => "document",
00089                 "collection" => "collection",
00090                 "user"       => "user",
00091                 "email"      => "email",
00092         );
00093 
00094         switch($item) {
00095 
00096         case "user":
00097 
00098                 $p = array (
00099                         "tables"  => "collection",
00100                         "cond"    => "collection.user_id = $id",
00101                         "columns" => "id"
00102                 );
00103 
00104                 $ans = sql_query('select', $p, $db);
00105 
00106                 foreach ($ans as $a) {
00107                         do_delete_item("collection",$a['id'], $db);
00108                 }
00109 
00110                 break;
00111 
00112         case "collection":
00113 
00114                 $p = array (
00115                         "tables"  => "document, doc_type",
00116                         "cond"    => "document.collection_id = $id AND doc_type.id = document.doc_type_id",
00117                         "columns" => "document.id AS id, doc_type.name AS type"
00118                 );
00119 
00120                 $ans = sql_query('select', $p, $db);
00121 
00122                 foreach ($ans as $a) {
00123                         do_delete_item($a['type'], $a['id'], $db);
00124                 }
00125 
00126                 break;
00127 
00128         case "email":
00129                 break;
00130 
00131         default: // document
00132 
00133                 $p = array (
00134                         "tables"  => "email",
00135                         "cond"    => "email.document_id = $id",
00136                         "columns" => "id"
00137                 );
00138 
00139                 $ans = sql_query('select', $p, $db);
00140 
00141                 foreach ($ans as $a) {
00142                         do_delete_item("email",$a['id'], $db);
00143                 }
00144 
00145                 break;
00146 
00147         }
00148 
00149 
00150         $t = $sql_table[$item];
00151 
00152         $p = array (
00153                 "table"  => $t,
00154                 "cond"    => "id = $id",
00155         );
00156 
00157         sql_query('delete', $p, $db);
00158 
00159         # delete all attached documents
00160 
00161         $files = list_files($item, $id);
00162 
00163         foreach ($files as $f => $f_info) {
00164                 delete_file($item, $id, $f);
00165         }
00166 
00167 }
00168 
00182 
00183 function may_delete_item($item, $id, $db) {
00184 
00185         $sql_table = array (
00186                 "article"    => "document",
00187                 "book"       => "document",
00188                 "file"        => "document",
00189                 "url"        => "document",
00190                 "collection" => "collection",
00191                 "user"       => "user",
00192                 "email"      => "email",
00193         );
00194 
00195         $t = $sql_table[$item];
00196 
00197         $p = array (
00198                 "tables"  => "state,$t",
00199                 "cond"    => "state.id = $t.state_id AND $t.id = $id", 
00200                 "columns" => "state.name AS state_name",
00201                 "order" => "$t.id"
00202         );
00203 
00204         $ans = sql_query('select', $p, $db);
00205         
00206         if (empty($ans)) {
00207                 user_error("No such item: $item, id=$id", E_USER_ERROR); 
00208         } 
00209 
00210         switch($item) {
00211 
00212         case "user":
00213 
00214                 $ok = TRUE;
00215 
00216                 $p = array (
00217                         "tables"  => "collection",
00218                         "cond"    => "collection.user_id = $id",
00219                         "columns" => "id"
00220                 );
00221 
00222                 $ans = sql_query('select', $p, $db);
00223 
00224                 foreach ($ans as $a) {
00225                         $ok = $ok && may_delete_item("collection",$a['id'], $db);
00226                 }
00227 
00228                 break;
00229 
00230         case "collection":
00231 
00232                 $ok = TRUE;
00233 
00234                 $p = array (
00235                         "tables"  => "document, doc_type",
00236                         "cond"    => "document.collection_id = $id AND doc_type.id = document.doc_type_id",
00237                         "columns" => "document.id AS id, doc_type.name AS type"
00238                 );
00239 
00240                 $ans = sql_query('select', $p, $db);
00241 
00242                 foreach ($ans as $a) {
00243                         $ok = $ok && may_delete_item($a['type'], $a['id'], $db);
00244                 }
00245 
00246                 break;
00247 
00248         case "email":
00249         case "file":
00250         case "url":
00251                 $ok = TRUE;
00252                 break;
00253 
00254         default:
00255                 $ok =   ($ans[0]['state_name'] == "inactive") or
00256                         ($ans[0]['state_name'] == "new");
00257                 break;
00258         }
00259 
00260         return $ok;
00261 }
00262 
00263 
00264 ?>

Generated on Fri Jul 14 17:38:55 2006 for semapp by  doxygen 1.4.7