Clean code

This commit is contained in:
Laurent Destailleur 2024-01-15 11:28:25 +01:00
parent abb4bed44e
commit 3ac7706a63
4 changed files with 28 additions and 14 deletions

View File

@ -218,8 +218,6 @@ class Members extends DolibarrApi
*/
public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $typeid = '', $category = 0, $sqlfilters = '', $properties = '')
{
global $db, $conf;
$obj_ret = array();
if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {

View File

@ -370,6 +370,7 @@ function convertDurationtoHour($duration_value, $duration_unit)
* @param mixed $gm False or 0 or 'tzserver' = Input date fields are date info in the server TZ. True or 1 or 'gmt' = Input are date info in GMT TZ.
* Note: In database, dates are always for the server TZ.
* @return string $sqldate String with SQL filter
* @see forgeSQLFromUniversalSearchCriteria()
*/
function dolSqlDateFilter($datefield, $day_date, $month_date, $year_date, $excludefirstand = 0, $gm = false)
{

View File

@ -12439,6 +12439,7 @@ function jsonOrUnserialize($stringtodecode)
* @param int $nopar 1=Do not add the perenthesis around the condition string.
* @param int $noerror 1=If search criteria is not valid, does not return an error string but invalidate the SQL
* @return string Return forged SQL string
* @see dolSqlDateFilter()
*/
function forgeSQLFromUniversalSearchCriteria($filter, &$errorstr = '', $noand = 0, $nopar = 0, $noerror = 0)
{

View File

@ -432,17 +432,20 @@ class MyObject extends CommonObject
/**
* Load list of objects in memory from the database. Using a fetchAll is a bad practice, instead try to forge you optimized and limited SQL request.
* Load list of objects in memory from the database.
* Using a fetchAll() with limit = 0 is a very bad practice. Instead try to forge yourself an optimized SQL request with
* your own loop with start and stop pagination.
*
* @param string $sortorder Sort Order
* @param string $sortfield Sort field
* @param int $limit limit
* @param int $offset Offset
* @param array $filter Filter array. Example array('mystringfield'=>'value', 'myintfield'=>4, 'customsql'=>...)
* @param string $filtermode Filter mode (AND or OR)
* @return array|int int <0 if KO, array of pages if OK
* @param string $sortorder Sort Order
* @param string $sortfield Sort field
* @param int $limit Limit the number of lines returned
* @param int $offset Offset
* @param string $filter Filter as an Universal Search string.
* Example: '((client:=:1) OR ((client:>=:2) AND (client:<=:3))) AND (client:!=:8) AND (nom:like:'a%')'
* @param string $filtermode Filter mode (AND or OR)
* @return array|int int <0 if KO, array of pages if OK
*/
public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
public function fetchAll($sortorder = '', $sortfield = '', $limit = 1000, $offset = 0, array $filter = array(), $filtermode = 'AND')
{
dol_syslog(__METHOD__, LOG_DEBUG);
@ -460,12 +463,14 @@ class MyObject extends CommonObject
$sql .= " WHERE 1 = 1";
}
// Manage filter
/* We keep this part of code that is still used by a lot of old class. The 'else" shows how to switch to Universal Search filters
$sqlwhere = array();
if (count($filter) > 0) {
if (is_array($filter) && count($filter) > 0) {
dol_syslog("Warning: Use of an array as filter is now forbidden and deprecated. Use an universal SQL filter string instead", LOG_WARNING);
foreach ($filter as $key => $value) {
$columnName = preg_replace('/^t\./', '', $key);
if ($key === 'customsql') {
// Never use 'customsql' with a value from user input since it is injected as is. The value must be hard coded.
// Never use 'customsql' with a value from a user input since it is injected as is. The value must be hard coded.
$sqlwhere[] = $value;
continue;
} elseif (isset($this->fields[$columnName])) {
@ -501,10 +506,19 @@ class MyObject extends CommonObject
}
}
}
} else { */
$errormessage = '';
$sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
if ($errormessage) {
$this->errors[] = $errormessage;
dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
return -1;
}
/*}
if (count($sqlwhere) > 0) {
$sql .= " AND (".implode(" ".$filtermode." ", $sqlwhere).")";
}
}*/
if (!empty($sortfield)) {
$sql .= $this->db->order($sortfield, $sortorder);