Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.x #5

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENCE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ follows:
----- begin license block -----

Copyright 2011 Solutions Nitriques
Copyright 2012 Deux Huit Huit
Copyright 2012-2013 Deux Huit Huit

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Duplicate Sections #

Version: 1.2
Version: 1.3

## Easily duplicate/clone your section parameters and fields ##

Expand All @@ -16,11 +16,15 @@ For user with symphony CMS version 2.2.x, use tag V1.1

### INSTALLATION ###

- Unzip the duplicate_section.zip file
- (re)Name the folder ***duplicate_section***
- `git clone` / download and unpack the tarball file
- (re)Name the folder **duplicate_section**
- Put into the extension directory
- Enable/install just like any other extension

See <http://getsymphony.com/learn/tasks/view/install-an-extension/>

*Voila !*

http://www.nitriques.com/open-source/
### CREDITS ###

Come say hi! -> <http://www.deuxhuithuit.com/>
195 changes: 134 additions & 61 deletions extension.driver.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/*
Copyight: Solutions Nitriques 2011
Copyight: Deux Huit Huit 2012-2013
Solutions Nitriques 2011
License: MIT, see the LICENCE file
*/

Expand Down Expand Up @@ -30,52 +31,65 @@ public function getSubscribedDelegates(){
)
);
}

