diff --git a/README b/README index c6b3937..ff7edbf 100644 --- a/README +++ b/README @@ -9,7 +9,7 @@ Sam Minnée (Nickname: sminnee) Requirements ----------------------------------------------- -- genericdataadmin Module +- SilverStripe 2.3.0 or higher Documentation ----------------------------------------------- diff --git a/_config.php b/_config.php index a165e7a..e59da04 100644 --- a/_config.php +++ b/_config.php @@ -17,17 +17,4 @@ Object::add_extension('Member', 'MemberSubsites'); Object::add_extension('File', 'FileSubsites'); Object::add_extension('ErrorPage', 'ErrorPageSubsite'); -// Backwards compatibility with SilverStripe 2.2 -if(!class_exists('CMSMenu')) { - Director::addRules(100, array( - 'admin/subsites/$Action/$ID/$OtherID' => 'SubsiteAdmin', - )); - Object::addStaticVars( 'LeftAndMain', array( 'extra_menu_items' => array( - 'Sub-sites' => array("intranets", "admin/subsites/", 'SubsiteAdmin') - ))); -} - -if(!class_exists('GenericDataAdmin')) { - user_error('Please install the module "genericdataadmin" to use subsites', E_USER_ERROR); -} ?> \ No newline at end of file diff --git a/code/Subsite.php b/code/Subsite.php index 476f77a..27eb964 100644 --- a/code/Subsite.php +++ b/code/Subsite.php @@ -25,14 +25,36 @@ class Subsite extends DataObject implements PermissionProvider { // If unset, will default to 'IsPublic' => 'Boolean' ); - + static $has_one = array( ); + + static $has_many = array( + 'Domains' => 'SubsiteDomain', + ); static $defaults = array( 'IsPublic' => 1, ); + static $searchable_fields = array( + 'Title' => array( + 'title' => 'Subsite Name' + ), + 'Domains.Domain' => array( + 'title' => 'Domain name' + ), + 'IsPublic' => array( + 'title' => 'Active subsite', + ), + ); + + static $summary_fields = array( + 'Title' => 'Subsite Name', + 'PrimaryDomain' => 'Primary Domain', + 'IsPublic' => 'Active subsite', + ); + /** * @var Subsite $cached_subsite Internal cache used by {@link currentSubsite()}. */ @@ -94,6 +116,10 @@ class Subsite extends DataObject implements PermissionProvider { } } } + + function getPrimaryDomain() { + return $this->domain(); + } function absoluteBaseURL() { return "http://" . $this->domain() . Director::baseURL(); @@ -106,11 +132,7 @@ class Subsite extends DataObject implements PermissionProvider { $domainTable = new TableField("Domains", "SubsiteDomain", array("Domain" => "Domain (use * as a wildcard)", "IsPrimary" => "Primary domain?"), array("Domain" => "TextField", "IsPrimary" => "CheckboxField"), - null, "SubsiteDomain.SubsiteID", $this->ID); - - $domainTable->setExtraData(array( - 'SubsiteID' => $this->ID ? $this->ID : '$RecordID', - )); + "SubsiteID", $this->ID); $fields = new FieldSet( new TabSet('Root', @@ -473,16 +495,18 @@ class Subsite_Template extends Subsite { /** * Create an instance of this template, with the given title & domain */ - function createInstance($title, $domain) { + function createInstance($title, $domain = null) { $intranet = Object::create('Subsite'); $intranet->Title = $title; $intranet->TemplateID = $this->ID; $intranet->write(); - $intranetDomain = Object::create('SubsiteDomain'); - $intranetDomain->SubsiteID = $intranet->ID; - $intranetDomain->Domain = $domain; - $intranetDomain->write(); + if($domain) { + $intranetDomain = Object::create('SubsiteDomain'); + $intranetDomain->SubsiteID = $intranet->ID; + $intranetDomain->Domain = $domain; + $intranetDomain->write(); + } $oldSubsiteID = Session::get('SubsiteID'); self::changeSubsite($this->ID); diff --git a/code/SubsiteAdmin.php b/code/SubsiteAdmin.php index 7bd358e..fbd1786 100644 --- a/code/SubsiteAdmin.php +++ b/code/SubsiteAdmin.php @@ -4,159 +4,75 @@ * * @package subsites */ -class SubsiteAdmin extends GenericDataAdmin { - - static $tree_class = "Subsite"; - static $subitem_class = "Subsite"; - static $data_type = 'Subsite'; +class SubsiteAdmin extends ModelAdmin { + static $managed_models = array('Subsite'); static $url_segment = 'subsites'; - static $url_rule = '/$Action/$ID/$OtherID'; - - static $menu_title = 'Subsites'; - - function performSearch() { - - } - - function getSearchFields() { - return singleton('Subsite')->adminSearchFields(); - } - - function getLink() { - return 'admin/subsites/'; - } + static $collection_controller_class = "SubsiteAdmin_CollectionController"; function Link() { - return $this->getLink(); + return 'admin/subsites/'; } - - function Results($data = null) { - if(!$data) $data = $this->requestParams; - - if(defined('DB::USE_ANSI_SQL')) - $q="\""; - else $q='`'; - - $where = ''; - if(isset($data['Name']) && $data['Name']) { - $SQL_name = Convert::raw2sql($data['Name']); - $where = "{$q}Title{$q} LIKE '%$SQL_name%'"; - } else { - $where = "{$q}Title{$q} != ''"; - } - - $intranets = null; - $intranets = DataObject::get('Subsite_Template', $where, "{$q}Title{$q}"); - $subsites = DataObject::get('Subsite', $where, "{$q}Title{$q}"); - - if($intranets) { - $intranets->merge($subsites); - } else { - $intranets = $subsites; - } - - if(!$intranets) return null; - - $intranets->removeDuplicates(); - - $html = ""; - - $numIntranets = 0; - foreach($intranets as $intranet) { - $numIntranets++; - $evenOdd = ($numIntranets % 2) ? 'odd':'even'; - $prefix = ($intranet instanceof Subsite_Template) ? " * " : ""; - $html .= ""; - } - $html .= "
NameDomain
ID}\">$prefix{$intranet->Title}ID}\">{$intranet->domain()}
"; - return $html; - } - - /** - * Returns the form for adding subsites. - * @returns Form A nerw form object - */ - function AddSubsiteForm() { - $templates = $this->getIntranetTemplates(); - +} + +class SubsiteAdmin_CollectionController extends ModelAdmin_CollectionController { + function AddForm() { + $form = parent::AddForm(); + + $templates = DataObject::get('Subsite_Template', '', 'Title'); $templateArray = array('' => "(No template)"); if($templates) { $templateArray = $templateArray + $templates->map('ID', 'Title'); } - - return new Form($this, 'AddSubsiteForm', new FieldSet( - new TextField('Name', 'Name:'), - new TextField('Domain', 'Domain name:'), + + $form->Fields()->addFieldsToTab('Root.Configuration', array( new DropdownField('Type', 'Type', array( 'subsite' => 'New site', 'template' => 'New template', )), - new DropdownField('TemplateID', 'Copy structure from:', $templateArray)//, - /*new TextField('AdminName', 'Admin name:'), - new EmailField('AdminEmail', 'Admin email:')*/ - ), - new FieldSet( - new FormAction('addintranet', 'Add') + new DropdownField('TemplateID', 'Copy structure from:', $templateArray) )); + + return $form; } - public function getIntranetTemplates() { - if(defined('DB::USE_ANSI_SQL')) - $q="\""; - else $q='`'; - - return DataObject::get('Subsite_Template', '', "{$q}Title{$q}"); - } - - function addintranet($data, $form) { - if($data['Name'] && ($data['Domain'] || $data['Type'] == 'template')) { - if(isset($data['TemplateID']) && $data['TemplateID']) { - $template = DataObject::get_by_id('Subsite_Template', $data['TemplateID']); - } else { - $template = null; - } - - // Create intranet from existing template - switch($data['Type']) { - case 'template': - if($template) $intranet = $template->duplicate(); - else $intranet = new Subsite_Template(); - - $intranet->Title = $data['Name']; - $intranet->write(); - break; - - case 'subsite': - default: - if($template) $intranet = $template->createInstance($data['Name'], $data['Domain']); - else { - $intranet = new Subsite(); - $intranet->Title = $data['Name']; - $intranet->write(); - - $newSubsiteDomain = new SubsiteDomain(); - $newSubsiteDomain->SubsiteID = $intranet->ID; - $newSubsiteDomain->write(); - $newSubsiteDomain->Domain = $data['Domain']; - $newSubsiteDomain->write(); - } - break; - } - - Director::redirect('admin/subsites/show/' . $intranet->ID); + function doCreate($data, $form, $request) { + if(isset($data['TemplateID']) && $data['TemplateID']) { + $template = DataObject::get_by_id('Subsite_Template', $data['TemplateID']); } else { - if($data['Type'] == 'template') echo "You must provide a name for your new template."; - else echo "You must provide a name and domain for your new site."; + $template = null; } - } - /** - * Use this as an action handler for custom CMS buttons. - */ - function callPageMethod2($data, $form) { - return $this->callPageMethod($data, $form); + // Create subsite from existing template + switch($data['Type']) { + case 'template': + if($template) $subsite = $template->duplicate(); + else { + $subsite = new Subsite_Template(); + $subsite->write(); + } + break; + + case 'subsite': + default: + if($template) $subsite = $template->createInstance($data['Title']); + else { + $subsite = new Subsite(); + $subsite->Title = $data['Title']; + $subsite->write(); + } + break; + } + + $form->dataFieldByName('Domains')->setExtraData(array( + "SubsiteID" => $subsite->ID, + )); + $form->saveInto($subsite); + $subsite->write(); + + Director::redirect(Controller::join_links($this->Link(), $subsite->ID , 'edit')); } } + ?> diff --git a/templates/Includes/SubsiteAdmin_left.ss b/templates/Includes/SubsiteAdmin_left.ss deleted file mode 100644 index 41a025c..0000000 --- a/templates/Includes/SubsiteAdmin_left.ss +++ /dev/null @@ -1,15 +0,0 @@ -
-

Create Sub-site

-
-$AddSubsiteForm -
-

Search for Sub-sites

-
-
- $SearchForm -
-
- $Results -
-
-
\ No newline at end of file diff --git a/templates/Includes/SubsiteAdmin_right.ss b/templates/Includes/SubsiteAdmin_right.ss deleted file mode 100644 index ba7cbef..0000000 --- a/templates/Includes/SubsiteAdmin_right.ss +++ /dev/null @@ -1,13 +0,0 @@ - -<% if EditForm %> $EditForm <% else %> -
-

$ApplicationName

- -

Welcome to $ApplicationName! Please choose click on one of the -entries on the left pane.

- -
-<% end_if %> - -