Thanks heaps nostradamus1935. You put me on the right track. I made some slight changes to the way you went about doing it. I checked out FileIframeField to get the ideas of how to save an image to the database then assign it to a dataobjects has_one relationship.
The basic idea here was to have a field that you input a vimeo video ID into and it will grab the video's thumbnail. If you re save the object it will update the image from vimeo.
You could use the same saving code for files as well as they require the same fields.
Here's what I ended up writing in the dataobject that has_one image eg. a page of with a thumbnail:
static $db = array(
'VideoID' => 'HTMLText'
);
static $has_one = array(
'ThumbnailImage' => 'Image'
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new NumericField('VideoID', 'Vimeo Video Number'));
return $fields;
}
function onBeforeWrite() {
if(!empty($this->VideoID) && $this->VideoID!=0) {
// call for large image (thumbnail_url)
$ThumbnailCall = new RestfulService('http://vimeo.com/api/oembed.xml', 0);
$params = array('url' => 'http://vimeo.com/'.$this->VideoID);
$ThumbnailCall->setQueryString($params);
$xmlResponse = $ThumbnailCall->request()->getBody();
$thumbnailURL = $ThumbnailCall->getValue($xmlResponse, 'thumbnail_url');
if(preg_match('/(.JPG|.jpg)$/',$thumbnailURL)) // check if thumbnail url is a jpg
{
$folderToSave = 'assets/Uploads/'; //if you want to save into another folder you will need to add one via the cms then change this path. Folders are a type of Folder DataObject.
$folderObject = DataObject::get_one("Folder", "`Filename` = '{$folderToSave}'");
if($folderObject)
{
//get image from url and save to folder
$thumbnailToCopy = file_get_contents($thumbnailURL);
$thumbnailExtraName = preg_replace('/.*?(_[0-9]{3,4})(.JPG|.jpg)$/','$1$2',$thumbnailURL); // renaming of the file, I strip off everything except the image width and the file extension.
$thumbnailBaseName = str_replace('-', '_', $this->URLSegment); // takes the url segment as its base
$thumbnailName = $thumbnailBaseName.'_thumbnail'.$thumbnailExtraName;
$thumbnailFile = fopen('./../'.$folderToSave.$thumbnailName, 'w'); // opens existing or creates a new file
fwrite($thumbnailFile, $thumbnailToCopy); //overwrites file
fclose($thumbnailFile); //close file
if(!DataObject::get_one('Image', "`Name` = '{$thumbnailName}'")) //checks if dataObject already exists, stops multiple records being created.
{
$thumbnailObject = Object::create('Image');
$thumbnailObject->ParentID = $folderObject->ID; //assign folder of image as parent
$thumbnailObject->Name = $thumbnailName; //this function also sets the images Filename and title in a round about way. (see setName() in File.php)
$thumbnailObject->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0); //assign current user as Owner
$thumbnailObject->write(); //write the object to the database
$this->ThumbnailImageID = DataObject::get_one('Image', "`Name` = '{$thumbnailName}'")->ID; //assign the object to the has_one, this took me ages to get. Basically it is the name of the has_one relationship with 'ID' tagged on the end and it must equal the new image objects ID.
}
}
}
}
parent::onBeforeWrite();
}