Problems, need help? Have a tip or advice? Post it here.
7 posts Page 1 of 1
Hello...

I have set up a back end of available resources with a series of editables, one of which is "relation". I am using this "relation" editable to set tags for the resources in the database using a separate template to name the tags. See the code for this below:

Code: Select all
<cms:editable name='catalog_tags' type='relation' masterpage='ppp-tagstemplate.php' label='Keyword Tags' orderby='page_title' order_dir='asc' advanced_gui='1' desc='Please click the add button and then check all of the applicable tags. If you need a new tag, this must be added to the Catalog Tags template first.'  />


Each resource now has several tags checked. So for example, an educational video may have tags checked for "education", "video", "animation", etc.

I have implemented the search functionality and have set up a results page that is working correctly with one exception: The search results are not returning results based on these tags. The search query is evaluating the other editable regions, like the title or the description, but the tags are not being searched with the query.

Here is my code for the search results:

Code: Select all
<cms:search masterpage='ppp.php'>
               <a href="<cms:show k_page_link />" style="text-decoration: none;">                        
                  <div class="container cataloglisting">
                     <div class="row">
                        <div class="span2" style="width:100px;">
                           <img src="<cms:show item_thumbnail />" />
                        </div>
                        <div class="span5" style="width:440px;">
                           <span style="font-size: 18px;"><cms:show k_search_title /></span>
                           <br>
                           <span style=" font-weight: 300;"><cms:show k_search_excerpt /></span><br>
                           <a href="<cms:show k_page_link />" class="btn btn-primary" style="margin-top: 20px;">MORE ABOUT THIS RESOURCE <span class="glyphicon glyphicon-share-alt" aria-hidden="true" style="top: 2px;margin-left: 3px;"></span></a>
                        </div>
                     </div>
                  </div>
               </a>


            </cms:search>



Can you help me figure out how to force the search query to include the tags that have been checked off via the "relation" method?

Thank in advance.
Hi,

I suppose what you want is that if someone searches for terms "education", "video", "animation" (i.e. your tag names), the pages related to those tags should show up.

The pages themselves, unfortunately, do not store any textual contents of the related entities (tags, in this case) and so those do not show in search results.

If you are willing to put in some extra effort, here is a workaround that could help -
the idea is to use a separate text or textarea editable region in your main template (ppp.php);
this region remains hidden but, whenever a page of ppp.php gets saved, it gets automatically populated by the 'page titles' (or whatever else you wish) of the tags the page has been related with.

Following post has an implementation of something very similar -
viewtopic.php?f=4&t=12687#p35824

The only difference is that in the post above, the relation is not multiple and so the PHP function actually picks only the first related page from the available array - you can easily modify it to loop through all the items (tags) in the array and save their data. Let me know if you happen to need any help with the process.

Hope this helps.
Thanks KK...

Yes, I was able to add the text field to the back end and have it populate with one of the tags—but like you said, I was unable to collect all of the tags in the array.

The kfunctions.php code I implemented is below:

Code: Select all
function my_set_tags_text(){

    $field_name = 'catalog_tags';

    ///
    global $FUNCS, $PAGE, $DB;

    $title = '';
    if( array_key_exists($field_name, $PAGE->_fields) ){
        $vals = $PAGE->_fields[$field_name]->items_selected;
        if( is_array($vals) ){
            $page_id = $vals[0];
            if( $FUNCS->is_non_zero_natural($page_id) ){
                $page_id = (int)$page_id;

                $rs = $DB->select( K_TBL_PAGES, array('page_title', 'page_name'), "id='" . $DB->sanitize( $page_id ). "'" );
                if( count($rs) ){
                    $title = ( $rs[0]['page_title'] )? $rs[0]['page_title'] : $rs[0]['page_name'];
                }
            }
        }
    }
    return $title;
}



As you can see, the tags array is called "catalog_tags" and the function is now called "my_set_tags_text".

Can you help me update this code so that it adds all of the tags instead of one? Preferably separated by commas?

That should hopefully do the trick.
Wanted to bump this to make sure it didn't fall of your radar. Any help you can provide would be greatly appreciated.

Thanks very very much.
My apologies, @thegeniusss - I did lose track of this issue.

Here is the modified PHP function that should save multiple values in the text region (with commas)
Code: Select all
function my_set_tags_text(){

    $field_name = 'catalog_tags';

    ///
    global $FUNCS, $PAGE, $DB;

    $title = $sep = '';
    if( array_key_exists($field_name, $PAGE->_fields) ){
        $vals = $PAGE->_fields[$field_name]->items_selected;
        if( is_array($vals) ){
            foreach( $vals as $page_id ){
                if( $FUNCS->is_non_zero_natural($page_id) ){
                    $page_id = (int)$page_id;

                    $rs = $DB->select( K_TBL_PAGES, array('page_title', 'page_name'), "id='" . $DB->sanitize( $page_id ). "'" );
                    if( count($rs) ){
                        $title .= $sep . ( ($rs[0]['page_title'])? $rs[0]['page_title'] : $rs[0]['page_name'] );
                        $sep = ', ';
                    }
                }
            }
        }
    }
    return $title;
}

Hope this helps. Please let me know.
Seems to have done the trick. Thank you so much for your help. I am using CouchCMS like crazy these days and every day I find a new use for it. Very much appreciated.
You are welcome :)
7 posts Page 1 of 1