We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 53699
    • 22 Posts
    Hi team I am new to modx , for search i am using adv search, by going through documentation ,I found queryhook we can restrict the results with custom conditions.created a snippet called SchHook.
    $andConditions = array();
    
    if (!empty($_REQUEST['pubfromdate']) && $pubfromdate = strtotime($_REQUEST['pubfromdate'])) {
        $andConditions['modResource.publishedon:>'] = "{$pubfromdate}:numeric";
    }
    
    if (!empty($_REQUEST['pubtodate']) && $pubtodate = strtotime($_REQUEST['pubtodate'])) {
        $andConditions['modResource.publishedon:<'] = "{$pubtodate}:numeric";
    }
    $pubfromdate='2018-11-01';
    $andConditions['modResource.publishedon:>'] = "{$pubfromdate}:numeric";
    $andConditions['modResource.id:='] = "{10}:numeric";
    if (!empty($andConditions)) {
        $qhDeclaration = array(
            'qhVersion' => '1.3',
            'andConditions' => $andConditions
        );
        $hook->setQueryHook($qhDeclaration);
    }
    
    return true;
    
    

    Search code:=
    [[!AdvSearchForm? 
    
    &tpl=`myTpl`
    &queryHook=`SchHook`
    &debug=`1`
    
    ]]
    <h2>Results</h2>
    <p>[[!AdvSearch? &parents=`12`
    
    ]]</p>
    [[!AdvSearchHelp]]
    

    Results:--
    2017-08-28 16:42:33] (DEBUG in AdvSearch::loadDefaultConfigs @ C:\xampp\htdocs\xxxx\core\components\advsearch\model\advsearch\advsearch.class.php : 113)

    [AdvSearch] System environment: Array
    (
    [MODx version] => 2.5.7-pl
    [Php version] => 5.6.31
    [MySql version] => 10.1.25-MariaDB
    [AdvSearch version] => 2.0.0 beta2
    )

    [2017-08-28 16:42:33] (DEBUG in AdvSearch::loadDefaultConfigs @ C:\xampp\htdocs\xxxx\core\components\advsearch\model\advsearch\advsearch.class.php : 114)

    [AdvSearch] Config parameters before checking: Array
    (
    [corePath] => C:/xampp/htdocs/xxxx/core/components/advsearch/
    [assetsUrl] => assets/components/advsearch/
    [chunksPath] => C:/xampp/htdocs/xxxx/core/components/advsearch/elements/chunks/
    [modelPath] => C:/xampp/htdocs/xxxx/core/components/advsearch/model/
    [addCss] => 1
    [addJQuery] => 1
    [addJs] => 1
    [ajaxResultsId] => 0
    [asId] => as0
    [clearDefault] => 1
    [debug] => 1
    [help] => 1
    [jsSearchForm] => assets/components/advsearch/js/advsearchform.min.js
    [jsJQuery] => assets/components/advsearch/js/jquery-1.7.1.min.js
    [landing] => 0
    [liveSearch] => 0
    [method] => GET
    [opacity] => 1
    [searchIndex] => search
    [toPlaceholder] =>
    [tpl] => myTpl
    [urlScheme] => -1
    [withAjax] => 0
    [queryHook] => SchHook
    [charset] => UTF-8
    )

    [2017-08-28 16:42:33] (DEBUG in AdvSearchForm::output @ C:\xampp\htdocs\xxxx\core\components\advsearch\model\advsearch\advsearchform.class.php : 315)

    [AdvSearch] Elapsed time:0.0315s

    Results

    [2017-08-28 16:42:33] (WARN @ C:\xampp\htdocs\xxxx\core\cache\includes\elements\modsnippet\54.include.cache.php : 31)

    PHP notice: Undefined index: asId

    [2017-08-28 16:42:33] (WARN @ C:\xampp\htdocs\xxxx\core\components\advsearch\controllers\advsearch.mysql.controller.class.php : 143)

    PHP notice: Undefined index: hideContainers

    [2017-08-28 16:42:33] (WARN @ C:\xampp\htdocs\xxxx\core\components\advsearch\model\advsearch\advsearchrequest.class.php : 354)

    PHP notice: Undefined index: moreResults

    1 - 7 / 7
    1. Event 7
    2. Event 1
    3. EVENT 6
    4. Event 5
    5. EVENT 4
    6. Event 3
    7. Event 2
    1 - 7 / 7

    Please any one can suggest, what Iam doing wrong here, my query hook is not working.Any depdent extra needs to be installed. ?
      • 4172
      • 5,888 Posts
      I can't see where you are setting

      $pubfromdate and $pubtodate
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 4172
        • 5,888 Posts
        sorry, I was just blind
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 53699
          • 22 Posts
          Quote from: Bruno17 at Aug 28, 2017, 03:05 PM
          I can't see where you are setting

          $pubfromdate and $pubtodate
          yes you are right,I copied this code from stackoverflow,
          For testing purpose I had set the
          $pubfromdate='2018-11-01';
          $andConditions['modResource.publishedon:>'] = "{$pubfromdate}:numeric";
          $andConditions['modResource.id:='] = "{10}:numeric";


          at line no 10.;
          I am not able to figure out it is working or not. My search results were not impacted with queryHook
            • 53699
            • 22 Posts
            After many painful debugging , got my code working.
            My code ,
            [[!AdvSearchForm? &tpl=`AdvanceSearchForm_tpl`]]
            </h1>
            <h2>Results</h2>
            <p>[[!AdvSearch? &parents=`12`&queryHook=`FilterCalenderSnippet`   ]]
            

            form tpl (AdvanceSearchForm_tpl) :--
            <form id="[[+advsearch.asId]]_advsea-form" class="advsea-form" action="[[~[[+advsearch.landing]]]]" method="[[+advsearch.method]]">
              <fieldset>
                <input type="hidden" name="id" value="[[+advsearch.landing]]" />
                <input type="hidden" name="asId" value="[[+advsearch.asId]]" />
                [[+advsearch.helpLink]]<input type="text" id="[[+advsearch.asId]]_advsea-search" name="[[+advsearch.searchIndex]]" value="[[+advsearch.searchValue]]" />
            [[$SeminarCalendarDateChunk]]// give the dropdown of dates,you can put your form elements 
                [[+advsearch.liveSearch:isnot=`1`:then=`<input type="submit" id="[[+advsearch.asId]]_advsea-submit"  name="sub" value="[[%advsearch.search? &namespace=`advsearch` &topic=`default`]]" />`:else`=``]]
              </fieldset>
            </form>
            [[+advsearch.resultsWindow]]
            


            Query Hook snippet(FilterCalenderSnippet)

            • My Date tv is EventDateTv


             
            
            <?php
            $andConditions = array();
            // here i need to show events between one given input month. so I did some php to fetch first and last days of given month
            if (!empty($_REQUEST['calendar_date'])) {
               $dateToTest = $_REQUEST['calendar_date'];// my form element name is calendar_date
            $lastday = date('Y-m-t',strtotime($dateToTest));
            $andConditions['tv.EventDateTv:>='] = $dateToTest;
            $andConditions['tv.EventDateTv:<='] = $lastday ;
            }
            
            if (!empty($andConditions)) {
                $qhDeclaration = array(
                    'qhVersion' => '1.3',
                    'andConditions' => $andConditions
                );
                $hook->setQueryHook($qhDeclaration);
            }
            
            return true;