Move function finished
This commit is contained in:
parent
f73fe22483
commit
fd7a5e084f
36
XMLDB.php
36
XMLDB.php
@ -241,6 +241,8 @@ class XMLDB{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function arrayToNode($node){
|
private function arrayToNode($node){
|
||||||
|
/*if(isset($node[0]))
|
||||||
|
$node = $node[0];*/
|
||||||
if(!is_array($node) || !in_array($node['name'], array($this->_tableName, $this->_itemName)))
|
if(!is_array($node) || !in_array($node['name'], array($this->_tableName, $this->_itemName)))
|
||||||
return;
|
return;
|
||||||
$element = $this->_doc->createElement($node['name']);
|
$element = $this->_doc->createElement($node['name']);
|
||||||
@ -390,8 +392,15 @@ class XMLDB{
|
|||||||
if(!$this->tableAlreadyExists($table) || $this->pkAlreadyExists($node['attributes'][$this->_primaryKey], $table)){
|
if(!$this->tableAlreadyExists($table) || $this->pkAlreadyExists($node['attributes'][$this->_primaryKey], $table)){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
$totalItemInTable = $this->selectAllFromTable($table, 'count');
|
||||||
|
if($position == null || $position < $totalItemInTable){
|
||||||
$request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$table.'"]');
|
$request = $this->_xpath->query('//' . $this->_tableName . '[@name = "'.$table.'"]');
|
||||||
$request->item(0)->appendChild($element);
|
$request->item(0)->appendChild($element);
|
||||||
|
}else{
|
||||||
|
$itemsAfter = $this->selectAllFromTable($table, 'node');
|
||||||
|
$itemAfter = $itemsAfter->item($position-1);
|
||||||
|
$itemAfter->parentNode->insertBefore($element, $itemAfter);
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -427,7 +436,7 @@ class XMLDB{
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function updateItemValue($table, $attribute = null, $child = null, $value){
|
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] . '"]');
|
//$request = $this->_xpath->query('//'.$node.'[@' . $attribute[0] . ' = "' . $attribute[1] . '"]');
|
||||||
if($request->length == 1){
|
if($request->length == 1){
|
||||||
$request = $request->item(0);
|
$request = $request->item(0);
|
||||||
@ -440,6 +449,27 @@ class XMLDB{
|
|||||||
return false;
|
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){
|
public function deleteItem($table, $id = null, $attributes = null){
|
||||||
if($id == null && $attributes == null)
|
if($id == null && $attributes == null)
|
||||||
return false;
|
return false;
|
||||||
@ -481,11 +511,11 @@ class XMLDB{
|
|||||||
return $this->save();
|
return $this->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function move($node, $to, $itemAfter = ''){
|
public function move($node, $to, $position = null){
|
||||||
$this->_buffer = $node;
|
$this->_buffer = $node;
|
||||||
if($this->deleteNode($node)){
|
if($this->deleteNode($node)){
|
||||||
$nodeArray = $this->requestToArray($this->_buffer);
|
$nodeArray = $this->requestToArray($this->_buffer);
|
||||||
return $this->insert($nodeArray, $to);
|
return $this->insert($nodeArray, $to, $position);
|
||||||
}else
|
}else
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ class TestOfLogging extends UnitTestCase {
|
|||||||
$log->message('Trying to do several updates');
|
$log->message('Trying to do several updates');
|
||||||
$this->assertTrue($xmldbtest->updateItemAttribute('table1', array('id', 'links'), array('id', 'zelda')));
|
$this->assertTrue($xmldbtest->updateItemAttribute('table1', array('id', 'links'), array('id', 'zelda')));
|
||||||
$this->assertTrue($xmldbtest->updateItemValue('table1', array('id', 'notes'), null, 'booga!'));
|
$this->assertTrue($xmldbtest->updateItemValue('table1', array('id', 'notes'), null, 'booga!'));
|
||||||
|
|
||||||
$log->message('Trying to delete the item "clock" from "table1"');
|
$log->message('Trying to delete the item "clock" from "table1"');
|
||||||
//var_dump($xmldbtest->selectFromPK('table1', 'clock', 'node'), true);
|
//var_dump($xmldbtest->selectFromPK('table1', 'clock', 'node'), true);
|
||||||
//$this->assertTrue($xmldbtest->deleteNode($xmldbtest->selectFromPK('table1', 'clock', 'node')));
|
//$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->deleteITem('table3', '4'));
|
||||||
$this->assertTrue($xmldbtest2->insertItem(null, null, array('visibility'=>'true', 'x'=>'31'), 'table3'));
|
$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->assertTrue($xmldbtest2->insertItem(null, null, array('visibility'=>'true', 'x'=>'33'), 'table3'));
|
||||||
//$this->assertEqual($xmldbtest2->getAttribute('aivalue', $xmldbtest2->selectTable('table3')), 5);
|
//$this->assertEqual($xmldbtest2->getAttribute('aivalue', $xmldbtest2->selectTable('table3')), 5);
|
||||||
/*$log->message('Trying to select w/o anything');
|
/*$log->message('Trying to select w/o anything');
|
||||||
|
@ -13,4 +13,10 @@
|
|||||||
<religion>atheist</religion>
|
<religion>atheist</religion>
|
||||||
</item>
|
</item>
|
||||||
</table>
|
</table>
|
||||||
|
<table name="table2">
|
||||||
|
<item id="2">
|
||||||
|
</item>
|
||||||
|
<item id="53">
|
||||||
|
</item>
|
||||||
|
</table>
|
||||||
</Database>
|
</Database>
|
||||||
|
Loading…
Reference in New Issue
Block a user