diff --git a/LICENCE.txt b/LICENCE.txt index 3dbe37d..d6116ad 100644 --- a/LICENCE.txt +++ b/LICENCE.txt @@ -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 diff --git a/README.md b/README.md index 21c811e..3a609d0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Duplicate Sections # -Version: 1.2 +Version: 1.3 ## Easily duplicate/clone your section parameters and fields ## @@ -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 + *Voila !* -http://www.nitriques.com/open-source/ +### CREDITS ### + +Come say hi! -> diff --git a/extension.driver.php b/extension.driver.php index aa155e3..5f1499b 100644 --- a/extension.driver.php +++ b/extension.driver.php @@ -1,6 +1,7 @@ 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', + '' + ); + + $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 @@ -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 } - } \ No newline at end of file + } diff --git a/extension.meta.xml b/extension.meta.xml index a592c20..a0ea45e 100644 --- a/extension.meta.xml +++ b/extension.meta.xml @@ -11,12 +11,12 @@ - Solutions Nitriques - http://www.nitriques.com + Deux Huit Huit + http://www.deuxhuithuit.com - Deux Huit Huit - http://www.deuxhuithuit.com + Solutions Nitriques + http://www.nitriques.com Pascal Piche @@ -27,6 +27,13 @@ + + - Update for Symphony 4.x + - Replace deprecated method fetch() by select() + + + - Fix some bugs with Duplicate Section + - Update for compatibility with symphony 2.3 @@ -35,4 +42,4 @@ - \ No newline at end of file +