Redfern_89

search_model.php

Jul 7th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.97 KB | None | 0 0
  1. <?php
  2.  
  3.     header('content-Type: text/plain');
  4.    
  5.     function create_sql_taxonomy($table_prefix, $link_table, $taxonomy_table, $terms_table, $taxonomies) {
  6.         $t_count = 1;
  7.         $sql = [];
  8.         $sql_str = '';
  9.         $terms_ = [];
  10.         $terms = [];
  11.        
  12.         for ($i = 0; $i < count($taxonomies); $i++) {
  13.             for ($j = 0; $j < count($taxonomies[$i]); $j++) {
  14.                
  15.                 $t = $taxonomies[$i][$j];
  16.                
  17.                 switch ($j) {
  18.                     case 0:
  19.                         $logic = $t;
  20.                         break;
  21.                     case 1:
  22.                         $taxonomy = $t;
  23.                         break;
  24.                     case 2:
  25.                         if (is_string($t) && strlen($t) > 0) {
  26.                             $t = explode(',', $t);
  27.                         } else {
  28.                             $t = [];
  29.                         }
  30.                         if (is_array($t) && count($t)) {
  31.                             $terms = $t;
  32.                         } else {
  33.                             $terms = [];
  34.                         }
  35.                         break;
  36.                 }
  37.             }
  38.  
  39.             if (count($terms)) {
  40.                 if ($logic == 'AND') {
  41.                     for ($x = 0; $x < count($terms); $x++) {
  42.                         $term = $terms[$x];
  43.                         if (strlen($term)) {
  44.                             $sql_str = "\tJOIN `$table_prefix$link_table` AS `tr$t_count` ON `tr$t_count`.`object_id` = `p`.`id`\r\n";
  45.                             $sql_str .= "\tJOIN `$table_prefix$taxonomy_table` AS `tt$t_count` ON `tt$t_count`.`term_id` = `tr$t_count`.`term_taxonomy_id`\r\n";
  46.                             $sql_str .= "\tJOIN `$table_prefix$terms_table` AS `t$t_count` ON `t$t_count`.`id` = `tt$t_count`.`term_id`\r\n";
  47.                             $sql_str .= "\t\tAND (`tt$t_count`.`taxonomy` = '$taxonomy' AND `t$t_count`.`name` = '$term')\r\n\r\n";
  48.                             $sql[] = $sql_str;
  49.                             $t_count++;
  50.                         }
  51.                     }
  52.                 }
  53.                 if ($logic == 'OR') {
  54.                     for ($x = 0; $x < count($terms); $x++) {
  55.                         $term = $terms[$x];
  56.                         if (strlen($term))
  57.                             $terms_[$x] = "'{$terms[$x]}'";
  58.                     }
  59.                     $sql_str = "\tJOIN `$table_prefix$link_table` AS `tr$t_count` ON `tr$t_count`.`object_id` = `p`.`id`\r\n";
  60.                     $sql_str .= "\tJOIN `$table_prefix$taxonomy_table` AS `tt$t_count` ON `tt$t_count`.`term_id` = `tr$t_count`.`term_taxonomy_id`\r\n";
  61.                     $sql_str .= "\tJOIN `$table_prefix$terms_table` AS `t$t_count` ON `t$t_count`.`id` = `tt$t_count`.`term_id`\r\n";
  62.                     $sql_str .= "\t\tAND (`tt$t_count`.`taxonomy` = '$taxonomy' AND `t$t_count`.`name` IN (" . implode(',', $terms_) . "))\r\n\r\n";
  63.                     $sql[] = $sql_str;
  64.                     $t_count++;
  65.                 }
  66.             }
  67.            
  68.         }
  69.         return implode(" ", $sql);
  70.        
  71.     }
  72.    
  73.     function prepare_sql_like($table, $cells, $words, $logic) {
  74.             $words = strlen($words) ? explode(' ', trim($words)) : [];
  75.             $logic = in_array($logic, ['OR', 'AND']) ? $logic : 'OR';
  76.             $like_expr = [];
  77.             $cells_expr = [];
  78.            
  79.             if (count($words) > 0) {
  80.                 for ($i = 0; $i < count($words); $i++) {
  81.                     $word = trim($words[$i]);
  82.                     if ((strlen($word) > 0) && (count($cells) > 0) && (strlen($table) > 0)) {
  83.                         foreach ($cells as $cell) {
  84.                             $like_expr[$i][] = "`$table`.`$cell` LIKE '%$word%'";
  85.                         }
  86.                         $cells_expr[] = '(' . implode(' OR ', $like_expr[$i]) . ')';
  87.                     }
  88.                 }
  89.                 $sql_like_str = '(' . implode(" $logic ", $cells_expr) . ')';
  90.             } else {
  91.                 $sql_like_str = '';
  92.             }
  93.         return $sql_like_str;
  94.     }
  95.    
  96.     function prepare_sql_match($table, $cells, $words, $logic) {
  97.             $words = strlen($words) ? explode(' ', trim($words)) : [];
  98.             $logic = in_array($logic, ['OR', 'AND']) ? $logic : 'OR';
  99.             $match_expr = [];
  100.             $cells_expr = [];
  101.            
  102.             if (count($words) > 0) {
  103.                 for ($i = 0; $i < count($words); $i++) {
  104.                     $word = trim($words[$i]);
  105.                     if ((strlen($word) > 0) && (count($cells) > 0) && (strlen($table) > 0)) {
  106.                         foreach ($cells as $cell) {
  107.                             $match_expr[$i][] = "MATCH `$table`.`$cell` AGAINST ('\"$word\"')";
  108.                         }
  109.                         $cells_expr[] = '(' . implode(' OR ', $match_expr[$i]) . ')';
  110.                     }
  111.                 }
  112.                 $sql_like_str = '(' . implode(" $logic ", $cells_expr) . ')';
  113.             } else {
  114.                 $sql_like_str = '';
  115.             }
  116.         return $sql_like_str;
  117.     }
  118.        
  119.     function create_sql_query($q, $words_in, $words_logic, $search_logic, $category, $tags, $tags_logic, $limit) {
  120.         $taxonomy = create_sql_taxonomy('ex_', 'term_relationships', 'term_taxonomy', 'terms',
  121.             array(
  122.                 [$tags_logic, 'post_tag', $tags],
  123.                 ['AND', 'category', $category]
  124.                 )
  125.             );
  126.         $where = [];
  127.         if ($words_in == 'all') {
  128.             strlen($q) ? array_push($where, prepare_sql_like('p', ['post_title'], $q, $words_logic)) : '';
  129.             strlen($q) ? array_push($where, prepare_sql_match('p', ['post_content'], $q, $words_logic)) : '';
  130.         } else if ($words_in == 'title') {
  131.             strlen($q) ? array_push($where, prepare_sql_like('p', ['post_title'], $q, $words_logic)) : '';
  132.         } else if ($words_in == 'content') {
  133.             strlen($q) ? array_push($where, prepare_sql_match('p', ['post_content'], $q, $words_logic)) : '';
  134.         }
  135.        
  136.         $where = implode(" $search_logic\r\n\t", $where);
  137.         if (strlen($where)) $where = "WHERE (\r\n\t" . $where . "\r\n\t)";
  138.        
  139.         $query = "SELECT \r\n\tSQL_CALC_FOUND_ROWS\r\n\t`p`.`id`\r\nFROM `ex_posts` AS `p`\r\n$taxonomy$where \r\nGROUP BY `p`.`id` LIMIT $limit";
  140.        
  141.         return $query;
  142.     }
  143.  
  144.    
  145.     echo create_sql_query('Ключевые слова для поиска', 'all', 'AND', 'OR', 'programming', 'php,mysql,delphi', 'AND', '0, 10');
  146. ?>
Advertisement
Add Comment
Please, Sign In to add comment