Forum for discussing general topics related to Couch.
13 posts Page 2 of 2
@trendoman,

Thanks man, I need to use that validation on search input which I using <cms:searchform> on the page, I don't know how to apply this <cms:input> on in....

For the typeahead, I gave up. I now using this for autocomplete: http://easyautocomplete.com/. It's really easy to use, the function is really good, you can even add the avatar easily. This autocomplete jquery can also use to search approximate string/partial terms/mid-words which typeahead fail to provide. You really need to take a try on this plugin...

I can easily using the JSON file with this plugin, here is the source code:
For the JSON file, which we can converted it in PHP:
Code: Select all
<?php require_once( 'admin/cms.php' ); ?>
<cms:template title='productlist' hidden='1' />
<cms:content_type 'application/json'/>

[
    <cms:pages masterpage='products.php'>
      {"name": "<cms:addslashes><cms:show k_page_title/></cms:addslashes>"}<cms:if "<cms:not k_paginated_bottom/>">,</cms:if>
    </cms:pages>
]
<?php COUCH::invoke(); ?>


In your main file:
Code: Select all
<script>
var options = {
   url: "productlist.php",

   getValue: "name",

   highlightPhrase: false,

   list: {
      maxNumberOfElements: 500,
      match: {
         enabled: true
      },
      sort: {
         enabled: true
      }
      
   }
};

$("#s").easyAutocomplete(options);
</script>

Just inserted the PHP file, then your autocomplete should now work as charm.... Please do correct me if I did anything wrong... :D :D
KK, working great on the front-end with the new search_ex tag. Can this new search tag be used on the admin when the following code is added to allow searchable pages on the template?

Code: Select all
<config_list_view searchable='1' />


KK wrote: With the above code in place, you'll have a new tag named <cms:search_ex>. It is a direct replacement of the core cms:search tag so simply change your existing code to use cms:search_ex instead of cms:search e.g.
Code: Select all
<cms:search_ex keywords='man' masterpage='products.php'>
    <h3><a href="<cms:show k_page_link />"><cms:show k_search_title /></a></h3>
    <cms:show k_search_excerpt />
</cms:search_ex>


Now you should be able to search for three character words too.

Please test this and let me know if it works as expected.

Thanks.
Can this new search tag be used on the admin when the following code is added to allow searchable pages on the template?

<config_list_view searchable='1' />

@Blutbaden, please use the following code in place of the original given in the previous post -

