We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25663 MODX Staff
    • 12,272 Posts
    Thanks Kyle... any chance you’ve looked at implementing templates? laugh
      Ryan Thrash, MODX Co-Founder
      Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
      • 15987
      • 786 Posts
      I’ve looked at it briefly, but have not had time to get really into it. I’m trying to finish up some of the work I’m doing with Garry, and will look at this after that. Send me a pm if this is something you want me to try and get done for the next release.

      Kyle
        • 30948
        • 3 Posts
        Here’s a modification of the doSearch function in AjaxSearch.inc.php to limit search results by web permissions. For regular visitors it will only return public documents; for logged in users it will return the public documents plus any documents of groups the user belongs to.

        function doSearch($searchString,$searchStyle,$useAllWords,$ajaxSearch) {
            if ($ajaxSearch) {
              $table_prefix = connectForAjax();
            } else {
              global $modx;
            }
            $search = explode(" ", $searchString);
            if ($ajaxSearch) {
                $tbl_sc = $table_prefix . "site_content";
                $tbl_dg = $table_prefix . "document_groups";
            } else {
                $tbl_sc = $modx->dbConfig['dbase'] . "." . $modx->dbConfig['table_prefix'] . "site_content";
                $tbl_dg = $modx->dbConfig['dbase'] . "." . $modx->dbConfig['table_prefix'] . "document_groups";
            }
            if ($docgrp = $modx->getUserDocGroups()) {
                $docgrp = implode(",", $docgrp);
                $tbl_sql = " LEFT JOIN $tbl_dg dg ON sc.id = dg.document";
                $qry_sql = "(ISNULL(dg.document_group) OR dg.document_group IN ($docgrp)) AND ";
            } else {
                $tbl_sql = "";
                $qry_sql = "sc.privateweb = 0 AND ";
            }
        
            if ($searchStyle == 'partial'){
              $sql = "SELECT DISTINCT sc.id, sc.pagetitle, sc.description, sc.content ";
              $sql .= "FROM $tbl_sc sc" . $tbl_sql . " WHERE ";
              if (count($search)>1 && $useAllWords){
                foreach ($search as $searchTerm){
                  $sql .= "(sc.pagetitle LIKE '%$searchString%' OR sc.description LIKE '%$searchString%' OR sc.content LIKE '%$searchTerm%') AND ";
                }
              } else {
                $sql .= "(sc.pagetitle LIKE '%$searchString%' OR sc.description LIKE '%$searchString%' OR sc.content LIKE '%$searchString%') AND ";
              }
              $sql .= $qry_sql . "sc.published = 1 AND sc.searchable=1 AND sc.deleted=0;";
            } else {
              $sql = "SELECT DISTINCT sc.id, sc.pagetitle, sc.description, sc.content ";
              $sql .= "FROM $tbl_sc" . $tbl_sql . " sc WHERE ";
              if (count($search)>1 && $useAllWords){
                foreach ($search as $searchTerm){
                  $sql .= "MATCH (sc.pagetitle, sc.longtitle, sc.introtext, sc.description, sc.content) AGAINST ('$searchTerm') AND ";
                }
              } else {
                $sql .= "MATCH (sc.pagetitle, sc.longtitle, sc.introtext, sc.description, sc.content) AGAINST ('$searchString') AND ";
              }
              $sql .= $qry_sql . "sc.published = 1 AND sc.searchable=1 AND sc.deleted=0;";
            }
        
            if ($ajaxSearch) {
                $rs = mysql_query($sql) or die ("Cannot query the database ($sql)");
            } else {
                $rs = $modx->dbQuery($sql);
            }
            return $rs;
        }
        


        Cheers,
        Ulrich

        Mod Note: Thread now locked - please continue discussions over at the Support/Comments for AjaxSearch thread in the Repository Items forum.

        This discussion is closed to further replies. Keep calm and carry on.