Can I add a Gallery2 search box to my website (without resorting to Google)?

WebKat
WebKat's picture

Joined: 2002-11-22
Posts: 182
Posted: Sun, 2012-03-25 22:13

Gallery version (not just "2"): 2.3.1 core 1.3.0.1
PHP version (e.g. 5.1.6):5.2.17 cgi-fcgi
PHPInfo Link (see FAQ): http://gallery2.opalcat.com/gallery2/phpinfo.php
Webserver (e.g. Apache 1.3.33): Apache
Database (e.g. MySql 5.0.32):mysqli 5.1.62-community-log, lock.system=flock
Activated toolkits (e.g. NetPbm, GD): ArchiveUpload, Exif, Getid3, ImageMagick, LinkItemToolkit, NetPBM, Thumbnail, Gd
Operating system (e.g. Linux):Linux host180.hostmonster.com 2.6.32-46.1.BHsmp #1 SMP Tue Sep 6 12:18:02 MDT 2011 x86_64
Browser (e.g. Firefox 2.0): Google Chrome 17.0.963.79 m

I was wondering if it was possible to put a search box for the gallery on my website (my gallery lives in a subdomain of my main site, so the folder structure is totally different). I tried copying the code from the sourcecode of a Gallery page and filling in the full url where it had relative paths, but that was a disaster that we shall not mention again. I think there is some javascript or php or something involved that I would need to access somehow. Does anyone know if this is possible?

--
WebKat, Gallery2 holdover extraordinaire.

 
floridave
floridave's picture

Joined: 2003-12-22
Posts: 27300
Posted: Sun, 2012-03-25 22:48

http://gallery.menalto.com/node/94321#comment-335056
You will need to edit the file, not sure about subdomain pathing. Then add the Javascript.

Dave
_____________________________________________
Blog & G2 || floridave - Gallery Team

 
WebKat
WebKat's picture

Joined: 2002-11-22
Posts: 182
Posted: Mon, 2012-03-26 02:19

Cool, thanks, I got it working by hardcoding SearchBlock.tpl... but what if I want to use a slightly different template for the search box on my site (for example, the search text box is too wide for my sidebar)... if I make a second template and call from that instead like <?php @readfile('http://gallery2.opalcat.com/gallery2/displayBlock.php?blockName=SearchBlockNarrow');?> ...is that how that would work? Would I have to edit any other files?

--
WebKat, Gallery2 holdover extraordinaire.

 
WebKat
WebKat's picture

Joined: 2002-11-22
Posts: 182
Posted: Mon, 2012-03-26 02:22

Also I added the javascript in my head tags... <script type="text/javascript" src="http://gallery2.opalcat.com/gallery2/modules/search/SearchBlock.js"></script> ...like the other thread said, but when I click into the search box the text doesn't disappear for me, either, just like the person in that thread said. I don't know anything about javascript so I woudln't be able to edit that file in any meaningful way, or tell you what any part of it was doing...

--
WebKat, Gallery2 holdover extraordinaire.

 
WebKat
WebKat's picture

Joined: 2002-11-22
Posts: 182
Posted: Fri, 2012-03-30 03:46

Anyone? I managed to get the search box to show up on my main site, but I want two different search boxes--one that is on the gallery and one that is narrower and doesn't have the "advanced search" text underneath for my main site. I don't want to alter my gallery's search box just to make it fit in the sidebar of my main site... so is there a way to duplicate it? I tried adding a "2" to the filename but there is obviously more to it than that because it didn't work. Also, the javascript that is supposed to empty the text when you click into the search box doesn't work...

--
WebKat, Gallery2 holdover extraordinaire.

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Fri, 2012-03-30 12:50

in order to add a new searchBlock you need to edit /modules/search/templates/blocks/blocks.inc and add your new block to the array:

$blocks['SearchBlockNarrow'] = array(
    'description' => $gallery->i18n('Search box narrow'),
    'vars' => array(
    'showAdvancedLink' => array(
        'description' => $gallery->i18n('Show advanced link'),
        'default' => 'true',
        'type' => 'boolean')));

and in /modules/search/Preloads.inc add case 'SearchBlockNarrow':

	case 'SearchBlock':
        case 'SearchBlockNarrow':

and then your template would be /modules/search/templates/blocks/SearchBlockNarrow.tpl should look something like:

{*
 * $Revision: 17650 $
 * Read this before changing templates!  http://codex.gallery2.org/Gallery2:Editing_Templates
 *}
{if !isset($showAdvancedLink)} {assign var="showAdvancedLink" value="true"} {/if}
<script type="text/javascript" src="{g->url href="modules/search/SearchBlock.js" forceFullUrl=true forceDirect=true}"></script>


