Skip to content

Commit

Permalink
Merge branch '20.0' of [email protected]:Dolibarr/dolibarr.git into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Sep 12, 2024
2 parents b428325 + e735d17 commit e4227c6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 35 deletions.
39 changes: 37 additions & 2 deletions htdocs/contact/class/contact.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,10 +971,11 @@ public function update_perso($id, $user = null, $notrigger = 0)
* @param ?User $user Load also alerts of this user (subscribing to alerts) that want alerts about this contact
* @param string $ref_ext External reference, not given by Dolibarr
* @param string $email Email
* @param int $loadalsoroles Load also roles. Try to always 0 here and load roles with a separate call of fetchRoles().
* @param int $loadalsoroles Load also roles. Try to always use 0 here and load roles with a separate call of fetchRoles().
* @param int $socid Filter on thirdparty id
* @return int >0 if OK, <0 if KO or if two records found for same ref or idprof, 0 if not found.
*/
public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0)
public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0, $socid = 0)
{
global $langs;

Expand Down Expand Up @@ -1019,6 +1020,9 @@ public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoro
if ($email) {
$sql .= " AND c.email = '".$this->db->escape($email)."'";
}
if ($socid) {
$sql .= " AND c.fk_soc = ".((int) $socid);
}
}

$resql = $this->db->query($sql);
Expand Down Expand Up @@ -1158,6 +1162,37 @@ public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoro
}
}

/**
* Search the contact that match the most the provided parameters.
* Searching rules try to find the existing contact.
*
* @param int $id Id of contact
* @param string $lastname Lastname (TODO Not yet implemented)
* @param string $firstname Firstname (TODO Not yet implemented)
* @param string $ref_ext External reference, not given by Dolibarr
* @param string $email Email
* @param string $ref_alias Name alias (TODO Not yet implemented)
* @param int $socid Filter on thirdparty id
* @return int >0 if OK, <0 if KO or if two records found for same ref or idprof, 0 if not found.
*/
public function findNearest($id = 0, $lastname = '', $firstname = '', $ref_ext = '', $email = '', $ref_alias = '', $socid = 0)
{
// A rowid is known, it is a unique key so we found it
if ($id) {
return $id;
}

// We try to find the contact with exact matching on all fields
// TODO Replace this with step by step search
// Then search on email
// Then search on lastname + firstname
// Then search ref_ext or alias with a OR
$tmpcontact = new Contact($this->db);
$result = $tmpcontact->fetch($id, null, $ref_ext, $email, 0, $socid);

return $result;
}



