From fd7a5e084ffa51ed6bd7fa5ea5b6e6f3c012e5af Mon Sep 17 00:00:00 2001 From: Chouchen Date: Fri, 15 Oct 2010 11:13:29 +0000 Subject: [PATCH] Move function finished --- XMLDB.php | 40 +++++++++++++++++++++++++++++++++++----- XMLDBtestUnit.php | 3 +++ database2.xml | 6 ++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/XMLDB.php b/XMLDB.php index dcac50c..39b45de 100644 --- a/XMLDB.php +++ b/XMLDB.php @@ -241,6 +241,8 @@ class XMLDB{ } private function arrayToNode($node){ + /*if(isset($node[0])) + $node = $node[0];*/ if(!is_array($node) || !in_array($node['name'], array($this->_tableName, $this->_itemName))) return; $element = $this->_doc->createElement($node['name']); @@ -390,8 +392,15 @@ class XMLDB{ if(!$this->tableAlreadyExists($table) || $this->pkAlreadyExists($node['attributes'][$this->_primaryKey], $table)){ return false; } - $request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$table.'"]'); - $request->item(0)->appendChild($element); + $totalItemInTable = $this->selectAllFromTable($table, 'count'); + if($position == null || $position < $totalItemInTable){ + $request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$table.'"]'); + $request->item(0)->appendChild($element); + }else{ + $itemsAfter = $this->selectAllFromTable($table, 'node'); + $itemAfter = $itemsAfter->item($position-1); + $itemAfter->parentNode->insertBefore($element, $itemAfter); + } }else{ return false; } @@ -427,7 +436,7 @@ class XMLDB{ * @return bool */ public function updateItemValue($table, $attribute = null, $child = null, $value){ - $request = $this->select($table, null, array($attribute[0]=>$attribute[1]), null, 'node', '/'.$this->_itemName); + $request = $this->select($table, null, array($attribute[0]=>$attribute[1]), $child, 'node', '/'.$this->_itemName); //$request = $this->_xpath->query('//'.$node.'[@' . $attribute[0] . ' = "' . $attribute[1] . '"]'); if($request->length == 1){ $request = $request->item(0); @@ -440,6 +449,27 @@ class XMLDB{ return false; } + public function updateChildValue($table, $node, $child, $value){ + if($node->length == 1){ + $node = $node->item(0); + $newChild = $this->_doc->createElement($child, $value); + $old_element_childNodes = $node->childNodes; + $length = $old_element_childNodes->length; + $index = 0; + for($i = 0; $i < $length; $i++) + { + if($old_element_childNodes->item($i)->nodeName == $child){ + $index = $i; + break; + } + } + //$request = $node->getElementsByTagName($child)->item(0); + if($node->replaceChild($newChild, $old_element_childNodes->item($index))) + return $this->save(); + } + return false; + } + public function deleteItem($table, $id = null, $attributes = null){ if($id == null && $attributes == null) return false; @@ -481,11 +511,11 @@ class XMLDB{ return $this->save(); } - public function move($node, $to, $itemAfter = ''){ + public function move($node, $to, $position = null){ $this->_buffer = $node; if($this->deleteNode($node)){ $nodeArray = $this->requestToArray($this->_buffer); - return $this->insert($nodeArray, $to); + return $this->insert($nodeArray, $to, $position); }else return false; } diff --git a/XMLDBtestUnit.php b/XMLDBtestUnit.php index f335963..d46243c 100644 --- a/XMLDBtestUnit.php +++ b/XMLDBtestUnit.php @@ -37,6 +37,7 @@ class TestOfLogging extends UnitTestCase { $log->message('Trying to do several updates'); $this->assertTrue($xmldbtest->updateItemAttribute('table1', array('id', 'links'), array('id', 'zelda'))); $this->assertTrue($xmldbtest->updateItemValue('table1', array('id', 'notes'), null, 'booga!')); + $log->message('Trying to delete the item "clock" from "table1"'); //var_dump($xmldbtest->selectFromPK('table1', 'clock', 'node'), true); //$this->assertTrue($xmldbtest->deleteNode($xmldbtest->selectFromPK('table1', 'clock', 'node'))); @@ -76,6 +77,8 @@ class TestOfLogging extends UnitTestCase { } $this->assertTrue($xmldbtest2->deleteITem('table3', '4')); $this->assertTrue($xmldbtest2->insertItem(null, null, array('visibility'=>'true', 'x'=>'31'), 'table3')); + $this->assertTrue($xmldbtest2->updateChildValue('table3', $xmldbtest2->selectFromPK('table3', '5', 'node'), 'prenom', 'Sophia')); + $this->assertTrue($xmldbtest2->move($xmldbtest2->selectFromPK('table1', '3', 'node'), 'table2', 2)); //$this->assertTrue($xmldbtest2->insertItem(null, null, array('visibility'=>'true', 'x'=>'33'), 'table3')); //$this->assertEqual($xmldbtest2->getAttribute('aivalue', $xmldbtest2->selectTable('table3')), 5); /*$log->message('Trying to select w/o anything'); diff --git a/database2.xml b/database2.xml index 0a61f22..c759167 100644 --- a/database2.xml +++ b/database2.xml @@ -13,4 +13,10 @@ atheist + + + + + +