Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
18 posts Page 1 of 2
Hi,

A long standing demand has been for a way to create new pages by copying data from existing pages (useful if the data does not change too much between pages or, perhaps, to create sample pages to fill a new site).

Attached is an addon that should help with this.

Installation:
Installation follows the regular method -
1. Extract the folder named 'copy-to-new' from the attached zip and place it in 'couch/addons' folder.
2. Activate the addon by adding the following entry in 'couch/addons/kfunctions.php' file (if this file is not present, rename 'kfunctions.example.php' to 'kfunctions.php') -
Code: Select all
require_once( K_COUCH_DIR.'addons/copy-to-new/copy-to-new.php' );

Usage:
Within the 'copy-to-new' folder we added to addons above, you'll find a 'config.example.php' file.
Rename it to 'config.php' and open it in your text editor. Add to the following setting the name(s) of the templates you'd like to add this copy to new page feature -
Code: Select all
$cfg['tpls'] = '';

For example, if you wanted to add it to 'portfolio.php', the setting will become -
Code: Select all
$cfg['tpls'] = 'portfolio.php';

Multiple templates may be added by separating them with a pipe (i.e. '|') character e.g.
Code: Select all
$cfg['tpls'] = 'portfolio.php | contacts.php ';

And now when you wish to create a new page copying data from an existing page (of the templates specified in config above), open that page for editing in the admin-panel and you should see a new button on the top of the screen -
Untitled-1.png
Untitled-1.png (2.82 KiB) Viewed 41905 times

Clicking it will open the familiar 'Add new' screen but this time the form would be pre-populated with data taken from the page we clicked the button on.

You may edit the page further to make changes if required and press save to create your new page.

Hope this helps.

Attachments

Adding to my last post (to avoid cluttering the main topic) -

Certain fields might need some special care for copying them correctly. As of this post, I could only find one such field (detailed below) but in case there are others in the future, the addon makes provision for them by throwing an event which could be tapped in to make corrections.

The field I alluded to above is the type 'reverse_relation' region we use for creating 'gallery per page' -
viewtopic.php?f=8&t=8559#p16293

Suppose you have a page containing that field and it shows 12 related images. Upon copying it, the field in the new page will show 0 related images.

This happens because the field actually does no have any data of its own to get copied - it only enumerates the images (in another template) which are related to the current page. The images, in our example, are related to the original page. When the copy creates a new page, the field enumerates images related to this new page and (correctly) shows zero.

Anyway, technicalities apart, the desired outcome would be for the new page to create new relations with the same images to give a seamless experience.

To do that, we need first to modify the definition of the type 'relation' region in the images template (gallery.php in the linked sample post) to make it support multiple relations (the original supports being related to only one page).
Modify the original code from this -
Code: Select all
<!-- in gallery.php -->

<cms:editable
    type='relation'
    name='photo_product'
    masterpage='products.php'
    has='one'
    no_gui='1'
    label='-'
/>

to make it as follows -
Code: Select all
<!-- in gallery.php -->

<cms:editable
    type='relation'
    name='photo_product'
    masterpage='products.php'
    has='many'
    no_gui='1'
    label='-'
/>

And now add the following code in your kfunctions.php file -
Code: Select all
// handle type 'reverse_relation' upon copying page to a new page ..
$FUNCS->add_event_listener( 'copy_to_new_complete', 'my_handle_reverse_related' );
function my_handle_reverse_related( &$pg, $orig_page_id ){
    global $FUNCS, $DB;

    for( $x=0; $x<count($pg->fields); $x++ ){
        $f = &$pg->fields[$x];
        if( (!$f->system) && $f->k_type=='reverse_relation'){
            $fid = $f->id;
            break;
        }
        unset( $f );
    }

    if( $f ){
        // get template_id of reverse related masterpage
        $rs = $DB->select( K_TBL_TEMPLATES, array('id', 'name'), "name='" . $DB->sanitize( $f->masterpage ). "'" );
        if( count($rs) ){
            $template_id = $rs[0]['id'];
            $template_name = $rs[0]['name'];
        }
        else{
            return;
        }

        // get relation_field_id using template_id
        if( $f->field ){
            $rs = $DB->select( K_TBL_FIELDS, array('*'), "template_id='" . $DB->sanitize( $template_id ). "' AND k_type='relation' AND name='" . $DB->sanitize( $f->field ) . "'" );
        }
        else{ // if field not specified, get the first 'relation' field defined
            $rs = $DB->select( K_TBL_FIELDS, array('*'), "template_id='" . $DB->sanitize( $template_id ). "' AND k_type='relation' LIMIT 1" );
        }
        if( count($rs) ){
            $field_id = $rs[0]['id'];
        }
        else{
            return;
        }

        // find all related pages
        $cid = $orig_page_id; // original page
        if( $cid != -1 ){ // not a new page
            $rel_tables = K_TBL_PAGES . ' p inner join ' . K_TBL_RELATIONS . ' rel on rel.pid = p.id' . "\r\n";
            $rel_sql = "p.parent_id=0 AND rel.cid='" . $DB->sanitize( $cid ). "' AND rel.fid='" . $DB->sanitize( $field_id ). "'";
            $rs = $DB->select( $rel_tables, array('p.id'), $rel_sql );

            // relate those pages to the newly created page
            if( count($rs) ){
                foreach( $rs as $row ){
                    $weight = 0; //TODO
                    $rs2 = $DB->insert( K_TBL_RELATIONS, array(
                        'pid'=>$row['id'],
                        'fid'=>$field_id,
                        'cid'=>$pg->id,
                        'weight'=>$weight
                        )
                    );
                    if( $rs2!=1 ) die( "ERROR: Failed to insert record in K_TBL_RELATIONS" );
                }
            }
        }
    }
}

Now any time a new page is created by copying an existing page, the code above goes ahead and duplicates the relations with images too.

Hope this helps.
cool !!!! great !!
thank you @KK for this add-on - glad I happened to come across it browsing the forum! It's something that will be VERY useful for many of the sites that I develop. E.G. events listings which have similar events - can now be cloned and amended - this will makes Couch even more user-friendly and will speed things up for clients. A brilliant extension ...
Works perfectly and is very useful!
Thanks KK :)
This is great!

Would be fantastic to have part of <cms:field 'k_actions' /> in the list view as another toggle button instead of having to edit then press copy.
Does the addon work with the latest couchcms version? I added
Code: Select all
require_once(K_COUCH_DIR.'addons/copy-to-new/copy-to-new.php');
to the kfunctions.php, saved config.php
Code: Select all
<?php
    if ( !defined('K_COUCH_DIR') ) die(); // cannot be loaded directly 
    $cfg['tpls'] = 'test.php';
    $cfg['btn_text'] = 'Copy to New';
    $cfg['btn_desc'] = 'Copies the current page into a new page';
and created a test page -> test.php. The button does not appear.

Second question, why is the edit-copy-to-new.php in the zip-file?

Greetings and happy new year!
Happy New Year to you too, Simon :)

Replying to your queries -

1. Does the addon work with the latest couchcms version?
Yes, it most certainly does.
Please double-check your setup for any oversights.

2. why is the edit-copy-to-new.php in the zip-file?
It is used by the routine that is invoked when one clicks on the "Copy to new" button.
KK wrote: Please double-check your setup for any oversights.


Ahhh, I missunderstood, that the addon only works with cloned pages.
Hi!
Does the addon work with a very old couchcms version?
I have checked the setup more times but still not working :cry:
18 posts Page 1 of 2