Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
texnixe committed May 30, 2016
1 parent 329f510 commit 8c4c28c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 38 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Version 1.0

- simplify code

Version 0.9

- copy all files to new page (not meta files, though)

Version 0.8

- change response handling
Expand Down
16 changes: 3 additions & 13 deletions field/duplicate/assets/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@
var fieldname = 'duplicate';
var field = $(this);


if(field.data( fieldname )) {
return true;
} else {
field.data( fieldname, true );
}

$('.btn-duplicate').click(function(e) {
$('.message-duplicate').hide().removeClass("success error");
$('.input-duplicate').val('').toggleClass('active');
//field.find('.input-duplicate').;
});

$('.input-duplicate').keypress(function(e) {
Expand All @@ -39,15 +31,13 @@

// Ajax function
$.fn.ajax = function(fieldname) {
var page = $('[data-field="' + fieldname + '"]').find('.btn-duplicate').data('page');
parent = $('[data-field="' + fieldname + '"]').find('.btn-duplicate').data('parent');
newID = $('[data-field="' + fieldname + '"]').find('.input-duplicate').val();
var newID = $('[data-field="' + fieldname + '"]').find('.input-duplicate').val();
newID = newID.replace(/[\/\\\)\($%^&*<>"'`´:;.\?=]/g, " ");
blueprintKey = $('[data-field="' + fieldname + '"]').find('button').data('fieldname');
base_url = window.location.href.replace(/(\/edit.*)/g, '/field') + '/' + blueprintKey + '/' + fieldname + '/ajax/';

console.log(base_url + encodeURIComponent(newID));
$.ajax({
url: base_url + page + '/' + encodeURIComponent(newID) + '/' + parent,
url: base_url + encodeURIComponent(newID),
type: 'GET',
success: function(response) {
var r = JSON.parse(response);
Expand Down
44 changes: 21 additions & 23 deletions field/duplicate/duplicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Clone Field for Kirby CMS (v. 2.3.0)
*
* @author Sonja Broda - info@exniq.de
* @version 0.8
* @version 1.0
*
*/

Expand All @@ -26,9 +26,8 @@ public function input() {
$html = tpl::load( __DIR__ . DS . 'template.php', $data = array(
'field' => $this,
'page' => $this->page(),
'parent' => $this->page()->parent()->uri(),
'placeholder' => $this->i18n($this->placeholder()),
'buttontext' => $this->i18n($this->buttontext()),
'placeholder' => $this->i18n($this->placeholder()),
'buttontext' => $this->i18n($this->buttontext()),
));
return $html;
} else {
Expand All @@ -46,51 +45,46 @@ public function element() {
return $element;
}

public function getFiles($page) {
$files = $page->files();
}


// Routes
public function routes() {
return array(
array(
'pattern' => 'ajax/(:any)/(:any)/(:all)',
'pattern' => 'ajax/(:any)',
'method' => 'GET',
'action' => function($uid, $newID, $parent) {
'action' => function($newID) {

$site = kirby()->site();
$page = $this->page();
$files = $this->page()->files();

if($parent === 'site') {

$p = page($uid);
$parent = $site;

} else {

$p = page($parent . '/' . $uid);
$parent = page($parent);

}
if($site->multiLanguage()->bool()) {

$data = $p->content($site->defaultLanguage()->code())->toArray();
$data = $page->content($site->defaultLanguage()->code())->toArray();
$data['title'] = urldecode($newID);

} else {

$data = $p->content()->toArray();
$data = $page->content()->toArray();
$data['title'] = urldecode($newID);

}

try {

$newPage = $parent->children()->create(str::slug(urldecode($newID)), $p->intendedTemplate(), $data);
$newPage = $page->parent()->children()->create(str::slug(urldecode($newID)), $page->intendedTemplate(), $data);

if($site->multiLanguage()->bool()) {

foreach($site->languages() as $l) {

if($l !== $site->defaultLanguage()) {

$data = $p->content($l->code())->toArray();
$data = $page->content($l->code())->toArray();
$data['title'] = urldecode($newID);
$newPage->update($data, $l->code());

Expand All @@ -99,6 +93,11 @@ public function routes() {
}

}
foreach($files as $file) {

$file->copy(kirby()->roots()->content() . '/' . $newPage->diruri() . "/" . $file->filename());
}

kirby()->trigger('panel.page.create', $newPage);

$response = array(
Expand All @@ -107,7 +106,7 @@ public function routes() {
);

return json_encode($response);
//return 'The new page has been created' . ' <i class="icon fa fa-close"></i>';


} catch(Exception $e) {

Expand All @@ -116,7 +115,6 @@ public function routes() {
'class' => 'error'
);


return json_encode($response);

}
Expand Down
2 changes: 1 addition & 1 deletion field/duplicate/template.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<button type="button" class="btn btn-duplicate" id="<?php echo $field->id(); ?>" data-page="<?php echo $page->uid() ?>" data-parent="<?php e($parent !== "", $parent, 'site') ?>" data-fieldname="<?php echo $field->name(); ?>"><i class="icon fa fa-clone"></i><?php e($buttontext, $buttontext, 'Clone page') ?></button>
<button type="button" class="btn btn-duplicate" id="<?php echo $field->id(); ?>" data-fieldname="<?php echo $field->name(); ?>"><i class="icon fa fa-clone"></i><?php e($buttontext, $buttontext, 'Clone page') ?></button>
<input type="text" class="input input-duplicate" id="<?php echo $field->id(); ?>" name="<?php echo $field->name(); ?>" value="<?php echo $field->value(); ?>" placeholder="<?php e($placeholder, $placeholder, 'Enter page title and press Enter …') ?>">
<div class="message-duplicate"></div>
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ This will create a button in your Panel form.
1. Click on the "Clone Page" button => an input field is shown.
2. Enter the title of the new page into the input field and press `ENTER`.
3. If all is well and the page does not exist yet, the new page is created.
3. If all is well and the page does not exist yet, the new page is created and all files are copied to the new location.

## Limitations

Expand Down

0 comments on commit 8c4c28c

Please sign in to comment.