2016-04-01 10:52:19 +02:00
< ? php
/* Advance Targeting Emailling for mass emailing module
* Copyright ( C ) 2013 Florian Henry < florian . henry @ open - concept . pro >
2020-11-27 00:25:45 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
2016-04-01 10:52:19 +02:00
/**
2016-05-06 21:28:06 +02:00
* \file comm / mailing / class / advtargetemailing . class . php
* \ingroup mailing
2016-04-01 10:52:19 +02:00
* \brief This file is an example CRUD class file ( Create / Read / Update / Delete )
*/
/**
2016-05-06 21:28:06 +02:00
* Class to manage advanced emailing target selector
2016-04-01 10:52:19 +02:00
*/
class AdvanceTargetingMailing extends CommonObject
{
2020-10-31 14:32:18 +01:00
/**
* @ var DoliDB Database handler .
*/
public $db ;
2016-04-01 10:52:19 +02:00
2018-08-22 11:48:26 +02:00
/**
* @ var string ID to identify managed object
*/
2019-11-13 19:35:39 +01:00
public $element = 'advtargetemailing' ;
2018-08-28 09:34:53 +02:00
2018-08-22 12:04:50 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
2022-07-24 18:27:15 +02:00
public $table_element = 'mailing_advtarget' ;
2016-04-01 17:09:03 +02:00
2018-08-22 11:24:31 +02:00
/**
* @ var int ID
*/
public $id ;
2016-04-01 17:09:03 +02:00
2020-11-27 00:23:03 +01:00
/**
* @ var string name
*/
2020-10-31 14:32:18 +01:00
public $name ;
2020-11-27 00:23:03 +01:00
/**
2020-12-05 23:53:55 +01:00
* @ var int element id ( it ' s not the name of a field )
2020-11-27 00:23:03 +01:00
*/
2020-10-31 14:32:18 +01:00
public $fk_element ;
2020-11-27 00:23:03 +01:00
/**
* @ var string type element
*/
2020-10-31 14:32:18 +01:00
public $type_element ;
2020-11-27 00:23:03 +01:00
/**
* @ var string filter value
*/
2020-10-31 14:32:18 +01:00
public $filtervalue ;
2020-11-27 00:23:03 +01:00
/**
* @ var int user author id
*/
2020-10-31 14:32:18 +01:00
public $fk_user_author ;
2020-11-27 00:23:03 +01:00
/**
* @ var int | string datec
*/
2020-10-31 14:32:18 +01:00
public $datec = '' ;
2020-11-27 00:23:03 +01:00
/**
* @ var int user mod id
*/
2020-10-31 14:32:18 +01:00
public $fk_user_mod ;
2020-11-27 00:23:03 +01:00
/**
* @ var int | string tms
*/
2020-10-31 14:32:18 +01:00
public $tms = '' ;
2019-02-27 20:45:07 +01:00
2020-11-27 00:23:03 +01:00
/**
* @ var array select target type
*/
2020-10-31 14:32:18 +01:00
public $select_target_type = array ();
2020-11-27 00:23:03 +01:00
/**
* @ var array type status comm prospect
*/
2020-10-31 14:32:18 +01:00
public $type_statuscommprospect = array ();
public $thirdparty_lines ;
public $contact_lines ;
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
2018-08-29 18:45:36 +02:00
* Constructor
2016-04-01 10:52:19 +02:00
*
2018-08-29 18:45:36 +02:00
* @ param DoliDb $db Database handler
2016-04-01 10:52:19 +02:00
*/
2020-10-31 14:32:18 +01:00
public function __construct ( $db )
2016-04-01 10:52:19 +02:00
{
global $langs ;
$langs -> load ( 'customers' );
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$this -> db = $db ;
2016-04-01 17:09:03 +02:00
2016-12-09 19:06:59 +01:00
$this -> select_target_type = array (
2020-11-27 00:25:45 +01:00
'2' => $langs -> trans ( 'Contacts' ),
'1' => $langs -> trans ( 'Contacts' ) . '+' . $langs -> trans ( 'ThirdParty' ),
'3' => $langs -> trans ( 'ThirdParty' ),
'4' => $langs -> trans ( 'ContactsWithThirdpartyFilter' )
2016-12-09 19:06:59 +01:00
);
2022-10-03 13:38:15 +02:00
require_once DOL_DOCUMENT_ROOT . '/societe/class/client.class.php' ;
$customerStatic = new Client ( $this -> db );
$customerStatic -> loadCacheOfProspStatus ();
if ( ! empty ( $customerStatic -> cacheprospectstatus )) {
foreach ( $customerStatic -> cacheprospectstatus as $dataProspectSt ) {
$this -> type_statuscommprospect [ $dataProspectSt [ 'id' ]] = $dataProspectSt [ 'label' ];
}
2022-10-03 13:40:33 +02:00
} else {
2022-10-03 13:41:07 +02:00
$this -> type_statuscommprospect = array (
- 1 => $langs -> trans ( " StatusProspect-1 " ),
0 => $langs -> trans ( " StatusProspect0 " ),
1 => $langs -> trans ( " StatusProspect1 " ),
2 => $langs -> trans ( " StatusProspect2 " ),
3 => $langs -> trans ( " StatusProspect3 " )
);
2022-10-03 13:38:15 +02:00
}
2016-04-01 10:52:19 +02:00
}
/**
* Create object into database
*
2021-02-23 18:59:19 +01:00
* @ param User $user User that creates
2016-04-01 10:52:19 +02:00
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int < 0 if KO , Id of created object if OK
*/
2020-10-31 14:32:18 +01:00
public function create ( $user , $notrigger = 0 )
2016-04-01 10:52:19 +02:00
{
global $conf , $langs ;
2019-11-13 19:35:39 +01:00
$error = 0 ;
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
// Clean parameters
2020-11-27 00:25:45 +01:00
if ( isset ( $this -> fk_element )) {
$this -> fk_element = ( int ) $this -> fk_element ;
}
if ( isset ( $this -> type_element )) {
$this -> type_element = trim ( $this -> type_element );
}
2018-06-24 18:15:49 +02:00
2020-11-27 00:25:45 +01:00
if ( isset ( $this -> name )) {
$this -> name = trim ( $this -> name );
}
if ( isset ( $this -> filtervalue )) {
$this -> filtervalue = trim ( $this -> filtervalue );
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
// Check parameters
// Put here code to add control on parameters values
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
// Insert request
2022-07-24 18:27:15 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " mailing_advtarget( " ;
2019-11-13 19:35:39 +01:00
$sql .= " name, " ;
$sql .= " entity, " ;
$sql .= " fk_element, " ;
$sql .= " type_element, " ;
$sql .= " filtervalue, " ;
$sql .= " fk_user_author, " ;
$sql .= " datec, " ;
$sql .= " fk_user_mod " ;
$sql .= " ) VALUES ( " ;
$sql .= " " . ( ! isset ( $this -> name ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> name ) . " ' " ) . " , " ;
$sql .= " " . $conf -> entity . " , " ;
$sql .= " " . ( ! isset ( $this -> fk_element ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> fk_element ) . " ' " ) . " , " ;
$sql .= " " . ( ! isset ( $this -> type_element ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> type_element ) . " ' " ) . " , " ;
$sql .= " " . ( ! isset ( $this -> filtervalue ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> filtervalue ) . " ' " ) . " , " ;
$sql .= " " . $user -> id . " , " ;
$sql .= " ' " . $this -> db -> idate ( dol_now ()) . " ', " ;
2020-04-10 10:59:32 +02:00
$sql .= " " . $user -> id ;
2019-11-13 19:35:39 +01:00
$sql .= " ) " ;
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$this -> db -> begin ();
2016-04-01 17:09:03 +02:00
2021-08-27 22:42:04 +02:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2019-11-13 19:35:39 +01:00
$resql = $this -> db -> query ( $sql );
2020-11-27 00:25:45 +01:00
if ( ! $resql ) {
$error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror ();
}
2016-04-01 17:09:03 +02:00
2020-11-27 00:25:45 +01:00
if ( ! $error ) {
2022-07-24 18:27:15 +02:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " mailing_advtarget " );
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
// Commit or rollback
2020-11-27 00:25:45 +01:00
if ( $error ) {
foreach ( $this -> errors as $errmsg ) {
2016-04-01 10:52:19 +02:00
dol_syslog ( get_class ( $this ) . " ::create " . $errmsg , LOG_ERR );
2019-11-13 19:35:39 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2016-04-01 10:52:19 +02:00
}
$this -> db -> rollback ();
2019-11-13 19:35:39 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2016-04-01 10:52:19 +02:00
$this -> db -> commit ();
return $this -> id ;
}
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
* Load object in memory from the database
*
* @ param int $id Id object
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function fetch ( $id )
2016-04-01 10:52:19 +02:00
{
global $langs ;
$sql = " SELECT " ;
2019-11-13 19:35:39 +01:00
$sql .= " t.rowid, " ;
2016-04-01 17:09:03 +02:00
2019-11-13 19:35:39 +01:00
$sql .= " t.name, " ;
$sql .= " t.entity, " ;
$sql .= " t.fk_element, " ;
$sql .= " t.type_element, " ;
$sql .= " t.filtervalue, " ;
$sql .= " t.fk_user_author, " ;
$sql .= " t.datec, " ;
$sql .= " t.fk_user_mod, " ;
$sql .= " t.tms " ;
2016-04-01 10:52:19 +02:00
2022-07-24 18:27:15 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " mailing_advtarget as t " ;
2021-03-14 11:48:39 +01:00
$sql .= " WHERE t.rowid = " . (( int ) $id );
2016-04-01 17:09:03 +02:00
2021-08-27 22:42:04 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2019-11-13 19:35:39 +01:00
$resql = $this -> db -> query ( $sql );
2020-11-27 00:25:45 +01:00
if ( $resql ) {
if ( $this -> db -> num_rows ( $resql )) {
2016-04-01 10:52:19 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2016-04-01 17:09:03 +02:00
2019-11-13 19:35:39 +01:00
$this -> id = $obj -> rowid ;
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$this -> name = $obj -> name ;
$this -> entity = $obj -> entity ;
2018-06-22 10:54:00 +02:00
$this -> fk_element = $obj -> fk_element ;
$this -> type_element = $obj -> type_element ;
2016-04-01 10:52:19 +02:00
$this -> filtervalue = $obj -> filtervalue ;
$this -> fk_user_author = $obj -> fk_user_author ;
$this -> datec = $this -> db -> jdate ( $obj -> datec );
$this -> fk_user_mod = $obj -> fk_user_mod ;
$this -> tms = $this -> db -> jdate ( $obj -> tms );
}
$this -> db -> free ( $resql );
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:35:39 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2016-04-01 10:52:19 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " . $this -> error , LOG_ERR );
return - 1 ;
}
}
2016-04-01 17:09:03 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2016-04-01 10:52:19 +02:00
/**
* Load object in memory from the database
*
* @ param int $id Id object
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function fetch_by_mailing ( $id = 0 )
2016-04-01 10:52:19 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2016-04-01 10:52:19 +02:00
global $langs ;
$sql = " SELECT " ;
2019-11-13 19:35:39 +01:00
$sql .= " t.rowid, " ;
$sql .= " t.name, " ;
$sql .= " t.entity, " ;
$sql .= " t.fk_element, " ;
$sql .= " t.type_element, " ;
$sql .= " t.filtervalue, " ;
$sql .= " t.fk_user_author, " ;
$sql .= " t.datec, " ;
$sql .= " t.fk_user_mod, " ;
$sql .= " t.tms " ;
2022-07-24 18:27:15 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " mailing_advtarget as t " ;
2016-04-01 10:52:19 +02:00
if ( ! empty ( $id )) {
2021-03-30 17:53:25 +02:00
$sql .= " WHERE t.fk_element = " . (( int ) $id ) . " AND type_element = 'mailing' " ;
2019-11-13 19:35:39 +01:00
} else {
2021-03-30 17:53:25 +02:00
$sql .= " WHERE t.fk_element = " . (( int ) $this -> fk_element ) . " AND type_element = 'mailing' " ;
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2021-08-27 22:42:04 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2019-11-13 19:35:39 +01:00
$resql = $this -> db -> query ( $sql );
2020-11-27 00:25:45 +01:00
if ( $resql ) {
if ( $this -> db -> num_rows ( $resql )) {
2016-04-01 10:52:19 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2016-04-01 17:09:03 +02:00
2019-11-13 19:35:39 +01:00
$this -> id = $obj -> rowid ;
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$this -> name = $obj -> name ;
$this -> entity = $obj -> entity ;
2018-06-22 10:54:00 +02:00
$this -> fk_element = $obj -> fk_element ;
$this -> type_element = $obj -> type_element ;
$this -> filtervalue = $obj -> filtervalue ;
$this -> fk_user_author = $obj -> fk_user_author ;
$this -> datec = $this -> db -> jdate ( $obj -> datec );
$this -> fk_user_mod = $obj -> fk_user_mod ;
$this -> tms = $this -> db -> jdate ( $obj -> tms );
}
$this -> db -> free ( $resql );
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:35:39 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2018-06-22 10:54:00 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " . $this -> error , LOG_ERR );
return - 1 ;
}
}
2018-06-24 18:15:49 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2018-06-22 10:54:00 +02:00
/**
* Load object in memory from the database
*
2018-06-24 18:15:49 +02:00
* @ param int $id Id object
* @ param string $type_element Type target
* @ return int < 0 if KO , > 0 if OK
2018-06-22 10:54:00 +02:00
*/
2020-10-31 14:32:18 +01:00
public function fetch_by_element ( $id = 0 , $type_element = 'mailing' )
2018-06-22 10:54:00 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2018-06-22 10:54:00 +02:00
global $langs ;
$sql = " SELECT " ;
2019-11-13 19:35:39 +01:00
$sql .= " t.rowid, " ;
$sql .= " t.name, " ;
$sql .= " t.entity, " ;
$sql .= " t.fk_element, " ;
$sql .= " t.type_element, " ;
$sql .= " t.filtervalue, " ;
$sql .= " t.fk_user_author, " ;
$sql .= " t.datec, " ;
$sql .= " t.fk_user_mod, " ;
$sql .= " t.tms " ;
2022-07-24 18:27:15 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " mailing_advtarget as t " ;
2018-06-22 10:54:00 +02:00
if ( ! empty ( $id )) {
2021-03-30 17:53:25 +02:00
$sql .= " WHERE t.fk_element = " . (( int ) $id ) . " AND type_element = ' " . $this -> db -> escape ( $type_element ) . " ' " ;
2019-11-13 19:35:39 +01:00
} else {
2021-03-30 17:53:25 +02:00
$sql .= " WHERE t.fk_element = " . (( int ) $this -> fk_element ) . " AND type_element = ' " . $this -> db -> escape ( $type_element ) . " ' " ;
2018-06-22 10:54:00 +02:00
}
2021-08-27 22:42:04 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2019-11-13 19:35:39 +01:00
$resql = $this -> db -> query ( $sql );
2020-11-27 00:25:45 +01:00
if ( $resql ) {
if ( $this -> db -> num_rows ( $resql )) {
2018-06-22 10:54:00 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2019-11-13 19:35:39 +01:00
$this -> id = $obj -> rowid ;
2018-06-22 10:54:00 +02:00
$this -> name = $obj -> name ;
$this -> entity = $obj -> entity ;
$this -> fk_element = $obj -> fk_element ;
$this -> type_element = $obj -> type_element ;
2016-04-01 10:52:19 +02:00
$this -> filtervalue = $obj -> filtervalue ;
$this -> fk_user_author = $obj -> fk_user_author ;
$this -> datec = $this -> db -> jdate ( $obj -> datec );
$this -> fk_user_mod = $obj -> fk_user_mod ;
$this -> tms = $this -> db -> jdate ( $obj -> tms );
}
$this -> db -> free ( $resql );
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:35:39 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2016-04-01 10:52:19 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " . $this -> error , LOG_ERR );
return - 1 ;
}
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
* Update object into database
*
* @ param User $user User that modifies
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function update ( $user , $notrigger = 0 )
2016-04-01 10:52:19 +02:00
{
global $conf , $langs ;
2019-11-13 19:35:39 +01:00
$error = 0 ;
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
// Clean parameters
2020-11-27 00:25:45 +01:00
if ( isset ( $this -> fk_element )) {
$this -> fk_element = ( int ) $this -> fk_element ;
}
if ( isset ( $this -> type_element )) {
$this -> type_element = trim ( $this -> type_element );
}
if ( isset ( $this -> name )) {
$this -> name = trim ( $this -> name );
}
if ( isset ( $this -> filtervalue )) {
$this -> filtervalue = trim ( $this -> filtervalue );
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
// Check parameters
// Put here code to add a control on parameters values
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
// Update request
2022-07-24 18:27:15 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " mailing_advtarget SET " ;
2016-04-01 17:09:03 +02:00
2019-11-13 19:35:39 +01:00
$sql .= " name= " . ( isset ( $this -> name ) ? " ' " . $this -> db -> escape ( $this -> name ) . " ' " : " '' " ) . " , " ;
$sql .= " entity= " . $conf -> entity . " , " ;
$sql .= " fk_element= " . ( isset ( $this -> fk_element ) ? $this -> fk_element : " null " ) . " , " ;
$sql .= " type_element= " . ( isset ( $this -> type_element ) ? " ' " . $this -> db -> escape ( $this -> type_element ) . " ' " : " null " ) . " , " ;
$sql .= " filtervalue= " . ( isset ( $this -> filtervalue ) ? " ' " . $this -> db -> escape ( $this -> filtervalue ) . " ' " : " null " ) . " , " ;
$sql .= " fk_user_mod= " . $user -> id ;
2019-04-23 13:21:17 +02:00
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$this -> db -> begin ();
2021-08-27 22:42:04 +02:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
2016-04-01 10:52:19 +02:00
$resql = $this -> db -> query ( $sql );
2019-11-13 19:35:39 +01:00
if ( ! $resql ) {
2020-10-31 14:32:18 +01:00
$error ++ ;
$this -> errors [] = " Error " . $this -> db -> lasterror ();
}
2019-02-24 20:06:55 +01:00
2016-04-01 10:52:19 +02:00
// Commit or rollback
2020-11-27 00:25:45 +01:00
if ( $error ) {
foreach ( $this -> errors as $errmsg ) {
2016-04-01 10:52:19 +02:00
dol_syslog ( get_class ( $this ) . " ::update " . $errmsg , LOG_ERR );
2019-11-13 19:35:39 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2016-04-01 10:52:19 +02:00
}
$this -> db -> rollback ();
2019-11-13 19:35:39 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2016-04-01 10:52:19 +02:00
$this -> db -> commit ();
return 1 ;
}
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
* Delete object in database
*
* @ param User $user User that deletes
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function delete ( $user , $notrigger = 0 )
2016-04-01 10:52:19 +02:00
{
global $conf , $langs ;
2019-11-13 19:35:39 +01:00
$error = 0 ;
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$this -> db -> begin ();
2016-04-01 17:09:03 +02:00
2020-11-27 00:25:45 +01:00
if ( ! $error ) {
2022-07-24 18:27:15 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " mailing_advtarget " ;
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
dol_syslog ( get_class ( $this ) . " ::delete sql= " . $sql );
$resql = $this -> db -> query ( $sql );
2020-11-27 00:25:45 +01:00
if ( ! $resql ) {
$error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror ();
}
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
// Commit or rollback
2020-11-27 00:25:45 +01:00
if ( $error ) {
foreach ( $this -> errors as $errmsg ) {
2016-04-01 10:52:19 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " . $errmsg , LOG_ERR );
2019-11-13 19:35:39 +01:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2016-04-01 10:52:19 +02:00
}
$this -> db -> rollback ();
2019-11-13 19:35:39 +01:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2016-04-01 10:52:19 +02:00
$this -> db -> commit ();
return 1 ;
}
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
2020-10-23 20:08:35 +02:00
* Save query in database to retrieve it
2016-04-01 17:09:03 +02:00
*
2016-04-01 10:52:19 +02:00
* @ param User $user User that deletes
* @ param array $arrayquery All element to Query
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function savequery ( $user , $arrayquery )
2016-04-01 10:52:19 +02:00
{
2019-11-13 19:35:39 +01:00
global $langs , $conf ;
2016-04-01 10:52:19 +02:00
if ( ! empty ( $arrayquery )) {
2019-11-13 19:35:39 +01:00
$result = $this -> fetch_by_mailing ( $this -> fk_element );
$this -> filtervalue = json_encode ( $arrayquery );
if ( $result < 0 ) {
2016-04-01 10:52:19 +02:00
return - 1 ;
}
2016-04-01 17:09:03 +02:00
if ( ! empty ( $this -> id )) {
2016-04-01 10:52:19 +02:00
$this -> update ( $user );
2019-11-13 19:35:39 +01:00
} else {
2016-04-01 10:52:19 +02:00
$this -> create ( $user );
}
}
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2016-04-01 10:52:19 +02:00
/**
* Load object in memory from database
*
* @ param array $arrayquery All element to Query
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function query_thirdparty ( $arrayquery )
2016-04-01 10:52:19 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2020-04-10 10:59:32 +02:00
global $langs , $conf , $extrafields ;
2016-04-01 10:52:19 +02:00
$sql = " SELECT " ;
2020-04-10 10:59:32 +02:00
$sql .= " t.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " societe as t " ;
$sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . " societe_extrafields as te ON te.fk_object=t.rowid " ;
2016-04-01 17:09:03 +02:00
2020-04-10 10:59:32 +02:00
$sqlwhere = array ();
2016-04-01 17:09:03 +02:00
2020-04-10 10:59:32 +02:00
$sqlwhere [] = 't.entity IN (' . getEntity ( 'societe' ) . ')' ;
2016-04-01 17:09:03 +02:00
2020-04-10 10:59:32 +02:00
if ( count ( $arrayquery ) > 0 ) {
2016-04-01 10:52:19 +02:00
if ( array_key_exists ( 'cust_saleman' , $arrayquery )) {
2020-04-10 10:59:32 +02:00
$sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . " societe_commerciaux as saleman ON saleman.fk_soc=t.rowid " ;
2016-04-01 10:52:19 +02:00
}
if ( array_key_exists ( 'cust_categ' , $arrayquery )) {
2020-04-10 10:59:32 +02:00
$sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . " categorie_societe as custcateg ON custcateg.fk_soc=t.rowid " ;
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
if ( ! empty ( $arrayquery [ 'cust_name' ])) {
2020-04-10 10:59:32 +02:00
$sqlwhere [] = $this -> transformToSQL ( 't.nom' , $arrayquery [ 'cust_name' ]);
2016-04-01 10:52:19 +02:00
}
if ( ! empty ( $arrayquery [ 'cust_code' ])) {
2020-04-10 10:59:32 +02:00
$sqlwhere [] = $this -> transformToSQL ( 't.code_client' , $arrayquery [ 'cust_code' ]);
2016-04-01 10:52:19 +02:00
}
if ( ! empty ( $arrayquery [ 'cust_adress' ])) {
2020-04-10 10:59:32 +02:00
$sqlwhere [] = $this -> transformToSQL ( 't.address' , $arrayquery [ 'cust_adress' ]);
2016-04-01 10:52:19 +02:00
}
if ( ! empty ( $arrayquery [ 'cust_zip' ])) {
2020-04-10 10:59:32 +02:00
$sqlwhere [] = $this -> transformToSQL ( 't.zip' , $arrayquery [ 'cust_zip' ]);
2016-04-01 10:52:19 +02:00
}
if ( ! empty ( $arrayquery [ 'cust_city' ])) {
2020-04-10 10:59:32 +02:00
$sqlwhere [] = $this -> transformToSQL ( 't.town' , $arrayquery [ 'cust_city' ]);
2016-04-01 10:52:19 +02:00
}
if ( ! empty ( $arrayquery [ 'cust_mothercompany' ])) {
2020-04-10 10:59:32 +02:00
$str = $this -> transformToSQL ( 'nom' , $arrayquery [ 'cust_mothercompany' ]);
$sqlwhere [] = " (t.parent IN (SELECT rowid FROM " . MAIN_DB_PREFIX . " societe WHERE ( " . $str . " ))) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_status' ]) && count ( $arrayquery [ 'cust_status' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (t.status IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_status' ])) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_typecust' ]) && count ( $arrayquery [ 'cust_typecust' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (t.client IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_typecust' ])) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_comm_status' ]) && count ( $arrayquery [ 'cust_comm_status' ] > 0 )) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (t.fk_stcomm IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_comm_status' ])) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_prospect_status' ]) && count ( $arrayquery [ 'cust_prospect_status' ]) > 0 ) {
2021-03-23 03:44:50 +01:00
$sqlwhere [] = " (t.fk_prospectlevel IN ( " . $this -> db -> sanitize ( " ' " . implode ( " ',' " , $arrayquery [ 'cust_prospect_status' ]) . " ' " , 1 ) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_typeent' ]) && count ( $arrayquery [ 'cust_typeent' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (t.fk_typent IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_typeent' ])) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_saleman' ]) && count ( $arrayquery [ 'cust_saleman' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (saleman.fk_user IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_saleman' ])) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_country' ]) && count ( $arrayquery [ 'cust_country' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (t.fk_pays IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_country' ])) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_effectif_id' ]) && count ( $arrayquery [ 'cust_effectif_id' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (t.fk_effectif IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_effectif_id' ])) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_categ' ]) && count ( $arrayquery [ 'cust_categ' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (custcateg.fk_categorie IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_categ' ])) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_language' ]) && count ( $arrayquery [ 'cust_language' ]) > 0 ) {
2021-03-23 03:44:50 +01:00
$sqlwhere [] = " (t.default_lang IN ( " . $this -> db -> sanitize ( " ' " . implode ( " ',' " , $arrayquery [ 'cust_language' ]) . " ' " , 1 ) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
//Standard Extrafield feature
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) {
2020-04-27 11:18:53 +02:00
$socstatic = new Societe ( $this -> db );
$elementtype = $socstatic -> table_element ;
2019-10-06 14:41:52 +02:00
$extrafields -> fetch_name_optionals_label ( $elementtype );
2016-04-01 10:52:19 +02:00
2020-11-27 00:25:45 +01:00
foreach ( $extrafields -> attributes [ $elementtype ][ 'label' ] as $key => $val ) {
2019-10-06 14:41:52 +02:00
if (( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'varchar' ) ||
( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'text' )) {
2016-04-01 10:52:19 +02:00
if ( ! empty ( $arrayquery [ 'options_' . $key ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " LIKE ' " . $this -> db -> escape ( $arrayquery [ " options_ " . $key ]) . " ') " ;
2016-04-01 10:52:19 +02:00
}
2019-10-06 14:41:52 +02:00
} elseif (( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'int' ) ||
( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'double' )) {
2016-04-01 10:52:19 +02:00
if ( ! empty ( $arrayquery [ 'options_' . $key . '_max' ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " >= " . (( float ) $arrayquery [ " options_ " . $key . " _max " ]) . " AND te. " . $key . " <= " . (( float ) $arrayquery [ " options_ " . $key . '_min' ]) . " ) " ;
2016-04-01 10:52:19 +02:00
}
2019-10-06 14:41:52 +02:00
} elseif (( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'date' ) ||
( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'datetime' )) {
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'options_' . $key . '_end_dt' ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " >= ' " . $this -> db -> idate ( $arrayquery [ " options_ " . $key . " _st_dt " ]) . " ' AND te. " . $key . " <= ' " . $this -> db -> idate ( $arrayquery [ " options_ " . $key . " _end_dt " ]) . " ') " ;
2016-04-01 10:52:19 +02:00
}
2019-10-06 14:41:52 +02:00
} elseif ( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'boolean' ) {
2020-04-10 10:59:32 +02:00
if ( $arrayquery [ 'options_' . $key ] != '' ) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " = " . (( int ) $arrayquery [ " options_ " . $key ]) . " ) " ;
2016-04-01 10:52:19 +02:00
}
2021-04-16 16:55:19 +02:00
} elseif ( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'link' ) {
2021-04-27 01:45:10 +02:00
if ( $arrayquery [ 'options_' . $key ] > 0 ) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " = " . (( int ) $arrayquery [ " options_ " . $key ]) . " ) " ;
2021-04-16 16:58:57 +02:00
}
2018-08-13 17:26:32 +02:00
} else {
2016-04-01 10:52:19 +02:00
if ( is_array ( $arrayquery [ 'options_' . $key ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " IN ( " . $this -> db -> sanitize ( " ' " . implode ( " ',' " , $arrayquery [ " options_ " . $key ]) . " ' " , 1 ) . " )) " ;
2016-04-01 10:52:19 +02:00
} elseif ( ! empty ( $arrayquery [ 'options_' . $key ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " LIKE ' " . $this -> db -> escape ( $arrayquery [ " options_ " . $key ]) . " ') " ;
2016-04-01 10:52:19 +02:00
}
}
}
}
2020-11-27 00:25:45 +01:00
if ( count ( $sqlwhere ) > 0 ) {
$sql .= " WHERE " . implode ( " AND " , $sqlwhere );
}
2016-04-01 10:52:19 +02:00
}
2021-08-27 22:42:04 +02:00
dol_syslog ( get_class ( $this ) . " ::query_thirdparty " , LOG_DEBUG );
2016-04-01 10:52:19 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$this -> thirdparty_lines = array ();
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2020-11-27 00:25:45 +01:00
if ( $num ) {
while ( $i < $num ) {
2016-04-01 10:52:19 +02:00
$obj = $this -> db -> fetch_object ( $resql );
$this -> thirdparty_lines [ $i ] = $obj -> rowid ;
$i ++ ;
}
}
$this -> db -> free ( $resql );
return $num ;
} else {
2019-11-13 19:35:39 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
dol_syslog ( get_class ( $this ) . " ::query_thirdparty " . $this -> error , LOG_ERR );
2016-04-01 10:52:19 +02:00
return - 1 ;
}
}
2016-04-01 17:09:03 +02:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2016-04-01 10:52:19 +02:00
/**
* Load object in memory from database
*
* @ param array $arrayquery All element to Query
2016-12-10 11:36:43 +01:00
* @ param int $withThirdpartyFilter add contact with tridparty filter
2016-04-01 10:52:19 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
2020-10-31 14:32:18 +01:00
public function query_contact ( $arrayquery , $withThirdpartyFilter = 0 )
2016-04-01 10:52:19 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2020-04-10 10:59:32 +02:00
global $langs , $conf ;
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$sql = " SELECT " ;
2020-04-10 10:59:32 +02:00
$sql .= " t.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " socpeople as t " ;
$sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . " socpeople_extrafields as te ON te.fk_object=t.rowid " ;
2016-04-01 17:09:03 +02:00
2020-04-10 10:59:32 +02:00
if ( ! empty ( $withThirdpartyFilter )) {
$sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . " societe as ts ON ts.rowid=t.fk_soc " ;
$sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . " societe_extrafields as tse ON tse.fk_object=ts.rowid " ;
2016-12-09 19:06:59 +01:00
}
2020-04-10 10:59:32 +02:00
$sqlwhere = array ();
2016-04-01 17:09:03 +02:00
2022-03-18 12:05:59 +01:00
$sqlwhere [] = 't.entity IN (' . getEntity ( 'contact' ) . ')' ;
2016-04-01 17:09:03 +02:00
2020-04-10 10:59:32 +02:00
if ( count ( $arrayquery ) > 0 ) {
2016-04-01 10:52:19 +02:00
if ( array_key_exists ( 'contact_categ' , $arrayquery )) {
2020-04-10 10:59:32 +02:00
$sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . " categorie_contact as contactcateg ON contactcateg.fk_socpeople=t.rowid " ;
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
if ( ! empty ( $arrayquery [ 'contact_lastname' ])) {
2020-04-10 10:59:32 +02:00
$sqlwhere [] = $this -> transformToSQL ( 't.lastname' , $arrayquery [ 'contact_lastname' ]);
2016-04-01 10:52:19 +02:00
}
if ( ! empty ( $arrayquery [ 'contact_firstname' ])) {
2020-04-10 10:59:32 +02:00
$sqlwhere [] = $this -> transformToSQL ( 't.firstname' , $arrayquery [ 'contact_firstname' ]);
2016-04-01 10:52:19 +02:00
}
if ( ! empty ( $arrayquery [ 'contact_country' ]) && count ( $arrayquery [ 'contact_country' ])) {
2020-09-18 17:24:31 +02:00
$sqlwhere [] = " (t.fk_pays IN ( " . $this -> db -> sanitize ( $this -> db -> escape ( implode ( ',' , $arrayquery [ 'contact_country' ]))) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'contact_status' ]) && count ( $arrayquery [ 'contact_status' ]) > 0 ) {
2020-09-18 17:24:31 +02:00
$sqlwhere [] = " (t.statut IN ( " . $this -> db -> sanitize ( $this -> db -> escape ( implode ( ',' , $arrayquery [ 'contact_status' ]))) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'contact_civility' ]) && count ( $arrayquery [ 'contact_civility' ]) > 0 ) {
2021-03-23 03:44:50 +01:00
$sqlwhere [] = " (t.civility IN ( " . $this -> db -> sanitize ( " ' " . implode ( " ',' " , $arrayquery [ 'contact_civility' ]) . " ' " , 1 ) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( $arrayquery [ 'contact_no_email' ] != '' ) {
2018-12-11 09:49:46 +01:00
$tmpwhere = '' ;
2020-11-27 00:25:45 +01:00
if ( ! empty ( $arrayquery [ 'contact_no_email' ])) {
2020-04-10 10:59:32 +02:00
$tmpwhere .= " (t.email IN (SELECT email FROM " . MAIN_DB_PREFIX . " mailing_unsubscribe WHERE t.entity IN ( " . getEntity ( 'mailing' ) . " ) AND email = ' " . $this -> db -> escape ( $arrayquery [ 'contact_no_email' ]) . " ')) " ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$tmpwhere .= " (t.email NOT IN (SELECT email FROM " . MAIN_DB_PREFIX . " mailing_unsubscribe WHERE t.entity IN ( " . getEntity ( 'mailing' ) . " ) AND email = ' " . $this -> db -> escape ( $arrayquery [ 'contact_no_email' ]) . " ')) " ;
2018-12-11 09:49:46 +01:00
}
2020-04-10 10:59:32 +02:00
$sqlwhere [] = $tmpwhere ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( $arrayquery [ 'contact_update_st_dt' ] != '' ) {
$sqlwhere [] = " (t.tms >= ' " . $this -> db -> idate ( $arrayquery [ 'contact_update_st_dt' ]) . " ' AND t.tms <= ' " . $this -> db -> idate ( $arrayquery [ 'contact_update_end_dt' ]) . " ') " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( $arrayquery [ 'contact_create_st_dt' ] != '' ) {
$sqlwhere [] = " (t.datec >= ' " . $this -> db -> idate ( $arrayquery [ 'contact_create_st_dt' ]) . " ' AND t.datec <= ' " . $this -> db -> idate ( $arrayquery [ 'contact_create_end_dt' ]) . " ') " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'contact_categ' ]) && count ( $arrayquery [ 'contact_categ' ]) > 0 ) {
$sqlwhere [] = " (contactcateg.fk_categorie IN ( " . $this -> db -> escape ( implode ( " , " , $arrayquery [ 'contact_categ' ])) . " )) " ;
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
//Standard Extrafield feature
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) {
2020-04-27 11:18:53 +02:00
$contactstatic = new Contact ( $this -> db );
$elementtype = $contactstatic -> table_element ;
2019-10-06 14:41:52 +02:00
2016-04-01 10:52:19 +02:00
// fetch optionals attributes and labels
dol_include_once ( '/core/class/extrafields.class.php' );
$extrafields = new ExtraFields ( $this -> db );
2019-10-06 14:41:52 +02:00
$extrafields -> fetch_name_optionals_label ( $elementtype );
$extrafields -> fetch_name_optionals_label ( $elementtype );
2016-04-01 17:09:03 +02:00
2020-11-27 00:25:45 +01:00
foreach ( $extrafields -> attributes [ $elementtype ][ 'label' ] as $key => $val ) {
2019-10-06 14:41:52 +02:00
if (( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'varchar' ) ||
( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'text' )) {
2016-04-01 10:52:19 +02:00
if ( ! empty ( $arrayquery [ 'options_' . $key . '_cnct' ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " LIKE ' " . $this -> db -> escape ( $arrayquery [ " options_ " . $key . " _cnct " ]) . " ') " ;
2016-04-01 10:52:19 +02:00
}
2019-10-06 14:41:52 +02:00
} elseif (( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'int' ) ||
( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'double' )) {
2020-04-26 23:52:23 +02:00
if ( ! empty ( $arrayquery [ 'options_' . $key . '_max_cnct' ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " >= " . (( float ) $arrayquery [ " options_ " . $key . " _max_cnct " ]) . " AND te. " . $key . " <= " . (( float ) $arrayquery [ " options_ " . $key . " _min_cnct " ]) . " ) " ;
2016-04-01 10:52:19 +02:00
}
2019-10-06 14:41:52 +02:00
} elseif (( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'date' ) ||
( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'datetime' )) {
2020-04-26 23:52:23 +02:00
if ( ! empty ( $arrayquery [ 'options_' . $key . '_end_dt_cnct' ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " >= ' " . $this -> db -> idate ( $arrayquery [ " options_ " . $key . " _st_dt_cnct " ]) . " ' AND te. " . $key . " <= ' " . $this -> db -> idate ( $arrayquery [ " options_ " . $key . " _end_dt_cnct " ]) . " ') " ;
2016-04-01 10:52:19 +02:00
}
2019-10-06 14:41:52 +02:00
} elseif ( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'boolean' ) {
2020-04-10 10:59:32 +02:00
if ( $arrayquery [ 'options_' . $key . '_cnct' ] != '' ) {
if ( $arrayquery [ 'options_' . $key . '_cnct' ] == 0 ) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " = " . (( int ) $arrayquery [ " options_ " . $key . " _cnct " ]) . " OR ((te. " . $key . " IS NULL) AND (te.fk_object IS NOT NULL))) " ;
2019-02-24 20:06:55 +01:00
} else {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " = " . (( int ) $arrayquery [ " options_ " . $key . " _cnct " ]) . " ) " ;
2016-04-01 10:52:19 +02:00
}
}
2019-02-24 20:06:55 +01:00
} else {
2016-04-01 10:52:19 +02:00
if ( is_array ( $arrayquery [ 'options_' . $key . '_cnct' ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " IN ( " . $this -> db -> sanitize ( " ' " . implode ( " ',' " , $arrayquery [ " options_ " . $key . " _cnct " ]) . " ' " , 1 ) . " )) " ;
2016-04-01 10:52:19 +02:00
} elseif ( ! empty ( $arrayquery [ 'options_' . $key . '_cnct' ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (te. " . $key . " LIKE ' " . $this -> db -> escape ( $arrayquery [ " options_ " . $key . " _cnct " ]) . " ') " ;
2016-04-01 10:52:19 +02:00
}
}
}
2016-04-01 17:09:03 +02:00
2020-04-10 10:59:32 +02:00
if ( ! empty ( $withThirdpartyFilter )) {
2016-12-09 19:06:59 +01:00
if ( array_key_exists ( 'cust_saleman' , $arrayquery )) {
2020-04-10 10:59:32 +02:00
$sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . " societe_commerciaux as saleman ON saleman.fk_soc=ts.rowid " ;
2016-12-09 19:06:59 +01:00
}
if ( array_key_exists ( 'cust_categ' , $arrayquery )) {
2020-04-10 10:59:32 +02:00
$sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . " categorie_societe as custcateg ON custcateg.fk_soc=ts.rowid " ;
2016-12-09 19:06:59 +01:00
}
2016-04-01 17:09:03 +02:00
2016-12-09 19:06:59 +01:00
if ( ! empty ( $arrayquery [ 'cust_name' ])) {
2020-04-10 10:59:32 +02:00
$sqlwhere [] = $this -> transformToSQL ( 'ts.nom' , $arrayquery [ 'cust_name' ]);
2016-12-09 19:06:59 +01:00
}
if ( ! empty ( $arrayquery [ 'cust_code' ])) {
2020-04-10 10:59:32 +02:00
$sqlwhere [] = $this -> transformToSQL ( 'ts.code_client' , $arrayquery [ 'cust_code' ]);
2016-12-09 19:06:59 +01:00
}
if ( ! empty ( $arrayquery [ 'cust_adress' ])) {
2020-04-10 10:59:32 +02:00
$sqlwhere [] = $this -> transformToSQL ( 'ts.address' , $arrayquery [ 'cust_adress' ]);
2016-12-09 19:06:59 +01:00
}
if ( ! empty ( $arrayquery [ 'cust_zip' ])) {
2020-04-10 10:59:32 +02:00
$sqlwhere [] = $this -> transformToSQL ( 'ts.zip' , $arrayquery [ 'cust_zip' ]);
2016-12-09 19:06:59 +01:00
}
if ( ! empty ( $arrayquery [ 'cust_city' ])) {
2020-04-10 10:59:32 +02:00
$sqlwhere [] = $this -> transformToSQL ( 'ts.town' , $arrayquery [ 'cust_city' ]);
2016-12-09 19:06:59 +01:00
}
if ( ! empty ( $arrayquery [ 'cust_mothercompany' ])) {
2020-04-10 10:59:32 +02:00
$str = $this -> transformToSQL ( 'nom' , $arrayquery [ 'cust_mothercompany' ]);
$sqlwhere [] = " (ts.parent IN (SELECT rowid FROM " . MAIN_DB_PREFIX . " societe WHERE ( " . $str . " ))) " ;
2016-12-09 19:06:59 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_status' ]) && count ( $arrayquery [ 'cust_status' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (ts.status IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_status' ])) . " )) " ;
2016-12-09 19:06:59 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_typecust' ]) && count ( $arrayquery [ 'cust_typecust' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (ts.client IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_typecust' ])) . " )) " ;
2016-12-09 19:06:59 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_comm_status' ]) && count ( $arrayquery [ 'cust_comm_status' ] > 0 )) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (ts.fk_stcomm IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_comm_status' ])) . " )) " ;
2016-12-09 19:06:59 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_prospect_status' ]) && count ( $arrayquery [ 'cust_prospect_status' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (ts.fk_prospectlevel IN (' " . $this -> db -> sanitize ( implode ( " ',' " , $arrayquery [ 'cust_prospect_status' ])) . " ')) " ;
2016-12-09 19:06:59 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_typeent' ]) && count ( $arrayquery [ 'cust_typeent' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (ts.fk_typent IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_typeent' ])) . " )) " ;
2016-12-09 19:06:59 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_saleman' ]) && count ( $arrayquery [ 'cust_saleman' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (saleman.fk_user IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_saleman' ])) . " )) " ;
2016-12-09 19:06:59 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_country' ]) && count ( $arrayquery [ 'cust_country' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (ts.fk_pays IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_country' ])) . " )) " ;
2016-12-09 19:06:59 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_effectif_id' ]) && count ( $arrayquery [ 'cust_effectif_id' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (ts.fk_effectif IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_effectif_id' ])) . " )) " ;
2016-12-09 19:06:59 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_categ' ]) && count ( $arrayquery [ 'cust_categ' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (custcateg.fk_categorie IN ( " . $this -> db -> sanitize ( implode ( ',' , $arrayquery [ 'cust_categ' ])) . " )) " ;
2016-12-09 19:06:59 +01:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'cust_language' ]) && count ( $arrayquery [ 'cust_language' ]) > 0 ) {
2021-03-22 11:30:18 +01:00
$sqlwhere [] = " (ts.default_lang IN (' " . $this -> db -> sanitize ( implode ( " ',' " , $arrayquery [ 'cust_language' ])) . " ')) " ;
2016-12-09 19:06:59 +01:00
}
2016-04-01 17:09:03 +02:00
2016-12-09 19:06:59 +01:00
//Standard Extrafield feature
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) {
2020-04-27 11:18:53 +02:00
$socstatic = new Societe ( $this -> db );
$elementtype = $socstatic -> table_element ;
2019-10-06 14:41:52 +02:00
2016-12-09 19:06:59 +01:00
// fetch optionals attributes and labels
dol_include_once ( '/core/class/extrafields.class.php' );
$extrafields = new ExtraFields ( $this -> db );
2019-10-06 14:41:52 +02:00
$extrafields -> fetch_name_optionals_label ( $elementtype );
2020-11-27 00:25:45 +01:00
foreach ( $extrafields -> attributes [ $elementtype ][ 'label' ] as $key => $val ) {
2019-10-06 14:41:52 +02:00
if (( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'varchar' ) ||
( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'text' )) {
2019-02-03 14:29:45 +01:00
if ( ! empty ( $arrayquery [ 'options_' . $key ])) {
2020-09-19 23:27:11 +02:00
$sqlwhere [] = " (tse. " . $key . " LIKE ' " . $this -> db -> escape ( $arrayquery [ 'options_' . $key ]) . " ') " ;
2019-02-03 14:29:45 +01:00
}
2019-10-06 14:41:52 +02:00
} elseif (( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'int' ) ||
( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'double' )) {
2019-02-03 14:29:45 +01:00
if ( ! empty ( $arrayquery [ 'options_' . $key . '_max' ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (tse. " . $key . " >= " . (( float ) $arrayquery [ " options_ " . $key . " _max " ]) . " AND tse. " . $key . " <= " . (( float ) $arrayquery [ " options_ " . $key . " _min " ]) . " ) " ;
2019-02-03 14:29:45 +01:00
}
2019-10-06 14:41:52 +02:00
} elseif (( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'date' ) ||
( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'datetime' )) {
2020-04-10 10:59:32 +02:00
if ( ! empty ( $arrayquery [ 'options_' . $key . '_end_dt' ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (tse. " . $key . " >= ' " . $this -> db -> idate ( $arrayquery [ " options_ " . $key . " _st_dt " ]) . " ' AND tse. " . $key . " <= ' " . $this -> db -> idate ( $arrayquery [ " options_ " . $key . " _end_dt " ]) . " ') " ;
2019-02-03 14:29:45 +01:00
}
2019-10-06 14:41:52 +02:00
} elseif ( $extrafields -> attributes [ $elementtype ][ 'type' ][ $key ] == 'boolean' ) {
2020-04-10 10:59:32 +02:00
if ( $arrayquery [ 'options_' . $key ] != '' ) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (tse. " . $key . " = " . (( int ) $arrayquery [ " options_ " . $key ]) . " ) " ;
2019-02-03 14:29:45 +01:00
}
} else {
if ( is_array ( $arrayquery [ 'options_' . $key ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (tse. " . $key . " IN ( " . $this -> db -> sanitize ( " ' " . implode ( " ',' " , $arrayquery [ " options_ " . $key ]) . " ' " , 1 ) . " )) " ;
2019-02-03 14:29:45 +01:00
} elseif ( ! empty ( $arrayquery [ 'options_' . $key ])) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = " (tse. " . $key . " LIKE ' " . $this -> db -> escape ( $arrayquery [ " options_ " . $key ]) . " ') " ;
2019-02-03 14:29:45 +01:00
}
}
2016-12-09 19:06:59 +01:00
}
}
}
}
2020-11-27 00:25:45 +01:00
if ( count ( $sqlwhere ) > 0 ) {
$sql .= " WHERE " . implode ( " AND " , $sqlwhere );
}
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2021-08-27 22:42:04 +02:00
dol_syslog ( get_class ( $this ) . " ::query_contact " , LOG_DEBUG );
2016-04-01 10:52:19 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$this -> contact_lines = array ();
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2016-04-01 17:09:03 +02:00
2020-11-27 00:25:45 +01:00
if ( $num ) {
while ( $i < $num ) {
2016-04-01 10:52:19 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$this -> contact_lines [ $i ] = $obj -> rowid ;
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$i ++ ;
}
}
$this -> db -> free ( $resql );
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
return $num ;
} else {
2019-11-13 19:35:39 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
dol_syslog ( get_class ( $this ) . " ::query_contact " . $this -> error , LOG_ERR );
2016-04-01 10:52:19 +02:00
return - 1 ;
}
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
/**
* Parse criteria to return a SQL qury formated
*
* @ param string $column_to_test column to test
* @ param string $criteria Use %% as magic caracters . For exemple to find all item like < b > jean , joe , jim </ b > , you can input < b > j %%</ b > , you can also use ; as separator for value ,
2016-05-06 21:28:06 +02:00
* and use ! for except this value .
2016-04-01 10:52:19 +02:00
* For exemple jean ; joe ; jim %% ; ! jimo ; ! jima %> will target all jean , joe , start with jim but not jimo and not everythnig taht start by jima
2016-05-06 21:28:06 +02:00
* @ return string Sql to use for the where condition
2016-04-01 10:52:19 +02:00
*/
2020-10-31 14:32:18 +01:00
public function transformToSQL ( $column_to_test , $criteria )
{
2016-04-01 10:52:19 +02:00
$return_sql_criteria = '(' ;
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
//This is a multiple value test
2019-01-27 11:55:16 +01:00
if ( preg_match ( '/;/' , $criteria )) {
2019-11-13 19:35:39 +01:00
$return_sql_not_like = array ();
$return_sql_like = array ();
2016-04-01 17:09:03 +02:00
2019-11-13 19:35:39 +01:00
$criteria_array = explode ( ';' , $criteria );
foreach ( $criteria_array as $inter_criteria ) {
2019-01-27 11:55:16 +01:00
if ( preg_match ( '/!/' , $inter_criteria )) {
2019-11-13 19:35:39 +01:00
$return_sql_not_like [] = '(' . $column_to_test . ' NOT LIKE \'' . str_replace ( '!' , '' , $inter_criteria ) . '\')' ;
2016-04-01 10:52:19 +02:00
} else {
2019-11-13 19:35:39 +01:00
$return_sql_like [] = '(' . $column_to_test . ' LIKE \'' . $inter_criteria . '\')' ;
2016-04-01 10:52:19 +02:00
}
}
2016-04-01 17:09:03 +02:00
2019-11-13 19:35:39 +01:00
if ( count ( $return_sql_like ) > 0 ) {
$return_sql_criteria .= '(' . implode ( ' OR ' , $return_sql_like ) . ')' ;
2016-04-01 10:52:19 +02:00
}
2019-11-13 19:35:39 +01:00
if ( count ( $return_sql_not_like ) > 0 ) {
$return_sql_criteria .= ' AND (' . implode ( ' AND ' , $return_sql_not_like ) . ')' ;
2016-04-01 10:52:19 +02:00
}
2019-02-24 20:06:55 +01:00
} else {
2019-11-13 19:35:39 +01:00
$return_sql_criteria .= $column_to_test . ' LIKE \'' . $this -> db -> escape ( $criteria ) . '\'' ;
2016-04-01 10:52:19 +02:00
}
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$return_sql_criteria .= ')' ;
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
return $return_sql_criteria ;
}
2018-08-13 17:26:32 +02:00
}