static function import($page)
 {
     include_once '../googledocspage/libs/simplehtmldom/simple_html_dom.php';
     //if import url is set, use that, else fall back on google doc id
     if (strlen($page->ImportURL) > 1) {
         $url = $page->ImportURL;
     } else {
         $url = GoogleDocsPage::$gdoc_pub_urlbase . $page->GoogleDocID;
     }
     //echo $url;
     $html = file_get_html($url);
     //$contents = $html->find('div[id="contents"]', 0)->innertext;
     $contents = $html->find('div[id="contents"]', 0);
     // remove h1
     //var_dump($contents->find('h1'));
     if (isset($contents)) {
         foreach ($contents->find('h1') as $e) {
             $e->outertext = '';
         }
     } else {
         return "Error retrieving document. <br /> Try visiting this URL: <br /><br /><a href=\"{$url}\">{$url}</a>";
     }
     // save style
     $style = "";
     foreach ($contents->find('style') as $e) {
         $style = $e->innertext;
     }
     $e->outertext = '';
     //changing img path
     $i = 1;
     foreach ($html->find('img') as $e) {
         if ($i < 99) {
             //echo $e->src . "<br />";
             //$e->outertext = '';
             $e->src = "http://docs.google.com/document/" . $e->src;
             //var_dump($page->PageID);
             $folderPath = 'import/' . $page->ID;
             //var_dump($folderPath);
             $folder = Folder::findOrMake($folderPath);
             //$tempFileName = $i . ".png";
             $tempFileName = $i;
             $filepath = "assets/" . $folderPath . "/" . $tempFileName;
             $src = str_replace("amp;", "", $e->src);
             $img = file_get_contents($src);
             //$size = getimagesize($img);
             //var_dump($img);
             $file = File::find($filepath);
             if (!$file) {
                 $file = new File();
                 $file->Filename = $filepath;
             }
             file_put_contents(Director::baseFolder() . "/" . $filepath, $img);
             //$file->Name = $a["FileName"];
             //$file->setName($tempFileName);
             $file->write();
             $file->setName($i);
             $file->setParentID($folder->ID);
             //$file->setName($filepath);
             $file->ClassName = "Image";
             $file->write();
             $e->src = "/" . $filepath;
         }
         $i = $i + 1;
     }
     //echo '<style>.c2 { font-weight:bold;}</style>';
     //echo $contents->innertext;
     //echo "importing";
     $import = new GoogleDocsPage_Import();
     //$import->Imported = date("Y-m-d H:i:s");
     $import->Content = $contents->innertext;
     $import->Css = $style;
     $import->CssParsed = GoogleDocsPage_Import::parsecss($style);
     $import->PageID = $page->ID;
     $import->write();
     //this is not neccessary, as it is done already be referencing the PageID
     //$pageimports = $page->Imports();
     //$pageimports->add($import);
     //writing content to the page
     //making sure the "real" page object is being used
     $page = SiteTree::get_by_id("Page", $page->ID);
     $page->Content = $import->Content;
     $page->writeToStage('Stage');
     $page->Publish('Stage', 'Live');
     $page->Status = "Published";
     $page->flushCache();
     return "import successful";
     //return $import;
 }
 public function saveImportForm($data, $form)
 {
     if (isset($data['imported_files']) && is_array($data['imported_files'])) {
         $_POST['uploaded_files'] = array();
         // If the user has set a custom upload folder, cut a new copy of the file when importing
         $custom_folder = $this->getUploadFolder() != "Uploads" ? Folder::findOrMake($this->getCleanUploadFolder()) : false;
         foreach ($data['imported_files'] as $file_id) {
             $file = DataObject::get_by_id("File", $file_id);
             if ($custom_folder && $file->ParentID != $custom_folder->ID) {
                 $new_path = Director::baseFolder() . '/' . $custom_folder->Filename . $file->Name;
                 copy($file->getFullPath(), $new_path);
                 $new_file = new File();
                 $new_file->setFilename($custom_folder->Filename . $file->Name);
                 $new_file->setName($file->Name);
                 $new_file->setParentID($custom_folder->ID);
                 $new_file->write();
                 $file = $new_file;
                 $file_id = $new_file->ID;
             }
             // If something other than File has been specified as the linked file class,
             // we need to "upgrade" the imported file to the correct class.
             if ($this->fileClassName != "File" && $file->ClassName != $this->fileClassName) {
                 $file = $file->newClassInstance($this->fileClassName);
                 $file->write();
             }
             $owner_id = $data['parentIDName'];
             if ($this->hasDataObject) {
                 $do_class = $data['dataObjectClassName'];
                 $idxfield = $data['fileFieldName'] . "ID";
                 $obj = new $do_class();
                 $obj->{$idxfield} = $file_id;
                 $obj->{$owner_id} = $data['controllerID'];
                 $obj->write();
                 $_POST['uploaded_files'][] = $obj->ID;
             } else {
                 if ($file = DataObject::get_by_id($this->fileClassName, $file_id)) {
                     $id_field = $this->controllerFieldName . "ID";
                     if ($file->hasField($owner_id)) {
                         $file->{$owner_id} = $this->controllerID;
                         $file->write();
                     }
                 }
             }
         }
         $form = $this->EditUploadedForm();
         return $this->customise(array('String' => is_string($form), 'DetailForm' => $form))->renderWith($this->templatePopup);
     }
 }
 function onAfterWrite()
 {
     parent::onAfterWrite();
     $page = $this->owner;
     //Importing - this should only happen once
     //if ($page->ImportComplete) {
     //	return;
     //}
     //Can't remember what this was for
     //Probably so we don't produce any infinite loops on write
     if (AggregatedBlogEntryExtension::$afterWriteLoop == $page->ID) {
         //echo "1";
         return NULL;
     } else {
         //echo "0";
         AggregatedBlogEntryExtension::$afterWriteLoop = $page->ID;
     }
     //Debug::dump($page->Title . " (#$page->ID)");
     //Importing images
     $html = HtmlDomParser::str_get_html($page->Content);
     //changing img path
     $i = 1;
     foreach ($html->find('img') as $e) {
         if ($i < 99) {
             $folderPath = 'import/' . $page->ID;
             $parsedSource = parse_url($e->src);
             //new way of checking for external images
             //-if scheme is set we download the image,
             //else we'll leave it, as then it will probably already
             //have been downloaded
             if (isset($parsedSource['scheme'])) {
                 $source = $parsedSource['scheme'] . "://" . $parsedSource['host'] . $parsedSource['path'];
                 $sourceName = pathinfo($parsedSource['path'], PATHINFO_BASENAME);
                 //Debug::dump($source);
                 //Debug::dump($sourceName);
                 $folder = Folder::find_or_make($folderPath);
                 //$tempFileName = $i;
                 $tempFileName = $sourceName;
                 $filepath = "assets/" . $folderPath . "/" . $tempFileName;
                 $src = str_replace("amp;", "", $e->src);
                 $img = file_get_contents($src);
                 //$size = getimagesize($img);
                 //var_dump($img);
                 $file = File::find($filepath);
                 if (!$file) {
                     $file = new File();
                     $file->Filename = $filepath;
                 }
                 file_put_contents(Director::baseFolder() . "/" . $filepath, $img);
                 //$file->Name = $a["FileName"];
                 //$file->setName($tempFileName);
                 $file->write();
                 $file->setName($i);
                 $file->setParentID($folder->ID);
                 //$file->setName($filepath);
                 $file->ClassName = "Image";
                 $file->write();
                 $e->src = "/" . $filepath;
             }
         }
         $i = $i + 1;
     }
     $page->Content = $html->innertext;
     $page->ImportComplete = true;
     $page->writeToStage('Stage');
     $page->publish('Stage', 'Live');
     //$this->owner->write();
     //Tags is temporarily disabled (will be added later)
     ////Setting Tags
     //$tags = split(" *, *", trim($this->owner->Tags));
     //$tagsComponentSet = $page->RelationTags();
     //if ($tagsComponentSet->exists()) {
     //	//It seems tags have already been imported.
     //	//Do nothing
     //	//echo "do nothing";
     //} else {
     //	//echo "do something";
     //	$newTags = array();
     //	if ($tags) foreach($tags as $tag) {
     //		//only import tags t
     //		if (($tag != "yoga") && (strlen($tag) > 4)) {
     //			$tagString = strtolower(str_replace(" ","_",$tag));
     //			$tagObj = DataObject::get_one("Tag", "Title = '" . $tagString . "'");
     //			if(!$tagObj) {
     //				//if tag doesn't exist, it will be created
     //				$tagObj = new Tag();
     //				$tagObj->Title = $tagString;
     //				$tagObj->Status = "PendingApproval";
     //				$tagObj->write();
     //			}
     //			$newTags[] = $tagObj->ID;
     //		}
     //	}
     //	// set new tags
     //	$tagsComponentSet->setByIdList($newTags);
     //	//$page->writeToStage('Stage');
     //	//$page->publish('Stage', 'Live');
     //}
     //echo "working on " . $page->ID;
     //Setting Thumb URL
     //$thumb = $page->ThumbnailImage();
     //if (!($thumb->exists())) {
     //	//echo "checking for/creating thumb image";
     //	$html = HtmlDomParser::str_get_html($page->Content);
     //
     //	$thumbURL = "";
     //	$i = 0;
     //	foreach($html->find('img') as $e) {
     //		if ($i == 0) {
     //			$thumbURL = $e->src;
     //			$i++;
     //		}
     //	}
     //
     //	$folderPath = 'import/' . $page->ID;
     //	$folder = Folder::findOrMake($folderPath);
     //
     //	$filename = strtolower($thumbURL) ;
     //	$exts = split("[/\\.]", $filename) ;
     //	$n = count($exts)-1;
     //	$exts = $exts[$n];
     //	if (strlen($exts) > 4) {
     //		$exts = NULL;
     //	} else {
     //		$exts = "." . $exts;
     //	}
     //	$filepath = "assets/" . $folderPath . "/thumb" . $exts;
     //
     //	$img = @file_get_contents($thumbURL);
     //	//echo $img;
     //
     //	//the squarespace hack
     //	$pos = strpos($img, "frameset");
     //	if ($pos === false) {
     //		//nothing
     //		//no frameset instead of img
     //	} else {
     //		//this is the special squarespace situation
     //		$html = str_get_html($img);
     //		foreach($html->find('frame') as $e) {
     //			$thumbURL = $e->src;
     //			$img = @file_get_contents($thumbURL);
     //		}
     //
     //	}
     //
     //	$file = File::find($filepath);
     //	if (!$file) {
     //		$file = new File();
     //		$file->Filename = $filepath;
     //	}
     //
     //	if (strlen($img) > 0) {
     //		file_put_contents(Director::baseFolder() . "/" . $filepath, $img);
     //
     //		try {
     //			$file->write();
     //		}
     //		catch (Exception $e) {
     //			//TODO send error message here
     //			//echo 'Exception caught: ',  $e->getMessage(), "\n";
     //		}
     //
     //		$file->setParentID($folder->ID);
     //		$file->ClassName = "Page_Image";
     //
     //		try {
     //			$file->write();
     //		}
     //		catch (Exception $e) {
     //			//TODO send error message here
     //			//echo 'Exception caught: ',  $e->getMessage(), "\n";
     //		}
     //
     //		$page->ThumbnailImageID = $file->ID;
     //		$page->ThumbnailOnPagePosition = "Floatleft";
     //	}
     //
     //	$page->writeToStage('Stage');
     //	$page->publish('Stage', 'Live');
     //}
     //echo $thumbURL;
 }