<div class="{$class}">
  <form id="search_SearchBlock" action="{g->url}" method="get" onsubmit="return search_SearchBlock_checkForm()">
    <div>
      {g->hiddenFormVars}
      <input type="hidden" name="{g->formVar var="view"}" value="search.SearchScan"/>
      <input type="hidden" name="{g->formVar var="form[formName]"}" value="search_SearchBlock"/>
      <input type="text" id="searchCriteria" size="18"
         name="{g->formVar var="form[searchCriteria]"}"
         value="{g->text text="Search the Gallery"}"
         onfocus="search_SearchBlock_focus()"
         onblur="search_SearchBlock_blur()"
         class="textbox"/>
      <input type="hidden" name="{g->formVar var="form[useDefaultSettings]"}" value="1" />
    </div>
    {if $showAdvancedLink}
    <div>
      <a href="{g->url arg1="view=search.SearchScan" arg2="form[useDefaultSettings]=1"
               arg3="return=1"}"
     class="{g->linkId view="search.SearchScan"} advanced">{g->text text="Advanced Search"}</a>
    </div>
    {/if}
  </form>
</div>

<script type="text/javascript">
    if(typeof(document.getElementById('search_SearchBlock') != undefined))
    search_SearchBlock_init('Search the Gallery', 'Please enter a search term.', 'Searching in progress, please wait!');
</script>

then visit http://gallery2.opalcat.com/gallery2/displayBlock.php?listAll=1 to get a list of all available blocks and make sure yours shows up.

tested http://testr.suprsidr.com/displayBlock.php?blockName=SearchBlockNarrow

-s
FlashYourWeb and Your Gallery with The E2 XML Media Player for Gallery2

 
WebKat
WebKat's picture

Joined: 2002-11-22
Posts: 182
Posted: Fri, 2012-03-30 17:45

Thanks! I'll try that tonight when I have a little time!

--
WebKat, Gallery2 holdover extraordinaire.

 
WebKat
WebKat's picture

Joined: 2002-11-22
Posts: 182
Posted: Fri, 2012-03-30 19:59

I think I need a little more hand holding on this one, if you don't mind too terribly much. Here is what I've changed, but I'm not sure I put everything in the right places...

/modules/search/templates/blocks/blocks.inc

<?php
$blocks['SearchBlock'] = array(
    'description' => $gallery->i18n('Search box'),
    'vars' => array(
	'showAdvancedLink' => array(
	    'description' => $gallery->i18n('Show advanced link'),
	    'default' => 'true',
	    'type' => 'boolean')));
		
$blocks['SearchBlockNarrow'] = array(
    'description' => $gallery->i18n('Search box narrow'),
    'vars' => array(
    'showAdvancedLink' => array(
        'description' => $gallery->i18n('Show advanced link'),
        'default' => 'false',
        'type' => 'boolean')));
?>

/modules/search/Preloads.inc

<?php
/*(removed extraneous expository content for brevity) */

class SearchPreloads {
    function preload(&$template, $preload, $paramSet) {
	global $gallery;

	switch($preload) {
	case 'SearchBlock':
	    $template->javascript('modules/search/SearchBlock.js');
	    return null;
	case 'SearchBlockNarrow':
	}

	return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
    }
}
?>

I also tried this placement:

/modules/search/Preloads.inc

<?php
/*(removed extraneous expository content for brevity) */

class SearchPreloads {
    function preload(&$template, $preload, $paramSet) {
	global $gallery;

	switch($preload) {
	case 'SearchBlock':
	case 'SearchBlockNarrow':
	    $template->javascript('modules/search/SearchBlock.js');
	    return null;
	}

	return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
    }
}
?>

/modules/search/templates/blocks/SearchBlockNarrow.tpl

{*
 * $Revision: 17650 $
 * Read this before changing templates!  http://codex.gallery2.org/Gallery2:Editing_Templates
 *}
{if !isset($showAdvancedLink)} {assign var="showAdvancedLink" value="true"} {/if}
<script type="text/javascript" src="{g->url href="modules/search/SearchBlock.js" forceFullUrl=true forceDirect=true}"></script>


<div class="{$class}">
  <form id="search_SearchBlock" action="{g->url}" method="get" onsubmit="return search_SearchBlock_checkForm()">
    <div>
      {g->hiddenFormVars}
      <input type="hidden" name="{g->formVar var="view"}" value="search.SearchScan"/>
      <input type="hidden" name="{g->formVar var="form[formName]"}" value="search_SearchBlock"/>
      <input type="text" id="searchCriteria" size="15"
         name="{g->formVar var="form[searchCriteria]"}"
         value="{g->text text="Search the Gallery"}"
         onfocus="search_SearchBlock_focus()"
         onblur="search_SearchBlock_blur()"
         class="textbox"/>
      <input type="hidden" name="{g->formVar var="form[useDefaultSettings]"}" value="1" />
    </div>
    {if $showAdvancedLink}
    <div>
      <a href="{g->url arg1="view=search.SearchScan" arg2="form[useDefaultSettings]=1"
               arg3="return=1"}"
     class="{g->linkId view="search.SearchScan"} advanced">{g->text text="Advanced Search"}</a>
    </div>
    {/if}
  </form>
