Max F 5 lat temu
rodzic
commit
9129311d40

+ 10 - 0
src/Database/Sql/SqlDb.php

@@ -138,6 +138,16 @@ class SqlDb
 		return $this->createQuery()->executeSelectAll($sql, $params);
 	}
 
+	public function executeSelectAllWithFoundRows($query, $params=[])
+	{
+		return $this->createQuery()->executeSelectAllWithFoundRows($query, $params);
+	}
+
+	public function executeSelectAllPagination($query, $nb_per_page=10, $page_idx=1, $params=[])
+	{
+		return $this->createQuery()->executeSelectAllPagination($query, $nb_per_page, $page_idx, $params);
+	}
+
 	public function executeSelectOne($sql, $params=[])
 	{
 		return $this->createQuery()->executeSelectOne($sql, $params);

+ 46 - 0
src/Database/Sql/SqlQuery.php

@@ -193,6 +193,52 @@ class SqlQuery
 	}
 
 
+	public function executeSelectAllWithFoundRows($query, $params=[])
+	{
+		if (strpos($query, 'SQL_CALC_FOUND_ROWS') === false) {
+			$query = str_replace("select ", "select SQL_CALC_FOUND_ROWS ", $query);
+		}
+
+		$rs = $this->execute($query, $params);
+		$data = $rs->fetchAll();
+		$found_rows = $rs->getfoundRowsCount();
+
+		return ['found_rows' => $found_rows, 'data' => $data];
+	}
+
+
+	public function executeSelectAllPagination($query, $nb_per_page=10, $page_idx=1, $params=[])
+	{
+		if (true) {
+			$page_idx = max(1, intval($page_idx));
+			$nb_per_page = max(1, intval($nb_per_page));
+
+			$offset = ($page_idx - 1) * $nb_per_page;
+			$limit = $offset . ', ' . $nb_per_page;
+
+			$query .= " limit " . $limit;
+		}
+
+		$result = $this->executeSelectAllWithFoundRows($query, $params);
+		$found_rows = $result['found_rows'];
+		$data = $result['data'];
+
+		$pagination = [
+			'page' => $page_idx,
+			'limit' => $nb_per_page,
+			'offset' => $offset,
+			'page_rows' => count($data),
+			'total_rows' => $found_rows,
+			'nb_pages' => empty($nb_per_page) ? null : ceil($found_rows / $nb_per_page),
+		];
+
+		return [
+			'pagination' => $pagination,
+			'data' => $data,
+		];
+	}
+
+
 	public function executeInsert($query, $params=[])
 	{
 		$this->execute($query, $params);

+ 17 - 5
src/Database/Sql/SqlTools.php

@@ -215,7 +215,7 @@ class SqlTools
 
 
 
-    public function buildSqlWhereSearch($q='', $search_fields=[], $min_length=1)
+    public function buildSqlWhereSearch($q='', $search_fields=[], $min_str_length=1, $max_words=10)
     {
         $db = $this->db;
         
@@ -224,27 +224,39 @@ class SqlTools
 
         $q = trim($q);
         
-        if ($search_fields && strlen($q) >= $min_length) {
+        if ($search_fields && strlen($q) >= $min_str_length) {
             $words = explode(" ", $q);
 
             foreach ($words as $word_idx => $word) {
+                $word_idx_score = 100 * max(1, 10 - $word_idx); // au dela de 10 mots, on compte comme le 10e mot
+
                 $w = $db->escape($word);
                 $w2 = $db->escape("%" . $word . "%");
 
                 $conditions_or = [];
-                foreach ($search_fields as $field) {
+                $select_sums = [0];
+                foreach ($search_fields as $term_idx => $field) {
                     $conditions_or[] = $field . " like " . $w2;
+
+                    $term_idx_score = 10 * max(1, 10 - $term_idx); // au dela de 10 fields, on compte comme le 10e field
+                    $select_sums[] = "( if( locate(" . $w . ", " . $field . ") > 0, 1, 0 ) * " . $word_idx_score . " * " . $term_idx_score . " * greatest( 100 - locate(" . $w . ", " . $field . "), 1) )";
                 }
 
                 $word_condition = "(" . implode(" or ", $conditions_or) . ")";
                 $search_where .= " or " . $word_condition;
 
-                $word_score = max(1, 10 - $word_idx);
-                $select_sum .= " + if(" . $word_condition . ", " . $word_score . ", 0)";
+
+                //$select_sum .= " + if(" . $word_condition . ", " . $word_idx_score . ", 0)";
+                $select_sum .= " + (" . implode(" + ", $select_sums) . ")";
+
+                if (! empty($max_words) && $word_idx >= $max_words) {
+                    break; // one ne prend plus en compte les mots au dela de $max_words
+                }
             }
         }
         $search_where .= ")";
         $select_sum .= ")";
+        //pre($select_sum, 1);
 
         return [
             'select' => $select_sum,