28/05/2012 5:38pm

PHP | Problem -



function onBeforeWrite(){

parent::onBeforeWrite();

// Preliminary check for whether page is ready to be written
if(!$this->isChanged('Version') && !self::$has_written && $this->seed){

// Grab Top Level Pages $tlp and Seeded Pages $seeded
$tlp_pages = DataObject::get('RedirectorPage', "RedirectorPage.tlp = 1");
$seeded_pages = DataObject::get('SeededPage', "SeededPage.seedID = ".$this->ID );

// Count Pages Created and Needed
if ($seeded_pages) $seed_count = $seeded_pages->Count();
$totalTLP = $tlp_pages->Count();

// Convert DOS to Array for easier parsing
$tlp_pages = $tlp_pages->toArray();


// Log to FirePHP Whats created and how many Top Level pages
if ($seeded_pages) fb::log ("Already Seeded: ".$seed_count." pages");
fb::log("Total Top Level Pages ".$totalTLP." pages");


// Loop through all Top Level Pages in Site
foreach ( $tlp_pages as $page ){

// Check if a seeded page exists
$seeded_child = DataObject::get_one("SeededPage", "seedID = $this->ID AND ParentID = $page->ID");

// If so update Title & Content to match updated parent page & save to Draft Status for User to edit.
if ($seeded_child) {
$seeded_child->Title = $this->Title."-".$page->URLSegment;
$seeded_child->seedContent = $this->Content;
$seeded_child->MetaTitle = $this->Title."-".$page->URLSegment;
$seeded_child->MetaKeyword = $this->MetaKeyword;
$seeded_child->MetaDescription = $this->MetaDescription;
$seeded_child->write();
$seeded_child->publish("Stage", "Stage");

fb::log("Got One!");

// If not create a new page based on the original page type and pre-populate data
} else {
$parentPage = $page;
$pageType = "Seeded".$this->ClassName;
$ssPage = new $pageType();
$ssPage->setParent($parentPage);
$ssPage->Title = $this->Title."-".$page->URLSegment;
$ssPage->seedID = $this->ID;
$ssPage->generateURLSegment($ssPage->Title);
$ssPage->MetaTitle = $this->Title."-".$page->URLSegment;
$ssPage->MetaKeyword = $this->MetaKeyword;
$ssPage->MetaDescription = $this->MetaDescription;
$ssPage->seedContent = $this->Content;
$ssPage->writeToStage('Stage');
$ssPage->publish("Stage", "Stage");

// Add Class of DataObjects to Duplicate when seeding page for first time
$items_to_duplicate = array(
'ProductDocument'
);

//duplicate has many items
foreach ($this->has_many() as $key => $className) {
if (in_array($key, $items_to_duplicate)) {
foreach ($this->{$key}() as $item) {
$newField = $item->duplicate();
$id = get_class($this) . 'ID';
$newField->{$id} = $ssPage->ID;
$newField->write();
}
}
}
}
}

self::$has_written = true;
fb::log("Pages Seeded");
}


1 Comments 1 Solutions

28/05/2012 7:05pm

PHP | Solution - Anonymous

Fully functional example if anyone is interested

	function onBeforeWrite(){ 

parent::onBeforeWrite();

// Preliminary check for whether page is ready to be written
if(!$this->isChanged('Version') && !self::$has_written && $this->seed){

// Grab Top Level Pages $tlp and Seeded Pages $seeded
$tlp_pages = DataObject::get('RedirectorPage', "RedirectorPage.tlp = 1");
$seeded_pages = DataObject::get('SeededPage', "SeededPage.seedID = ".$this->ID );

// Count Pages Created and Needed
if ($seeded_pages) $seed_count = $seeded_pages->Count();
$totalTLP = $tlp_pages->Count();

// Convert DOS to Array for easier parsing
$tlp_pages = $tlp_pages->toArray();


// Log to FirePHP Whats created and how many Top Level pages
if ($seeded_pages) fb::log ($seed_count, "Already seeded pages: ");
fb::log($totalTLP, "Total Top Level Pages: ");


// Loop through all Top Level Pages in Site
foreach ( $tlp_pages as $page ){

// Check if a seeded page exists
$seeded_child = DataObject::get_one("SeededPage", "SeededPage.seedID = $this->ID AND ParentID = $page->ID");

// If so update Title & Content to match updated parent page & save to Draft Status for User to edit.
if ($seeded_child) {
$seeded_child->Title = $this->Title."-".$page->URLSegment;
$seeded_child->seedContent = $this->Content;
$seeded_child->MetaTitle = $this->Title."-".$page->URLSegment;
$seeded_child->MetaKeyword = $this->MetaKeyword;
$seeded_child->MetaDescription = $this->MetaDescription;
$seeded_child->write();
$seeded_child->publish("Stage", "Stage");

fb::log("Got One!");

// If not create a new page based on the original page type and pre-populate data
} else {
$parentPage = $page;
$pageType = "Seeded".$this->ClassName;
$ssPage = new $pageType();
$ssPage->setParent($parentPage);
$ssPage->Title = $this->Title."-".$page->URLSegment;
$ssPage->seedID = $this->ID;
$ssPage->generateURLSegment($ssPage->Title);
$ssPage->MetaTitle = $this->Title."-".$page->URLSegment;
$ssPage->MetaKeyword = $this->MetaKeyword;
$ssPage->MetaDescription = $this->MetaDescription;
$ssPage->seedContent = $this->Content;
$ssPage->writeToStage('Stage');
$ssPage->publish("Stage", "Stage");

// Add Class of DataObjects to Duplicate when seeding page for first time
$items_to_duplicate = array(
'ProductDocument'
);

//duplicate has many items
foreach ($this->has_many() as $key => $className) {
if (in_array($key, $items_to_duplicate)) {
foreach ($this->{$key}() as $item) {
fb::log($item, "Item:");
$newField = $item->duplicate();
$newField->resourceID = $ssPage->ID;
fb::log($ssPage->ID, "Page ID:");
$newField->write();
fb::log($newField->ResourceID, "New DataObject ID:");
}
}
}
}
}

self::$has_written = true;
fb::log("Finished Seeding");
}

Post Comment