copy an animal when creating a new one #15

This commit is contained in:
Andreas Gohr
2016-04-20 18:12:31 +02:00
parent 23164e01e3
commit 801ebaa1b6
8 changed files with 111 additions and 45 deletions

View File

@ -38,7 +38,7 @@ class admin_plugin_farmer_new extends DokuWiki_Admin_Plugin {
$data = $this->validateAnimalData();
if(!$data) return;
if($this->createNewAnimal($data['name'], $data['admin'], $data['pass'])) {
if($this->createNewAnimal($data['name'], $data['admin'], $data['pass'], $data['template'])) {
$url = $this->helper->getAnimalURL($data['name']);
$link = '<a href="' . $url . '">' . hsc($data['name']) . '</a>';
@ -61,12 +61,16 @@ class admin_plugin_farmer_new extends DokuWiki_Admin_Plugin {
$form->addTextInput('animalname', $this->getLang('animal'));
$form->addFieldsetClose();
$animals = $this->helper->getAllAnimals();
array_unshift($animals, '');
$form->addFieldsetOpen($this->getLang('animal template'));
$form->addDropdown('animaltemplate', $animals)->addClass('farmer_choosen_animals');
$form->addFieldsetClose();
$form->addFieldsetOpen($this->getLang('animal administrator'));
$btn = $form->addRadioButton('adminsetup', $this->getLang('noUsers'))->val('noUsers');
if($farmconfig['inherit']['users']) {
$btn->attr('checked', 'checked'); // default when inherit available
} else {
$btn->attr('disabled', 'disabled');
}
$form->addRadioButton('adminsetup', $this->getLang('importUsers'))->val('importUsers');
$form->addRadioButton('adminsetup', $this->getLang('currentAdmin'))->val('currentAdmin');
@ -92,6 +96,7 @@ class admin_plugin_farmer_new extends DokuWiki_Admin_Plugin {
$animalname = $INPUT->filter('trim')->str('animalname');
$adminsetup = $INPUT->str('adminsetup');
$adminpass = $INPUT->filter('trim')->str('adminPassword');
$template = $INPUT->filter('trim')->str('animaltemplate');
$errors = array();
@ -116,10 +121,15 @@ class admin_plugin_farmer_new extends DokuWiki_Admin_Plugin {
return false;
}
if(!is_dir(DOKU_FARMDIR . '/' . $template)) {
$template = '';
}
return array(
'name' => $animalname,
'admin' => $adminsetup,
'pass' => $adminpass
'pass' => $adminpass,
'template' => $template
);
}
@ -129,9 +139,10 @@ class admin_plugin_farmer_new extends DokuWiki_Admin_Plugin {
* @param string $name name/title of the animal, will be the directory name for htaccess setup
* @param string $adminSetup newAdmin, currentAdmin or importUsers
* @param string $adminPassword required if $adminSetup is newAdmin
* @param string $template name of animal to copy
* @return bool true if successful
*/
protected function createNewAnimal($name, $adminSetup, $adminPassword) {
protected function createNewAnimal($name, $adminSetup, $adminPassword, $template) {
$animaldir = DOKU_FARMDIR . '/' . $name;
// copy basic template
@ -141,6 +152,24 @@ class admin_plugin_farmer_new extends DokuWiki_Admin_Plugin {
return false;
}
// copy animal template
if($template != '') {
foreach(array('conf', 'data/pages', 'data/media', 'data/meta', 'data/media_meta', 'index') as $dir) {
$templatedir = DOKU_FARMDIR . '/' . $template . '/' . $dir;
if(!is_dir($templatedir)) continue;
// do not copy changelogs in meta
if(substr($dir, -4) == 'meta') {
$exclude = '/\.changes$/';
} else {
$exclude = '';
}
if(!$this->helper->io_copyDir($templatedir, $animaldir . '/' . $dir, $exclude)) {
msg(sprintf($this->getLang('animal template copy error'), $dir), -1);
// we go on anyway
}
}
}
// append title to local config
$ok &= io_saveFile($animaldir . '/conf/local.php', "\n" . '$conf[\'title\'] = \'' . $name . '\';' . "\n", true);
@ -166,11 +195,19 @@ class admin_plugin_farmer_new extends DokuWiki_Admin_Plugin {
} elseif($adminSetup === 'currentAdmin') {
$users = "# <?php exit()?>\n" . $this->getAdminLine() . "\n";
} elseif($adminSetup === 'noUsers') {
$users = "# <?php exit()?>\n";
if(file_exists($animaldir . '/conf/users.auth.php')) {
// a user file exists already, probably from animal template - don't overwrite
$users = '';
} else {
// create empty user file
$users = "# <?php exit()?>\n";
}
} else {
$users = io_readFile(DOKU_CONF . 'users.auth.php');
}
$ok &= io_saveFile($animaldir . '/conf/users.auth.php', $users);
if($users) {
$ok &= io_saveFile($animaldir . '/conf/users.auth.php', $users);
}
/* FIXME handle deactivated plugins
if($this->getConf('deactivated plugins') === '') {

13
all.less Normal file
View File

@ -0,0 +1,13 @@
@import "css/chosen.less";
.chosen-container-single .chosen-single abbr,
.chosen-container-single .chosen-single div b,
.chosen-container-single .chosen-search input[type="text"],
.chosen-container-multi .chosen-choices li.search-choice .search-choice-close,
.chosen-rtl .chosen-search input[type="text"] {
background-image: url(css/chosen-sprite.png);
}
@media only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (min-resolution: 144dpi), only screen and (min-resolution: 1.5dppx) {
background-image: url(css/chosen-sprite.png);
}

BIN
css/chosen-sprite.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 538 B

View File

@ -125,15 +125,19 @@ class helper_plugin_farmer extends DokuWiki_Plugin {
*
* @author Aidan Lister <aidan@php.net>
* @author Michael Große <grosse@cosmocode.de>
* @version 1.0.1
* @author Andreas Gohr <gohr@cosmocode.de>
* @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
*
* @param string $source Source path
* @param string $destination Destination path
*
* @return bool Returns TRUE on success, FALSE on failure
* @param string $source Source path
* @param string $destination Destination path
* @param string $exclude Regular expression to exclude files or directories (complete with delimiters)
* @return bool Returns TRUE on success, FALSE on failure
*/
function io_copyDir($source, $destination) {
function io_copyDir($source, $destination, $exclude = '') {
if($exclude && preg_match($exclude, $source)) {
return true;
}
if(is_link($source)) {
io_lock($destination);
$result = symlink(readlink($source), $destination);
@ -160,7 +164,7 @@ class helper_plugin_farmer extends DokuWiki_Plugin {
}
// recurse into directories
$this->io_copyDir("$source/$entry", "$destination/$entry");
$this->io_copyDir("$source/$entry", "$destination/$entry", $exclude);
}
$dir->close();

View File

@ -68,6 +68,7 @@ $lang['conf_notfound_url'] = 'URL to redirect to if selected above';
$lang['save'] = 'Save';
// new
$lang['animal template'] = 'Copy existing animal';
$lang['animal creation success'] = 'The animal "%s" has been successfully created.';
$lang['animal creation error'] = 'There was an error while creating the animal.';
$lang['animal configuration'] = 'Basic animal configuration';
@ -81,6 +82,7 @@ $lang['animalname_missing'] = 'Please enter a name for the new animal.';
$lang['animalname_invalid'] = 'The animalname may only contain alphanumeric characters and hyphens(but not as first or last character).';
$lang['animalname_preexisting'] = 'An animal with that name already exists.';
$lang['adminPassword_empty'] = 'The password for the new admin account must not be empty.';
$lang['animal template copy error'] = 'There was a problem copying %s from the existing Animal to the new one.';
// plugins
$lang['bulkSingleSwitcher'] = 'Edit a single animal or all at once?';

View File

@ -15,9 +15,17 @@ Examples:
The latter two require the appropriate DNS setup!
==== Copy Animal ====
You can select an existing animal to base the new one on. All configuration, page, media and meta data will be copied
to the new animal. Page and media revisions will not be copied.
The title and logo image will be overwritten to make sure you can distinguish the new from the old wiki.
==== Animal Administrator ====
The Animal will be a fully functional wiki with it's own user base. You will need at least one administrative user
to configure it. You can copy your current user or all users from the farmer or create a completely new user for the Animal.
When inheriting users from the Farmer was enabled in the configuration tab, you can also select to not create any
explicit Animal users.
You can choose to not create any users. This should only be chosen when inheriting users from the Farmer was enabled in the
configuration tab or you copied a different animal with existing users.

View File

@ -5,32 +5,36 @@
* @author Michael Große <grosse@cosmocode.de>
* @author Andreas Gohr <gohr@cosmocode.de>
*/
(function(exports) {
(function (exports) {
'use strict';
jQuery(document).ready(function () {
// general animal select
jQuery('select.farmer_choosen_animals').chosen({
width: '100%',
search_contains: true,
allow_single_deselect: true,
"placeholder_text_single": LANG.plugins.farmer.animalSelect
});
// Plugin Management for all Animals
var bulkPluginSelector = jQuery('#farmer__bulkPluginSelect');
if (jQuery().chosen) {
bulkPluginSelector.chosen({
width: '100%',
search_contains: true,
"placeholder_text_single": LANG.plugins.farmer.pluginSelect
});
}
bulkPluginSelector.chosen({
width: '100%',
search_contains: true,
"placeholder_text_single": LANG.plugins.farmer.pluginSelect
});
bulkPluginSelector.change(function () {
jQuery(".bulkButton").prop('disabled',false);
jQuery(".bulkButton").prop('disabled', false);
});
// Plugin Management of a single Animal
var animalSelector = jQuery('#farmer__animalSelect');
if (jQuery().chosen) {
animalSelector.chosen({
width: '100%',
search_contains: true,
"placeholder_text_single": LANG.plugins.farmer.animalSelect
});
}
animalSelector.chosen({
width: '100%',
search_contains: true,
"placeholder_text_single": LANG.plugins.farmer.animalSelect
});
animalSelector.change(function () {
var animal = animalSelector.val();
jQuery.post(
@ -38,13 +42,13 @@
{
call: 'plugin_farmer_getPlugins_' + animal
},
function(data) {
var submitButton = jQuery('<button type="submit" value="updateSingleAnimal" name="plugin_farmer[submit_type]">'+LANG.plugins.farmer.submit+'</button>');
var resetButton = jQuery('<button type="reset">'+LANG.plugins.farmer.reset+'</button>');
function (data) {
var submitButton = jQuery('<button type="submit" value="updateSingleAnimal" name="plugin_farmer[submit_type]">' + LANG.plugins.farmer.submit + '</button>');
var resetButton = jQuery('<button type="reset">' + LANG.plugins.farmer.reset + '</button>');
var pluginContainer = jQuery('#farmer__animalPlugins');
pluginContainer.html('');
pluginContainer.append(submitButton, resetButton);
jQuery.each(data[0], function(index, value) {
jQuery.each(data[0], function (index, value) {
var checked = 'checked';
if (typeof data[1][value] !== 'undefined' && data[1][value] === 0) {
checked = '';
@ -52,7 +56,7 @@
var pluginLabel = jQuery('<label></label>')
.append(jQuery('<span></span>').text(value))
.append(jQuery('<input class="edit" type="checkbox" name="plugin_farmer_plugins[' + value + ']" ' + checked + '>'))
;
;
pluginContainer.append(pluginLabel);
});
pluginContainer.append(submitButton.clone(), resetButton.clone());
@ -64,11 +68,11 @@
});
// make sure there's enough space for the dropdown
jQuery('select').on('chosen:showing_dropdown', function(evt, params) {
jQuery('select').on('chosen:showing_dropdown', function (evt, params) {
jQuery(evt.target).parent('fieldset').animate({
"padding-bottom": '20em'
}, 400);
}).on('chosen:hiding_dropdown', function(evt, params) {
}).on('chosen:hiding_dropdown', function (evt, params) {
jQuery(evt.target).parent('fieldset').animate({
"padding-bottom": '7px'
}, 400);
@ -77,14 +81,14 @@
jQuery("input[name=bulkSingleSwitch]:radio").change(function () {
if (jQuery('#farmer__bulk').prop("checked")) {
jQuery('#farmer__bulkForm').css('display','initial');
jQuery('#farmer__bulkForm').css('display', 'initial');
} else {
jQuery('#farmer__bulkForm').css('display','none');
jQuery('#farmer__bulkForm').css('display', 'none');
}
if (jQuery('#farmer__single').prop("checked")) {
jQuery('#farmer__singlePluginForm').css('display','initial');
jQuery('#farmer__singlePluginForm').css('display', 'initial');
} else {
jQuery('#farmer__singlePluginForm').css('display','none');
jQuery('#farmer__singlePluginForm').css('display', 'none');
}
});
jQuery('#farmer__bulk').click();

View File

@ -1,5 +1,3 @@
@import "css/chosen.less";
#plugin__farmer_admin {
.panelHeader {