Commit 952d9101124f074005f64bad5c32387a1eb3ddf5

Authored by Benjamin Renard
1 parent e3dab642

Add function to update a shared object

php/classes/SharedObjectMgrAbstract.php
@@ -81,6 +81,30 @@ abstract class SharedObjectMgrAbstract { @@ -81,6 +81,30 @@ abstract class SharedObjectMgrAbstract {
81 return array('success' => TRUE, 'object_id' => $newId); 81 return array('success' => TRUE, 'object_id' => $newId);
82 } 82 }
83 83
  84 + public function delete($folder_name, $objectId) {
  85 + $dst_folder_path = $this->getFolderDirPath($folder_name);
  86 + if (!is_dir($dst_folder_path))
  87 + return array('success' => false, 'message' => 'Cannot get folder path');
  88 +
  89 + $dst_header_path = $this->getHeaderDirPath($folder_name);
  90 + if (is_dir($dst_header_path)) {
  91 + $headerFilePath = $this->getHeaderFilePath($folder_name, $objectId);
  92 + if (is_file($headerFilePath)) {
  93 + unlink($headerFilePath);
  94 + }
  95 + }
  96 +
  97 + $dst_data_path = $this->getDataDirPath($folder_name);
  98 + if (is_dir($dst_data_path)) {
  99 + $dataFilePath = $this->getDataFilePath($folder_name, $objectId);
  100 + if (is_file($dataFilePath)) {
  101 + unlink($dataFilePath);
  102 + }
  103 + }
  104 +
  105 + return array('success' => TRUE, 'object_id' => $objectId);
  106 + }
  107 +
84 public function createFolder($folder_name) { 108 public function createFolder($folder_name) {
85 $folder_path = $this->getFolderDirPath($folder_name); 109 $folder_path = $this->getFolderDirPath($folder_name);
86 if (is_dir($folder_path)) { 110 if (is_dir($folder_path)) {
php/classes/SharedObjectTreeFile.php
@@ -185,6 +185,34 @@ class SharedObjectTreeFile { @@ -185,6 +185,34 @@ class SharedObjectTreeFile {
185 } 185 }
186 return ""; 186 return "";
187 } 187 }
  188 +
  189 + public function getObjectsByName($object_type, $folder_name, $object_name) {
  190 + $folderNode = $this->getObjectFolderNode($object_type, $folder_name);
  191 +
  192 + if (!isset($folderNode))
  193 + return NULL;
  194 +
  195 + $objectNodes = $folderNode->getElementsByTagName($object_type);
  196 +
  197 + $result = array();
  198 + foreach ($objectNodes as $objectNode) {
  199 + if ($objectNode->getAttribute('name') == $object_name)
  200 + $result[] = $objectNode->getAttribute("xml:id");
  201 + }
  202 +
  203 + return $result;
  204 + }
  205 +
  206 + public function deleteObjectByObjectId($object_type, $folder_name, $object_id) {
  207 + $node = $this->getObjectNode($object_type, $folder_name, $object_id);
  208 + if (!isset($node)) {
  209 + return array('success' => FALSE, 'message' => 'Cannot retrieve object node in tree');
  210 + }
  211 + $node->parentNode->removeChild($node);
  212 + $this->doc->save($this->getFilePath());
  213 +
  214 + return array('success' => TRUE);
  215 + }
188 216
189 private function init() { 217 private function init() {
190 if (!file_exists($this->getFilePath())) 218 if (!file_exists($this->getFilePath()))
@@ -283,7 +311,7 @@ class SharedObjectTreeFile { @@ -283,7 +311,7 @@ class SharedObjectTreeFile {
283 311
284 return NULL; 312 return NULL;
285 } 313 }
286 - 314 +
287 private function createObjectNode($object_type, $folder_name, $object_id) { 315 private function createObjectNode($object_type, $folder_name, $object_id) {
288 $listNode = $this->getObjectListNode($object_type); 316 $listNode = $this->getObjectListNode($object_type);
289 317
php/classes/SharedObjectsMgr.php
@@ -50,6 +50,29 @@ class SharedObjectsMgr { @@ -50,6 +50,29 @@ class SharedObjectsMgr {
50 50
51 return $result; 51 return $result;
52 } 52 }
  53 +
  54 + public function update($object_type, $object_name, $folder_id, $src_object_path, $description, $sharedBy) {
  55 + if (!array_key_exists($object_type, $this->sharedObjectsMgr) || !isset($this->sharedObjectsMgr[$object_type]))
  56 + return array('success' => FALSE, 'message' => 'Unknown type');
  57 +
  58 + $folder_name = $this->treeFile->getFolderNameById($object_type, $folder_id);
  59 +
  60 + if (empty($folder_name))
  61 + return array('success' => FALSE, 'message' => 'Cannot retrieve folder');
  62 +
  63 + $objects = $this->treeFile->getObjectsByName($object_type, $folder_name, $object_name);
  64 +
  65 + if (!empty($objects)) {
  66 + //delete old objects
  67 + foreach ($objects as $object_id) {
  68 + $result = $this->treeFile->deleteObjectByObjectId($object_type, $folder_name, $object_id);
  69 + $result = $this->sharedObjectsMgr[$object_type]->delete($folder_name, $object_id);
  70 + }
  71 + }
  72 +
  73 + //add new object
  74 + return $this->add($object_type, $object_name, $folder_id, $src_object_path, $description, $sharedBy);
  75 + }
53 76
54 public function getFolders($object_type) { 77 public function getFolders($object_type) {
55 if (!array_key_exists($object_type, $this->sharedObjectsMgr) || !isset($this->sharedObjectsMgr[$object_type])) 78 if (!array_key_exists($object_type, $this->sharedObjectsMgr) || !isset($this->sharedObjectsMgr[$object_type]))