May 17, 2007

Working with the Alfresco PHP Library - Browsing Folders

Filed under: PHP, Alfresco — Marcus @ 10:21 am

Alfresco’s 1.4 version of the PHP library has been around as a technical preview, but doesn’t seem to have progressed much further. Regardless, it’s still quite functional, and allows you to do most things in a more sane way than the previous releases of it. Over the next couple of weeks I’ll be playing around with some of the functionality available, and given that some people are still a bit unsure of how everything works in the new version, hopefully I can help shed some light on things. First up; browsing spaces.

Everything in the PHP API requires the use of the Session object, either directly or indirectly. Connecting to the repository must be done before anything else, and is quite simple; provide the username, password, and URL of where the repository is located (remember that api bit!).

Code (php)
  1.  
  2. $session = Session::create("admin", "admin", "http://localhost:8080/alfresco/api");
  3. $spacesStore = new SpacesStore($session);

Next, I’m going to get the selected node; note that if I haven’t specified one on the request, I’ll just get the company home.

Code (php)
  1.  
  2. $selected = isset($_GET[‘id’]) ? $_GET[‘id’] : null;
  3. $root = null;
  4. if (!$selected) {
  5. $root = $spacesStore->getCompanyHome();
  6. } else {
  7. $root = getNode($session, $spacesStore, $selected);
  8. }
  9.  
  10. function getNode($session, $store, $id)
  11. {
  12. $nodes = $session->query($store, ‘@sys:node-uuid:"’.$id.‘"’);
  13. return isset($nodes[0]) ? $nodes[0] : null;
  14. }

With the node in hand, I can now iterate over its children and list out a browseable listing.

Code (php)
  1.  
  2. $children = $root->getChildren();
  3. echo ‘<table>’;
  4. foreach ($children as $childAssociation) {
  5. /* @var $childAssociation ChildAssociation */
  6.  
  7. $child = $childAssociation->getChild();
  8. /* @var $child Node */
  9. echo ‘<tr>’;
  10. echo ‘<td>’;
  11. echo preg_replace(‘/{.*?}/’, , $child->getType());
  12. echo ‘</td>’;
  13. echo ‘<td>’;
  14. echo ‘<a href="?id=’.$child->getId().‘">’.$childAssociation->getChild()->cm_name."</a><br/>";
  15. echo ‘</td>’;
  16. echo ‘</tr>’;
  17. }
  18. echo ‘</table>’;

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

.

Powered by WordPress