/**
*
*
* Fired on each backend page, detect when it's time to append elements into the backend page
* @param array $context
*/
public function appendElementBelowView(Array &$context) {
// only if logged in
// this prevents the clone button from appearing on the login screen
if (Administration::instance()->isLoggedIn()) {

$c = Administration::instance()->getPageCallback();

// when editing a section
if ($c['driver'] == 'blueprintssections' && $c['context'][0] == 'edit') {
if ($c['driver'] == 'blueprintssections' && $c['context']['action'] == 'edit') {

$form = Administration::instance()->Page->Form;
$button_wrap = new XMLELement('div', NULL, array(

$button_wrap = new XMLELement('div', null, array(
'id' => 'duplicate-section'
));

$btn = new XMLElement('button', __('Clone'), array(
'id' => 'duplicate-section-clone',
'class' => 'button',
'name' => 'action[clone]',
'type' => 'submit',
'title' => __('Duplicate this section'),
'style' => 'margin-left: 10px; background: #81B934',
'onclick' => "jQuery('fieldset.settings').empty();return true;"
));

$button_wrap->appendChild($btn);


Widget::registerSVGIcon(
'clone',
'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="28px" height="31px" viewBox="0 0 28 31"><path fill="currentColor" d="M21,29c-0.6,0-1-0.4-1-1V16.5L17.6,16c0,0,0,0,0,0c-3.7,0-6.6-2.5-6.6-6.1V6c0-0.6,0.4-1,1-1s1,0.4,1,1v3.9c0,2.5,2,4.5,4.6,4.5l3.4,0.1c0.5,0,1,0.5,1,1V28C22,28.6,21.6,29,21,29z"/><path fill="currentColor" d="M27,24c-0.6,0-1-0.4-1-1V11.5L23.6,11c0,0,0,0,0,0C19.9,11,17,8.5,17,4.9V1c0-0.6,0.4-1,1-1s1,0.4,1,1v3.9c0,2.5,2,4.5,4.6,4.5L27,9.6c0.5,0,1,0.5,1,1V23C28,23.6,27.6,24,27,24z"/><path fill="currentColor" d="M16,25H6c-0.6,0-1-0.4-1-1s0.4-1,1-1h10c0.6,0,1,0.4,1,1S16.6,25,16,25z"/><path fill="currentColor" d="M16,20H6c-0.6,0-1-0.4-1-1s0.4-1,1-1h10c0.6,0,1,0.4,1,1S16.6,20,16,20z"/><path fill="currentColor" d="M9,15H6c-0.6,0-1-0.4-1-1s0.4-1,1-1h3c0.6,0,1,0.4,1,1S9.6,15,9,15z"/><path fill="currentColor" d="M19,31H3c-1.7,0-3-1.3-3-3V8c0-1.7,1.3-3,3-3h9c1.7,0,10,8.3,10,10v13C22,29.7,20.7,31,19,31z M3,7C2.4,7,2,7.4,2,8v20c0,0.6,0.4,1,1,1h16c0.6,0,1-0.4,1-1V15.1c-0.6-1.3-6.8-7.5-8.1-8.1H3z M20,15.2L20,15.2L20,15.2z M11.8,7L11.8,7L11.8,7z"/><path fill="currentColor" d="M25,26c-0.6,0-1-0.4-1-1s0.4-1,1-1c0.6,0,1-0.4,1-1V10.1c-0.6-1.3-6.8-7.5-8.1-8.1H9C8.4,2,8,2.4,8,3c0,0.6-0.4,1-1,1S6,3.6,6,3c0-1.7,1.3-3,3-3h9c1.7,0,10,8.3,10,10v13C28,24.7,26.7,26,25,26z M26,10.2L26,10.2L26,10.2z M17.8,2L17.8,2L17.8,2z"/></svg>'
);

$btn = new XMLElement(
'button',
__('Clone'),
array(
'id' => 'duplicate-section-clone',
'class' => 'button',
'name' => 'action[clone]',
'type' => 'submit',
'title' => __('Duplicate this section'),
'style' => 'margin-left: 10px; background: #81B934',
'onclick' => "jQuery('fieldset.settings').empty();return true;"
)
);

$button_wrap = Widget::SVGIconContainer(
'clone',
$btn,
array('id' => 'duplicate-section')
);

// add content to the right div
$div_action = self::getChildrenWithClass($form, 'div', 'actions');
if ($div_action != NULL) {
$div_action->appendChild($button_wrap);

if ($div_action != null) {
$div_action->insertChildAt(1, $button_wrap);
}
}
}
}

/**
*
*
* Recursive search for an Element with the right name and css class.
* Stops at fists match
* @param XMLElement $rootElement
Expand All @@ -84,100 +98,159 @@ public function appendElementBelowView(Array &$context) {
*/
private static function getChildrenWithClass($rootElement, $tagName, $className) {
if (! ($rootElement) instanceof XMLElement) {
return NULL; // not and XMLElement
return null; // not and XMLElement
}

// contains the right css class and the right node name
if (strpos($rootElement->getAttribute('class'), $className) > -1 && $rootElement->getName() == $tagName) {
return $rootElement;
}

// recursive search in child elements
foreach ($rootElement->getChildren() as $child) {
$res = self::getChildrenWithClass($child, $tagName, $className);
if ($res != NULL) {

if ($res != null) {
return $res;
}
}
return NULL;
return null;
}


/**
*
* This method search to the field referenced by the $handle param
* and returns its id.
*
* @param array $fields
* @param string $handle
*/
private static function getNewFieldId(Array &$fields, $handle) {
foreach ($fields as &$field) {
var_dump($field->get());
if ($field->get('element_name') == $handle) {
return intval($field->get('id'));
}
}
return null;
}

/**
*
* Delegate AdminPagePreGenerate that handles the click of the 'clone' button and append the button in the form
* @param array $context
*/
public function __action(Array &$context) {

public function __action(Array &$context) {
// append button
self::appendElementBelowView($context);

// if the clone button was hit
if (is_array($_POST['action']) && isset($_POST['action']['clone'])) {
$c = Administration::instance()->getPageCallback();

$section_id = $c['context'][1];

$section = SectionManager::fetch($section_id);


$section_id = $c['context']['id'];

// original section
$section = (new SectionManager)
->select()
->section($section_id)
->execute()
->next();

if ($section != null) {
// get its settings
$section_settings = $section->get();

// remove id
unset($section_settings['id']);

// new name
$section_settings['name'] .= ' ' . time();
$section_settings['handle'] = Lang::createHandle($section_settings['name']);

// save it
$new_section_id = SectionManager::add($section_settings);

// if the create new section was successful
if ( is_numeric($new_section_id) && $new_section_id > 0) {


if (is_numeric($new_section_id) && $new_section_id > 0) {

// get the fields of the section
$fields = $section->fetchFields();

// if we have some
if (is_array($fields)) {

// copy each field
foreach ($fields as &$field) {

// get field settings
$fs = $field->get();

// un set the current id
unset($fs['id']);

// set the new section as the parent
$fs['parent_section'] = $new_section_id;

// create the new field
$f = FieldManager::create($fs['type']);

// set its settings
$f->setArray($fs);

// save
$f->commit();
}
}

// get this section relations
/*$relationships = SectionManager::fetchAssociatedSections($section_id, false);

// fetch the new fields
$new_section = (new SectionManager)
->select()
->section($new_section_id)
->execute()
->next();
$new_fields = $new_section->fetchFields();

if (is_array($relationships)) {
// re-create all of those relations
foreach ($relationships as $relation) {
var_dump($relation);die;

$new_section_field_id = self::getNewFieldId($new_fields, $relation['handle']);

if (is_numeric($new_section_field_id) && $new_section_field_id > 0) {
SectionManager::createSectionAssociation(
$new_section_id, // the new parent (cloned section)
$relation['child_field_id'], // the section on which the cloned section is linked to
$new_section_field_id, // the new parent field (cloned secyion)
$relation['show_association']
);
var_dump($child_field_id);
} else {
throw new Exception(sprintf("Could not find the '%s' field", $relation['handle']));
}
}
}
die;*/

// redirect to the new cloned section
redirect(sprintf(
'%s/blueprints/sections/edit/%s/',
SYMPHONY_URL,
$new_section_id
));

// stop everything now
exit;

} else {
throw new Exception("Could not create a new section");
}
} else {
Throw new Exception("Section not found");
}
}
} // end clonde button
}
}
}
17 changes: 12 additions & 5 deletions extension.meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
</types>
<authors>
<author>
<name github="Solutions-Nitriques" symphony="Nitriques">Solutions Nitriques</name>
<website>http://www.nitriques.com</website>
<name github="DeuxHuitHuit" symphony="" twitter="DeuxHuitHuit">Deux Huit Huit</name>
<website>http://www.deuxhuithuit.com</website>
</author>
<author>
<name github="DeuxHuitHuit" symphony="">Deux Huit Huit</name>
<website>http://www.deuxhuithuit.com</website>
<name github="Solutions-Nitriques" symphony="Nitriques" twitter="Nitriques">Solutions Nitriques</name>
<website>http://www.nitriques.com</website>
</author>
<author>
<name github="PascalPiche" symphony="">Pascal Piche</name>
Expand All @@ -27,6 +27,13 @@
<!-- None -->
</dependencies>
<releases>
<release version="2.0.0" date="TBA" min="4.0.0" max="4.x.x" php-min="5.6.x" php-max="7.x.x">
- Update for Symphony 4.x
- Replace deprecated method fetch() by select()
</release>
<release version="1.3" date="TBA" min="2.3">
- Fix some bugs with Duplicate Section
</release>
<release version="1.2" date="2012-06-12" min="2.3">
- Update for compatibility with symphony 2.3
</release>
Expand All @@ -35,4 +42,4 @@
</release>
<release version="1.0" date="2011-07-11" min="2.2" />
</releases>
</extension>
</extension>