Code: Select all
/////////// begin <cms:search_ex> ///////////
    class SearchEx{
        function tag_handler( $tag_name, $params, $node, &$html ){
            global $CTX, $FUNCS, $TAGS, $DB, $AUTH;

            extract( $FUNCS->get_named_vars(
                       array(
                             'masterpage'=>'',
                             'keywords'=>'',
                            ),
                       $params) );

            $masterpage = trim( $masterpage );
            if( $keywords ){
                $keywords = trim( $keywords );
            }
            else{
                $keywords = trim( $_GET['s'] );
            }
            // is something being searched?
            if( !$keywords ) return;

            // get the keywords being searched
            $keywords = strip_tags( $keywords );
            $orig_keywords = $keywords;
            $keywords = str_replace( array(",", "+", "-", "(", ")"), ' ', $keywords );

            // http://www.askingbox.com/tip/mysql-combine-full-text-search-with-like-search-for-words-with-3-letters
            $searchwords = array_filter( array_map("trim", explode(' ', $keywords)) );
            $longwords = array();
            $shortwords = array();

            foreach( $searchwords as $word ){
                if( mb_strlen($word, 'UTF-8')>3 ){
                    $longwords[] = $word;
                }
                else{
                    if( mb_strlen($word, 'UTF-8')==3 ){
                        if(!in_array($val,array('and','the','him','her')))
                        $shortwords[] = $word;
                    }
                }
            }

            if( !count($shortwords) ){ // no 3 character words - delegate to original cms:search tag
                if( !count($longwords) ) return;

                $keywords = implode (' ', $longwords );

                // delegate to 'pages' tag
                for( $x=0; $x<count($params); $x++ ){
                    $param = &$params[$x];
                    if( strtolower($param['lhs'])=='keywords' ){
                        $param['rhs'] = $keywords;
                        $added = 1;
                        break;
                    }
                }
                if( !$added ){
                    $params[] = array('lhs'=>'keywords', 'op'=>'=', 'rhs'=>$keywords);
                }

                $html = $TAGS->pages( $params, $node, 1 );
            }
            else{
                // we have 3 character words to contend with.

                // craft sql query
                // select ..
                $sql = 'SELECT cp.template_id, cp.id, cf.title, cf.content';

                if( count($longwords) ){
                    // add the '+' for boolean search
                    $sep = "";
                    foreach( $longwords as $kw ){
                        $bool_keywords .= $sep . "+" . $kw;
                        $sep = " ";
                    }

                    $sql .= ", ((MATCH(cf.content) AGAINST ('".$DB->sanitize($bool_keywords)."') * 1) + (MATCH(cf.title) AGAINST ('".$DB->sanitize($bool_keywords)."') * 1.25)) as score";
                }

                // from ..
                $sql .= "\r\n" . 'FROM ';
                $sql .= K_TBL_FULLTEXT ." cf
                inner join  ".K_TBL_PAGES." cp on cp.id=cf.page_id
                inner join ".K_TBL_TEMPLATES." ct on ct.id=cp.template_id";

                // where ..
                $sql .= "\r\n" . 'WHERE ';
                if( count($longwords) ){
                    $sql .= " ((MATCH(cf.content) AGAINST ('".$DB->sanitize($bool_keywords)."' IN BOOLEAN MODE) * 1) + (MATCH(cf.title) AGAINST ('".$DB->sanitize($bool_keywords)."' IN BOOLEAN MODE) * 1.25))";
                }
                else{
                    $sql .= '1=1';
                }
                if( $masterpage ){
                    // masterpage="NOT blog.php, testimonial.php"
                    $sql .= $FUNCS->gen_sql( $masterpage, 'ct.name');
                }
                foreach( $shortwords as $kw ){
                    $sql .= " AND (cf.content LIKE '%".$DB->sanitize($kw)."%' OR cf.title LIKE '%".$DB->sanitize($kw)."%')";
                }
                if( $hide_future_entries ){
                    $sql .= " AND cp.publish_date < '".$FUNCS->get_current_desktop_time()."'";
                }
                $sql .= " AND NOT cp.publish_date = '0000-00-00 00:00:00'";
                $sql .= " AND cp.access_level<='".$AUTH->user->access_level."'";
                $sql .= " AND ct.executable=1";

                // order
                if( count($longwords) ){
                    $sql .= "\r\n";
                    $sql .= "ORDER BY score DESC";
                }

                // delegate sql to cms:query
                for( $x=0; $x<count($params); $x++ ){
                    $param = &$params[$x];
                    if( strtolower($param['lhs'])=='sql' ){
                        $param['rhs'] = $sql;
                        $added = 1;
                        break;
                    }
                }
                if( !$added ){
                    $params[] = array('lhs'=>'sql', 'op'=>'=', 'rhs'=>$sql);
                }
                $params[] = array('lhs'=>'fetch_pages', 'op'=>'=', 'rhs'=>'1');

                $FUNCS->add_event_listener( 'alter_page_tag_context', array('SearchEx', 'ctx_handler') );
                $node->__my_keywords = array_merge( $longwords, $shortwords );
                $html = $TAGS->pages( $params, $node, 5 );
                $FUNCS->remove_event_listener( 'alter_page_tag_context', array('SearchEx', 'ctx_handler') );
            }

            $CTX->set( 'k_search_query', $orig_keywords, 'global' );
           
            return 1; // prevent the original tag from executing as we have set the output above

        }// end func tag_handler

        function ctx_handler( $rec, $mode, $params, $node ){
            global $CTX, $FUNCS;

            if( !is_array($node->__my_keywords) ) return;

            $keywords = $node->__my_keywords;
            if( $CTX->get('k_template_is_clonable', true) ){
                $hilited = $FUNCS->hilite_search_terms( $keywords, $rec['title'], 1 );
            }
            else{
                $tpl_title = $CTX->get('k_template_title', true);
                $hilited = $tpl_title ? $tpl_title : $CTX->get('k_template_name', true);
            }
            $CTX->set( 'k_search_title', $hilited );
            $CTX->set( 'k_search_content', $rec['content'] ); //entire content searched

            $hilited = $FUNCS->hilite_search_terms( $keywords, $rec['content'] );
            $CTX->set( 'k_search_excerpt', $hilited ); //hilighted excerpt of searched content
        }
    }

    $FUNCS->add_event_listener( 'alter_tag_search_execute', array('SearchEx', 'tag_handler') );

    /////////// end <cms:search_ex> ///////////

And now you can use the original <cms:search> tag (instead of <cms:search_ex>) and it should support the 3 char words too.
Since the admin panel also uses <cms:search>, it should now also support the shortened limit.

Please note that now your code on frontend templates using <cms:search_ex> will throw error that the tag is not found. That is fine - just replace <cms:search_ex> with <cms:search>.

Do test things out in the admin panel and let me know it this helps.
13 posts Page 2 of 2

Who is online

In total there are 4 users online :: 0 registered, 0 hidden and 4 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 4 guests