Page 1 of 1

how to make a page not searchable deppending on its position [SOLVED]

Posted: Sun Feb 14, 2010 4:04 pm
by isaacd
Hello,
   I am trying to make pages unsearchable depending on their position. I want all pages with a position of between 1 and 2 to not be indexed. I tried this:

{if $position >= 1 && $position < 2}{/if}

and it prevented more pages than just those between 1 and 2 from being indexed. I suspect this is because the search module sees and does not care that it is in an if statement.

Thanks in advance.
   Isaac.

Re: how to make a page not searchable deppending on its position

Posted: Sun Feb 14, 2010 4:07 pm
by JeremyBASS
IIRC can go any where... you could go in the menumanger...

{if $node->depth>= 1 && $node->depth< 2}{/if}

may be one  way...
Cheers -Jeremy

Re: how to make a page not searchable deppending on its position

Posted: Sun Feb 14, 2010 10:41 pm
by isaacd
I tried putting it in the menu template and it did not work. I am not sure that I did it the right way though. Where does it go in the template? Two things to consider:
1) What you mentioned as node->depth should be node->hierarchy and
2) The pages that I want to exclude from the search are not visible from the home page of my site, and other sites. To see what I mean, go to trilliumcharterschool.org  then go to trilliumcharterschool.org/inside  . Note how the navigation structures change.

Re: how to make a page not searchable deppending on its position

Posted: Thu Feb 18, 2010 2:10 am
by isaacd
As another approach, is it possible to change the default options (template, show in menu, page is searchable etc.) on the options tab depending on the hierarchical position. So it would be something like this:

All pages that are children of the first page are not searchable by default but...
Any other pages that are not children of the first page are searchable by default.

Thanks in advance
    Isaac.

Re: how to make a page not searchable deppending on its position

Posted: Sun Feb 28, 2010 5:37 am
by isaacd
I got found out how to make this work. Here is a very comprehensive guide:

Purpose

To exclude pages from the search results based upon their root parrent.

Method

