Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
2 posts Page 1 of 1
Hello, this one is helpful if you deal with multitude of templates during work.

Provided solution will rename system-created page for clonable templates from
Default page for your.template.here.php * PLEASE CHANGE THIS TITLE *

to something less emotional: 'Default page' for page title and 'default-page-for-your.template.here-php' for page name.
It will also unpublish this first page for clonable templates and publish it back for non-clonable ones.

Following goes to kfunction.php
Code: Select all
$FUNCS->add_event_listener( 'alter_create_insert', array('CustomEvents', 'default_page_naming') );
$FUNCS->add_event_listener( 'template_modified', array('CustomEvents', 'unpublish_clonable_master') );


class CustomEvents{

    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.
            }
        }
    }

    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" );
        }
    }

}



Kudos to @KK for help with this.

Please update your CouchCMS installation by downloading the latest version from CouchCMS-GitHub.
2 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