From 8796f7a504ece9bd35b52e0ea4f41aad7f2004e6 Mon Sep 17 00:00:00 2001 From: edward Date: Thu, 31 Mar 2016 10:06:38 +0800 Subject: [PATCH] RooPostTrait.php --- RooPostTrait.php | 142 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/RooPostTrait.php b/RooPostTrait.php index 1863bcfe..bd4b2713 100644 --- a/RooPostTrait.php +++ b/RooPostTrait.php @@ -158,5 +158,147 @@ trait Pman_Core_RooPostTrait { + } + + function delete($x, $req) + { + // do we really delete stuff!?!?!? + if (empty($req['_delete'])) { + $this->jerr("Delete Requested with no value"); + } + // build a list of tables to queriy for dependant data.. + $map = $x->links(); + + $affects = array(); + + $all_links = $GLOBALS['_DB_DATAOBJECT']['LINKS'][$x->_database]; + foreach($all_links as $tbl => $links) { + foreach($links as $col => $totbl_col) { + $to = explode(':', $totbl_col); + if ($to[0] != $x->tableName()) { + continue; + } + + $affects[$tbl .'.' . $col] = true; + } + } + // collect tables + + // echo '
';print_r($affects);exit;
+       // DB_Dataobject::debugLevel(1);
+       
+        
+        $clean = create_function('$v', 'return (int)$v;');
+        
+        $bits = array_map($clean, explode(',', $req['_delete']));
+        
+       // print_r($bits);exit;
+         
+        // let's assume it has a key!!!
+        
+        
+        $x->whereAdd($this->key .'  IN ('. implode(',', $bits) .')');
+        if (!$x->find()) {
+            $this->jerr("Nothing found to delete");
+        }
+        $errs = array();
+        while ($x->fetch()) {
+            $xx = clone($x);
+            
+           
+            // perms first.
+            
+            if (!$this->checkPerm($x,'D') )  {
+                $this->jerr("PERMISSION DENIED (d)");
+            }
+            
+            $match_ar = array();
+            foreach($affects as $k=> $true) {
+                $ka = explode('.', $k);
+                
+                $chk = DB_DataObject::factory($ka[0]);
+                if (!is_a($chk,'DB_DataObject')) {
+                    $this->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
+                }
+               // print_r(array($chk->tablename() , $ka[1] ,  $xx->tablename() , $this->key ));
+                $chk->{$ka[1]} =  $xx->{$this->key};
+                
+                if (count($chk->keys())) {
+                    $matches = $chk->count();
+                } else {
+                    //DB_DataObject::DebugLevel(1);
+                    $matches = $chk->count($ka[1]);
+                }
+                
+                if ($matches) {
+                    $chk->_match_key = $ka[1];
+                    $match_ar[] = clone($chk);
+                    continue;
+                }          
+            }
+            
+            $has_beforeDelete = method_exists($xx, 'beforeDelete');
+            // before delte = allows us to trash dependancies if needed..
+            $match_total = 0;
+            
+            if ( $has_beforeDelete ) {
+                if ($xx->beforeDelete($match_ar, $this) === false) {
+                    $errs[] = "Delete failed ({$xx->id})\n".
+                        (isset($xx->err) ? $xx->err : '');
+                    continue;
+                }
+                // refetch affects..
+                
+                $match_ar = array();
+                foreach($affects as $k=> $true) {
+                    $ka = explode('.', $k);
+                    $chk = DB_DataObject::factory($ka[0]);
+                    if (!is_a($chk,'DB_DataObject')) {
+                        $this->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
+                    }
+                    $chk->{$ka[1]} =  $xx->{$this->key};
+                    $matches = $chk->count();
+                    $match_total += $matches;
+                    if ($matches) {
+                        $chk->_match_key = $ka[1];
+                        $match_ar[] = clone($chk);
+                        continue;
+                    }          
+                }
+                
+            }
+            
+            if (!empty($match_ar)) {
+                $chk = $match_ar[0];
+                $chk->limit(1);
+                $o = $chk->fetchAll();
+                $key = isset($chk->_match_key) ?$chk->_match_key  : '?unknown column?';
+                $desc =  $chk->tableName(). '.' . $key .'='.$xx->{$this->key} ;
+                if (method_exists($chk, 'toEventString')) {
+                    $desc .=  ' : ' . $o[0]->toEventString();
+                }
+                    
+                $this->jerr("Delete Dependant records ($match_total  found),  " .
+                             "first is ( $desc )");
+          
+            }
+            
+            DB_DataObject::Factory('Events')->logDeletedRecord($x);
+            
+            $this->addEvent("DELETE", $x);
+            
+            $xx->delete();
+            
+            if (method_exists($xx,'onDelete')) {
+                $xx->onDelete($req, $this);
+            }
+            
+            
+        }
+        if ($errs) {
+            $this->jerr(implode("\n
", $errs)); + } + $this->jok("Deleted"); + } } \ No newline at end of file -- 2.39.2