[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Copyright (C) 2005-2010 Alfresco Software Limited. 4 * 5 * This file is part of Alfresco 6 * 7 * Alfresco is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * Alfresco is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with Alfresco. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 require_once $CFG->libdir.'/alfresco/Service/Store.php'; 22 require_once $CFG->libdir.'/alfresco/Service/ChildAssociation.php'; 23 require_once $CFG->libdir.'/alfresco/Service/Association.php'; 24 require_once $CFG->libdir.'/alfresco/Service/NamespaceMap.php'; 25 require_once $CFG->libdir.'/alfresco/Service/ContentData.php'; 26 require_once $CFG->libdir.'/alfresco/Service/VersionHistory.php'; 27 require_once $CFG->libdir.'/alfresco/Service/Version.php'; 28 29 class Node extends BaseObject 30 { 31 private $_session; 32 private $_store; 33 private $_id; 34 private $_type; 35 private $_aspects; 36 private $_properties; 37 private $_children; 38 private $_parents; 39 private $_primaryParent; 40 private $_isNewNode; 41 private $_associations; 42 private $_versionHistory; 43 private $origionalProperties; 44 private $addedAspects; 45 private $removedAspects; 46 private $addedChildren; 47 private $addedParents; 48 private $addedAssociations; 49 private $removedAssociations; 50 51 /** 52 * Constructor 53 */ 54 public function __construct($session, $store, $id) 55 { 56 $this->_session = $session; 57 $this->_store = $store; 58 $this->_id = $id; 59 $this->_isNewNode = false; 60 $this->addedChildren = array(); 61 $this->addedParents = array(); 62 $this->addedAssociations = array(); 63 } 64 65 /** 66 * Util method to create a node from a web service node structure. 67 */ 68 public static function createFromWebServiceData($session, $webServiceNode) 69 { 70 $scheme = $webServiceNode->reference->store->scheme; 71 $address = $webServiceNode->reference->store->address; 72 $id = $webServiceNode->reference->uuid; 73 74 $store = $session->getStore($address, $scheme); 75 $node = $session->getNode($store, $id); 76 $node->populateFromWebServiceNode($webServiceNode); 77 78 return $node; 79 } 80 81 public function setPropertyValues($properties) 82 { 83 // Check that the properties of the node have been populated 84 $this->populateProperties(); 85 86 // Set the property values 87 foreach ($properties as $name=>$value) 88 { 89 $name = $this->_session->namespaceMap->getFullName($name); 90 $this->_properties[$name] = $value; 91 } 92 } 93 94 public function updateContent($property, $mimetype, $encoding="UTF-8", $content=null) 95 { 96 list($property) = $this->_session->namespaceMap->getFullNames(array($property)); 97 $contentData = new ContentData($this, $property, $mimetype, $encoding); 98 if ($content != null) 99 { 100 $contentData->content = $content; 101 } 102 $this->_properties[$property] = $contentData; 103 104 return $contentData; 105 } 106 107 public function hasAspect($aspect) 108 { 109 list($aspect) = $this->_session->namespaceMap->getFullNames(array($aspect)); 110 $this->populateProperties(); 111 return in_array($aspect, $this->_aspects); 112 } 113 114 public function addAspect($aspect, $properties = null) 115 { 116 list($aspect) = $this->_session->namespaceMap->getFullNames(array($aspect)); 117 $this->populateProperties(); 118 119 if (in_array($aspect, $this->_aspects) == false) 120 { 121 $this->_aspects[] = $aspect; 122 if ($properties != null) 123 { 124 foreach ($properties as $name=>$value) 125 { 126 $name = $this->_session->namespaceMap->getFullName($name); 127 $this->_properties[$name] = $value; 128 } 129 } 130 131 $this->remove_array_value($aspect, $this->removedAspects); 132 $this->addedAspects[] = $aspect; 133 } 134 } 135 136 public function removeAspect($aspect) 137 { 138 list($aspect) = $this->_session->namespaceMap->getFullNames(array($aspect)); 139 $this->populateProperties(); 140 141 if (in_array($aspect, $this->_aspects) == true) 142 { 143 $this->remove_array_value($aspect, $this->_aspects); 144 $this->remove_array_value($aspect, $this->addedAspects); 145 $this->removedAspects[] = $aspect; 146 } 147 } 148 149 public function createChild($type, $associationType, $associationName) 150 { 151 list($type, $associationType, $associationName) = $this->_session->namespaceMap->getFullNames(array($type, $associationType, $associationName)); 152 153 $id = $this->_session->nextSessionId(); 154 $newNode = new Node($this->_session, $this->_store, $id); 155 $childAssociation = new ChildAssociation($this, $newNode, $associationType, $associationName, true); 156 157 $newNode->_isNewNode = true; 158 159 $newNode->_properties = array(); 160 $newNode->_aspects = array(); 161 $newNode->_properties = array(); 162 $newNode->_children = array(); 163 $newNode->origionalProperties = array(); 164 $newNode->addedAspects = array(); 165 $newNode->removedAspects = array(); 166 167 $newNode->_type = $type; 168 $newNode->_parents = array(); 169 $newNode->addedParents = array($this->__toString() => $childAssociation); 170 $newNode->_primaryParent = $this; 171 172 $this->addedChildren[$newNode->__toString()] = $childAssociation; 173 174 $this->_session->addNode($newNode); 175 176 return $newNode; 177 } 178 179 public function addChild($node, $associationType, $associationName) 180 { 181 list($associationType, $associationName) = $this->_session->namespaceMap->getFullNames(array($associationType, $associationName)); 182 183 $childAssociation = new ChildAssociation($this, $node, $associationType, $associationName, false); 184 $this->addedChildren[$node->__toString()] = $childAssociation; 185 $node->addedParents[$this->__toString()] = $childAssociation; 186 } 187 188 public function removeChild($childAssociation) 189 { 190 191 } 192 193 public function addAssociation($to, $associationType) 194 { 195 list($associationType) = $this->_session->namespaceMap->getFullNames(array($associationType)); 196 197 $association = new Association($this, $to, $associationType); 198 $this->addedAssociations[$to->__toString()] = $association; 199 } 200 201 public function removeAssociation($association) 202 { 203 204 } 205 206 public function createVersion($description=null, $major=false) 207 { 208 // We can only create a version if there are no outstanding changes for this node 209 if ($this->isDirty() == true) 210 { 211 throw new Exception("You must save any outstanding modifications before a new version can be created."); 212 } 213 214 // TODO implement major flag ... 215 216 $client = WebServiceFactory::getAuthoringService($this->_session->repository->connectionUrl, $this->_session->ticket); 217 $result = $client->createVersion( 218 array("items" => array("nodes" => $this->__toArray()), 219 "comments" => array("name" => "description", "value" => $description), 220 "versionChildren" => false)); 221 222 // Clear the properties and aspects 223 $this->_properties = null; 224 $this->_aspects = null; 225 226 // Get the version details 227 // TODO get some of the other details too ... 228 $versionId = $result->createVersionReturn->versions->id->uuid; 229 $versionStoreScheme = $result->createVersionReturn->versions->id->store->scheme; 230 $versionStoreAddress = $result->createVersionReturn->versions->id->store->address; 231 232 // Create the version object to return 233 return new Version($this->_session, new Store($this->_session, $versionStoreAddress, $versionStoreScheme), $versionId); 234 } 235 236 private function isDirty() 237 { 238 $result = true; 239 if ($this->_isNewNode == false && 240 count($this->getModifiedProperties()) == 0 && 241 ($this->addedAspects == null || count($this->addedAspects) == 0) && 242 ($this->removedAssociations == null || count($this->removedAssociations) == 0) && 243 ($this->addedChildren == null || count($this->addedChildren) == 0) && 244 ($this->addedAssociations == null || count($this->addedAssociations) == 0)) 245 { 246 $result = false; 247 } 248 return $result; 249 } 250 251 public function __get($name) 252 { 253 $fullName = $this->_session->namespaceMap->getFullName($name); 254 if ($fullName != $name) 255 { 256 $this->populateProperties(); 257 if (array_key_exists($fullName, $this->_properties) == true) 258 { 259 return $this->_properties[$fullName]; 260 } 261 else 262 { 263 return null; 264 } 265 } 266 else 267 { 268 return parent::__get($name); 269 } 270 } 271 272 public function __set($name, $value) 273 { 274 $fullName = $this->_session->namespaceMap->getFullName($name); 275 if ($fullName != $name) 276 { 277 $this->populateProperties(); 278 $this->_properties[$fullName] = $value; 279 280 // Ensure that the node and property details are stored on the contentData object 281 if ($value instanceof ContentData) 282 { 283 $value->setPropertyDetails($this, $fullName); 284 } 285 } 286 else 287 { 288 parent::__set($name, $value); 289 } 290 } 291 292 /** 293 * toString method. Returns node as a node reference style string. 294 */ 295 public function __toString() 296 { 297 return Node::__toNodeRef($this->_store, $this->id); 298 } 299 300 public static function __toNodeRef($store, $id) 301 { 302 return $store->scheme . "://" . $store->address . "/" . $id; 303 } 304 305 public function __toArray() 306 { 307 return array("store" => $this->_store->__toArray(), 308 "uuid" => $this->_id); 309 } 310 311 public function getSession() 312 { 313 return $this->_session; 314 } 315 316 public function getStore() 317 { 318 return $this->_store; 319 } 320 321 public function getId() 322 { 323 return $this->_id; 324 } 325 326 public function getIsNewNode() 327 { 328 return $this->_isNewNode; 329 } 330 331 public function getType() 332 { 333 $this->populateProperties(); 334 return $this->_type; 335 } 336 337 public function getAspects() 338 { 339 $this->populateProperties(); 340 return $this->_aspects; 341 } 342 343 public function getProperties() 344 { 345 $this->populateProperties(); 346 return $this->_properties; 347 } 348 349 public function setProperties($properties) 350 { 351 $this->populateProperties(); 352 $this->_properties = $properties; 353 } 354 355 /** 356 * Accessor for the versionHistory property. 357 * 358 * @return VersionHistory the versionHistory for the node, null is none 359 */ 360 public function getVersionHistory() 361 { 362 if ($this->_versionHistory == null) 363 { 364 $this->_versionHistory = new VersionHistory($this); 365 } 366 return $this->_versionHistory; 367 } 368 369 public function getChildren() 370 { 371 if ($this->_children == null) 372 { 373 $this->populateChildren(); 374 } 375 return $this->_children + $this->addedChildren; 376 } 377 378 public function getParents() 379 { 380 if ($this->_parents == null) 381 { 382 $this->populateParents(); 383 } 384 return $this->_parents + $this->addedParents; 385 } 386 387 public function getPrimaryParent() 388 { 389 if ($this->_primaryParent == null) 390 { 391 $this->populateParents(); 392 } 393 return $this->_primaryParent; 394 } 395 396 public function getAssociations() 397 { 398 if ($this->_associations == null) 399 { 400 $this->populateAssociations(); 401 } 402 return $this->_associations + $this->addedAssociations; 403 } 404 405 /** Methods used to populate node details from repository */ 406 407 private function populateProperties() 408 { 409 if ($this->_isNewNode == false && $this->_properties == null) 410 { 411 $result = $this->_session->repositoryService->get(array ( 412 "where" => array ( 413 "nodes" => array( 414 "store" => $this->_store->__toArray(), 415 "uuid" => $this->_id)))); 416 417 $this->populateFromWebServiceNode($result->getReturn); 418 } 419 } 420 421 private function populateFromWebServiceNode($webServiceNode) 422 { 423 $this->_type = $webServiceNode->type; 424 425 // Get the aspects 426 $this->_aspects = array(); 427 $aspects = $webServiceNode->aspects; 428 if (is_array($aspects) == true) 429 { 430 foreach ($aspects as $aspect) 431 { 432 $this->_aspects[] = $aspect; 433 } 434 } 435 else 436 { 437 $this->_aspects[] = $aspects; 438 } 439 440 // Set the property values 441 // NOTE: do we need to be concerned with identifying whether this is an array or not when there is 442 // only one property on a node 443 $this->_properties = array(); 444 foreach ($webServiceNode->properties as $propertyDetails) 445 { 446 $name = $propertyDetails->name; 447 $isMultiValue = $propertyDetails->isMultiValue; 448 $value = null; 449 if ($isMultiValue == false) 450 { 451 $value = $propertyDetails->value; 452 if ($this->isContentData($value) == true) 453 { 454 $value = new ContentData($this, $name); 455 } 456 } 457 else 458 { 459 $value = $propertyDetails->value; 460 } 461 $this->_properties[$name] = $value; 462 463 } 464 465 $this->origionalProperties = $this->_properties; 466 $this->addedAspects = array(); 467 $this->removedAspects = array(); 468 469 } 470 471 private function populateChildren() 472 { 473 // TODO should do some sort of limited pull here 474 $result = $this->_session->repositoryService->queryChildren(array("node" => $this->__toArray())); 475 $resultSet = $result->queryReturn->resultSet; 476 477 $children = array(); 478 $map = $this->resultSetToMap($resultSet); 479 foreach($map as $value) 480 { 481 $id = $value["{http://www.alfresco.org/model/system/1.0}node-uuid"]; 482 $store_scheme = $value["{http://www.alfresco.org/model/system/1.0}store-protocol"]; 483 $store_address = $value["{http://www.alfresco.org/model/system/1.0}store-identifier"]; 484 $assoc_type = $value["associationType"]; 485 $assoc_name = $value["associationName"]; 486 $isPrimary = $value["isPrimary"]; 487 $nthSibling = $value["nthSibling"]; 488 489 $child = $this->_session->getNode(new Store($this->_session, $store_address, $store_scheme), $id); 490 $children[$child->__toString()] = new ChildAssociation($this, $child, $assoc_type, $assoc_name, $isPrimary, $nthSibling); 491 } 492 493 $this->_children = $children; 494 } 495 496 private function populateAssociations() 497 { 498 // TODO should do some sort of limited pull here 499 $result = $this->_session->repositoryService->queryAssociated(array("node" => $this->__toArray(), 500 "association" => array("associationType" => null, 501 "direction" => null))); 502 $resultSet = $result->queryReturn->resultSet; 503 504 $associations = array(); 505 $map = $this->resultSetToMap($resultSet); 506 foreach($map as $value) 507 { 508 $id = $value["{http://www.alfresco.org/model/system/1.0}node-uuid"]; 509 $store_scheme = $value["{http://www.alfresco.org/model/system/1.0}store-protocol"]; 510 $store_address = $value["{http://www.alfresco.org/model/system/1.0}store-identifier"]; 511 $assoc_type = $value["associationType"]; 512 513 $to = $this->_session->getNode(new Store($this->_session, $store_address, $store_scheme), $id); 514 $associations[$to->__toString()] = new Association($this, $to, $assoc_type); 515 } 516 517 $this->_associations = $associations; 518 } 519 520 private function populateParents() 521 { 522 // TODO should do some sort of limited pull here 523 $result = $this->_session->repositoryService->queryParents(array("node" => $this->__toArray())); 524 $resultSet = $result->queryReturn->resultSet; 525 526 $parents = array(); 527 $map = $this->resultSetToMap($resultSet); 528 foreach($map as $value) 529 { 530 $id = $value["{http://www.alfresco.org/model/system/1.0}node-uuid"]; 531 $store_scheme = $value["{http://www.alfresco.org/model/system/1.0}store-protocol"]; 532 $store_address = $value["{http://www.alfresco.org/model/system/1.0}store-identifier"]; 533 $assoc_type = $value["associationType"]; 534 $assoc_name = $value["associationName"]; 535 $isPrimary = $value["isPrimary"]; 536 $nthSibling = $value["nthSibling"]; 537 538 $parent = $this->_session->getNode(new Store($this->_session, $store_address, $store_scheme), $id); 539 if ($isPrimary == "true" or $isPrimary == true) 540 { 541 $this->_primaryParent = $parent; 542 } 543 $parents[$parent->__toString()] = new ChildAssociation($parent, $this, $assoc_type, $assoc_name, $isPrimary, $nthSibling); 544 } 545 546 $this->_parents = $parents; 547 } 548 549 public function onBeforeSave(&$statements) 550 { 551 if ($this->_isNewNode == true) 552 { 553 $childAssociation = $this->addedParents[$this->_primaryParent->__toString()]; 554 555 $parentArray = array(); 556 $parent = $this->_primaryParent; 557 if ($parent->_isNewNode == true) 558 { 559 $parentArray["parent_id"] = $parent->id; 560 $parentArray["associationType"] = $childAssociation->type; 561 $parentArray["childName"] = $childAssociation->name; 562 } 563 else 564 { 565 $parentArray["parent"] = array( 566 "store" => $this->_store->__toArray(), 567 "uuid" => $this->_primaryParent->_id, 568 "associationType" => $childAssociation->type, 569 "childName" => $childAssociation->name); 570 } 571 572 $this->addStatement($statements, "create", 573 array("id" => $this->_id) + 574 $parentArray + 575 array( 576 "type" => $this->_type, 577 "property" => $this->getPropertyArray($this->_properties))); 578 } 579 else 580 { 581 // Add the update statement for the modified properties 582 $modifiedProperties = $this->getModifiedProperties(); 583 if (count($modifiedProperties) != 0) 584 { 585 $this->addStatement($statements, "update", array("property" => $this->getPropertyArray($modifiedProperties)) + $this->getWhereArray()); 586 } 587 588 // TODO deal with any deleted properties 589 } 590 591 // Update any modified content properties 592 if ($this->_properties != null) 593 { 594 foreach($this->_properties as $name=>$value) 595 { 596 if (($value instanceof ContentData) && $value->isDirty == true) 597 { 598 $value->onBeforeSave($statements, $this->getWhereArray()); 599 } 600 } 601 } 602 603 // Add the addAspect statements 604 if ($this->addedAspects != null) 605 { 606 foreach($this->addedAspects as $aspect) 607 { 608 $this->addStatement($statements, "addAspect", array("aspect" => $aspect) + $this->getWhereArray()); 609 } 610 } 611 612 // Add the removeAspect 613 if ($this->removedAspects != null) 614 { 615 foreach($this->removedAspects as $aspect) 616 { 617 $this->addStatement($statements, "removeAspect", array("aspect" => $aspect) + $this->getWhereArray()); 618 } 619 } 620 621 // Add non primary children 622 foreach($this->addedChildren as $childAssociation) 623 { 624 if ($childAssociation->isPrimary == false) 625 { 626 627 $assocDetails = array("associationType" => $childAssociation->type, "childName" => $childAssociation->name); 628 629 $temp = array(); 630 if ($childAssociation->child->_isNewNode == true) 631 { 632 $temp["to_id"] = $childAssociation->child->_id; 633 $temp = $temp + $assocDetails; 634 } 635 else 636 { 637 $temp["to"] = array( 638 "store" => $this->_store->__toArray(), 639 "uuid" => $childAssociation->child->_id) + 640 $assocDetails; 641 } 642 $temp = $temp + $this->getWhereArray(); 643 $this->addStatement($statements, "addChild", $temp); 644 } 645 } 646 647 // Add associations 648 foreach($this->addedAssociations as $association) 649 { 650 $temp = array("association" => $association->type); 651 $temp = $temp + $this->getPredicateArray("from", $this) + $this->getPredicateArray("to", $association->to); 652 $this->addStatement($statements, "createAssociation", $temp); 653 } 654 } 655 656 private function addStatement(&$statements, $statement, $body) 657 { 658 $result = array(); 659 if (array_key_exists($statement, $statements) == true) 660 { 661 $result = $statements[$statement]; 662 } 663 $result[] = $body; 664 $statements[$statement] = $result; 665 } 666 667 private function getWhereArray() 668 { 669 return $this->getPredicateArray("where", $this); 670 } 671 672 private function getPredicateArray($label, $node) 673 { 674 if ($node->_isNewNode == true) 675 { 676 return array($label."_id" => $node->_id); 677 } 678 else 679 { 680 return array( 681 $label => array( 682 "nodes" => $node->__toArray() 683 )); 684 } 685 } 686 687 private function getPropertyArray($properties) 688 { 689 $result = array(); 690 foreach ($properties as $name=>$value) 691 { 692 // Ignore content properties 693 if (($value instanceof ContentData) == false) 694 { 695 // TODO need to support multi values 696 $result[] = array( 697 "name" => $name, 698 "isMultiValue" => false, 699 "value" => $value); 700 } 701 } 702 return $result; 703 } 704 705 private function getModifiedProperties() 706 { 707 $modified = $this->_properties; 708 $origional = $this->origionalProperties; 709 $result = array(); 710 if ($modified != null) 711 { 712 foreach ($modified as $key=>$value) 713 { 714 // Ignore content properties 715 if (($value instanceof ContentData) == false) 716 { 717 if (array_key_exists($key, $origional) == true) 718 { 719 // Check to see if the value have been modified 720 if ($value != $origional[$key]) 721 { 722 $result[$key] = $value; 723 } 724 } 725 else 726 { 727 $result[$key] = $value; 728 } 729 } 730 } 731 } 732 return $result; 733 } 734 735 public function onAfterSave($idMap) 736 { 737 if (array_key_exists($this->_id, $idMap ) == true) 738 { 739 $uuid = $idMap[$this->_id]; 740 if ($uuid != null) 741 { 742 $this->_id = $uuid; 743 } 744 } 745 746 if ($this->_isNewNode == true) 747 { 748 $this->_isNewNode = false; 749 750 // Clear the properties and aspect 751 $this->_properties = null; 752 $this->_aspects = null; 753 } 754 755 // Update any modified content properties 756 if ($this->_properties != null) 757 { 758 foreach($this->_properties as $name=>$value) 759 { 760 if (($value instanceof ContentData) && $value->isDirty == true) 761 { 762 $value->onAfterSave(); 763 } 764 } 765 } 766 767 $this->origionalProperties = $this->_properties; 768 769 if ($this->_aspects != null) 770 { 771 // Calculate the updated aspect list 772 if ($this->addedAspects != null) 773 { 774 $this->_aspects = $this->_aspects + $this->addedAspects; 775 } 776 if ($this->removedAspects != null) 777 { 778 foreach ($this->_aspects as $aspect) 779 { 780 if (in_array($aspect, $this->removedAspects) == true) 781 { 782 $this->remove_array_value($aspect, $this->_aspects); 783 } 784 } 785 } 786 } 787 $this->addedAspects = array(); 788 $this->removedAspects = array(); 789 790 if ($this->_parents != null) 791 { 792 $this->_parents = $this->_parents + $this->addedParents; 793 } 794 $this->addedParents = array(); 795 796 if ($this->_children != null) 797 { 798 $this->_children = $this->_children + $this->addedChildren; 799 } 800 $this->addedChildren = array(); 801 802 if ($this->_associations != null) 803 { 804 $this->_associations = $this->_associations + $this->addedAssociations; 805 } 806 $this->addedAssociations = array(); 807 } 808 } 809 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |