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
All pages in CouchCMS have a unique ID, therefore following often helps to build a link with cms:link tag
Code: Select all
<cms:link id='12345' />


To make this happen, put the following code in /couch/addons/kfunctions.php
The rest of the usual parameters work as expected (refer to http://docs.couchcms.com/tags-reference/link.html ) and masterpage is required for any parameter other than 'id'.
Code: Select all

$FUNCS->add_event_listener( 'alter_tag_link_execute', 'link_extended' );


    // Get links of the various views.
    function link_extended( $tag_name, $params, $node, &$link ){
        global $FUNCS, $DB, $PAGE, $CTX;
        if( count($node->children) ) {die("ERROR: Tag \"".$node->name."\" is a self closing tag");}

        extract( $FUNCS->get_named_vars(
                    array(
                           'masterpage'=>'',
                           'id'=>'',
                           'page'=>'',
                           'folder'=>'',
                           'year'=>'',
                           'month'=>'',
                           'day'=>''
                          ),
                    $params)
               );

        // sanitize params
        $masterpage = trim( $masterpage );
        $id = trim( $id );
        $page = trim( $page );
        $folder = trim( $folder );
        $year = trim( $year );
        $month = trim( $month );
        $day = trim( $day );

        if( $id ){
            // page-view by id
            $sql = "p.id='" . $DB->sanitize( $id ). "'";
            $rs = $DB->select( K_TBL_PAGES . ' p ', array('p.template_id as tid'), $sql );

            if( count($rs) ){
                $tid = $rs[0]['tid'];
                if( K_PRETTY_URLS ){
                    $pg = new KWebpage( $tid, $id );
                    if( $pg->error ){ return; }
                    $pg->set_context();
                    $link = $CTX->get( 'k_page_link', 1 );
                }
                else{
                    if( !$masterpage ){
                        // get masterpage by id
                        $sql = "t.id='" . $tid . "'";
                        $rs = $DB->select( K_TBL_TEMPLATES . ' t ', array('t.name as tname'), $sql );
                        if( count($rs) ){
                            $masterpage = $rs[0]['tname'];                       
                        }
                    }

                    if( $masterpage ){
                        $link = K_SITE_URL . $masterpage . '?p=' . $id;
                    }

                }
            }
            return 1;
        }


        if( $masterpage=='' ){ return; } // No masterpage, no link for the rest of the options

        if( $page ){
            // page-view
            $sql = "t.id = p.template_id and t.name='" . $DB->sanitize( $masterpage ) . "' and page_name='" . $DB->sanitize( $page ). "'";
            $rs = $DB->select( K_TBL_TEMPLATES . ' t, ' . K_TBL_PAGES . ' p ', array('t.id as tid', 'p.id as pid'), $sql );
            if( count($rs) ){
                $tid = $rs[0]['tid'];
                $pid = $rs[0]['pid'];
                if( K_PRETTY_URLS ){
                    $pg = new KWebpage( $tid, $pid );
                    if( $pg->error ){ return; }
                    $pg->set_context();
                    $link = $CTX->get( 'k_page_link', 1 );
                }
                else{
                    $link = K_SITE_URL . $masterpage . '?p=' . $pid;
                }
            }
        }
        elseif( $folder ){
            // folder-view
            $sql = "t.id = f.template_id and t.name='" . $DB->sanitize( $masterpage ) . "' and f.name='" . $DB->sanitize( $folder ). "'";
            $rs = $DB->select( K_TBL_TEMPLATES . ' t, ' . K_TBL_FOLDERS . ' f ', array('t.id as tid', 'f.id as fid', 'f.pid as pid'), $sql );
            if( count($rs) ){
                $tid = $rs[0]['tid'];
                $fid = $rs[0]['fid'];
                $pid = $rs[0]['pid']; //parent folder id
                if( K_PRETTY_URLS ){
                    if( $pid=='-1' ){
                        $link = K_SITE_URL . $FUNCS->get_pretty_template_link( $masterpage ) . $folder . '/';
                    }
                    else{
                        // get all the folders of this template
                        $folders = &$FUNCS->get_folders_tree( $tid, $masterpage );
                        $folder = &$folders->find_by_id( $fid );
                        if( !$folder ) return;
                        $link = K_SITE_URL . $folder->get_link();
                    }
                }
                else{
                    $link = K_SITE_URL . $masterpage . '?f=' . $fid;
                }
            }
        }
        elseif( $year ){
            // archive-view
            $sql = "t.name='" . $DB->sanitize( $masterpage ) . "'";
            $rs = $DB->select( K_TBL_TEMPLATES . ' t', array('t.id as tid'), $sql );
            if( count($rs) ){
                $link = K_SITE_URL . $FUNCS->get_archive_link( $masterpage, $year, $month, $day );
            }
        }
        else{
            // home-view
            $sql = "t.name='" . $DB->sanitize( $masterpage ) . "'";
            $rs = $DB->select( K_TBL_TEMPLATES . ' t', array('t.id as tid'), $sql );
            if( count($rs) ){
                if( K_PRETTY_URLS ){
                    $link = K_SITE_URL . $FUNCS->get_pretty_template_link( $masterpage );
                }
                else{
                    $link = K_SITE_URL . $masterpage;
                }
            }
        }

        return 1;
    }


unlimited time premium coding support per project - PM or email me or discuss in topic
active topics
Hello,

Thanks for this addon.

However I tried it filling the id attribute with the name of a view but it didn't work. The name is supposed to be the unique id or I missed something.

Here's the code I tried :

<a href="<cms:link id=button_link_unmissable_1 />" class="button"><cms:get "button_unmissable_1_<cms:show k_lang />" /></a>

button_link_unmissable_1 being a variable containing the name of a view

Could you give me some clarification please ?
@atmospheric,
The 'id' param actually expects the 'numeric' ID that MySQL automatically assigns to each record internally.
Since you are passing a string instead, it is being ignored.

button_link_unmissable_1 being a variable containing the name of a view
None of the other params accept the name of a view directly. However, you can set the params that relate to a particular view to get the desired link e.g. for a 'page_view', you can set the 'page' param with the name of the page whereas for a 'folder_view' you may set the 'folder' param with the folder's name.

Please see https://docs.couchcms.com/tags-reference/link.html for details.

Hope this helps.
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