Search code examples
zend-frameworkgdatagdata-apizend-gdata

How to move a google doc into a folder using Zend Gdata


Hi Guys I am really struggling with moving a document from my root folder to another folder using zend gdata here is how i am trying to do it, but its not working.

$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($gUser, $gPass, $service);
$link = "https://docs.google.com/feeds/documents/private/full/spreadsheet:0AUFNVEpLOVg2U0E"; // Not real id for privacy purposes
$docs = new Zend_GData_Docs($client);

// Attach a category object of folder to this entry
// I have tried many variations of this including attaching label categories
$cat = new Zend_Gdata_App_Extension_Category('My Folder Name','http://schemas.google.com/docs/2007#folder');

$entry = $docs->getDocumentListEntry($link);
$entry->setCategory(array($cat));

$return = $docs->updateEntry($entry,$entry->getEditLink()->href);

When I run this I get No errors, but nothing seems to change, and the return data does not contain the new category.

EDIT: Ok I realised that its not the category but the link that decides which "collection" (folder) a resource belongs too. https://developers.google.com/google-apps/documents-list/#managing_collections_and_their_contents says that each resource has as et of parent links, so I tried changing my code to do set link instead of set category, but this did not work.

$folder = "https://docs.google.com/feeds/documents/private/full/folder%3A0wSFA2WHc";
$rel = "http://schemas.google.com/docs/2007#parent";

$linkObj = new Zend_Gdata_App_Extension_Link($folder,$rel,'application/atom+xml', NULL,'Folder Name');
$links = $entry->getLink();
array_push($links,$linkObj);
$entry->setLink($links);

$return = $docs->updateEntry($entry,$entry->getEditLink()->href);

EDIT: SOLVED [nearly] OK Here is how to move/copy, sort of, from one folder to another: simpler than initially thought, but problem is it creates a reference and NOT a move! It now in both places at the same time....

// Folder you want to move too
$folder = "https://docs.google.com/feeds/folders/private/full/folder%asdsad";
$data = $docs->insertDocument($entry, $folder); // Entry is the entry you want moved using insert automatically assigns link & category for you...

Solution

  • To copy/ move a file into a folder do the following:

    $service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
    $client = Zend_Gdata_ClientLogin::getHttpClient($gUser, $gPass, $service); 
    $docs = new Zend_GData_Docs($client);
    
    $link = "https://docs.google.com/feeds/documents/private/full/spreadsheet:0AUFNVEpLOVg2U0E";
    $entry = $docs->getDocumentListEntry($link);
    // Folder (Collection you want to move to)
    $folder = "https://docs.google.com/feeds/folders/private/full/folder%asdsad";
    // Move
    $data = $docs->insertDocument($entry, $folder);
    

    Now some issues are:

    1. It seem to make reference copy only, this means its still the same document only it has an extra category and parent link (so its now in two places). This may explain it better: https://groups.google.com/forum/embed/?place=forum/google-documents-list-api#!topic/google-documents-list-api/OrLi2JCINw4

    I am sure there must be a way to do it "cleaner"/better but at least it moves it for now.