</div>

<script type="text/javascript">
    if(typeof(document.getElementById('search_SearchBlock') != undefined))
    search_SearchBlock_init('Search the Gallery', 'Please enter a search term.', 'Searching in progress, please wait!');
</script>

I did all of that and uploaded it. Then I went to http://gallery2.opalcat.com/gallery2/displayBlock.php?listAll=1 and got a blank page. Even the source code was blank. (I tried two different ways to edit the Preloads.inc, as you can see, but neither of them made anything different happen--perhaps there is a third, correct placement. Heh.)

This the code I use to calld the block on my site:

<?php @readfile('http://gallery2.opalcat.com/gallery2/displayBlock.php?blockName=SearchBlockNarrow');?>

I tried it, like this:

<!--gallery search-->
	<div class="left_menu_fix">
	<?php @readfile('http://gallery2.opalcat.com/gallery2/displayBlock.php?blockName=SearchBlockNarrow');?>
	</div>
<!--end gallery search-->

And what I saw on the page in the sourcecode when I loaded the page was this:

<!--gallery search-->

	<div class="left_menu_fix">

	
	</div>

<!--end gallery search-->

So obviously I've taken a wrong turn here, somewhere. Now, before, when I was winging it, I came up with a template that looked like this (the site is opalcat.com, the gallery is gallery2.opalcat.com so I hardcoded some of the urls, but I'm not sure if I got them all right...) and it DID show up on my page... and when I tried a search, it worked... but there may be something wrong with it that I haven't thought of, I don't know. I also used some other, in-line javascript code to clear out the value text when you click into the search box because I could never get the gallery's js to do it:

{*
 * $Revision: 17650 $
 * Read this before changing templates!  http://codex.gallery2.org/Gallery2:Editing_Templates
 *}
{if !isset($showAdvancedLink)} {assign var="showAdvancedLink" value="false"} {/if}

{g->addToTrailer}
<script type="text/javascript">
  // <![CDATA[
  search_SearchBlock_init('{g->text text="Search the Gallery" forJavascript=true}', '{g->text text="Please enter a search term." forJavascript=true}', '{g->text text="Searching in progress, please wait!" forJavascript=true}');
  // ]]>
</script>
{/g->addToTrailer}

<div class="block-search-SearchBlock">
<form id="search_SearchBlock" action="http://gallery2.opalcat.com/gallery2/" method="get" onsubmit="return search_SearchBlock_checkForm()">
<div>
<input type="hidden" name="g2_return" value="http://gallery2.opalcat.com/gallery2/?g2_ckName=SearchBlock"/>
<input type="hidden" name="g2_formUrl" value="http://gallery2.opalcat.com/gallery2/?g2_ckName=SearchBlock"/>
<input type="hidden" name="g2_authToken" value="94ae04857f69"/>

<input type="hidden" name="g2_view" value="search.SearchScan"/>
<input type="hidden" name="g2_form[formName]" value="search_SearchBlock"/>
<input type="text" id="searchCriteria" size="15"
name="g2_form[searchCriteria]"
value="Gallery Search"
onclick="this.value='';" 
onfocus="this.select()" 
onblur="this.value=!this.value?'Gallery Search':this.value;"
class="textbox"/>
<input type="hidden" name="g2_form[useDefaultSettings]" value="1" />
</div>
</form>
</div>

It shows up in the upper left column at http://opalcat.com if you want to test it. I still can't get http://gallery2.opalcat.com/gallery2/displayBlock.php?listAll=1 to show anything at all...
--
WebKat, Gallery2 holdover extraordinaire.

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Fri, 2012-03-30 20:16

block.inc looks right, second prloads.inc is correct SearchBlockNarrow.tpl is fine.
not sure why listAll does not work, this is what it should look like http://testr.suprsidr.com/displayBlock.php?listAll=1
but its only for your information.

but http://gallery2.opalcat.com/gallery2/displayBlock.php?blockName=SearchBlockNarrow works mostly

the reason I rearranged the javascript is you expressed using it externally and your external page does not know what to do with {g->addToTrailer}
and we need that javascript included in a certain order.

-s
FlashYourWeb and Your Gallery with The E2 XML Media Player for Gallery2

 
WebKat
WebKat's picture

Joined: 2002-11-22
Posts: 182
Posted: Fri, 2012-03-30 20:58

The current SearchBlockNarrow is my hacked up one with all of the hardcoded urls and such. I renamed the SearchBlockNarrow that you gave me to something else while I tested it... the one you gave me just didn't show up at all. It was just empty where the code should be :(

--
WebKat, Gallery2 holdover extraordinaire.