Move function finished

This commit is contained in:
Chouchen 2010-10-15 11:13:29 +00:00
parent f73fe22483
commit fd7a5e084f
3 changed files with 44 additions and 5 deletions

View File

@ -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;
}

View File

@ -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');

View File

@ -13,4 +13,10 @@
<religion>atheist</religion>
</item>
</table>
<table name="table2">
<item id="2">
</item>
<item id="53">
</item>
</table>
</Database>