/**
Expand Down
8 changes: 4 additions & 4 deletions htdocs/emailcollector/class/emailcollector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2565,7 +2565,7 @@ public function doCollectOneCollector($mode = 0)

if (!$errorforactions && ($idtouseforthirdparty || $emailtouseforthirdparty || $nametouseforthirdparty || $namealiastouseforthirdparty)) {
// We make another search on thirdparty
$operationslog .= '<br>We have this data to search thirdparty: id='.$idtouseforthirdparty.', email='.$emailtouseforthirdparty.', name='.$nametouseforthirdparty.', name_alias='.$namealiastouseforthirdparty;
$operationslog .= '<br>We have this initial main data to search thirdparty: id='.$idtouseforthirdparty.', email='.$emailtouseforthirdparty.', name='.$nametouseforthirdparty.', name_alias='.$namealiastouseforthirdparty.'.';

$tmpobject = new stdClass();
$tmpobject->element = 'generic';
Expand All @@ -2581,7 +2581,7 @@ public function doCollectOneCollector($mode = 0)
$namealiastouseforthirdparty = $tmpobject->name_alias;
$emailtouseforthirdparty = $tmpobject->email;

$operationslog .= '<br>We try to search existing thirdparty with '.$idtouseforthirdparty.' '.$emailtouseforthirdparty.' '.$nametouseforthirdparty.' '.$namealiastouseforthirdparty;
$operationslog .= '<br>We try to search existing thirdparty with idtouseforthirdparty='.$idtouseforthirdparty.' emailtouseforthirdparty='.$emailtouseforthirdparty.' nametouseforthirdparty='.$nametouseforthirdparty.' namealiastouseforthirdparty='.$namealiastouseforthirdparty;

// Try to find the thirdparty that match the most the information we have
$result = $thirdpartystatic->findNearest($idtouseforthirdparty, $nametouseforthirdparty, '', '', '', '', '', '', '', '', $emailtouseforthirdparty, $namealiastouseforthirdparty);
Expand All @@ -2595,8 +2595,8 @@ public function doCollectOneCollector($mode = 0)
if ($operation['type'] == 'loadthirdparty') {
dol_syslog("Third party with id=".$idtouseforthirdparty." email=".$emailtouseforthirdparty." name=".$nametouseforthirdparty." name_alias=".$namealiastouseforthirdparty." was not found");

//search into contacts of thirdparty
$resultContact = $contactstatic->fetch(0, '', '', $emailtouseforthirdparty);
// Search into contacts of thirdparties to try to guess the thirdparty to use
$resultContact = $contactstatic->findNearest(0, '', '', '', $emailtouseforthirdparty, '', 0);
if ($resultContact > 0) {
$idtouseforthirdparty = $contactstatic->socid;
$result = $thirdpartystatic->fetch($idtouseforthirdparty);
Expand Down
2 changes: 1 addition & 1 deletion htdocs/langs/en_US/mails.lang
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ Information=Information
ContactsWithThirdpartyFilter=Contacts with third-party filter
Unanswered=Unanswered
Answered=Answered
IsNotAnAnswer=Is not answer (initial email)
IsNotAnAnswer=Is not answer (Is an initial email)
IsAnAnswer=Is an answer of an initial email
RecordCreatedByEmailCollector=Record created by the Email Collector %s
DefaultBlacklistMailingStatus=Default value for field '%s' when creating a new contact
Expand Down
2 changes: 1 addition & 1 deletion htdocs/societe/class/societe.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2172,7 +2172,7 @@ public function findNearest($rowid = 0, $ref = '', $ref_ext = '', $barcode = '',
// TODO Replace this with step by step search
// Then search on barcode if we have it (+ restriction on is_client and is_supplier)
// Then search on profids with a OR (+ restriction on is_client and is_supplier)
// Then search on email with a OR (+ restriction on is_client and is_supplier)
// Then search on email (+ restriction on is_client and is_supplier)
// Then search ref, ref_ext or alias with a OR (+ restriction on is_client and is_supplier)
$tmpthirdparty = new Societe($this->db);
$result = $tmpthirdparty->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias, $is_client, $is_supplier);
Expand Down
76 changes: 49 additions & 27 deletions htdocs/website/class/websitepage.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,13 @@ public function fetchAll($websiteid = '', $sortorder = '', $sortfield = '', $lim
/**
* Count objects in the database.
*
* @param string $websiteid Web site
* @param array $filter Filter array
* @param string $filtermode Filter mode (AND or OR)
* @return int int <0 if KO, array of pages if OK
* @param string $websiteid Web site
* @param string|array $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 int int <0 if KO, array of pages if OK
*/
public function countAll($websiteid, array $filter = array(), $filtermode = 'AND')
public function countAll($websiteid, string $filter = '', $filtermode = 'AND')
{
dol_syslog(__METHOD__, LOG_DEBUG);

Expand All @@ -519,34 +520,55 @@ public function countAll($websiteid, array $filter = array(), $filtermode = 'AND
$sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
$sql .= ' WHERE t.fk_website = '.((int) $websiteid);

// Manage filter (same than into fetchAll)
$sqlwhere = array();
if (count($filter) > 0) {
foreach ($filter as $key => $value) {
if ($key == 't.rowid' || $key == 't.fk_website' || $key == 'status') {
$sqlwhere[] = $key." = ".((int) $value);
} elseif ($key == 'type_container') {
$sqlwhere[] = $key." = '".$this->db->escape($value)."'";
} elseif ($key == 'lang' || $key == 't.lang') {
$listoflang = array();
$foundnull = 0;
foreach (explode(',', $value) as $tmpvalue) {
if ($tmpvalue == 'null') {
$foundnull++;
continue;
// Deprecated. If we receive an array, we use it. Prefer using the USF syntax.
if (is_array($filter)) {
$sqlwhere = array();

if (count($filter) > 0) {
foreach ($filter as $key => $value) {
if ($key == 't.rowid' || $key == 'rowid' || $key == 't.fk_website' || $key == 'fk_website' || $key == 'status' || $key == 't.status') {
$sqlwhere[] = $key." = ".((int) $value);
} elseif ($key == 'type_container' || $key == 't.type_container') {
$sqlwhere[] = $key." = '".$this->db->escape($value)."'";
} elseif ($key == 'lang' || $key == 't.lang') {
$listoflang = array();
$foundnull = 0;
foreach (explode(',', $value) as $tmpvalue) {
if ($tmpvalue == 'null') {
$foundnull++;
continue;
}
$listoflang[] = "'".$this->db->escape(substr(str_replace("'", '', $tmpvalue), 0, 2))."'";
}
$listoflang[] = "'".$this->db->escape(substr(str_replace("'", '', $tmpvalue), 0, 2))."'";
}
$stringtouse = $key." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
if ($foundnull) {
$stringtouse = "(".$stringtouse." OR ".$key." IS NULL)";
$stringtouse = $this->db->sanitize($key)." IN (".$this->db->sanitize(implode(',', $listoflang), 1).")";
if ($foundnull) {
$stringtouse = "(".$stringtouse." OR ".$this->db->sanitize($key)." IS NULL)";
}
$sqlwhere[] = $stringtouse;
} else {
$sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
}
$sqlwhere[] = $stringtouse;
}
}
if (count($sqlwhere) > 0) {
if (!empty($websiteid)) {
$sql .= " AND (".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
} else {
$sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
$sql .= " WHERE ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
}
}

$filter = '';
}

$errormessage = '';
$sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
if ($errormessage) {
$this->errors[] = $errormessage;
dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
return -1;
}

if (count($sqlwhere) > 0) {
$sql .= ' AND ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
}
Expand Down

0 comments on commit e4227c6

Please sign in to comment.