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

We create a new clonable template and visit it as super-admin very often.
This trick is to enjoy some automation with autocreated default pages, to make a nice-looking name and auto-un-publish like this:
BEFORE:
download (2).jpg
BEFORE
download (2).jpg (9.26 KiB) Viewed 867 times

AFTER:
download (1).jpg
AFTER
download (1).jpg (5.96 KiB) Viewed 867 times


Implementation:
Find in /couch/page.php, line 650:
Code: Select all
$title = 'Default page for '.$DB->sanitize( $this->tpl_name ).' * PLEASE CHANGE THIS TITLE *'; // . $this->tpl_name;
$name = $FUNCS->get_clean_url( $title );

change to:
Code: Select all
$title = 'default page'; // paste here any new title you like
$name = 'Default page for '.$DB->sanitize( $this->tpl_name ).' * PLEASE CHANGE THIS TITLE *'; // name will be left untouched, I don't care about it.
$name = $FUNCS->get_clean_url( $name );

and to unpublish, find in the same page.php Line 1289
Code: Select all
'publish_date'=>( $this->tpl_gallery && $is_master ) ? '0000-00-00 00:00:00' : $cur_time,
'is_master'=>$is_master

change to
Code: Select all
'publish_date'=>( $this->tpl_gallery && $is_master ) ? '0000-00-00 00:00:00' : $cur_time,
'publish_date'=>( $is_master ) ? '0000-00-00 00:00:00' : $cur_time,
'is_master'=>$is_master


I love it!
After a while using it: This tip has a drawback as it puts newly-registered non-clonable templates to unpublished state too, which needs to be amended through Advanced Settings of the template.

A solution is coming on the way.
Ultimate correct solution for Couch 2.0+
It is very useful if you want to automatically hide and unpublish default pages.
Listing below goes to couch/addons/kfunctions.php

Code: Select all

// Page-view: the very first cloned page created automatically now has a shorter default title - "Default page".
// A clonable template's first page now gets auto-un-published.
$FUNCS->add_event_listener( 'alter_create_insert', 'default_page_naming' );
function default_page_naming( &$arr_insert, &$pg ){
    global $PAGE, $FUNCS;

    $is_master = $arr_insert['is_master'];
    $title = $arr_insert['page_title'];
    $name = $arr_insert['page_name'];
    $unwelcomed_title_str = '* PLEASE CHANGE THIS TITLE *';
    $unwelcomed_name_str = '-please-change-this-title';

    if( $is_master && strpos( $title, $unwelcomed_title_str )){
        $arr_insert['page_title'] = 'Default page';
        $arr_insert['page_name'] = str_replace( $unwelcomed_name_str, '', $name);

        if( $pg->tpl_is_clonable ){
            $arr_insert['publish_date'] = '0000-00-00 00:00:00';
        }
        else{
            // don't know for sure if the template is indeed non-clonable.
            // The <cms:template> tag that might follow can set this as clonable.
            // This is therefore handled by the other hook.
        }
    }
}

// A clonable template's first page now gets auto-un-published in case template was converted from non-clonable.
$FUNCS->add_event_listener( 'template_modified', 'unpublish_clonable_master' );
function unpublish_clonable_master( $rec, $attr, $prev_custom_values, $attr_custom, $modified ){
    global $PAGE, $DB, $FUNCS;

    // if the clonable status of masterpage is being modified ..
    if( array_key_exists('clonable', $modified) ){

        // get id of the masterpage
        if( $PAGE->is_master ){
            $id = $PAGE->id;
        }
        else{
            $rs = $DB->select( K_TBL_PAGES, array('id'), "template_id='" . $DB->sanitize( $PAGE->tpl_id ). "' AND is_master='1'" );
            if( !count($rs) ) return;
            $id = $rs[0]['id'];
        }

        if( $modified['clonable']==='1' ){
            // .. update page record to unpublish it
            $rs = $DB->update( K_TBL_PAGES, array('publish_date'=>'0000-00-00 00:00:00'), "id='" . $DB->sanitize( $id ). "'" );
        }
        elseif( $modified['clonable']==='0' ){
            // .. update page record to publish it
            $rs = $DB->update( K_TBL_PAGES, array('publish_date'=>$FUNCS->get_current_desktop_time() ), "id='" . $DB->sanitize( $id ). "'" );
        }
        if( $rs==-1 ) die( "ERROR: Unable to change publish status of masterpage" );
    }
}



Here is how to hide default pages automatically:

First, my default pages in each new template get the naming 'default-page-for-template-name-php' thanks to the code above.

Second, I use the technique described here viewtopic.php?f=8&t=11394 that helps to apply backend configuration code to all templates via "cms:config_list_view".

Finally, I use this snippet to get correct name of template automatically: viewtopic.php?f=8&t=11368&p=30152#p30152

So, in the end I can have following code in default configuration meant for all clonable templates:
Code: Select all
<cms:config_list_view exclude="default-page-for-<cms:embed 'functions/string/get-clean-url.html' url=k_template_name ></cms:embed>" searchable='1' >
    ...
</cms:config_list_view>


It is never late to optimize the workflow :)
3 posts Page 1 of 1

Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 1 guest