Browse Source

Domains and Mailboxes datatable - server side processing - filtering by tags

Signed-off-by: Kristian Feldsam <feldsam@gmail.com>
Kristian Feldsam 1 year ago
parent
commit
ac4f131fa8
3 changed files with 36 additions and 13 deletions
  1. 32 12
      data/web/inc/lib/ssp.class.php
  2. 2 1
      data/web/js/site/mailbox.js
  3. 2 0
      data/web/json_api.php

+ 32 - 12
data/web/inc/lib/ssp.class.php

@@ -177,6 +177,7 @@ class SSP {
 	{
 	{
 		$globalSearch = array();
 		$globalSearch = array();
 		$columnSearch = array();
 		$columnSearch = array();
+		$joins = array();
 		$dtColumns = self::pluck( $columns, 'dt' );
 		$dtColumns = self::pluck( $columns, 'dt' );
 
 
 		if ( isset($request['search']) && $request['search']['value'] != '' ) {
 		if ( isset($request['search']) && $request['search']['value'] != '' ) {
@@ -184,13 +185,19 @@ class SSP {
 
 
 			for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
 			for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
 				$requestColumn = $request['columns'][$i];
 				$requestColumn = $request['columns'][$i];
-				$columnIdx = array_search( $requestColumn['data'], $dtColumns );
+				$columnIdx = array_search( $i, $dtColumns );
 				$column = $columns[ $columnIdx ];
 				$column = $columns[ $columnIdx ];
 
 
 				if ( $requestColumn['searchable'] == 'true' ) {
 				if ( $requestColumn['searchable'] == 'true' ) {
 					if(!empty($column['db'])){
 					if(!empty($column['db'])){
-						$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
-						$globalSearch[] = "`".$tablesAS."`.`".$column['db']."` LIKE ".$binding;
+    					$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
+    					
+    					if(isset($column['search']['join'])) {
+            				$joins[] = $column['search']['join'];
+            				$globalSearch[] = $column['search']['where_column'].' LIKE '.$binding;
+        				} else {
+						    $globalSearch[] = "`".$tablesAS."`.`".$column['db']."` LIKE ".$binding;
+						}
 					}
 					}
 				}
 				}
 			}
 			}
@@ -227,12 +234,17 @@ class SSP {
 				implode(' AND ', $columnSearch) :
 				implode(' AND ', $columnSearch) :
 				$where .' AND '. implode(' AND ', $columnSearch);
 				$where .' AND '. implode(' AND ', $columnSearch);
 		}
 		}
+		
+		$join = '';
+		if( count($joins) ) {
+    		$join = implode(' ', $joins);
+		}
 
 
 		if ( $where !== '' ) {
 		if ( $where !== '' ) {
 			$where = 'WHERE '.$where;
 			$where = 'WHERE '.$where;
 		}
 		}
 
 
-		return $where;
+		return [$join, $where];
 	}
 	}
 
 
 
 
@@ -270,13 +282,14 @@ class SSP {
 		// Build the SQL query string from the request
 		// Build the SQL query string from the request
 		list($select, $order) = self::order( $tablesAS, $request, $columns );
 		list($select, $order) = self::order( $tablesAS, $request, $columns );
 		$limit = self::limit( $request, $columns );
 		$limit = self::limit( $request, $columns );
-		$where = self::filter( $tablesAS, $request, $columns, $bindings );
+		list($join, $where) = self::filter( $tablesAS, $request, $columns, $bindings );
 
 
 		// Main query to actually get the data
 		// Main query to actually get the data
 		$data = self::sql_exec( $db, $bindings,
 		$data = self::sql_exec( $db, $bindings,
 			"SELECT `$tablesAS`.`".implode("`, `$tablesAS`.`", self::pluck($columns, 'db'))."`
 			"SELECT `$tablesAS`.`".implode("`, `$tablesAS`.`", self::pluck($columns, 'db'))."`
 			 $select
 			 $select
 			 FROM `$table` AS `$tablesAS`
 			 FROM `$table` AS `$tablesAS`
+			 $join
 			 $where
 			 $where
 			 $order
 			 $order
 			 $limit"
 			 $limit"
@@ -284,15 +297,16 @@ class SSP {
 
 
 		// Data set length after filtering
 		// Data set length after filtering
 		$resFilterLength = self::sql_exec( $db, $bindings,
 		$resFilterLength = self::sql_exec( $db, $bindings,
-			"SELECT COUNT(`{$primaryKey}`)
+			"SELECT COUNT(`{$tablesAS}`.`{$primaryKey}`)
 			 FROM   `$table` AS `$tablesAS`
 			 FROM   `$table` AS `$tablesAS`
+			 $join
 			 $where"
 			 $where"
 		);
 		);
 		$recordsFiltered = $resFilterLength[0][0];
 		$recordsFiltered = $resFilterLength[0][0];
 
 
 		// Total data set length
 		// Total data set length
 		$resTotalLength = self::sql_exec( $db,
 		$resTotalLength = self::sql_exec( $db,
-			"SELECT COUNT(`{$primaryKey}`)
+			"SELECT COUNT(`{$tablesAS}`.`{$primaryKey}`)
 			 FROM   `$table` AS `$tablesAS`"
 			 FROM   `$table` AS `$tablesAS`"
 		);
 		);
 		$recordsTotal = $resTotalLength[0][0];
 		$recordsTotal = $resTotalLength[0][0];
@@ -362,7 +376,7 @@ class SSP {
 		// Build the SQL query string from the request
 		// Build the SQL query string from the request
 		list($select, $order) = self::order( $tablesAS, $request, $columns );
 		list($select, $order) = self::order( $tablesAS, $request, $columns );
 		$limit = self::limit( $request, $columns );
 		$limit = self::limit( $request, $columns );
-		$where = self::filter( $tablesAS, $request, $columns, $bindings );
+		list($join_filter, $where) = self::filter( $tablesAS, $request, $columns, $bindings );
 
 
 		// whereResult can be a simple string, or an assoc. array with a
 		// whereResult can be a simple string, or an assoc. array with a
 		// condition and bindings
 		// condition and bindings
@@ -388,7 +402,9 @@ class SSP {
 			 $select
 			 $select
 			 FROM `$table` AS `$tablesAS`
 			 FROM `$table` AS `$tablesAS`
 			 $join
 			 $join
+			 $join_filter
 			 $where
 			 $where
+			 GROUP BY `{$tablesAS}`.`{$primaryKey}`
 			 $order
 			 $order
 			 $limit"
 			 $limit"
 		);
 		);
@@ -398,18 +414,22 @@ class SSP {
 			"SELECT COUNT(`{$tablesAS}`.`{$primaryKey}`)
 			"SELECT COUNT(`{$tablesAS}`.`{$primaryKey}`)
 			 FROM   `$table` AS `$tablesAS`
 			 FROM   `$table` AS `$tablesAS`
 			 $join
 			 $join
-			 $where"
+			 $join_filter
+			 $where
+			 GROUP BY `{$tablesAS}`.`{$primaryKey}`"
 		);
 		);
-		$recordsFiltered = $resFilterLength[0][0];
+		$recordsFiltered = (isset($resFilterLength[0])) ? $resFilterLength[0][0] : 0;
 
 
 		// Total data set length
 		// Total data set length
 		$resTotalLength = self::sql_exec( $db, $bindings,
 		$resTotalLength = self::sql_exec( $db, $bindings,
 			"SELECT COUNT(`{$tablesAS}`.`{$primaryKey}`)
 			"SELECT COUNT(`{$tablesAS}`.`{$primaryKey}`)
 			 FROM   `$table` AS `$tablesAS`
 			 FROM   `$table` AS `$tablesAS`
       $join
       $join
-      $where"
+      $join_filter
+      $where
+      GROUP BY `{$tablesAS}`.`{$primaryKey}`"
 		);
 		);
-		$recordsTotal = $resTotalLength[0][0];
+		$recordsTotal = (isset($resTotalLength[0])) ? $resTotalLength[0][0] : 0;
 
 
 		/*
 		/*
 		 * Output
 		 * Output

+ 2 - 1
data/web/js/site/mailbox.js

@@ -613,7 +613,7 @@ jQuery(function($){
         {
         {
           title: 'Tags',
           title: 'Tags',
           data: 'tags',
           data: 'tags',
-          searchable: false,
+          searchable: true,
           orderable: false,
           orderable: false,
           defaultContent: '',
           defaultContent: '',
           className: 'none'
           className: 'none'
@@ -1107,6 +1107,7 @@ jQuery(function($){
         {
         {
           title: 'Tags',
           title: 'Tags',
           data: 'tags',
           data: 'tags',
+          searchable: true,
           defaultContent: '',
           defaultContent: '',
           className: 'none'
           className: 'none'
         },
         },

+ 2 - 0
data/web/json_api.php

@@ -535,6 +535,7 @@ if (isset($_GET['query'])) {
                   ['db' => 'defquota', 'dt' => 7],
                   ['db' => 'defquota', 'dt' => 7],
                   ['db' => 'maxquota', 'dt' => 8],
                   ['db' => 'maxquota', 'dt' => 8],
                   ['db' => 'backupmx', 'dt' => 10],
                   ['db' => 'backupmx', 'dt' => 10],
+                  ['db' => 'tags', 'dt' => 14, 'dummy' => true, 'search' => ['join' => 'LEFT JOIN `tags_domain` AS `td` ON `td`.`domain` = `d`.`domain`', 'where_column' => '`td`.`tag_name`']],
                   ['db' => 'active', 'dt' => 15],
                   ['db' => 'active', 'dt' => 15],
                 ];
                 ];
 
 
@@ -1061,6 +1062,7 @@ if (isset($_GET['query'])) {
                   ['db' => 'last_pw_change', 'dt' => 5, 'dummy' => true, 'order_subquery' => "JSON_EXTRACT(attributes, '$.passwd_update')"],
                   ['db' => 'last_pw_change', 'dt' => 5, 'dummy' => true, 'order_subquery' => "JSON_EXTRACT(attributes, '$.passwd_update')"],
                   ['db' => 'in_use', 'dt' => 6, 'dummy' => true, 'order_subquery' => "(SELECT SUM(bytes) FROM `quota2` WHERE `quota2`.`username` = `m`.`username`) / `m`.`quota`"],
                   ['db' => 'in_use', 'dt' => 6, 'dummy' => true, 'order_subquery' => "(SELECT SUM(bytes) FROM `quota2` WHERE `quota2`.`username` = `m`.`username`) / `m`.`quota`"],
                   ['db' => 'messages', 'dt' => 17, 'dummy' => true, 'order_subquery' => "SELECT SUM(messages) FROM `quota2` WHERE `quota2`.`username` = `m`.`username`"],
                   ['db' => 'messages', 'dt' => 17, 'dummy' => true, 'order_subquery' => "SELECT SUM(messages) FROM `quota2` WHERE `quota2`.`username` = `m`.`username`"],
+                  ['db' => 'tags', 'dt' => 20, 'dummy' => true, 'search' => ['join' => 'LEFT JOIN `tags_mailbox` AS `tm` ON `tm`.`username` = `m`.`username`', 'where_column' => '`tm`.`tag_name`']],
                   ['db' => 'active', 'dt' => 21]
                   ['db' => 'active', 'dt' => 21]
                 ];
                 ];