This will not exclude those pages from being indexed, but rather, this guide will simply hide them from the results page. This will get a list of aliases of those pages that we want to exclude. This will use a hierarchical position to govern which pages will be excluded. For example, if you use 1 as your hierarchical position, you will exclude all pages that have a position that is greater than or equal to 1 and less than 2 (>= 1 && url of every result.

Steps

1. Create a UDT with this code called all_content:

Code: Select all

/*$params['assign']*/
/*$params['parent_hierarchy']*/

if (!empty($params['assign']) && !empty($params['parent_hierarchy'])){
	global $gCms;
	$contentops =& $gCms->GetContentOperations();
	$all_content = $contentops->GetAllContent(false);
	$assign = array();
	foreach($all_content as $one_page){
		if (strpos($one_page->mHierarchy, $params['parent_hierarchy']) === 0){
			$assign[] = $one_page->mAlias;
		}
	}
	$smarty = $gCms->GetSmarty();
	$smarty->assign($params['assign'], $assign);
}
Thanks to Peciura for replying to my other post: http://forum.cmsmadesimple.org/index.php/topic,41759.0.html.

2. Put this at the very top of your search results template:

Code: Select all

{assign var="nocount" value=0}
3. Call the UDT in your search results template like this (where parrent_hierarchy is the position you want to exclude, in this case, children of page 1):

{all_content assign='children' parent_hierarchy='00001'}

The tag should be called directly after the {foreach from=$results item=entry} tag. So it should look like this:

 {foreach from=$results item=entry}

 {all_content assign='children' parent_hierarchy='00001'}

This causes the all_content tag to be called once per search result entry.

4. Put this code directly after {all_content assign='children' parent_hierarchy='00001'}:

Code: Select all

{foreach from=$children item=child}
    {cms_selflink href=$child assign='childurl'}
    {if $childurl == $entry->url}
       {assign var="show" value="no"}
    {/if}
{/foreach}
5. After that, you need to make an {if} statement like this:

{if $show != "no"}
[Some code to display the search results]
{else}
 {assign var="nocount" value=$nocount+1}
{/if}

This might look something like this:

Code: Select all

{if $show != "no"}
  <li><a href="{$entry->url}">{$entry->title}</a> ({$entry->weight}%){$entry->id}</li>
{else}
  {assign var="nocount" value=$nocount+1}
{/if}
6. Directly after the {if $show != "no"} .............. {/if} statement, we need to reset the show variable to something besides "no". Add this code directly after the {if $show != "no"} .............. {/if}:

Code: Select all

{assign var="show" value=""}
7. By default, you will have something like this in your results template:

{if $itemcount > 0}
[Some code to display the search results]
{else}
{$noresultsfound}
{/if}

You need to get rid of the else condition, so you will have something like this:

{if $itemcount > 0}
[Some code to display the search results]
{/if}

The reason for this is because the $itemcount will still be greater than 0 even if all the items are from the excluded pages, and thus hidden. So after the end of {if $itemcount > 0} ...................... {/if} statement, you need to add this (assuming you want your $noresultsfound message in bold and in the element):

Code: Select all

{if $nocount == $itemcount}
  <p><strong>{$noresultsfound}</strong></p>
{/if}
The $nocount variable is how many results have been excluded. If this is equal to the total number of results ($itemcount), then that means that there are no search results. This holds true when $itemcount is equal to 0, because $nocount is also equal to 0.

The Code
In Syntax (the bare minimum code, assuming that you have created the UDT, see step 1):

{assign var="nocount" value=0}

{if $itemcount > 0}

 {foreach from=$results item=entry}

 {all_content assign='children' parent_hierarchy='[the hierarchical position root that you want to exclude]'}

 {foreach from=$children item=child}
   {cms_selflink href=$child assign='childurl'}
   {if $childurl == $entry->url}
      {assign var="show" value="no"}
   {/if}
 {/foreach}

 {if $show != "no"}
 

Code: Select all

[/i][/b]
  {else}
    {assign var="nocount" value=$nocount+1}
  {/if}
  {assign var="show" value=""}
  {/foreach}
{/if}

{if $nocount == $itemcount}
  [b][i][Code to show that no results were found. Usually {$noresultsfound}][/i][/b]
{/if}

In example (adapted to the default result template using 1 as the hierarchy # to exclude):

[code]{assign var="nocount" value=0}

{if $itemcount > 0}

<ul>
  {foreach from=$results item=entry}

  {all_content assign='children' parent_hierarchy='00001'}

  {foreach from=$children item=child}
    {cms_selflink href=$child assign='childurl'}
    {if $childurl == $entry->url}
       {assign var="show" value="no"}
    {/if}
  {/foreach}

  {if $show != "no"}
  <li><a href="{$entry->url}">{$entry->title}</a> ({$entry->weight}%){$entry->id}</li>
  {* 
     You can also instantiate custom behaviour on a module by module basis by looking at
     the $entry->module and $entry->modulerecord fields in $entry 
      ie: {if $entry->module == 'News'}{News action='detail' article_id=$entry->modulerecord detailpage='News'} 
  *}
  {else}
    {assign var="nocount" value=$nocount+1}
  {/if}
  {assign var="show" value=""}
  {/foreach}
</ul>
{else}
<ul></ul>
{/if}

{if $nocount == $itemcount}
  <p><strong>{$noresultsfound}</strong></p>
{/if}
Notes
[Important] When I say "children" of a certain page, that includes the page itself. So If I say children of page 1, that will include page 1.

[Minor] If you have a setup that has all the search results listed in a element, you may need to have an else condition for {if $itemcount > 0}, that would be {else} because if all the results are excluded (your $itemcount is more than 0, but all the items are excluded), you will still have a even though there are no show results, because there are still "entries." But if there are no entries, then you will not have that unless you add it manually with an else condition. This will change the look of the front end a little bit.