Added Subsite::duplicate()

Added duplication 'create copy' button to subsites and templates
Templates shown with a * in the search results list
Ability to create new templates as well as new subsites
This commit is contained in:
Sam Minnee 2007-08-28 22:29:44 +00:00
parent fa54508d5e
commit 538cfeda6a
3 changed files with 75 additions and 23 deletions

View File

@ -81,8 +81,10 @@ class SiteTreeSubsites extends DataObjectDecorator {
/**
* Create a duplicate of this page and save it to another subsite
* @param $subsiteID int|Subsite The Subsite to copy to, or its ID
* @param $isTemplate boolean If this is true, then the current page will be treated as the template, and MasterPageID will be set
*/
public function duplicateToSubsite($subsiteID = null, $stage = 'Live') {
public function duplicateToSubsite($subsiteID = null, $isTemplate = true) {
if(is_object($subsiteID)) {
$subsite = $subsiteID;
$subsiteID = $subsite->ID;
@ -95,7 +97,8 @@ class SiteTreeSubsites extends DataObjectDecorator {
$page->CheckedPublicationDifferences = $page->AddedToStage = true;
$subsiteID = ($subsiteID ? $subsiteID : Subsite::currentSubsiteID());
$page->SubsiteID = $subsiteID;
$page->MasterPageID = $this->owner->ID;
if($isTemplate) $page->MasterPageID = $this->owner->ID;
foreach(self::$template_fields as $field) {
foreach(self::$template_variables as $variable => $intranetField) {

View File

@ -92,9 +92,19 @@ class Subsite extends DataObject implements PermissionProvider {
function getCMSActions() {
return new FieldSet(
new FormAction('callPageMethod', "Create copy", null, 'adminDuplicate')
);
}
function adminDuplicate() {
$newItem = $this->duplicate();
$JS_title = Convert::raw2js($this->Title);
return <<<JS
statusMessage('Created a copy of $JS_title', 'good');
$('Form_EditForm').loadURLFromServer('admin/subsites/show/$newItem->ID');
JS;
}
static function currentSubsite() {
if(!self::$cached_subsite) self::$cached_subsite = DataObject::get_by_id('Subsite', self::currentSubsiteID());
return self::$cached_subsite;
@ -225,7 +235,43 @@ SQL;
function createInitialRecords() {
}
/**
* Duplicate this subsite
*/
function duplicate() {
$newTemplate = parent::duplicate();
$oldSubsiteID = Session::get('SubsiteID');
self::changeSubsite($this->ID);
/*
* Copy data from this template to the given subsite. Does this using an iterative depth-first search.
* This will make sure that the new parents on the new subsite are correct, and there are no funny
* issues with having to check whether or not the new parents have been added to the site tree
* when a page, etc, is duplicated
*/
$stack = array(array(0,0));
while(count($stack) > 0) {
list($sourceParentID, $destParentID) = array_pop($stack);
$children = Versioned::get_by_stage('Page', 'Live', "`ParentID`=$sourceParentID", '');
if($children) {
foreach($children as $child) {
$childClone = $child->duplicateToSubsite($newTemplate, false);
$childClone->ParentID = $destParentID;
$childClone->writeToStage('Stage');
$childClone->publish('Stage', 'Live');
array_push($stack, array($child->ID, $childClone->ID));
}
}
}
self::changeSubsite($oldSubsiteID);
return $newTemplate;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CMS ADMINISTRATION HELPERS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -250,20 +296,6 @@ SQL;
* An instance of subsite that can be duplicated to provide a quick way to create new subsites.
*/
class Subsite_Template extends Subsite {
function duplicate($args = null) {
if(!$args)
$args = array();
// create the subsite
$subsite = Subsite::create('');
// Apply arguments to field values
$subsite->write();
}
/**
* Create an instance of this template, with the given title & subdomain
*/
@ -293,7 +325,7 @@ class Subsite_Template extends Subsite {
if($children) {
foreach($children as $child) {
$childClone = $child->duplicateToSubsite($intranet, 'Stage');
$childClone = $child->duplicateToSubsite($intranet);
$childClone->ParentID = $destParentID;
$childClone->writeToStage('Stage');
$childClone->publish('Stage', 'Live');

View File

@ -30,7 +30,7 @@ class SubsiteAdmin extends GenericDataAdmin {
$where = "`Title` LIKE '%$SQL_name%'";
}
$intranets = DataObject::get('Subsite', $where);
$intranets = DataObject::get('Subsite', $where, "if(ClassName = 'Subsite_Template',0,1), Title");
if(!$intranets)
return null;
@ -40,7 +40,8 @@ class SubsiteAdmin extends GenericDataAdmin {
foreach($intranets as $intranet) {
$numIntranets++;
$evenOdd = ($numIntranets % 2) ? 'odd':'even';
$html .= "<tr class=\"$evenOdd\"><td><a class=\"show\" href=\"admin/subsites/show/{$intranet->ID}\">{$intranet->Title}</a></td><td>{$intranet->Subdomain}.{$intranet->Domain}</td></tr>";
$prefix = ($intranet instanceof Subsite_Template) ? " * " : "";
$html .= "<tr class=\"$evenOdd\"><td><a class=\"show\" href=\"admin/subsites/show/{$intranet->ID}\">$prefix{$intranet->Title}</a></td><td><a class=\"show\" href=\"admin/subsites/show/{$intranet->ID}\">{$intranet->Subdomain}.{$intranet->Domain}</a></td></tr>";
}
$html .= "</tbody></table>";
return $html;
@ -60,6 +61,10 @@ class SubsiteAdmin extends GenericDataAdmin {
return new Form($this, 'AddSubsiteForm', new FieldSet(
new TextField('Name', 'Name:'),
new TextField('Subdomain', 'Subdomain:'),
new DropdownField('Type', 'Type', array(
'subsite' => 'New site',
'template' => 'New template',
)),
new DropdownField('TemplateID', 'Use template:', $templateArray),
new TextField('AdminName', 'Admin name:'),
new EmailField('AdminEmail', 'Admin email:')
@ -85,12 +90,24 @@ class SubsiteAdmin extends GenericDataAdmin {
$member->Email = $data['AdminEmail'];
$member->write();
}
$template = DataObject::get_by_id('Subsite_Template', $data['TemplateID']);
// Create intranet from existing template
// TODO Change template based on the domain selected.
$template = DataObject::get_by_id('Subsite_Template', $data['TemplateID']);
$intranet = $template->createInstance($data['Name'], $data['Subdomain']);
switch($data['Type']) {
case 'template':
$intranet = $template->duplicate();
$intranet->Title = $data['Name'];
$intranet->write();
break;
default:
case 'subsite':
$intranet = $template->createInstance($data['Name'], $data['Subdomain']);
break;
}
// NOTE: This stuff is pretty oriwave2-specific...
$groupObjects = array();
// create Staff, Management and Administrator groups