2006-06-18 17:06:29 +02:00
< ? php
2015-04-26 16:30:33 +02:00
/* Copyright ( C ) 2006 - 2015 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2013 Regis Houssin < regis . houssin @ inodbox . com >
2020-09-03 12:43:38 +02:00
* Copyright ( C ) 2010 - 2020 Juanjo Menent < jmenent @ 2 byte . es >
2013-10-30 18:17:37 +01:00
* Copyright ( C ) 2012 - 2013 Christophe Battarel < christophe . battarel @ altairis . fr >
2019-01-19 12:40:17 +01:00
* Copyright ( C ) 2011 - 2019 Philippe Grand < philippe . grand @ atoo - net . com >
2015-04-18 20:50:03 +02:00
* Copyright ( C ) 2012 - 2015 Marcos GarcÃa < marcosgdf @ gmail . com >
2015-04-23 23:21:06 +02:00
* Copyright ( C ) 2012 - 2015 Raphaël Doursenaud < rdoursenaud @ gpcsolutions . fr >
2015-02-26 14:15:33 +01:00
* Copyright ( C ) 2012 Cedric Salvador < csalvador @ gpcsolutions . fr >
2021-02-28 07:32:23 +01:00
* Copyright ( C ) 2015 - 2021 Alexandre Spangaro < aspangaro @ open - dsi . fr >
2016-04-16 12:19:46 +02:00
* Copyright ( C ) 2016 Bahfir abbes < dolipar @ dolipar . org >
2017-05-02 15:19:55 +02:00
* Copyright ( C ) 2017 ATM Consulting < support @ atm - consulting . fr >
2019-07-10 18:36:12 +02:00
* Copyright ( C ) 2017 - 2019 Nicolas ZABOURI < info @ inovea - conseil . com >
2021-02-28 07:32:23 +01:00
* Copyright ( C ) 2017 Rui Strecht < rui . strecht @ aliartalentos . com >
2021-03-15 20:31:55 +01:00
* Copyright ( C ) 2018 - 2021 Frédéric France < frederic . france @ netlogic . fr >
2018-08-31 17:16:27 +02:00
* Copyright ( C ) 2018 Josep LluÃs Amador < joseplluis @ lliuretic . cat >
2021-02-28 07:32:23 +01:00
* Copyright ( C ) 2021 Gauthier VERDOL < gauthier . verdol @ atm - consulting . fr >
2012-01-10 17:16:17 +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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2012-01-10 17:16:17 +01:00
* ( at your option ) any later version .
2012-02-01 13:21:39 +01:00
*
2012-01-10 17:16:17 +01:00
* 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 .
2012-02-01 13:21:39 +01:00
*
2012-01-10 17:16:17 +01:00
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2012-01-10 17:16:17 +01:00
*/
2006-06-18 17:06:29 +02:00
/**
2010-06-08 01:52:43 +02:00
* \file htdocs / core / class / commonobject . class . php
2009-06-19 09:32:39 +02:00
* \ingroup core
2011-06-22 12:58:22 +02:00
* \brief File of parent class of all other business classes ( invoices , contracts , proposals , orders , ... )
2009-02-20 23:53:15 +01:00
*/
2006-06-18 17:06:29 +02:00
/**
2012-02-01 13:21:39 +01:00
* Parent class of all other business classes ( invoices , contracts , proposals , orders , ... )
2009-02-20 23:53:15 +01:00
*/
2011-08-23 00:04:21 +02:00
abstract class CommonObject
2006-06-18 17:06:29 +02:00
{
2017-10-07 13:09:31 +02:00
/**
* @ var DoliDb Database handler ( result of a new DoliDB )
*/
2015-03-17 10:47:20 +01:00
public $db ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ var int The object identifier
*/
public $id ;
2018-08-29 17:03:18 +02:00
2020-12-14 20:40:47 +01:00
/**
* @ var int The environment ID when using a multicompany module
*/
public $entity ;
2015-09-07 14:22:03 +02:00
/**
2017-10-07 13:09:31 +02:00
* @ var string Error string
2019-07-18 17:50:15 +02:00
* @ see $errors
2017-10-07 13:09:31 +02:00
*/
public $error ;
2018-08-29 17:03:18 +02:00
2020-12-14 20:40:47 +01:00
/**
* @ var string Error string that is hidden but can be used to store complementatry technical code .
*/
public $errorhidden ;
2017-04-14 12:38:52 +02:00
/**
2017-10-07 13:09:31 +02:00
* @ var string [] Array of error strings
*/
2019-11-13 19:34:37 +01:00
public $errors = array ();
2018-08-29 17:03:18 +02:00
2017-04-14 12:38:52 +02:00
/**
2018-08-23 18:17:09 +02:00
* @ var string ID to identify managed object
2017-04-14 12:38:52 +02:00
*/
public $element ;
2018-08-29 17:03:18 +02:00
2017-04-14 12:38:52 +02:00
/**
2018-08-22 18:34:50 +02:00
* @ var string Name of table without prefix where object is stored
2017-04-14 12:38:52 +02:00
*/
public $table_element ;
2018-08-29 17:03:18 +02:00
2017-04-14 12:38:52 +02:00
/**
2019-12-12 20:48:14 +01:00
* @ var string Name of subtable line
2017-04-14 12:38:52 +02:00
*/
2019-11-13 19:34:37 +01:00
public $table_element_line = '' ;
2017-06-07 16:44:04 +02:00
2017-10-07 13:09:31 +02:00
/**
* @ var string Key value used to track if data is coming from import wizard
*/
public $import_key ;
2014-12-25 21:47:39 +01:00
2017-10-07 13:09:31 +02:00
/**
* @ var mixed Contains data to manage extrafields
*/
2019-11-13 19:34:37 +01:00
public $array_options = array ();
2017-06-07 16:44:04 +02:00
2020-03-27 16:02:58 +01:00
/**
* @ var mixed Array to store alternative languages values of object
*/
2020-04-10 10:59:32 +02:00
public $array_languages = null ; // Value is array() when load already tried
2017-06-07 16:44:04 +02:00
2017-10-07 13:09:31 +02:00
/**
2018-02-01 10:50:50 +01:00
* @ var int [][] Array of linked objects ids . Loaded by -> fetchObjectLinked
2017-10-07 13:09:31 +02:00
*/
public $linkedObjectsIds ;
2015-03-17 10:47:20 +01:00
2017-10-07 13:09:31 +02:00
/**
* @ var mixed Array of linked objects . Loaded by -> fetchObjectLinked
*/
public $linkedObjects ;
2018-08-29 17:03:18 +02:00
2017-10-07 13:09:31 +02:00
/**
* @ var Object To store a cloned copy of object before to edit it and keep track of old properties
*/
public $oldcopy ;
/**
* @ var string Column name of the ref field .
*/
protected $table_ref_field = '' ;
2015-04-26 16:30:33 +02:00
2017-10-07 13:09:31 +02:00
// Following vars are used by some objects only. We keep this property here in CommonObject to be able to provide common method using them.
/**
2017-10-27 00:07:58 +02:00
* @ var array < string , mixed > Can be used to pass information when only object is provided to method
2017-10-07 13:09:31 +02:00
*/
2019-11-13 19:34:37 +01:00
public $context = array ();
2017-10-07 13:09:31 +02:00
/**
* @ var string Contains canvas name if record is an alternative canvas record
*/
public $canvas ;
2015-04-26 16:30:33 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ var Project The related project
* @ see fetch_projet ()
*/
public $project ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ var int The related project ID
* @ see setProject (), project
*/
public $fk_project ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ deprecated
* @ see project
*/
public $projet ;
2015-05-05 22:09:58 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ var Contact a related contact
* @ see fetch_contact ()
*/
public $contact ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ var int The related contact ID
* @ see fetch_contact ()
*/
public $contact_id ;
/**
* @ var Societe A related thirdparty
* @ see fetch_thirdparty ()
*/
public $thirdparty ;
/**
* @ var User A related user
* @ see fetch_user ()
*/
public $user ;
/**
2017-11-25 12:11:02 +01:00
* @ var string The type of originating object ( 'commande' , 'facture' , ... )
2015-05-23 23:17:39 +02:00
* @ see fetch_origin ()
*/
public $origin ;
2018-09-02 11:08:41 +02:00
2015-05-23 23:17:39 +02:00
/**
2017-11-25 12:11:02 +01:00
* @ var int The id of originating object
* @ see fetch_origin ()
2015-05-23 23:17:39 +02:00
*/
public $origin_id ;
/**
* @ var string The object ' s reference
*/
public $ref ;
2018-08-29 17:03:18 +02:00
2020-12-14 20:40:47 +01:00
/**
* @ var string An external reference for the object
*/
public $ref_ext ;
2015-05-23 23:17:39 +02:00
/**
* @ var string The object ' s previous reference
*/
public $ref_previous ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ var string The object ' s next reference
*/
public $ref_next ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
2020-12-17 18:08:56 +01:00
* @ var string Ref to store on object to save the new ref to use for example when making a validate () of an object
2015-05-23 23:17:39 +02:00
*/
2020-12-17 18:08:56 +01:00
public $newref ;
2015-05-23 23:17:39 +02:00
/**
* @ var int The object ' s status
* @ see setStatut ()
*/
public $statut ;
2021-02-02 16:12:03 +01:00
/**
* @ var int The object ' s status
* @ see setStatut ()
*/
public $status ;
2015-05-23 23:17:39 +02:00
/**
* @ var string
* @ see getFullAddress ()
*/
public $country ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ var int
* @ see getFullAddress (), country
*/
public $country_id ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ var string
* @ see getFullAddress (), isInEEC (), country
*/
2020-09-07 10:18:17 +02:00
public $country_code ;
2018-08-29 17:03:18 +02:00
2020-09-07 10:18:17 +02:00
/**
2017-09-20 13:06:35 +02:00
* @ var string
* @ see getFullAddress ()
*/
public $state ;
2018-08-29 17:03:18 +02:00
2017-09-20 13:06:35 +02:00
/**
* @ var int
* @ see getFullAddress (), state
*/
public $state_id ;
2018-08-29 17:03:18 +02:00
2017-09-20 13:06:35 +02:00
/**
* @ var string
2020-08-22 03:49:46 +02:00
* @ see getFullAddress (), $state
2017-09-20 13:06:35 +02:00
*/
2020-09-07 10:18:17 +02:00
public $state_code ;
2018-08-29 17:03:18 +02:00
2020-08-22 03:49:46 +02:00
/**
* @ var int
* @ see getFullAddress (), $region_code , $region
2017-09-20 13:06:35 +02:00
*/
2020-08-22 03:49:46 +02:00
public $region_id ;
2018-08-29 17:03:18 +02:00
2017-09-20 13:06:35 +02:00
/**
* @ var string
2020-08-22 03:49:46 +02:00
* @ see getFullAddress (), $region_id , $region
2017-09-20 13:06:35 +02:00
*/
2020-09-07 10:18:17 +02:00
public $region_code ;
2018-08-29 17:03:18 +02:00
2017-09-20 13:06:35 +02:00
/**
* @ var string
2020-09-07 10:18:17 +02:00
* @ see getFullAddress (), $region_id , $region_code
2017-09-20 13:06:35 +02:00
*/
2020-09-07 10:18:17 +02:00
public $region ;
2015-05-23 23:17:39 +02:00
/**
* @ var int
* @ see fetch_barcode ()
*/
public $barcode_type ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ var string
* @ see fetch_barcode (), barcode_type
*/
public $barcode_type_code ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ var string
* @ see fetch_barcode (), barcode_type
*/
public $barcode_type_label ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ var string
* @ see fetch_barcode (), barcode_type
*/
public $barcode_type_coder ;
/**
2016-02-24 12:01:50 +01:00
* @ var int Payment method ID ( cheque , cash , ... )
2015-05-23 23:17:39 +02:00
* @ see setPaymentMethods ()
*/
public $mode_reglement_id ;
2015-04-23 23:21:06 +02:00
/**
2016-05-08 12:08:23 +02:00
* @ var int Payment terms ID
2015-05-23 23:17:39 +02:00
* @ see setPaymentTerms ()
2015-04-23 23:21:06 +02:00
*/
2015-05-23 23:17:39 +02:00
public $cond_reglement_id ;
2018-08-29 17:03:18 +02:00
2020-12-14 20:40:47 +01:00
/**
* @ var int Demand reason ID
*/
public $demand_reason_id ;
/**
* @ var int Transport mode ID ( For module intracomm report )
* @ see setTransportMode ()
*/
public $transport_mode_id ;
2015-04-23 23:21:06 +02:00
/**
2016-05-08 12:08:23 +02:00
* @ var int Payment terms ID
* @ deprecated Kept for compatibility
2015-05-23 23:17:39 +02:00
* @ see cond_reglement_id ;
2015-04-23 23:21:06 +02:00
*/
2015-05-23 23:17:39 +02:00
public $cond_reglement ;
2015-04-23 23:21:06 +02:00
/**
2015-05-23 23:17:39 +02:00
* @ var int Delivery address ID
* @ see setDeliveryAddress ()
2019-10-01 16:19:54 +02:00
* @ deprecated
2015-04-23 23:21:06 +02:00
*/
2015-05-23 23:17:39 +02:00
public $fk_delivery_address ;
2015-03-17 10:47:20 +01:00
2015-05-23 23:17:39 +02:00
/**
* @ var int Shipping method ID
* @ see setShippingMethod ()
*/
public $shipping_method_id ;
/**
* @ var string
* @ see SetDocModel ()
*/
2020-08-18 14:48:38 +02:00
public $model_pdf ;
2015-05-23 23:17:39 +02:00
2021-02-08 18:30:09 +01:00
/**
* @ var string
* @ deprecated
2021-03-29 23:24:52 +02:00
* @ see $model_pdf
2021-02-08 18:30:09 +01:00
*/
public $modelpdf ;
2019-01-29 18:40:47 +01:00
/**
* @ var string
* Contains relative path of last generated main file
*/
public $last_main_doc ;
2020-12-14 20:40:47 +01:00
/**
2020-12-23 22:17:05 +01:00
* @ var int Bank account ID sometimes , ID of record into llx_bank sometimes
2020-12-14 20:40:47 +01:00
* @ deprecated
* @ see $fk_account
*/
public $fk_bank ;
2015-05-23 23:17:39 +02:00
/**
* @ var int Bank account ID
* @ see SetBankAccount ()
*/
public $fk_account ;
2020-12-14 20:40:47 +01:00
/**
* @ var string Open ID
*/
public $openid ;
2015-05-23 23:17:39 +02:00
/**
* @ var string Public note
* @ see update_note ()
*/
public $note_public ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ var string Private note
* @ see update_note ()
*/
public $note_private ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
* @ deprecated
2020-02-09 18:28:34 +01:00
* @ see $note_private
2015-05-23 23:17:39 +02:00
*/
public $note ;
/**
2015-09-24 20:12:55 +02:00
* @ var float Total amount before taxes
2015-05-23 23:17:39 +02:00
* @ see update_price ()
*/
public $total_ht ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
2015-09-24 20:12:55 +02:00
* @ var float Total VAT amount
2015-05-23 23:17:39 +02:00
* @ see update_price ()
*/
public $total_tva ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
2015-09-24 20:12:55 +02:00
* @ var float Total local tax 1 amount
2015-05-23 23:17:39 +02:00
* @ see update_price ()
*/
public $total_localtax1 ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
2015-09-24 20:12:55 +02:00
* @ var float Total local tax 2 amount
2015-05-23 23:17:39 +02:00
* @ see update_price ()
*/
public $total_localtax2 ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
2015-09-24 20:12:55 +02:00
* @ var float Total amount with taxes
2015-05-23 23:17:39 +02:00
* @ see update_price ()
*/
public $total_ttc ;
2014-12-25 21:47:39 +01:00
2015-05-23 23:17:39 +02:00
/**
* @ var CommonObjectLine []
*/
public $lines ;
2017-09-18 17:27:29 +02:00
/**
* @ var mixed Contains comments
* @ see fetchComments ()
*/
2019-11-13 19:34:37 +01:00
public $comments = array ();
2015-05-23 23:17:39 +02:00
/**
2019-03-09 18:05:48 +01:00
* @ var string The name
2015-05-23 23:17:39 +02:00
*/
2017-10-07 13:09:31 +02:00
public $name ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
2020-09-07 10:18:17 +02:00
* @ var string The lastname
2015-05-23 23:17:39 +02:00
*/
2017-10-07 13:09:31 +02:00
public $lastname ;
2018-08-29 17:03:18 +02:00
2015-05-23 23:17:39 +02:00
/**
2020-09-07 10:18:17 +02:00
* @ var string The firstname
2015-05-23 23:17:39 +02:00
*/
2017-10-07 13:09:31 +02:00
public $firstname ;
2015-05-23 23:17:39 +02:00
2019-03-09 18:05:48 +01:00
/**
2020-09-07 10:18:17 +02:00
* @ var string The civility code , not an integer
2019-03-09 18:05:48 +01:00
*/
2017-10-07 13:09:31 +02:00
public $civility_id ;
2019-03-09 18:05:48 +01:00
2018-10-19 13:38:05 +02:00
// Dates
2019-11-01 10:42:14 +01:00
/**
2020-09-07 10:18:17 +02:00
* @ var integer | string date_creation
*/
2019-11-01 10:42:14 +01:00
public $date_creation ;
2019-03-09 18:05:48 +01:00
2019-11-10 11:11:43 +01:00
/**
* @ var integer | string $date_validation ;
*/
2019-11-16 23:24:29 +01:00
public $date_validation ; // Date validation
2019-03-09 18:05:48 +01:00
2019-11-10 11:11:43 +01:00
/**
* @ var integer | string $date_modification ;
*/
2019-11-16 23:24:29 +01:00
public $date_modification ; // Date last change (tms field)
2015-05-23 23:17:39 +02:00
2019-10-01 16:19:54 +02:00
public $next_prev_filter ;
2020-12-14 20:40:47 +01:00
/**
* @ var int 1 if object is specimen
*/
public $specimen = 0 ;
2015-05-23 23:17:39 +02:00
2020-12-14 20:40:47 +01:00
/**
* @ var int Id of contact to send object ( used by the trigger of module Agenda )
*/
public $sendtoid ;
2017-06-07 16:44:04 +02:00
2021-02-27 19:43:56 +01:00
/**
* @ var float Amount already paid ( used to show correct status )
*/
public $alreadypaid ;
2011-11-07 15:04:02 +01:00
2020-06-06 21:56:23 +02:00
/**
* @ var array List of child tables . To test if we can delete object .
*/
protected $childtables = array ();
2020-06-04 20:14:19 +02:00
/**
* @ var array List of child tables . To know object to delete on cascade .
2021-06-10 16:47:45 +02:00
* If name is like '@ClassName:FilePathClass:ParentFkFieldName' , it will
* call method deleteByParentField ( parentId , ParentFkFieldName ) to fetch and delete child object .
2020-06-04 20:14:19 +02:00
*/
protected $childtablesoncascade = array ();
2017-06-07 16:44:04 +02:00
2020-06-06 21:56:23 +02:00
2017-10-07 13:09:31 +02:00
// No constructor as it is an abstract class
2017-07-24 08:52:10 +02:00
/**
* Check an object id / ref exists
* If you don ' t need / want to instantiate object and just need to know if object exists , use this method instead of fetch
*
2014-05-13 10:12:45 +02:00
* @ param string $element String of element ( 'product' , 'facture' , ... )
* @ param int $id Id of object
* @ param string $ref Ref of object to check
* @ param string $ref_ext Ref ext of object to check
* @ return int < 0 if KO , 0 if OK but not found , > 0 if OK and exists
2017-07-24 08:52:10 +02:00
*/
2019-02-25 22:27:04 +01:00
public static function isExistingObject ( $element , $id , $ref = '' , $ref_ext = '' )
2017-07-24 08:52:10 +02:00
{
2019-11-13 19:34:37 +01:00
global $db , $conf ;
2014-05-13 10:12:45 +02:00
$sql = " SELECT rowid, ref, ref_ext " ;
2019-11-13 19:34:37 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . $element ;
$sql .= " WHERE entity IN ( " . getEntity ( $element ) . " ) " ;
2015-06-06 12:26:47 +02:00
2021-02-23 22:03:23 +01:00
if ( $id > 0 ) {
2021-06-09 15:36:47 +02:00
$sql .= " AND rowid = " . (( int ) $id );
2021-02-23 22:03:23 +01:00
} elseif ( $ref ) {
$sql .= " AND ref = ' " . $db -> escape ( $ref ) . " ' " ;
} elseif ( $ref_ext ) {
$sql .= " AND ref_ext = ' " . $db -> escape ( $ref_ext ) . " ' " ;
} else {
2019-11-13 19:34:37 +01:00
$error = 'ErrorWrongParameters' ;
2014-05-13 10:12:45 +02:00
dol_print_error ( get_class () . " ::isExistingObject " . $error , LOG_ERR );
return - 1 ;
}
2021-02-23 22:03:23 +01:00
if ( $ref || $ref_ext ) {
2021-06-09 15:36:47 +02:00
$sql .= " AND entity = " . (( int ) $conf -> entity );
2021-02-23 22:03:23 +01:00
}
2014-05-13 10:12:45 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class () . " ::isExistingObject " , LOG_DEBUG );
2014-05-13 10:12:45 +02:00
$resql = $db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2019-11-13 19:34:37 +01:00
$num = $db -> num_rows ( $resql );
2021-02-23 22:03:23 +01:00
if ( $num > 0 ) {
return 1 ;
} else {
return 0 ;
}
2014-05-13 10:12:45 +02:00
}
return - 1 ;
2017-07-24 08:52:10 +02:00
}
2014-05-13 10:12:45 +02:00
2017-07-24 08:52:10 +02:00
/**
* Method to output saved errors
*
* @ return string String with errors
*/
2019-02-25 22:27:04 +01:00
public function errorsToString ()
2017-07-24 08:52:10 +02:00
{
2019-11-13 19:34:37 +01:00
return $this -> error . ( is_array ( $this -> errors ) ? (( $this -> error != '' ? ', ' : '' ) . join ( ', ' , $this -> errors )) : '' );
2017-07-24 08:52:10 +02:00
}
2013-06-28 12:44:17 +02:00
2020-02-16 12:05:00 +01:00
2019-01-21 15:22:35 +01:00
/**
* Return customer ref for screen output .
*
* @ param string $objref Customer ref
* @ return string Customer ref formated
*/
2019-02-25 22:27:04 +01:00
public function getFormatedCustomerRef ( $objref )
2019-01-21 15:22:35 +01:00
{
2020-09-07 10:18:17 +02:00
global $hookmanager ;
2019-01-21 15:22:35 +01:00
2020-09-07 10:18:17 +02:00
$parameters = array ( 'objref' => $objref );
$action = '' ;
$reshook = $hookmanager -> executeHooks ( 'getFormatedCustomerRef' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2021-02-23 22:03:23 +01:00
if ( $reshook > 0 ) {
2020-09-07 10:18:17 +02:00
return $hookmanager -> resArray [ 'objref' ];
}
return $objref . ( isset ( $hookmanager -> resArray [ 'objref' ]) ? $hookmanager -> resArray [ 'objref' ] : '' );
2019-01-21 15:22:35 +01:00
}
/**
* Return supplier ref for screen output .
*
* @ param string $objref Supplier ref
* @ return string Supplier ref formated
*/
2019-02-25 22:27:04 +01:00
public function getFormatedSupplierRef ( $objref )
2019-01-21 15:22:35 +01:00
{
2020-09-07 10:18:17 +02:00
global $hookmanager ;
2019-01-21 15:22:35 +01:00
2020-09-07 10:18:17 +02:00
$parameters = array ( 'objref' => $objref );
$action = '' ;
$reshook = $hookmanager -> executeHooks ( 'getFormatedSupplierRef' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2021-02-23 22:03:23 +01:00
if ( $reshook > 0 ) {
2020-09-07 10:18:17 +02:00
return $hookmanager -> resArray [ 'objref' ];
}
return $objref . ( isset ( $hookmanager -> resArray [ 'objref' ]) ? $hookmanager -> resArray [ 'objref' ] : '' );
2019-01-21 15:22:35 +01:00
}
2017-10-07 13:09:31 +02:00
/**
* Return full name ( civility + ' ' + name + ' ' + lastname )
*
2017-12-01 15:39:18 +01:00
* @ param Translate $langs Language object for translation of civility ( used only if option is 1 )
2017-10-07 13:09:31 +02:00
* @ param int $option 0 = No option , 1 = Add civility
2020-12-14 20:40:47 +01:00
* @ param int $nameorder - 1 = Auto , 0 = Lastname + Firstname , 1 = Firstname + Lastname , 2 = Firstname , 3 = Firstname if defined else lastname , 4 = Lastname , 5 = Lastname if defined else firstname
2017-10-07 13:09:31 +02:00
* @ param int $maxlen Maximum length
* @ return string String with full name
*/
2019-02-25 22:27:04 +01:00
public function getFullName ( $langs , $option = 0 , $nameorder = - 1 , $maxlen = 0 )
2017-10-07 13:09:31 +02:00
{
//print "lastname=".$this->lastname." name=".$this->name." nom=".$this->nom."<br>\n";
2019-11-13 19:34:37 +01:00
$lastname = $this -> lastname ;
$firstname = $this -> firstname ;
2021-02-23 22:03:23 +01:00
if ( empty ( $lastname )) {
$lastname = ( isset ( $this -> lastname ) ? $this -> lastname : ( isset ( $this -> name ) ? $this -> name : ( isset ( $this -> nom ) ? $this -> nom : ( isset ( $this -> societe ) ? $this -> societe : ( isset ( $this -> company ) ? $this -> company : '' )))));
}
2017-06-07 16:44:04 +02:00
2019-11-13 19:34:37 +01:00
$ret = '' ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $option ) && ! empty ( $this -> civility_code )) {
if ( $langs -> transnoentitiesnoconv ( " Civility " . $this -> civility_code ) != " Civility " . $this -> civility_code ) {
$ret .= $langs -> transnoentitiesnoconv ( " Civility " . $this -> civility_code ) . ' ' ;
} else {
$ret .= $this -> civility_code . ' ' ;
}
2017-10-07 13:09:31 +02:00
}
2012-02-01 13:21:39 +01:00
2019-11-13 19:34:37 +01:00
$ret .= dolGetFirstLastname ( $firstname , $lastname , $nameorder );
2012-02-01 13:21:39 +01:00
2019-01-27 11:55:16 +01:00
return dol_trunc ( $ret , $maxlen );
2017-10-07 13:09:31 +02:00
}
2012-02-01 13:21:39 +01:00
2020-12-14 20:40:47 +01:00
/**
* Set to upper or ucwords / lower if needed
*
* @ return void ;
*/
public function setUpperOrLowerCase ()
{
global $conf ;
if ( ! empty ( $conf -> global -> MAIN_FIRST_TO_UPPER )) {
$this -> lastname = dol_ucwords ( dol_strtolower ( $this -> lastname ));
$this -> firstname = dol_ucwords ( dol_strtolower ( $this -> firstname ));
$this -> name = dol_ucwords ( dol_strtolower ( $this -> name ));
2021-02-11 12:16:07 +01:00
$this -> name_alias = dol_ucwords ( dol_strtolower ( $this -> name_alias ));
2020-12-14 20:40:47 +01:00
}
if ( ! empty ( $conf -> global -> MAIN_ALL_TO_UPPER )) {
$this -> lastname = dol_strtoupper ( $this -> lastname );
$this -> name = dol_strtoupper ( $this -> name );
2021-02-11 12:16:07 +01:00
$this -> name_alias = dol_strtoupper ( $this -> name_alias );
2020-12-14 20:40:47 +01:00
}
if ( ! empty ( $conf -> global -> MAIN_ALL_TOWN_TO_UPPER )) {
2021-02-11 17:46:28 +01:00
$this -> address = dol_strtoupper ( $this -> address );
2020-12-14 20:40:47 +01:00
$this -> town = dol_strtoupper ( $this -> town );
}
}
2020-02-16 12:05:00 +01:00
/**
* Return clicable link of object ( with eventually picto )
*
2020-02-16 19:35:52 +01:00
* @ param string $option Where point the link ( 0 => main card , 1 , 2 => shipment , 'nolink' => No link )
* @ return string HTML Code for Kanban thumb .
2020-02-16 12:05:00 +01:00
*/
public function getKanbanView ( $option = '' )
{
$return = '<div class="box-flex-item">' ;
$return .= '<div class="info-box info-box-sm">' ;
2020-04-20 02:48:05 +02:00
$return .= '<span class="info-box-icon bg-infobox-action">' ;
2020-02-21 17:53:37 +01:00
$return .= '<i class="fa fa-dol-action"></i>' ; // Can be image
2020-02-16 12:05:00 +01:00
$return .= '</span>' ;
$return .= '<div class="info-box-content">' ;
$return .= '<span class="info-box-title">' . ( method_exists ( $this , 'getNomUrl' ) ? $this -> getNomUrl () : $this -> ref ) . '</span>' ;
$return .= '</div>' ;
$return .= '</div>' ;
$return .= '</div>' ;
return $return ;
}
2017-10-07 13:09:31 +02:00
/**
* Return full address of contact
*
* @ param int $withcountry 1 = Add country into address string
* @ param string $sep Separator to use to build string
2018-04-15 07:37:23 +02:00
* @ param int $withregion 1 = Add region into address string
2020-04-01 18:11:28 +02:00
* @ param string $extralangcode User extralanguages as value
2017-10-07 13:09:31 +02:00
* @ return string Full address string
*/
2020-04-01 18:11:28 +02:00
public function getFullAddress ( $withcountry = 0 , $sep = " \n " , $withregion = 0 , $extralangcode = '' )
2017-10-07 13:09:31 +02:00
{
2021-02-23 22:03:23 +01:00
if ( $withcountry && $this -> country_id && ( empty ( $this -> country_code ) || empty ( $this -> country ))) {
2019-11-13 19:34:37 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php' ;
$tmparray = getCountry ( $this -> country_id , 'all' );
$this -> country_code = $tmparray [ 'code' ];
2019-11-16 23:24:29 +01:00
$this -> country = $tmparray [ 'label' ];
2017-10-07 13:09:31 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $withregion && $this -> state_id && ( empty ( $this -> state_code ) || empty ( $this -> state ) || empty ( $this -> region ) || empty ( $this -> region_code ))) {
2020-09-07 10:18:17 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php' ;
$tmparray = getState ( $this -> state_id , 'all' , 0 , 1 );
2019-11-13 19:34:37 +01:00
$this -> state_code = $tmparray [ 'code' ];
$this -> state = $tmparray [ 'label' ];
$this -> region_code = $tmparray [ 'region_code' ];
$this -> region = $tmparray [ 'region' ];
2020-09-07 10:18:17 +02:00
}
2013-06-05 16:24:32 +02:00
2020-04-01 18:11:28 +02:00
return dol_format_address ( $this , $withcountry , $sep , '' , 0 , $extralangcode );
2017-10-07 13:09:31 +02:00
}
2013-04-07 17:39:08 +02:00
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
/**
* Return full address for banner
*
* @ param string $htmlkey HTML id to make banner content unique
* @ param Object $object Object ( thirdparty , thirdparty of contact for contact , null for a member )
* @ return string Full address string
*/
2019-02-25 22:27:04 +01:00
public function getBannerAddress ( $htmlkey , $object )
2017-10-07 13:09:31 +02:00
{
2020-04-01 23:15:53 +02:00
global $conf , $langs , $form , $extralanguages ;
2015-10-10 01:51:12 +02:00
2019-11-16 23:24:29 +01:00
$countriesusingstate = array ( 'AU' , 'US' , 'IN' , 'GB' , 'ES' , 'UK' , 'TR' ); // See also option MAIN_FORCE_STATE_INTO_ADDRESS
2016-06-30 17:28:13 +02:00
2019-11-16 23:24:29 +01:00
$contactid = 0 ;
$thirdpartyid = 0 ;
2020-05-29 21:59:33 +02:00
$elementforaltlanguage = $this -> element ;
2020-12-14 20:40:47 +01:00
if ( $this -> element == 'societe' ) {
2019-11-16 23:24:29 +01:00
$thirdpartyid = $this -> id ;
2017-10-07 13:09:31 +02:00
}
2020-12-14 20:40:47 +01:00
if ( $this -> element == 'contact' ) {
2019-11-16 23:24:29 +01:00
$contactid = $this -> id ;
2021-03-15 20:55:59 +01:00
$thirdpartyid = empty ( $object -> fk_soc ) ? 0 : $object -> fk_soc ;
2017-10-07 13:09:31 +02:00
}
2020-12-14 20:40:47 +01:00
if ( $this -> element == 'user' ) {
2019-11-16 23:24:29 +01:00
$contactid = $this -> contact_id ;
2021-03-15 20:55:59 +01:00
$thirdpartyid = empty ( $object -> fk_soc ) ? 0 : $object -> fk_soc ;
2017-10-07 13:09:31 +02:00
}
2016-06-30 17:28:13 +02:00
2019-12-09 00:54:42 +01:00
$out = '' ;
2016-06-30 17:28:13 +02:00
2019-11-16 23:24:29 +01:00
$outdone = 0 ;
2020-12-29 17:48:52 +01:00
$coords = $this -> getFullAddress ( 1 , ', ' , ( ! empty ( $conf -> global -> MAIN_SHOW_REGION_IN_STATE_SELECT ) ? $conf -> global -> MAIN_SHOW_REGION_IN_STATE_SELECT : 0 ));
2021-02-23 22:03:23 +01:00
if ( $coords ) {
if ( ! empty ( $conf -> use_javascript_ajax )) {
2020-04-01 18:11:28 +02:00
// Add picto with tooltip on map
2019-09-25 12:18:19 +02:00
$namecoords = '' ;
2021-02-23 22:03:23 +01:00
if ( $this -> element == 'contact' && ! empty ( $conf -> global -> MAIN_SHOW_COMPANY_NAME_IN_BANNER_ADDRESS )) {
2019-11-16 23:24:29 +01:00
$namecoords .= $object -> name . '<br>' ;
2019-09-25 12:18:19 +02:00
}
2019-11-16 23:24:29 +01:00
$namecoords .= $this -> getFullName ( $langs , 1 ) . '<br>' . $coords ;
2015-10-11 15:21:21 +02:00
// hideonsmatphone because copyToClipboard call jquery dialog that does not work with jmobile
2019-11-16 23:24:29 +01:00
$out .= '<a href="#" class="hideonsmartphone" onclick="return copyToClipboard(\'' . dol_escape_js ( $namecoords ) . '\',\'' . dol_escape_js ( $langs -> trans ( " HelpCopyToClipboard " )) . '\');">' ;
2020-04-22 17:59:54 +02:00
$out .= img_picto ( $langs -> trans ( " Address " ), 'map-marker-alt' );
2019-11-16 23:24:29 +01:00
$out .= '</a> ' ;
2015-10-11 15:21:21 +02:00
}
2021-03-01 20:37:16 +01:00
$out .= dol_print_address ( $coords , 'address_' . $htmlkey . '_' . $this -> id , $this -> element , $this -> id , 1 , ', ' );
2015-10-10 01:51:12 +02:00
$outdone ++ ;
$outdone ++ ;
2020-04-01 18:11:28 +02:00
2020-04-01 23:15:53 +02:00
// List of extra languages
$arrayoflangcode = array ();
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> PDF_USE_ALSO_LANGUAGE_CODE )) {
$arrayoflangcode [] = $conf -> global -> PDF_USE_ALSO_LANGUAGE_CODE ;
}
2020-04-01 18:11:28 +02:00
2020-04-01 23:15:53 +02:00
if ( is_array ( $arrayoflangcode ) && count ( $arrayoflangcode )) {
2020-04-10 10:59:32 +02:00
if ( ! is_object ( $extralanguages )) {
2020-04-01 23:15:53 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/extralanguages.class.php' ;
$extralanguages = new ExtraLanguages ( $this -> db );
}
2020-05-29 21:59:33 +02:00
$extralanguages -> fetch_name_extralanguages ( $elementforaltlanguage );
2020-04-01 23:15:53 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $extralanguages -> attributes [ $elementforaltlanguage ][ 'address' ]) || ! empty ( $extralanguages -> attributes [ $elementforaltlanguage ][ 'town' ])) {
2020-09-07 10:18:17 +02:00
$out .= " <!-- alternatelanguage for ' " . $elementforaltlanguage . " ' set to fields ' " . join ( ',' , $extralanguages -> attributes [ $elementforaltlanguage ]) . " ' --> \n " ;
2020-04-01 23:15:53 +02:00
$this -> fetchValuesForExtraLanguages ();
2021-02-23 22:03:23 +01:00
if ( ! is_object ( $form )) {
$form = new Form ( $this -> db );
}
2020-04-01 18:11:28 +02:00
$htmltext = '' ;
// If there is extra languages
2020-04-10 10:59:32 +02:00
foreach ( $arrayoflangcode as $extralangcode ) {
$s = picto_from_langcode ( $extralangcode , 'class="pictoforlang paddingright"' );
2020-04-01 18:11:28 +02:00
$coords = $this -> getFullAddress ( 1 , ', ' , $conf -> global -> MAIN_SHOW_REGION_IN_STATE_SELECT , $extralangcode );
$htmltext .= $s . dol_print_address ( $coords , 'address_' . $htmlkey . '_' . $this -> id , $this -> element , $this -> id , 1 , ', ' );
}
$out .= $form -> textwithpicto ( '' , $htmltext , - 1 , 'language' , 'opacitymedium paddingleft' );
}
}
2015-10-10 01:51:12 +02:00
}
2016-02-22 13:28:08 +01:00
2019-11-16 23:24:29 +01:00
if ( ! in_array ( $this -> country_code , $countriesusingstate ) && empty ( $conf -> global -> MAIN_FORCE_STATE_INTO_ADDRESS ) // If MAIN_FORCE_STATE_INTO_ADDRESS is on, state is already returned previously with getFullAddress
2021-02-23 22:03:23 +01:00
&& empty ( $conf -> global -> SOCIETE_DISABLE_STATE ) && $this -> state ) {
2020-09-07 10:18:17 +02:00
if ( ! empty ( $conf -> global -> MAIN_SHOW_REGION_IN_STATE_SELECT ) && $conf -> global -> MAIN_SHOW_REGION_IN_STATE_SELECT == 1 && $this -> region ) {
$out .= ( $outdone ? ' - ' : '' ) . $this -> region . ' - ' . $this -> state ;
} else {
$out .= ( $outdone ? ' - ' : '' ) . $this -> state ;
}
2015-10-10 01:51:12 +02:00
$outdone ++ ;
}
2015-10-11 12:27:04 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $this -> phone ) || ! empty ( $this -> phone_pro ) || ! empty ( $this -> phone_mobile ) || ! empty ( $this -> phone_perso ) || ! empty ( $this -> fax ) || ! empty ( $this -> office_phone ) || ! empty ( $this -> user_mobile ) || ! empty ( $this -> office_fax )) {
$out .= ( $outdone ? '<br>' : '' );
}
2019-11-16 23:24:29 +01:00
if ( ! empty ( $this -> phone ) && empty ( $this -> phone_pro )) { // For objects that store pro phone into ->phone
2020-12-14 20:40:47 +01:00
$out .= dol_print_phone ( $this -> phone , $this -> country_code , $contactid , $thirdpartyid , 'AC_TEL' , ' ' , 'phone' , $langs -> trans ( " PhonePro " ));
$outdone ++ ;
2015-10-11 15:21:21 +02:00
}
2019-11-16 23:24:29 +01:00
if ( ! empty ( $this -> phone_pro )) {
2020-12-14 20:40:47 +01:00
$out .= dol_print_phone ( $this -> phone_pro , $this -> country_code , $contactid , $thirdpartyid , 'AC_TEL' , ' ' , 'phone' , $langs -> trans ( " PhonePro " ));
$outdone ++ ;
2015-10-11 15:21:21 +02:00
}
2019-11-16 23:24:29 +01:00
if ( ! empty ( $this -> phone_mobile )) {
2020-12-14 20:40:47 +01:00
$out .= dol_print_phone ( $this -> phone_mobile , $this -> country_code , $contactid , $thirdpartyid , 'AC_TEL' , ' ' , 'mobile' , $langs -> trans ( " PhoneMobile " ));
$outdone ++ ;
2017-12-19 16:16:31 +01:00
}
2019-11-16 23:24:29 +01:00
if ( ! empty ( $this -> phone_perso )) {
2020-12-14 20:40:47 +01:00
$out .= dol_print_phone ( $this -> phone_perso , $this -> country_code , $contactid , $thirdpartyid , 'AC_TEL' , ' ' , 'phone' , $langs -> trans ( " PhonePerso " ));
2015-10-10 01:51:12 +02:00
$outdone ++ ;
}
2019-11-16 23:24:29 +01:00
if ( ! empty ( $this -> office_phone )) {
2020-12-14 20:40:47 +01:00
$out .= dol_print_phone ( $this -> office_phone , $this -> country_code , $contactid , $thirdpartyid , 'AC_TEL' , ' ' , 'phone' , $langs -> trans ( " PhonePro " ));
2015-10-10 01:51:12 +02:00
$outdone ++ ;
}
2019-11-16 23:24:29 +01:00
if ( ! empty ( $this -> user_mobile )) {
2020-12-14 20:40:47 +01:00
$out .= dol_print_phone ( $this -> user_mobile , $this -> country_code , $contactid , $thirdpartyid , 'AC_TEL' , ' ' , 'mobile' , $langs -> trans ( " PhoneMobile " ));
2015-11-28 14:43:16 +01:00
$outdone ++ ;
2015-10-11 15:21:21 +02:00
}
2019-11-16 23:24:29 +01:00
if ( ! empty ( $this -> fax )) {
2020-12-14 20:40:47 +01:00
$out .= dol_print_phone ( $this -> fax , $this -> country_code , $contactid , $thirdpartyid , 'AC_FAX' , ' ' , 'fax' , $langs -> trans ( " Fax " ));
2018-12-11 09:17:02 +01:00
$outdone ++ ;
2015-10-11 15:21:21 +02:00
}
2019-11-16 23:24:29 +01:00
if ( ! empty ( $this -> office_fax )) {
2020-12-14 20:40:47 +01:00
$out .= dol_print_phone ( $this -> office_fax , $this -> country_code , $contactid , $thirdpartyid , 'AC_FAX' , ' ' , 'fax' , $langs -> trans ( " Fax " ));
2018-10-12 11:02:03 +02:00
$outdone ++ ;
2017-12-19 16:16:31 +01:00
}
2016-06-30 17:28:13 +02:00
2021-02-23 22:03:23 +01:00
if ( $out ) {
$out .= '<div style="clear: both;"></div>' ;
}
2019-11-16 23:24:29 +01:00
$outdone = 0 ;
2020-12-14 20:40:47 +01:00
if ( ! empty ( $this -> email )) {
2019-11-16 23:24:29 +01:00
$out .= dol_print_email ( $this -> email , $this -> id , $object -> id , 'AC_EMAIL' , 0 , 0 , 1 );
2015-11-28 14:43:16 +01:00
$outdone ++ ;
2015-10-10 01:51:12 +02:00
}
2020-12-14 20:40:47 +01:00
if ( ! empty ( $this -> url )) {
2020-09-07 10:18:17 +02:00
//$out.=dol_print_url($this->url,'_goout',0,1);//steve changed to blank
$out .= dol_print_url ( $this -> url , '_blank' , 0 , 1 );
2019-02-25 09:07:58 +01:00
$outdone ++ ;
2015-10-10 01:51:12 +02:00
}
2016-06-30 17:28:13 +02:00
2020-12-14 20:40:47 +01:00
if ( ! empty ( $conf -> socialnetworks -> enabled )) {
2019-12-09 00:54:42 +01:00
$outsocialnetwork = '' ;
2016-06-30 17:28:13 +02:00
2021-11-28 15:43:22 +01:00
if ( ! empty ( $this -> socialnetworks ) && is_array ( $this -> socialnetworks ) && count ( $this -> socialnetworks ) > 0 ) {
2020-12-14 20:40:47 +01:00
$socialnetworksdict = getArrayOfSocialNetworks ();
2019-09-11 22:27:09 +02:00
foreach ( $this -> socialnetworks as $key => $value ) {
2020-12-14 20:40:47 +01:00
if ( $value ) {
$outsocialnetwork .= dol_print_socialnetworks ( $value , $this -> id , $object -> id , $key , $socialnetworksdict );
}
2019-09-11 22:27:09 +02:00
$outdone ++ ;
}
2020-12-14 20:40:47 +01:00
} else { // Old code to remove
2021-03-15 20:31:55 +01:00
if ( ! empty ( $this -> skype )) {
2021-02-23 22:03:23 +01:00
$outsocialnetwork .= dol_print_socialnetworks ( $this -> skype , $this -> id , $object -> id , 'skype' );
}
2019-09-11 22:27:09 +02:00
$outdone ++ ;
2021-03-15 20:31:55 +01:00
if ( ! empty ( $this -> jabberid )) {
2021-02-23 22:03:23 +01:00
$outsocialnetwork .= dol_print_socialnetworks ( $this -> jabberid , $this -> id , $object -> id , 'jabber' );
}
2019-09-11 22:27:09 +02:00
$outdone ++ ;
2021-03-15 20:31:55 +01:00
if ( ! empty ( $this -> twitter )) {
2021-02-23 22:03:23 +01:00
$outsocialnetwork .= dol_print_socialnetworks ( $this -> twitter , $this -> id , $object -> id , 'twitter' );
}
2019-09-11 22:27:09 +02:00
$outdone ++ ;
2021-03-15 20:31:55 +01:00
if ( ! empty ( $this -> facebook )) {
2021-02-23 22:03:23 +01:00
$outsocialnetwork .= dol_print_socialnetworks ( $this -> facebook , $this -> id , $object -> id , 'facebook' );
}
2019-09-11 22:27:09 +02:00
$outdone ++ ;
2021-03-15 20:31:55 +01:00
if ( ! empty ( $this -> linkedin )) {
2021-02-23 22:03:23 +01:00
$outsocialnetwork .= dol_print_socialnetworks ( $this -> linkedin , $this -> id , $object -> id , 'linkedin' );
}
2019-09-11 22:27:09 +02:00
$outdone ++ ;
}
2016-06-30 17:28:13 +02:00
2019-12-09 00:54:42 +01:00
if ( $outsocialnetwork ) {
$out .= '<div style="clear: both;">' . $outsocialnetwork . '</div>' ;
}
}
2016-06-30 17:28:13 +02:00
2021-02-23 22:03:23 +01:00
if ( $out ) {
return '<!-- BEGIN part to show address block -->' . " \n " . $out . '<!-- END Part to show address block -->' . " \n " ;
} else {
return '' ;
}
2017-10-07 13:09:31 +02:00
}
2011-11-07 15:04:02 +01:00
2017-10-30 00:45:47 +01:00
/**
* Return the link of last main doc file for direct public download .
*
* @ param string $modulepart Module related to document
* @ param int $initsharekey Init the share key if it was not yet defined
2018-02-15 19:49:50 +01:00
* @ param int $relativelink 0 = Return full external link , 1 = Return link relative to root of file
2017-10-30 00:45:47 +01:00
* @ return string Link or empty string if there is no download link
*/
2019-02-25 22:27:04 +01:00
public function getLastMainDocLink ( $modulepart , $initsharekey = 0 , $relativelink = 0 )
2017-10-30 00:45:47 +01:00
{
global $user , $dolibarr_main_url_root ;
2011-12-07 15:06:29 +01:00
2021-02-23 22:03:23 +01:00
if ( empty ( $this -> last_main_doc )) {
2019-11-13 19:34:37 +01:00
return '' ; // No way to known which document name to use
2017-10-30 00:45:47 +01:00
}
2011-11-07 15:04:02 +01:00
2017-10-30 00:45:47 +01:00
include_once DOL_DOCUMENT_ROOT . '/ecm/class/ecmfiles.class.php' ;
2019-11-13 19:34:37 +01:00
$ecmfile = new EcmFiles ( $this -> db );
2017-10-30 00:45:47 +01:00
$result = $ecmfile -> fetch ( 0 , '' , $this -> last_main_doc );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2017-10-30 00:45:47 +01:00
$this -> error = $ecmfile -> error ;
$this -> errors = $ecmfile -> errors ;
return - 1 ;
}
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( empty ( $ecmfile -> id )) {
2017-10-30 00:45:47 +01:00
// Add entry into index
2021-02-23 22:03:23 +01:00
if ( $initsharekey ) {
2017-10-30 00:45:47 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php' ;
// TODO We can't, we dont' have full path of file, only last_main_doc adn ->element, so we must rebuild full path first
/*
$ecmfile -> filepath = $rel_dir ;
$ecmfile -> filename = $filename ;
$ecmfile -> label = md5_file ( dol_osencode ( $destfull )); // hash of file content
$ecmfile -> fullpath_orig = '' ;
$ecmfile -> gen_or_uploaded = 'generated' ;
$ecmfile -> description = '' ; // indexed content
2021-06-29 19:49:26 +02:00
$ecmfile -> keywords = '' ; // keyword content
2017-10-30 00:45:47 +01:00
$ecmfile -> share = getRandomPassword ( true );
$result = $ecmfile -> create ( $user );
if ( $result < 0 )
{
$this -> error = $ecmfile -> error ;
$this -> errors = $ecmfile -> errors ;
}
*/
2021-02-23 22:03:23 +01:00
} else {
return '' ;
}
} elseif ( empty ( $ecmfile -> share )) {
2017-10-30 00:45:47 +01:00
// Add entry into index
2021-02-23 22:03:23 +01:00
if ( $initsharekey ) {
2017-10-30 00:45:47 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php' ;
$ecmfile -> share = getRandomPassword ( true );
$ecmfile -> update ( $user );
2021-02-23 22:03:23 +01:00
} else {
return '' ;
}
2017-10-30 00:45:47 +01:00
}
// Define $urlwithroot
2019-11-13 19:34:37 +01:00
$urlwithouturlroot = preg_replace ( '/' . preg_quote ( DOL_URL_ROOT , '/' ) . '$/i' , '' , trim ( $dolibarr_main_url_root ));
2019-01-06 05:16:50 +01:00
// This is to use external domain name found into config file
//if (DOL_URL_ROOT && ! preg_match('/\/$/', $urlwithouturlroot) && ! preg_match('/^\//', DOL_URL_ROOT)) $urlwithroot=$urlwithouturlroot.'/'.DOL_URL_ROOT;
//else
2019-11-13 19:34:37 +01:00
$urlwithroot = $urlwithouturlroot . DOL_URL_ROOT ;
2017-10-30 00:45:47 +01:00
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
2017-06-07 16:44:04 +02:00
2019-11-13 19:34:37 +01:00
$forcedownload = 0 ;
2017-07-12 13:25:18 +02:00
2019-11-13 19:34:37 +01:00
$paramlink = '' ;
2017-11-19 20:44:57 +01:00
//if (! empty($modulepart)) $paramlink.=($paramlink?'&':'').'modulepart='.$modulepart; // For sharing with hash (so public files), modulepart is not required.
//if (! empty($ecmfile->entity)) $paramlink.='&entity='.$ecmfile->entity; // For sharing with hash (so public files), entity is not required.
//$paramlink.=($paramlink?'&':'').'file='.urlencode($filepath); // No need of name of file for public link, we will use the hash
2021-02-23 22:03:23 +01:00
if ( ! empty ( $ecmfile -> share )) {
$paramlink .= ( $paramlink ? '&' : '' ) . 'hashp=' . $ecmfile -> share ; // Hash for public share
}
if ( $forcedownload ) {
$paramlink .= ( $paramlink ? '&' : '' ) . 'attachment=1' ;
}
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( $relativelink ) {
2019-11-13 19:34:37 +01:00
$linktoreturn = 'document.php' . ( $paramlink ? '?' . $paramlink : '' );
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$linktoreturn = $urlwithroot . '/document.php' . ( $paramlink ? '?' . $paramlink : '' );
2018-02-15 19:49:50 +01:00
}
2014-01-16 11:43:59 +01:00
2017-10-30 00:45:47 +01:00
// Here $ecmfile->share is defined
2018-02-15 19:49:50 +01:00
return $linktoreturn ;
2017-10-30 00:45:47 +01:00
}
2011-11-07 15:04:02 +01:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Add a link between element $this -> element and a contact
*
2020-12-14 20:40:47 +01:00
* @ param int $fk_socpeople Id of thirdparty contact ( if source = 'external' ) or id of user ( if souce = 'internal' ) to link
* @ param int | string $type_contact Type of contact ( code or id ) . Must be id or code found into table llx_c_type_contact . For example : SALESREPFOLL
* @ param string $source external = Contact extern ( llx_socpeople ), internal = Contact intern ( llx_user )
* @ param int $notrigger Disable all triggers
2021-02-09 17:52:33 +01:00
* @ return int < 0 if KO , 0 if already added , > 0 if OK
2017-10-07 13:09:31 +02:00
*/
2019-02-25 22:27:04 +01:00
public function add_contact ( $fk_socpeople , $type_contact , $source = 'external' , $notrigger = 0 )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2019-11-13 19:34:37 +01:00
global $user , $langs ;
2011-12-07 15:06:29 +01:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::add_contact $fk_socpeople , $type_contact , $source , $notrigger " );
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
// Check parameters
2021-02-23 22:03:23 +01:00
if ( $fk_socpeople <= 0 ) {
2017-10-07 13:09:31 +02:00
$langs -> load ( " errors " );
2019-11-13 19:34:37 +01:00
$this -> error = $langs -> trans ( " ErrorWrongValueForParameterX " , " 1 " );
2019-01-27 11:55:16 +01:00
dol_syslog ( get_class ( $this ) . " ::add_contact " . $this -> error , LOG_ERR );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2021-02-23 22:03:23 +01:00
if ( ! $type_contact ) {
2017-10-07 13:09:31 +02:00
$langs -> load ( " errors " );
2019-11-13 19:34:37 +01:00
$this -> error = $langs -> trans ( " ErrorWrongValueForParameterX " , " 2 " );
2019-01-27 11:55:16 +01:00
dol_syslog ( get_class ( $this ) . " ::add_contact " . $this -> error , LOG_ERR );
2017-10-07 13:09:31 +02:00
return - 2 ;
}
2011-11-07 15:04:02 +01:00
2019-11-13 19:34:37 +01:00
$id_type_contact = 0 ;
2021-02-23 22:03:23 +01:00
if ( is_numeric ( $type_contact )) {
2019-11-13 19:34:37 +01:00
$id_type_contact = $type_contact ;
2020-05-21 15:05:19 +02:00
} else {
2018-04-15 07:37:23 +02:00
// We look for id type_contact
2017-10-07 13:09:31 +02:00
$sql = " SELECT tc.rowid " ;
2019-11-13 19:34:37 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_type_contact as tc " ;
$sql .= " WHERE tc.element=' " . $this -> db -> escape ( $this -> element ) . " ' " ;
$sql .= " AND tc.source=' " . $this -> db -> escape ( $source ) . " ' " ;
$sql .= " AND tc.code=' " . $this -> db -> escape ( $type_contact ) . " ' AND tc.active=1 " ;
2017-10-07 13:09:31 +02:00
//print $sql;
2019-11-13 19:34:37 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-23 22:03:23 +01:00
if ( $obj ) {
$id_type_contact = $obj -> rowid ;
}
2017-10-07 13:09:31 +02:00
}
}
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( $id_type_contact == 0 ) {
2019-11-13 19:34:37 +01:00
$this -> error = 'CODE_NOT_VALID_FOR_THIS_ELEMENT' ;
2018-06-01 17:20:49 +02:00
dol_syslog ( " CODE_NOT_VALID_FOR_THIS_ELEMENT: Code type of contact ' " . $type_contact . " ' does not exists or is not active for element " . $this -> element . " , we can ignore it " );
2017-10-07 13:09:31 +02:00
return - 3 ;
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$datecreate = dol_now ();
2011-11-07 15:04:02 +01:00
2018-04-15 07:37:23 +02:00
// Socpeople must have already been added by some trigger, then we have to check it to avoid DB_ERROR_RECORD_ALREADY_EXISTS error
2019-11-13 19:34:37 +01:00
$TListeContacts = $this -> liste_contact ( - 1 , $source );
$already_added = false ;
if ( is_array ( $TListeContacts ) && ! empty ( $TListeContacts )) {
foreach ( $TListeContacts as $array_contact ) {
2019-10-01 16:26:55 +02:00
if ( $array_contact [ 'status' ] == 4 && $array_contact [ 'id' ] == $fk_socpeople && $array_contact [ 'fk_c_type_contact' ] == $id_type_contact ) {
2019-11-13 19:34:37 +01:00
$already_added = true ;
2017-10-07 13:09:31 +02:00
break ;
2016-01-22 22:35:32 +01:00
}
2017-10-07 13:09:31 +02:00
}
}
2011-11-07 15:04:02 +01:00
2019-11-13 19:34:37 +01:00
if ( ! $already_added ) {
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2012-02-01 13:21:39 +01:00
2018-04-15 07:37:23 +02:00
// Insert into database
2017-10-07 13:09:31 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " element_contact " ;
2019-11-13 19:34:37 +01:00
$sql .= " (element_id, fk_socpeople, datecreate, statut, fk_c_type_contact) " ;
2021-03-24 19:53:31 +01:00
$sql .= " VALUES ( " . $this -> id . " , " . (( int ) $fk_socpeople ) . " , " ;
2019-11-13 19:34:37 +01:00
$sql .= " ' " . $this -> db -> idate ( $datecreate ) . " ' " ;
2021-03-24 19:53:31 +01:00
$sql .= " , 4, " . (( int ) $id_type_contact );
2019-11-13 19:34:37 +01:00
$sql .= " ) " ;
2011-11-08 00:17:20 +01:00
2019-11-13 19:34:37 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
if ( ! $notrigger ) {
2019-11-13 19:34:37 +01:00
$result = $this -> call_trigger ( strtoupper ( $this -> element ) . '_ADD_CONTACT' , $user );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
2012-01-23 22:23:00 +01:00
}
2008-02-24 17:46:42 +01:00
}
2012-01-23 22:23:00 +01:00
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 22:03:23 +01:00
if ( $this -> db -> errno () == 'DB_ERROR_RECORD_ALREADY_EXISTS' ) {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> errno ();
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 2 ;
2020-05-21 15:05:19 +02:00
} else {
2021-06-22 17:09:02 +02:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
2012-01-23 22:23:00 +01:00
}
}
2021-02-09 17:52:33 +01:00
} else {
return 0 ;
}
2017-10-07 13:09:31 +02:00
}
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Copy contact from one element to current
*
* @ param CommonObject $objFrom Source element
* @ param string $source Nature of contact ( 'internal' or 'external' )
* @ return int > 0 if OK , < 0 if KO
*/
2019-02-25 22:27:04 +01:00
public function copy_linked_contact ( $objFrom , $source = 'internal' )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
$contacts = $objFrom -> liste_contact ( - 1 , $source );
2021-02-23 22:03:23 +01:00
foreach ( $contacts as $contact ) {
if ( $this -> add_contact ( $contact [ 'id' ], $contact [ 'fk_c_type_contact' ], $contact [ 'source' ]) < 0 ) {
2017-10-07 13:09:31 +02:00
return - 1 ;
2012-09-15 15:33:00 +02:00
}
2012-01-23 22:23:00 +01:00
}
2016-04-09 15:07:55 +02:00
return 1 ;
2012-01-23 22:23:00 +01:00
}
2011-11-07 15:04:02 +01:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2012-01-23 22:23:00 +01:00
/**
2017-10-07 13:09:31 +02:00
* Update a link to contact line
2012-02-01 13:21:39 +01:00
*
2017-10-07 13:09:31 +02:00
* @ param int $rowid Id of line contact - element
* @ param int $statut New status of link
* @ param int $type_contact_id Id of contact type ( not modified if 0 )
* @ param int $fk_socpeople Id of soc_people to update ( not modified if 0 )
* @ return int < 0 if KO , >= 0 if OK
2012-01-23 22:23:00 +01:00
*/
2019-02-25 22:27:04 +01:00
public function update_contact ( $rowid , $statut , $type_contact_id = 0 , $fk_socpeople = 0 )
2012-01-23 22:23:00 +01:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2018-04-15 07:37:23 +02:00
// Insert into database
2017-10-07 13:09:31 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " element_contact set " ;
2019-11-13 19:34:37 +01:00
$sql .= " statut = " . $statut ;
2021-02-23 22:03:23 +01:00
if ( $type_contact_id ) {
$sql .= " , fk_c_type_contact = " . (( int ) $type_contact_id );
}
if ( $fk_socpeople ) {
$sql .= " , fk_socpeople = " . (( int ) $fk_socpeople );
}
2021-03-30 17:53:25 +02:00
$sql .= " where rowid = " . (( int ) $rowid );
2019-11-13 19:34:37 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
return 0 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
return - 1 ;
2008-02-24 17:46:42 +01:00
}
}
2011-11-07 15:04:02 +01:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2010-08-20 19:57:25 +02:00
/**
2017-10-07 13:09:31 +02:00
* Delete a link to contact line
2011-08-28 14:30:15 +02:00
*
2017-10-07 13:09:31 +02:00
* @ param int $rowid Id of contact link line to delete
* @ param int $notrigger Disable all triggers
* @ return int > 0 if OK , < 0 if KO
2010-08-20 19:57:25 +02:00
*/
2019-02-25 22:27:04 +01:00
public function delete_contact ( $rowid , $notrigger = 0 )
2010-08-20 19:57:25 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
global $user ;
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " element_contact " ;
2021-03-30 11:26:49 +02:00
$sql .= " WHERE rowid = " . (( int ) $rowid );
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_contact " , LOG_DEBUG );
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
if ( ! $notrigger ) {
2019-11-13 19:34:37 +01:00
$result = $this -> call_trigger ( strtoupper ( $this -> element ) . '_DELETE_CONTACT' , $user );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2021-03-01 20:37:16 +01:00
$this -> db -> rollback ();
return - 1 ;
2021-02-23 22:03:23 +01:00
}
2017-10-07 13:09:31 +02:00
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
2010-08-20 19:57:25 +02:00
}
}
2011-11-07 15:04:02 +01:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Delete all links between an object $this and all its contacts
*
* @ param string $source '' or 'internal' or 'external'
* @ param string $code Type of contact ( code or id )
* @ return int > 0 if OK , < 0 if KO
*/
2019-02-25 22:27:04 +01:00
public function delete_linked_contact ( $source = '' , $code = '' )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
$temp = array ();
2019-01-27 11:55:16 +01:00
$typeContact = $this -> liste_type_contact ( $source , '' , 0 , 0 , $code );
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
foreach ( $typeContact as $key => $value ) {
2019-01-27 11:55:16 +01:00
array_push ( $temp , $key );
2013-04-14 23:42:24 +02:00
}
2017-10-07 13:09:31 +02:00
$listId = implode ( " , " , $temp );
2017-06-07 16:44:04 +02:00
2017-10-07 13:09:31 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " element_contact " ;
2019-11-13 19:34:37 +01:00
$sql .= " WHERE element_id = " . $this -> id ;
2021-02-23 22:03:23 +01:00
if ( $listId ) {
2021-03-22 11:30:18 +01:00
$sql .= " AND fk_c_type_contact IN ( " . $this -> db -> sanitize ( $listId ) . " ) " ;
2021-02-23 22:03:23 +01:00
}
2017-10-06 12:55:48 +02:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_linked_contact " , LOG_DEBUG );
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
return - 1 ;
2017-10-06 12:55:48 +02:00
}
2017-10-07 13:09:31 +02:00
}
2017-10-06 12:55:48 +02:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Get array of all contacts for an object
*
2019-11-02 11:36:48 +01:00
* @ param int $status Status of links to get ( - 1 = all )
2020-02-17 12:22:52 +01:00
* @ param string $source Source of contact : 'external' or 'thirdparty' ( llx_socpeople ) or 'internal' ( llx_user )
2017-10-07 13:09:31 +02:00
* @ param int $list 0 : Return array contains all properties , 1 : Return array contains just id
* @ param string $code Filter on this code of contact type ( 'SHIPPING' , 'BILLING' , ... )
2018-09-30 17:59:04 +02:00
* @ return array | int Array of contacts , - 1 if error
2017-10-07 13:09:31 +02:00
*/
2019-11-02 11:36:48 +01:00
public function liste_contact ( $status = - 1 , $source = 'external' , $list = 0 , $code = '' )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
global $langs ;
2019-11-16 23:24:29 +01:00
$tab = array ();
$sql = " SELECT ec.rowid, ec.statut as statuslink, ec.fk_socpeople as id, ec.fk_c_type_contact " ; // This field contains id of llx_socpeople or id of llx_user
2021-02-23 22:03:23 +01:00
if ( $source == 'internal' ) {
$sql .= " , '-1' as socid, t.statut as statuscontact, t.login, t.photo " ;
}
if ( $source == 'external' || $source == 'thirdparty' ) {
$sql .= " , t.fk_soc as socid, t.statut as statuscontact " ;
}
2019-11-16 23:24:29 +01:00
$sql .= " , t.civility as civility, t.lastname as lastname, t.firstname, t.email " ;
$sql .= " , tc.source, tc.element, tc.code, tc.libelle " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " c_type_contact tc " ;
$sql .= " , " . MAIN_DB_PREFIX . " element_contact ec " ;
2021-02-23 22:03:23 +01:00
if ( $source == 'internal' ) {
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " user t on ec.fk_socpeople = t.rowid " ;
}
if ( $source == 'external' || $source == 'thirdparty' ) {
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " socpeople t on ec.fk_socpeople = t.rowid " ;
}
2021-09-18 15:05:31 +02:00
$sql .= " WHERE ec.element_id = " . (( int ) $this -> id );
2019-11-16 23:24:29 +01:00
$sql .= " AND ec.fk_c_type_contact=tc.rowid " ;
$sql .= " AND tc.element=' " . $this -> db -> escape ( $this -> element ) . " ' " ;
2021-02-23 22:03:23 +01:00
if ( $code ) {
$sql .= " AND tc.code = ' " . $this -> db -> escape ( $code ) . " ' " ;
}
if ( $source == 'internal' ) {
$sql .= " AND tc.source = 'internal' " ;
}
if ( $source == 'external' || $source == 'thirdparty' ) {
$sql .= " AND tc.source = 'external' " ;
}
2019-11-16 23:24:29 +01:00
$sql .= " AND tc.active=1 " ;
2021-02-23 22:03:23 +01:00
if ( $status >= 0 ) {
2021-06-09 15:36:47 +02:00
$sql .= " AND ec.statut = " . (( int ) $status );
2021-02-23 22:03:23 +01:00
}
2019-11-16 23:24:29 +01:00
$sql .= " ORDER BY t.lastname ASC " ;
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::liste_contact " , LOG_DEBUG );
2019-11-16 23:24:29 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2019-11-16 23:24:29 +01:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2013-07-04 00:11:40 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $list ) {
2019-11-16 23:24:29 +01:00
$transkey = " TypeContact_ " . $obj -> element . " _ " . $obj -> source . " _ " . $obj -> code ;
$libelle_type = ( $langs -> trans ( $transkey ) != $transkey ? $langs -> trans ( $transkey ) : $obj -> libelle );
2021-03-20 10:50:02 +01:00
$tab [ $i ] = array (
'source' => $obj -> source ,
'socid' => $obj -> socid ,
'id' => $obj -> id ,
'nom' => $obj -> lastname , // For backward compatibility
'civility' => $obj -> civility ,
'lastname' => $obj -> lastname ,
'firstname' => $obj -> firstname ,
'email' => $obj -> email ,
'login' => ( empty ( $obj -> login ) ? '' : $obj -> login ),
'photo' => ( empty ( $obj -> photo ) ? '' : $obj -> photo ),
'statuscontact' => $obj -> statuscontact ,
'rowid' => $obj -> rowid ,
'code' => $obj -> code ,
'libelle' => $libelle_type ,
'status' => $obj -> statuslink ,
'fk_c_type_contact' => $obj -> fk_c_type_contact
);
2020-05-21 15:05:19 +02:00
} else {
2019-11-16 23:24:29 +01:00
$tab [ $i ] = $obj -> id ;
2017-10-07 13:09:31 +02:00
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$i ++ ;
}
2017-09-13 19:27:21 +02:00
2017-10-07 13:09:31 +02:00
return $tab ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
return - 1 ;
}
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
/**
* Update status of a contact linked to object
*
* @ param int $rowid Id of link between object and contact
* @ return int < 0 if KO , >= 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function swapContactStatus ( $rowid )
2017-10-07 13:09:31 +02:00
{
$sql = " SELECT ec.datecreate, ec.statut, ec.fk_socpeople, ec.fk_c_type_contact, " ;
2019-11-13 19:34:37 +01:00
$sql .= " tc.code, tc.libelle " ;
$sql .= " FROM ( " . MAIN_DB_PREFIX . " element_contact as ec, " . MAIN_DB_PREFIX . " c_type_contact as tc) " ;
2021-03-29 15:07:23 +02:00
$sql .= " WHERE ec.rowid = " . (( int ) $rowid );
2019-11-13 19:34:37 +01:00
$sql .= " AND ec.fk_c_type_contact=tc.rowid " ;
$sql .= " AND tc.element = ' " . $this -> db -> escape ( $this -> element ) . " ' " ;
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::swapContactStatus " , LOG_DEBUG );
2019-11-13 19:34:37 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
$newstatut = ( $obj -> statut == 4 ) ? 5 : 4 ;
$result = $this -> update_contact ( $rowid , $newstatut );
$this -> db -> free ( $resql );
return $result ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> error ();
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
return - 1 ;
2017-10-06 10:42:25 +02:00
}
2017-10-07 13:09:31 +02:00
}
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Return array with list of possible values for type of contacts
*
* @ param string $source 'internal' , 'external' or 'all'
* @ param string $order Sort order by : 'position' , 'code' , 'rowid' ...
* @ param int $option 0 = Return array id -> label , 1 = Return array code -> label
* @ param int $activeonly 0 = all status of contact , 1 = only the active
* @ param string $code Type of contact ( Example : 'CUSTOMER' , 'SERVICE' )
* @ return array Array list of type of contacts ( id -> label if option = 0 , code -> label if option = 1 )
*/
2019-02-25 22:27:04 +01:00
public function liste_type_contact ( $source = 'internal' , $order = 'position' , $option = 0 , $activeonly = 0 , $code = '' )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
global $langs ;
2021-02-23 22:03:23 +01:00
if ( empty ( $order )) {
$order = 'position' ;
}
if ( $order == 'position' ) {
$order .= ',code' ;
}
2017-10-07 13:09:31 +02:00
$tab = array ();
$sql = " SELECT DISTINCT tc.rowid, tc.code, tc.libelle, tc.position " ;
2019-11-13 19:34:37 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_type_contact as tc " ;
$sql .= " WHERE tc.element=' " . $this -> db -> escape ( $this -> element ) . " ' " ;
2021-02-23 22:03:23 +01:00
if ( $activeonly == 1 ) {
$sql .= " AND tc.active=1 " ; // only the active types
}
if ( ! empty ( $source ) && $source != 'all' ) {
$sql .= " AND tc.source=' " . $this -> db -> escape ( $source ) . " ' " ;
}
if ( ! empty ( $code )) {
$sql .= " AND tc.code=' " . $this -> db -> escape ( $code ) . " ' " ;
}
2019-11-13 19:34:37 +01:00
$sql .= $this -> db -> order ( $order , 'ASC' );
2017-10-07 13:09:31 +02:00
//print "sql=".$sql;
2019-11-13 19:34:37 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2019-11-13 19:34:37 +01:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2019-11-13 19:34:37 +01:00
$transkey = " TypeContact_ " . $this -> element . " _ " . $source . " _ " . $obj -> code ;
$libelle_type = ( $langs -> trans ( $transkey ) != $transkey ? $langs -> trans ( $transkey ) : $obj -> libelle );
2021-02-23 22:03:23 +01:00
if ( empty ( $option )) {
$tab [ $obj -> rowid ] = $libelle_type ;
} else {
$tab [ $obj -> code ] = $libelle_type ;
}
2017-10-07 13:09:31 +02:00
$i ++ ;
}
return $tab ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
//dol_print_error($this->db);
return null ;
}
}
2013-09-18 16:46:54 +02:00
2019-09-02 14:47:32 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Return array with list of possible values for type of contacts
*
2020-02-17 12:21:07 +01:00
* @ param string $source 'internal' , 'external' or 'all'
* @ param int $option 0 = Return array id -> label , 1 = Return array code -> label
* @ param int $activeonly 0 = all status of contact , 1 = only the active
* @ param string $code Type of contact ( Example : 'CUSTOMER' , 'SERVICE' )
* @ param string $element Filter on 1 element type
* @ param string $excludeelement Exclude 1 element type . Example : 'agenda'
* @ return array Array list of type of contacts ( id -> label if option = 0 , code -> label if option = 1 )
*/
public function listeTypeContacts ( $source = 'internal' , $option = 0 , $activeonly = 0 , $code = '' , $element = '' , $excludeelement = '' )
2019-09-02 14:47:32 +02:00
{
// phpcs:enable
2019-09-03 17:04:42 +02:00
global $langs , $conf ;
2019-09-02 14:47:32 +02:00
2020-09-01 16:59:40 +02:00
$langs -> loadLangs ( array ( 'bills' , 'contracts' , 'interventions' , 'orders' , 'projects' , 'propal' , 'ticket' , 'agenda' ));
2020-09-01 16:53:42 +02:00
2019-09-02 14:47:32 +02:00
$tab = array ();
2019-09-03 17:04:42 +02:00
2019-09-02 14:47:32 +02:00
$sql = " SELECT DISTINCT tc.rowid, tc.code, tc.libelle, tc.position, tc.element " ;
2019-11-13 19:34:37 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_type_contact as tc " ;
2019-09-02 14:47:32 +02:00
2019-11-13 19:34:37 +01:00
$sqlWhere = array ();
2020-02-17 12:21:07 +01:00
if ( ! empty ( $element )) {
2019-11-13 19:34:37 +01:00
$sqlWhere [] = " tc.element=' " . $this -> db -> escape ( $element ) . " ' " ;
2020-02-17 12:21:07 +01:00
}
if ( ! empty ( $excludeelement )) {
$sqlWhere [] = " tc.element <> ' " . $this -> db -> escape ( $excludeelement ) . " ' " ;
}
2019-09-02 14:47:32 +02:00
2021-02-23 22:03:23 +01:00
if ( $activeonly == 1 ) {
2019-11-13 19:34:37 +01:00
$sqlWhere [] = " tc.active=1 " ; // only the active types
2021-02-23 22:03:23 +01:00
}
2019-09-02 14:47:32 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $source ) && $source != 'all' ) {
2019-11-13 19:34:37 +01:00
$sqlWhere [] = " tc.source=' " . $this -> db -> escape ( $source ) . " ' " ;
2021-02-23 22:03:23 +01:00
}
2019-09-02 14:47:32 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $code )) {
2019-11-13 19:34:37 +01:00
$sqlWhere [] = " tc.code=' " . $this -> db -> escape ( $code ) . " ' " ;
2021-02-23 22:03:23 +01:00
}
2019-09-02 14:47:32 +02:00
2019-11-13 19:34:37 +01:00
if ( count ( $sqlWhere ) > 0 ) {
$sql .= " WHERE " . implode ( ' AND ' , $sqlWhere );
2019-09-02 14:47:32 +02:00
}
2019-11-13 19:34:37 +01:00
$sql .= $this -> db -> order ( 'tc.element, tc.position' , 'ASC' );
2019-09-02 14:47:32 +02:00
2020-02-17 12:34:00 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2019-11-13 19:34:37 +01:00
$resql = $this -> db -> query ( $sql );
2019-09-03 17:04:42 +02:00
if ( $resql ) {
$num = $this -> db -> num_rows ( $resql );
if ( $num > 0 ) {
2021-02-22 15:51:00 +01:00
$langs -> loadLangs ( array ( " propal " , " orders " , " bills " , " suppliers " , " contracts " , " supplier_proposal " ));
2019-09-03 17:04:42 +02:00
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2020-02-17 12:21:07 +01:00
$modulename = $obj -> element ;
2019-11-13 19:34:37 +01:00
if ( strpos ( $obj -> element , 'project' ) !== false ) {
2020-02-17 12:21:07 +01:00
$modulename = 'projet' ;
2019-11-13 19:34:37 +01:00
} elseif ( $obj -> element == 'contrat' ) {
$element = 'contract' ;
2020-02-17 12:21:07 +01:00
} elseif ( $obj -> element == 'action' ) {
$modulename = 'agenda' ;
2019-11-13 19:34:37 +01:00
} elseif ( strpos ( $obj -> element , 'supplier' ) !== false && $obj -> element != 'supplier_proposal' ) {
2020-02-17 12:21:07 +01:00
$modulename = 'fournisseur' ;
2019-11-13 19:34:37 +01:00
} elseif ( strpos ( $obj -> element , 'supplier' ) !== false && $obj -> element != 'supplier_proposal' ) {
2020-02-17 12:21:07 +01:00
$modulename = 'fournisseur' ;
2019-09-03 17:04:42 +02:00
}
2020-12-29 17:48:52 +01:00
if ( ! empty ( $conf -> { $modulename } -> enabled )) {
2019-11-13 19:34:37 +01:00
$libelle_element = $langs -> trans ( 'ContactDefault_' . $obj -> element );
2021-02-22 15:51:00 +01:00
$tmpelement = $obj -> element ;
$transkey = " TypeContact_ " . $tmpelement . " _ " . $source . " _ " . $obj -> code ;
2019-09-03 17:04:42 +02:00
$libelle_type = ( $langs -> trans ( $transkey ) != $transkey ? $langs -> trans ( $transkey ) : $obj -> libelle );
2021-02-22 15:51:00 +01:00
if ( empty ( $option )) {
$tab [ $obj -> rowid ] = $libelle_element . ' - ' . $libelle_type ;
2021-02-23 22:03:23 +01:00
} else {
2019-11-13 19:34:37 +01:00
$tab [ $obj -> rowid ] = $libelle_element . ' - ' . $libelle_type ;
2021-02-22 15:51:00 +01:00
}
2019-09-03 17:04:42 +02:00
}
}
2019-09-02 14:47:32 +02:00
}
return $tab ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2019-09-02 14:47:32 +02:00
return null ;
}
}
2017-10-07 13:09:31 +02:00
/**
* Return id of contacts for a source and a contact code .
* Example : contact client de facturation ( 'external' , 'BILLING' )
* Example : contact client de livraison ( 'external' , 'SHIPPING' )
* Example : contact interne suivi paiement ( 'internal' , 'SALESREPFOLL' )
*
* @ param string $source 'external' or 'internal'
* @ param string $code 'BILLING' , 'SHIPPING' , 'SALESREPFOLL' , ...
* @ param int $status limited to a certain status
* @ return array List of id for such contacts
*/
2019-02-25 22:27:04 +01:00
public function getIdContact ( $source , $code , $status = 0 )
2017-10-07 13:09:31 +02:00
{
global $conf ;
2019-11-13 19:34:37 +01:00
$result = array ();
$i = 0 ;
2017-10-07 13:09:31 +02:00
//cas particulier pour les expeditions
2019-11-13 19:34:37 +01:00
if ( $this -> element == 'shipping' && $this -> origin_id != 0 ) {
$id = $this -> origin_id ;
$element = 'commande' ;
} elseif ( $this -> element == 'reception' && $this -> origin_id != 0 ) {
$id = $this -> origin_id ;
$element = 'order_supplier' ;
2017-10-07 13:09:31 +02:00
} else {
2019-11-13 19:34:37 +01:00
$id = $this -> id ;
$element = $this -> element ;
2017-10-07 13:09:31 +02:00
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$sql = " SELECT ec.fk_socpeople " ;
2019-11-13 19:34:37 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " element_contact as ec, " ;
2021-02-23 22:03:23 +01:00
if ( $source == 'internal' ) {
$sql .= " " . MAIN_DB_PREFIX . " user as c, " ;
}
if ( $source == 'external' ) {
$sql .= " " . MAIN_DB_PREFIX . " socpeople as c, " ;
}
2019-11-13 19:34:37 +01:00
$sql .= " " . MAIN_DB_PREFIX . " c_type_contact as tc " ;
2021-03-30 17:53:25 +02:00
$sql .= " WHERE ec.element_id = " . (( int ) $id );
2019-11-13 19:34:37 +01:00
$sql .= " AND ec.fk_socpeople = c.rowid " ;
2021-02-23 22:03:23 +01:00
if ( $source == 'internal' ) {
$sql .= " AND c.entity IN ( " . getEntity ( 'user' ) . " ) " ;
}
if ( $source == 'external' ) {
$sql .= " AND c.entity IN ( " . getEntity ( 'societe' ) . " ) " ;
}
2019-11-13 19:34:37 +01:00
$sql .= " AND ec.fk_c_type_contact = tc.rowid " ;
2020-09-19 21:19:04 +02:00
$sql .= " AND tc.element = ' " . $this -> db -> escape ( $element ) . " ' " ;
$sql .= " AND tc.source = ' " . $this -> db -> escape ( $source ) . " ' " ;
2021-02-23 22:03:23 +01:00
if ( $code ) {
$sql .= " AND tc.code = ' " . $this -> db -> escape ( $code ) . " ' " ;
}
2019-11-13 19:34:37 +01:00
$sql .= " AND tc.active = 1 " ;
2021-02-23 22:03:23 +01:00
if ( $status ) {
2021-03-30 17:53:25 +02:00
$sql .= " AND ec.statut = " . (( int ) $status );
2021-02-23 22:03:23 +01:00
}
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::getIdContact " , LOG_DEBUG );
2019-11-13 19:34:37 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2019-11-13 19:34:37 +01:00
$result [ $i ] = $obj -> fk_socpeople ;
2017-10-07 13:09:31 +02:00
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> error ();
2017-10-07 13:09:31 +02:00
return null ;
}
2009-02-20 23:53:15 +01:00
2017-10-07 13:09:31 +02:00
return $result ;
}
2014-09-29 13:21:03 +02:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
2020-06-25 12:17:47 +02:00
* Load object contact with id = $this -> contact_id into $this -> contact
2017-10-07 13:09:31 +02:00
*
2020-06-25 12:17:47 +02:00
* @ param int $contactid Id du contact . Use this -> contact_id if empty .
2017-10-07 13:09:31 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function fetch_contact ( $contactid = null )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2021-02-23 22:03:23 +01:00
if ( empty ( $contactid )) {
$contactid = $this -> contact_id ;
}
2014-09-29 13:21:03 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $contactid )) {
return 0 ;
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php' ;
$contact = new Contact ( $this -> db );
2019-11-13 19:34:37 +01:00
$result = $contact -> fetch ( $contactid );
2017-10-07 13:09:31 +02:00
$this -> contact = $contact ;
return $result ;
}
2011-11-07 15:04:02 +01:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Load the third party of object , from id $this -> socid or $this -> fk_soc , into this -> thirdparty
*
* @ param int $force_thirdparty_id Force thirdparty id
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function fetch_thirdparty ( $force_thirdparty_id = 0 )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
global $conf ;
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( empty ( $this -> socid ) && empty ( $this -> fk_soc ) && empty ( $this -> fk_thirdparty ) && empty ( $force_thirdparty_id )) {
2017-10-07 13:09:31 +02:00
return 0 ;
2021-02-23 22:03:23 +01:00
}
2011-11-07 15:04:02 +01:00
2019-11-13 19:34:37 +01:00
require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
2014-11-20 14:44:49 +01:00
2017-10-07 13:09:31 +02:00
$idtofetch = isset ( $this -> socid ) ? $this -> socid : ( isset ( $this -> fk_soc ) ? $this -> fk_soc : $this -> fk_thirdparty );
2021-02-23 22:03:23 +01:00
if ( $force_thirdparty_id ) {
2017-10-07 13:09:31 +02:00
$idtofetch = $force_thirdparty_id ;
2021-02-23 22:03:23 +01:00
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
if ( $idtofetch ) {
$thirdparty = new Societe ( $this -> db );
$result = $thirdparty -> fetch ( $idtofetch );
2021-03-11 19:09:42 +01:00
if ( $result < 0 ) {
$this -> errors = array_merge ( $this -> errors , $thirdparty -> errors );
}
2017-10-07 13:09:31 +02:00
$this -> thirdparty = $thirdparty ;
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
// Use first price level if level not defined for third party
if ( ! empty ( $conf -> global -> PRODUIT_MULTIPRICES ) && empty ( $this -> thirdparty -> price_level )) {
$this -> thirdparty -> price_level = 1 ;
}
return $result ;
2021-02-23 22:03:23 +01:00
} else {
return - 1 ;
}
2017-10-07 13:09:31 +02:00
}
2011-11-07 15:04:02 +01:00
2011-12-07 15:06:29 +01:00
2017-10-07 13:09:31 +02:00
/**
* Looks for an object with ref matching the wildcard provided
* It does only work when $this -> table_ref_field is set
*
* @ param string $ref Wildcard
* @ return int > 1 = OK , 0 = Not found or table_ref_field not defined , < 0 = KO
*/
public function fetchOneLike ( $ref )
{
if ( ! $this -> table_ref_field ) {
return 0 ;
}
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . $this -> table_element . ' WHERE ' . $this -> table_ref_field . ' LIKE "' . $this -> db -> escape ( $ref ) . '" LIMIT 1' ;
$query = $this -> db -> query ( $sql );
if ( ! $this -> db -> num_rows ( $query )) {
return 0 ;
}
$result = $this -> db -> fetch_object ( $query );
return $this -> fetch ( $result -> rowid );
}
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Load data for barcode into properties -> barcode_type *
* Properties -> barcode_type that is id of barcode . Type is used to find other properties , but
* if it is not defined , -> element must be defined to know default barcode type .
*
* @ return int < 0 if KO , 0 if can ' t guess type of barcode ( ISBN , EAN13 ... ), > 0 if OK ( all barcode properties loaded )
*/
2019-02-25 22:27:04 +01:00
public function fetch_barcode ()
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
global $conf ;
dol_syslog ( get_class ( $this ) . '::fetch_barcode this->element=' . $this -> element . ' this->barcode_type=' . $this -> barcode_type );
2019-11-13 19:34:37 +01:00
$idtype = $this -> barcode_type ;
2021-02-23 22:03:23 +01:00
if ( empty ( $idtype ) && $idtype != '0' ) { // If type of barcode no set, we try to guess. If set to '0' it means we forced to have type remain not defined
if ( $this -> element == 'product' ) {
$idtype = $conf -> global -> PRODUIT_DEFAULT_BARCODE_TYPE ;
} elseif ( $this -> element == 'societe' ) {
$idtype = $conf -> global -> GENBARCODE_BARCODETYPE_THIRDPARTY ;
} else {
dol_syslog ( 'Call fetch_barcode with barcode_type not defined and cant be guessed' , LOG_WARNING );
}
2017-10-07 13:09:31 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $idtype > 0 ) {
if ( empty ( $this -> barcode_type ) || empty ( $this -> barcode_type_code ) || empty ( $this -> barcode_type_label ) || empty ( $this -> barcode_type_coder )) { // If data not already loaded
2017-10-07 13:09:31 +02:00
$sql = " SELECT rowid, code, libelle as label, coder " ;
2019-11-13 19:34:37 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_barcode_type " ;
2021-03-30 17:53:25 +02:00
$sql .= " WHERE rowid = " . (( int ) $idtype );
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::fetch_barcode' , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
$this -> barcode_type = $obj -> rowid ;
$this -> barcode_type_code = $obj -> code ;
$this -> barcode_type_label = $obj -> label ;
$this -> barcode_type_coder = $obj -> coder ;
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
return - 1 ;
}
}
}
return 0 ;
}
2021-09-18 12:39:26 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Load the project with id $this -> fk_project into this -> project
*
* @ return int < 0 if KO , >= 0 if OK
*/
public function fetch_project ()
{
// phpcs:enable
return $this -> fetch_projet ();
}
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
2018-02-27 16:33:50 +01:00
* Load the project with id $this -> fk_project into this -> project
2017-10-07 13:09:31 +02:00
*
* @ return int < 0 if KO , >= 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function fetch_projet ()
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
include_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php' ;
2021-02-23 22:03:23 +01:00
if ( empty ( $this -> fk_project ) && ! empty ( $this -> fk_projet )) {
$this -> fk_project = $this -> fk_projet ; // For backward compatibility
}
if ( empty ( $this -> fk_project )) {
return 0 ;
}
2017-10-07 13:09:31 +02:00
$project = new Project ( $this -> db );
$result = $project -> fetch ( $this -> fk_project );
2019-11-13 19:34:37 +01:00
$this -> projet = $project ; // deprecated
2017-10-07 13:09:31 +02:00
$this -> project = $project ;
return $result ;
}
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
2018-02-27 16:33:50 +01:00
* Load the product with id $this -> fk_product into this -> product
*
* @ return int < 0 if KO , >= 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function fetch_product ()
2018-02-27 16:33:50 +01:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2018-02-27 16:33:50 +01:00
include_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
2021-02-23 22:03:23 +01:00
if ( empty ( $this -> fk_product )) {
return 0 ;
}
2018-02-27 16:33:50 +01:00
$product = new Product ( $this -> db );
$result = $product -> fetch ( $this -> fk_product );
$this -> product = $product ;
return $result ;
}
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2018-02-27 16:33:50 +01:00
/**
* Load the user with id $userid into this -> user
2017-10-07 13:09:31 +02:00
*
* @ param int $userid Id du contact
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function fetch_user ( $userid )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
$user = new User ( $this -> db );
2019-11-13 19:34:37 +01:00
$result = $user -> fetch ( $userid );
2017-10-07 13:09:31 +02:00
$this -> user = $user ;
return $result ;
}
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Read linked origin object
*
* @ return void
*/
2019-02-25 22:27:04 +01:00
public function fetch_origin ()
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2021-02-23 22:03:23 +01:00
if ( $this -> origin == 'shipping' ) {
$this -> origin = 'expedition' ;
}
if ( $this -> origin == 'delivery' ) {
$this -> origin = 'livraison' ;
}
if ( $this -> origin == 'order_supplier' ) {
$this -> origin = 'commandeFournisseur' ;
}
2017-10-07 13:09:31 +02:00
$origin = $this -> origin ;
$classname = ucfirst ( $origin );
$this -> $origin = new $classname ( $this -> db );
$this -> $origin -> fetch ( $this -> origin_id );
}
/**
2020-09-07 10:18:17 +02:00
* Load object from specific field
*
* @ param string $table Table element or element line
* @ param string $field Field selected
* @ param string $key Import key
* @ param string $element Element name
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function fetchObjectFrom ( $table , $field , $key , $element = null )
2017-10-07 13:09:31 +02:00
{
global $conf ;
2014-12-25 21:47:39 +01:00
2019-11-13 19:34:37 +01:00
$result = false ;
2014-12-25 21:47:39 +01:00
2017-10-07 13:09:31 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . $table ;
2019-11-13 19:34:37 +01:00
$sql .= " WHERE " . $field . " = ' " . $key . " ' " ;
if ( ! empty ( $element )) {
$sql .= " AND entity IN ( " . getEntity ( $element ) . " ) " ;
2018-02-02 12:36:47 +01:00
} else {
2021-08-23 18:56:46 +02:00
$sql .= " AND entity = " . (( int ) $conf -> entity );
2018-02-02 12:36:47 +01:00
}
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::fetchObjectFrom' , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$row = $this -> db -> fetch_row ( $resql );
2018-02-05 11:30:31 +01:00
// Test for avoid error -1
if ( $row [ 0 ] > 0 ) {
$result = $this -> fetch ( $row [ 0 ]);
}
2017-10-07 13:09:31 +02:00
}
return $result ;
}
/**
* Getter generic . Load value from a specific field
*
* @ param string $table Table of element or element line
* @ param int $id Element id
* @ param string $field Field selected
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function getValueFrom ( $table , $id , $field )
2017-10-07 13:09:31 +02:00
{
2019-11-13 19:34:37 +01:00
$result = false ;
2017-10-07 13:09:31 +02:00
if ( ! empty ( $id ) && ! empty ( $field ) && ! empty ( $table )) {
$sql = " SELECT " . $field . " FROM " . MAIN_DB_PREFIX . $table ;
2021-03-14 11:48:39 +01:00
$sql .= " WHERE rowid = " . (( int ) $id );
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::getValueFrom' , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$row = $this -> db -> fetch_row ( $resql );
$result = $row [ 0 ];
}
}
return $result ;
}
/**
* Setter generic . Update a specific field into database .
* Warning : Trigger is run only if param trigkey is provided .
*
2018-04-12 23:16:23 +02:00
* @ param string $field Field to update
* @ param mixed $value New value
* @ param string $table To force other table element or element line ( should not be used )
* @ param int $id To force other object id ( should not be used )
* @ param string $format Data format ( 'text' , 'date' ) . 'text' is used if not defined
* @ param string $id_field To force rowid field name . 'rowid' is used if not defined
* @ param User | string $fuser Update the user of last update field with this user . If not provided , current user is used except if value is 'none'
* @ param string $trigkey Trigger key to run ( in most cases something like 'XXX_MODIFY' )
* @ param string $fk_user_field Name of field to save user id making change
* @ return int < 0 if KO , > 0 if OK
2019-01-22 10:39:06 +01:00
* @ see updateExtraField ()
2017-10-07 13:09:31 +02:00
*/
2019-02-25 22:27:04 +01:00
public function setValueFrom ( $field , $value , $table = '' , $id = null , $format = '' , $id_field = '' , $fuser = null , $trigkey = '' , $fk_user_field = 'fk_user_modif' )
2017-10-07 13:09:31 +02:00
{
2019-11-13 19:34:37 +01:00
global $user , $langs , $conf ;
2017-10-07 13:09:31 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $table )) {
$table = $this -> table_element ;
}
if ( empty ( $id )) {
$id = $this -> id ;
}
if ( empty ( $format )) {
$format = 'text' ;
}
if ( empty ( $id_field )) {
$id_field = 'rowid' ;
}
2017-10-07 13:09:31 +02:00
2019-11-13 19:34:37 +01:00
$error = 0 ;
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
// Special case
2021-02-23 22:03:23 +01:00
if ( $table == 'product' && $field == 'note_private' ) {
$field = 'note' ;
}
if ( in_array ( $table , array ( 'actioncomm' , 'adherent' , 'advtargetemailing' , 'cronjob' , 'establishment' ))) {
$fk_user_field = 'fk_user_mod' ;
}
2017-10-07 13:09:31 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $table . " SET " ;
2018-04-12 23:16:23 +02:00
2021-02-23 22:03:23 +01:00
if ( $format == 'text' ) {
$sql .= $field . " = ' " . $this -> db -> escape ( $value ) . " ' " ;
} elseif ( $format == 'int' ) {
2021-06-09 15:36:47 +02:00
$sql .= $field . " = " . (( int ) $value );
2021-02-23 22:03:23 +01:00
} elseif ( $format == 'date' ) {
$sql .= $field . " = " . ( $value ? " ' " . $this -> db -> idate ( $value ) . " ' " : " null " );
}
2018-04-12 23:16:23 +02:00
2021-02-23 22:03:23 +01:00
if ( $fk_user_field ) {
if ( ! empty ( $fuser ) && is_object ( $fuser )) {
$sql .= " , " . $fk_user_field . " = " . $fuser -> id ;
} elseif ( empty ( $fuser ) || $fuser != 'none' ) {
$sql .= " , " . $fk_user_field . " = " . $user -> id ;
}
2018-04-12 23:16:23 +02:00
}
2021-03-30 17:53:25 +02:00
$sql .= " WHERE " . $id_field . " = " . (( int ) $id );
2017-10-07 13:09:31 +02:00
2020-02-17 12:22:52 +01:00
dol_syslog ( __METHOD__ . " " , LOG_DEBUG );
2017-10-07 13:09:31 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
if ( $trigkey ) {
2018-06-06 17:10:46 +02:00
// call trigger with updated object values
2021-02-23 22:03:23 +01:00
if ( empty ( $this -> fields ) && method_exists ( $this , 'fetch' )) {
2018-06-06 22:04:12 +02:00
$result = $this -> fetch ( $id );
2020-05-21 15:05:19 +02:00
} else {
2018-06-06 17:10:46 +02:00
$result = $this -> fetchCommon ( $id );
}
2021-02-23 22:03:23 +01:00
if ( $result >= 0 ) {
$result = $this -> call_trigger ( $trigkey , ( ! empty ( $fuser ) && is_object ( $fuser )) ? $fuser : $user ); // This may set this->errors
}
if ( $result < 0 ) {
$error ++ ;
}
2017-10-07 13:09:31 +02:00
}
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
if ( property_exists ( $this , $field )) {
$this -> $field = $value ;
}
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 2 ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-12-14 20:40:47 +01:00
if ( $this -> db -> lasterrno () == 'DB_ERROR_RECORD_ALREADY_EXISTS' ) {
$this -> error = 'DB_ERROR_RECORD_ALREADY_EXISTS' ;
} else {
$this -> error = $this -> db -> lasterror ();
}
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
2017-11-03 17:20:08 +01:00
* Load properties id_previous and id_next by comparing $fieldid with $this -> ref
2017-10-07 13:09:31 +02:00
*
2021-08-23 17:41:11 +02:00
* @ param string $filter Optional filter . Example : " AND (t.field1 = 'aa' OR t.field2 = 'bb') " . Do not allow user input data here .
2017-10-07 13:09:31 +02:00
* @ param string $fieldid Name of field to use for the select MAX and MIN
* @ param int $nodbprefix Do not include DB prefix to forge table name
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function load_previous_next_ref ( $filter , $fieldid , $nodbprefix = 0 )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2018-07-02 10:09:56 +02:00
global $conf , $user ;
2017-10-07 13:09:31 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $this -> table_element ) {
2019-01-27 11:55:16 +01:00
dol_print_error ( '' , get_class ( $this ) . " ::load_previous_next_ref was called on objet with property table_element not defined " );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2021-02-23 22:03:23 +01:00
if ( $fieldid == 'none' ) {
return 1 ;
}
2017-10-07 13:09:31 +02:00
2021-09-08 19:45:55 +02:00
// For backward compatibility
if ( $this -> table_element == 'facture_rec' && $fieldid == 'title' ) {
$fieldid = 'titre' ;
}
2017-12-18 15:39:40 +01:00
// Security on socid
$socid = 0 ;
2021-02-23 22:03:23 +01:00
if ( $user -> socid > 0 ) {
$socid = $user -> socid ;
}
2017-12-18 15:39:40 +01:00
2017-10-07 13:09:31 +02:00
// this->ismultientitymanaged contains
2020-02-08 14:40:30 +01:00
// 0=No test on entity, 1=Test with field entity, 'field@table'=Test with link by field@table
$aliastablesociete = 's' ;
2021-02-23 22:03:23 +01:00
if ( $this -> element == 'societe' ) {
$aliastablesociete = 'te' ; // te as table_element
}
2021-03-16 21:13:15 +01:00
$restrictiononfksoc = empty ( $this -> restrictiononfksoc ) ? 0 : $this -> restrictiononfksoc ;
2017-10-07 13:09:31 +02:00
$sql = " SELECT MAX(te. " . $fieldid . " ) " ;
2019-11-13 19:34:37 +01:00
$sql .= " FROM " . ( empty ( $nodbprefix ) ? MAIN_DB_PREFIX : '' ) . $this -> table_element . " as te " ;
if ( $this -> element == 'user' && ! empty ( $conf -> global -> MULTICOMPANY_TRANSVERSE_MODE )) {
$sql .= " , " . MAIN_DB_PREFIX . " usergroup_user as ug " ;
}
2020-02-21 17:53:37 +01:00
if ( isset ( $this -> ismultientitymanaged ) && ! is_numeric ( $this -> ismultientitymanaged )) {
2020-02-08 14:40:30 +01:00
$tmparray = explode ( '@' , $this -> ismultientitymanaged );
$sql .= " , " . MAIN_DB_PREFIX . $tmparray [ 1 ] . " as " . ( $tmparray [ 1 ] == 'societe' ? 's' : 'parenttable' ); // If we need to link to this table to limit select to entity
2021-03-16 21:13:15 +01:00
} elseif ( $restrictiononfksoc == 1 && $this -> element != 'societe' && ! $user -> rights -> societe -> client -> voir && ! $socid ) {
2021-02-23 22:03:23 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe as s " ; // If we need to link to societe to limit select to socid
2021-03-16 21:13:15 +01:00
} elseif ( $restrictiononfksoc == 2 && $this -> element != 'societe' && ! $user -> rights -> societe -> client -> voir && ! $socid ) {
2021-02-23 22:03:23 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON te.fk_soc = s.rowid " ; // If we need to link to societe to limit select to socid
}
2021-03-16 21:13:15 +01:00
if ( $restrictiononfksoc && ! $user -> rights -> societe -> client -> voir && ! $socid ) {
2021-02-23 22:03:23 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe_commerciaux as sc ON " . $aliastablesociete . " .rowid = sc.fk_soc " ;
}
2020-09-07 10:18:17 +02:00
$sql .= " WHERE te. " . $fieldid . " < ' " . $this -> db -> escape ( $fieldid == 'rowid' ? $this -> id : $this -> ref ) . " ' " ; // ->ref must always be defined (set to id if field does not exists)
2021-03-16 21:13:15 +01:00
if ( $restrictiononfksoc == 1 && ! $user -> rights -> societe -> client -> voir && ! $socid ) {
2021-08-23 17:41:11 +02:00
$sql .= " AND sc.fk_user = " . (( int ) $user -> id );
2021-02-23 22:03:23 +01:00
}
2021-03-16 21:13:15 +01:00
if ( $restrictiononfksoc == 2 && ! $user -> rights -> societe -> client -> voir && ! $socid ) {
2021-08-23 17:41:11 +02:00
$sql .= " AND (sc.fk_user = " . (( int ) $user -> id ) . ' OR te.fk_soc IS NULL)' ;
2021-02-23 22:03:23 +01:00
}
if ( ! empty ( $filter )) {
if ( ! preg_match ( '/^\s*AND/i' , $filter )) {
$sql .= " AND " ; // For backward compatibility
}
2019-11-13 19:34:37 +01:00
$sql .= $filter ;
}
2020-02-21 17:53:37 +01:00
if ( isset ( $this -> ismultientitymanaged ) && ! is_numeric ( $this -> ismultientitymanaged )) {
2020-02-08 14:40:30 +01:00
$tmparray = explode ( '@' , $this -> ismultientitymanaged );
$sql .= ' AND te.' . $tmparray [ 0 ] . ' = ' . ( $tmparray [ 1 ] == 'societe' ? 's' : 'parenttable' ) . '.rowid' ; // If we need to link to this table to limit select to entity
2021-03-16 21:13:15 +01:00
} elseif ( $restrictiononfksoc == 1 && $this -> element != 'societe' && ! $user -> rights -> societe -> client -> voir && ! $socid ) {
2021-02-23 22:03:23 +01:00
$sql .= ' AND te.fk_soc = s.rowid' ; // If we need to link to societe to limit select to socid
}
2018-07-02 10:09:56 +02:00
if ( isset ( $this -> ismultientitymanaged ) && $this -> ismultientitymanaged == 1 ) {
2019-11-13 19:34:37 +01:00
if ( $this -> element == 'user' && ! empty ( $conf -> global -> MULTICOMPANY_TRANSVERSE_MODE )) {
if ( ! empty ( $user -> admin ) && empty ( $user -> entity ) && $conf -> entity == 1 ) {
$sql .= " AND te.entity IS NOT NULL " ; // Show all users
2018-07-02 10:18:16 +02:00
} else {
2019-11-13 19:34:37 +01:00
$sql .= " AND ug.fk_user = te.rowid " ;
$sql .= " AND ug.entity IN ( " . getEntity ( $this -> element ) . " ) " ;
2018-07-02 10:18:16 +02:00
}
2018-07-02 10:09:56 +02:00
} else {
2019-11-13 19:34:37 +01:00
$sql .= ' AND te.entity IN (' . getEntity ( $this -> element ) . ')' ;
2018-07-02 10:09:56 +02:00
}
}
2020-02-21 17:53:37 +01:00
if ( isset ( $this -> ismultientitymanaged ) && ! is_numeric ( $this -> ismultientitymanaged ) && $this -> element != 'societe' ) {
2020-02-08 14:40:30 +01:00
$tmparray = explode ( '@' , $this -> ismultientitymanaged );
$sql .= ' AND parenttable.entity IN (' . getEntity ( $tmparray [ 1 ]) . ')' ;
}
2021-03-16 21:13:15 +01:00
if ( $restrictiononfksoc == 1 && $socid && $this -> element != 'societe' ) {
2021-02-23 22:03:23 +01:00
$sql .= ' AND te.fk_soc = ' . $socid ;
}
2021-03-16 21:13:15 +01:00
if ( $restrictiononfksoc == 2 && $socid && $this -> element != 'societe' ) {
2021-02-23 22:03:23 +01:00
$sql .= ' AND (te.fk_soc = ' . $socid . ' OR te.fk_soc IS NULL)' ;
}
2021-03-16 21:13:15 +01:00
if ( $restrictiononfksoc && $socid && $this -> element == 'societe' ) {
2021-02-23 22:03:23 +01:00
$sql .= ' AND te.rowid = ' . $socid ;
}
2021-03-16 21:13:15 +01:00
//print 'socid='.$socid.' restrictiononfksoc='.$restrictiononfksoc.' ismultientitymanaged = '.$this->ismultientitymanaged.' filter = '.$filter.' -> '.$sql."<br>";
2017-10-07 13:09:31 +02:00
$result = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( ! $result ) {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
$row = $this -> db -> fetch_row ( $result );
$this -> ref_previous = $row [ 0 ];
$sql = " SELECT MIN(te. " . $fieldid . " ) " ;
2019-11-13 19:34:37 +01:00
$sql .= " FROM " . ( empty ( $nodbprefix ) ? MAIN_DB_PREFIX : '' ) . $this -> table_element . " as te " ;
if ( $this -> element == 'user' && ! empty ( $conf -> global -> MULTICOMPANY_TRANSVERSE_MODE )) {
$sql .= " , " . MAIN_DB_PREFIX . " usergroup_user as ug " ;
}
2020-07-26 21:07:39 +02:00
if ( isset ( $this -> ismultientitymanaged ) && ! is_numeric ( $this -> ismultientitymanaged )) {
$tmparray = explode ( '@' , $this -> ismultientitymanaged );
$sql .= " , " . MAIN_DB_PREFIX . $tmparray [ 1 ] . " as " . ( $tmparray [ 1 ] == 'societe' ? 's' : 'parenttable' ); // If we need to link to this table to limit select to entity
2021-03-16 21:13:15 +01:00
} elseif ( $restrictiononfksoc == 1 && $this -> element != 'societe' && ! $user -> rights -> societe -> client -> voir && ! $socid ) {
2021-02-23 22:03:23 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe as s " ; // If we need to link to societe to limit select to socid
2021-03-16 21:13:15 +01:00
} elseif ( $restrictiononfksoc == 2 && $this -> element != 'societe' && ! $user -> rights -> societe -> client -> voir && ! $socid ) {
2021-02-23 22:03:23 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON te.fk_soc = s.rowid " ; // If we need to link to societe to limit select to socid
}
2021-03-16 21:13:15 +01:00
if ( $restrictiononfksoc && ! $user -> rights -> societe -> client -> voir && ! $socid ) {
2021-02-23 22:03:23 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe_commerciaux as sc ON " . $aliastablesociete . " .rowid = sc.fk_soc " ;
}
2020-09-07 10:18:17 +02:00
$sql .= " WHERE te. " . $fieldid . " > ' " . $this -> db -> escape ( $fieldid == 'rowid' ? $this -> id : $this -> ref ) . " ' " ; // ->ref must always be defined (set to id if field does not exists)
2021-03-16 21:13:15 +01:00
if ( $restrictiononfksoc == 1 && ! $user -> rights -> societe -> client -> voir && ! $socid ) {
2021-08-23 17:41:11 +02:00
$sql .= " AND sc.fk_user = " . (( int ) $user -> id );
2021-02-23 22:03:23 +01:00
}
2021-03-16 21:13:15 +01:00
if ( $restrictiononfksoc == 2 && ! $user -> rights -> societe -> client -> voir && ! $socid ) {
2021-08-23 17:41:11 +02:00
$sql .= " AND (sc.fk_user = " . (( int ) $user -> id ) . ' OR te.fk_soc IS NULL)' ;
2021-02-23 22:03:23 +01:00
}
if ( ! empty ( $filter )) {
if ( ! preg_match ( '/^\s*AND/i' , $filter )) {
$sql .= " AND " ; // For backward compatibility
}
2019-11-13 19:34:37 +01:00
$sql .= $filter ;
}
2020-07-26 21:07:39 +02:00
if ( isset ( $this -> ismultientitymanaged ) && ! is_numeric ( $this -> ismultientitymanaged )) {
$tmparray = explode ( '@' , $this -> ismultientitymanaged );
$sql .= ' AND te.' . $tmparray [ 0 ] . ' = ' . ( $tmparray [ 1 ] == 'societe' ? 's' : 'parenttable' ) . '.rowid' ; // If we need to link to this table to limit select to entity
2021-03-16 21:13:15 +01:00
} elseif ( $restrictiononfksoc == 1 && $this -> element != 'societe' && ! $user -> rights -> societe -> client -> voir && ! $socid ) {
2021-02-23 22:03:23 +01:00
$sql .= ' AND te.fk_soc = s.rowid' ; // If we need to link to societe to limit select to socid
}
2018-07-02 10:09:56 +02:00
if ( isset ( $this -> ismultientitymanaged ) && $this -> ismultientitymanaged == 1 ) {
2019-11-13 19:34:37 +01:00
if ( $this -> element == 'user' && ! empty ( $conf -> global -> MULTICOMPANY_TRANSVERSE_MODE )) {
if ( ! empty ( $user -> admin ) && empty ( $user -> entity ) && $conf -> entity == 1 ) {
$sql .= " AND te.entity IS NOT NULL " ; // Show all users
2018-07-02 10:18:16 +02:00
} else {
2019-11-13 19:34:37 +01:00
$sql .= " AND ug.fk_user = te.rowid " ;
$sql .= " AND ug.entity IN ( " . getEntity ( $this -> element ) . " ) " ;
2018-07-02 10:18:16 +02:00
}
2018-07-02 10:09:56 +02:00
} else {
2019-11-13 19:34:37 +01:00
$sql .= ' AND te.entity IN (' . getEntity ( $this -> element ) . ')' ;
2018-07-02 10:09:56 +02:00
}
}
2020-07-26 21:07:39 +02:00
if ( isset ( $this -> ismultientitymanaged ) && ! is_numeric ( $this -> ismultientitymanaged ) && $this -> element != 'societe' ) {
$tmparray = explode ( '@' , $this -> ismultientitymanaged );
$sql .= ' AND parenttable.entity IN (' . getEntity ( $tmparray [ 1 ]) . ')' ;
}
2021-03-16 21:13:15 +01:00
if ( $restrictiononfksoc == 1 && $socid && $this -> element != 'societe' ) {
2021-02-23 22:03:23 +01:00
$sql .= ' AND te.fk_soc = ' . $socid ;
}
2021-03-16 21:13:15 +01:00
if ( $restrictiononfksoc == 2 && $socid && $this -> element != 'societe' ) {
2021-02-23 22:03:23 +01:00
$sql .= ' AND (te.fk_soc = ' . $socid . ' OR te.fk_soc IS NULL)' ;
}
2021-03-16 21:13:15 +01:00
if ( $restrictiononfksoc && $socid && $this -> element == 'societe' ) {
2021-02-23 22:03:23 +01:00
$sql .= ' AND te.rowid = ' . $socid ;
}
2021-03-16 21:13:15 +01:00
//print 'socid='.$socid.' restrictiononfksoc='.$restrictiononfksoc.' ismultientitymanaged = '.$this->ismultientitymanaged.' filter = '.$filter.' -> '.$sql."<br>";
2017-10-07 13:09:31 +02:00
// Rem: Bug in some mysql version: SELECT MIN(rowid) FROM llx_socpeople WHERE rowid > 1 when one row in database with rowid=1, returns 1 instead of null
$result = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( ! $result ) {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
return - 2 ;
}
$row = $this -> db -> fetch_row ( $result );
$this -> ref_next = $row [ 0 ];
return 1 ;
}
/**
2018-05-25 12:48:33 +02:00
* Return list of id of contacts of object
2017-10-07 13:09:31 +02:00
*
* @ param string $source Source of contact : external ( llx_socpeople ) or internal ( llx_user ) or thirdparty ( llx_societe )
* @ return array Array of id of contacts ( if source = external or internal )
2018-05-25 12:48:33 +02:00
* Array of id of third parties with at least one contact on object ( if source = thirdparty )
2017-10-07 13:09:31 +02:00
*/
2019-02-25 22:27:04 +01:00
public function getListContactId ( $source = 'external' )
2017-10-07 13:09:31 +02:00
{
$contactAlreadySelected = array ();
2019-01-27 11:55:16 +01:00
$tab = $this -> liste_contact ( - 1 , $source );
2019-11-13 19:34:37 +01:00
$num = count ( $tab );
2017-10-07 13:09:31 +02:00
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
if ( $source == 'thirdparty' ) {
$contactAlreadySelected [ $i ] = $tab [ $i ][ 'socid' ];
} else {
$contactAlreadySelected [ $i ] = $tab [ $i ][ 'id' ];
}
2017-10-07 13:09:31 +02:00
$i ++ ;
}
return $contactAlreadySelected ;
}
/**
* Link element with a project
*
* @ param int $projectid Project id to link element to
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function setProject ( $projectid )
2017-10-07 13:09:31 +02:00
{
2021-02-23 22:03:23 +01:00
if ( ! $this -> table_element ) {
2019-01-27 11:55:16 +01:00
dol_syslog ( get_class ( $this ) . " ::setProject was called on objet with property table_element not defined " , LOG_ERR );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $this -> fields [ 'fk_project' ])) { // Common case
if ( $projectid ) {
2021-03-29 23:24:52 +02:00
$sql .= ' SET fk_project = ' . (( int ) $projectid );
2021-02-23 22:03:23 +01:00
} else {
$sql .= ' SET fk_project = NULL' ;
}
2021-03-29 23:24:52 +02:00
$sql .= ' WHERE rowid = ' . (( int ) $this -> id );
2021-02-23 22:03:23 +01:00
} elseif ( $this -> table_element == 'actioncomm' ) { // Special case for actioncomm
if ( $projectid ) {
2021-03-29 23:24:52 +02:00
$sql .= ' SET fk_project = ' . (( int ) $projectid );
2021-02-23 22:03:23 +01:00
} else {
$sql .= ' SET fk_project = NULL' ;
}
2021-03-29 23:24:52 +02:00
$sql .= ' WHERE id = ' . (( int ) $this -> id );
2020-05-21 15:05:19 +02:00
} else // Special case for old architecture objects
2017-10-07 13:09:31 +02:00
{
2021-02-23 22:03:23 +01:00
if ( $projectid ) {
2021-03-29 23:24:52 +02:00
$sql .= ' SET fk_projet = ' . (( int ) $projectid );
2021-02-23 22:03:23 +01:00
} else {
$sql .= ' SET fk_projet = NULL' ;
}
2019-11-16 23:24:29 +01:00
$sql .= ' WHERE rowid = ' . $this -> id ;
2017-10-07 13:09:31 +02:00
}
2014-12-25 21:47:39 +01:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::setProject " , LOG_DEBUG );
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2021-03-29 23:24:52 +02:00
$this -> fk_project = (( int ) $projectid );
2017-10-07 13:09:31 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
return - 1 ;
}
}
2014-12-25 21:47:39 +01:00
2017-10-07 13:09:31 +02:00
/**
* Change the payments methods
*
* @ param int $id Id of new payment method
* @ return int > 0 if OK , < 0 if KO
*/
2019-02-25 22:27:04 +01:00
public function setPaymentMethods ( $id )
2017-10-07 13:09:31 +02:00
{
dol_syslog ( get_class ( $this ) . '::setPaymentMethods(' . $id . ')' );
2021-02-23 22:03:23 +01:00
if ( $this -> statut >= 0 || $this -> element == 'societe' ) {
2017-10-07 13:09:31 +02:00
// TODO uniformize field name
$fieldname = 'fk_mode_reglement' ;
2021-02-23 22:03:23 +01:00
if ( $this -> element == 'societe' ) {
$fieldname = 'mode_reglement' ;
}
if ( get_class ( $this ) == 'Fournisseur' ) {
$fieldname = 'mode_reglement_supplier' ;
}
if ( get_class ( $this ) == 'Tva' ) {
$fieldname = 'fk_typepayment' ;
}
2021-02-26 11:50:18 +01:00
if ( get_class ( $this ) == 'Salary' ) {
$fieldname = 'fk_typepayment' ;
}
2014-12-25 21:47:39 +01:00
2017-10-07 13:09:31 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2018-12-28 23:13:39 +01:00
$sql .= ' SET ' . $fieldname . ' = ' . (( $id > 0 || $id == '0' ) ? $id : 'NULL' );
2021-03-14 12:20:23 +01:00
$sql .= ' WHERE rowid=' . (( int ) $this -> id );
2014-12-25 21:47:39 +01:00
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
$this -> mode_reglement_id = $id ;
// for supplier
2021-02-23 22:03:23 +01:00
if ( get_class ( $this ) == 'Fournisseur' ) {
$this -> mode_reglement_supplier_id = $id ;
}
2017-10-07 13:09:31 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2020-12-14 20:40:47 +01:00
dol_syslog ( get_class ( $this ) . '::setPaymentMethods Error ' . $sql . ' - ' . $this -> db -> error ());
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> error ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::setPaymentMethods, status of the object is incompatible' );
2019-11-13 19:34:37 +01:00
$this -> error = 'Status of the object is incompatible ' . $this -> statut ;
2017-10-07 13:09:31 +02:00
return - 2 ;
}
}
2011-12-07 15:06:29 +01:00
2017-10-07 13:09:31 +02:00
/**
* Change the multicurrency code
*
* @ param string $code multicurrency code
* @ return int > 0 if OK , < 0 if KO
*/
2019-02-25 22:27:04 +01:00
public function setMulticurrencyCode ( $code )
2017-10-07 13:09:31 +02:00
{
2019-12-09 23:22:13 +01:00
dol_syslog ( get_class ( $this ) . '::setMulticurrencyCode(' . $code . ')' );
2021-02-23 22:03:23 +01:00
if ( $this -> statut >= 0 || $this -> element == 'societe' ) {
2017-10-07 13:09:31 +02:00
$fieldname = 'multicurrency_code' ;
2014-05-29 21:01:00 +02:00
2017-10-07 13:09:31 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= ' SET ' . $fieldname . " = ' " . $this -> db -> escape ( $code ) . " ' " ;
2021-03-14 12:20:23 +01:00
$sql .= ' WHERE rowid=' . (( int ) $this -> id );
2014-05-29 21:01:00 +02:00
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
$this -> multicurrency_code = $code ;
2011-12-07 15:06:29 +01:00
2017-10-07 13:09:31 +02:00
list ( $fk_multicurrency , $rate ) = MultiCurrency :: getIdAndTxFromCode ( $this -> db , $code );
2021-02-23 22:03:23 +01:00
if ( $rate ) {
$this -> setMulticurrencyRate ( $rate , 2 );
}
2015-06-03 18:24:09 +02:00
2017-10-07 13:09:31 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2020-12-14 20:40:47 +01:00
dol_syslog ( get_class ( $this ) . '::setMulticurrencyCode Error ' . $sql . ' - ' . $this -> db -> error ());
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> error ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::setMulticurrencyCode, status of the object is incompatible' );
2019-11-13 19:34:37 +01:00
$this -> error = 'Status of the object is incompatible ' . $this -> statut ;
2017-10-07 13:09:31 +02:00
return - 2 ;
}
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
/**
* Change the multicurrency rate
*
* @ param double $rate multicurrency rate
2019-08-15 19:53:38 +02:00
* @ param int $mode mode 1 : amounts in company currency will be recalculated , mode 2 : amounts in foreign currency will be recalculated
2017-10-07 13:09:31 +02:00
* @ return int > 0 if OK , < 0 if KO
*/
2019-02-25 22:27:04 +01:00
public function setMulticurrencyRate ( $rate , $mode = 1 )
2017-10-07 13:09:31 +02:00
{
2019-12-09 23:22:13 +01:00
dol_syslog ( get_class ( $this ) . '::setMulticurrencyRate(' . $rate . ',' . $mode . ')' );
2021-02-23 22:03:23 +01:00
if ( $this -> statut >= 0 || $this -> element == 'societe' ) {
2017-10-07 13:09:31 +02:00
$fieldname = 'multicurrency_tx' ;
2015-06-03 18:24:09 +02:00
2017-10-07 13:09:31 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2021-03-30 11:36:50 +02:00
$sql .= ' SET ' . $fieldname . ' = ' . (( float ) $rate );
2021-03-14 12:20:23 +01:00
$sql .= ' WHERE rowid=' . (( int ) $this -> id );
2018-10-05 16:21:50 +02:00
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
$this -> multicurrency_tx = $rate ;
2018-10-05 16:21:50 +02:00
2017-10-07 13:09:31 +02:00
// Update line price
2021-02-23 22:03:23 +01:00
if ( ! empty ( $this -> lines )) {
foreach ( $this -> lines as & $line ) {
2019-10-02 11:12:30 +02:00
// Amounts in company currency will be recalculated
2019-11-13 19:34:37 +01:00
if ( $mode == 1 ) {
2017-10-07 13:09:31 +02:00
$line -> subprice = 0 ;
}
2018-10-05 16:21:50 +02:00
2019-10-02 11:12:30 +02:00
// Amounts in foreign currency will be recalculated
2019-11-13 19:34:37 +01:00
if ( $mode == 2 ) {
2019-10-02 11:12:30 +02:00
$line -> multicurrency_subprice = 0 ;
}
2017-10-07 13:09:31 +02:00
switch ( $this -> element ) {
case 'propal' :
2018-05-27 15:04:12 +02:00
$this -> updateline (
2021-02-23 22:03:23 +01:00
$line -> id ,
$line -> subprice ,
$line -> qty ,
$line -> remise_percent ,
$line -> tva_tx ,
$line -> localtax1_tx ,
$line -> localtax2_tx ,
( $line -> description ? $line -> description : $line -> desc ),
'HT' ,
$line -> info_bits ,
$line -> special_code ,
$line -> fk_parent_line ,
$line -> skip_update_total ,
$line -> fk_fournprice ,
$line -> pa_ht ,
$line -> label ,
$line -> product_type ,
$line -> date_start ,
$line -> date_end ,
$line -> array_options ,
$line -> fk_unit ,
$line -> multicurrency_subprice
2018-05-27 15:04:12 +02:00
);
2017-10-07 13:09:31 +02:00
break ;
case 'commande' :
2018-05-27 15:04:12 +02:00
$this -> updateline (
2021-02-23 22:03:23 +01:00
$line -> id ,
( $line -> description ? $line -> description : $line -> desc ),
$line -> subprice ,
$line -> qty ,
$line -> remise_percent ,
$line -> tva_tx ,
$line -> localtax1_tx ,
$line -> localtax2_tx ,
'HT' ,
$line -> info_bits ,
$line -> date_start ,
$line -> date_end ,
$line -> product_type ,
$line -> fk_parent_line ,
$line -> skip_update_total ,
$line -> fk_fournprice ,
$line -> pa_ht ,
$line -> label ,
$line -> special_code ,
$line -> array_options ,
$line -> fk_unit ,
$line -> multicurrency_subprice
2018-05-27 15:04:12 +02:00
);
2017-10-07 13:09:31 +02:00
break ;
case 'facture' :
2018-05-27 15:04:12 +02:00
$this -> updateline (
2021-02-23 22:03:23 +01:00
$line -> id ,
( $line -> description ? $line -> description : $line -> desc ),
$line -> subprice ,
$line -> qty ,
$line -> remise_percent ,
$line -> date_start ,
$line -> date_end ,
$line -> tva_tx ,
$line -> localtax1_tx ,
$line -> localtax2_tx ,
'HT' ,
$line -> info_bits ,
$line -> product_type ,
$line -> fk_parent_line ,
$line -> skip_update_total ,
$line -> fk_fournprice ,
$line -> pa_ht ,
$line -> label ,
$line -> special_code ,
$line -> array_options ,
$line -> situation_percent ,
$line -> fk_unit ,
$line -> multicurrency_subprice
2018-05-27 15:04:12 +02:00
);
2017-10-07 13:09:31 +02:00
break ;
case 'supplier_proposal' :
2018-05-27 15:04:12 +02:00
$this -> updateline (
2021-02-23 22:03:23 +01:00
$line -> id ,
$line -> subprice ,
$line -> qty ,
$line -> remise_percent ,
$line -> tva_tx ,
$line -> localtax1_tx ,
$line -> localtax2_tx ,
( $line -> description ? $line -> description : $line -> desc ),
'HT' ,
$line -> info_bits ,
$line -> special_code ,
$line -> fk_parent_line ,
$line -> skip_update_total ,
$line -> fk_fournprice ,
$line -> pa_ht ,
$line -> label ,
$line -> product_type ,
$line -> array_options ,
$line -> ref_fourn ,
$line -> multicurrency_subprice
2018-05-27 15:04:12 +02:00
);
2017-10-07 13:09:31 +02:00
break ;
case 'order_supplier' :
2018-05-27 15:04:12 +02:00
$this -> updateline (
2021-02-23 22:03:23 +01:00
$line -> id ,
( $line -> description ? $line -> description : $line -> desc ),
$line -> subprice ,
$line -> qty ,
$line -> remise_percent ,
$line -> tva_tx ,
$line -> localtax1_tx ,
$line -> localtax2_tx ,
'HT' ,
$line -> info_bits ,
$line -> product_type ,
false ,
$line -> date_start ,
$line -> date_end ,
$line -> array_options ,
$line -> fk_unit ,
2021-02-28 13:37:45 +01:00
$line -> multicurrency_subprice ,
2021-02-27 08:00:02 +01:00
$line -> ref_supplier
2018-05-27 15:04:12 +02:00
);
2017-10-07 13:09:31 +02:00
break ;
case 'invoice_supplier' :
2018-05-27 15:04:12 +02:00
$this -> updateline (
2021-02-23 22:03:23 +01:00
$line -> id ,
( $line -> description ? $line -> description : $line -> desc ),
$line -> subprice ,
$line -> tva_tx ,
$line -> localtax1_tx ,
$line -> localtax2_tx ,
$line -> qty ,
0 ,
'HT' ,
$line -> info_bits ,
$line -> product_type ,
$line -> remise_percent ,
false ,
$line -> date_start ,
$line -> date_end ,
$line -> array_options ,
$line -> fk_unit ,
2021-02-28 13:37:45 +01:00
$line -> multicurrency_subprice ,
2021-02-27 08:52:40 +01:00
$line -> ref_supplier
2018-05-27 15:04:12 +02:00
);
2017-10-07 13:09:31 +02:00
break ;
default :
dol_syslog ( get_class ( $this ) . '::setMulticurrencyRate no updateline defined' , LOG_DEBUG );
break ;
}
}
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2020-12-14 20:40:47 +01:00
dol_syslog ( get_class ( $this ) . '::setMulticurrencyRate Error ' . $sql . ' - ' . $this -> db -> error ());
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> error ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::setMulticurrencyRate, status of the object is incompatible' );
2019-11-13 19:34:37 +01:00
$this -> error = 'Status of the object is incompatible ' . $this -> statut ;
2017-10-07 13:09:31 +02:00
return - 2 ;
}
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
/**
* Change the payments terms
*
* @ param int $id Id of new payment terms
* @ return int > 0 if OK , < 0 if KO
*/
2019-02-25 22:27:04 +01:00
public function setPaymentTerms ( $id )
2017-10-07 13:09:31 +02:00
{
dol_syslog ( get_class ( $this ) . '::setPaymentTerms(' . $id . ')' );
2021-02-23 22:03:23 +01:00
if ( $this -> statut >= 0 || $this -> element == 'societe' ) {
2017-10-07 13:09:31 +02:00
// TODO uniformize field name
$fieldname = 'fk_cond_reglement' ;
2021-02-23 22:03:23 +01:00
if ( $this -> element == 'societe' ) {
$fieldname = 'cond_reglement' ;
}
if ( get_class ( $this ) == 'Fournisseur' ) {
$fieldname = 'cond_reglement_supplier' ;
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2018-12-28 23:13:39 +01:00
$sql .= ' SET ' . $fieldname . ' = ' . (( $id > 0 || $id == '0' ) ? $id : 'NULL' );
2021-03-14 12:20:23 +01:00
$sql .= ' WHERE rowid=' . (( int ) $this -> id );
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
$this -> cond_reglement_id = $id ;
// for supplier
2021-02-23 22:03:23 +01:00
if ( get_class ( $this ) == 'Fournisseur' ) {
$this -> cond_reglement_supplier_id = $id ;
}
2019-11-13 19:34:37 +01:00
$this -> cond_reglement = $id ; // for compatibility
2017-10-07 13:09:31 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2020-12-14 20:40:47 +01:00
dol_syslog ( get_class ( $this ) . '::setPaymentTerms Error ' . $sql . ' - ' . $this -> db -> error ());
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> error ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::setPaymentTerms, status of the object is incompatible' );
2019-11-13 19:34:37 +01:00
$this -> error = 'Status of the object is incompatible ' . $this -> statut ;
2017-10-07 13:09:31 +02:00
return - 2 ;
}
}
2020-12-14 20:40:47 +01:00
/**
* Change the transport mode methods
*
2021-06-03 21:47:10 +02:00
* @ param int $id Id of transport mode
2020-12-14 20:40:47 +01:00
* @ return int > 0 if OK , < 0 if KO
*/
public function setTransportMode ( $id )
{
dol_syslog ( get_class ( $this ) . '::setTransportMode(' . $id . ')' );
2021-02-23 22:03:23 +01:00
if ( $this -> statut >= 0 || $this -> element == 'societe' ) {
2020-12-14 20:40:47 +01:00
$fieldname = 'fk_transport_mode' ;
2021-02-23 22:03:23 +01:00
if ( $this -> element == 'societe' ) {
$fieldname = 'transport_mode' ;
}
if ( get_class ( $this ) == 'Fournisseur' ) {
$fieldname = 'transport_mode_supplier' ;
}
2020-12-14 20:40:47 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= ' SET ' . $fieldname . ' = ' . (( $id > 0 || $id == '0' ) ? $id : 'NULL' );
2021-03-14 12:20:23 +01:00
$sql .= ' WHERE rowid=' . (( int ) $this -> id );
2020-12-14 20:40:47 +01:00
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2020-12-14 20:40:47 +01:00
$this -> transport_mode_id = $id ;
// for supplier
2021-02-23 22:03:23 +01:00
if ( get_class ( $this ) == 'Fournisseur' ) {
$this -> transport_mode_supplier_id = $id ;
}
2020-12-14 20:40:47 +01:00
return 1 ;
} else {
dol_syslog ( get_class ( $this ) . '::setTransportMode Error ' . $sql . ' - ' . $this -> db -> error ());
$this -> error = $this -> db -> error ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2020-12-14 20:40:47 +01:00
} else {
dol_syslog ( get_class ( $this ) . '::setTransportMode, status of the object is incompatible' );
$this -> error = 'Status of the object is incompatible ' . $this -> statut ;
return - 2 ;
2017-10-07 13:09:31 +02:00
}
2020-12-14 20:40:47 +01:00
}
2019-08-03 16:32:35 +02:00
2018-08-10 15:06:00 +02:00
/**
* Change the retained warranty payments terms
*
* @ param int $id Id of new payment terms
* @ return int > 0 if OK , < 0 if KO
*/
2019-06-24 14:36:06 +02:00
public function setRetainedWarrantyPaymentTerms ( $id )
2018-08-10 15:06:00 +02:00
{
2020-09-07 10:18:17 +02:00
dol_syslog ( get_class ( $this ) . '::setRetainedWarrantyPaymentTerms(' . $id . ')' );
2021-02-23 22:03:23 +01:00
if ( $this -> statut >= 0 || $this -> element == 'societe' ) {
2020-09-07 10:18:17 +02:00
$fieldname = 'retained_warranty_fk_cond_reglement' ;
2019-08-03 16:32:35 +02:00
2020-09-07 10:18:17 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2021-03-30 11:36:50 +02:00
$sql .= ' SET ' . $fieldname . ' = ' . (( int ) $id );
2021-03-14 12:20:23 +01:00
$sql .= ' WHERE rowid=' . (( int ) $this -> id );
2019-08-03 16:32:35 +02:00
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2020-09-07 10:18:17 +02:00
$this -> retained_warranty_fk_cond_reglement = $id ;
return 1 ;
} else {
2020-12-14 20:40:47 +01:00
dol_syslog ( get_class ( $this ) . '::setRetainedWarrantyPaymentTerms Error ' . $sql . ' - ' . $this -> db -> error ());
2020-09-07 10:18:17 +02:00
$this -> error = $this -> db -> error ();
return - 1 ;
}
} else {
dol_syslog ( get_class ( $this ) . '::setRetainedWarrantyPaymentTerms, status of the object is incompatible' );
$this -> error = 'Status of the object is incompatible ' . $this -> statut ;
2017-10-07 13:09:31 +02:00
return - 2 ;
}
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
/**
* Define delivery address
* @ deprecated
*
* @ param int $id Address id
* @ return int < 0 si ko , > 0 si ok
*/
2019-02-25 22:27:04 +01:00
public function setDeliveryAddress ( $id )
2017-10-07 13:09:31 +02:00
{
$fieldname = 'fk_delivery_address' ;
2021-02-23 22:03:23 +01:00
if ( $this -> element == 'delivery' || $this -> element == 'shipping' ) {
$fieldname = 'fk_address' ;
}
2011-11-07 15:04:02 +01:00
2021-03-30 03:37:54 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element . " SET " . $fieldname . " = " . (( int ) $id );
2019-11-13 19:34:37 +01:00
$sql .= " WHERE rowid = " . $this -> id . " AND fk_statut = 0 " ;
2012-03-18 17:47:17 +01:00
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
$this -> fk_delivery_address = $id ;
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> error ();
2020-12-14 20:40:47 +01:00
dol_syslog ( get_class ( $this ) . '::setDeliveryAddress Error ' . $sql . ' - ' . $this -> error );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
/**
* Change the shipping method
*
* @ param int $shipping_method_id Id of shipping method
2019-05-13 22:02:33 +02:00
* @ param bool $notrigger false = launch triggers after , true = disable triggers
* @ param User $userused Object user
2018-01-26 08:33:35 +01:00
*
2017-10-07 13:09:31 +02:00
* @ return int 1 if OK , 0 if KO
*/
2019-02-25 22:27:04 +01:00
public function setShippingMethod ( $shipping_method_id , $notrigger = false , $userused = null )
2017-10-07 13:09:31 +02:00
{
2019-05-13 22:02:33 +02:00
global $user ;
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( empty ( $userused )) {
$userused = $user ;
}
2017-06-07 16:44:04 +02:00
2019-05-13 22:02:33 +02:00
$error = 0 ;
2011-11-07 15:04:02 +01:00
2019-11-13 19:34:37 +01:00
if ( ! $this -> table_element ) {
2019-01-27 11:55:16 +01:00
dol_syslog ( get_class ( $this ) . " ::setShippingMethod was called on objet with property table_element not defined " , LOG_ERR );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2017-06-07 16:44:04 +02:00
2019-05-13 22:02:33 +02:00
$this -> db -> begin ();
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( $shipping_method_id < 0 ) {
$shipping_method_id = 'NULL' ;
}
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::setShippingMethod(' . $shipping_method_id . ')' );
2017-06-07 16:44:04 +02:00
2017-10-07 13:09:31 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
2021-03-30 03:37:54 +02:00
$sql .= " SET fk_shipping_method = " . (( int ) $shipping_method_id );
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2019-05-13 22:02:33 +02:00
$resql = $this -> db -> query ( $sql );
2019-11-13 19:34:37 +01:00
if ( ! $resql ) {
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::setShippingMethod Error ' , LOG_DEBUG );
2018-01-26 08:33:35 +01:00
$this -> error = $this -> db -> lasterror ();
$error ++ ;
2019-05-13 22:02:33 +02:00
} else {
2021-02-23 22:03:23 +01:00
if ( ! $notrigger ) {
2019-05-13 22:02:33 +02:00
// Call trigger
2019-11-13 19:34:37 +01:00
$this -> context = array ( 'shippingmethodupdate' => 1 );
$result = $this -> call_trigger ( strtoupper ( get_class ( $this )) . '_MODIFY' , $userused );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2019-05-13 22:02:33 +02:00
// End call trigger
}
}
2021-02-23 22:03:23 +01:00
if ( $error ) {
2019-05-13 22:02:33 +02:00
$this -> db -> rollback ();
return - 1 ;
} else {
2019-11-13 19:34:37 +01:00
$this -> shipping_method_id = ( $shipping_method_id == 'NULL' ) ? null : $shipping_method_id ;
2019-05-13 22:02:33 +02:00
$this -> db -> commit ();
return 1 ;
}
2017-10-07 13:09:31 +02:00
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
/**
* Change the warehouse
*
* @ param int $warehouse_id Id of warehouse
* @ return int 1 if OK , 0 if KO
*/
2019-02-25 22:27:04 +01:00
public function setWarehouse ( $warehouse_id )
2017-10-07 13:09:31 +02:00
{
2019-11-13 19:34:37 +01:00
if ( ! $this -> table_element ) {
2019-01-27 11:55:16 +01:00
dol_syslog ( get_class ( $this ) . " ::setWarehouse was called on objet with property table_element not defined " , LOG_ERR );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2021-02-23 22:03:23 +01:00
if ( $warehouse_id < 0 ) {
$warehouse_id = 'NULL' ;
}
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::setWarehouse(' . $warehouse_id . ')' );
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
2021-03-30 03:37:54 +02:00
$sql .= " SET fk_warehouse = " . (( int ) $warehouse_id );
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
if ( $this -> db -> query ( $sql )) {
2019-11-13 19:34:37 +01:00
$this -> warehouse_id = ( $warehouse_id == 'NULL' ) ? null : $warehouse_id ;
2017-10-07 13:09:31 +02:00
return 1 ;
} else {
dol_syslog ( get_class ( $this ) . '::setWarehouse Error ' , LOG_DEBUG );
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> error ();
2017-10-07 13:09:31 +02:00
return 0 ;
}
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
/**
* Set last model used by doc generator
*
* @ param User $user User object that make change
* @ param string $modelpdf Modele name
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function setDocModel ( $user , $modelpdf )
2017-10-07 13:09:31 +02:00
{
2021-02-23 22:03:23 +01:00
if ( ! $this -> table_element ) {
2019-01-27 11:55:16 +01:00
dol_syslog ( get_class ( $this ) . " ::setDocModel was called on objet with property table_element not defined " , LOG_ERR );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2011-11-07 15:04:02 +01:00
2019-11-13 19:34:37 +01:00
$newmodelpdf = dol_trunc ( $modelpdf , 255 );
2017-10-07 13:09:31 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
2019-11-13 19:34:37 +01:00
$sql .= " SET model_pdf = ' " . $this -> db -> escape ( $newmodelpdf ) . " ' " ;
$sql .= " WHERE rowid = " . $this -> id ;
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::setDocModel " , LOG_DEBUG );
2019-11-13 19:34:37 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2020-08-18 14:48:38 +02:00
$this -> model_pdf = $modelpdf ;
2020-09-07 10:18:17 +02:00
$this -> modelpdf = $modelpdf ; // For bakward compatibility
2017-10-07 13:09:31 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
return 0 ;
}
}
/**
* Change the bank account
*
* @ param int $fk_account Id of bank account
2018-01-26 08:14:39 +01:00
* @ param bool $notrigger false = launch triggers after , true = disable triggers
* @ param User $userused Object user
2017-10-07 13:09:31 +02:00
* @ return int 1 if OK , 0 if KO
*/
2019-02-25 22:27:04 +01:00
public function setBankAccount ( $fk_account , $notrigger = false , $userused = null )
2017-10-07 13:09:31 +02:00
{
2019-05-13 22:02:33 +02:00
global $user ;
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( empty ( $userused )) {
$userused = $user ;
}
2011-11-07 15:04:02 +01:00
2019-05-13 22:02:33 +02:00
$error = 0 ;
2011-11-07 15:04:02 +01:00
2019-11-13 19:34:37 +01:00
if ( ! $this -> table_element ) {
2019-01-27 11:55:16 +01:00
dol_syslog ( get_class ( $this ) . " ::setBankAccount was called on objet with property table_element not defined " , LOG_ERR );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2019-05-13 22:02:33 +02:00
$this -> db -> begin ();
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( $fk_account < 0 ) {
$fk_account = 'NULL' ;
}
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . '::setBankAccount(' . $fk_account . ')' );
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
2021-03-30 03:37:54 +02:00
$sql .= " SET fk_account = " . (( int ) $fk_account );
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2011-11-07 15:04:02 +01:00
2019-05-13 22:02:33 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( ! $resql ) {
2019-05-13 22:02:33 +02:00
dol_syslog ( get_class ( $this ) . '::setBankAccount Error ' . $sql . ' - ' . $this -> db -> error ());
$this -> error = $this -> db -> lasterror ();
$error ++ ;
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 22:03:23 +01:00
if ( ! $notrigger ) {
2019-05-13 22:02:33 +02:00
// Call trigger
2019-11-13 19:34:37 +01:00
$this -> context = array ( 'bankaccountupdate' => 1 );
$result = $this -> call_trigger ( strtoupper ( get_class ( $this )) . '_MODIFY' , $userused );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2019-05-13 22:02:33 +02:00
// End call trigger
}
}
2021-02-23 22:03:23 +01:00
if ( $error ) {
2019-05-13 22:02:33 +02:00
$this -> db -> rollback ();
return - 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> fk_account = ( $fk_account == 'NULL' ) ? null : $fk_account ;
2019-05-13 22:02:33 +02:00
$this -> db -> commit ();
return 1 ;
}
}
2012-03-18 00:59:24 +01:00
2017-10-07 13:09:31 +02:00
// TODO: Move line related operations to CommonObjectLine?
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2016-01-18 19:45:27 +01:00
/**
2017-10-07 13:09:31 +02:00
* Save a new position ( field rang ) for details lines .
* You can choose to set position for lines with already a position or lines without any position defined .
*
* @ param boolean $renum True to renum all already ordered lines , false to renum only not already ordered lines .
* @ param string $rowidorder ASC or DESC
* @ param boolean $fk_parent_line Table with fk_parent_line field or not
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function line_order ( $renum = false , $rowidorder = 'ASC' , $fk_parent_line = true )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2021-02-23 22:03:23 +01:00
if ( ! $this -> table_element_line ) {
2019-01-27 11:55:16 +01:00
dol_syslog ( get_class ( $this ) . " ::line_order was called on objet with property table_element_line not defined " , LOG_ERR );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2021-02-23 22:03:23 +01:00
if ( ! $this -> fk_element ) {
2019-01-27 11:55:16 +01:00
dol_syslog ( get_class ( $this ) . " ::line_order was called on objet with property fk_element not defined " , LOG_ERR );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2016-06-30 17:28:13 +02:00
2017-10-07 13:09:31 +02:00
// Count number of lines to reorder (according to choice $renum)
2019-11-13 19:34:37 +01:00
$nl = 0 ;
2017-10-07 13:09:31 +02:00
$sql = 'SELECT count(rowid) FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
2019-11-13 19:34:37 +01:00
$sql .= ' WHERE ' . $this -> fk_element . '=' . $this -> id ;
2021-02-23 22:03:23 +01:00
if ( ! $renum ) {
$sql .= ' AND rang = 0' ;
}
if ( $renum ) {
$sql .= ' AND rang <> 0' ;
}
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::line_order " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$row = $this -> db -> fetch_row ( $resql );
$nl = $row [ 0 ];
2021-02-23 22:03:23 +01:00
} else {
dol_print_error ( $this -> db );
}
if ( $nl > 0 ) {
2020-12-14 20:40:47 +01:00
// The goal of this part is to reorder all lines, with all children lines sharing the same counter that parents.
2019-11-13 19:34:37 +01:00
$rows = array ();
2016-01-18 19:45:27 +01:00
2017-10-07 13:09:31 +02:00
// We first search all lines that are parent lines (for multilevel details lines)
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
2019-11-13 19:34:37 +01:00
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
2021-02-23 22:03:23 +01:00
if ( $fk_parent_line ) {
$sql .= ' AND fk_parent_line IS NULL' ;
}
2019-11-13 19:34:37 +01:00
$sql .= ' ORDER BY rang ASC, rowid ' . $rowidorder ;
2016-06-30 17:28:13 +02:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::line_order search all parent lines " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2019-11-13 19:34:37 +01:00
$i = 0 ;
2017-10-07 13:09:31 +02:00
$num = $this -> db -> num_rows ( $resql );
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2017-10-07 13:09:31 +02:00
$row = $this -> db -> fetch_row ( $resql );
2019-11-13 19:34:37 +01:00
$rows [] = $row [ 0 ]; // Add parent line into array rows
2017-10-07 13:09:31 +02:00
$childrens = $this -> getChildrenOfLine ( $row [ 0 ]);
2021-02-23 22:03:23 +01:00
if ( ! empty ( $childrens )) {
foreach ( $childrens as $child ) {
2017-10-07 13:09:31 +02:00
array_push ( $rows , $child );
}
}
$i ++ ;
}
2016-06-30 17:28:13 +02:00
2017-10-07 13:09:31 +02:00
// Now we set a new number for each lines (parent and children with children included into parent tree)
2021-02-23 22:03:23 +01:00
if ( ! empty ( $rows )) {
foreach ( $rows as $key => $row ) {
2019-11-13 19:34:37 +01:00
$this -> updateRangOfLine ( $row , ( $key + 1 ));
2017-10-07 13:09:31 +02:00
}
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
}
}
return 1 ;
}
2016-06-30 17:28:13 +02:00
2016-01-18 19:45:27 +01:00
/**
2017-10-07 13:09:31 +02:00
* Get children of line
*
2020-07-01 14:10:43 +02:00
* @ param int $id Id of parent line
* @ param int $includealltree 0 = 1 st level child , 1 = All level child
* @ return array Array with list of children lines id
2017-10-07 13:09:31 +02:00
*/
2020-07-01 14:10:43 +02:00
public function getChildrenOfLine ( $id , $includealltree = 0 )
2017-10-07 13:09:31 +02:00
{
2019-11-13 19:34:37 +01:00
$rows = array ();
2016-06-30 17:28:13 +02:00
2017-10-07 13:09:31 +02:00
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
2019-11-13 19:34:37 +01:00
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
$sql .= ' AND fk_parent_line = ' . $id ;
$sql .= ' ORDER BY rang ASC' ;
2016-01-18 19:45:27 +01:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::getChildrenOfLine search children lines for line " . $id . " " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2020-07-03 00:27:30 +02:00
if ( $this -> db -> num_rows ( $resql ) > 0 ) {
while ( $row = $this -> db -> fetch_row ( $resql )) {
2020-07-01 14:10:43 +02:00
$rows [] = $row [ 0 ];
2021-02-23 22:03:23 +01:00
if ( ! empty ( $includealltree )) {
$rows = array_merge ( $rows , $this -> getChildrenOfLine ( $row [ 0 ]), $includealltree );
}
2020-07-01 14:10:43 +02:00
}
2017-10-07 13:09:31 +02:00
}
}
return $rows ;
}
2017-06-07 16:44:04 +02:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Update a line to have a lower rank
*
* @ param int $rowid Id of line
* @ param boolean $fk_parent_line Table with fk_parent_line field or not
* @ return void
*/
2019-02-25 22:27:04 +01:00
public function line_up ( $rowid , $fk_parent_line = true )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
$this -> line_order ( false , 'ASC' , $fk_parent_line );
2016-06-30 17:28:13 +02:00
2017-10-07 13:09:31 +02:00
// Get rang of line
$rang = $this -> getRangOfLine ( $rowid );
2016-06-30 17:28:13 +02:00
2017-10-07 13:09:31 +02:00
// Update position of line
$this -> updateLineUp ( $rowid , $rang );
}
2016-01-18 19:45:27 +01:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Update a line to have a higher rank
*
* @ param int $rowid Id of line
* @ param boolean $fk_parent_line Table with fk_parent_line field or not
* @ return void
*/
2019-02-25 22:27:04 +01:00
public function line_down ( $rowid , $fk_parent_line = true )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
$this -> line_order ( false , 'ASC' , $fk_parent_line );
2012-03-18 00:59:24 +01:00
2017-10-07 13:09:31 +02:00
// Get rang of line
$rang = $this -> getRangOfLine ( $rowid );
// Get max value for rang
$max = $this -> line_max ();
// Update position of line
$this -> updateLineDown ( $rowid , $rang , $max );
}
/**
* Update position of line ( rang )
*
* @ param int $rowid Id of line
* @ param int $rang Position
* @ return void
*/
2019-02-25 22:27:04 +01:00
public function updateRangOfLine ( $rowid , $rang )
2017-10-07 13:09:31 +02:00
{
2019-12-11 19:11:13 +01:00
$fieldposition = 'rang' ; // @todo Rename 'rang' into 'position'
2021-02-23 22:03:23 +01:00
if ( in_array ( $this -> table_element_line , array ( 'bom_bomline' , 'ecm_files' , 'emailcollector_emailcollectoraction' ))) {
$fieldposition = 'position' ;
}
2017-10-07 13:09:31 +02:00
2021-03-23 16:09:02 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET ' . $fieldposition . ' = ' . (( int ) $rang );
$sql .= ' WHERE rowid = ' . (( int ) $rowid );
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::updateRangOfLine " , LOG_DEBUG );
2021-02-23 22:03:23 +01:00
if ( ! $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
}
}
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Update position of line with ajax ( rang )
*
* @ param array $rows Array of rows
* @ return void
*/
2019-02-25 22:27:04 +01:00
public function line_ajaxorder ( $rows )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
$num = count ( $rows );
2021-02-23 22:03:23 +01:00
for ( $i = 0 ; $i < $num ; $i ++ ) {
2019-11-13 19:34:37 +01:00
$this -> updateRangOfLine ( $rows [ $i ], ( $i + 1 ));
2017-10-07 13:09:31 +02:00
}
}
/**
* Update position of line up ( rang )
*
* @ param int $rowid Id of line
* @ param int $rang Position
* @ return void
*/
2019-02-25 22:27:04 +01:00
public function updateLineUp ( $rowid , $rang )
2017-10-07 13:09:31 +02:00
{
2021-02-23 22:03:23 +01:00
if ( $rang > 1 ) {
2018-11-15 19:58:06 +01:00
$fieldposition = 'rang' ;
2021-02-23 22:03:23 +01:00
if ( in_array ( $this -> table_element_line , array ( 'ecm_files' , 'emailcollector_emailcollectoraction' ))) {
$fieldposition = 'position' ;
}
2018-11-15 19:58:06 +01:00
2021-03-30 11:36:50 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET ' . $fieldposition . ' = ' . (( int ) $rang );
2019-11-13 19:34:37 +01:00
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
$sql .= ' AND rang = ' . ( $rang - 1 );
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2021-03-30 11:36:50 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET ' . $fieldposition . ' = ' . (( int ) ( $rang - 1 ));
2021-03-29 15:32:09 +02:00
$sql .= ' WHERE rowid = ' . (( int ) $rowid );
2021-02-23 22:03:23 +01:00
if ( ! $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
}
}
}
/**
* Update position of line down ( rang )
*
* @ param int $rowid Id of line
* @ param int $rang Position
* @ param int $max Max
* @ return void
*/
2019-02-25 22:27:04 +01:00
public function updateLineDown ( $rowid , $rang , $max )
2017-10-07 13:09:31 +02:00
{
2021-02-23 22:03:23 +01:00
if ( $rang < $max ) {
2018-11-15 19:58:06 +01:00
$fieldposition = 'rang' ;
2021-02-23 22:03:23 +01:00
if ( in_array ( $this -> table_element_line , array ( 'ecm_files' , 'emailcollector_emailcollectoraction' ))) {
$fieldposition = 'position' ;
}
2018-11-15 19:58:06 +01:00
2021-03-30 11:36:50 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET ' . $fieldposition . ' = ' . (( int ) $rang );
2019-11-13 19:34:37 +01:00
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
2021-03-30 11:36:50 +02:00
$sql .= ' AND rang = ' . (( int ) ( $rang + 1 ));
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2021-03-30 11:36:50 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element_line . ' SET ' . $fieldposition . ' = ' . (( int ) ( $rang + 1 ));
2021-03-29 15:32:09 +02:00
$sql .= ' WHERE rowid = ' . (( int ) $rowid );
2021-02-23 22:03:23 +01:00
if ( ! $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
}
}
}
2012-03-18 00:59:24 +01:00
2017-10-07 13:09:31 +02:00
/**
* Get position of line ( rang )
*
* @ param int $rowid Id of line
* @ return int Value of rang in table of lines
*/
2019-02-25 22:27:04 +01:00
public function getRangOfLine ( $rowid )
2017-10-07 13:09:31 +02:00
{
$sql = 'SELECT rang FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
2021-04-26 19:12:23 +02:00
$sql .= ' WHERE rowid =' . (( int ) $rowid );
2012-03-18 00:59:24 +01:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::getRangOfLine " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$row = $this -> db -> fetch_row ( $resql );
return $row [ 0 ];
}
}
2012-03-18 00:59:24 +01:00
2017-10-07 13:09:31 +02:00
/**
* Get rowid of the line relative to its position
*
* @ param int $rang Rang value
* @ return int Rowid of the line
*/
2019-02-25 22:27:04 +01:00
public function getIdOfLine ( $rang )
2017-10-07 13:09:31 +02:00
{
$sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
2019-11-13 19:34:37 +01:00
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
2021-04-26 19:12:23 +02:00
$sql .= ' AND rang = ' . (( int ) $rang );
2017-10-07 13:09:31 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$row = $this -> db -> fetch_row ( $resql );
return $row [ 0 ];
}
}
2014-07-24 18:08:08 +02:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Get max value used for position of line ( rang )
*
* @ param int $fk_parent_line Parent line id
* @ return int Max value of rang in table of lines
*/
2019-02-25 22:27:04 +01:00
public function line_max ( $fk_parent_line = 0 )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
$positionfield = 'rang' ;
2021-02-23 22:03:23 +01:00
if ( $this -> table_element == 'bom_bom' ) {
$positionfield = 'position' ;
}
2020-02-03 04:48:17 +01:00
2017-10-07 13:09:31 +02:00
// Search the last rang with fk_parent_line
2021-02-23 22:03:23 +01:00
if ( $fk_parent_line ) {
2020-02-03 04:48:17 +01:00
$sql = 'SELECT max(' . $positionfield . ') FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
2019-11-13 19:34:37 +01:00
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
$sql .= ' AND fk_parent_line = ' . $fk_parent_line ;
2014-07-24 18:08:08 +02:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::line_max " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$row = $this -> db -> fetch_row ( $resql );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $row [ 0 ])) {
2017-10-07 13:09:31 +02:00
return $row [ 0 ];
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
return $this -> getRangOfLine ( $fk_parent_line );
}
}
2021-03-01 20:37:16 +01:00
} else {
// If not, search the last rang of element
2020-02-03 04:48:17 +01:00
$sql = 'SELECT max(' . $positionfield . ') FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
2019-11-13 19:34:37 +01:00
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
2014-07-24 18:08:08 +02:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::line_max " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$row = $this -> db -> fetch_row ( $resql );
return $row [ 0 ];
}
}
}
2014-07-24 18:08:08 +02:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Update external ref of element
*
* @ param string $ref_ext Update field ref_ext
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function update_ref_ext ( $ref_ext )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2021-02-23 22:03:23 +01:00
if ( ! $this -> table_element ) {
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::update_ref_ext was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
2014-07-24 18:08:08 +02:00
2017-10-07 13:09:31 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2019-11-13 19:34:37 +01:00
$sql .= " SET ref_ext = ' " . $this -> db -> escape ( $ref_ext ) . " ' " ;
$sql .= " WHERE " . ( isset ( $this -> table_rowid ) ? $this -> table_rowid : 'rowid' ) . " = " . $this -> id ;
2015-10-19 21:09:02 +02:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::update_ref_ext " , LOG_DEBUG );
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
$this -> ref_ext = $ref_ext ;
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> error ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
}
2015-10-19 21:09:02 +02:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Update note of element
*
* @ param string $note New value for note
* @ param string $suffix '' , '_public' or '_private'
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function update_note ( $note , $suffix = '' )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
global $user ;
2015-10-19 21:09:02 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $this -> table_element ) {
2019-11-13 19:34:37 +01:00
$this -> error = 'update_note was called on objet with property table_element not defined' ;
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::update_note was called on objet with property table_element not defined " , LOG_ERR );
return - 1 ;
}
2021-02-23 22:03:23 +01:00
if ( ! in_array ( $suffix , array ( '' , '_public' , '_private' ))) {
2019-11-13 19:34:37 +01:00
$this -> error = 'update_note Parameter suffix must be empty, \'_private\' or \'_public\'' ;
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::update_note Parameter suffix must be empty, '_private' or '_public' " , LOG_ERR );
return - 2 ;
}
2015-10-19 21:09:02 +02:00
2020-08-02 17:09:21 +02:00
$newsuffix = $suffix ;
2017-10-07 13:09:31 +02:00
// Special cas
2021-02-23 22:03:23 +01:00
if ( $this -> table_element == 'product' && $newsuffix == '_private' ) {
$newsuffix = '' ;
}
2020-11-24 18:54:59 +01:00
if ( in_array ( $this -> table_element , array ( 'actioncomm' , 'adherent' , 'advtargetemailing' , 'cronjob' , 'establishment' ))) {
$fieldusermod = " fk_user_mod " ;
} elseif ( $this -> table_element == 'ecm_files' ) {
$fieldusermod = " fk_user_m " ;
} else {
$fieldusermod = " fk_user_modif " ;
}
2017-10-07 13:09:31 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element ;
2020-08-02 17:09:21 +02:00
$sql .= " SET note " . $newsuffix . " = " . ( ! empty ( $note ) ? ( " ' " . $this -> db -> escape ( $note ) . " ' " ) : " NULL " );
2020-11-24 18:54:59 +01:00
$sql .= " , " . $fieldusermod . " = " . $user -> id ;
2019-11-13 19:34:37 +01:00
$sql .= " WHERE rowid = " . $this -> id ;
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::update_note " , LOG_DEBUG );
2020-11-24 18:54:59 +01:00
if ( $this -> db -> query ( $sql )) {
if ( $suffix == '_public' ) {
$this -> note_public = $note ;
} elseif ( $suffix == '_private' ) {
$this -> note_private = $note ;
} else {
2019-11-13 19:34:37 +01:00
$this -> note = $note ; // deprecated
2017-10-07 13:09:31 +02:00
$this -> note_private = $note ;
}
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
}
2011-11-07 15:04:02 +01:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Update public note ( kept for backward compatibility )
*
* @ param string $note New value for note
* @ return int < 0 if KO , > 0 if OK
* @ deprecated
* @ see update_note ()
*/
2019-02-25 22:27:04 +01:00
public function update_note_public ( $note )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2019-01-27 11:55:16 +01:00
return $this -> update_note ( $note , '_public' );
2017-10-07 13:09:31 +02:00
}
2011-11-07 15:04:02 +01:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Update total_ht , total_ttc , total_vat , total_localtax1 , total_localtax2 for an object ( sum of lines ) .
* Must be called at end of methods addline or updateline .
*
* @ param int $exclspec > 0 = Exclude special product ( product_type = 9 )
* @ param string $roundingadjust 'none' = Do nothing , 'auto' = Use default method ( MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND if defined , or '0' ), '0' = Force mode total of rounding , '1' = Force mode rounding of total
* @ param int $nodatabaseupdate 1 = Do not update database . Update only properties of object .
* @ param Societe $seller If roundingadjust is '0' or '1' or maybe 'auto' , it means we recalculate total for lines before calculating total for object and for this , we need seller object .
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function update_price ( $exclspec = 0 , $roundingadjust = 'none' , $nodatabaseupdate = 0 , $seller = null )
2017-10-07 13:09:31 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2018-08-03 11:47:43 +02:00
global $conf , $hookmanager , $action ;
2017-10-07 13:09:31 +02:00
2020-09-07 10:18:17 +02:00
$parameters = array ( 'exclspec' => $exclspec , 'roundingadjust' => $roundingadjust , 'nodatabaseupdate' => $nodatabaseupdate , 'seller' => $seller );
$reshook = $hookmanager -> executeHooks ( 'updateTotalPrice' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook > 0 ) {
return 1 ; // replacement code
} elseif ( $reshook < 0 ) {
return - 1 ; // failure
} // reshook = 0 => execute normal code
2020-04-25 10:19:09 +02:00
2017-10-07 13:09:31 +02:00
// Some external module want no update price after a trigger because they have another method to calculate the total (ex: with an extrafield)
$MODULE = " " ;
2021-02-23 22:03:23 +01:00
if ( $this -> element == 'propal' ) {
2017-10-07 13:09:31 +02:00
$MODULE = " MODULE_DISALLOW_UPDATE_PRICE_PROPOSAL " ;
2021-02-23 22:03:23 +01:00
} elseif ( $this -> element == 'commande' || $this -> element == 'order' ) {
2017-10-07 13:09:31 +02:00
$MODULE = " MODULE_DISALLOW_UPDATE_PRICE_ORDER " ;
2021-02-23 22:03:23 +01:00
} elseif ( $this -> element == 'facture' || $this -> element == 'invoice' ) {
2017-10-07 13:09:31 +02:00
$MODULE = " MODULE_DISALLOW_UPDATE_PRICE_INVOICE " ;
2021-02-23 22:03:23 +01:00
} elseif ( $this -> element == 'facture_fourn' || $this -> element == 'supplier_invoice' || $this -> element == 'invoice_supplier' ) {
2017-10-07 13:09:31 +02:00
$MODULE = " MODULE_DISALLOW_UPDATE_PRICE_SUPPLIER_INVOICE " ;
2021-02-23 22:03:23 +01:00
} elseif ( $this -> element == 'order_supplier' || $this -> element == 'supplier_order' ) {
2017-10-07 13:09:31 +02:00
$MODULE = " MODULE_DISALLOW_UPDATE_PRICE_SUPPLIER_ORDER " ;
2021-02-23 22:03:23 +01:00
} elseif ( $this -> element == 'supplier_proposal' ) {
2017-10-07 13:09:31 +02:00
$MODULE = " MODULE_DISALLOW_UPDATE_PRICE_SUPPLIER_PROPOSAL " ;
2021-02-23 22:03:23 +01:00
}
2017-10-07 13:09:31 +02:00
2019-11-13 19:34:37 +01:00
if ( ! empty ( $MODULE )) {
if ( ! empty ( $conf -> global -> $MODULE )) {
2017-10-07 13:09:31 +02:00
$modsactivated = explode ( ',' , $conf -> global -> $MODULE );
foreach ( $modsactivated as $mod ) {
2021-02-23 22:03:23 +01:00
if ( $conf -> $mod -> enabled ) {
2017-10-07 13:09:31 +02:00
return 1 ; // update was disabled by specific setup
2021-02-23 22:03:23 +01:00
}
2017-10-07 13:09:31 +02:00
}
}
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/price.lib.php' ;
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( $roundingadjust == '-1' ) {
$roundingadjust = 'auto' ; // For backward compatibility
}
2014-07-06 20:49:34 +02:00
2019-11-13 19:34:37 +01:00
$forcedroundingmode = $roundingadjust ;
2021-02-23 22:03:23 +01:00
if ( $forcedroundingmode == 'auto' && isset ( $conf -> global -> MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND )) {
$forcedroundingmode = $conf -> global -> MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND ;
} elseif ( $forcedroundingmode == 'auto' ) {
$forcedroundingmode = '0' ;
}
2014-07-06 20:49:34 +02:00
2019-11-13 19:34:37 +01:00
$error = 0 ;
2014-07-06 20:49:34 +02:00
2017-10-07 13:09:31 +02:00
$multicurrency_tx = ! empty ( $this -> multicurrency_tx ) ? $this -> multicurrency_tx : 1 ;
2015-05-23 23:17:39 +02:00
2017-10-07 13:09:31 +02:00
// Define constants to find lines to sum
2019-11-13 19:34:37 +01:00
$fieldtva = 'total_tva' ;
$fieldlocaltax1 = 'total_localtax1' ;
$fieldlocaltax2 = 'total_localtax2' ;
$fieldup = 'subprice' ;
2021-02-23 22:03:23 +01:00
if ( $this -> element == 'facture_fourn' || $this -> element == 'invoice_supplier' ) {
2019-11-13 19:34:37 +01:00
$fieldtva = 'tva' ;
$fieldup = 'pu_ht' ;
2017-10-07 13:09:31 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $this -> element == 'expensereport' ) {
2019-11-13 19:34:37 +01:00
$fieldup = 'value_unit' ;
2017-10-07 13:09:31 +02:00
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$sql = 'SELECT rowid, qty, ' . $fieldup . ' as up, remise_percent, total_ht, ' . $fieldtva . ' as total_tva, total_ttc, ' . $fieldlocaltax1 . ' as total_localtax1, ' . $fieldlocaltax2 . ' as total_localtax2,' ;
2019-11-13 19:34:37 +01:00
$sql .= ' tva_tx as vatrate, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, info_bits, product_type' ;
2021-02-23 22:03:23 +01:00
if ( $this -> table_element_line == 'facturedet' ) {
$sql .= ', situation_percent' ;
}
2020-04-24 19:56:19 +02:00
$sql .= ', multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc' ;
2019-11-13 19:34:37 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
2021-02-23 22:03:23 +01:00
if ( $exclspec ) {
2019-11-13 19:34:37 +01:00
$product_field = 'product_type' ;
2021-02-23 22:03:23 +01:00
if ( $this -> table_element_line == 'contratdet' ) {
$product_field = '' ; // contratdet table has no product_type field
}
if ( $product_field ) {
$sql .= ' AND ' . $product_field . ' <> 9' ;
}
2017-10-07 13:09:31 +02:00
}
2019-11-13 19:34:37 +01:00
$sql .= ' ORDER by rowid' ; // We want to be sure to always use same order of line to not change lines differently when option MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND is used
2011-11-08 00:17:20 +01:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::update_price " , LOG_DEBUG );
2008-02-24 17:46:42 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$this -> total_ht = 0 ;
$this -> total_tva = 0 ;
$this -> total_localtax1 = 0 ;
$this -> total_localtax2 = 0 ;
$this -> total_ttc = 0 ;
$total_ht_by_vats = array ();
$total_tva_by_vats = array ();
$total_ttc_by_vats = array ();
2019-11-13 19:34:37 +01:00
$this -> multicurrency_total_ht = 0 ;
2017-10-07 13:09:31 +02:00
$this -> multicurrency_total_tva = 0 ;
$this -> multicurrency_total_ttc = 0 ;
2011-11-08 00:17:20 +01:00
2017-10-07 13:09:31 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
// Note: There is no check on detail line and no check on total, if $forcedroundingmode = 'none'
2019-11-13 19:34:37 +01:00
$parameters = array ( 'fk_element' => $obj -> rowid );
2018-08-03 11:47:43 +02:00
$reshook = $hookmanager -> executeHooks ( 'changeRoundingMode' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2021-02-23 22:03:23 +01:00
if ( empty ( $reshook ) && $forcedroundingmode == '0' ) { // Check if data on line are consistent. This may solve lines that were not consistent because set with $forcedroundingmode='auto'
2020-04-24 19:52:34 +02:00
// This part of code is to fix data. We should not call it too often.
2020-05-02 03:39:56 +02:00
$localtax_array = array ( $obj -> localtax1_type , $obj -> localtax1_tx , $obj -> localtax2_type , $obj -> localtax2_tx );
2019-11-13 19:34:37 +01:00
$tmpcal = calcul_price_total ( $obj -> qty , $obj -> up , $obj -> remise_percent , $obj -> vatrate , $obj -> localtax1_tx , $obj -> localtax2_tx , 0 , 'HT' , $obj -> info_bits , $obj -> product_type , $seller , $localtax_array , ( isset ( $obj -> situation_percent ) ? $obj -> situation_percent : 100 ), $multicurrency_tx );
2020-04-24 19:52:34 +02:00
2020-05-02 03:39:56 +02:00
$diff_when_using_price_ht = price2num ( $tmpcal [ 1 ] - $obj -> total_tva , 'MT' , 1 ); // If price was set with tax price adn unit price HT has a low number of digits, then we may have a diff on recalculation from unit price HT.
2020-04-24 19:56:19 +02:00
$diff_on_current_total = price2num ( $obj -> total_ttc - $obj -> total_ht - $obj -> total_tva - $obj -> total_localtax1 - $obj -> total_localtax2 , 'MT' , 1 );
2020-04-24 19:52:34 +02:00
//var_dump($obj->total_ht.' '.$obj->total_tva.' '.$obj->total_localtax1.' '.$obj->total_localtax2.' =? '.$obj->total_ttc);
//var_dump($diff_when_using_price_ht.' '.$diff_on_current_total);
2021-02-23 22:03:23 +01:00
if ( $diff_when_using_price_ht && $diff_on_current_total ) {
2019-11-13 19:34:37 +01:00
$sqlfix = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element_line . " SET " . $fieldtva . " = " . $tmpcal [ 1 ] . " , total_ttc = " . $tmpcal [ 2 ] . " WHERE rowid = " . $obj -> rowid ;
2020-04-24 19:52:34 +02:00
dol_syslog ( 'We found unconsistent data into detailed line (diff_when_using_price_ht = ' . $diff_when_using_price_ht . ' and diff_on_current_total = ' . $diff_on_current_total . ') for line rowid = ' . $obj -> rowid . " (total vat of line calculated= " . $tmpcal [ 1 ] . " , database= " . $obj -> total_tva . " ). We fix the total_vat and total_ttc of line by running sqlfix = " . $sqlfix , LOG_WARNING );
2020-04-24 19:56:19 +02:00
$resqlfix = $this -> db -> query ( $sqlfix );
2021-02-23 22:03:23 +01:00
if ( ! $resqlfix ) {
dol_print_error ( $this -> db , 'Failed to update line' );
}
2020-04-24 19:52:34 +02:00
$obj -> total_tva = $tmpcal [ 1 ];
$obj -> total_ttc = $tmpcal [ 2 ];
2012-01-23 22:23:00 +01:00
}
2008-02-24 17:46:42 +01:00
}
2012-01-23 22:23:00 +01:00
2019-11-13 19:34:37 +01:00
$this -> total_ht += $obj -> total_ht ; // The field visible at end of line detail
2017-10-07 13:09:31 +02:00
$this -> total_tva += $obj -> total_tva ;
$this -> total_localtax1 += $obj -> total_localtax1 ;
$this -> total_localtax2 += $obj -> total_localtax2 ;
$this -> total_ttc += $obj -> total_ttc ;
2019-11-13 19:34:37 +01:00
$this -> multicurrency_total_ht += $obj -> multicurrency_total_ht ; // The field visible at end of line detail
2017-10-07 13:09:31 +02:00
$this -> multicurrency_total_tva += $obj -> multicurrency_total_tva ;
$this -> multicurrency_total_ttc += $obj -> multicurrency_total_ttc ;
2021-02-23 22:03:23 +01:00
if ( ! isset ( $total_ht_by_vats [ $obj -> vatrate ])) {
$total_ht_by_vats [ $obj -> vatrate ] = 0 ;
}
if ( ! isset ( $total_tva_by_vats [ $obj -> vatrate ])) {
$total_tva_by_vats [ $obj -> vatrate ] = 0 ;
}
if ( ! isset ( $total_ttc_by_vats [ $obj -> vatrate ])) {
$total_ttc_by_vats [ $obj -> vatrate ] = 0 ;
}
2017-10-07 13:09:31 +02:00
$total_ht_by_vats [ $obj -> vatrate ] += $obj -> total_ht ;
$total_tva_by_vats [ $obj -> vatrate ] += $obj -> total_tva ;
$total_ttc_by_vats [ $obj -> vatrate ] += $obj -> total_ttc ;
2021-02-23 22:03:23 +01:00
if ( $forcedroundingmode == '1' ) { // Check if we need adjustement onto line for vat. TODO This works on the company currency but not on multicurrency
2019-11-13 19:34:37 +01:00
$tmpvat = price2num ( $total_ht_by_vats [ $obj -> vatrate ] * $obj -> vatrate / 100 , 'MT' , 1 );
$diff = price2num ( $total_tva_by_vats [ $obj -> vatrate ] - $tmpvat , 'MT' , 1 );
2017-10-07 13:09:31 +02:00
//print 'Line '.$i.' rowid='.$obj->rowid.' vat_rate='.$obj->vatrate.' total_ht='.$obj->total_ht.' total_tva='.$obj->total_tva.' total_ttc='.$obj->total_ttc.' total_ht_by_vats='.$total_ht_by_vats[$obj->vatrate].' total_tva_by_vats='.$total_tva_by_vats[$obj->vatrate].' (new calculation = '.$tmpvat.') total_ttc_by_vats='.$total_ttc_by_vats[$obj->vatrate].($diff?" => DIFF":"")."<br>\n";
2021-02-23 22:03:23 +01:00
if ( $diff ) {
2020-06-08 15:12:07 +02:00
if ( abs ( $diff ) > 0.1 ) {
2020-06-08 15:20:01 +02:00
$errmsg = 'A rounding difference was detected into TOTAL but is too high to be corrected. Some data in your line may be corrupted. Try to edit each line manually.' ;
dol_syslog ( $errmsg , LOG_WARNING );
dol_print_error ( '' , $errmsg );
2020-06-08 15:12:07 +02:00
exit ;
}
2019-11-13 19:34:37 +01:00
$sqlfix = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element_line . " SET " . $fieldtva . " = " . ( $obj -> total_tva - $diff ) . " , total_ttc = " . ( $obj -> total_ttc - $diff ) . " WHERE rowid = " . $obj -> rowid ;
2017-10-07 13:09:31 +02:00
dol_syslog ( 'We found a difference of ' . $diff . ' for line rowid = ' . $obj -> rowid . " . We fix the total_vat and total_ttc of line by running sqlfix = " . $sqlfix );
2019-11-13 19:34:37 +01:00
$resqlfix = $this -> db -> query ( $sqlfix );
2021-02-23 22:03:23 +01:00
if ( ! $resqlfix ) {
dol_print_error ( $this -> db , 'Failed to update line' );
}
2017-10-07 13:09:31 +02:00
$this -> total_tva -= $diff ;
$this -> total_ttc -= $diff ;
$total_tva_by_vats [ $obj -> vatrate ] -= $diff ;
$total_ttc_by_vats [ $obj -> vatrate ] -= $diff ;
}
}
$i ++ ;
}
// Add revenue stamp to total
2019-11-13 19:34:37 +01:00
$this -> total_ttc += isset ( $this -> revenuestamp ) ? $this -> revenuestamp : 0 ;
$this -> multicurrency_total_ttc += isset ( $this -> revenuestamp ) ? ( $this -> revenuestamp * $multicurrency_tx ) : 0 ;
2017-10-07 13:09:31 +02:00
// Situations totals
2021-02-23 22:03:23 +01:00
if ( ! empty ( $this -> situation_cycle_ref ) && $this -> situation_counter > 1 && method_exists ( $this , 'get_prev_sits' ) && $this -> type != $this :: TYPE_CREDIT_NOTE ) {
2017-10-06 12:55:48 +02:00
$prev_sits = $this -> get_prev_sits ();
2017-10-07 13:09:31 +02:00
foreach ( $prev_sits as $sit ) { // $sit is an object Facture loaded with a fetch.
$this -> total_ht -= $sit -> total_ht ;
$this -> total_tva -= $sit -> total_tva ;
$this -> total_localtax1 -= $sit -> total_localtax1 ;
$this -> total_localtax2 -= $sit -> total_localtax2 ;
$this -> total_ttc -= $sit -> total_ttc ;
$this -> multicurrency_total_ht -= $sit -> multicurrency_total_ht ;
$this -> multicurrency_total_tva -= $sit -> multicurrency_total_tva ;
$this -> multicurrency_total_ttc -= $sit -> multicurrency_total_ttc ;
}
}
$this -> db -> free ( $resql );
2021-09-22 15:11:18 +02:00
// Now update global field total_ht, total_ttc, total_tva, total_localtax1, total_localtax2, multicurrency_total_*
2019-11-13 19:34:37 +01:00
$fieldht = 'total_ht' ;
$fieldtva = 'tva' ;
$fieldlocaltax1 = 'localtax1' ;
$fieldlocaltax2 = 'localtax2' ;
$fieldttc = 'total_ttc' ;
2017-10-07 13:09:31 +02:00
// Specific code for backward compatibility with old field names
2021-02-23 22:03:23 +01:00
if ( $this -> element == 'facture' || $this -> element == 'facturerec' ) {
2021-02-28 07:32:23 +01:00
$fieldtva = 'total_tva' ;
2021-02-23 22:03:23 +01:00
}
if ( $this -> element == 'facture_fourn' || $this -> element == 'invoice_supplier' ) {
$fieldtva = 'total_tva' ;
}
if ( $this -> element == 'propal' ) {
$fieldtva = 'total_tva' ;
}
if ( $this -> element == 'expensereport' ) {
$fieldtva = 'total_tva' ;
}
if ( $this -> element == 'supplier_proposal' ) {
$fieldtva = 'total_tva' ;
}
if ( $this -> element == 'commande' ) {
$fieldtva = 'total_tva' ;
}
if ( $this -> element == 'order_supplier' ) {
$fieldtva = 'total_tva' ;
}
if ( empty ( $nodatabaseupdate )) {
2017-10-07 13:09:31 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element . ' SET' ;
2021-06-14 13:51:09 +02:00
$sql .= " " . $fieldht . " = " . price2num ( $this -> total_ht ) . " , " ;
$sql .= " " . $fieldtva . " = " . price2num ( $this -> total_tva ) . " , " ;
$sql .= " " . $fieldlocaltax1 . " = " . price2num ( $this -> total_localtax1 ) . " , " ;
$sql .= " " . $fieldlocaltax2 . " = " . price2num ( $this -> total_localtax2 ) . " , " ;
$sql .= " " . $fieldttc . " = " . price2num ( $this -> total_ttc );
$sql .= " , multicurrency_total_ht = " . price2num ( $this -> multicurrency_total_ht , 'MT' , 1 );
$sql .= " , multicurrency_total_tva = " . price2num ( $this -> multicurrency_total_tva , 'MT' , 1 );
$sql .= " , multicurrency_total_ttc = " . price2num ( $this -> multicurrency_total_ttc , 'MT' , 1 );
2017-10-07 13:09:31 +02:00
$sql .= ' WHERE rowid = ' . $this -> id ;
dol_syslog ( get_class ( $this ) . " ::update_price " , LOG_DEBUG );
2019-11-13 19:34:37 +01:00
$resql = $this -> db -> query ( $sql );
2021-09-22 15:11:18 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $resql ) {
2017-10-07 13:09:31 +02:00
$error ++ ;
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
$this -> errors [] = $this -> db -> lasterror ();
2012-01-23 22:23:00 +01:00
}
}
2017-10-07 13:09:31 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
return - 1 ;
2012-09-15 15:33:00 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-01-27 11:55:16 +01:00
dol_print_error ( $this -> db , 'Bad request in update_price' );
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2012-01-23 22:23:00 +01:00
}
2012-02-01 13:21:39 +01:00
2019-05-13 22:02:33 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2012-01-23 22:23:00 +01:00
/**
2017-10-07 13:09:31 +02:00
* Add objects linked in llx_element_element .
2012-02-01 13:21:39 +01:00
*
2017-10-07 13:09:31 +02:00
* @ param string $origin Linked element type
* @ param int $origin_id Linked element id
2021-01-06 11:14:13 +01:00
* @ param User $f_user User that create
2021-08-25 14:53:14 +02:00
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
2017-10-07 13:09:31 +02:00
* @ return int <= 0 if KO , > 0 if OK
2019-01-22 10:39:06 +01:00
* @ see fetchObjectLinked (), updateObjectLinked (), deleteObjectLinked ()
2012-01-23 22:23:00 +01:00
*/
2021-01-06 11:14:13 +01:00
public function add_object_linked ( $origin = null , $origin_id = null , $f_user = null , $notrigger = 0 )
2012-01-23 22:23:00 +01:00
{
2019-05-13 22:02:33 +02:00
// phpcs:enable
2021-01-06 11:14:13 +01:00
global $user ;
2019-11-13 19:34:37 +01:00
$origin = ( ! empty ( $origin ) ? $origin : $this -> origin );
$origin_id = ( ! empty ( $origin_id ) ? $origin_id : $this -> origin_id );
2021-01-06 11:14:13 +01:00
$f_user = isset ( $f_user ) ? $f_user : $user ;
2017-10-07 13:09:31 +02:00
// Special case
2021-02-23 22:03:23 +01:00
if ( $origin == 'order' ) {
$origin = 'commande' ;
}
if ( $origin == 'invoice' ) {
$origin = 'facture' ;
}
if ( $origin == 'invoice_template' ) {
$origin = 'facturerec' ;
}
if ( $origin == 'supplierorder' ) {
$origin = 'order_supplier' ;
}
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2021-01-06 11:14:13 +01:00
$error = 0 ;
2012-02-01 13:21:39 +01:00
2021-01-06 11:14:13 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " element_element ( " ;
2019-11-13 19:34:37 +01:00
$sql .= " fk_source " ;
$sql .= " , sourcetype " ;
$sql .= " , fk_target " ;
$sql .= " , targettype " ;
$sql .= " ) VALUES ( " ;
2021-08-25 14:53:14 +02:00
$sql .= (( int ) $origin_id );
2021-01-06 11:14:13 +01:00
$sql .= " , ' " . $this -> db -> escape ( $origin ) . " ' " ;
2021-08-25 14:53:14 +02:00
$sql .= " , " . (( int ) $this -> id );
2021-01-06 11:14:13 +01:00
$sql .= " , ' " . $this -> db -> escape ( $this -> element ) . " ' " ;
2019-11-13 19:34:37 +01:00
$sql .= " ) " ;
2017-10-07 13:09:31 +02:00
2021-01-06 11:14:13 +01:00
dol_syslog ( get_class ( $this ) . " ::add_object_linked " , LOG_DEBUG );
if ( $this -> db -> query ( $sql )) {
if ( ! $notrigger ) {
// Call trigger
$this -> context [ 'link_origin' ] = $origin ;
$this -> context [ 'link_origin_id' ] = $origin_id ;
$result = $this -> call_trigger ( 'OBJECT_LINK_INSERT' , $f_user );
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
}
} else {
$this -> error = $this -> db -> lasterror ();
$error ++ ;
}
if ( ! $error ) {
2019-05-13 22:02:33 +02:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-05-13 22:02:33 +02:00
$this -> db -> rollback ();
return 0 ;
}
2017-10-07 13:09:31 +02:00
}
/**
2018-09-28 17:38:29 +02:00
* Fetch array of objects linked to current object ( object of enabled modules only ) . Links are loaded into
2020-12-20 18:21:34 +01:00
* this -> linkedObjectsIds array +
2018-09-28 17:38:29 +02:00
* this -> linkedObjects array if $loadalsoobjects = 1
2017-10-07 13:09:31 +02:00
* Possible usage for parameters :
* - all parameters empty -> we look all link to current object ( current object can be source or target )
* - source id + type -> will get target list linked to source
* - target id + type -> will get source list linked to target
* - source id + type + target type -> will get target list of the type
* - target id + type + target source -> will get source list of the type
*
2018-09-28 17:38:29 +02:00
* @ param int $sourceid Object source id ( if not defined , id of object )
* @ param string $sourcetype Object source type ( if not defined , element name of object )
* @ param int $targetid Object target id ( if not defined , id of object )
* @ param string $targettype Object target type ( if not defined , elemennt name of object )
* @ param string $clause 'OR' or 'AND' clause used when both source id and target id are provided
* @ param int $alsosametype 0 = Return only links to object that differs from source type . 1 = Include also link to objects of same type .
* @ param string $orderby SQL 'ORDER BY' clause
* @ param int $loadalsoobjects Load also array this -> linkedObjects ( Use 0 to increase performances )
* @ return int < 0 if KO , > 0 if OK
2019-01-22 10:39:06 +01:00
* @ see add_object_linked (), updateObjectLinked (), deleteObjectLinked ()
2017-10-07 13:09:31 +02:00
*/
2019-02-25 22:27:04 +01:00
public function fetchObjectLinked ( $sourceid = null , $sourcetype = '' , $targetid = null , $targettype = '' , $clause = 'OR' , $alsosametype = 1 , $orderby = 'sourcetype' , $loadalsoobjects = 1 )
2017-10-07 13:09:31 +02:00
{
global $conf ;
2019-11-13 19:34:37 +01:00
$this -> linkedObjectsIds = array ();
$this -> linkedObjects = array ();
2017-10-07 13:09:31 +02:00
2019-11-13 19:34:37 +01:00
$justsource = false ;
$justtarget = false ;
$withtargettype = false ;
$withsourcetype = false ;
2017-10-07 13:09:31 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $sourceid ) && ! empty ( $sourcetype ) && empty ( $targetid )) {
2019-11-13 19:34:37 +01:00
$justsource = true ; // the source (id and type) is a search criteria
2021-02-23 22:03:23 +01:00
if ( ! empty ( $targettype )) {
$withtargettype = true ;
}
2017-10-07 13:09:31 +02:00
}
2021-02-23 22:03:23 +01:00
if ( ! empty ( $targetid ) && ! empty ( $targettype ) && empty ( $sourceid )) {
2019-11-13 19:34:37 +01:00
$justtarget = true ; // the target (id and type) is a search criteria
2021-02-23 22:03:23 +01:00
if ( ! empty ( $sourcetype )) {
$withsourcetype = true ;
}
2017-10-07 13:09:31 +02:00
}
2019-11-13 19:34:37 +01:00
$sourceid = ( ! empty ( $sourceid ) ? $sourceid : $this -> id );
$targetid = ( ! empty ( $targetid ) ? $targetid : $this -> id );
$sourcetype = ( ! empty ( $sourcetype ) ? $sourcetype : $this -> element );
$targettype = ( ! empty ( $targettype ) ? $targettype : $this -> element );
2017-10-07 13:09:31 +02:00
/* if ( empty ( $sourceid ) && empty ( $targetid ))
2018-09-28 17:38:29 +02:00
{
dol_syslog ( 'Bad usage of function. No source nor target id defined (nor as parameter nor as object id)' , LOG_ERR );
return - 1 ;
} */
2017-10-07 13:09:31 +02:00
// Links between objects are stored in table element_element
$sql = 'SELECT rowid, fk_source, sourcetype, fk_target, targettype' ;
2019-11-13 19:34:37 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'element_element' ;
$sql .= " WHERE " ;
2021-02-23 22:03:23 +01:00
if ( $justsource || $justtarget ) {
if ( $justsource ) {
2021-06-09 15:36:47 +02:00
$sql .= " fk_source = " . (( int ) $sourceid ) . " AND sourcetype = ' " . $this -> db -> escape ( $sourcetype ) . " ' " ;
2021-02-23 22:03:23 +01:00
if ( $withtargettype ) {
$sql .= " AND targettype = ' " . $this -> db -> escape ( $targettype ) . " ' " ;
}
} elseif ( $justtarget ) {
2021-06-09 15:36:47 +02:00
$sql .= " fk_target = " . (( int ) $targetid ) . " AND targettype = ' " . $this -> db -> escape ( $targettype ) . " ' " ;
2021-02-23 22:03:23 +01:00
if ( $withsourcetype ) {
$sql .= " AND sourcetype = ' " . $this -> db -> escape ( $sourcetype ) . " ' " ;
}
2017-10-07 13:09:31 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2021-06-09 15:36:47 +02:00
$sql .= " (fk_source = " . (( int ) $sourceid ) . " AND sourcetype = ' " . $this -> db -> escape ( $sourcetype ) . " ') " ;
$sql .= " " . $clause . " (fk_target = " . (( int ) $targetid ) . " AND targettype = ' " . $this -> db -> escape ( $targettype ) . " ') " ;
2017-10-07 13:09:31 +02:00
}
2018-04-10 16:23:56 +02:00
$sql .= ' ORDER BY ' . $orderby ;
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::fetchObjectLink " , LOG_DEBUG );
2012-01-23 22:23:00 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2012-01-23 22:23:00 +01:00
$num = $this -> db -> num_rows ( $resql );
2017-10-07 13:09:31 +02:00
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-23 22:03:23 +01:00
if ( $justsource || $justtarget ) {
if ( $justsource ) {
2019-11-13 19:34:37 +01:00
$this -> linkedObjectsIds [ $obj -> targettype ][ $obj -> rowid ] = $obj -> fk_target ;
2021-02-23 22:03:23 +01:00
} elseif ( $justtarget ) {
2019-11-13 19:34:37 +01:00
$this -> linkedObjectsIds [ $obj -> sourcetype ][ $obj -> rowid ] = $obj -> fk_source ;
2017-10-07 13:09:31 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 22:03:23 +01:00
if ( $obj -> fk_source == $sourceid && $obj -> sourcetype == $sourcetype ) {
2019-11-13 19:34:37 +01:00
$this -> linkedObjectsIds [ $obj -> targettype ][ $obj -> rowid ] = $obj -> fk_target ;
2017-10-07 13:09:31 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $obj -> fk_target == $targetid && $obj -> targettype == $targettype ) {
2019-11-13 19:34:37 +01:00
$this -> linkedObjectsIds [ $obj -> sourcetype ][ $obj -> rowid ] = $obj -> fk_source ;
2017-10-07 13:09:31 +02:00
}
}
2012-01-23 22:23:00 +01:00
$i ++ ;
2008-02-24 17:46:42 +01:00
}
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $this -> linkedObjectsIds )) {
2018-09-28 17:38:29 +02:00
$tmparray = $this -> linkedObjectsIds ;
2021-02-23 22:03:23 +01:00
foreach ( $tmparray as $objecttype => $objectids ) { // $objecttype is a module name ('facture', 'mymodule', ...) or a module name with a suffix ('project_task', 'mymodule_myobj', ...)
2017-10-07 13:09:31 +02:00
// Parse element/subelement (ex: project_task, cabinetmed_consultation, ...)
$module = $element = $subelement = $objecttype ;
2020-12-20 18:21:34 +01:00
$regs = array ();
2017-10-07 13:09:31 +02:00
if ( $objecttype != 'supplier_proposal' && $objecttype != 'order_supplier' && $objecttype != 'invoice_supplier'
2021-02-23 22:03:23 +01:00
&& preg_match ( '/^([^_]+)_([^_]+)/i' , $objecttype , $regs )) {
2017-10-07 13:09:31 +02:00
$module = $element = $regs [ 1 ];
$subelement = $regs [ 2 ];
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$classpath = $element . '/class' ;
// To work with non standard classpath or module name
2019-11-13 19:34:37 +01:00
if ( $objecttype == 'facture' ) {
2017-10-07 13:09:31 +02:00
$classpath = 'compta/facture/class' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'facturerec' ) {
2021-03-01 20:37:16 +01:00
$classpath = 'compta/facture/class' ;
$module = 'facture' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'propal' ) {
2017-10-07 13:09:31 +02:00
$classpath = 'comm/propal/class' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'supplier_proposal' ) {
2017-10-07 13:09:31 +02:00
$classpath = 'supplier_proposal/class' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'shipping' ) {
2021-03-01 20:37:16 +01:00
$classpath = 'expedition/class' ;
$subelement = 'expedition' ;
$module = 'expedition_bon' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'delivery' ) {
2021-03-01 20:37:16 +01:00
$classpath = 'delivery/class' ;
$subelement = 'delivery' ;
$module = 'delivery_note' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'invoice_supplier' || $objecttype == 'order_supplier' ) {
2021-03-01 20:37:16 +01:00
$classpath = 'fourn/class' ;
$module = 'fournisseur' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'fichinter' ) {
2021-03-01 20:37:16 +01:00
$classpath = 'fichinter/class' ;
$subelement = 'fichinter' ;
$module = 'ficheinter' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'subscription' ) {
2021-03-01 20:37:16 +01:00
$classpath = 'adherents/class' ;
$module = 'adherent' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'contact' ) {
2020-02-24 17:28:25 +01:00
$module = 'societe' ;
2017-10-07 13:09:31 +02:00
}
// Set classfile
2021-03-01 20:37:16 +01:00
$classfile = strtolower ( $subelement );
$classname = ucfirst ( $subelement );
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
if ( $objecttype == 'order' ) {
2021-03-01 20:37:16 +01:00
$classfile = 'commande' ;
$classname = 'Commande' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'invoice_supplier' ) {
2021-03-01 20:37:16 +01:00
$classfile = 'fournisseur.facture' ;
$classname = 'FactureFournisseur' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'order_supplier' ) {
2021-03-01 20:37:16 +01:00
$classfile = 'fournisseur.commande' ;
$classname = 'CommandeFournisseur' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'supplier_proposal' ) {
2021-03-01 20:37:16 +01:00
$classfile = 'supplier_proposal' ;
$classname = 'SupplierProposal' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'facturerec' ) {
2021-03-01 20:37:16 +01:00
$classfile = 'facture-rec' ;
$classname = 'FactureRec' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'subscription' ) {
2021-03-01 20:37:16 +01:00
$classfile = 'subscription' ;
$classname = 'Subscription' ;
2020-05-21 15:05:19 +02:00
} elseif ( $objecttype == 'project' || $objecttype == 'projet' ) {
2021-03-01 20:37:16 +01:00
$classpath = 'projet/class' ;
$classfile = 'project' ;
$classname = 'Project' ;
2021-04-20 16:09:22 +02:00
} elseif ( $objecttype == 'conferenceorboothattendee' ) {
$classpath = 'eventorganization/class' ;
$classfile = 'conferenceorboothattendee' ;
$classname = 'ConferenceOrBoothAttendee' ;
$module = 'eventorganization' ;
2021-07-29 17:18:05 +02:00
} elseif ( $objecttype == 'conferenceorbooth' ) {
$classpath = 'eventorganization/class' ;
$classfile = 'conferenceorbooth' ;
$classname = 'ConferenceOrBooth' ;
$module = 'eventorganization' ;
2017-10-07 13:09:31 +02:00
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
// Here $module, $classfile and $classname are set
2021-02-23 22:03:23 +01:00
if ( $conf -> $module -> enabled && (( $element != $this -> element ) || $alsosametype )) {
if ( $loadalsoobjects ) {
2018-09-28 17:38:29 +02:00
dol_include_once ( '/' . $classpath . '/' . $classfile . '.class.php' );
//print '/'.$classpath.'/'.$classfile.'.class.php '.class_exists($classname);
2021-02-23 22:03:23 +01:00
if ( class_exists ( $classname )) {
foreach ( $objectids as $i => $objectid ) { // $i is rowid into llx_element_element
2018-09-28 17:38:29 +02:00
$object = new $classname ( $this -> db );
$ret = $object -> fetch ( $objectid );
2021-02-23 22:03:23 +01:00
if ( $ret >= 0 ) {
2018-09-28 17:38:29 +02:00
$this -> linkedObjects [ $objecttype ][ $i ] = $object ;
}
2017-10-07 13:09:31 +02:00
}
}
}
2020-05-21 15:05:19 +02:00
} else {
2018-09-28 17:38:29 +02:00
unset ( $this -> linkedObjectsIds [ $objecttype ]);
}
2017-10-06 10:42:25 +02:00
}
2017-10-06 12:55:48 +02:00
}
2018-03-04 11:22:43 +01:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
2018-03-04 11:22:43 +01:00
return - 1 ;
2017-10-07 13:09:31 +02:00
}
}
2011-11-07 15:04:02 +01:00
2010-08-20 19:57:25 +02:00
/**
2017-10-07 13:09:31 +02:00
* Update object linked of a current object
2011-08-28 14:30:15 +02:00
*
2017-10-07 13:09:31 +02:00
* @ param int $sourceid Object source id
* @ param string $sourcetype Object source type
* @ param int $targetid Object target id
* @ param string $targettype Object target type
2021-01-06 11:14:13 +01:00
* @ param User $f_user User that create
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
2017-10-07 13:09:31 +02:00
* @ return int > 0 if OK , < 0 if KO
2019-01-22 10:39:06 +01:00
* @ see add_object_linked (), fetObjectLinked (), deleteObjectLinked ()
2010-08-20 19:57:25 +02:00
*/
2021-01-06 11:14:13 +01:00
public function updateObjectLinked ( $sourceid = null , $sourcetype = '' , $targetid = null , $targettype = '' , $f_user = null , $notrigger = 0 )
2010-08-20 19:57:25 +02:00
{
2021-01-06 11:14:13 +01:00
global $user ;
2019-11-13 19:34:37 +01:00
$updatesource = false ;
$updatetarget = false ;
2021-01-06 11:14:13 +01:00
$f_user = isset ( $f_user ) ? $f_user : $user ;
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $sourceid ) && ! empty ( $sourcetype ) && empty ( $targetid ) && empty ( $targettype )) {
$updatesource = true ;
} elseif ( empty ( $sourceid ) && empty ( $sourcetype ) && ! empty ( $targetid ) && ! empty ( $targettype )) {
$updatetarget = true ;
}
2011-11-08 00:17:20 +01:00
2021-01-06 11:14:13 +01:00
$this -> db -> begin ();
$error = 0 ;
2017-10-06 12:55:48 +02:00
2021-01-06 11:14:13 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " element_element SET " ;
if ( $updatesource ) {
2021-08-23 18:56:46 +02:00
$sql .= " fk_source = " . (( int ) $sourceid );
2021-01-06 11:14:13 +01:00
$sql .= " , sourcetype = ' " . $this -> db -> escape ( $sourcetype ) . " ' " ;
2021-08-23 18:56:46 +02:00
$sql .= " WHERE fk_target = " . (( int ) $this -> id );
2021-01-06 11:14:13 +01:00
$sql .= " AND targettype = ' " . $this -> db -> escape ( $this -> element ) . " ' " ;
} elseif ( $updatetarget ) {
2021-08-23 18:56:46 +02:00
$sql .= " fk_target = " . (( int ) $targetid );
2021-01-06 11:14:13 +01:00
$sql .= " , targettype = ' " . $this -> db -> escape ( $targettype ) . " ' " ;
2021-08-23 18:56:46 +02:00
$sql .= " WHERE fk_source = " . (( int ) $this -> id );
2021-01-06 11:14:13 +01:00
$sql .= " AND sourcetype = ' " . $this -> db -> escape ( $this -> element ) . " ' " ;
2010-08-20 19:57:25 +02:00
}
2011-11-07 15:04:02 +01:00
2021-01-06 11:14:13 +01:00
dol_syslog ( get_class ( $this ) . " ::updateObjectLinked " , LOG_DEBUG );
if ( $this -> db -> query ( $sql )) {
if ( ! $notrigger ) {
// Call trigger
$this -> context [ 'link_source_id' ] = $sourceid ;
$this -> context [ 'link_source_type' ] = $sourcetype ;
$this -> context [ 'link_target_id' ] = $targetid ;
$this -> context [ 'link_target_type' ] = $targettype ;
$result = $this -> call_trigger ( 'OBJECT_LINK_UPDATE' , $f_user );
if ( $result < 0 ) {
$error ++ ;
}
// End call triggers
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2021-01-06 11:14:13 +01:00
$error ++ ;
}
if ( ! $error ) {
$this -> db -> commit ();
return 1 ;
} else {
$this -> db -> rollback ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
}
/**
* Delete all links between an object $this
*
* @ param int $sourceid Object source id
* @ param string $sourcetype Object source type
* @ param int $targetid Object target id
* @ param string $targettype Object target type
* @ param int $rowid Row id of line to delete . If defined , other parameters are not used .
2021-01-06 11:14:13 +01:00
* @ param User $f_user User that create
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
2017-10-07 13:09:31 +02:00
* @ return int > 0 if OK , < 0 if KO
2019-04-04 18:33:12 +02:00
* @ see add_object_linked (), updateObjectLinked (), fetchObjectLinked ()
2017-10-07 13:09:31 +02:00
*/
2021-01-06 11:14:13 +01:00
public function deleteObjectLinked ( $sourceid = null , $sourcetype = '' , $targetid = null , $targettype = '' , $rowid = '' , $f_user = null , $notrigger = 0 )
2017-10-07 13:09:31 +02:00
{
2021-01-06 11:14:13 +01:00
global $user ;
2019-11-13 19:34:37 +01:00
$deletesource = false ;
$deletetarget = false ;
2021-01-06 11:14:13 +01:00
$f_user = isset ( $f_user ) ? $f_user : $user ;
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $sourceid ) && ! empty ( $sourcetype ) && empty ( $targetid ) && empty ( $targettype )) {
$deletesource = true ;
} elseif ( empty ( $sourceid ) && empty ( $sourcetype ) && ! empty ( $targetid ) && ! empty ( $targettype )) {
$deletetarget = true ;
}
2011-11-07 15:04:02 +01:00
2019-11-13 19:34:37 +01:00
$sourceid = ( ! empty ( $sourceid ) ? $sourceid : $this -> id );
$sourcetype = ( ! empty ( $sourcetype ) ? $sourcetype : $this -> element );
$targetid = ( ! empty ( $targetid ) ? $targetid : $this -> id );
$targettype = ( ! empty ( $targettype ) ? $targettype : $this -> element );
2021-01-06 11:14:13 +01:00
$this -> db -> begin ();
$error = 0 ;
2011-11-07 15:04:02 +01:00
2021-01-06 11:14:13 +01:00
if ( ! $notrigger ) {
// Call trigger
$this -> context [ 'link_id' ] = $rowid ;
$this -> context [ 'link_source_id' ] = $sourceid ;
$this -> context [ 'link_source_type' ] = $sourcetype ;
$this -> context [ 'link_target_id' ] = $targetid ;
$this -> context [ 'link_target_type' ] = $targettype ;
$result = $this -> call_trigger ( 'OBJECT_LINK_DELETE' , $f_user );
if ( $result < 0 ) {
$error ++ ;
2017-10-07 13:09:31 +02:00
}
2021-01-06 11:14:13 +01:00
// End call triggers
}
if ( ! $error ) {
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " element_element " ;
$sql .= " WHERE " ;
if ( $rowid > 0 ) {
2021-04-24 20:18:11 +02:00
$sql .= " rowid = " . (( int ) $rowid );
2020-05-21 15:05:19 +02:00
} else {
2021-01-06 11:14:13 +01:00
if ( $deletesource ) {
2021-08-23 18:56:46 +02:00
$sql .= " fk_source = " . (( int ) $sourceid ) . " AND sourcetype = ' " . $this -> db -> escape ( $sourcetype ) . " ' " ;
$sql .= " AND fk_target = " . (( int ) $this -> id ) . " AND targettype = ' " . $this -> db -> escape ( $this -> element ) . " ' " ;
2021-01-06 11:14:13 +01:00
} elseif ( $deletetarget ) {
2021-08-23 18:56:46 +02:00
$sql .= " fk_target = " . (( int ) $targetid ) . " AND targettype = ' " . $this -> db -> escape ( $targettype ) . " ' " ;
$sql .= " AND fk_source = " . (( int ) $this -> id ) . " AND sourcetype = ' " . $this -> db -> escape ( $this -> element ) . " ' " ;
2021-01-06 11:14:13 +01:00
} else {
2021-08-23 18:56:46 +02:00
$sql .= " (fk_source = " . (( int ) $this -> id ) . " AND sourcetype = ' " . $this -> db -> escape ( $this -> element ) . " ') " ;
2021-01-06 11:14:13 +01:00
$sql .= " OR " ;
2021-08-23 18:56:46 +02:00
$sql .= " (fk_target = " . (( int ) $this -> id ) . " AND targettype = ' " . $this -> db -> escape ( $this -> element ) . " ') " ;
2021-01-06 11:14:13 +01:00
}
2017-10-07 13:09:31 +02:00
}
2021-01-06 11:14:13 +01:00
dol_syslog ( get_class ( $this ) . " ::deleteObjectLinked " , LOG_DEBUG );
if ( ! $this -> db -> query ( $sql )) {
$this -> error = $this -> db -> lasterror ();
$this -> errors [] = $this -> error ;
$error ++ ;
2017-10-07 13:09:31 +02:00
}
}
2011-11-07 15:04:02 +01:00
2021-01-06 11:14:13 +01:00
if ( ! $error ) {
$this -> db -> commit ();
2017-10-07 13:09:31 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2021-01-06 11:14:13 +01:00
$this -> db -> rollback ();
return 0 ;
2017-10-07 13:09:31 +02:00
}
}
2011-11-07 15:04:02 +01:00
2021-01-11 11:36:19 +01:00
/**
* Function used to get an array with all items linked to an object id in association table
2021-01-13 17:45:17 +01:00
*
2021-01-11 11:36:19 +01:00
* @ param int $fk_object_where id of object we need to get linked items
* @ param string $field_select name of field we need to get a list
* @ param string $field_where name of field of object we need to get linked items
* @ param string $table_element name of association table
2021-01-25 12:39:44 +01:00
* @ return array Array of record
2021-01-11 11:36:19 +01:00
*/
2021-02-23 22:03:23 +01:00
public static function getAllItemsLinkedByObjectID ( $fk_object_where , $field_select , $field_where , $table_element )
2021-01-11 11:36:19 +01:00
{
2021-01-13 16:18:23 +01:00
if ( empty ( $fk_object_where ) || empty ( $field_where ) || empty ( $table_element )) {
return - 1 ;
}
2021-01-11 11:36:19 +01:00
global $db ;
2021-01-25 12:40:52 +01:00
$sql = 'SELECT ' . $field_select . ' FROM ' . MAIN_DB_PREFIX . $table_element . ' WHERE ' . $field_where . ' = ' . (( int ) $fk_object_where );
2021-01-11 11:36:19 +01:00
$resql = $db -> query ( $sql );
$TRes = array ();
if ( ! empty ( $resql )) {
while ( $res = $db -> fetch_object ( $resql )) {
$TRes [] = $res -> { $field_select };
}
}
return $TRes ;
}
/**
* Function used to remove all items linked to an object id in association table
2021-01-13 17:45:17 +01:00
*
2021-01-11 11:36:19 +01:00
* @ param int $fk_object_where id of object we need to remove linked items
* @ param string $field_where name of field of object we need to delete linked items
* @ param string $table_element name of association table
2021-01-25 12:39:44 +01:00
* @ return int < 0 if KO , 0 if nothing done , > 0 if OK and something done
2021-01-11 11:36:19 +01:00
*/
2021-02-23 22:03:23 +01:00
public static function deleteAllItemsLinkedByObjectID ( $fk_object_where , $field_where , $table_element )
2021-01-11 11:36:19 +01:00
{
2021-01-13 16:23:54 +01:00
if ( empty ( $fk_object_where ) || empty ( $field_where ) || empty ( $table_element )) {
2017-10-07 13:09:31 +02:00
return - 1 ;
}
2021-01-11 11:36:19 +01:00
global $db ;
2021-03-30 17:45:15 +02:00
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $table_element . ' WHERE ' . $field_where . ' = ' . (( int ) $fk_object_where );
2021-01-11 11:36:19 +01:00
$resql = $db -> query ( $sql );
2021-01-13 16:23:54 +01:00
if ( empty ( $resql )) {
return 0 ;
}
2021-01-11 11:36:19 +01:00
return 1 ;
2017-10-07 13:09:31 +02:00
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
/**
* Set status of an object
*
* @ param int $status Status to set
* @ param int $elementId Id of element to force ( use this -> id by default )
* @ param string $elementType Type of element to force ( use this -> table_element by default )
2018-03-22 21:42:19 +01:00
* @ param string $trigkey Trigger key to use for trigger
2017-10-07 13:09:31 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function setStatut ( $status , $elementId = null , $elementType = '' , $trigkey = '' )
2017-10-07 13:09:31 +02:00
{
2019-11-13 19:34:37 +01:00
global $user , $langs , $conf ;
2011-11-07 15:04:02 +01:00
2019-11-13 19:34:37 +01:00
$savElementId = $elementId ; // To be used later to know if we were using the method using the id of this or not.
2011-11-07 15:04:02 +01:00
2019-11-13 19:34:37 +01:00
$elementId = ( ! empty ( $elementId ) ? $elementId : $this -> id );
$elementTable = ( ! empty ( $elementType ) ? $elementType : $this -> table_element );
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2011-11-07 15:04:02 +01:00
2019-11-13 19:34:37 +01:00
$fieldstatus = " fk_statut " ;
2021-02-23 22:03:23 +01:00
if ( $elementTable == 'facture_rec' ) {
$fieldstatus = " suspended " ;
}
if ( $elementTable == 'mailing' ) {
$fieldstatus = " statut " ;
}
if ( $elementTable == 'cronjob' ) {
$fieldstatus = " status " ;
}
if ( $elementTable == 'user' ) {
$fieldstatus = " statut " ;
}
if ( $elementTable == 'expensereport' ) {
$fieldstatus = " fk_statut " ;
}
if ( $elementTable == 'commande_fournisseur_dispatch' ) {
$fieldstatus = " status " ;
}
if ( is_array ( $this -> fields ) && array_key_exists ( 'status' , $this -> fields )) {
$fieldstatus = 'status' ;
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $elementTable ;
2021-06-09 15:36:47 +02:00
$sql .= " SET " . $fieldstatus . " = " . (( int ) $status );
2017-10-07 13:09:31 +02:00
// If status = 1 = validated, update also fk_user_valid
2021-02-23 22:03:23 +01:00
if ( $status == 1 && $elementTable == 'expensereport' ) {
2021-08-11 22:07:13 +02:00
$sql .= " , fk_user_valid = " . (( int ) $user -> id );
2021-02-23 22:03:23 +01:00
}
2021-06-09 15:36:47 +02:00
$sql .= " WHERE rowid= " . (( int ) $elementId );
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::setStatut " , LOG_DEBUG );
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
$error = 0 ;
2011-11-07 15:04:02 +01:00
2018-03-22 21:42:19 +01:00
// Try autoset of trigkey
2021-02-23 22:03:23 +01:00
if ( empty ( $trigkey )) {
if ( $this -> element == 'supplier_proposal' && $status == 2 ) {
$trigkey = 'SUPPLIER_PROPOSAL_SIGN' ; // 2 = SupplierProposal::STATUS_SIGNED. Can't use constant into this generic class
}
if ( $this -> element == 'supplier_proposal' && $status == 3 ) {
$trigkey = 'SUPPLIER_PROPOSAL_REFUSE' ; // 3 = SupplierProposal::STATUS_REFUSED. Can't use constant into this generic class
}
if ( $this -> element == 'supplier_proposal' && $status == 4 ) {
$trigkey = 'SUPPLIER_PROPOSAL_CLOSE' ; // 4 = SupplierProposal::STATUS_CLOSED. Can't use constant into this generic class
}
if ( $this -> element == 'fichinter' && $status == 3 ) {
$trigkey = 'FICHINTER_CLASSIFY_DONE' ;
}
if ( $this -> element == 'fichinter' && $status == 2 ) {
$trigkey = 'FICHINTER_CLASSIFY_BILLED' ;
}
if ( $this -> element == 'fichinter' && $status == 1 ) {
$trigkey = 'FICHINTER_CLASSIFY_UNBILLED' ;
}
2018-03-22 21:42:19 +01:00
}
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( $trigkey ) {
2020-04-02 12:03:29 +02:00
// Call trigger
$result = $this -> call_trigger ( $trigkey , $user );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2020-04-02 12:03:29 +02:00
// End call triggers
2017-10-07 13:09:31 +02:00
}
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
2013-04-10 00:08:55 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $savElementId )) { // If the element we update was $this (so $elementId is null)
2017-10-07 13:09:31 +02:00
$this -> statut = $status ;
$this -> status = $status ;
2017-10-06 12:55:48 +02:00
}
2011-11-07 15:04:02 +01:00
2017-10-06 12:55:48 +02:00
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
2020-01-06 13:25:14 +01:00
dol_syslog ( get_class ( $this ) . " ::setStatut " . $this -> error , LOG_ERR );
2017-10-06 12:55:48 +02:00
return - 1 ;
2017-10-07 13:09:31 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
2013-04-16 01:13:20 +02:00
2013-07-04 00:11:40 +02:00
2017-10-07 13:09:31 +02:00
/**
* Load type of canvas of an object if it exists
*
* @ param int $id Record id
* @ param string $ref Record ref
* @ return int < 0 if KO , 0 if nothing done , > 0 if OK
*/
2019-02-25 22:27:04 +01:00
public function getCanvas ( $id = 0 , $ref = '' )
2017-10-07 13:09:31 +02:00
{
global $conf ;
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( empty ( $id ) && empty ( $ref )) {
return 0 ;
}
if ( ! empty ( $conf -> global -> MAIN_DISABLE_CANVAS )) {
return 0 ; // To increase speed. Not enabled by default.
}
2014-05-13 13:22:29 +02:00
2017-10-07 13:09:31 +02:00
// Clean parameters
$ref = trim ( $ref );
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$sql = " SELECT rowid, canvas " ;
2019-11-13 19:34:37 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " WHERE entity IN ( " . getEntity ( $this -> element ) . " ) " ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $id )) {
2021-03-14 11:48:39 +01:00
$sql .= " AND rowid = " . (( int ) $id );
2021-02-23 22:03:23 +01:00
}
if ( ! empty ( $ref )) {
$sql .= " AND ref = ' " . $this -> db -> escape ( $ref ) . " ' " ;
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-23 22:03:23 +01:00
if ( $obj ) {
2019-11-13 19:34:37 +01:00
$this -> canvas = $obj -> canvas ;
2017-10-07 13:09:31 +02:00
return 1 ;
2021-02-23 22:03:23 +01:00
} else {
return 0 ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
return - 1 ;
}
}
2017-09-13 19:27:21 +02:00
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
/**
* Get special code of a line
*
* @ param int $lineid Id of line
* @ return int Special code
*/
2019-02-25 22:27:04 +01:00
public function getSpecialCode ( $lineid )
2017-10-07 13:09:31 +02:00
{
$sql = 'SELECT special_code FROM ' . MAIN_DB_PREFIX . $this -> table_element_line ;
2021-03-30 11:36:50 +02:00
$sql .= ' WHERE rowid = ' . (( int ) $lineid );
2017-10-07 13:09:31 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$row = $this -> db -> fetch_row ( $resql );
return $row [ 0 ];
}
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
/**
* Function to check if an object is used by others .
* Check is done into this -> childtables . There is no check into llx_element_element .
*
* @ param int $id Force id of object
* @ return int < 0 if KO , 0 if not used , > 0 if already used
*/
2019-02-25 22:27:04 +01:00
public function isObjectUsed ( $id = 0 )
2017-10-07 13:09:31 +02:00
{
global $langs ;
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
if ( empty ( $id )) {
$id = $this -> id ;
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
// Check parameters
2021-02-23 22:03:23 +01:00
if ( ! isset ( $this -> childtables ) || ! is_array ( $this -> childtables ) || count ( $this -> childtables ) == 0 ) {
2017-10-07 13:09:31 +02:00
dol_print_error ( 'Called isObjectUsed on a class with property this->childtables not defined' );
return - 1 ;
}
2013-04-07 17:39:08 +02:00
2017-10-07 13:09:31 +02:00
$arraytoscan = $this -> childtables ;
// For backward compatibility, we check if array is old format array('table1', 'table2', ...)
2019-11-13 19:34:37 +01:00
$tmparray = array_keys ( $this -> childtables );
2021-02-23 22:03:23 +01:00
if ( is_numeric ( $tmparray [ 0 ])) {
2017-10-07 13:09:31 +02:00
$arraytoscan = array_flip ( $this -> childtables );
}
2014-11-14 16:43:49 +01:00
2017-10-07 13:09:31 +02:00
// Test if child exists
2019-11-13 19:34:37 +01:00
$haschild = 0 ;
2021-02-23 22:03:23 +01:00
foreach ( $arraytoscan as $table => $elementname ) {
2017-10-07 13:09:31 +02:00
//print $id.'-'.$table.'-'.$elementname.'<br>';
// Check if third party can be deleted
$sql = " SELECT COUNT(*) as nb from " . MAIN_DB_PREFIX . $table ;
2021-03-30 17:53:25 +02:00
$sql .= " WHERE " . $this -> fk_element . " = " . (( int ) $id );
2019-11-13 19:34:37 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2019-11-13 19:34:37 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-23 22:03:23 +01:00
if ( $obj -> nb > 0 ) {
2017-10-07 13:09:31 +02:00
$langs -> load ( " errors " );
//print 'Found into table '.$table.', type '.$langs->transnoentitiesnoconv($elementname).', haschild='.$haschild;
$haschild += $obj -> nb ;
2021-02-23 22:03:23 +01:00
if ( is_numeric ( $elementname )) { // old usage
2019-11-13 19:34:37 +01:00
$this -> errors [] = $langs -> trans ( " ErrorRecordHasAtLeastOneChildOfType " , $table );
2020-05-21 15:05:19 +02:00
} else // new usage: $elementname=Translation key
2018-06-26 18:26:44 +02:00
{
2019-11-13 19:34:37 +01:00
$this -> errors [] = $langs -> trans ( " ErrorRecordHasAtLeastOneChildOfType " , $langs -> transnoentitiesnoconv ( $elementname ));
2018-06-26 18:26:44 +02:00
}
2019-11-13 19:34:37 +01:00
break ; // We found at least one, we stop here
2014-11-14 16:43:49 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> errors [] = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
return - 1 ;
}
}
2021-02-23 22:03:23 +01:00
if ( $haschild > 0 ) {
2019-11-13 19:34:37 +01:00
$this -> errors [] = " ErrorRecordHasChildren " ;
2017-10-07 13:09:31 +02:00
return $haschild ;
2021-02-23 22:03:23 +01:00
} else {
return 0 ;
}
2017-10-07 13:09:31 +02:00
}
2016-06-30 17:28:13 +02:00
2017-10-07 13:09:31 +02:00
/**
* Function to say how many lines object contains
*
* @ param int $predefined - 1 = All , 0 = Count free product / service only , 1 = Count predefined product / service only , 2 = Count predefined product , 3 = Count predefined service
* @ return int < 0 if KO , 0 if no predefined products , nb of lines with predefined products if found
*/
2019-02-25 22:27:04 +01:00
public function hasProductsOrServices ( $predefined = - 1 )
2017-10-07 13:09:31 +02:00
{
2019-11-13 19:34:37 +01:00
$nb = 0 ;
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
foreach ( $this -> lines as $key => $val ) {
2019-11-13 19:34:37 +01:00
$qualified = 0 ;
2021-02-23 22:03:23 +01:00
if ( $predefined == - 1 ) {
$qualified = 1 ;
}
if ( $predefined == 1 && $val -> fk_product > 0 ) {
$qualified = 1 ;
}
if ( $predefined == 0 && $val -> fk_product <= 0 ) {
$qualified = 1 ;
}
if ( $predefined == 2 && $val -> fk_product > 0 && $val -> product_type == 0 ) {
$qualified = 1 ;
}
if ( $predefined == 3 && $val -> fk_product > 0 && $val -> product_type == 1 ) {
$qualified = 1 ;
}
if ( $qualified ) {
$nb ++ ;
}
2017-10-07 13:09:31 +02:00
}
dol_syslog ( get_class ( $this ) . '::hasProductsOrServices we found ' . $nb . ' qualified lines of products/servcies' );
return $nb ;
2011-11-09 16:54:34 +01:00
}
2011-08-14 05:13:50 +02:00
2017-10-07 13:09:31 +02:00
/**
* Function that returns the total amount HT of discounts applied for all lines .
*
2021-06-20 05:11:46 +02:00
* @ return float | string Total amout of discount
2017-10-07 13:09:31 +02:00
*/
2019-02-25 22:27:04 +01:00
public function getTotalDiscount ()
2017-10-07 13:09:31 +02:00
{
2021-06-20 05:11:46 +02:00
if ( ! empty ( $this -> table_element_line ) ) {
$total_discount = 0.00 ;
2012-03-18 00:59:24 +01:00
2021-06-20 05:11:46 +02:00
$sql = " SELECT subprice as pu_ht, qty, remise_percent, total_ht " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element_line ;
$sql .= " WHERE " . $this -> fk_element . " = " . $this -> id ;
2017-06-07 16:44:04 +02:00
2021-06-20 05:11:46 +02:00
dol_syslog ( get_class ( $this ) . '::getTotalDiscount' , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num ) {
$obj = $this -> db -> fetch_object ( $resql );
2011-11-07 15:04:02 +01:00
2021-06-20 05:11:46 +02:00
$pu_ht = $obj -> pu_ht ;
$qty = $obj -> qty ;
$total_ht = $obj -> total_ht ;
2009-12-15 12:46:54 +01:00
2021-06-20 05:11:46 +02:00
$total_discount_line = floatval ( price2num (( $pu_ht * $qty ) - $total_ht , 'MT' ));
$total_discount += $total_discount_line ;
2011-04-11 10:34:26 +02:00
2021-06-20 05:11:46 +02:00
$i ++ ;
}
2017-10-07 13:09:31 +02:00
}
2021-06-20 05:11:46 +02:00
//print $total_discount; exit;
return price2num ( $total_discount );
2017-10-07 13:09:31 +02:00
}
2011-10-23 22:10:30 +02:00
2021-06-20 05:11:46 +02:00
return null ;
2017-10-07 13:09:31 +02:00
}
2008-03-07 11:34:16 +01:00
2012-02-07 16:47:46 +01:00
/**
2017-10-07 13:09:31 +02:00
* Return into unit = 0 , the calculated total of weight and volume of all lines * qty
* Calculate by adding weight and volume of each product line , so properties -> volume / volume_units / weight / weight_units must be loaded on line .
2012-02-07 16:47:46 +01:00
*
2017-10-07 13:09:31 +02:00
* @ return array array ( 'weight' =>... , 'volume' =>... )
2012-02-07 16:47:46 +01:00
*/
2019-02-25 22:27:04 +01:00
public function getTotalWeightVolume ()
2012-02-07 16:47:46 +01:00
{
2017-10-07 13:09:31 +02:00
$totalWeight = 0 ;
$totalVolume = 0 ;
// defined for shipment only
$totalOrdered = '' ;
// defined for shipment only
$totalToShip = '' ;
2011-04-11 10:34:26 +02:00
2021-02-23 22:03:23 +01:00
foreach ( $this -> lines as $line ) {
if ( isset ( $line -> qty_asked )) {
if ( empty ( $totalOrdered )) {
$totalOrdered = 0 ; // Avoid warning because $totalOrdered is ''
}
2019-11-13 19:34:37 +01:00
$totalOrdered += $line -> qty_asked ; // defined for shipment only
2015-07-28 14:13:16 +02:00
}
2021-02-23 22:03:23 +01:00
if ( isset ( $line -> qty_shipped )) {
if ( empty ( $totalToShip )) {
$totalToShip = 0 ; // Avoid warning because $totalToShip is ''
}
2019-11-13 19:34:37 +01:00
$totalToShip += $line -> qty_shipped ; // defined for shipment only
2021-02-23 22:03:23 +01:00
} elseif ( $line -> element == 'commandefournisseurdispatch' && isset ( $line -> qty )) {
if ( empty ( $totalToShip )) {
$totalToShip = 0 ;
}
2019-11-13 19:34:37 +01:00
$totalToShip += $line -> qty ; // defined for reception only
2015-07-28 14:13:16 +02:00
}
2012-02-13 01:43:35 +01:00
2017-10-07 13:09:31 +02:00
// Define qty, weight, volume, weight_units, volume_units
if ( $this -> element == 'shipping' ) {
// for shipments
$qty = $line -> qty_shipped ? $line -> qty_shipped : 0 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$qty = $line -> qty ? $line -> qty : 0 ;
}
2011-04-11 10:34:26 +02:00
2017-10-07 13:09:31 +02:00
$weight = $line -> weight ? $line -> weight : 0 ;
2020-09-07 10:18:17 +02:00
( $weight == 0 && ! empty ( $line -> product -> weight )) ? $weight = $line -> product -> weight : 0 ;
2017-10-07 13:09:31 +02:00
$volume = $line -> volume ? $line -> volume : 0 ;
2019-11-13 19:34:37 +01:00
( $volume == 0 && ! empty ( $line -> product -> volume )) ? $volume = $line -> product -> volume : 0 ;
2012-02-21 10:47:08 +01:00
2019-11-13 19:34:37 +01:00
$weight_units = $line -> weight_units ;
( $weight_units == 0 && ! empty ( $line -> product -> weight_units )) ? $weight_units = $line -> product -> weight_units : 0 ;
$volume_units = $line -> volume_units ;
( $volume_units == 0 && ! empty ( $line -> product -> volume_units )) ? $volume_units = $line -> product -> volume_units : 0 ;
2012-02-13 01:43:35 +01:00
2019-11-13 19:34:37 +01:00
$weightUnit = 0 ;
$volumeUnit = 0 ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $weight_units )) {
$weightUnit = $weight_units ;
}
if ( ! empty ( $volume_units )) {
$volumeUnit = $volume_units ;
}
2012-02-13 01:43:35 +01:00
2021-02-23 22:03:23 +01:00
if ( empty ( $totalWeight )) {
$totalWeight = 0 ; // Avoid warning because $totalWeight is ''
}
if ( empty ( $totalVolume )) {
$totalVolume = 0 ; // Avoid warning because $totalVolume is ''
}
2017-05-05 13:25:30 +02:00
2017-10-07 13:09:31 +02:00
//var_dump($line->volume_units);
2021-02-23 22:03:23 +01:00
if ( $weight_units < 50 ) { // < 50 means a standard unit (power of 10 of official unit), > 50 means an exotic unit (like inch)
2019-11-13 19:34:37 +01:00
$trueWeightUnit = pow ( 10 , $weightUnit );
2017-10-07 13:09:31 +02:00
$totalWeight += $weight * $qty * $trueWeightUnit ;
2020-05-21 15:05:19 +02:00
} else {
2019-10-22 18:36:36 +02:00
if ( $weight_units == 99 ) {
// conversion 1 Pound = 0.45359237 KG
$trueWeightUnit = 0.45359237 ;
$totalWeight += $weight * $qty * $trueWeightUnit ;
} elseif ( $weight_units == 98 ) {
// conversion 1 Ounce = 0.0283495 KG
$trueWeightUnit = 0.0283495 ;
$totalWeight += $weight * $qty * $trueWeightUnit ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-16 23:24:29 +01:00
$totalWeight += $weight * $qty ; // This may be wrong if we mix different units
2019-10-22 18:36:36 +02:00
}
2017-10-07 13:09:31 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $volume_units < 50 ) { // >50 means a standard unit (power of 10 of official unit), > 50 means an exotic unit (like inch)
2017-10-07 13:09:31 +02:00
//print $line->volume."x".$line->volume_units."x".($line->volume_units < 50)."x".$volumeUnit;
2019-11-16 23:24:29 +01:00
$trueVolumeUnit = pow ( 10 , $volumeUnit );
2017-10-07 13:09:31 +02:00
//print $line->volume;
$totalVolume += $volume * $qty * $trueVolumeUnit ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-16 23:24:29 +01:00
$totalVolume += $volume * $qty ; // This may be wrong if we mix different units
2017-10-07 13:09:31 +02:00
}
}
return array ( 'weight' => $totalWeight , 'volume' => $totalVolume , 'ordered' => $totalOrdered , 'toship' => $totalToShip );
}
2012-02-13 01:43:35 +01:00
2012-02-07 16:47:46 +01:00
/**
2017-10-07 13:09:31 +02:00
* Set extra parameters
2012-02-07 16:47:46 +01:00
*
2017-10-07 13:09:31 +02:00
* @ return int < 0 if KO , > 0 if OK
2012-02-07 16:47:46 +01:00
*/
2019-02-25 22:27:04 +01:00
public function setExtraParameters ()
2012-02-07 16:47:46 +01:00
{
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2012-07-05 14:31:13 +02:00
2019-11-13 19:34:37 +01:00
$extraparams = ( ! empty ( $this -> extraparams ) ? json_encode ( $this -> extraparams ) : null );
2012-07-05 14:31:13 +02:00
2017-10-07 13:09:31 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
2019-11-13 19:34:37 +01:00
$sql .= " SET extraparams = " . ( ! empty ( $extraparams ) ? " ' " . $this -> db -> escape ( $extraparams ) . " ' " : " null " );
$sql .= " WHERE rowid = " . $this -> id ;
2012-07-05 14:31:13 +02:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::setExtraParameters " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( ! $resql ) {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return 1 ;
}
}
// --------------------
// TODO: All functions here must be redesigned and moved as they are not business functions but output functions
// --------------------
/* This is to show add lines */
/**
* Show add free and predefined products / services form
*
* @ param int $dateSelector 1 = Show also date range input fields
* @ param Societe $seller Object thirdparty who sell
* @ param Societe $buyer Object thirdparty who buy
2019-05-23 12:35:53 +02:00
* @ param string $defaulttpldir Directory where to find the template
2017-10-07 13:09:31 +02:00
* @ return void
*/
2019-05-22 20:28:24 +02:00
public function formAddObjectLine ( $dateSelector , $seller , $buyer , $defaulttpldir = '/core/tpl' )
2010-08-06 18:10:03 +02:00
{
2019-11-16 23:24:29 +01:00
global $conf , $user , $langs , $object , $hookmanager , $extrafields ;
2019-11-12 01:48:12 +01:00
global $form ;
2011-04-11 01:25:49 +02:00
2018-04-24 12:57:31 +02:00
// Line extrafield
2021-02-23 22:03:23 +01:00
if ( ! is_object ( $extrafields )) {
2019-10-06 14:41:52 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' ;
$extrafields = new ExtraFields ( $this -> db );
}
$extrafields -> fetch_name_optionals_label ( $this -> table_element_line );
2013-06-10 16:05:41 +02:00
2012-04-29 11:00:28 +02:00
// Output template part (modules that overwrite templates must declare this into descriptor)
2017-10-07 13:09:31 +02:00
// Use global variables + $dateSelector + $seller and $buyer
2020-03-19 10:15:34 +01:00
// Note: This is deprecated. If you need to overwrite the tpl file, use instead the hook 'formAddObjectLine'.
2019-11-13 19:34:37 +01:00
$dirtpls = array_merge ( $conf -> modules_parts [ 'tpl' ], array ( $defaulttpldir ));
2021-02-23 22:03:23 +01:00
foreach ( $dirtpls as $module => $reldir ) {
if ( ! empty ( $module )) {
2019-05-24 12:10:58 +02:00
$tpl = dol_buildpath ( $reldir . '/objectline_create.tpl.php' );
2020-05-21 15:05:19 +02:00
} else {
2019-05-24 12:10:58 +02:00
$tpl = DOL_DOCUMENT_ROOT . $reldir . '/objectline_create.tpl.php' ;
}
2012-08-23 13:45:51 +02:00
if ( empty ( $conf -> file -> strict_mode )) {
2019-11-13 19:34:37 +01:00
$res = @ include $tpl ;
2012-08-22 17:42:40 +02:00
} else {
2019-11-13 19:34:37 +01:00
$res = include $tpl ; // for debug
2012-08-22 17:42:40 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $res ) {
break ;
}
2012-02-07 16:47:46 +01:00
}
}
2012-02-13 01:43:35 +01:00
2014-09-22 20:16:58 +02:00
2017-06-07 16:44:04 +02:00
2017-10-07 13:09:31 +02:00
/* This is to show array of line of details */
2012-03-18 00:59:24 +01:00
2011-11-07 15:04:02 +01:00
2012-04-06 11:38:13 +02:00
/**
* Return HTML table for object lines
* TODO Move this into an output class file ( htmlline . class . php )
* If lines are into a template , title must also be into a template
2018-03-04 11:22:43 +01:00
* But for the moment we don 't know if it' s possible as we keep a method available on overloaded objects .
2012-04-06 11:38:13 +02:00
*
* @ param string $action Action code
2015-05-23 23:17:39 +02:00
* @ param string $seller Object of seller third party
* @ param string $buyer Object of buyer third party
2015-05-25 18:43:46 +02:00
* @ param int $selected Object line selected
2012-04-06 11:38:13 +02:00
* @ param int $dateSelector 1 = Show also date range input fields
2019-06-03 19:29:49 +02:00
* @ param string $defaulttpldir Directory where to find the template
2012-04-06 11:38:13 +02:00
* @ return void
*/
2019-06-03 19:29:49 +02:00
public function printObjectLines ( $action , $seller , $buyer , $selected = 0 , $dateSelector = 0 , $defaulttpldir = '/core/tpl' )
2012-04-06 11:38:13 +02:00
{
2020-03-19 09:53:20 +01:00
global $conf , $hookmanager , $langs , $user , $form , $extrafields , $object ;
2019-06-03 19:29:49 +02:00
// TODO We should not use global var for this
2016-10-24 10:18:24 +02:00
global $inputalsopricewithtax , $usemargins , $disableedit , $disablemove , $disableremove , $outputalsopricetotalwithtax ;
2015-02-27 18:05:12 +01:00
2015-03-04 02:28:16 +01:00
// Define usemargins
2019-11-13 19:34:37 +01:00
$usemargins = 0 ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> margin -> enabled ) && ! empty ( $this -> element ) && in_array ( $this -> element , array ( 'facture' , 'facturerec' , 'propal' , 'commande' ))) {
$usemargins = 1 ;
}
2012-05-05 20:22:46 +02:00
2012-04-06 11:38:13 +02:00
$num = count ( $this -> lines );
2011-11-07 15:04:02 +01:00
2018-08-29 17:03:18 +02:00
// Line extrafield
2021-02-23 22:03:23 +01:00
if ( ! is_object ( $extrafields )) {
2019-10-06 14:41:52 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' ;
$extrafields = new ExtraFields ( $this -> db );
}
$extrafields -> fetch_name_optionals_label ( $this -> table_element_line );
2017-06-07 16:44:04 +02:00
2019-10-06 14:41:52 +02:00
$parameters = array ( 'num' => $num , 'dateSelector' => $dateSelector , 'seller' => $seller , 'buyer' => $buyer , 'selected' => $selected , 'table_element_line' => $this -> table_element_line );
2017-01-07 20:42:35 +01:00
$reshook = $hookmanager -> executeHooks ( 'printObjectLineTitle' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2021-02-23 22:03:23 +01:00
if ( empty ( $reshook )) {
2019-06-03 19:29:49 +02:00
// Output template part (modules that overwrite templates must declare this into descriptor)
// Use global variables + $dateSelector + $seller and $buyer
2020-03-19 09:53:20 +01:00
// Note: This is deprecated. If you need to overwrite the tpl file, use instead the hook.
2019-11-13 19:34:37 +01:00
$dirtpls = array_merge ( $conf -> modules_parts [ 'tpl' ], array ( $defaulttpldir ));
2021-02-23 22:03:23 +01:00
foreach ( $dirtpls as $module => $reldir ) {
if ( ! empty ( $module )) {
2019-06-03 19:29:49 +02:00
$tpl = dol_buildpath ( $reldir . '/objectline_title.tpl.php' );
2020-05-21 15:05:19 +02:00
} else {
2019-06-03 19:29:49 +02:00
$tpl = DOL_DOCUMENT_ROOT . $reldir . '/objectline_title.tpl.php' ;
}
if ( empty ( $conf -> file -> strict_mode )) {
2019-11-16 23:24:29 +01:00
$res = @ include $tpl ;
2019-06-03 19:29:49 +02:00
} else {
2019-11-16 23:24:29 +01:00
$res = include $tpl ; // for debug
2019-06-03 19:29:49 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $res ) {
break ;
}
2014-09-22 20:16:58 +02:00
}
2017-01-07 20:42:35 +01:00
}
2010-08-18 09:28:12 +02:00
2019-11-16 23:24:29 +01:00
$i = 0 ;
2010-10-03 20:49:15 +02:00
2019-12-09 00:06:51 +01:00
print " <!-- begin printObjectLines() --><tbody> \n " ;
2021-02-23 22:03:23 +01:00
foreach ( $this -> lines as $line ) {
2013-06-10 16:05:41 +02:00
//Line extrafield
2018-02-21 14:48:25 +01:00
$line -> fetch_optionals ();
2011-08-23 00:04:21 +02:00
2015-10-26 14:00:43 +01:00
//if (is_object($hookmanager) && (($line->product_type == 9 && ! empty($line->special_code)) || ! empty($line->fk_parent_line)))
2021-02-23 22:03:23 +01:00
if ( is_object ( $hookmanager )) { // Old code is commented on preceding line.
if ( empty ( $line -> fk_parent_line )) {
2019-11-12 01:48:12 +01:00
$parameters = array ( 'line' => $line , 'num' => $num , 'i' => $i , 'dateSelector' => $dateSelector , 'seller' => $seller , 'buyer' => $buyer , 'selected' => $selected , 'table_element_line' => $line -> table_element );
2019-11-16 23:24:29 +01:00
$reshook = $hookmanager -> executeHooks ( 'printObjectLine' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 01:48:12 +01:00
$parameters = array ( 'line' => $line , 'num' => $num , 'i' => $i , 'dateSelector' => $dateSelector , 'seller' => $seller , 'buyer' => $buyer , 'selected' => $selected , 'table_element_line' => $line -> table_element , 'fk_parent_line' => $line -> fk_parent_line );
2019-11-16 23:24:29 +01:00
$reshook = $hookmanager -> executeHooks ( 'printObjectSubLine' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2012-04-06 11:38:13 +02:00
}
}
2021-02-23 22:03:23 +01:00
if ( empty ( $reshook )) {
2019-11-12 01:48:12 +01:00
$this -> printObjectLine ( $action , $line , '' , $num , $i , $dateSelector , $seller , $buyer , $selected , $extrafields , $defaulttpldir );
2012-04-06 11:38:13 +02:00
}
2010-09-02 23:08:29 +02:00
2012-04-06 11:38:13 +02:00
$i ++ ;
}
2019-04-09 19:23:34 +02:00
print " </tbody><!-- end printObjectLines() --> \n " ;
2012-04-06 11:38:13 +02:00
}
2010-09-02 23:08:29 +02:00
2012-04-06 11:38:13 +02:00
/**
* Return HTML content of a detail line
* TODO Move this into an output class file ( htmlline . class . php )
*
2019-11-12 01:48:12 +01:00
* @ param string $action GET / POST action
* @ param CommonObjectLine $line Selected object line to output
* @ param string $var Is it a an odd line ( true )
* @ param int $num Number of line ( 0 )
* @ param int $i I
* @ param int $dateSelector 1 = Show also date range input fields
* @ param string $seller Object of seller third party
* @ param string $buyer Object of buyer third party
* @ param int $selected Object line selected
* @ param Extrafields $extrafields Object of extrafields
2020-03-19 10:15:34 +01:00
* @ param string $defaulttpldir Directory where to find the template ( deprecated )
2012-04-06 11:38:13 +02:00
* @ return void
2010-08-10 00:39:20 +02:00
*/
2019-10-06 14:41:52 +02:00
public function printObjectLine ( $action , $line , $var , $num , $i , $dateSelector , $seller , $buyer , $selected = 0 , $extrafields = null , $defaulttpldir = '/core/tpl' )
2010-08-10 00:39:20 +02:00
{
2019-11-16 23:24:29 +01:00
global $conf , $langs , $user , $object , $hookmanager ;
2019-11-12 01:48:12 +01:00
global $form ;
2019-11-16 23:24:29 +01:00
global $object_rights , $disableedit , $disablemove , $disableremove ; // TODO We should not use global var for this !
2010-09-02 23:08:29 +02:00
2015-03-23 01:39:12 +01:00
$object_rights = $this -> getRights ();
2010-09-02 23:08:29 +02:00
2019-11-16 23:24:29 +01:00
$element = $this -> element ;
2017-06-07 16:44:04 +02:00
2021-03-01 20:37:16 +01:00
$text = '' ;
$description = '' ;
2010-09-02 23:08:29 +02:00
2020-03-19 10:15:34 +01:00
// Line in view mode
2021-02-23 22:03:23 +01:00
if ( $action != 'editline' || $selected != $line -> id ) {
2013-03-22 17:10:17 +01:00
// Product
2021-02-23 22:03:23 +01:00
if ( $line -> fk_product > 0 ) {
2013-03-30 14:27:13 +01:00
$product_static = new Product ( $this -> db );
2015-03-11 14:55:45 +01:00
$product_static -> fetch ( $line -> fk_product );
2010-12-14 10:03:50 +01:00
2017-10-07 13:09:31 +02:00
$product_static -> ref = $line -> ref ; //can change ref in hook
$product_static -> label = $line -> label ; //can change label in hook
2020-01-20 12:25:25 +01:00
2019-11-13 19:34:37 +01:00
$text = $product_static -> getNomUrl ( 1 );
2012-12-10 19:06:59 +01:00
2014-02-17 18:27:45 +01:00
// Define output language and label
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_MULTILANGS )) {
if ( property_exists ( $this , 'socid' ) && ! is_object ( $this -> thirdparty )) {
2019-01-27 11:55:16 +01:00
dol_print_error ( '' , 'Error: Method printObjectLine was called on an object and object->fetch_thirdparty was not done before' );
2014-02-17 18:27:45 +01:00
return ;
2014-02-17 18:22:15 +01:00
}
2011-06-22 12:58:22 +02:00
2012-08-23 13:45:51 +02:00
$prod = new Product ( $this -> db );
2013-03-22 17:10:17 +01:00
$prod -> fetch ( $line -> fk_product );
2012-04-07 02:24:32 +02:00
2012-04-06 11:38:13 +02:00
$outputlangs = $langs ;
2019-11-13 19:34:37 +01:00
$newlang = '' ;
2021-02-23 22:03:23 +01:00
if ( empty ( $newlang ) && GETPOST ( 'lang_id' , 'aZ09' )) {
$newlang = GETPOST ( 'lang_id' , 'aZ09' );
}
if ( ! empty ( $conf -> global -> PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE ) && empty ( $newlang ) && is_object ( $this -> thirdparty )) {
$newlang = $this -> thirdparty -> default_lang ; // To use language of customer
}
if ( ! empty ( $newlang )) {
2019-01-27 11:55:16 +01:00
$outputlangs = new Translate ( " " , $conf );
2012-04-06 11:38:13 +02:00
$outputlangs -> setDefaultLang ( $newlang );
}
2012-02-13 01:43:35 +01:00
2019-11-13 19:34:37 +01:00
$label = ( ! empty ( $prod -> multilangs [ $outputlangs -> defaultlang ][ " label " ])) ? $prod -> multilangs [ $outputlangs -> defaultlang ][ " label " ] : $line -> product_label ;
2020-05-21 15:05:19 +02:00
} else {
2012-04-06 11:38:13 +02:00
$label = $line -> product_label ;
}
2014-01-04 12:29:20 +01:00
2019-11-13 19:34:37 +01:00
$text .= ' - ' . ( ! empty ( $line -> label ) ? $line -> label : $label );
$description .= ( ! empty ( $conf -> global -> PRODUIT_DESC_IN_FORM ) ? '' : dol_htmlentitiesbr ( $line -> description )); // Description is what to show on popup. We shown nothing if already into desc.
2012-04-06 11:38:13 +02:00
}
2012-02-01 20:44:06 +01:00
2019-11-13 19:34:37 +01:00
$line -> pu_ttc = price2num ( $line -> subprice * ( 1 + ( $line -> tva_tx / 100 )), 'MU' );
2011-09-13 14:12:48 +02:00
2012-08-25 15:21:34 +02:00
// Output template part (modules that overwrite templates must declare this into descriptor)
// Use global variables + $dateSelector + $seller and $buyer
2020-03-19 10:15:34 +01:00
// Note: This is deprecated. If you need to overwrite the tpl file, use instead the hook printObjectLine and printObjectSubLine.
2019-11-13 19:34:37 +01:00
$dirtpls = array_merge ( $conf -> modules_parts [ 'tpl' ], array ( $defaulttpldir ));
2021-02-23 22:03:23 +01:00
foreach ( $dirtpls as $module => $reldir ) {
if ( ! empty ( $module )) {
2019-05-24 12:10:58 +02:00
$tpl = dol_buildpath ( $reldir . '/objectline_view.tpl.php' );
2020-05-21 15:05:19 +02:00
} else {
2019-05-24 12:10:58 +02:00
$tpl = DOL_DOCUMENT_ROOT . $reldir . '/objectline_view.tpl.php' ;
}
2012-08-25 15:21:34 +02:00
if ( empty ( $conf -> file -> strict_mode )) {
2019-11-13 19:34:37 +01:00
$res = @ include $tpl ;
2012-08-25 15:21:34 +02:00
} else {
2019-11-13 19:34:37 +01:00
$res = include $tpl ; // for debug
2012-08-25 15:21:34 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $res ) {
break ;
}
2012-04-06 11:38:13 +02:00
}
}
2011-09-13 14:12:48 +02:00
2019-06-03 19:29:49 +02:00
// Line in update mode
2021-02-23 22:03:23 +01:00
if ( $this -> statut == 0 && $action == 'editline' && $selected == $line -> id ) {
2019-11-13 19:34:37 +01:00
$label = ( ! empty ( $line -> label ) ? $line -> label : (( $line -> fk_product > 0 ) ? $line -> product_label : '' ));
2014-09-23 19:37:59 +02:00
2019-11-13 19:34:37 +01:00
$line -> pu_ttc = price2num ( $line -> subprice * ( 1 + ( $line -> tva_tx / 100 )), 'MU' );
2013-04-07 17:39:08 +02:00
2012-08-25 15:21:34 +02:00
// Output template part (modules that overwrite templates must declare this into descriptor)
// Use global variables + $dateSelector + $seller and $buyer
2020-03-19 10:15:34 +01:00
// Note: This is deprecated. If you need to overwrite the tpl file, use instead the hook printObjectLine and printObjectSubLine.
2019-11-13 19:34:37 +01:00
$dirtpls = array_merge ( $conf -> modules_parts [ 'tpl' ], array ( $defaulttpldir ));
2021-02-23 22:03:23 +01:00
foreach ( $dirtpls as $module => $reldir ) {
if ( ! empty ( $module )) {
2019-05-24 12:10:58 +02:00
$tpl = dol_buildpath ( $reldir . '/objectline_edit.tpl.php' );
2020-05-21 15:05:19 +02:00
} else {
2019-05-24 12:10:58 +02:00
$tpl = DOL_DOCUMENT_ROOT . $reldir . '/objectline_edit.tpl.php' ;
}
2012-08-23 13:45:51 +02:00
if ( empty ( $conf -> file -> strict_mode )) {
2019-11-13 19:34:37 +01:00
$res = @ include $tpl ;
2012-08-23 13:45:51 +02:00
} else {
2019-11-13 19:34:37 +01:00
$res = include $tpl ; // for debug
2012-08-25 15:21:34 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $res ) {
break ;
}
2012-04-06 11:38:13 +02:00
}
}
}
2016-06-30 17:28:13 +02:00
2017-10-07 13:09:31 +02:00
/* This is to show array of line of details of source object */
2016-06-30 17:28:13 +02:00
2017-10-07 13:09:31 +02:00
/**
* Return HTML table table of source object lines
* TODO Move this and previous function into output html class file ( htmlline . class . php ) .
* If lines are into a template , title must also be into a template
2017-10-17 10:44:18 +02:00
* But for the moment we don 't know if it' s possible , so we keep the method available on overloaded objects .
2017-10-07 13:09:31 +02:00
*
2017-10-17 10:44:18 +02:00
* @ param string $restrictlist '' = All lines , 'services' = Restrict to services only
2019-07-27 18:13:44 +02:00
* @ param array $selectedLines Array of lines id for selected lines
2017-10-07 13:09:31 +02:00
* @ return void
*/
2019-07-27 18:13:44 +02:00
public function printOriginLinesList ( $restrictlist = '' , $selectedLines = array ())
2017-10-07 13:09:31 +02:00
{
2019-07-08 23:16:32 +02:00
global $langs , $hookmanager , $conf , $form ;
2017-06-07 16:44:04 +02:00
2017-10-07 13:09:31 +02:00
print '<tr class="liste_titre">' ;
print '<td>' . $langs -> trans ( 'Ref' ) . '</td>' ;
print '<td>' . $langs -> trans ( 'Description' ) . '</td>' ;
2019-01-20 23:36:39 +01:00
print '<td class="right">' . $langs -> trans ( 'VATRate' ) . '</td>' ;
print '<td class="right">' . $langs -> trans ( 'PriceUHT' ) . '</td>' ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> multicurrency -> enabled )) {
print '<td class="right">' . $langs -> trans ( 'PriceUHTCurrency' ) . '</td>' ;
}
2019-01-20 23:36:39 +01:00
print '<td class="right">' . $langs -> trans ( 'Qty' ) . '</td>' ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> PRODUCT_USE_UNITS )) {
2019-01-20 23:36:39 +01:00
print '<td class="left">' . $langs -> trans ( 'Unit' ) . '</td>' ;
2017-06-17 10:16:52 +02:00
}
2019-07-08 23:16:32 +02:00
print '<td class="right">' . $langs -> trans ( 'ReductionShort' ) . '</td>' ;
2020-09-07 10:18:17 +02:00
print '<td class="center">' . $form -> showCheckAddButtons ( 'checkforselect' , 1 ) . '</td>' ;
print '</tr>' ;
2019-11-16 23:24:29 +01:00
$i = 0 ;
2016-06-30 17:28:13 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $this -> lines )) {
foreach ( $this -> lines as $line ) {
if ( is_object ( $hookmanager ) && (( $line -> product_type == 9 && ! empty ( $line -> special_code )) || ! empty ( $line -> fk_parent_line ))) {
if ( empty ( $line -> fk_parent_line )) {
2019-11-16 23:24:29 +01:00
$parameters = array ( 'line' => $line , 'i' => $i );
$action = '' ;
$hookmanager -> executeHooks ( 'printOriginObjectLine' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2018-04-18 10:13:20 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 01:48:12 +01:00
$this -> printOriginLine ( $line , '' , $restrictlist , '/core/tpl' , $selectedLines );
2017-10-07 13:09:31 +02:00
}
2013-04-07 17:39:08 +02:00
2018-04-18 10:13:20 +02:00
$i ++ ;
}
2017-10-07 13:09:31 +02:00
}
}
2013-04-07 17:39:08 +02:00
2017-10-07 13:09:31 +02:00
/**
* Return HTML with a line of table array of source object lines
* TODO Move this and previous function into output html class file ( htmlline . class . php ) .
* If lines are into a template , title must also be into a template
* But for the moment we don 't know if it' s possible as we keep a method available on overloaded objects .
*
2017-10-17 10:44:18 +02:00
* @ param CommonObjectLine $line Line
* @ param string $var Var
* @ param string $restrictlist '' = All lines , 'services' = Restrict to services only ( strike line if not )
2019-06-04 19:03:27 +02:00
* @ param string $defaulttpldir Directory where to find the template
2019-07-27 18:13:44 +02:00
* @ param array $selectedLines Array of lines id for selected lines
2017-10-07 13:09:31 +02:00
* @ return void
*/
2019-07-27 18:13:44 +02:00
public function printOriginLine ( $line , $var , $restrictlist = '' , $defaulttpldir = '/core/tpl' , $selectedLines = array ())
2017-10-07 13:09:31 +02:00
{
global $langs , $conf ;
2012-03-18 00:59:24 +01:00
2017-10-07 13:09:31 +02:00
//var_dump($line);
2021-02-23 22:03:23 +01:00
if ( ! empty ( $line -> date_start )) {
2019-11-13 19:34:37 +01:00
$date_start = $line -> date_start ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$date_start = $line -> date_debut_prevue ;
2021-02-23 22:03:23 +01:00
if ( $line -> date_debut_reel ) {
$date_start = $line -> date_debut_reel ;
}
2013-04-15 15:55:10 +02:00
}
2021-02-23 22:03:23 +01:00
if ( ! empty ( $line -> date_end )) {
2019-11-13 19:34:37 +01:00
$date_end = $line -> date_end ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$date_end = $line -> date_fin_prevue ;
2021-02-23 22:03:23 +01:00
if ( $line -> date_fin_reel ) {
$date_end = $line -> date_fin_reel ;
}
2013-04-15 15:55:10 +02:00
}
2012-03-18 00:59:24 +01:00
2020-09-07 10:18:17 +02:00
$this -> tpl [ 'id' ] = $line -> id ;
2019-07-08 23:16:32 +02:00
2017-10-07 13:09:31 +02:00
$this -> tpl [ 'label' ] = '' ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $line -> fk_parent_line )) {
$this -> tpl [ 'label' ] .= img_picto ( '' , 'rightarrow' );
}
2011-06-22 12:58:22 +02:00
2021-02-23 22:03:23 +01:00
if (( $line -> info_bits & 2 ) == 2 ) { // TODO Not sure this is used for source object
2019-11-13 19:34:37 +01:00
$discount = new DiscountAbsolute ( $this -> db );
2017-10-07 13:09:31 +02:00
$discount -> fk_soc = $this -> socid ;
2019-11-13 19:34:37 +01:00
$this -> tpl [ 'label' ] .= $discount -> getNomUrl ( 0 , 'discount' );
2021-02-23 22:03:23 +01:00
} elseif ( ! empty ( $line -> fk_product )) {
2017-10-07 13:09:31 +02:00
$productstatic = new Product ( $this -> db );
$productstatic -> id = $line -> fk_product ;
$productstatic -> ref = $line -> ref ;
$productstatic -> type = $line -> fk_product_type ;
2019-05-13 22:02:33 +02:00
if ( empty ( $productstatic -> ref )) {
2018-10-23 10:27:46 +02:00
$line -> fetch_product ();
$productstatic = $line -> product ;
}
2018-12-23 01:28:36 +01:00
2019-11-13 19:34:37 +01:00
$this -> tpl [ 'label' ] .= $productstatic -> getNomUrl ( 1 );
$this -> tpl [ 'label' ] .= ' - ' . ( ! empty ( $line -> label ) ? $line -> label : $line -> product_label );
2017-10-07 13:09:31 +02:00
// Dates
2021-02-23 22:03:23 +01:00
if ( $line -> product_type == 1 && ( $date_start || $date_end )) {
2019-11-13 19:34:37 +01:00
$this -> tpl [ 'label' ] .= get_date_range ( $date_start , $date_end );
2017-10-07 13:09:31 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> tpl [ 'label' ] .= ( $line -> product_type == - 1 ? ' ' : ( $line -> product_type == 1 ? img_object ( $langs -> trans ( '' ), 'service' ) : img_object ( $langs -> trans ( '' ), 'product' )));
2017-10-07 13:09:31 +02:00
if ( ! empty ( $line -> desc )) {
2019-11-13 19:34:37 +01:00
$this -> tpl [ 'label' ] .= $line -> desc ;
} else {
$this -> tpl [ 'label' ] .= ( $line -> label ? ' ' . $line -> label : '' );
2017-10-07 13:09:31 +02:00
}
2018-12-23 01:28:36 +01:00
2017-10-07 13:09:31 +02:00
// Dates
2021-02-23 22:03:23 +01:00
if ( $line -> product_type == 1 && ( $date_start || $date_end )) {
2019-11-13 19:34:37 +01:00
$this -> tpl [ 'label' ] .= get_date_range ( $date_start , $date_end );
2015-02-10 11:46:09 +01:00
}
2015-03-06 01:53:02 +01:00
}
2021-02-23 22:03:23 +01:00
if ( ! empty ( $line -> desc )) {
if ( $line -> desc == '(CREDIT_NOTE)' ) { // TODO Not sure this is used for source object
2019-11-13 19:34:37 +01:00
$discount = new DiscountAbsolute ( $this -> db );
2017-10-07 13:09:31 +02:00
$discount -> fetch ( $line -> fk_remise_except );
2019-01-27 11:55:16 +01:00
$this -> tpl [ 'description' ] = $langs -> transnoentities ( " DiscountFromCreditNote " , $discount -> getNomUrl ( 0 ));
2021-02-23 22:03:23 +01:00
} elseif ( $line -> desc == '(DEPOSIT)' ) { // TODO Not sure this is used for source object
2019-11-13 19:34:37 +01:00
$discount = new DiscountAbsolute ( $this -> db );
2017-10-07 13:09:31 +02:00
$discount -> fetch ( $line -> fk_remise_except );
2019-01-27 11:55:16 +01:00
$this -> tpl [ 'description' ] = $langs -> transnoentities ( " DiscountFromDeposit " , $discount -> getNomUrl ( 0 ));
2021-02-23 22:03:23 +01:00
} elseif ( $line -> desc == '(EXCESS RECEIVED)' ) {
2019-11-13 19:34:37 +01:00
$discount = new DiscountAbsolute ( $this -> db );
2017-10-07 13:09:31 +02:00
$discount -> fetch ( $line -> fk_remise_except );
2019-01-27 11:55:16 +01:00
$this -> tpl [ 'description' ] = $langs -> transnoentities ( " DiscountFromExcessReceived " , $discount -> getNomUrl ( 0 ));
2021-02-23 22:03:23 +01:00
} elseif ( $line -> desc == '(EXCESS PAID)' ) {
2019-11-13 19:34:37 +01:00
$discount = new DiscountAbsolute ( $this -> db );
2018-02-19 15:52:07 +01:00
$discount -> fetch ( $line -> fk_remise_except );
2019-01-27 11:55:16 +01:00
$this -> tpl [ 'description' ] = $langs -> transnoentities ( " DiscountFromExcessPaid " , $discount -> getNomUrl ( 0 ));
2020-05-21 15:05:19 +02:00
} else {
2019-01-27 11:55:16 +01:00
$this -> tpl [ 'description' ] = dol_trunc ( $line -> desc , 60 );
2017-04-07 14:15:51 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> tpl [ 'description' ] = ' ' ;
2015-02-23 10:36:44 +01:00
}
2012-04-08 13:05:58 +02:00
2020-09-07 10:18:17 +02:00
// VAT Rate
$this -> tpl [ 'vat_rate' ] = vatrate ( $line -> tva_tx , true );
$this -> tpl [ 'vat_rate' ] .= (( $line -> info_bits & 1 ) == 1 ) ? '*' : '' ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $line -> vat_src_code ) && ! preg_match ( '/\(/' , $this -> tpl [ 'vat_rate' ])) {
$this -> tpl [ 'vat_rate' ] .= ' (' . $line -> vat_src_code . ')' ;
}
2011-04-11 10:34:26 +02:00
2017-10-07 13:09:31 +02:00
$this -> tpl [ 'price' ] = price ( $line -> subprice );
2016-01-23 00:38:17 +01:00
$this -> tpl [ 'multicurrency_price' ] = price ( $line -> multicurrency_subprice );
2017-10-07 13:09:31 +02:00
$this -> tpl [ 'qty' ] = (( $line -> info_bits & 2 ) != 2 ) ? $line -> qty : ' ' ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> PRODUCT_USE_UNITS )) {
$this -> tpl [ 'unit' ] = $langs -> transnoentities ( $line -> getLabelOfUnit ( 'long' ));
}
2017-10-07 13:09:31 +02:00
$this -> tpl [ 'remise_percent' ] = (( $line -> info_bits & 2 ) != 2 ) ? vatrate ( $line -> remise_percent , true ) : ' ' ;
2011-04-11 01:25:49 +02:00
2017-10-17 10:44:18 +02:00
// Is the line strike or not
2019-11-13 19:34:37 +01:00
$this -> tpl [ 'strike' ] = 0 ;
2021-02-23 22:03:23 +01:00
if ( $restrictlist == 'services' && $line -> product_type != Product :: TYPE_SERVICE ) {
$this -> tpl [ 'strike' ] = 1 ;
}
2013-06-10 16:05:41 +02:00
2012-04-29 11:00:28 +02:00
// Output template part (modules that overwrite templates must declare this into descriptor)
2017-10-07 13:09:31 +02:00
// Use global variables + $dateSelector + $seller and $buyer
2019-11-13 19:34:37 +01:00
$dirtpls = array_merge ( $conf -> modules_parts [ 'tpl' ], array ( $defaulttpldir ));
2021-02-23 22:03:23 +01:00
foreach ( $dirtpls as $module => $reldir ) {
if ( ! empty ( $module )) {
2019-05-24 12:10:58 +02:00
$tpl = dol_buildpath ( $reldir . '/originproductline.tpl.php' );
2020-05-21 15:05:19 +02:00
} else {
2019-05-24 12:10:58 +02:00
$tpl = DOL_DOCUMENT_ROOT . $reldir . '/originproductline.tpl.php' ;
}
2012-08-23 13:45:51 +02:00
if ( empty ( $conf -> file -> strict_mode )) {
2019-11-13 19:34:37 +01:00
$res = @ include $tpl ;
2012-08-22 17:42:40 +02:00
} else {
2019-11-13 19:34:37 +01:00
$res = include $tpl ; // for debug
2012-08-22 17:42:40 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $res ) {
break ;
}
2012-04-28 18:24:52 +02:00
}
2017-10-07 13:09:31 +02:00
}
2012-07-18 15:26:33 +02:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2014-03-15 14:54:23 +01:00
/**
2014-04-23 19:04:10 +02:00
* Add resources to the current object : add entry into llx_element_resources
2015-05-25 18:43:46 +02:00
* Need $this -> element & $this -> id
2014-03-15 14:54:23 +01:00
*
2014-05-14 12:19:45 +02:00
* @ param int $resource_id Resource id
2015-08-14 17:35:05 +02:00
* @ param string $resource_type 'resource'
2014-05-14 12:19:45 +02:00
* @ param int $busy Busy or not
* @ param int $mandatory Mandatory or not
* @ return int <= 0 if KO , > 0 if OK
2014-03-15 14:54:23 +01:00
*/
2019-02-25 22:27:04 +01:00
public function add_element_resource ( $resource_id , $resource_type , $busy = 0 , $mandatory = 0 )
2014-03-15 14:54:23 +01:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2014-03-15 14:54:23 +01:00
$this -> db -> begin ();
2014-04-23 19:04:10 +02:00
2014-03-15 14:54:23 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " element_resources ( " ;
2019-11-13 19:34:37 +01:00
$sql .= " resource_id " ;
$sql .= " , resource_type " ;
$sql .= " , element_id " ;
$sql .= " , element_type " ;
$sql .= " , busy " ;
$sql .= " , mandatory " ;
$sql .= " ) VALUES ( " ;
$sql .= $resource_id ;
$sql .= " , ' " . $this -> db -> escape ( $resource_type ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> id ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> element ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $busy ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $mandatory ) . " ' " ;
$sql .= " ) " ;
2014-04-23 19:04:10 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::add_element_resource " , LOG_DEBUG );
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
2014-03-15 14:54:23 +01:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2014-03-15 14:54:23 +01:00
$this -> db -> rollback ();
return 0 ;
}
}
2014-05-14 12:19:45 +02:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2014-05-11 12:26:57 +02:00
/**
* Delete a link to resource line
*
* @ param int $rowid Id of resource line to delete
* @ param int $element element name ( for trigger ) TODO : use $this -> element into commonobject class
* @ param int $notrigger Disable all triggers
* @ return int > 0 if OK , < 0 if KO
*/
2019-02-25 22:27:04 +01:00
public function delete_resource ( $rowid , $element , $notrigger = 0 )
2014-05-11 12:26:57 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2017-10-07 13:09:31 +02:00
global $user ;
2014-05-14 12:19:45 +02:00
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2010-04-24 15:39:16 +02:00
2017-10-07 13:09:31 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " element_resources " ;
2021-04-24 20:18:11 +02:00
$sql .= " WHERE rowid = " . (( int ) $rowid );
2011-02-20 21:53:26 +01:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_resource " , LOG_DEBUG );
2010-04-24 15:39:16 +02:00
2019-11-13 19:34:37 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( ! $resql ) {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 22:03:23 +01:00
if ( ! $notrigger ) {
2019-11-13 19:34:37 +01:00
$result = $this -> call_trigger ( strtoupper ( $element ) . '_DELETE_RESOURCE' , $user );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2021-03-01 20:37:16 +01:00
$this -> db -> rollback ();
return - 1 ;
2021-02-23 22:03:23 +01:00
}
2017-10-07 13:09:31 +02:00
}
$this -> db -> commit ();
return 1 ;
}
2014-05-11 12:26:57 +02:00
}
2011-03-02 11:38:32 +01:00
2010-08-18 09:28:12 +02:00
2012-04-06 11:38:13 +02:00
/**
2013-09-10 13:45:39 +02:00
* Overwrite magic function to solve problem of cloning object that are kept as references
2012-04-06 11:38:13 +02:00
*
2013-09-10 13:45:39 +02:00
* @ return void
2012-04-06 11:38:13 +02:00
*/
2019-02-25 22:27:04 +01:00
public function __clone ()
2012-04-06 11:38:13 +02:00
{
2017-10-07 13:09:31 +02:00
// Force a copy of this->lines, otherwise it will point to same object.
2021-02-23 22:03:23 +01:00
if ( isset ( $this -> lines ) && is_array ( $this -> lines )) {
2019-11-13 19:34:37 +01:00
$nboflines = count ( $this -> lines );
2021-02-23 22:03:23 +01:00
for ( $i = 0 ; $i < $nboflines ; $i ++ ) {
2017-10-07 13:09:31 +02:00
$this -> lines [ $i ] = clone $this -> lines [ $i ];
}
}
}
2015-02-27 18:05:12 +01:00
2014-09-23 19:37:59 +02:00
/**
* Common function for all objects extending CommonObject for generating documents
*
2015-05-03 20:07:16 +02:00
* @ param string $modelspath Relative folder where generators are placed
2020-08-18 14:48:38 +02:00
* @ param string $modele Generator to use . Caller must set it to obj -> model_pdf or GETPOST ( 'model_pdf' , 'alpha' ) for example .
2017-08-18 23:16:40 +02:00
* @ param Translate $outputlangs Output language to use
2015-05-03 20:07:16 +02:00
* @ param int $hidedetails 1 to hide details . 0 by default
* @ param int $hidedesc 1 to hide product description . 0 by default
* @ param int $hideref 1 to hide product reference . 0 by default
2016-10-05 22:24:14 +02:00
* @ param null | array $moreparams Array to provide more information
2015-05-03 20:07:16 +02:00
* @ return int > 0 if OK , < 0 if KO
2019-01-22 10:39:06 +01:00
* @ see addFileIntoDatabaseIndex ()
2014-09-23 19:37:59 +02:00
*/
2019-01-27 15:20:16 +01:00
protected function commonGenerateDocument ( $modelspath , $modele , $outputlangs , $hidedetails , $hidedesc , $hideref , $moreparams = null )
2014-09-23 19:37:59 +02:00
{
2019-10-30 01:35:41 +01:00
global $conf , $langs , $user , $hookmanager , $action ;
2012-04-07 02:24:32 +02:00
2019-11-16 23:24:29 +01:00
$srctemplatepath = '' ;
2017-06-07 16:44:04 +02:00
2019-11-16 23:24:29 +01:00
$parameters = array ( 'modelspath' => $modelspath , 'modele' => $modele , 'outputlangs' => $outputlangs , 'hidedetails' => $hidedetails , 'hidedesc' => $hidedesc , 'hideref' => $hideref , 'moreparams' => $moreparams );
2018-09-25 14:47:13 +02:00
$reshook = $hookmanager -> executeHooks ( 'commonGenerateDocument' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2014-09-21 18:16:14 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $reshook )) {
2020-09-08 21:27:28 +02:00
dol_syslog ( " commonGenerateDocument modele= " . $modele . " outputlangs->defaultlang= " . ( is_object ( $outputlangs ) ? $outputlangs -> defaultlang : 'null' ));
2017-06-07 16:44:04 +02:00
2020-12-14 20:40:47 +01:00
if ( empty ( $modele )) {
$this -> error = 'BadValueForParameterModele' ;
return - 1 ;
}
2020-09-08 21:27:28 +02:00
// Increase limit for PDF build
$err = error_reporting ();
error_reporting ( 0 );
@ set_time_limit ( 120 );
error_reporting ( $err );
// If selected model is a filename template (then $modele="modelname" or "modelname:filename")
$tmp = explode ( ':' , $modele , 2 );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $tmp [ 1 ])) {
2020-09-08 21:27:28 +02:00
$modele = $tmp [ 0 ];
$srctemplatepath = $tmp [ 1 ];
}
2014-09-21 18:16:14 +02:00
2020-09-08 21:27:28 +02:00
// Search template files
2019-11-16 23:24:29 +01:00
$file = '' ;
$classname = '' ;
2020-05-31 22:59:33 +02:00
$filefound = '' ;
2020-09-08 21:27:28 +02:00
$dirmodels = array ( '/' );
2021-02-23 22:03:23 +01:00
if ( is_array ( $conf -> modules_parts [ 'models' ])) {
$dirmodels = array_merge ( $dirmodels , $conf -> modules_parts [ 'models' ]);
}
foreach ( $dirmodels as $reldir ) {
foreach ( array ( 'doc' , 'pdf' ) as $prefix ) {
2020-12-14 20:40:47 +01:00
if ( in_array ( get_class ( $this ), array ( 'Adherent' ))) {
// Member module use prefix_modele.class.php
$file = $prefix . " _ " . $modele . " .class.php " ;
} else {
// Other module use prefix_modele.modules.php
$file = $prefix . " _ " . $modele . " .modules.php " ;
}
2020-09-08 21:27:28 +02:00
// On verifie l'emplacement du modele
$file = dol_buildpath ( $reldir . $modelspath . $file , 0 );
2020-12-14 20:40:47 +01:00
if ( file_exists ( $file )) {
2020-09-08 21:27:28 +02:00
$filefound = $file ;
$classname = $prefix . '_' . $modele ;
break ;
}
2014-09-21 18:16:14 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $filefound ) {
break ;
}
2014-09-21 18:16:14 +02:00
}
2017-06-07 16:44:04 +02:00
2020-09-08 21:27:28 +02:00
// If generator was found
2021-02-23 22:03:23 +01:00
if ( $filefound ) {
2020-09-08 21:27:28 +02:00
global $db ; // Required to solve a conception default making an include of code using $db instead of $this->db just after.
2017-06-07 16:44:04 +02:00
2020-09-08 21:27:28 +02:00
require_once $file ;
2017-06-07 16:44:04 +02:00
2020-09-08 21:27:28 +02:00
$obj = new $classname ( $this -> db );
2017-06-07 16:44:04 +02:00
2020-09-08 21:27:28 +02:00
// If generator is ODT, we must have srctemplatepath defined, if not we set it.
2021-02-23 22:03:23 +01:00
if ( $obj -> type == 'odt' && empty ( $srctemplatepath )) {
2020-09-08 21:27:28 +02:00
$varfortemplatedir = $obj -> scandir ;
2021-02-23 22:03:23 +01:00
if ( $varfortemplatedir && ! empty ( $conf -> global -> $varfortemplatedir )) {
2020-09-08 21:27:28 +02:00
$dirtoscan = $conf -> global -> $varfortemplatedir ;
2017-06-07 16:44:04 +02:00
2020-09-08 21:27:28 +02:00
$listoffiles = array ();
2017-06-07 16:44:04 +02:00
2020-09-08 21:27:28 +02:00
// Now we add first model found in directories scanned
$listofdir = explode ( ',' , $dirtoscan );
2021-02-23 22:03:23 +01:00
foreach ( $listofdir as $key => $tmpdir ) {
2020-09-08 21:27:28 +02:00
$tmpdir = trim ( $tmpdir );
$tmpdir = preg_replace ( '/DOL_DATA_ROOT/' , DOL_DATA_ROOT , $tmpdir );
2021-02-23 22:03:23 +01:00
if ( ! $tmpdir ) {
2021-03-01 20:37:16 +01:00
unset ( $listofdir [ $key ]);
continue ;
2021-02-23 22:03:23 +01:00
}
if ( is_dir ( $tmpdir )) {
2020-09-08 21:27:28 +02:00
$tmpfiles = dol_dir_list ( $tmpdir , 'files' , 0 , '\.od(s|t)$' , '' , 'name' , SORT_ASC , 0 );
2021-02-23 22:03:23 +01:00
if ( count ( $tmpfiles )) {
$listoffiles = array_merge ( $listoffiles , $tmpfiles );
}
2020-09-08 21:27:28 +02:00
}
2017-10-07 13:09:31 +02:00
}
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( count ( $listoffiles )) {
foreach ( $listoffiles as $record ) {
2020-09-08 21:27:28 +02:00
$srctemplatepath = $record [ 'fullname' ];
break ;
}
2017-10-07 13:09:31 +02:00
}
}
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $srctemplatepath )) {
2020-09-08 21:27:28 +02:00
$this -> error = 'ErrorGenerationAskedForOdtTemplateWithSrcFileNotDefined' ;
return - 1 ;
}
2015-05-03 20:07:16 +02:00
}
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( $obj -> type == 'odt' && ! empty ( $srctemplatepath )) {
if ( ! dol_is_file ( $srctemplatepath )) {
2020-09-08 21:27:28 +02:00
dol_syslog ( " Failed to locate template file " . $srctemplatepath , LOG_WARNING );
$this -> error = 'ErrorGenerationAskedForOdtTemplateWithSrcFileNotFound' ;
return - 1 ;
}
2017-10-07 13:09:31 +02:00
}
2017-06-07 16:44:04 +02:00
2020-09-08 21:27:28 +02:00
// We save charset_output to restore it because write_file can change it if needed for
// output format that does not support UTF8.
$sav_charset_output = $outputlangs -> charset_output ;
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( in_array ( get_class ( $this ), array ( 'Adherent' ))) {
2020-09-08 21:27:28 +02:00
$arrayofrecords = array (); // The write_file of templates of adherent class need this var
$resultwritefile = $obj -> write_file ( $this , $outputlangs , $srctemplatepath , 'member' , 1 , $moreparams );
} else {
$resultwritefile = $obj -> write_file ( $this , $outputlangs , $srctemplatepath , $hidedetails , $hidedesc , $hideref , $moreparams );
}
// After call of write_file $obj->result['fullpath'] is set with generated file. It will be used to update the ECM database index.
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( $resultwritefile > 0 ) {
2020-09-08 21:27:28 +02:00
$outputlangs -> charset_output = $sav_charset_output ;
2017-06-07 16:44:04 +02:00
2020-09-08 21:27:28 +02:00
// We delete old preview
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
dol_delete_preview ( $this );
2017-06-07 16:44:04 +02:00
2020-09-08 21:27:28 +02:00
// Index file in database
2021-02-23 22:03:23 +01:00
if ( ! empty ( $obj -> result [ 'fullpath' ])) {
2020-09-08 21:27:28 +02:00
$destfull = $obj -> result [ 'fullpath' ];
$upload_dir = dirname ( $destfull );
$destfile = basename ( $destfull );
$rel_dir = preg_replace ( '/^' . preg_quote ( DOL_DATA_ROOT , '/' ) . '/' , '' , $upload_dir );
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( ! preg_match ( '/[\\/]temp[\\/]|[\\/]thumbs|\.meta$/' , $rel_dir )) { // If not a tmp dir
2020-09-08 21:27:28 +02:00
$filename = basename ( $destfile );
$rel_dir = preg_replace ( '/[\\/]$/' , '' , $rel_dir );
$rel_dir = preg_replace ( '/^[\\/]/' , '' , $rel_dir );
2013-06-16 21:31:21 +02:00
2020-09-08 21:27:28 +02:00
include_once DOL_DOCUMENT_ROOT . '/ecm/class/ecmfiles.class.php' ;
$ecmfile = new EcmFiles ( $this -> db );
$result = $ecmfile -> fetch ( 0 , '' , ( $rel_dir ? $rel_dir . '/' : '' ) . $filename );
2013-06-10 16:05:41 +02:00
2020-09-08 21:27:28 +02:00
// Set the public "share" key
$setsharekey = false ;
2021-02-23 22:03:23 +01:00
if ( $this -> element == 'propal' ) {
2020-09-08 21:27:28 +02:00
$useonlinesignature = $conf -> global -> MAIN_FEATURES_LEVEL ; // Replace this with 1 when feature to make online signature is ok
2021-02-23 22:03:23 +01:00
if ( $useonlinesignature ) {
$setsharekey = true ;
}
if ( ! empty ( $conf -> global -> PROPOSAL_ALLOW_EXTERNAL_DOWNLOAD )) {
$setsharekey = true ;
}
2017-08-18 23:16:40 +02:00
}
2020-09-08 21:27:28 +02:00
if ( $this -> element == 'commande' && ! empty ( $conf -> global -> ORDER_ALLOW_EXTERNAL_DOWNLOAD )) {
$setsharekey = true ;
}
if ( $this -> element == 'facture' && ! empty ( $conf -> global -> INVOICE_ALLOW_EXTERNAL_DOWNLOAD )) {
$setsharekey = true ;
}
if ( $this -> element == 'bank_account' && ! empty ( $conf -> global -> BANK_ACCOUNT_ALLOW_EXTERNAL_DOWNLOAD )) {
$setsharekey = true ;
2017-08-18 23:16:40 +02:00
}
2021-02-18 21:39:25 +01:00
if ( $this -> element == 'contrat' && ! empty ( $conf -> global -> CONTRACT_ALLOW_EXTERNAL_DOWNLOAD )) {
$setsharekey = true ;
}
2021-04-24 08:05:00 +02:00
if ( $this -> element == 'supplier_proposal' && ! empty ( $conf -> global -> SUPPLIER_PROPOSAL_ALLOW_EXTERNAL_DOWNLOAD )) {
$setsharekey = true ;
}
2017-06-07 16:44:04 +02:00
2020-09-08 21:27:28 +02:00
if ( $setsharekey ) {
2021-02-23 22:03:23 +01:00
if ( empty ( $ecmfile -> share )) { // Because object not found or share not set yet
2020-09-08 21:27:28 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php' ;
$ecmfile -> share = getRandomPassword ( true );
}
}
2012-04-07 02:24:32 +02:00
2021-02-23 22:03:23 +01:00
if ( $result > 0 ) {
2020-09-08 21:27:28 +02:00
$ecmfile -> label = md5_file ( dol_osencode ( $destfull )); // hash of file content
$ecmfile -> fullpath_orig = '' ;
$ecmfile -> gen_or_uploaded = 'generated' ;
$ecmfile -> description = '' ; // indexed content
2021-06-29 19:49:26 +02:00
$ecmfile -> keywords = '' ; // keyword content
2020-09-08 21:27:28 +02:00
$result = $ecmfile -> update ( $user );
if ( $result < 0 ) {
setEventMessages ( $ecmfile -> error , $ecmfile -> errors , 'warnings' );
}
} else {
$ecmfile -> entity = $conf -> entity ;
$ecmfile -> filepath = $rel_dir ;
$ecmfile -> filename = $filename ;
$ecmfile -> label = md5_file ( dol_osencode ( $destfull )); // hash of file content
$ecmfile -> fullpath_orig = '' ;
$ecmfile -> gen_or_uploaded = 'generated' ;
$ecmfile -> description = '' ; // indexed content
2021-06-29 19:49:26 +02:00
$ecmfile -> keywords = '' ; // keyword content
2021-10-06 14:01:48 +02:00
$ecmfile -> src_object_type = $this -> table_element ; // $this->table_name is 'myobject' or 'mymodule_myobject'.
2020-09-08 21:27:28 +02:00
$ecmfile -> src_object_id = $this -> id ;
$result = $ecmfile -> create ( $user );
if ( $result < 0 ) {
setEventMessages ( $ecmfile -> error , $ecmfile -> errors , 'warnings' );
}
}
/* $this -> result [ 'fullname' ] = $destfull ;
2021-02-23 22:03:23 +01:00
$this -> result [ 'filepath' ] = $ecmfile -> filepath ;
$this -> result [ 'filename' ] = $ecmfile -> filename ; */
2020-09-08 21:27:28 +02:00
//var_dump($obj->update_main_doc_field);exit;
2019-01-29 18:40:47 +01:00
2021-02-05 18:23:17 +01:00
// Update the last_main_doc field into main object (if document generator has property ->update_main_doc_field set)
2020-09-08 21:27:28 +02:00
$update_main_doc_field = 0 ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $obj -> update_main_doc_field )) {
$update_main_doc_field = 1 ;
}
if ( $update_main_doc_field && ! empty ( $this -> table_element )) {
2020-09-08 21:27:28 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element . " SET last_main_doc = ' " . $this -> db -> escape ( $ecmfile -> filepath . '/' . $ecmfile -> filename ) . " ' " ;
$sql .= ' WHERE rowid = ' . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2019-10-27 11:18:36 +01:00
dol_print_error ( $this -> db );
} else {
2020-09-08 21:27:28 +02:00
$this -> last_main_doc = $ecmfile -> filepath . '/' . $ecmfile -> filename ;
}
2019-01-29 18:40:47 +01:00
}
2017-10-19 11:14:31 +02:00
}
2020-09-08 21:27:28 +02:00
} else {
dol_syslog ( 'Method ->write_file was called on object ' . get_class ( $obj ) . ' and return a success but the return array ->result["fullpath"] was not set.' , LOG_WARNING );
2017-08-18 23:16:40 +02:00
}
2020-09-08 21:27:28 +02:00
// Success in building document. We build meta file.
dol_meta_create ( $this );
2014-09-21 18:16:14 +02:00
2020-09-08 21:27:28 +02:00
return 1 ;
} else {
$outputlangs -> charset_output = $sav_charset_output ;
dol_print_error ( $this -> db , " Error generating document for " . __CLASS__ . " . Error: " . $obj -> error , $obj -> errors );
return - 1 ;
}
} else {
if ( ! $filefound ) {
$this -> error = $langs -> trans ( " Error " ) . ' Failed to load doc generator with modelpaths=' . $modelspath . ' - modele=' . $modele ;
dol_print_error ( '' , $this -> error );
} else {
$this -> error = $langs -> trans ( " Error " ) . " " . $langs -> trans ( " ErrorFileDoesNotExists " , $filefound );
dol_print_error ( '' , $this -> error );
}
2014-09-21 18:16:14 +02:00
return - 1 ;
2012-04-06 11:38:13 +02:00
}
2021-02-23 22:03:23 +01:00
} else {
return $reshook ;
}
2012-04-06 11:38:13 +02:00
}
2012-04-07 02:24:32 +02:00
2012-04-06 11:38:13 +02:00
/**
2015-03-14 14:55:41 +01:00
* Build thumb
2019-12-11 19:11:13 +01:00
* @ todo Move this into files . lib . php
2012-04-06 11:38:13 +02:00
*
2015-03-14 14:55:41 +01:00
* @ param string $file Path file in UTF8 to original file to create thumbs from .
* @ return void
2010-08-10 00:39:20 +02:00
*/
2019-02-25 22:27:04 +01:00
public function addThumbs ( $file )
2010-08-10 00:39:20 +02:00
{
2015-03-14 14:55:41 +01:00
global $maxwidthsmall , $maxheightsmall , $maxwidthmini , $maxheightmini , $quality ;
2012-04-07 02:24:32 +02:00
2019-11-13 19:34:37 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/images.lib.php' ; // This define also $maxwidthsmall, $quality, ...
2012-08-23 13:45:51 +02:00
2019-11-13 19:34:37 +01:00
$file_osencoded = dol_osencode ( $file );
2021-02-23 22:03:23 +01:00
if ( file_exists ( $file_osencoded )) {
2015-03-14 14:55:41 +01:00
// Create small thumbs for company (Ratio is near 16/9)
2017-10-07 13:09:31 +02:00
// Used on logon for example
vignette ( $file_osencoded , $maxwidthsmall , $maxheightsmall , '_small' , $quality );
2013-03-22 17:10:17 +01:00
2017-10-07 13:09:31 +02:00
// Create mini thumbs for company (Ratio is near 16/9)
// Used on menu or for setup page for example
vignette ( $file_osencoded , $maxwidthmini , $maxheightmini , '_mini' , $quality );
2015-03-14 14:55:41 +01:00
}
}
2014-02-17 18:22:15 +01:00
2012-04-07 02:24:32 +02:00
2014-09-23 19:37:59 +02:00
/* Functions common to commonobject and commonobjectline */
2012-04-07 02:24:32 +02:00
2017-10-07 13:09:31 +02:00
/* For default values */
2012-04-07 02:24:32 +02:00
2017-10-07 13:09:31 +02:00
/**
* Return the default value to use for a field when showing the create form of object .
* Return values in this order :
* 1 ) If parameter is available into POST , we return it first .
* 2 ) If not but an alternate value was provided as parameter of function , we return it .
* 3 ) If not but a constant $conf -> global -> OBJECTELEMENT_FIELDNAME is set , we return it ( It is better to use the dedicated table ) .
* 4 ) Return value found into database ( TODO No yet implemented )
*
* @ param string $fieldname Name of field
* @ param string $alternatevalue Alternate value to use
* @ return string | string [] Default value ( can be an array if the GETPOST return an array )
**/
2019-02-25 22:27:04 +01:00
public function getDefaultCreateValueFor ( $fieldname , $alternatevalue = null )
2017-10-07 13:09:31 +02:00
{
global $conf , $_POST ;
2016-06-30 17:28:13 +02:00
2017-10-07 13:09:31 +02:00
// If param here has been posted, we use this value first.
2021-02-23 22:03:23 +01:00
if ( GETPOSTISSET ( $fieldname )) {
return GETPOST ( $fieldname , 'alphanohtml' , 3 );
}
2012-08-23 13:45:51 +02:00
2021-02-23 22:03:23 +01:00
if ( isset ( $alternatevalue )) {
return $alternatevalue ;
}
2012-04-07 02:24:32 +02:00
2019-11-13 19:34:37 +01:00
$newelement = $this -> element ;
2021-02-23 22:03:23 +01:00
if ( $newelement == 'facture' ) {
$newelement = 'invoice' ;
}
if ( $newelement == 'commande' ) {
$newelement = 'order' ;
}
if ( empty ( $newelement )) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " Ask a default value using common method getDefaultCreateValueForField on an object with no property ->element defined. Return empty string. " , LOG_WARNING );
return '' ;
2012-04-06 11:38:13 +02:00
}
2010-12-14 10:03:50 +01:00
2019-11-13 19:34:37 +01:00
$keyforfieldname = strtoupper ( $newelement . '_DEFAULT_' . $fieldname );
2017-10-07 13:09:31 +02:00
//var_dump($keyforfieldname);
2021-02-23 22:03:23 +01:00
if ( isset ( $conf -> global -> $keyforfieldname )) {
return $conf -> global -> $keyforfieldname ;
}
2011-04-11 10:34:26 +02:00
2017-10-07 13:09:31 +02:00
// TODO Ad here a scan into table llx_overwrite_default with a filter on $this->element and $fieldname
}
2011-04-11 10:34:26 +02:00
2014-09-23 19:37:59 +02:00
/* For triggers */
2011-11-07 15:04:02 +01:00
2019-05-13 22:02:33 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
* Call trigger based on this instance .
* Some context information may also be provided into array property this -> context .
2017-10-24 18:45:34 +02:00
* NB : Error from trigger are stacked in interface -> errors
2017-10-07 13:09:31 +02:00
* NB2 : If return code of triggers are < 0 , action calling trigger should cancel all transaction .
*
2019-12-02 09:38:16 +01:00
* @ param string $triggerName trigger ' s name to execute
2017-10-07 13:09:31 +02:00
* @ param User $user Object user
* @ return int Result of run_triggers
*/
2019-12-02 09:38:16 +01:00
public function call_trigger ( $triggerName , $user )
2017-10-07 13:09:31 +02:00
{
2019-05-13 22:02:33 +02:00
// phpcs:enable
2019-11-16 23:24:29 +01:00
global $langs , $conf ;
2011-11-07 15:04:02 +01:00
2019-11-16 23:24:29 +01:00
if ( ! is_object ( $langs )) { // If lang was not defined, we set it. It is required by run_triggers.
2019-11-09 13:08:30 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/class/translate.class.php' ;
$langs = new Translate ( '' , $conf );
}
2019-11-16 23:24:29 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
$interface = new Interfaces ( $this -> db );
2019-12-02 09:38:16 +01:00
$result = $interface -> run_triggers ( $triggerName , $this , $user , $langs , $conf );
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
if ( ! empty ( $this -> errors )) {
2019-11-16 23:24:29 +01:00
$this -> errors = array_unique ( array_merge ( $this -> errors , $interface -> errors )); // We use array_unique because when a trigger call another trigger on same object, this->errors is added twice.
2020-05-21 15:05:19 +02:00
} else {
2019-11-16 23:24:29 +01:00
$this -> errors = $interface -> errors ;
2017-10-07 13:09:31 +02:00
}
}
return $result ;
}
2020-03-27 16:02:58 +01:00
/* Functions for data in other language */
/**
* Function to get alternative languages of a data into $this -> array_languages
* This method is NOT called by method fetch of objects but must be called separately .
*
* @ return int < 0 if error , 0 if no values of alternative languages to find nor found , 1 if a value was found and loaded
2020-04-01 16:21:03 +02:00
* @ see fetch_optionnals ()
2020-03-27 16:02:58 +01:00
*/
2020-04-01 16:21:03 +02:00
public function fetchValuesForExtraLanguages ()
2020-03-27 16:02:58 +01:00
{
// To avoid SQL errors. Probably not the better solution though
if ( ! $this -> element ) {
return 0 ;
}
2020-04-10 10:59:32 +02:00
if ( ! ( $this -> id > 0 )) {
2020-03-27 16:02:58 +01:00
return 0 ;
}
2020-03-29 04:28:23 +02:00
if ( is_array ( $this -> array_languages )) {
return 1 ;
}
2020-03-27 16:02:58 +01:00
$this -> array_languages = array ();
$element = $this -> element ;
2021-02-23 22:03:23 +01:00
if ( $element == 'categorie' ) {
$element = 'categories' ; // For compatibility
}
2020-03-27 16:02:58 +01:00
// Request to get translation values for object
$sql = " SELECT rowid, property, lang , value " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " object_lang " ;
2020-09-19 21:19:04 +02:00
$sql .= " WHERE type_object = ' " . $this -> db -> escape ( $element ) . " ' " ;
2021-08-23 18:56:46 +02:00
$sql .= " AND fk_object = " . (( int ) $this -> id );
2020-03-27 16:02:58 +01:00
//dol_syslog(get_class($this)."::fetch_optionals get extrafields data for ".$this->table_element, LOG_DEBUG); // Too verbose
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2020-03-27 16:02:58 +01:00
$numrows = $this -> db -> num_rows ( $resql );
2021-02-23 22:03:23 +01:00
if ( $numrows ) {
2020-03-29 04:28:23 +02:00
$i = 0 ;
while ( $i < $numrows ) {
$obj = $this -> db -> fetch_object ( $resql );
$key = $obj -> property ;
$value = $obj -> value ;
$codelang = $obj -> lang ;
$type = $this -> fields [ $key ][ 'type' ];
2020-03-27 16:02:58 +01:00
// we can add this attribute to object
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/date/' , $type )) {
2020-03-29 04:28:23 +02:00
$this -> array_languages [ $key ][ $codelang ] = $this -> db -> jdate ( $value );
2020-05-21 15:05:19 +02:00
} else {
2020-03-29 04:28:23 +02:00
$this -> array_languages [ $key ][ $codelang ] = $value ;
2020-03-27 16:02:58 +01:00
}
2020-03-29 04:28:23 +02:00
$i ++ ;
2020-03-27 16:02:58 +01:00
}
}
$this -> db -> free ( $resql );
2021-02-23 22:03:23 +01:00
if ( $numrows ) {
return $numrows ;
} else {
return 0 ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-03-27 16:02:58 +01:00
dol_print_error ( $this -> db );
return - 1 ;
}
}
2020-03-29 04:28:23 +02:00
/**
* Fill array_options property of object by extrafields value ( using for data sent by forms )
*
* @ param string $onlykey Only the following key is filled . When we make update of only one language field ( $action = 'update_languages' ), calling page must set this to avoid to have other languages being reset .
* @ return int 1 if array_options set , 0 if no value , - 1 if error ( field required missing for example )
*/
2020-04-01 16:21:03 +02:00
public function setValuesForExtraLanguages ( $onlykey = '' )
2020-03-29 04:28:23 +02:00
{
global $_POST , $langs ;
// Get extra fields
2020-04-10 10:59:32 +02:00
foreach ( $_POST as $postfieldkey => $postfieldvalue ) {
2020-03-29 04:28:23 +02:00
$tmparray = explode ( '-' , $postfieldkey );
2021-02-23 22:03:23 +01:00
if ( $tmparray [ 0 ] != 'field' ) {
continue ;
}
2020-03-29 04:28:23 +02:00
$element = $tmparray [ 1 ];
$key = $tmparray [ 2 ];
$codelang = $tmparray [ 3 ];
//var_dump("postfieldkey=".$postfieldkey." element=".$element." key=".$key." codelang=".$codelang);
2021-02-23 22:03:23 +01:00
if ( ! empty ( $onlykey ) && $key != $onlykey ) {
continue ;
}
if ( $element != $this -> element ) {
continue ;
}
2020-03-29 04:28:23 +02:00
$key_type = $this -> fields [ $key ][ 'type' ];
$enabled = 1 ;
2021-02-23 22:03:23 +01:00
if ( isset ( $this -> fields [ $key ][ 'enabled' ])) {
2020-03-29 04:28:23 +02:00
$enabled = dol_eval ( $this -> fields [ $key ][ 'enabled' ], 1 );
2017-10-07 13:09:31 +02:00
}
2020-03-29 04:28:23 +02:00
/* $perms = 1 ;
if ( isset ( $this -> fields [ $key ][ 'perms' ]))
{
$perms = dol_eval ( $this -> fields [ $key ][ 'perms' ], 1 );
} */
2021-02-23 22:03:23 +01:00
if ( empty ( $enabled )) {
continue ;
}
2020-03-29 04:28:23 +02:00
//if (empty($perms)) continue;
2021-02-23 22:03:23 +01:00
if ( in_array ( $key_type , array ( 'date' ))) {
2020-03-29 04:28:23 +02:00
// Clean parameters
// TODO GMT date in memory must be GMT so we should add gm=true in parameters
$value_key = dol_mktime ( 0 , 0 , 0 , $_POST [ $postfieldkey . " month " ], $_POST [ $postfieldkey . " day " ], $_POST [ $postfieldkey . " year " ]);
2021-02-23 22:03:23 +01:00
} elseif ( in_array ( $key_type , array ( 'datetime' ))) {
2020-03-29 04:28:23 +02:00
// Clean parameters
// TODO GMT date in memory must be GMT so we should add gm=true in parameters
$value_key = dol_mktime ( $_POST [ $postfieldkey . " hour " ], $_POST [ $postfieldkey . " min " ], 0 , $_POST [ $postfieldkey . " month " ], $_POST [ $postfieldkey . " day " ], $_POST [ $postfieldkey . " year " ]);
2021-02-23 22:03:23 +01:00
} elseif ( in_array ( $key_type , array ( 'checkbox' , 'chkbxlst' ))) {
2020-03-29 04:28:23 +02:00
$value_arr = GETPOST ( $postfieldkey , 'array' ); // check if an array
if ( ! empty ( $value_arr )) {
2020-12-14 20:40:47 +01:00
$value_key = implode ( ',' , $value_arr );
2020-03-29 04:28:23 +02:00
} else {
$value_key = '' ;
}
2021-02-23 22:03:23 +01:00
} elseif ( in_array ( $key_type , array ( 'price' , 'double' ))) {
2020-03-29 04:28:23 +02:00
$value_arr = GETPOST ( $postfieldkey , 'alpha' );
$value_key = price2num ( $value_arr );
2020-05-21 15:05:19 +02:00
} else {
2020-03-29 04:28:23 +02:00
$value_key = GETPOST ( $postfieldkey );
2021-02-23 22:03:23 +01:00
if ( in_array ( $key_type , array ( 'link' )) && $value_key == '-1' ) {
$value_key = '' ;
}
2017-10-07 13:09:31 +02:00
}
2020-03-29 04:28:23 +02:00
$this -> array_languages [ $key ][ $codelang ] = $value_key ;
/* if ( $nofillrequired ) {
$langs -> load ( 'errors' );
setEventMessages ( $langs -> trans ( 'ErrorFieldsRequired' ) . ' : ' . implode ( ', ' , $error_field_required ), null , 'errors' );
return - 1 ;
} */
2017-10-07 13:09:31 +02:00
}
2020-03-29 04:28:23 +02:00
return 1 ;
2017-10-07 13:09:31 +02:00
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
/* Functions for extrafields */
2011-11-07 15:04:02 +01:00
2020-04-28 14:25:07 +02:00
/**
* Function to make a fetch but set environment to avoid to load computed values before .
*
* @ param int $id ID of object
* @ return int > 0 if OK , 0 if not found , < 0 if KO
*/
public function fetchNoCompute ( $id )
{
global $conf ;
$savDisableCompute = $conf -> disable_compute ;
$conf -> disable_compute = 1 ;
$ret = $this -> fetch ( $id );
$conf -> disable_compute = $savDisableCompute ;
return $ret ;
}
2011-11-07 15:04:02 +01:00
2019-05-13 22:02:33 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-10-07 13:09:31 +02:00
/**
2017-11-24 20:02:46 +01:00
* Function to get extra fields of an object into $this -> array_options
2017-10-07 13:09:31 +02:00
* This method is in most cases called by method fetch of objects but you can call it separately .
*
* @ param int $rowid Id of line . Use the id of object if not defined . Deprecated . Function must be called without parameters .
* @ param array $optionsArray Array resulting of call of extrafields -> fetch_name_optionals_label () . Deprecated . Function must be called without parameters .
* @ return int < 0 if error , 0 if no values of extrafield to find nor found , 1 if an attribute is found and value loaded
2020-04-01 16:21:03 +02:00
* @ see fetchValuesForExtraLanguages ()
2017-10-07 13:09:31 +02:00
*/
2019-05-13 22:02:33 +02:00
public function fetch_optionals ( $rowid = null , $optionsArray = null )
2017-10-07 13:09:31 +02:00
{
2019-05-13 22:02:33 +02:00
// phpcs:enable
2020-04-28 14:25:07 +02:00
global $conf , $extrafields ;
2019-10-06 14:41:52 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $rowid )) {
$rowid = $this -> id ;
}
if ( empty ( $rowid ) && isset ( $this -> rowid )) {
$rowid = $this -> rowid ; // deprecated
}
2017-10-07 13:09:31 +02:00
// To avoid SQL errors. Probably not the better solution though
if ( ! $this -> table_element ) {
return 0 ;
2013-04-15 15:55:10 +02:00
}
2017-10-07 13:09:31 +02:00
2019-11-13 19:34:37 +01:00
$this -> array_options = array ();
2018-04-17 14:44:55 +02:00
2021-02-23 22:03:23 +01:00
if ( ! is_array ( $optionsArray )) {
2017-10-07 13:09:31 +02:00
// If $extrafields is not a known object, we initialize it. Best practice is to have $extrafields defined into card.php or list.php page.
2021-02-23 22:03:23 +01:00
if ( ! isset ( $extrafields ) || ! is_object ( $extrafields )) {
2017-10-07 13:09:31 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' ;
$extrafields = new ExtraFields ( $this -> db );
2019-09-14 00:02:48 +02:00
}
2017-10-07 13:09:31 +02:00
// Load array of extrafields for elementype = $this->table_element
2021-02-23 22:03:23 +01:00
if ( empty ( $extrafields -> attributes [ $this -> table_element ][ 'loaded' ])) {
2017-10-07 13:09:31 +02:00
$extrafields -> fetch_name_optionals_label ( $this -> table_element );
}
2019-11-13 19:34:37 +01:00
$optionsArray = ( ! empty ( $extrafields -> attributes [ $this -> table_element ][ 'label' ]) ? $extrafields -> attributes [ $this -> table_element ][ 'label' ] : null );
2020-05-21 15:05:19 +02:00
} else {
2018-03-04 11:28:01 +01:00
global $extrafields ;
2018-02-21 14:48:25 +01:00
dol_syslog ( " Warning: fetch_optionals was called with param optionsArray defined when you should pass null now " , LOG_WARNING );
2013-04-15 15:55:10 +02:00
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
$table_element = $this -> table_element ;
2021-02-23 22:03:23 +01:00
if ( $table_element == 'categorie' ) {
$table_element = 'categories' ; // For compatibility
}
2011-11-07 15:04:02 +01:00
2017-10-07 13:09:31 +02:00
// Request to get complementary values
2021-02-23 22:03:23 +01:00
if ( is_array ( $optionsArray ) && count ( $optionsArray ) > 0 ) {
2017-10-07 13:09:31 +02:00
$sql = " SELECT rowid " ;
2021-02-23 22:03:23 +01:00
foreach ( $optionsArray as $name => $label ) {
if ( empty ( $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $name ]) || $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $name ] != 'separate' ) {
2019-11-13 19:34:37 +01:00
$sql .= " , " . $name ;
2017-10-07 13:09:31 +02:00
}
2018-10-09 16:26:40 +02:00
}
2019-11-13 19:34:37 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . $table_element . " _extrafields " ;
2020-08-19 01:43:48 +02:00
$sql .= " WHERE fk_object = " . (( int ) $rowid );
2011-11-07 15:04:02 +01:00
2018-04-21 11:24:45 +02:00
//dol_syslog(get_class($this)."::fetch_optionals get extrafields data for ".$this->table_element, LOG_DEBUG); // Too verbose
2019-11-13 19:34:37 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2019-11-13 19:34:37 +01:00
$numrows = $this -> db -> num_rows ( $resql );
2021-02-23 22:03:23 +01:00
if ( $numrows ) {
2017-10-07 13:09:31 +02:00
$tab = $this -> db -> fetch_array ( $resql );
2011-11-07 15:04:02 +01:00
2021-02-23 22:03:23 +01:00
foreach ( $tab as $key => $value ) {
2017-10-07 13:09:31 +02:00
// Test fetch_array ! is_int($key) because fetch_array result is a mix table with Key as alpha and Key as int (depend db engine)
2021-02-23 22:03:23 +01:00
if ( $key != 'rowid' && $key != 'tms' && $key != 'fk_member' && ! is_int ( $key )) {
2017-10-07 13:09:31 +02:00
// we can add this attribute to object
2021-02-23 22:03:23 +01:00
if ( ! empty ( $extrafields ) && in_array ( $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $key ], array ( 'date' , 'datetime' ))) {
2018-02-20 19:19:27 +01:00
//var_dump($extrafields->attributes[$this->table_element]['type'][$key]);
2019-11-13 19:34:37 +01:00
$this -> array_options [ " options_ " . $key ] = $this -> db -> jdate ( $value );
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> array_options [ " options_ " . $key ] = $value ;
2018-02-20 19:19:27 +01:00
}
2017-05-05 01:22:46 +02:00
2018-02-20 19:19:27 +01:00
//var_dump('key '.$key.' '.$value.' type='.$extrafields->attributes[$this->table_element]['type'][$key].' '.$this->array_options["options_".$key]);
2017-10-07 13:09:31 +02:00
}
}
2019-01-29 18:40:47 +01:00
2019-01-21 14:40:33 +01:00
// If field is a computed field, value must become result of compute
foreach ( $tab as $key => $value ) {
2021-02-23 22:03:23 +01:00
if ( ! empty ( $extrafields ) && ! empty ( $extrafields -> attributes [ $this -> table_element ][ 'computed' ][ $key ])) {
2020-04-28 14:25:07 +02:00
//var_dump($conf->disable_compute);
if ( empty ( $conf -> disable_compute )) {
$this -> array_options [ " options_ " . $key ] = dol_eval ( $extrafields -> attributes [ $this -> table_element ][ 'computed' ][ $key ], 1 , 0 );
}
2019-01-21 14:40:33 +01:00
}
}
2017-10-07 13:09:31 +02:00
}
2012-07-18 15:26:33 +02:00
2017-10-07 13:09:31 +02:00
$this -> db -> free ( $resql );
2021-02-23 22:03:23 +01:00
if ( $numrows ) {
return $numrows ;
} else {
return 0 ;
}
2020-05-21 15:05:19 +02:00
} else {
2021-03-11 19:09:42 +01:00
$this -> errors [] = $this -> db -> lasterror ;
2017-10-07 13:09:31 +02:00
return - 1 ;
}
}
return 0 ;
}
2012-07-18 15:26:33 +02:00
2014-03-15 14:54:23 +01:00
/**
2017-10-07 13:09:31 +02:00
* Delete all extra fields values for the current object .
2014-03-15 14:54:23 +01:00
*
2017-10-07 13:09:31 +02:00
* @ return int < 0 if KO , > 0 if OK
2020-04-23 13:21:39 +02:00
* @ see deleteExtraLanguages (), insertExtraField (), updateExtraField (), setValueFrom ()
2014-03-15 14:54:23 +01:00
*/
2019-02-25 22:27:04 +01:00
public function deleteExtraFields ()
2014-03-15 14:54:23 +01:00
{
2020-04-23 13:21:39 +02:00
global $conf ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) {
return 0 ;
}
2020-04-23 13:21:39 +02:00
2014-03-15 14:54:23 +01:00
$this -> db -> begin ();
2014-04-23 19:04:10 +02:00
2017-07-12 16:45:32 +02:00
$table_element = $this -> table_element ;
2021-02-23 22:03:23 +01:00
if ( $table_element == 'categorie' ) {
$table_element = 'categories' ; // For compatibility
}
2014-04-23 19:04:10 +02:00
2014-09-23 19:37:59 +02:00
dol_syslog ( get_class ( $this ) . " ::deleteExtraFields delete " , LOG_DEBUG );
2021-06-22 17:09:02 +02:00
2021-08-23 19:33:24 +02:00
$sql_del = " DELETE FROM " . MAIN_DB_PREFIX . $table_element . " _extrafields WHERE fk_object = " . (( int ) $this -> id );
2021-06-22 17:09:02 +02:00
2019-11-13 19:34:37 +01:00
$resql = $this -> db -> query ( $sql_del );
2021-02-23 22:03:23 +01:00
if ( ! $resql ) {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2014-09-23 19:37:59 +02:00
$this -> db -> rollback ();
return - 1 ;
2020-05-21 15:05:19 +02:00
} else {
2014-03-15 14:54:23 +01:00
$this -> db -> commit ();
return 1 ;
}
2014-09-23 19:37:59 +02:00
}
2017-10-07 13:09:31 +02:00
/**
* Add / Update all extra fields values for the current object .
* Data to describe values to insert / update are stored into $this -> array_options = array ( 'options_codeforfield1' => 'valueforfield1' , 'options_codeforfield2' => 'valueforfield2' , ... )
* This function delete record with all extrafields and insert them again from the array $this -> array_options .
*
2018-02-05 15:40:18 +01:00
* @ param string $trigger If defined , call also the trigger ( for example COMPANY_MODIFY )
2017-10-24 18:45:34 +02:00
* @ param User $userused Object user
* @ return int - 1 = error , O = did nothing , 1 = OK
2020-04-23 13:21:39 +02:00
* @ see insertExtraLanguages (), updateExtraField (), deleteExtraField (), setValueFrom ()
2017-10-07 13:09:31 +02:00
*/
2019-02-25 22:27:04 +01:00
public function insertExtraFields ( $trigger = '' , $userused = null )
2017-10-07 13:09:31 +02:00
{
2019-11-13 19:34:37 +01:00
global $conf , $langs , $user ;
2017-10-24 18:45:34 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) {
return 0 ;
}
2014-09-23 19:37:59 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $userused )) {
$userused = $user ;
}
2014-09-23 19:37:59 +02:00
2019-11-13 19:34:37 +01:00
$error = 0 ;
2014-09-23 19:37:59 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $this -> array_options )) {
2017-10-07 13:09:31 +02:00
// Check parameters
$langs -> load ( 'admin' );
require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' ;
$extrafields = new ExtraFields ( $this -> db );
2019-11-13 19:34:37 +01:00
$target_extrafields = $extrafields -> fetch_name_optionals_label ( $this -> table_element );
2017-10-07 13:09:31 +02:00
2020-12-14 20:40:47 +01:00
// Eliminate copied source object extra fields that do not exist in target object
2019-11-13 19:34:37 +01:00
$new_array_options = array ();
2017-10-07 13:09:31 +02:00
foreach ( $this -> array_options as $key => $value ) {
2021-02-23 22:03:23 +01:00
if ( in_array ( substr ( $key , 8 ), array_keys ( $target_extrafields ))) { // We remove the 'options_' from $key for test
2017-10-07 13:09:31 +02:00
$new_array_options [ $key ] = $value ;
2021-02-23 22:03:23 +01:00
} elseif ( in_array ( $key , array_keys ( $target_extrafields ))) { // We test on $key that does not contains the 'options_' prefix
2017-12-09 17:29:19 +01:00
$new_array_options [ 'options_' . $key ] = $value ;
2021-02-23 22:03:23 +01:00
}
2017-10-07 13:09:31 +02:00
}
2021-02-23 22:03:23 +01:00
foreach ( $new_array_options as $key => $value ) {
$attributeKey = substr ( $key , 8 ); // Remove 'options_' prefix
$attributeType = $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $attributeKey ];
$attributeLabel = $extrafields -> attributes [ $this -> table_element ][ 'label' ][ $attributeKey ];
$attributeParam = $extrafields -> attributes [ $this -> table_element ][ 'param' ][ $attributeKey ];
$attributeRequired = $extrafields -> attributes [ $this -> table_element ][ 'required' ][ $attributeKey ];
2019-07-01 12:34:27 +02:00
$attrfieldcomputed = $extrafields -> attributes [ $this -> table_element ][ 'computed' ][ $attributeKey ];
2018-02-01 19:03:21 +01:00
2020-12-14 20:40:47 +01:00
// Similar code than into insertExtraFields
2021-02-23 22:03:23 +01:00
if ( $attributeRequired ) {
$mandatorypb = false ;
if ( $attributeType == 'link' && $this -> array_options [ $key ] == '-1' ) {
$mandatorypb = true ;
}
if ( $this -> array_options [ $key ] === '' ) {
$mandatorypb = true ;
}
if ( $attributeType == 'sellist' && $this -> array_options [ $key ] == '0' ) {
$mandatorypb = true ;
}
if ( $mandatorypb ) {
2021-06-21 19:47:43 +02:00
$langs -> load ( " errors " );
dol_syslog ( " Mandatory field ' " . $key . " ' is empty during create and set to required into definition of extrafields " );
2021-02-23 22:03:23 +01:00
$this -> errors [] = $langs -> trans ( 'ErrorFieldRequired' , $attributeLabel );
return - 1 ;
}
}
2018-02-01 19:03:21 +01:00
2018-09-11 14:57:31 +02:00
//dol_syslog("attributeLabel=".$attributeLabel, LOG_DEBUG);
//dol_syslog("attributeType=".$attributeType, LOG_DEBUG);
2021-02-23 22:03:23 +01:00
if ( ! empty ( $attrfieldcomputed )) {
if ( ! empty ( $conf -> global -> MAIN_STORE_COMPUTED_EXTRAFIELDS )) {
2019-07-01 12:34:27 +02:00
$value = dol_eval ( $attrfieldcomputed , 1 , 0 );
2020-09-07 10:18:17 +02:00
dol_syslog ( $langs -> trans ( " Extrafieldcomputed " ) . " sur " . $attributeLabel . " ( " . $value . " ) " , LOG_DEBUG );
$new_array_options [ $key ] = $value ;
2020-05-21 15:05:19 +02:00
} else {
2019-07-03 11:09:24 +02:00
$new_array_options [ $key ] = null ;
}
2019-07-01 12:34:27 +02:00
}
2021-02-23 22:03:23 +01:00
switch ( $attributeType ) {
case 'int' :
if ( ! is_numeric ( $value ) && $value != '' ) {
$this -> errors [] = $langs -> trans ( " ExtraFieldHasWrongValue " , $attributeLabel );
return - 1 ;
} elseif ( $value == '' ) {
$new_array_options [ $key ] = null ;
}
break ;
case 'price' :
case 'double' :
2018-09-11 14:57:31 +02:00
$value = price2num ( $value );
2021-02-23 22:03:23 +01:00
if ( ! is_numeric ( $value ) && $value != '' ) {
2020-08-03 12:08:33 +02:00
dol_syslog ( $langs -> trans ( " ExtraFieldHasWrongValue " ) . " for " . $attributeLabel . " ( " . $value . " is not ' " . $attributeType . " ') " , LOG_DEBUG );
2019-11-13 19:34:37 +01:00
$this -> errors [] = $langs -> trans ( " ExtraFieldHasWrongValue " , $attributeLabel );
2018-09-11 14:57:31 +02:00
return - 1 ;
2021-02-23 22:03:23 +01:00
} elseif ( $value == '' ) {
2018-09-11 14:57:31 +02:00
$new_array_options [ $key ] = null ;
}
//dol_syslog("double value"." sur ".$attributeLabel."(".$value." is '".$attributeType."')", LOG_DEBUG);
$new_array_options [ $key ] = $value ;
break ;
2021-02-23 22:03:23 +01:00
/* case 'select' : // Not required, we chosed value='0' for undefined values
if ( $value == '-1' )
{
$this -> array_options [ $key ] = null ;
}
break ; */
case 'password' :
$algo = '' ;
if ( $this -> array_options [ $key ] != '' && is_array ( $extrafields -> attributes [ $this -> table_element ][ 'param' ][ $attributeKey ][ 'options' ])) {
// If there is an encryption choice, we use it to crypt data before insert
$tmparrays = array_keys ( $extrafields -> attributes [ $this -> table_element ][ 'param' ][ $attributeKey ][ 'options' ]);
$algo = reset ( $tmparrays );
if ( $algo != '' ) {
//global $action; // $action may be 'create', 'update', 'update_extras'...
//var_dump($action);
//var_dump($this->oldcopy);exit;
if ( is_object ( $this -> oldcopy )) { // If this->oldcopy is not defined, we can't know if we change attribute or not, so we must keep value
//var_dump($this->oldcopy->array_options[$key]); var_dump($this->array_options[$key]);
if ( $this -> array_options [ $key ] == $this -> oldcopy -> array_options [ $key ]) { // If old value crypted in database is same than submited new value, it means we don't change it, so we don't update.
$new_array_options [ $key ] = $this -> array_options [ $key ]; // Value is kept
} else {
2018-02-23 16:11:25 +01:00
// var_dump($algo);
$newvalue = dol_hash ( $this -> array_options [ $key ], $algo );
$new_array_options [ $key ] = $newvalue ;
}
2021-02-23 22:03:23 +01:00
} else {
$new_array_options [ $key ] = $this -> array_options [ $key ]; // Value is kept
}
}
} else // Common usage
{
$new_array_options [ $key ] = $this -> array_options [ $key ];
}
break ;
2017-10-07 13:09:31 +02:00
case 'date' :
case 'datetime' :
2018-04-11 16:27:56 +02:00
// If data is a string instead of a timestamp, we convert it
2019-11-13 19:34:37 +01:00
if ( ! is_int ( $this -> array_options [ $key ])) {
2018-04-10 16:39:28 +02:00
$this -> array_options [ $key ] = strtotime ( $this -> array_options [ $key ]);
}
2017-10-07 13:09:31 +02:00
$new_array_options [ $key ] = $this -> db -> idate ( $this -> array_options [ $key ]);
break ;
2021-02-23 22:03:23 +01:00
case 'link' :
2019-11-13 19:34:37 +01:00
$param_list = array_keys ( $attributeParam [ 'options' ]);
2015-05-25 18:43:46 +02:00
// 0 : ObjectName
// 1 : classPath
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
dol_include_once ( $InfoFieldList [ 1 ]);
2021-02-23 22:03:23 +01:00
if ( $InfoFieldList [ 0 ] && class_exists ( $InfoFieldList [ 0 ])) {
if ( $value == '-1' ) { // -1 is key for no defined in combo list of objects
2019-11-13 19:34:37 +01:00
$new_array_options [ $key ] = '' ;
2020-12-14 20:40:47 +01:00
} elseif ( $value ) {
2018-02-01 19:03:21 +01:00
$object = new $InfoFieldList [ 0 ]( $this -> db );
2021-02-23 22:03:23 +01:00
if ( is_numeric ( $value )) {
$res = $object -> fetch ( $value ); // Common case
} else {
$res = $object -> fetch ( '' , $value ); // For compatibility
}
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( $res > 0 ) {
$new_array_options [ $key ] = $object -> id ;
} else {
2019-11-13 19:34:37 +01:00
$this -> error = " Id/Ref ' " . $value . " ' for object ' " . $object -> element . " ' not found " ;
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_syslog ( 'Error bad setup of extrafield' , LOG_WARNING );
}
2015-05-25 18:43:46 +02:00
break ;
2021-02-23 22:03:23 +01:00
}
2017-10-07 13:09:31 +02:00
}
2018-02-01 19:03:21 +01:00
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
$table_element = $this -> table_element ;
2021-02-23 22:03:23 +01:00
if ( $table_element == 'categorie' ) {
$table_element = 'categories' ; // For compatibility
}
2017-10-07 13:09:31 +02:00
2019-03-21 11:53:04 +01:00
dol_syslog ( get_class ( $this ) . " ::insertExtraFields delete then insert " , LOG_DEBUG );
2021-08-23 19:33:24 +02:00
$sql_del = " DELETE FROM " . MAIN_DB_PREFIX . $table_element . " _extrafields WHERE fk_object = " . (( int ) $this -> id );
2017-10-07 13:09:31 +02:00
$this -> db -> query ( $sql_del );
$sql = " INSERT INTO " . MAIN_DB_PREFIX . $table_element . " _extrafields (fk_object " ;
2021-02-23 22:03:23 +01:00
foreach ( $new_array_options as $key => $value ) {
2019-11-13 19:34:37 +01:00
$attributeKey = substr ( $key , 8 ); // Remove 'options_' prefix
2017-10-07 13:09:31 +02:00
// Add field of attribut
2021-02-23 22:03:23 +01:00
if ( $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $attributeKey ] != 'separate' ) { // Only for other type than separator
2019-11-13 19:34:37 +01:00
$sql .= " , " . $attributeKey ;
2021-02-23 22:03:23 +01:00
}
2017-10-07 13:09:31 +02:00
}
2019-03-21 11:53:04 +01:00
// We must insert a default value for fields for other entities that are mandatory to avoid not null error
2020-12-14 20:40:47 +01:00
if ( ! empty ( $extrafields -> attributes [ $this -> table_element ][ 'mandatoryfieldsofotherentities' ]) && is_array ( $extrafields -> attributes [ $this -> table_element ][ 'mandatoryfieldsofotherentities' ])) {
2021-02-23 22:03:23 +01:00
foreach ( $extrafields -> attributes [ $this -> table_element ][ 'mandatoryfieldsofotherentities' ] as $tmpkey => $tmpval ) {
2020-12-14 20:40:47 +01:00
if ( ! isset ( $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $tmpkey ])) { // If field not already added previously
2020-09-07 10:18:17 +02:00
$sql .= " , " . $tmpkey ;
}
}
2019-03-21 11:53:04 +01:00
}
2017-10-07 13:09:31 +02:00
$sql .= " ) VALUES ( " . $this -> id ;
2017-10-28 16:00:07 +02:00
2020-12-14 20:40:47 +01:00
foreach ( $new_array_options as $key => $value ) {
2019-11-13 19:34:37 +01:00
$attributeKey = substr ( $key , 8 ); // Remove 'options_' prefix
2018-02-23 14:38:41 +01:00
// Add field of attribute
2020-12-14 20:40:47 +01:00
if ( $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $attributeKey ] != 'separate' ) { // Only for other type than separator)
if ( $new_array_options [ $key ] != '' || $new_array_options [ $key ] == '0' ) {
2019-11-13 19:34:37 +01:00
$sql .= " ,' " . $this -> db -> escape ( $new_array_options [ $key ]) . " ' " ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$sql .= " ,null " ;
2017-10-07 13:09:31 +02:00
}
}
}
2019-03-21 11:53:04 +01:00
// We must insert a default value for fields for other entities that are mandatory to avoid not null error
2020-12-14 20:40:47 +01:00
if ( ! empty ( $extrafields -> attributes [ $this -> table_element ][ 'mandatoryfieldsofotherentities' ]) && is_array ( $extrafields -> attributes [ $this -> table_element ][ 'mandatoryfieldsofotherentities' ])) {
2021-02-23 22:03:23 +01:00
foreach ( $extrafields -> attributes [ $this -> table_element ][ 'mandatoryfieldsofotherentities' ] as $tmpkey => $tmpval ) {
2020-12-14 20:40:47 +01:00
if ( ! isset ( $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $tmpkey ])) { // If field not already added previously
2021-02-23 22:03:23 +01:00
if ( in_array ( $tmpval , array ( 'int' , 'double' , 'price' ))) {
$sql .= " , 0 " ;
} else {
$sql .= " , '' " ;
}
2020-09-07 10:18:17 +02:00
}
}
2019-03-21 11:53:04 +01:00
}
2019-11-13 19:34:37 +01:00
$sql .= " ) " ;
2017-10-07 13:09:31 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( ! $resql ) {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-24 18:45:34 +02:00
$error ++ ;
}
2018-02-05 15:40:18 +01:00
2021-02-23 22:03:23 +01:00
if ( ! $error && $trigger ) {
2018-02-05 15:40:18 +01:00
// Call trigger
2019-11-13 19:34:37 +01:00
$this -> context = array ( 'extrafieldaddupdate' => 1 );
$result = $this -> call_trigger ( $trigger , $userused );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2018-02-05 15:40:18 +01:00
// End call trigger
2017-10-24 18:45:34 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $error ) {
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return 1 ;
}
2021-02-23 22:03:23 +01:00
} else {
return 0 ;
}
2014-03-15 14:54:23 +01:00
}
2014-05-14 12:19:45 +02:00
2014-05-11 12:26:57 +02:00
/**
2020-04-01 16:21:03 +02:00
* Add / Update all extra fields values for the current object .
* Data to describe values to insert / update are stored into $this -> array_options = array ( 'options_codeforfield1' => 'valueforfield1' , 'options_codeforfield2' => 'valueforfield2' , ... )
* This function delete record with all extrafields and insert them again from the array $this -> array_options .
2014-05-11 12:26:57 +02:00
*
2018-02-05 15:31:40 +01:00
* @ param string $trigger If defined , call also the trigger ( for example COMPANY_MODIFY )
* @ param User $userused Object user
2020-04-01 16:21:03 +02:00
* @ return int - 1 = error , O = did nothing , 1 = OK
* @ see insertExtraFields (), updateExtraField (), setValueFrom ()
2014-05-11 12:26:57 +02:00
*/
2020-04-01 16:21:03 +02:00
public function insertExtraLanguages ( $trigger = '' , $userused = null )
2014-05-11 12:26:57 +02:00
{
2020-04-01 16:21:03 +02:00
global $conf , $langs , $user ;
2014-05-14 12:19:45 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $userused )) {
$userused = $user ;
}
2014-05-14 12:19:45 +02:00
2020-04-01 16:21:03 +02:00
$error = 0 ;
2014-05-14 12:19:45 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_EXTRALANGUAGES_DISABLED )) {
return 0 ; // For avoid conflicts if trigger used
}
2016-10-02 20:15:11 +02:00
2021-02-23 22:03:23 +01:00
if ( is_array ( $this -> array_languages )) {
2020-04-01 16:21:03 +02:00
$new_array_languages = $this -> array_languages ;
2017-10-07 13:09:31 +02:00
2021-02-23 22:03:23 +01:00
foreach ( $new_array_languages as $key => $value ) {
2020-04-01 16:21:03 +02:00
$attributeKey = $key ;
$attributeType = $this -> fields [ $attributeKey ][ 'type' ];
$attributeLabel = $this -> fields [ $attributeKey ][ 'label' ];
2018-02-23 14:38:41 +01:00
2020-04-01 16:21:03 +02:00
//dol_syslog("attributeLabel=".$attributeLabel, LOG_DEBUG);
//dol_syslog("attributeType=".$attributeType, LOG_DEBUG);
2021-02-23 22:03:23 +01:00
switch ( $attributeType ) {
2020-04-01 16:21:03 +02:00
case 'int' :
2021-02-23 22:03:23 +01:00
if ( ! is_numeric ( $value ) && $value != '' ) {
2020-04-01 16:21:03 +02:00
$this -> errors [] = $langs -> trans ( " ExtraLanguageHasWrongValue " , $attributeLabel );
return - 1 ;
2021-02-23 22:03:23 +01:00
} elseif ( $value == '' ) {
2020-04-01 16:21:03 +02:00
$new_array_languages [ $key ] = null ;
}
break ;
case 'double' :
$value = price2num ( $value );
2021-02-23 22:03:23 +01:00
if ( ! is_numeric ( $value ) && $value != '' ) {
2020-04-01 16:21:03 +02:00
dol_syslog ( $langs -> trans ( " ExtraLanguageHasWrongValue " ) . " sur " . $attributeLabel . " ( " . $value . " is not ' " . $attributeType . " ') " , LOG_DEBUG );
$this -> errors [] = $langs -> trans ( " ExtraLanguageHasWrongValue " , $attributeLabel );
return - 1 ;
2021-02-23 22:03:23 +01:00
} elseif ( $value == '' ) {
2020-04-01 16:21:03 +02:00
$new_array_languages [ $key ] = null ;
}
//dol_syslog("double value"." sur ".$attributeLabel."(".$value." is '".$attributeType."')", LOG_DEBUG);
$new_array_languages [ $key ] = $value ;
break ;
/* case 'select' : // Not required, we chosed value='0' for undefined values
if ( $value == '-1' )
{
$this -> array_options [ $key ] = null ;
}
break ; */
}
}
$this -> db -> begin ();
$table_element = $this -> table_element ;
2021-02-23 22:03:23 +01:00
if ( $table_element == 'categorie' ) {
$table_element = 'categories' ; // For compatibility
}
2020-04-01 16:21:03 +02:00
dol_syslog ( get_class ( $this ) . " ::insertExtraLanguages delete then insert " , LOG_DEBUG );
2020-04-10 10:59:32 +02:00
foreach ( $new_array_languages as $key => $langcodearray ) { // $key = 'name', 'town', ...
foreach ( $langcodearray as $langcode => $value ) {
2020-04-01 16:21:03 +02:00
$sql_del = " DELETE FROM " . MAIN_DB_PREFIX . " object_lang " ;
$sql_del .= " WHERE fk_object = " . $this -> id . " AND property = ' " . $this -> db -> escape ( $key ) . " ' AND type_object = ' " . $this -> db -> escape ( $table_element ) . " ' " ;
$sql_del .= " AND lang = ' " . $this -> db -> escape ( $langcode ) . " ' " ;
$this -> db -> query ( $sql_del );
if ( $value !== '' ) {
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " object_lang (fk_object, property, type_object, lang, value " ;
$sql .= " ) VALUES ( " . $this -> id . " , ' " . $this -> db -> escape ( $key ) . " ', ' " . $this -> db -> escape ( $table_element ) . " ', ' " . $this -> db -> escape ( $langcode ) . " ', ' " . $this -> db -> escape ( $value ) . " ' " ;
$sql .= " ) " ;
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( ! $resql ) {
2020-04-01 16:21:03 +02:00
$this -> error = $this -> db -> lasterror ();
$error ++ ;
break ;
}
}
}
}
2021-02-23 22:03:23 +01:00
if ( ! $error && $trigger ) {
2020-04-01 16:21:03 +02:00
// Call trigger
$this -> context = array ( 'extralanguagesaddupdate' => 1 );
$result = $this -> call_trigger ( $trigger , $userused );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2020-04-01 16:21:03 +02:00
// End call trigger
}
2021-02-23 22:03:23 +01:00
if ( $error ) {
2020-04-01 16:21:03 +02:00
$this -> db -> rollback ();
return - 1 ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-01 16:21:03 +02:00
$this -> db -> commit ();
return 1 ;
}
2021-02-23 22:03:23 +01:00
} else {
return 0 ;
}
2020-04-01 16:21:03 +02:00
}
2017-10-07 13:09:31 +02:00
/**
2020-12-14 20:40:47 +01:00
* Update 1 extra field value for the current object . Keep other fields unchanged .
2018-02-05 15:31:40 +01:00
* Data to describe values to update are stored into $this -> array_options = array ( 'options_codeforfield1' => 'valueforfield1' , 'options_codeforfield2' => 'valueforfield2' , ... )
2017-10-07 13:09:31 +02:00
*
2020-07-04 17:09:20 +02:00
* @ param string $key Key of the extrafield to update ( without starting 'options_' )
2018-02-05 15:31:40 +01:00
* @ param string $trigger If defined , call also the trigger ( for example COMPANY_MODIFY )
* @ param User $userused Object user
* @ return int - 1 = error , O = did nothing , 1 = OK
2020-04-23 13:21:39 +02:00
* @ see updateExtraLanguages (), insertExtraFields (), deleteExtraFields (), setValueFrom ()
2017-10-07 13:09:31 +02:00
*/
2019-02-25 22:27:04 +01:00
public function updateExtraField ( $key , $trigger = null , $userused = null )
2017-10-07 13:09:31 +02:00
{
2019-11-13 19:34:37 +01:00
global $conf , $langs , $user ;
2014-05-14 12:19:45 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) {
return 0 ;
}
2020-04-23 13:21:39 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $userused )) {
$userused = $user ;
}
2016-04-16 12:19:46 +02:00
2019-11-13 19:34:37 +01:00
$error = 0 ;
2016-04-16 12:19:46 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $this -> array_options ) && isset ( $this -> array_options [ " options_ " . $key ])) {
2017-10-07 13:09:31 +02:00
// Check parameters
$langs -> load ( 'admin' );
require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php' ;
$extrafields = new ExtraFields ( $this -> db );
2020-04-01 16:21:03 +02:00
$extrafields -> fetch_name_optionals_label ( $this -> table_element );
2017-10-07 13:09:31 +02:00
2019-11-13 19:34:37 +01:00
$value = $this -> array_options [ " options_ " . $key ];
2018-02-23 14:38:41 +01:00
$attributeType = $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $key ];
$attributeLabel = $extrafields -> attributes [ $this -> table_element ][ 'label' ][ $key ];
$attributeParam = $extrafields -> attributes [ $this -> table_element ][ 'param' ][ $key ];
$attributeRequired = $extrafields -> attributes [ $this -> table_element ][ 'required' ][ $key ];
2020-12-14 20:40:47 +01:00
$attrfieldcomputed = $extrafields -> attributes [ $this -> table_element ][ 'computed' ][ $key ];
// Similar code than into insertExtraFields
2021-02-23 22:03:23 +01:00
if ( $attributeRequired ) {
2020-12-14 20:40:47 +01:00
$mandatorypb = false ;
2021-02-23 22:03:23 +01:00
if ( $attributeType == 'link' && $this -> array_options [ " options_ " . $key ] == '-1' ) {
$mandatorypb = true ;
}
if ( $this -> array_options [ " options_ " . $key ] === '' ) {
$mandatorypb = true ;
}
if ( $mandatorypb ) {
2021-06-21 19:47:43 +02:00
$langs -> load ( " errors " );
dol_syslog ( " Mandatory field 'options_ " . $key . " ' is empty during update and set to required into definition of extrafields " );
2020-12-14 20:40:47 +01:00
$this -> errors [] = $langs -> trans ( 'ErrorFieldRequired' , $attributeLabel );
return - 1 ;
}
}
2018-02-23 14:38:41 +01:00
2018-09-11 14:57:31 +02:00
//dol_syslog("attributeLabel=".$attributeLabel, LOG_DEBUG);
//dol_syslog("attributeType=".$attributeType, LOG_DEBUG);
2021-02-23 22:03:23 +01:00
if ( ! empty ( $attrfieldcomputed )) {
if ( ! empty ( $conf -> global -> MAIN_STORE_COMPUTED_EXTRAFIELDS )) {
2020-12-14 20:40:47 +01:00
$value = dol_eval ( $attrfieldcomputed , 1 , 0 );
dol_syslog ( $langs -> trans ( " Extrafieldcomputed " ) . " sur " . $attributeLabel . " ( " . $value . " ) " , LOG_DEBUG );
$this -> array_options [ " options_ " . $key ] = $value ;
} else {
$this -> array_options [ " options_ " . $key ] = null ;
}
}
2021-02-23 22:03:23 +01:00
switch ( $attributeType ) {
2017-10-07 13:09:31 +02:00
case 'int' :
2021-02-23 22:03:23 +01:00
if ( ! is_numeric ( $value ) && $value != '' ) {
2019-11-13 19:34:37 +01:00
$this -> errors [] = $langs -> trans ( " ExtraFieldHasWrongValue " , $attributeLabel );
2017-10-07 13:09:31 +02:00
return - 1 ;
2021-02-23 22:03:23 +01:00
} elseif ( $value === '' ) {
2017-10-07 13:09:31 +02:00
$this -> array_options [ " options_ " . $key ] = null ;
}
break ;
2018-09-11 14:57:31 +02:00
case 'double' :
$value = price2num ( $value );
2021-02-23 22:03:23 +01:00
if ( ! is_numeric ( $value ) && $value != '' ) {
2018-09-11 14:57:31 +02:00
dol_syslog ( $langs -> trans ( " ExtraFieldHasWrongValue " ) . " sur " . $attributeLabel . " ( " . $value . " is not ' " . $attributeType . " ') " , LOG_DEBUG );
2019-11-13 19:34:37 +01:00
$this -> errors [] = $langs -> trans ( " ExtraFieldHasWrongValue " , $attributeLabel );
2018-09-11 14:57:31 +02:00
return - 1 ;
2021-02-23 22:03:23 +01:00
} elseif ( $value === '' ) {
2018-09-11 20:51:54 +02:00
$this -> array_options [ " options_ " . $key ] = null ;
2018-09-11 14:57:31 +02:00
}
//dol_syslog("double value"." sur ".$attributeLabel."(".$value." is '".$attributeType."')", LOG_DEBUG);
$this -> array_options [ " options_ " . $key ] = $value ;
break ;
2021-02-23 22:03:23 +01:00
/* case 'select' : // Not required, we chosed value='0' for undefined values
if ( $value == '-1' )
{
$this -> array_options [ $key ] = null ;
}
break ; */
2017-10-07 13:09:31 +02:00
case 'price' :
$this -> array_options [ " options_ " . $key ] = price2num ( $this -> array_options [ " options_ " . $key ]);
break ;
case 'date' :
case 'datetime' :
2021-07-01 15:15:35 +02:00
if ( empty ( $this -> array_options [ " options_ " . $key ])) {
$this -> array_options [ " options_ " . $key ] = null ;
} else {
$this -> array_options [ " options_ " . $key ] = $this -> db -> idate ( $this -> array_options [ " options_ " . $key ]);
}
2017-10-07 13:09:31 +02:00
break ;
2021-10-21 09:49:14 +02:00
case 'boolean' :
if ( empty ( $this -> array_options [ " options_ " . $key ])) {
$this -> array_options [ " options_ " . $key ] = null ;
}
break ;
2020-12-14 20:40:47 +01:00
/*
2017-10-07 13:09:31 +02:00
case 'link' :
2019-11-13 19:34:37 +01:00
$param_list = array_keys ( $attributeParam [ 'options' ]);
2017-10-07 13:09:31 +02:00
// 0 : ObjectName
// 1 : classPath
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
dol_include_once ( $InfoFieldList [ 1 ]);
2020-12-14 20:40:47 +01:00
if ( $InfoFieldList [ 0 ] && class_exists ( $InfoFieldList [ 0 ]))
2017-10-07 13:09:31 +02:00
{
2020-12-14 20:40:47 +01:00
if ( $value == '-1' ) // -1 is key for no defined in combo list of objects
{
$new_array_options [ $key ] = '' ;
} elseif ( $value ) {
$object = new $InfoFieldList [ 0 ]( $this -> db );
if ( is_numeric ( $value )) $res = $object -> fetch ( $value ); // Common case
else $res = $object -> fetch ( '' , $value ); // For compatibility
if ( $res > 0 ) $new_array_options [ $key ] = $object -> id ;
else {
$this -> error = " Id/Ref ' " . $value . " ' for object ' " . $object -> element . " ' not found " ;
$this -> db -> rollback ();
return - 1 ;
}
}
} else {
dol_syslog ( 'Error bad setup of extrafield' , LOG_WARNING );
2017-10-07 13:09:31 +02:00
}
break ;
2020-12-14 20:40:47 +01:00
*/
2017-10-07 13:09:31 +02:00
}
$this -> db -> begin ();
2020-12-14 20:40:47 +01:00
$linealreadyfound = 0 ;
// Check if there is already a line for this object (in most cases, it is, but sometimes it is not, for example when extra field has been created after), so we must keep this overload)
$sql = " SELECT COUNT(rowid) as nb FROM " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields WHERE fk_object = " . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$tmpobj = $this -> db -> fetch_object ( $resql );
if ( $tmpobj ) {
$linealreadyfound = $tmpobj -> nb ;
}
}
if ( $linealreadyfound ) {
2021-07-01 15:15:35 +02:00
if ( $this -> array_options [ " options_ " . $key ] === null ) {
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields SET " . $key . " = null " ;
} else {
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element . " _extrafields SET " . $key . " = ' " . $this -> db -> escape ( $this -> array_options [ " options_ " . $key ]) . " ' " ;
}
2020-12-14 20:40:47 +01:00
$sql .= " WHERE fk_object = " . $this -> id ;
} else {
$result = $this -> insertExtraFields ( '' , $user );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2020-12-14 20:40:47 +01:00
}
2017-10-07 13:09:31 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( ! $resql ) {
2018-02-05 15:31:40 +01:00
$error ++ ;
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
2018-02-05 15:31:40 +01:00
}
2021-02-23 22:03:23 +01:00
if ( ! $error && $trigger ) {
2018-02-05 15:31:40 +01:00
// Call trigger
2019-11-13 19:34:37 +01:00
$this -> context = array ( 'extrafieldupdate' => 1 );
$result = $this -> call_trigger ( $trigger , $userused );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2018-02-05 15:31:40 +01:00
// End call trigger
}
2021-02-23 22:03:23 +01:00
if ( $error ) {
2020-02-17 12:34:00 +01:00
dol_syslog ( __METHOD__ . $this -> error , LOG_ERR );
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return 1 ;
}
2021-02-23 22:03:23 +01:00
} else {
return 0 ;
}
2017-10-07 13:09:31 +02:00
}
2014-09-23 19:37:59 +02:00
2020-04-01 16:21:03 +02:00
/**
* Update an extra language value for the current object .
* Data to describe values to update are stored into $this -> array_options = array ( 'options_codeforfield1' => 'valueforfield1' , 'options_codeforfield2' => 'valueforfield2' , ... )
*
* @ param string $key Key of the extrafield ( without starting 'options_' )
* @ param string $trigger If defined , call also the trigger ( for example COMPANY_MODIFY )
* @ param User $userused Object user
* @ return int - 1 = error , O = did nothing , 1 = OK
* @ see updateExtraFields (), insertExtraLanguages ()
*/
public function updateExtraLanguages ( $key , $trigger = null , $userused = null )
{
global $conf , $langs , $user ;
2021-02-23 22:03:23 +01:00
if ( empty ( $userused )) {
$userused = $user ;
}
2020-04-01 16:21:03 +02:00
$error = 0 ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_EXTRALANGUAGES_DISABLED )) {
return 0 ; // For avoid conflicts if trigger used
}
2020-04-01 16:21:03 +02:00
return 0 ;
2014-05-11 12:26:57 +02:00
}
2014-04-23 19:04:10 +02:00
2013-09-10 13:45:39 +02:00
/**
2017-10-25 11:42:14 +02:00
* Return HTML string to put an input field into a page
* Code very similar with showInputField of extra fields
2013-09-18 16:46:54 +02:00
*
2020-01-09 22:14:39 +01:00
* @ param array $val Array of properties for field to show ( used only if -> fields not defined )
2017-12-22 21:29:09 +01:00
* @ param string $key Key of attribute
2020-12-14 20:40:47 +01:00
* @ param string | array $value Preselected value to show ( for date type it must be in timestamp format , for amount or price it must be a php numeric value , for array type must be array )
2018-04-15 07:07:04 +02:00
* @ param string $moreparam To add more parameters on html input tag
2017-12-22 21:29:09 +01:00
* @ param string $keysuffix Prefix string to add into name and id of field ( can be used to avoid duplicate names )
* @ param string $keyprefix Suffix string to add into name and id of field ( can be used to avoid duplicate names )
2019-03-17 01:45:46 +01:00
* @ param string | int $morecss Value for css to define style / length of field . May also be a numeric .
2020-01-09 22:14:39 +01:00
* @ param int $nonewbutton Force to not show the new button on field that are links to object
2017-10-25 11:42:14 +02:00
* @ return string
2013-09-10 13:45:39 +02:00
*/
2020-01-09 22:14:39 +01:00
public function showInputField ( $val , $key , $value , $moreparam = '' , $keysuffix = '' , $keyprefix = '' , $morecss = 0 , $nonewbutton = 0 )
2017-10-25 11:42:14 +02:00
{
2019-11-13 19:34:37 +01:00
global $conf , $langs , $form ;
2017-10-25 22:02:07 +02:00
2021-02-23 22:03:23 +01:00
if ( ! is_object ( $form )) {
2017-10-25 22:02:07 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/html.form.class.php' ;
2019-11-13 19:34:37 +01:00
$form = new Form ( $this -> db );
2017-10-25 22:02:07 +02:00
}
2017-10-25 11:42:14 +02:00
2020-02-21 17:53:37 +01:00
if ( ! empty ( $this -> fields )) {
2020-01-09 22:14:39 +01:00
$val = $this -> fields [ $key ];
}
2017-10-25 11:42:14 +02:00
2019-11-13 19:34:37 +01:00
$out = '' ;
2020-09-07 10:18:17 +02:00
$type = '' ;
2021-03-02 14:06:11 +01:00
$isDependList = 0 ;
2020-09-07 10:18:17 +02:00
$param = array ();
$param [ 'options' ] = array ();
$reg = array ();
2021-05-27 23:11:05 +02:00
$size = ! empty ( $this -> fields [ $key ][ 'size' ]) ? $this -> fields [ $key ][ 'size' ] : 0 ;
2020-09-07 10:18:17 +02:00
// Because we work on extrafields
if ( preg_match ( '/^(integer|link):(.*):(.*):(.*):(.*)/i' , $val [ 'type' ], $reg )) {
$param [ 'options' ] = array ( $reg [ 2 ] . ':' . $reg [ 3 ] . ':' . $reg [ 4 ] . ':' . $reg [ 5 ] => 'N' );
$type = 'link' ;
} elseif ( preg_match ( '/^(integer|link):(.*):(.*):(.*)/i' , $val [ 'type' ], $reg )) {
$param [ 'options' ] = array ( $reg [ 2 ] . ':' . $reg [ 3 ] . ':' . $reg [ 4 ] => 'N' );
$type = 'link' ;
} elseif ( preg_match ( '/^(integer|link):(.*):(.*)/i' , $val [ 'type' ], $reg )) {
$param [ 'options' ] = array ( $reg [ 2 ] . ':' . $reg [ 3 ] => 'N' );
$type = 'link' ;
2021-01-18 15:31:49 +01:00
} elseif ( preg_match ( '/^(sellist):(.*):(.*):(.*):(.*)/i' , $val [ 'type' ], $reg )) {
$param [ 'options' ] = array ( $reg [ 2 ] . ':' . $reg [ 3 ] . ':' . $reg [ 4 ] . ':' . $reg [ 5 ] => 'N' );
$type = 'sellist' ;
} elseif ( preg_match ( '/^(sellist):(.*):(.*):(.*)/i' , $val [ 'type' ], $reg )) {
$param [ 'options' ] = array ( $reg [ 2 ] . ':' . $reg [ 3 ] . ':' . $reg [ 4 ] => 'N' );
$type = 'sellist' ;
} elseif ( preg_match ( '/^(sellist):(.*):(.*)/i' , $val [ 'type' ], $reg )) {
$param [ 'options' ] = array ( $reg [ 2 ] . ':' . $reg [ 3 ] => 'N' );
2020-09-07 10:18:17 +02:00
$type = 'sellist' ;
} elseif ( preg_match ( '/varchar\((\d+)\)/' , $val [ 'type' ], $reg )) {
$param [ 'options' ] = array ();
$type = 'varchar' ;
$size = $reg [ 1 ];
} elseif ( preg_match ( '/varchar/' , $val [ 'type' ])) {
$param [ 'options' ] = array ();
$type = 'varchar' ;
} else {
$param [ 'options' ] = array ();
$type = $this -> fields [ $key ][ 'type' ];
}
2013-09-18 16:46:54 +02:00
2021-04-29 12:10:55 +02:00
// Special case that force options and type ($type can be integer, varchar, ...)
2021-05-27 23:11:05 +02:00
if ( ! empty ( $this -> fields [ $key ][ 'arrayofkeyval' ]) && is_array ( $this -> fields [ $key ][ 'arrayofkeyval' ])) {
2021-04-29 12:10:55 +02:00
$param [ 'options' ] = $this -> fields [ $key ][ 'arrayofkeyval' ];
$type = 'select' ;
}
2013-09-18 16:46:54 +02:00
2019-11-13 19:34:37 +01:00
$label = $this -> fields [ $key ][ 'label' ];
2018-05-22 22:00:20 +02:00
//$elementtype=$this->fields[$key]['elementtype']; // Seems not used
2021-05-27 23:11:05 +02:00
$default = ( ! empty ( $this -> fields [ $key ][ 'default' ]) ? $this -> fields [ $key ][ 'default' ] : '' );
$computed = ( ! empty ( $this -> fields [ $key ][ 'computed' ]) ? $this -> fields [ $key ][ 'computed' ] : '' );
$unique = ( ! empty ( $this -> fields [ $key ][ 'unique' ]) ? $this -> fields [ $key ][ 'unique' ] : 0 );
$required = ( ! empty ( $this -> fields [ $key ][ 'required' ]) ? $this -> fields [ $key ][ 'required' ] : 0 );
$autofocusoncreate = ( ! empty ( $this -> fields [ $key ][ 'autofocusoncreate' ]) ? $this -> fields [ $key ][ 'autofocusoncreate' ] : 0 );
$langfile = ( ! empty ( $this -> fields [ $key ][ 'langfile' ]) ? $this -> fields [ $key ][ 'langfile' ] : '' );
$list = ( ! empty ( $this -> fields [ $key ][ 'list' ]) ? $this -> fields [ $key ][ 'list' ] : 0 );
2019-11-13 19:34:37 +01:00
$hidden = ( in_array ( abs ( $this -> fields [ $key ][ 'visible' ]), array ( 0 , 2 )) ? 1 : 0 );
2014-09-21 18:16:14 +02:00
2017-10-26 02:55:43 +02:00
$objectid = $this -> id ;
2014-09-21 18:16:14 +02:00
2021-01-23 19:50:04 +01:00
if ( $computed ) {
2021-02-23 22:03:23 +01:00
if ( ! preg_match ( '/^search_/' , $keyprefix )) {
return '<span class="opacitymedium">' . $langs -> trans ( " AutomaticallyCalculated " ) . '</span>' ;
} else {
return '' ;
}
2014-09-21 18:16:14 +02:00
}
2017-06-07 16:44:04 +02:00
2019-03-17 01:45:46 +01:00
// Set value of $morecss. For this, we use in priority showsize from parameters, then $val['css'] then autodefine
2020-09-22 08:54:40 +02:00
if ( empty ( $morecss ) && ! empty ( $val [ 'css' ])) {
2020-09-07 10:18:17 +02:00
$morecss = $val [ 'css' ];
2020-09-22 08:54:40 +02:00
} elseif ( empty ( $morecss )) {
2021-01-23 19:50:04 +01:00
if ( $type == 'date' ) {
2018-05-22 22:00:20 +02:00
$morecss = 'minwidth100imp' ;
2021-01-23 19:50:04 +01:00
} elseif ( $type == 'datetime' || $type == 'link' ) { // link means an foreign key to another primary id
2018-05-22 22:00:20 +02:00
$morecss = 'minwidth200imp' ;
2021-01-23 19:50:04 +01:00
} elseif ( in_array ( $type , array ( 'int' , 'integer' , 'price' )) || preg_match ( '/^double(\([0-9],[0-9]\)){0,1}/' , $type )) {
2018-05-22 22:00:20 +02:00
$morecss = 'maxwidth75' ;
2019-05-13 22:02:33 +02:00
} elseif ( $type == 'url' ) {
2019-11-13 19:34:37 +01:00
$morecss = 'minwidth400' ;
2021-01-23 19:50:04 +01:00
} elseif ( $type == 'boolean' ) {
2019-11-13 19:34:37 +01:00
$morecss = '' ;
2020-05-21 15:05:19 +02:00
} else {
2021-01-23 19:50:04 +01:00
if ( round ( $size ) < 12 ) {
2018-05-22 22:00:20 +02:00
$morecss = 'minwidth100' ;
2021-01-23 19:50:04 +01:00
} elseif ( round ( $size ) <= 48 ) {
2018-05-22 22:00:20 +02:00
$morecss = 'minwidth200' ;
2020-05-21 15:05:19 +02:00
} else {
2018-05-22 22:00:20 +02:00
$morecss = 'minwidth400' ;
2014-09-21 18:16:14 +02:00
}
}
}
2021-03-04 10:31:31 +01:00
if ( in_array ( $type , array ( 'date' ))) {
2019-11-13 19:34:37 +01:00
$tmp = explode ( ',' , $size );
$newsize = $tmp [ 0 ];
2021-03-04 10:31:31 +01:00
$showtime = 0 ;
2014-09-21 18:16:14 +02:00
2018-09-09 10:40:00 +02:00
// Do not show current date when field not required (see selectDate() method)
2021-02-23 22:03:23 +01:00
if ( ! $required && $value == '' ) {
$value = '-1' ;
}
2014-09-21 18:16:14 +02:00
2017-10-25 11:42:14 +02:00
// TODO Must also support $moreparam
2018-09-17 09:17:11 +02:00
$out = $form -> selectDate ( $value , $keyprefix . $key . $keysuffix , $showtime , $showtime , $required , '' , 1 , (( $keyprefix != 'search_' && $keyprefix != 'search_options_' ) ? 1 : 0 ), 0 , 1 );
2021-03-04 10:31:31 +01:00
} elseif ( in_array ( $type , array ( 'datetime' ))) {
2019-11-13 19:34:37 +01:00
$tmp = explode ( ',' , $size );
$newsize = $tmp [ 0 ];
2021-03-04 10:31:31 +01:00
$showtime = 1 ;
// Do not show current date when field not required (see selectDate() method)
if ( ! $required && $value == '' ) $value = '-1' ;
// TODO Must also support $moreparam
$out = $form -> selectDate ( $value , $keyprefix . $key . $keysuffix , $showtime , $showtime , $required , '' , 1 , (( $keyprefix != 'search_' && $keyprefix != 'search_options_' ) ? 1 : 0 ), 0 , 1 , '' , '' , '' , 1 , '' , '' , 'tzuserrel' );
2020-12-14 20:40:47 +01:00
} elseif ( in_array ( $type , array ( 'duration' ))) {
2019-11-19 09:30:26 +01:00
$out = $form -> select_duration ( $keyprefix . $key . $keysuffix , $value , 0 , 'text' , 0 , 1 );
2020-12-14 20:40:47 +01:00
} elseif ( in_array ( $type , array ( 'int' , 'integer' ))) {
2019-11-13 19:34:37 +01:00
$tmp = explode ( ',' , $size );
$newsize = $tmp [ 0 ];
2021-07-16 00:05:00 +02:00
$out = '<input type="text" class="flat ' . $morecss . '" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '"' . ( $newsize > 0 ? ' maxlength="' . $newsize . '"' : '' ) . ' value="' . dol_escape_htmltag ( $value ) . '"' . ( $moreparam ? $moreparam : '' ) . ( $autofocusoncreate ? ' autofocus' : '' ) . '>' ;
2020-12-14 20:40:47 +01:00
} elseif ( in_array ( $type , array ( 'real' ))) {
2020-09-22 08:54:40 +02:00
$out = '<input type="text" class="flat ' . $morecss . '" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" value="' . dol_escape_htmltag ( $value ) . '"' . ( $moreparam ? $moreparam : '' ) . ( $autofocusoncreate ? ' autofocus' : '' ) . '>' ;
2020-12-14 20:40:47 +01:00
} elseif ( preg_match ( '/varchar/' , $type )) {
2021-07-16 00:05:00 +02:00
$out = '<input type="text" class="flat ' . $morecss . '" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '"' . ( $size > 0 ? ' maxlength="' . $size . '"' : '' ) . ' value="' . dol_escape_htmltag ( $value ) . '"' . ( $moreparam ? $moreparam : '' ) . ( $autofocusoncreate ? ' autofocus' : '' ) . '>' ;
2020-12-14 20:40:47 +01:00
} elseif ( in_array ( $type , array ( 'mail' , 'phone' , 'url' ))) {
2020-09-22 08:54:40 +02:00
$out = '<input type="text" class="flat ' . $morecss . '" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" value="' . dol_escape_htmltag ( $value ) . '" ' . ( $moreparam ? $moreparam : '' ) . ( $autofocusoncreate ? ' autofocus' : '' ) . '>' ;
2020-12-14 20:40:47 +01:00
} elseif ( preg_match ( '/^text/' , $type )) {
2021-02-23 22:03:23 +01:00
if ( ! preg_match ( '/search_/' , $keyprefix )) { // If keyprefix is search_ or search_options_, we must just use a simple text field
2018-05-22 22:00:20 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php' ;
2019-11-13 19:34:37 +01:00
$doleditor = new DolEditor ( $keyprefix . $key . $keysuffix , $value , '' , 200 , 'dolibarr_notes' , 'In' , false , false , false , ROWS_5 , '90%' );
$out = $doleditor -> Create ( 1 );
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$out = '<input type="text" class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" value="' . dol_escape_htmltag ( $value ) . '" ' . ( $moreparam ? $moreparam : '' ) . '>' ;
2018-05-22 22:00:20 +02:00
}
2020-12-14 20:40:47 +01:00
} elseif ( preg_match ( '/^html/' , $type )) {
if ( ! preg_match ( '/search_/' , $keyprefix )) { // If keyprefix is search_ or search_options_, we must just use a simple text field
2018-05-22 22:00:20 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php' ;
2019-11-13 19:34:37 +01:00
$doleditor = new DolEditor ( $keyprefix . $key . $keysuffix , $value , '' , 200 , 'dolibarr_notes' , 'In' , false , false , ! empty ( $conf -> fckeditor -> enabled ) && $conf -> global -> FCKEDITOR_ENABLE_SOCIETE , ROWS_5 , '90%' );
$out = $doleditor -> Create ( 1 );
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$out = '<input type="text" class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" value="' . dol_escape_htmltag ( $value ) . '" ' . ( $moreparam ? $moreparam : '' ) . '>' ;
2018-05-22 22:00:20 +02:00
}
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'boolean' ) {
2019-11-13 19:34:37 +01:00
$checked = '' ;
2017-10-25 11:42:14 +02:00
if ( ! empty ( $value )) {
2019-11-13 19:34:37 +01:00
$checked = ' checked value="1" ' ;
2017-10-25 11:42:14 +02:00
} else {
2019-11-13 19:34:37 +01:00
$checked = ' value="1" ' ;
2017-10-25 11:42:14 +02:00
}
2019-11-13 19:34:37 +01:00
$out = '<input type="checkbox" class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" ' . $checked . ' ' . ( $moreparam ? $moreparam : '' ) . '>' ;
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'price' ) {
2017-10-25 11:42:14 +02:00
if ( ! empty ( $value )) { // $value in memory is a php numeric, we format it into user number format.
2019-11-13 19:34:37 +01:00
$value = price ( $value );
2017-10-25 11:42:14 +02:00
}
2019-11-13 19:34:37 +01:00
$out = '<input type="text" class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" value="' . $value . '" ' . ( $moreparam ? $moreparam : '' ) . '> ' . $langs -> getCurrencySymbol ( $conf -> currency );
2020-12-14 20:40:47 +01:00
} elseif ( preg_match ( '/^double(\([0-9],[0-9]\)){0,1}/' , $type )) {
2017-10-25 11:42:14 +02:00
if ( ! empty ( $value )) { // $value in memory is a php numeric, we format it into user number format.
2019-11-13 19:34:37 +01:00
$value = price ( $value );
2017-10-25 11:42:14 +02:00
}
2019-11-13 19:34:37 +01:00
$out = '<input type="text" class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" value="' . $value . '" ' . ( $moreparam ? $moreparam : '' ) . '> ' ;
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'select' ) {
2017-10-25 11:42:14 +02:00
$out = '' ;
2021-03-15 14:16:55 +01:00
if ( ! empty ( $conf -> use_javascript_ajax ) && empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLE_SELECT2 )) {
2019-11-13 19:34:37 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php' ;
$out .= ajax_combobox ( $keyprefix . $key . $keysuffix , array (), 0 );
2017-10-25 11:42:14 +02:00
}
2015-05-03 20:07:16 +02:00
2019-11-13 19:34:37 +01:00
$out .= '<select class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" ' . ( $moreparam ? $moreparam : '' ) . '>' ;
2021-02-23 22:03:23 +01:00
if (( ! isset ( $this -> fields [ $key ][ 'default' ])) || ( $this -> fields [ $key ][ 'notnull' ] != 1 )) {
$out .= '<option value="0"> </option>' ;
}
foreach ( $param [ 'options' ] as $key => $val ) {
if (( string ) $key == '' ) {
continue ;
}
2017-10-25 11:42:14 +02:00
list ( $val , $parent ) = explode ( '|' , $val );
2019-11-13 19:34:37 +01:00
$out .= '<option value="' . $key . '"' ;
$out .= ((( string ) $value == ( string ) $key ) ? ' selected' : '' );
$out .= ( ! empty ( $parent ) ? ' parent="' . $parent . '"' : '' );
$out .= '>' . $val . '</option>' ;
2017-10-25 11:42:14 +02:00
}
2019-11-13 19:34:37 +01:00
$out .= '</select>' ;
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'sellist' ) {
2017-10-25 11:42:14 +02:00
$out = '' ;
2021-03-15 14:16:55 +01:00
if ( ! empty ( $conf -> use_javascript_ajax ) && empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLE_SELECT2 )) {
2019-11-13 19:34:37 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php' ;
$out .= ajax_combobox ( $keyprefix . $key . $keysuffix , array (), 0 );
2017-10-25 11:42:14 +02:00
}
2015-05-03 20:07:16 +02:00
2019-11-13 19:34:37 +01:00
$out .= '<select class="flat ' . $morecss . ' maxwidthonsmartphone" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" ' . ( $moreparam ? $moreparam : '' ) . '>' ;
2020-12-14 20:40:47 +01:00
if ( is_array ( $param [ 'options' ])) {
2019-11-13 19:34:37 +01:00
$param_list = array_keys ( $param [ 'options' ]);
2017-10-25 11:42:14 +02:00
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
2019-11-13 19:34:37 +01:00
$parentName = '' ;
$parentField = '' ;
2017-10-25 11:42:14 +02:00
// 0 : tableName
// 1 : label field name
// 2 : key fields name (if differ of rowid)
// 3 : key field parent (for dependent lists)
// 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value
2019-11-13 19:34:37 +01:00
$keyList = ( empty ( $InfoFieldList [ 2 ]) ? 'rowid' : $InfoFieldList [ 2 ] . ' as rowid' );
2017-10-25 11:42:14 +02:00
2021-01-18 15:31:49 +01:00
if ( count ( $InfoFieldList ) > 4 && ! empty ( $InfoFieldList [ 4 ])) {
if ( strpos ( $InfoFieldList [ 4 ], 'extra.' ) !== false ) {
2019-11-13 19:34:37 +01:00
$keyList = 'main.' . $InfoFieldList [ 2 ] . ' as rowid' ;
2017-10-25 11:42:14 +02:00
} else {
2019-11-13 19:34:37 +01:00
$keyList = $InfoFieldList [ 2 ] . ' as rowid' ;
2017-10-25 11:42:14 +02:00
}
}
2021-01-18 15:31:49 +01:00
if ( count ( $InfoFieldList ) > 3 && ! empty ( $InfoFieldList [ 3 ])) {
2017-10-25 11:42:14 +02:00
list ( $parentName , $parentField ) = explode ( '|' , $InfoFieldList [ 3 ]);
2019-11-13 19:34:37 +01:00
$keyList .= ', ' . $parentField ;
2015-05-03 20:07:16 +02:00
}
2019-01-27 11:55:16 +01:00
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
2021-01-18 15:31:49 +01:00
if ( is_array ( $fields_label )) {
2019-11-13 19:34:37 +01:00
$keyList .= ', ' ;
2017-10-25 11:42:14 +02:00
$keyList .= implode ( ', ' , $fields_label );
2015-05-03 20:07:16 +02:00
}
2019-11-13 19:34:37 +01:00
$sqlwhere = '' ;
2017-10-25 11:42:14 +02:00
$sql = 'SELECT ' . $keyList ;
2019-11-13 19:34:37 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList [ 0 ];
2021-02-23 22:03:23 +01:00
if ( ! empty ( $InfoFieldList [ 4 ])) {
2017-10-25 11:42:14 +02:00
// can use SELECT request
2019-11-13 19:34:37 +01:00
if ( strpos ( $InfoFieldList [ 4 ], '$SEL$' ) !== false ) {
$InfoFieldList [ 4 ] = str_replace ( '$SEL$' , 'SELECT' , $InfoFieldList [ 4 ]);
2017-10-25 11:42:14 +02:00
}
2016-06-30 17:28:13 +02:00
2017-10-25 11:42:14 +02:00
// current object id can be use into filter
2019-11-13 19:34:37 +01:00
if ( strpos ( $InfoFieldList [ 4 ], '$ID$' ) !== false && ! empty ( $objectid )) {
$InfoFieldList [ 4 ] = str_replace ( '$ID$' , $objectid , $InfoFieldList [ 4 ]);
2017-10-25 11:42:14 +02:00
} else {
2019-11-13 19:34:37 +01:00
$InfoFieldList [ 4 ] = str_replace ( '$ID$' , '0' , $InfoFieldList [ 4 ]);
2017-10-25 11:42:14 +02:00
}
2021-06-14 01:55:58 +02:00
2017-10-25 11:42:14 +02:00
//We have to join on extrafield table
2021-02-23 22:03:23 +01:00
if ( strpos ( $InfoFieldList [ 4 ], 'extra' ) !== false ) {
2019-11-13 19:34:37 +01:00
$sql .= ' as main, ' . MAIN_DB_PREFIX . $InfoFieldList [ 0 ] . '_extrafields as extra' ;
$sqlwhere .= ' WHERE extra.fk_object=main.' . $InfoFieldList [ 2 ] . ' AND ' . $InfoFieldList [ 4 ];
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$sqlwhere .= ' WHERE ' . $InfoFieldList [ 4 ];
2017-10-25 11:42:14 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$sqlwhere .= ' WHERE 1=1' ;
2017-10-25 11:42:14 +02:00
}
// Some tables may have field, some other not. For the moment we disable it.
2021-02-23 22:03:23 +01:00
if ( in_array ( $InfoFieldList [ 0 ], array ( 'tablewithentity' ))) {
2019-11-13 19:34:37 +01:00
$sqlwhere .= ' AND entity = ' . $conf -> entity ;
2017-10-25 11:42:14 +02:00
}
2019-11-13 19:34:37 +01:00
$sql .= $sqlwhere ;
2017-10-25 11:42:14 +02:00
//print $sql;
2016-10-05 22:24:14 +02:00
2019-11-13 19:34:37 +01:00
$sql .= ' ORDER BY ' . implode ( ', ' , $fields_label );
2016-10-05 22:24:14 +02:00
2017-10-25 11:42:14 +02:00
dol_syslog ( get_class ( $this ) . '::showInputField type=sellist' , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2019-11-13 19:34:37 +01:00
$out .= '<option value="0"> </option>' ;
2017-10-25 11:42:14 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2019-11-13 19:34:37 +01:00
$labeltoshow = '' ;
2017-10-25 11:42:14 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2014-09-21 18:16:14 +02:00
2017-10-25 11:42:14 +02:00
// Several field into label (eq table:code|libelle:rowid)
2018-05-22 22:00:20 +02:00
$notrans = false ;
2019-01-27 11:55:16 +01:00
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
2021-02-23 22:03:23 +01:00
if ( count ( $fields_label ) > 1 ) {
2017-10-25 11:42:14 +02:00
$notrans = true ;
2021-02-23 22:03:23 +01:00
foreach ( $fields_label as $field_toshow ) {
2019-11-13 19:34:37 +01:00
$labeltoshow .= $obj -> $field_toshow . ' ' ;
2017-10-25 11:42:14 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$labeltoshow = $obj -> { $InfoFieldList [ 1 ]};
2017-10-25 11:42:14 +02:00
}
2019-11-13 19:34:37 +01:00
$labeltoshow = dol_trunc ( $labeltoshow , 45 );
2014-09-21 18:16:14 +02:00
2021-02-23 22:03:23 +01:00
if ( $value == $obj -> rowid ) {
foreach ( $fields_label as $field_toshow ) {
2019-11-13 19:34:37 +01:00
$translabel = $langs -> trans ( $obj -> $field_toshow );
if ( $translabel != $obj -> $field_toshow ) {
2021-09-07 19:48:32 +02:00
$labeltoshow = dol_trunc ( $translabel ) . ' ' ;
2019-11-13 19:34:37 +01:00
} else {
2021-09-07 19:48:32 +02:00
$labeltoshow = dol_trunc ( $obj -> $field_toshow ) . ' ' ;
2017-10-25 11:42:14 +02:00
}
}
2019-11-13 19:34:37 +01:00
$out .= '<option value="' . $obj -> rowid . '" selected>' . $labeltoshow . '</option>' ;
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 22:03:23 +01:00
if ( ! $notrans ) {
2019-11-13 19:34:37 +01:00
$translabel = $langs -> trans ( $obj -> { $InfoFieldList [ 1 ]});
if ( $translabel != $obj -> { $InfoFieldList [ 1 ]}) {
$labeltoshow = dol_trunc ( $translabel , 18 );
2020-05-21 15:05:19 +02:00
} else {
2021-09-07 19:48:32 +02:00
$labeltoshow = dol_trunc ( $obj -> { $InfoFieldList [ 1 ]});
2017-10-25 11:42:14 +02:00
}
}
2021-02-23 22:03:23 +01:00
if ( empty ( $labeltoshow )) {
$labeltoshow = '(not defined)' ;
}
if ( $value == $obj -> rowid ) {
2019-11-13 19:34:37 +01:00
$out .= '<option value="' . $obj -> rowid . '" selected>' . $labeltoshow . '</option>' ;
2017-10-25 11:42:14 +02:00
}
2014-09-21 18:16:14 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $InfoFieldList [ 3 ]) && $parentField ) {
2017-10-25 11:42:14 +02:00
$parent = $parentName . ':' . $obj -> { $parentField };
2021-03-02 14:06:11 +01:00
$isDependList = 1 ;
2017-10-25 11:42:14 +02:00
}
2019-11-13 19:34:37 +01:00
$out .= '<option value="' . $obj -> rowid . '"' ;
$out .= ( $value == $obj -> rowid ? ' selected' : '' );
$out .= ( ! empty ( $parent ) ? ' parent="' . $parent . '"' : '' );
$out .= '>' . $labeltoshow . '</option>' ;
2017-10-25 11:42:14 +02:00
}
$i ++ ;
}
$this -> db -> free ( $resql );
2020-05-21 15:05:19 +02:00
} else {
2017-10-25 11:42:14 +02:00
print 'Error in request ' . $sql . ' ' . $this -> db -> lasterror () . '. Check setup of extra parameters.<br>' ;
}
2014-09-21 18:16:14 +02:00
}
2019-11-13 19:34:37 +01:00
$out .= '</select>' ;
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'checkbox' ) {
2019-11-13 19:34:37 +01:00
$value_arr = explode ( ',' , $value );
$out = $form -> multiselectarray ( $keyprefix . $key . $keysuffix , ( empty ( $param [ 'options' ]) ? null : $param [ 'options' ]), $value_arr , '' , 0 , '' , 0 , '100%' );
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'radio' ) {
2019-11-13 19:34:37 +01:00
$out = '' ;
2021-02-23 22:03:23 +01:00
foreach ( $param [ 'options' ] as $keyopt => $val ) {
2019-11-13 19:34:37 +01:00
$out .= '<input class="flat ' . $morecss . '" type="radio" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" ' . ( $moreparam ? $moreparam : '' );
$out .= ' value="' . $keyopt . '"' ;
$out .= ' id="' . $keyprefix . $key . $keysuffix . '_' . $keyopt . '"' ;
$out .= ( $value == $keyopt ? 'checked' : '' );
$out .= '/><label for="' . $keyprefix . $key . $keysuffix . '_' . $keyopt . '">' . $val . '</label><br>' ;
2014-09-21 18:16:14 +02:00
}
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'chkbxlst' ) {
2017-10-25 11:42:14 +02:00
if ( is_array ( $value )) {
$value_arr = $value ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-25 11:42:14 +02:00
$value_arr = explode ( ',' , $value );
}
2014-09-21 18:16:14 +02:00
2017-10-25 11:42:14 +02:00
if ( is_array ( $param [ 'options' ])) {
$param_list = array_keys ( $param [ 'options' ]);
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
2019-11-13 19:34:37 +01:00
$parentName = '' ;
$parentField = '' ;
2017-10-25 11:42:14 +02:00
// 0 : tableName
// 1 : label field name
// 2 : key fields name (if differ of rowid)
// 3 : key field parent (for dependent lists)
// 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value
2019-11-13 19:34:37 +01:00
$keyList = ( empty ( $InfoFieldList [ 2 ]) ? 'rowid' : $InfoFieldList [ 2 ] . ' as rowid' );
2017-10-25 11:42:14 +02:00
2019-11-13 19:34:37 +01:00
if ( count ( $InfoFieldList ) > 3 && ! empty ( $InfoFieldList [ 3 ])) {
list ( $parentName , $parentField ) = explode ( '|' , $InfoFieldList [ 3 ]);
$keyList .= ', ' . $parentField ;
2017-10-25 11:42:14 +02:00
}
2019-11-13 19:34:37 +01:00
if ( count ( $InfoFieldList ) > 4 && ! empty ( $InfoFieldList [ 4 ])) {
2017-10-25 11:42:14 +02:00
if ( strpos ( $InfoFieldList [ 4 ], 'extra.' ) !== false ) {
2019-11-13 19:34:37 +01:00
$keyList = 'main.' . $InfoFieldList [ 2 ] . ' as rowid' ;
2017-10-25 11:42:14 +02:00
} else {
2019-11-13 19:34:37 +01:00
$keyList = $InfoFieldList [ 2 ] . ' as rowid' ;
2017-10-25 11:42:14 +02:00
}
}
2015-03-14 14:55:41 +01:00
2017-10-25 11:42:14 +02:00
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
if ( is_array ( $fields_label )) {
$keyList .= ', ' ;
$keyList .= implode ( ', ' , $fields_label );
}
2015-03-14 14:55:41 +01:00
2017-10-25 11:42:14 +02:00
$sqlwhere = '' ;
2019-11-13 19:34:37 +01:00
$sql = 'SELECT ' . $keyList ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList [ 0 ];
if ( ! empty ( $InfoFieldList [ 4 ])) {
2017-10-25 11:42:14 +02:00
// can use SELECT request
2019-11-13 19:34:37 +01:00
if ( strpos ( $InfoFieldList [ 4 ], '$SEL$' ) !== false ) {
$InfoFieldList [ 4 ] = str_replace ( '$SEL$' , 'SELECT' , $InfoFieldList [ 4 ]);
2017-10-25 11:42:14 +02:00
}
2015-03-14 14:55:41 +01:00
2017-10-25 11:42:14 +02:00
// current object id can be use into filter
2019-11-13 19:34:37 +01:00
if ( strpos ( $InfoFieldList [ 4 ], '$ID$' ) !== false && ! empty ( $objectid )) {
$InfoFieldList [ 4 ] = str_replace ( '$ID$' , $objectid , $InfoFieldList [ 4 ]);
2017-10-25 11:42:14 +02:00
} else {
2019-11-13 19:34:37 +01:00
$InfoFieldList [ 4 ] = str_replace ( '$ID$' , '0' , $InfoFieldList [ 4 ]);
2017-10-25 11:42:14 +02:00
}
2014-09-23 19:37:59 +02:00
2017-10-25 11:42:14 +02:00
// We have to join on extrafield table
if ( strpos ( $InfoFieldList [ 4 ], 'extra' ) !== false ) {
2019-11-13 19:34:37 +01:00
$sql .= ' as main, ' . MAIN_DB_PREFIX . $InfoFieldList [ 0 ] . '_extrafields as extra' ;
$sqlwhere .= ' WHERE extra.fk_object=main.' . $InfoFieldList [ 2 ] . ' AND ' . $InfoFieldList [ 4 ];
2017-10-25 11:42:14 +02:00
} else {
2019-11-13 19:34:37 +01:00
$sqlwhere .= ' WHERE ' . $InfoFieldList [ 4 ];
2017-10-25 11:42:14 +02:00
}
} else {
$sqlwhere .= ' WHERE 1=1' ;
}
// Some tables may have field, some other not. For the moment we disable it.
2021-02-23 22:03:23 +01:00
if ( in_array ( $InfoFieldList [ 0 ], array ( 'tablewithentity' ))) {
2019-11-13 19:34:37 +01:00
$sqlwhere .= ' AND entity = ' . $conf -> entity ;
2017-10-25 11:42:14 +02:00
}
// $sql.=preg_replace('/^ AND /','',$sqlwhere);
// print $sql;
$sql .= $sqlwhere ;
2019-11-13 19:34:37 +01:00
dol_syslog ( get_class ( $this ) . '::showInputField type=chkbxlst' , LOG_DEBUG );
2017-10-25 11:42:14 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2019-11-13 19:34:37 +01:00
$data = array ();
2017-10-25 11:42:14 +02:00
2019-11-13 19:34:37 +01:00
while ( $i < $num ) {
2017-10-25 11:42:14 +02:00
$labeltoshow = '' ;
$obj = $this -> db -> fetch_object ( $resql );
2018-05-22 22:00:20 +02:00
$notrans = false ;
2017-10-25 11:42:14 +02:00
// Several field into label (eq table:code|libelle:rowid)
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
2020-06-01 21:02:05 +02:00
if ( count ( $fields_label ) > 1 ) {
2017-10-25 11:42:14 +02:00
$notrans = true ;
2019-02-02 18:25:01 +01:00
foreach ( $fields_label as $field_toshow ) {
2019-11-13 19:34:37 +01:00
$labeltoshow .= $obj -> $field_toshow . ' ' ;
2017-10-25 11:42:14 +02:00
}
} else {
$labeltoshow = $obj -> { $InfoFieldList [ 1 ]};
}
$labeltoshow = dol_trunc ( $labeltoshow , 45 );
if ( is_array ( $value_arr ) && in_array ( $obj -> rowid , $value_arr )) {
2019-02-02 18:25:01 +01:00
foreach ( $fields_label as $field_toshow ) {
2017-10-25 11:42:14 +02:00
$translabel = $langs -> trans ( $obj -> $field_toshow );
if ( $translabel != $obj -> $field_toshow ) {
2019-11-13 19:34:37 +01:00
$labeltoshow = dol_trunc ( $translabel , 18 ) . ' ' ;
2017-10-25 11:42:14 +02:00
} else {
2019-11-13 19:34:37 +01:00
$labeltoshow = dol_trunc ( $obj -> $field_toshow , 18 ) . ' ' ;
2017-10-25 11:42:14 +02:00
}
}
2014-09-23 19:37:59 +02:00
2019-11-13 19:34:37 +01:00
$data [ $obj -> rowid ] = $labeltoshow ;
2017-10-25 11:42:14 +02:00
} else {
2019-11-13 19:34:37 +01:00
if ( ! $notrans ) {
2017-10-25 11:42:14 +02:00
$translabel = $langs -> trans ( $obj -> { $InfoFieldList [ 1 ]});
if ( $translabel != $obj -> { $InfoFieldList [ 1 ]}) {
$labeltoshow = dol_trunc ( $translabel , 18 );
} else {
$labeltoshow = dol_trunc ( $obj -> { $InfoFieldList [ 1 ]}, 18 );
}
}
2019-10-27 11:18:36 +01:00
if ( empty ( $labeltoshow )) {
2017-10-25 11:42:14 +02:00
$labeltoshow = '(not defined)' ;
2019-10-27 11:18:36 +01:00
}
2014-09-23 19:37:59 +02:00
2019-10-27 11:18:36 +01:00
if ( is_array ( $value_arr ) && in_array ( $obj -> rowid , $value_arr )) {
2019-11-16 23:24:29 +01:00
$data [ $obj -> rowid ] = $labeltoshow ;
2019-10-27 11:18:36 +01:00
}
2016-06-30 17:28:13 +02:00
2019-11-16 23:24:29 +01:00
if ( ! empty ( $InfoFieldList [ 3 ]) && $parentField ) {
$parent = $parentName . ':' . $obj -> { $parentField };
2021-03-02 14:06:11 +01:00
$isDependList = 1 ;
2019-10-27 11:18:36 +01:00
}
2016-06-30 17:28:13 +02:00
2019-11-16 23:24:29 +01:00
$data [ $obj -> rowid ] = $labeltoshow ;
2017-10-25 11:42:14 +02:00
}
2019-11-16 23:24:29 +01:00
$i ++ ;
2017-10-25 11:42:14 +02:00
}
$this -> db -> free ( $resql );
2019-11-16 23:24:29 +01:00
$out = $form -> multiselectarray ( $keyprefix . $key . $keysuffix , $data , $value_arr , '' , 0 , '' , 0 , '100%' );
2017-10-25 11:42:14 +02:00
} else {
2019-11-16 23:24:29 +01:00
print 'Error in request ' . $sql . ' ' . $this -> db -> lasterror () . '. Check setup of extra parameters.<br>' ;
2017-10-25 11:42:14 +02:00
}
}
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'link' ) {
2019-11-16 23:24:29 +01:00
$param_list = array_keys ( $param [ 'options' ]); // $param_list='ObjectName:classPath[:AddCreateButtonOrNot[:Filter]]'
2019-10-21 14:44:39 +02:00
$param_list_array = explode ( ':' , $param_list [ 0 ]);
2019-11-16 23:24:29 +01:00
$showempty = (( $required && $default != '' ) ? 0 : 1 );
2020-01-21 14:17:53 +01:00
2020-12-14 20:40:47 +01:00
if ( ! preg_match ( '/search_/' , $keyprefix )) {
if ( ! empty ( $param_list_array [ 2 ])) { // If the entry into $fields is set to add a create button
2021-01-23 19:50:04 +01:00
if ( ! empty ( $this -> fields [ $key ][ 'picto' ])) {
2020-12-14 20:40:47 +01:00
$morecss .= ' widthcentpercentminusxx' ;
} else {
$morecss .= ' widthcentpercentminusx' ;
}
} else {
2021-01-23 19:50:04 +01:00
if ( ! empty ( $this -> fields [ $key ][ 'picto' ])) {
2020-12-14 20:40:47 +01:00
$morecss .= ' widthcentpercentminusx' ;
}
}
2018-12-14 11:01:27 +01:00
}
2019-10-21 14:44:39 +02:00
2020-01-22 14:10:46 +01:00
$out = $form -> selectForForms ( $param_list [ 0 ], $keyprefix . $key . $keysuffix , $value , $showempty , '' , '' , $morecss , $moreparam , 0 , empty ( $val [ 'disabled' ]) ? 0 : 1 );
2019-10-21 14:44:39 +02:00
2020-06-20 00:00:17 +02:00
if ( ! empty ( $param_list_array [ 2 ])) { // If the entry into $fields is set to add a create button
2021-02-23 22:03:23 +01:00
if ( ! GETPOSTISSET ( 'backtopage' ) && empty ( $val [ 'disabled' ]) && empty ( $nonewbutton )) { // To avoid to open several times the 'Create Object' button and to avoid to have button if field is protected by a "disabled".
list ( $class , $classfile ) = explode ( ':' , $param_list [ 0 ]);
if ( file_exists ( dol_buildpath ( dirname ( dirname ( $classfile )) . '/card.php' ))) {
$url_path = dol_buildpath ( dirname ( dirname ( $classfile )) . '/card.php' , 1 );
} else {
$url_path = dol_buildpath ( dirname ( dirname ( $classfile )) . '/' . strtolower ( $class ) . '_card.php' , 1 );
}
$paramforthenewlink = '' ;
$paramforthenewlink .= ( GETPOSTISSET ( 'action' ) ? '&action=' . GETPOST ( 'action' , 'aZ09' ) : '' );
$paramforthenewlink .= ( GETPOSTISSET ( 'id' ) ? '&id=' . GETPOST ( 'id' , 'int' ) : '' );
$paramforthenewlink .= '&fk_' . strtolower ( $class ) . '=--IDFORBACKTOPAGE--' ;
// TODO Add Javascript code to add input fields already filled into $paramforthenewlink so we won't loose them when going back to main page
$out .= '<a class="butActionNew" title="' . $langs -> trans ( " New " ) . '" href="' . $url_path . '?action=create&backtopage=' . urlencode ( $_SERVER [ 'PHP_SELF' ] . ( $paramforthenewlink ? '?' . $paramforthenewlink : '' )) . '"><span class="fa fa-plus-circle valignmiddle"></span></a>' ;
2019-10-21 14:44:39 +02:00
}
2018-12-14 11:01:27 +01:00
}
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'password' ) {
2017-10-25 11:42:14 +02:00
// If prefix is 'search_', field is used as a filter, we use a common text field.
2019-11-16 23:24:29 +01:00
$out = '<input type="' . ( $keyprefix == 'search_' ? 'text' : 'password' ) . '" class="flat ' . $morecss . '" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '" value="' . $value . '" ' . ( $moreparam ? $moreparam : '' ) . '>' ;
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'array' ) {
2018-04-18 18:02:38 +02:00
$newval = $val ;
$newval [ 'type' ] = 'varchar(256)' ;
2019-11-16 23:24:29 +01:00
$out = '' ;
if ( ! empty ( $value )) {
foreach ( $value as $option ) {
$out .= '<span><a class="' . dol_escape_htmltag ( $keyprefix . $key . $keysuffix ) . '_del" href="javascript:;"><span class="fa fa-minus-circle valignmiddle"></span></a> ' ;
$out .= $this -> showInputField ( $newval , $keyprefix . $key . $keysuffix . '[]' , $option , $moreparam , '' , '' , $morecss ) . '<br></span>' ;
2018-04-18 18:02:38 +02:00
}
}
2019-11-16 23:24:29 +01:00
$out .= '<a id="' . dol_escape_htmltag ( $keyprefix . $key . $keysuffix ) . '_add" href="javascript:;"><span class="fa fa-plus-circle valignmiddle"></span></a>' ;
2016-06-30 17:28:13 +02:00
2018-04-18 18:02:38 +02:00
$newInput = '<span><a class="' . dol_escape_htmltag ( $keyprefix . $key . $keysuffix ) . '_del" href="javascript:;"><span class="fa fa-minus-circle valignmiddle"></span></a> ' ;
2019-11-16 23:24:29 +01:00
$newInput .= $this -> showInputField ( $newval , $keyprefix . $key . $keysuffix . '[]' , '' , $moreparam , '' , '' , $morecss ) . '<br></span>' ;
2016-06-30 17:28:13 +02:00
2019-11-16 23:24:29 +01:00
if ( ! empty ( $conf -> use_javascript_ajax )) {
$out .= '
2019-01-20 23:36:39 +01:00
< script >
2018-04-20 16:43:27 +02:00
$ ( document ) . ready ( function () {
$ ( " a#'.dol_escape_js( $keyprefix . $key . $keysuffix ).'_add " ) . click ( function () {
$ ( " '.dol_escape_js( $newInput ).' " ) . insertBefore ( this );
});
2016-06-30 17:28:13 +02:00
2018-04-20 16:43:27 +02:00
$ ( document ) . on ( " click " , " a.'.dol_escape_js( $keyprefix . $key . $keysuffix ).'_del " , function () {
$ ( this ) . parent () . remove ();
});
2018-04-19 10:25:19 +02:00
});
2018-04-20 16:43:27 +02:00
</ script > ' ;
}
2018-04-18 18:02:38 +02:00
}
2017-10-25 11:42:14 +02:00
if ( ! empty ( $hidden )) {
2019-11-13 19:34:37 +01:00
$out = '<input type="hidden" value="' . $value . '" name="' . $keyprefix . $key . $keysuffix . '" id="' . $keyprefix . $key . $keysuffix . '"/>' ;
2017-10-25 11:42:14 +02:00
}
2021-03-02 14:06:11 +01:00
if ( $isDependList == 1 ) {
$out .= $this -> getJSListDependancies ( '_common' );
}
2017-10-25 11:42:14 +02:00
/* Add comments
if ( $type == 'date' ) $out .= ' (YYYY-MM-DD)' ;
elseif ( $type == 'datetime' ) $out .= ' (YYYY-MM-DD HH:MM:SS)' ;
*/
return $out ;
}
2014-09-23 19:37:59 +02:00
2017-10-26 02:55:43 +02:00
/**
* Return HTML string to show a field into a page
* Code very similar with showOutputField of extra fields
*
2017-11-03 15:56:16 +01:00
* @ param array $val Array of properties of field to show
2017-10-26 02:55:43 +02:00
* @ param string $key Key of attribute
* @ param string $value Preselected value to show ( for date type it must be in timestamp format , for amount or price it must be a php numeric value )
* @ param string $moreparam To add more parametes on html input tag
* @ param string $keysuffix Prefix string to add into name and id of field ( can be used to avoid duplicate names )
* @ param string $keyprefix Suffix string to add into name and id of field ( can be used to avoid duplicate names )
2019-03-17 01:45:46 +01:00
* @ param mixed $morecss Value for css to define size . May also be a numeric .
2017-10-26 02:55:43 +02:00
* @ return string
*/
2019-03-17 01:45:46 +01:00
public function showOutputField ( $val , $key , $value , $moreparam = '' , $keysuffix = '' , $keyprefix = '' , $morecss = '' )
2017-10-26 02:55:43 +02:00
{
2019-11-13 19:34:37 +01:00
global $conf , $langs , $form ;
2014-09-23 19:37:59 +02:00
2021-02-23 22:03:23 +01:00
if ( ! is_object ( $form )) {
2017-10-26 02:55:43 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/html.form.class.php' ;
2019-11-13 19:34:37 +01:00
$form = new Form ( $this -> db );
2017-10-26 02:55:43 +02:00
}
2021-06-14 01:55:58 +02:00
$objectid = $this -> id ; // Not used ???
$label = empty ( $val [ 'label' ]) ? '' : $val [ 'label' ];
$type = empty ( $val [ 'type' ]) ? '' : $val [ 'type' ];
$size = empty ( $val [ 'css' ]) ? '' : $val [ 'css' ];
2019-10-21 14:44:39 +02:00
$reg = array ();
2014-09-23 19:37:59 +02:00
2017-10-26 02:55:43 +02:00
// Convert var to be able to share same code than showOutputField of extrafields
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/varchar\((\d+)\)/' , $type , $reg )) {
2019-11-13 19:34:37 +01:00
$type = 'varchar' ; // convert varchar(xx) int varchar
2017-12-09 20:17:47 +01:00
$size = $reg [ 1 ];
2021-02-23 22:03:23 +01:00
} elseif ( preg_match ( '/varchar/' , $type )) {
$type = 'varchar' ; // convert varchar(xx) int varchar
}
if ( ! empty ( $val [ 'arrayofkeyval' ]) && is_array ( $val [ 'arrayofkeyval' ])) {
$type = 'select' ;
}
if ( preg_match ( '/^integer:(.*):(.*)/i' , $val [ 'type' ], $reg )) {
$type = 'link' ;
}
2017-10-26 02:55:43 +02:00
2021-06-14 01:55:58 +02:00
$default = empty ( $val [ 'default' ]) ? '' : $val [ 'default' ];
$computed = empty ( $val [ 'computed' ]) ? '' : $val [ 'computed' ];
$unique = empty ( $val [ 'unique' ]) ? '' : $val [ 'unique' ];
$required = empty ( $val [ 'required' ]) ? '' : $val [ 'required' ];
2019-11-13 19:34:37 +01:00
$param = array ();
$param [ 'options' ] = array ();
2019-10-21 14:44:39 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $val [ 'arrayofkeyval' ]) && is_array ( $val [ 'arrayofkeyval' ])) {
$param [ 'options' ] = $val [ 'arrayofkeyval' ];
}
2021-01-18 15:31:49 +01:00
if ( preg_match ( '/^integer:(.*):(.*)/i' , $val [ 'type' ], $reg )) {
2019-11-13 19:34:37 +01:00
$type = 'link' ;
$param [ 'options' ] = array ( $reg [ 1 ] . ':' . $reg [ 2 ] => $reg [ 1 ] . ':' . $reg [ 2 ]);
2020-05-21 15:05:19 +02:00
} elseif ( preg_match ( '/^sellist:(.*):(.*):(.*):(.*)/i' , $val [ 'type' ], $reg )) {
2020-09-07 10:18:17 +02:00
$param [ 'options' ] = array ( $reg [ 1 ] . ':' . $reg [ 2 ] . ':' . $reg [ 3 ] . ':' . $reg [ 4 ] => 'N' );
$type = 'sellist' ;
2021-01-18 15:31:49 +01:00
} elseif ( preg_match ( '/^sellist:(.*):(.*):(.*)/i' , $val [ 'type' ], $reg )) {
$param [ 'options' ] = array ( $reg [ 1 ] . ':' . $reg [ 2 ] . ':' . $reg [ 3 ] => 'N' );
$type = 'sellist' ;
} elseif ( preg_match ( '/^sellist:(.*):(.*)/i' , $val [ 'type' ], $reg )) {
$param [ 'options' ] = array ( $reg [ 1 ] . ':' . $reg [ 2 ] => 'N' );
$type = 'sellist' ;
2020-09-07 10:18:17 +02:00
}
2019-07-27 18:25:25 +02:00
2021-06-14 01:55:58 +02:00
$langfile = empty ( $val [ 'langfile' ]) ? '' : $val [ 'langfile' ];
$list = ( empty ( $val [ 'list' ]) ? '' : $val [ 'list' ]);
2021-06-14 01:33:01 +02:00
$help = ( empty ( $val [ 'help' ]) ? '' : $val [ 'help' ]);
2019-11-13 19:34:37 +01:00
$hidden = (( $val [ 'visible' ] == 0 ) ? 1 : 0 ); // If zero, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller)
2014-09-23 19:37:59 +02:00
2021-02-23 22:03:23 +01:00
if ( $hidden ) {
return '' ;
}
2014-09-23 19:37:59 +02:00
2017-10-26 02:55:43 +02:00
// If field is a computed field, value must become result of compute
2021-02-23 22:03:23 +01:00
if ( $computed ) {
2017-10-26 02:55:43 +02:00
// Make the eval of compute string
//var_dump($computed);
$value = dol_eval ( $computed , 1 , 0 );
}
2014-09-23 19:37:59 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $morecss )) {
2020-12-14 20:40:47 +01:00
if ( $type == 'date' ) {
2019-03-17 01:45:46 +01:00
$morecss = 'minwidth100imp' ;
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'datetime' || $type == 'timestamp' ) {
2019-03-17 01:45:46 +01:00
$morecss = 'minwidth200imp' ;
2020-12-14 20:40:47 +01:00
} elseif ( in_array ( $type , array ( 'int' , 'double' , 'price' ))) {
2019-03-17 01:45:46 +01:00
$morecss = 'maxwidth75' ;
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'url' ) {
2019-11-13 19:34:37 +01:00
$morecss = 'minwidth400' ;
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'boolean' ) {
2019-11-13 19:34:37 +01:00
$morecss = '' ;
2020-05-21 15:05:19 +02:00
} else {
2021-06-14 01:33:01 +02:00
if ( is_numeric ( $size ) && round ( $size ) < 12 ) {
2019-03-17 01:45:46 +01:00
$morecss = 'minwidth100' ;
2021-06-14 01:33:01 +02:00
} elseif ( is_numeric ( $size ) && round ( $size ) <= 48 ) {
2019-03-17 01:45:46 +01:00
$morecss = 'minwidth200' ;
2020-05-21 15:05:19 +02:00
} else {
2019-03-17 01:45:46 +01:00
$morecss = 'minwidth400' ;
2017-12-09 20:17:47 +01:00
}
2017-11-03 15:56:16 +01:00
}
}
2014-09-23 19:37:59 +02:00
2017-11-05 03:39:27 +01:00
// Format output value differently according to properties of field
2021-02-23 22:03:23 +01:00
if ( $key == 'ref' && method_exists ( $this , 'getNomUrl' )) {
$value = $this -> getNomUrl ( 1 , '' , 0 , '' , 1 );
} elseif ( $key == 'status' && method_exists ( $this , 'getLibStatut' )) {
$value = $this -> getLibStatut ( 3 );
} elseif ( $type == 'date' ) {
2019-11-13 19:34:37 +01:00
if ( ! empty ( $value )) {
2021-03-04 09:36:19 +01:00
$value = dol_print_date ( $value , 'day' ); // We suppose dates without time are always gmt (storage of course + output)
2018-04-18 11:13:18 +02:00
} else {
2019-11-13 19:34:37 +01:00
$value = '' ;
2018-04-18 11:13:18 +02:00
}
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'datetime' || $type == 'timestamp' ) {
2019-11-13 19:34:37 +01:00
if ( ! empty ( $value )) {
2021-03-04 09:36:19 +01:00
$value = dol_print_date ( $value , 'dayhour' , 'tzuserrel' );
2018-04-18 11:13:18 +02:00
} else {
2019-11-13 19:34:37 +01:00
$value = '' ;
2018-04-18 11:13:18 +02:00
}
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'duration' ) {
2019-11-17 17:36:36 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
2019-11-19 09:30:26 +01:00
if ( ! is_null ( $value ) && $value !== '' ) {
2019-11-17 17:36:36 +01:00
$value = convertSecondToTime ( $value , 'allhourmin' );
2017-10-26 02:55:43 +02:00
}
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'double' || $type == 'real' ) {
2019-11-19 09:30:26 +01:00
if ( ! is_null ( $value ) && $value !== '' ) {
2019-11-13 19:34:37 +01:00
$value = price ( $value );
2017-10-26 02:55:43 +02:00
}
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'boolean' ) {
2019-11-13 19:34:37 +01:00
$checked = '' ;
2017-10-26 02:55:43 +02:00
if ( ! empty ( $value )) {
2019-11-13 19:34:37 +01:00
$checked = ' checked ' ;
2017-10-26 02:55:43 +02:00
}
2019-11-13 19:34:37 +01:00
$value = '<input type="checkbox" ' . $checked . ' ' . ( $moreparam ? $moreparam : '' ) . ' readonly disabled>' ;
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'mail' ) {
2019-11-13 19:34:37 +01:00
$value = dol_print_email ( $value , 0 , 0 , 0 , 64 , 1 , 1 );
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'url' ) {
2019-11-13 19:34:37 +01:00
$value = dol_print_url ( $value , '_blank' , 32 , 1 );
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'phone' ) {
2019-11-13 19:34:37 +01:00
$value = dol_print_phone ( $value , '' , 0 , 0 , '' , ' ' , 1 );
2021-02-23 22:03:23 +01:00
} elseif ( $type == 'price' ) {
2019-11-19 09:30:26 +01:00
if ( ! is_null ( $value ) && $value !== '' ) {
2019-11-17 17:36:36 +01:00
$value = price ( $value , 0 , $langs , 0 , 0 , - 1 , $conf -> currency );
}
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'select' ) {
2019-11-13 19:34:37 +01:00
$value = $param [ 'options' ][ $value ];
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'sellist' ) {
2019-11-13 19:34:37 +01:00
$param_list = array_keys ( $param [ 'options' ]);
2017-10-26 02:55:43 +02:00
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
2014-09-23 19:37:59 +02:00
2019-11-13 19:34:37 +01:00
$selectkey = " rowid " ;
$keyList = 'rowid' ;
2015-08-29 19:42:51 +02:00
2021-01-18 15:31:49 +01:00
if ( count ( $InfoFieldList ) > 4 && ! empty ( $InfoFieldList [ 4 ])) {
2017-10-26 02:55:43 +02:00
$selectkey = $InfoFieldList [ 2 ];
2019-11-13 19:34:37 +01:00
$keyList = $InfoFieldList [ 2 ] . ' as rowid' ;
2017-10-26 02:55:43 +02:00
}
2014-09-23 19:37:59 +02:00
2019-01-27 11:55:16 +01:00
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
2019-11-13 19:34:37 +01:00
if ( is_array ( $fields_label )) {
$keyList .= ', ' ;
2017-10-26 02:55:43 +02:00
$keyList .= implode ( ', ' , $fields_label );
}
2017-07-13 15:16:17 +02:00
2017-10-26 02:55:43 +02:00
$sql = 'SELECT ' . $keyList ;
2019-11-13 19:34:37 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList [ 0 ];
2021-02-23 22:03:23 +01:00
if ( strpos ( $InfoFieldList [ 4 ], 'extra' ) !== false ) {
2019-11-13 19:34:37 +01:00
$sql .= ' as main' ;
2017-10-26 02:55:43 +02:00
}
2019-11-13 19:34:37 +01:00
if ( $selectkey == 'rowid' && empty ( $value )) {
2021-06-09 15:36:47 +02:00
$sql .= " WHERE " . $selectkey . " = 0 " ;
2019-11-13 19:34:37 +01:00
} elseif ( $selectkey == 'rowid' ) {
2021-06-09 15:36:47 +02:00
$sql .= " WHERE " . $selectkey . " = " . (( int ) $value );
2019-11-13 19:34:37 +01:00
} else {
2021-06-09 15:36:47 +02:00
$sql .= " WHERE " . $selectkey . " = ' " . $this -> db -> escape ( $value ) . " ' " ;
2017-10-26 02:55:43 +02:00
}
2014-09-23 19:37:59 +02:00
2017-10-26 02:55:43 +02:00
//$sql.= ' AND entity = '.$conf->entity;
2014-09-23 19:37:59 +02:00
2017-10-26 02:55:43 +02:00
dol_syslog ( get_class ( $this ) . ':showOutputField:$type=sellist' , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2019-11-13 19:34:37 +01:00
$value = '' ; // value was used, so now we reste it to use it to build final output
2014-09-23 19:37:59 +02:00
2017-10-26 02:55:43 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2017-07-12 16:45:32 +02:00
2017-10-26 02:55:43 +02:00
// Several field into label (eq table:code|libelle:rowid)
2019-01-27 11:55:16 +01:00
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
2017-10-26 02:55:43 +02:00
2021-02-23 22:03:23 +01:00
if ( is_array ( $fields_label ) && count ( $fields_label ) > 1 ) {
foreach ( $fields_label as $field_toshow ) {
2019-11-13 19:34:37 +01:00
$translabel = '' ;
2017-10-26 02:55:43 +02:00
if ( ! empty ( $obj -> $field_toshow )) {
2019-11-13 19:34:37 +01:00
$translabel = $langs -> trans ( $obj -> $field_toshow );
2017-10-26 02:55:43 +02:00
}
2019-11-13 19:34:37 +01:00
if ( $translabel != $field_toshow ) {
$value .= dol_trunc ( $translabel , 18 ) . ' ' ;
} else {
$value .= $obj -> $field_toshow . ' ' ;
2017-10-26 02:55:43 +02:00
}
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$translabel = '' ;
2017-10-26 02:55:43 +02:00
if ( ! empty ( $obj -> { $InfoFieldList [ 1 ]})) {
2019-11-13 19:34:37 +01:00
$translabel = $langs -> trans ( $obj -> { $InfoFieldList [ 1 ]});
2017-10-26 02:55:43 +02:00
}
2019-11-13 19:34:37 +01:00
if ( $translabel != $obj -> { $InfoFieldList [ 1 ]}) {
$value = dol_trunc ( $translabel , 18 );
} else {
$value = $obj -> { $InfoFieldList [ 1 ]};
2017-10-26 02:55:43 +02:00
}
}
2021-02-23 22:03:23 +01:00
} else {
dol_syslog ( get_class ( $this ) . '::showOutputField error ' . $this -> db -> lasterror (), LOG_WARNING );
}
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'radio' ) {
2019-11-13 19:34:37 +01:00
$value = $param [ 'options' ][ $value ];
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'checkbox' ) {
2019-11-13 19:34:37 +01:00
$value_arr = explode ( ',' , $value );
$value = '' ;
2021-02-23 22:03:23 +01:00
if ( is_array ( $value_arr ) && count ( $value_arr ) > 0 ) {
2019-11-13 19:34:37 +01:00
$toprint = array ();
2021-02-23 22:03:23 +01:00
foreach ( $value_arr as $keyval => $valueval ) {
2020-12-14 20:40:47 +01:00
$toprint [] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #bbb">' . $param [ 'options' ][ $valueval ] . '</li>' ;
2017-10-26 02:55:43 +02:00
}
2019-11-13 19:34:37 +01:00
$value = '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode ( ' ' , $toprint ) . '</ul></div>' ;
2017-10-26 02:55:43 +02:00
}
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'chkbxlst' ) {
2017-10-26 02:55:43 +02:00
$value_arr = explode ( ',' , $value );
2014-09-23 19:37:59 +02:00
2017-11-05 03:39:27 +01:00
$param_list = array_keys ( $param [ 'options' ]);
2017-10-26 02:55:43 +02:00
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
2014-09-23 19:37:59 +02:00
2017-10-26 02:55:43 +02:00
$selectkey = " rowid " ;
$keyList = 'rowid' ;
2014-09-23 19:37:59 +02:00
2017-10-26 02:55:43 +02:00
if ( count ( $InfoFieldList ) >= 3 ) {
$selectkey = $InfoFieldList [ 2 ];
2019-11-13 19:34:37 +01:00
$keyList = $InfoFieldList [ 2 ] . ' as rowid' ;
2017-10-26 02:55:43 +02:00
}
2017-06-07 16:44:04 +02:00
2017-10-26 02:55:43 +02:00
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
if ( is_array ( $fields_label )) {
$keyList .= ', ' ;
$keyList .= implode ( ', ' , $fields_label );
}
2014-09-23 19:37:59 +02:00
2019-11-13 19:34:37 +01:00
$sql = 'SELECT ' . $keyList ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList [ 0 ];
2017-10-26 02:55:43 +02:00
if ( strpos ( $InfoFieldList [ 4 ], 'extra' ) !== false ) {
$sql .= ' as main' ;
}
// $sql.= " WHERE ".$selectkey."='".$this->db->escape($value)."'";
// $sql.= ' AND entity = '.$conf->entity;
2017-07-13 15:16:17 +02:00
2019-11-13 19:34:37 +01:00
dol_syslog ( get_class ( $this ) . ':showOutputField:$type=chkbxlst' , LOG_DEBUG );
2017-10-26 02:55:43 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$value = '' ; // value was used, so now we reste it to use it to build final output
2019-11-13 19:34:37 +01:00
$toprint = array ();
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2017-10-26 02:55:43 +02:00
// Several field into label (eq table:code|libelle:rowid)
$fields_label = explode ( '|' , $InfoFieldList [ 1 ]);
if ( is_array ( $value_arr ) && in_array ( $obj -> rowid , $value_arr )) {
if ( is_array ( $fields_label ) && count ( $fields_label ) > 1 ) {
2019-02-02 18:25:01 +01:00
foreach ( $fields_label as $field_toshow ) {
2017-10-26 02:55:43 +02:00
$translabel = '' ;
2019-11-13 19:34:37 +01:00
if ( ! empty ( $obj -> $field_toshow )) {
2017-10-26 02:55:43 +02:00
$translabel = $langs -> trans ( $obj -> $field_toshow );
}
if ( $translabel != $field_toshow ) {
2020-12-14 20:40:47 +01:00
$toprint [] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #bbb">' . dol_trunc ( $translabel , 18 ) . '</li>' ;
2017-10-26 02:55:43 +02:00
} else {
2020-12-14 20:40:47 +01:00
$toprint [] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #bbb">' . $obj -> $field_toshow . '</li>' ;
2017-10-26 02:55:43 +02:00
}
}
} else {
$translabel = '' ;
2019-11-13 19:34:37 +01:00
if ( ! empty ( $obj -> { $InfoFieldList [ 1 ]})) {
2017-10-26 02:55:43 +02:00
$translabel = $langs -> trans ( $obj -> { $InfoFieldList [ 1 ]});
}
if ( $translabel != $obj -> { $InfoFieldList [ 1 ]}) {
2020-12-14 20:40:47 +01:00
$toprint [] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #bbb">' . dol_trunc ( $translabel , 18 ) . '</li>' ;
2017-10-26 02:55:43 +02:00
} else {
2020-12-14 20:40:47 +01:00
$toprint [] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #bbb">' . $obj -> { $InfoFieldList [ 1 ]} . '</li>' ;
2017-10-26 02:55:43 +02:00
}
}
}
}
2019-11-13 19:34:37 +01:00
$value = '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode ( ' ' , $toprint ) . '</ul></div>' ;
2017-10-26 02:55:43 +02:00
} else {
2019-11-13 19:34:37 +01:00
dol_syslog ( get_class ( $this ) . '::showOutputField error ' . $this -> db -> lasterror (), LOG_WARNING );
2017-10-26 02:55:43 +02:00
}
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'link' ) {
2019-11-13 19:34:37 +01:00
$out = '' ;
2014-09-23 19:37:59 +02:00
2017-10-26 02:55:43 +02:00
// only if something to display (perf)
2021-02-23 22:03:23 +01:00
if ( $value ) {
2019-11-13 19:34:37 +01:00
$param_list = array_keys ( $param [ 'options' ]); // $param_list='ObjectName:classPath'
2016-06-30 17:28:13 +02:00
2017-10-26 02:55:43 +02:00
$InfoFieldList = explode ( " : " , $param_list [ 0 ]);
2019-11-13 19:34:37 +01:00
$classname = $InfoFieldList [ 0 ];
$classpath = $InfoFieldList [ 1 ];
$getnomurlparam = ( empty ( $InfoFieldList [ 2 ]) ? 3 : $InfoFieldList [ 2 ]);
2021-03-15 20:20:53 +01:00
$getnomurlparam2 = ( empty ( $InfoFieldList [ 4 ]) ? '' : $InfoFieldList [ 4 ]);
2021-02-23 22:03:23 +01:00
if ( ! empty ( $classpath )) {
2017-10-26 02:55:43 +02:00
dol_include_once ( $InfoFieldList [ 1 ]);
2021-02-23 22:03:23 +01:00
if ( $classname && class_exists ( $classname )) {
2017-10-26 02:55:43 +02:00
$object = new $classname ( $this -> db );
2021-09-20 15:59:39 +02:00
if ( $object -> element === 'product' ) { // Special cas for product because default valut of fetch are wrong
$object -> fetch ( $value , '' , '' , '' , 0 , 1 , 1 );
} else {
$object -> fetch ( $value );
}
2021-03-15 20:20:53 +01:00
$value = $object -> getNomUrl ( $getnomurlparam , $getnomurlparam2 );
2017-10-26 02:55:43 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-26 02:55:43 +02:00
dol_syslog ( 'Error bad setup of extrafield' , LOG_WARNING );
return 'Error bad setup of extrafield' ;
}
2021-02-23 22:03:23 +01:00
} else {
$value = '' ;
}
2020-12-14 20:40:47 +01:00
} elseif ( preg_match ( '/^(text|html)/' , $type )) {
2019-11-13 19:34:37 +01:00
$value = dol_htmlentitiesbr ( $value );
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'password' ) {
2019-11-13 19:34:37 +01:00
$value = preg_replace ( '/./i' , '*' , $value );
2020-12-14 20:40:47 +01:00
} elseif ( $type == 'array' ) {
2018-04-19 10:25:19 +02:00
$value = implode ( '<br>' , $value );
2018-04-18 18:02:38 +02:00
}
2016-04-16 12:19:46 +02:00
2020-09-11 16:16:04 +02:00
//print $type.'-'.$size.'-'.$value;
2019-11-13 19:34:37 +01:00
$out = $value ;
2016-04-16 12:19:46 +02:00
2017-10-26 02:55:43 +02:00
return $out ;
}
2016-04-16 12:19:46 +02:00
2016-06-30 17:28:13 +02:00
2017-10-16 21:05:12 +02:00
/**
2019-11-22 14:22:35 +01:00
* Function to show lines of extrafields with output datas .
2021-02-14 15:00:20 +01:00
* This function is responsible to output the < tr > and < td > according to correct number of columns received into $params [ 'colspan' ] or < div > according to $display_type
2017-10-16 21:05:12 +02:00
*
2018-03-20 10:10:21 +01:00
* @ param Extrafields $extrafields Extrafield Object
2020-04-05 03:14:12 +02:00
* @ param string $mode Show output ( 'view' ) or input ( 'create' or 'edit' ) for extrafield
2018-09-01 14:24:02 +02:00
* @ param array $params Optional parameters . Example : array ( 'style' => 'class="oddeven"' , 'colspan' => $colspan )
2018-03-20 10:10:21 +01:00
* @ param string $keysuffix Suffix string to add after name and id of field ( can be used to avoid duplicate names )
* @ param string $keyprefix Prefix string to add before name and id of field ( can be used to avoid duplicate names )
2020-12-14 20:40:47 +01:00
* @ param string $onetrtd All fields in same tr td . Used by objectline_create . tpl . php for example .
2021-01-04 20:00:57 +01:00
* @ param string $display_type " card " for form display , " line " for document line display ( extrafields on propal line , order line , etc ... )
2018-03-20 10:10:21 +01:00
* @ return string
*/
2021-01-04 20:00:57 +01:00
public function showOptionals ( $extrafields , $mode = 'view' , $params = null , $keysuffix = '' , $keyprefix = '' , $onetrtd = 0 , $display_type = 'card' )
2017-10-07 13:09:31 +02:00
{
2020-07-08 15:20:59 +02:00
global $db , $conf , $langs , $action , $form , $hookmanager ;
2014-09-23 19:37:59 +02:00
2021-02-23 22:03:23 +01:00
if ( ! is_object ( $form )) {
$form = new Form ( $db );
}
2014-09-23 19:37:59 +02:00
$out = '' ;
2020-09-07 10:18:17 +02:00
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'showOptionals' , $parameters , $this , $action ); // Note that $action and $object may have been modified by hook
2021-02-23 22:03:23 +01:00
if ( empty ( $reshook )) {
2021-05-18 20:13:26 +02:00
if ( key_exists ( 'label' , $extrafields -> attributes [ $this -> table_element ]) && is_array ( $extrafields -> attributes [ $this -> table_element ][ 'label' ]) && count ( $extrafields -> attributes [ $this -> table_element ][ 'label' ]) > 0 ) {
2020-07-08 15:25:37 +02:00
$out .= " \n " ;
2020-08-03 14:44:01 +02:00
$out .= '<!-- showOptionals --> ' ;
2020-07-08 15:25:37 +02:00
$out .= " \n " ;
2018-09-02 11:08:41 +02:00
2020-09-07 10:18:17 +02:00
$extrafields_collapse_num = '' ;
2020-07-08 15:25:37 +02:00
$e = 0 ;
2021-02-23 22:03:23 +01:00
foreach ( $extrafields -> attributes [ $this -> table_element ][ 'label' ] as $key => $label ) {
2020-07-08 15:25:37 +02:00
// Show only the key field in params
2021-02-23 22:03:23 +01:00
if ( is_array ( $params ) && array_key_exists ( 'onlykey' , $params ) && $key != $params [ 'onlykey' ]) {
continue ;
}
2018-04-12 23:16:23 +02:00
2020-09-26 04:25:06 +02:00
// Test on 'enabled' ('enabled' is different than 'list' = 'visibility')
2020-07-08 15:25:37 +02:00
$enabled = 1 ;
2021-02-23 22:03:23 +01:00
if ( $enabled && isset ( $extrafields -> attributes [ $this -> table_element ][ 'enabled' ][ $key ])) {
2020-09-07 10:18:17 +02:00
$enabled = dol_eval ( $extrafields -> attributes [ $this -> table_element ][ 'enabled' ][ $key ], 1 );
}
2021-02-23 22:03:23 +01:00
if ( empty ( $enabled )) {
continue ;
}
2018-04-10 15:39:11 +02:00
2020-07-08 15:25:37 +02:00
$visibility = 1 ;
2021-02-23 22:03:23 +01:00
if ( $visibility && isset ( $extrafields -> attributes [ $this -> table_element ][ 'list' ][ $key ])) {
2020-09-07 10:18:17 +02:00
$visibility = dol_eval ( $extrafields -> attributes [ $this -> table_element ][ 'list' ][ $key ], 1 );
2020-07-08 15:25:37 +02:00
}
2017-10-25 11:42:14 +02:00
2020-07-08 15:25:37 +02:00
$perms = 1 ;
2021-02-23 22:03:23 +01:00
if ( $perms && isset ( $extrafields -> attributes [ $this -> table_element ][ 'perms' ][ $key ])) {
2020-07-08 15:25:37 +02:00
$perms = dol_eval ( $extrafields -> attributes [ $this -> table_element ][ 'perms' ][ $key ], 1 );
}
2017-08-27 12:06:46 +02:00
2021-02-23 22:03:23 +01:00
if (( $mode == 'create' ) && abs ( $visibility ) != 1 && abs ( $visibility ) != 3 ) {
continue ; // <> -1 and <> 1 and <> 3 = not visible on forms, only on list
} elseif (( $mode == 'edit' ) && abs ( $visibility ) != 1 && abs ( $visibility ) != 3 && abs ( $visibility ) != 4 ) {
continue ; // <> -1 and <> 1 and <> 3 = not visible on forms, only on list and <> 4 = not visible at the creation
} elseif ( $mode == 'view' && empty ( $visibility )) {
continue ;
}
if ( empty ( $perms )) {
continue ;
}
2020-07-08 15:25:37 +02:00
// Load language if required
if ( ! empty ( $extrafields -> attributes [ $this -> table_element ][ 'langfile' ][ $key ])) {
$langs -> load ( $extrafields -> attributes [ $this -> table_element ][ 'langfile' ][ $key ]);
2014-09-23 19:37:59 +02:00
}
2017-09-30 20:05:34 +02:00
2020-07-08 15:25:37 +02:00
$colspan = '' ;
2021-01-04 20:00:57 +01:00
if ( is_array ( $params ) && count ( $params ) > 0 && $display_type == 'card' ) {
2020-07-08 15:25:37 +02:00
if ( array_key_exists ( 'cols' , $params )) {
$colspan = $params [ 'cols' ];
} elseif ( array_key_exists ( 'colspan' , $params )) { // For backward compatibility. Use cols instead now.
$reg = array ();
if ( preg_match ( '/colspan="(\d+)"/' , $params [ 'colspan' ], $reg )) {
$colspan = $reg [ 1 ];
2015-10-05 20:39:14 +02:00
} else {
2020-07-08 15:25:37 +02:00
$colspan = $params [ 'colspan' ];
2015-10-05 20:39:14 +02:00
}
}
2014-09-23 19:37:59 +02:00
}
2017-09-30 20:05:34 +02:00
2020-07-08 15:25:37 +02:00
switch ( $mode ) {
case " view " :
2020-12-14 20:40:47 +01:00
$value = $this -> array_options [ " options_ " . $key . $keysuffix ]; // Value may be clean or formated later
2020-07-08 15:25:37 +02:00
break ;
case " create " :
2020-09-07 10:18:17 +02:00
case " edit " :
2020-09-26 04:25:06 +02:00
// We get the value of property found with GETPOST so it takes into account:
// default values overwrite, restore back to list link, ... (but not 'default value in database' of field)
$check = 'alphanohtml' ;
if ( in_array ( $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $key ], array ( 'html' , 'text' ))) {
$check = 'restricthtml' ;
}
$getposttemp = GETPOST ( $keyprefix . 'options_' . $key . $keysuffix , $check , 3 ); // GETPOST can get value from GET, POST or setup of default values overwrite.
2020-09-07 10:18:17 +02:00
// GETPOST("options_" . $key) can be 'abc' or array(0=>'abc')
2021-02-23 22:03:23 +01:00
if ( is_array ( $getposttemp ) || $getposttemp != '' || GETPOSTISSET ( $keyprefix . 'options_' . $key . $keysuffix )) {
2020-07-08 15:25:37 +02:00
if ( is_array ( $getposttemp )) {
// $getposttemp is an array but following code expects a comma separated string
$value = implode ( " , " , $getposttemp );
} else {
$value = $getposttemp ;
}
2015-10-05 20:39:14 +02:00
} else {
2021-02-23 22:03:23 +01:00
$value = ( ! empty ( $this -> array_options [ " options_ " . $key ]) ? $this -> array_options [ " options_ " . $key ] : '' ); // No GET, no POST, no default value, so we take value of object.
2015-10-05 20:39:14 +02:00
}
2020-07-08 15:25:37 +02:00
//var_dump($keyprefix.' - '.$key.' - '.$keysuffix.' - '.$keyprefix.'options_'.$key.$keysuffix.' - '.$this->array_options["options_".$key.$keysuffix].' - '.$getposttemp.' - '.$value);
break ;
2014-09-23 19:37:59 +02:00
}
2018-03-20 10:10:21 +01:00
2021-01-04 16:07:54 +01:00
// Output value of the current field
2021-02-23 22:03:23 +01:00
if ( $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $key ] == 'separate' ) {
2020-09-07 10:18:17 +02:00
$extrafields_collapse_num = '' ;
$extrafield_param = $extrafields -> attributes [ $this -> table_element ][ 'param' ][ $key ];
if ( ! empty ( $extrafield_param ) && is_array ( $extrafield_param )) {
$extrafield_param_list = array_keys ( $extrafield_param [ 'options' ]);
2019-05-03 16:14:44 +02:00
2020-09-07 10:18:17 +02:00
if ( count ( $extrafield_param_list ) > 0 ) {
$extrafield_collapse_display_value = intval ( $extrafield_param_list [ 0 ]);
2019-05-03 16:14:44 +02:00
2020-09-07 10:18:17 +02:00
if ( $extrafield_collapse_display_value == 1 || $extrafield_collapse_display_value == 2 ) {
$extrafields_collapse_num = $extrafields -> attributes [ $this -> table_element ][ 'pos' ][ $key ];
}
}
}
2018-04-09 14:25:02 +02:00
2021-01-04 20:00:57 +01:00
$out .= $extrafields -> showSeparator ( $key , $this , ( $colspan + 1 ), $display_type );
2020-07-08 15:25:37 +02:00
} else {
$class = ( ! empty ( $extrafields -> attributes [ $this -> table_element ][ 'hidden' ][ $key ]) ? 'hideobject ' : '' );
$csstyle = '' ;
if ( is_array ( $params ) && count ( $params ) > 0 ) {
if ( array_key_exists ( 'class' , $params )) {
$class .= $params [ 'class' ] . ' ' ;
}
if ( array_key_exists ( 'style' , $params )) {
$csstyle = $params [ 'style' ];
}
2014-09-23 19:37:59 +02:00
}
2018-04-09 14:25:02 +02:00
2020-07-08 15:25:37 +02:00
// add html5 elements
$domData = ' data-element="extrafield"' ;
$domData .= ' data-targetelement="' . $this -> element . '"' ;
$domData .= ' data-targetid="' . $this -> id . '"' ;
2018-04-09 14:25:02 +02:00
2020-07-08 15:25:37 +02:00
$html_id = ( empty ( $this -> id ) ? '' : 'extrarow-' . $this -> element . '_' . $key . '_' . $this -> id );
2021-01-04 20:00:57 +01:00
if ( $display_type == 'card' ) {
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_USE_TWO_COLUMS ) && ( $e % 2 ) == 0 ) {
$colspan = '0' ;
}
2018-03-20 10:10:21 +01:00
2021-02-23 22:03:23 +01:00
if ( $action == 'selectlines' ) {
$colspan ++ ;
}
2021-01-04 20:00:57 +01:00
}
2018-04-03 11:38:12 +02:00
2020-07-08 15:25:37 +02:00
// Convert date into timestamp format (value in memory must be a timestamp)
2021-03-05 19:29:09 +01:00
if ( in_array ( $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $key ], array ( 'date' ))) {
2020-07-08 15:25:37 +02:00
$datenotinstring = $this -> array_options [ 'options_' . $key ];
2021-02-23 22:03:23 +01:00
if ( ! is_numeric ( $this -> array_options [ 'options_' . $key ])) { // For backward compatibility
2020-07-08 15:25:37 +02:00
$datenotinstring = $this -> db -> jdate ( $datenotinstring );
}
2021-03-04 10:31:31 +01:00
$value = ( GETPOSTISSET ( $keyprefix . 'options_' . $key . $keysuffix )) ? dol_mktime ( 12 , 0 , 0 , GETPOST ( $keyprefix . 'options_' . $key . $keysuffix . " month " , 'int' , 3 ), GETPOST ( $keyprefix . 'options_' . $key . $keysuffix . " day " , 'int' , 3 ), GETPOST ( $keyprefix . 'options_' . $key . $keysuffix . " year " , 'int' , 3 )) : $datenotinstring ;
}
2021-03-06 11:37:23 +01:00
if ( in_array ( $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $key ], array ( 'datetime' ))) {
2020-07-08 15:25:37 +02:00
$datenotinstring = $this -> array_options [ 'options_' . $key ];
2021-03-06 11:37:23 +01:00
if ( ! is_numeric ( $this -> array_options [ 'options_' . $key ])) { // For backward compatibility
2020-07-08 15:25:37 +02:00
$datenotinstring = $this -> db -> jdate ( $datenotinstring );
}
2021-03-04 10:31:31 +01:00
$value = ( GETPOSTISSET ( $keyprefix . 'options_' . $key . $keysuffix )) ? dol_mktime ( GETPOST ( $keyprefix . 'options_' . $key . $keysuffix . " hour " , 'int' , 3 ), GETPOST ( $keyprefix . 'options_' . $key . $keysuffix . " min " , 'int' , 3 ), GETPOST ( $keyprefix . 'options_' . $key . $keysuffix . " sec " , 'int' , 3 ), GETPOST ( $keyprefix . 'options_' . $key . $keysuffix . " month " , 'int' , 3 ), GETPOST ( $keyprefix . 'options_' . $key . $keysuffix . " day " , 'int' , 3 ), GETPOST ( $keyprefix . 'options_' . $key . $keysuffix . " year " , 'int' , 3 ), 'tzuserrel' ) : $datenotinstring ;
2020-07-08 15:25:37 +02:00
}
// Convert float submited string into real php numeric (value in memory must be a php numeric)
2021-02-23 22:03:23 +01:00
if ( in_array ( $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $key ], array ( 'price' , 'double' ))) {
2020-09-26 04:25:06 +02:00
$value = ( GETPOSTISSET ( $keyprefix . 'options_' . $key . $keysuffix ) || $value ) ? price2num ( $value ) : $this -> array_options [ 'options_' . $key ];
2018-05-21 16:55:35 +02:00
}
2014-09-23 19:37:59 +02:00
2020-09-26 04:25:06 +02:00
// HTML, text, select, integer and varchar: take into account default value in database if in create mode
2021-02-23 22:03:23 +01:00
if ( in_array ( $extrafields -> attributes [ $this -> table_element ][ 'type' ][ $key ], array ( 'html' , 'text' , 'varchar' , 'select' , 'int' ))) {
if ( $action == 'create' ) {
$value = ( GETPOSTISSET ( $keyprefix . 'options_' . $key . $keysuffix ) || $value ) ? $value : $extrafields -> attributes [ $this -> table_element ][ 'default' ][ $key ];
}
2018-05-29 10:13:34 +02:00
}
2017-10-04 16:08:22 +02:00
2020-07-08 15:25:37 +02:00
$labeltoshow = $langs -> trans ( $label );
$helptoshow = $langs -> trans ( $extrafields -> attributes [ $this -> table_element ][ 'help' ][ $key ]);
2014-09-23 19:37:59 +02:00
2021-02-01 15:40:59 +01:00
if ( $display_type == 'card' ) {
2021-02-27 12:45:07 +01:00
$out .= '<tr ' . ( $html_id ? 'id="' . $html_id . '" ' : '' ) . $csstyle . ' class="valuefieldcreate ' . $class . $this -> element . '_extras_' . $key . ' trextrafields_collapse' . $extrafields_collapse_num . ( ! empty ( $this -> id ) ? '_' . $this -> id : '' ) . '" ' . $domData . ' >' ;
2021-11-19 11:12:53 +01:00
if ( ! empty ( $conf -> global -> MAIN_VIEW_LINE_NUMBER ) && ( $action == 'view' || $action == 'valid' || $action == 'editline' )) {
2021-07-07 10:30:01 +02:00
$out .= '<td></td>' ;
}
2021-01-04 20:00:57 +01:00
$out .= '<td class="wordbreak' ;
2021-02-01 15:40:59 +01:00
} elseif ( $display_type == 'line' ) {
2021-02-27 12:45:07 +01:00
$out .= '<div ' . ( $html_id ? 'id="' . $html_id . '" ' : '' ) . $csstyle . ' class="valuefieldlinecreate ' . $class . $this -> element . '_extras_' . $key . ' trextrafields_collapse' . $extrafields_collapse_num . ( ! empty ( $this -> id ) ? '_' . $this -> id : '' ) . '" ' . $domData . ' >' ;
2021-01-04 20:00:57 +01:00
$out .= '<div style="display: inline-block; padding-right:4px" class="wordbreak' ;
2020-12-14 20:40:47 +01:00
}
2020-07-08 15:25:37 +02:00
//$out .= "titlefield";
2020-09-18 01:29:17 +02:00
//if (GETPOST('action', 'restricthtml') == 'create') $out.='create';
2020-07-08 15:25:37 +02:00
// BUG #11554 : For public page, use red dot for required fields, instead of bold label
$tpl_context = isset ( $params [ " tpl_context " ]) ? $params [ " tpl_context " ] : " none " ;
if ( $tpl_context == " public " ) { // Public page : red dot instead of fieldrequired characters
$out .= '">' ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $extrafields -> attributes [ $this -> table_element ][ 'help' ][ $key ])) {
$out .= $form -> textwithpicto ( $labeltoshow , $helptoshow );
} else {
$out .= $labeltoshow ;
}
if ( $mode != 'view' && ! empty ( $extrafields -> attributes [ $this -> table_element ][ 'required' ][ $key ])) {
$out .= ' <font color="red">*</font>' ;
}
2020-07-08 15:25:37 +02:00
} else {
2021-02-23 22:03:23 +01:00
if ( $mode != 'view' && ! empty ( $extrafields -> attributes [ $this -> table_element ][ 'required' ][ $key ])) {
$out .= ' fieldrequired' ;
}
2020-07-08 15:25:37 +02:00
$out .= '">' ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $extrafields -> attributes [ $this -> table_element ][ 'help' ][ $key ])) {
$out .= $form -> textwithpicto ( $labeltoshow , $helptoshow );
} else {
$out .= $labeltoshow ;
}
2020-07-08 15:25:37 +02:00
}
2019-09-06 12:44:23 +02:00
2021-02-01 15:40:59 +01:00
$out .= ( $display_type == 'card' ? '</td>' : '</div>' );
2014-09-23 19:37:59 +02:00
2021-01-04 20:00:57 +01:00
$html_id = ! empty ( $this -> id ) ? $this -> element . '_extras_' . $key . '_' . $this -> id : '' ;
2021-02-01 15:40:59 +01:00
if ( $display_type == 'card' ) {
2021-01-04 20:00:57 +01:00
$out .= '<td ' . ( $html_id ? 'id="' . $html_id . '" ' : '' ) . ' class="' . $this -> element . '_extras_' . $key . '" ' . ( $colspan ? ' colspan="' . $colspan . '"' : '' ) . '>' ;
2021-02-01 15:40:59 +01:00
} elseif ( $display_type == 'line' ) {
2021-01-04 20:00:57 +01:00
$out .= '<div ' . ( $html_id ? 'id="' . $html_id . '" ' : '' ) . ' style="display: inline-block" class="' . $this -> element . '_extras_' . $key . '">' ;
}
2014-09-23 19:37:59 +02:00
2020-07-08 15:25:37 +02:00
switch ( $mode ) {
case " view " :
$out .= $extrafields -> showOutputField ( $key , $value );
break ;
2020-09-07 10:18:17 +02:00
case " create " :
2020-09-26 04:25:06 +02:00
$out .= $extrafields -> showInputField ( $key , $value , '' , $keysuffix , '' , 0 , $this -> id , $this -> table_element );
break ;
2020-09-07 10:18:17 +02:00
case " edit " :
2020-07-08 15:25:37 +02:00
$out .= $extrafields -> showInputField ( $key , $value , '' , $keysuffix , '' , 0 , $this -> id , $this -> table_element );
break ;
}
2014-09-23 19:37:59 +02:00
2021-01-04 20:00:57 +01:00
$out .= ( $display_type == 'card' ? '</td>' : '</div>' );
2014-09-23 19:37:59 +02:00
2020-07-08 15:25:37 +02:00
/* for ( $ii = 0 ; $ii < ( $colspan - 1 ); $ii ++ )
{
$out .= '<td class="' . $this -> element . '_extras_' . $key . '"></td>' ;
} */
2019-09-06 12:44:23 +02:00
2021-01-04 20:00:57 +01:00
if ( ! empty ( $conf -> global -> MAIN_EXTRAFIELDS_USE_TWO_COLUMS ) && (( $e % 2 ) == 1 )) {
$out .= ( $display_type == 'card' ? '</tr>' : '</div>' );
2021-01-05 15:25:47 +01:00
} else {
2021-01-04 20:00:57 +01:00
$out .= ( $display_type == 'card' ? '</tr>' : '</div>' );
}
2020-07-08 15:25:37 +02:00
$e ++ ;
}
2014-09-23 19:37:59 +02:00
}
2020-07-08 15:25:37 +02:00
$out .= " \n " ;
// Add code to manage list depending on others
if ( ! empty ( $conf -> use_javascript_ajax )) {
2021-03-02 17:21:55 +01:00
$out .= $this -> getJSListDependancies ();
2021-03-02 14:06:11 +01:00
}
2014-09-23 19:37:59 +02:00
2021-03-02 14:06:11 +01:00
$out .= '<!-- /showOptionals --> ' . " \n " ;
}
}
2014-09-23 19:37:59 +02:00
2021-03-02 14:06:11 +01:00
$out .= $hookmanager -> resPrint ;
2014-09-23 19:37:59 +02:00
2021-03-02 14:06:11 +01:00
return $out ;
}
2019-09-06 12:44:23 +02:00
2021-03-02 14:06:11 +01:00
/**
2021-03-02 14:47:35 +01:00
* @ param string $type Type for prefix
* @ return string Javacript code to manage dependency
2021-03-02 14:06:11 +01:00
*/
2021-03-02 15:09:29 +01:00
public function getJSListDependancies ( $type = '_extra' )
{
2021-03-03 12:24:44 +01:00
$out = '
2020-07-08 15:25:37 +02:00
< script >
2020-12-14 20:40:47 +01:00
jQuery ( document ) . ready ( function () {
2021-03-02 14:06:11 +01:00
function showOptions '.$type.' ( child_list , parent_list , orig_select )
2020-12-14 20:40:47 +01:00
{
var val = $ ( " select[name= \" " + parent_list + " \" ] " ) . val ();
var parentVal = parent_list + " : " + val ;
2021-01-26 09:37:28 +01:00
if ( typeof val == " string " ){
if ( val != " " ) {
var options = orig_select . find ( " option[parent= \" " + parentVal + " \" ] " ) . clone ();
$ ( " select[name= \" " + child_list + " \" ] option[parent] " ) . remove ();
$ ( " select[name= \" " + child_list + " \" ] " ) . append ( options );
} else {
var options = orig_select . find ( " option[parent] " ) . clone ();
$ ( " select[name= \" " + child_list + " \" ] option[parent] " ) . remove ();
$ ( " select[name= \" " + child_list + " \" ] " ) . append ( options );
}
} else if ( val > 0 ) {
2020-12-14 20:40:47 +01:00
var options = orig_select . find ( " option[parent= \" " + parentVal + " \" ] " ) . clone ();
$ ( " select[name= \" " + child_list + " \" ] option[parent] " ) . remove ();
$ ( " select[name= \" " + child_list + " \" ] " ) . append ( options );
2014-09-23 19:37:59 +02:00
} else {
2020-12-14 20:40:47 +01:00
var options = orig_select . find ( " option[parent] " ) . clone ();
$ ( " select[name= \" " + child_list + " \" ] option[parent] " ) . remove ();
$ ( " select[name= \" " + child_list + " \" ] " ) . append ( options );
2014-09-23 19:37:59 +02:00
}
2020-12-14 20:40:47 +01:00
}
2021-03-02 14:06:11 +01:00
function setListDependencies '.$type.' () {
2020-12-14 20:40:47 +01:00
jQuery ( " select option[parent] " ) . parent () . each ( function () {
var orig_select = {};
var child_list = $ ( this ) . attr ( " name " );
orig_select [ child_list ] = $ ( this ) . clone ();
2014-09-23 19:37:59 +02:00
var parent = $ ( this ) . find ( " option[parent]:first " ) . attr ( " parent " );
var infos = parent . split ( " : " );
var parent_list = infos [ 0 ];
2021-01-21 13:20:34 +01:00
2021-01-20 18:39:18 +01:00
//Hide daughters lists
if ( $ ( " # " + child_list ) . val () == 0 && $ ( " # " + parent_list ) . val () == 0 ){
$ ( " # " + child_list ) . hide ();
//Show mother lists
} else if ( $ ( " # " + parent_list ) . val () != 0 ){
$ ( " # " + parent_list ) . show ();
}
2021-01-21 13:20:34 +01:00
//Show the child list if the parent list value is selected
2021-01-20 18:39:18 +01:00
$ ( " select[name= \" " + parent_list + " \" ] " ) . click ( function () {
if ( $ ( this ) . val () != 0 ){
$ ( " # " + child_list ) . show ()
}
});
2021-01-25 12:39:44 +01:00
2021-01-21 13:21:22 +01:00
//When we change parent list
2017-01-27 15:16:28 +01:00
$ ( " select[name= \" " + parent_list + " \" ] " ) . change ( function () {
2021-03-02 14:06:11 +01:00
showOptions '.$type.' ( child_list , parent_list , orig_select [ child_list ]);
2021-01-21 13:20:34 +01:00
//Select the value 0 on child list after a change on the parent list
2021-01-20 18:39:18 +01:00
$ ( " # " + child_list ) . val ( 0 ) . trigger ( " change " );
//Hide child lists if the parent value is set to 0
if ( $ ( this ) . val () == 0 ){
$ ( " # " + child_list ) . hide ();
}
2014-09-23 19:37:59 +02:00
});
2020-12-14 20:40:47 +01:00
});
2014-09-23 19:37:59 +02:00
}
2021-03-02 14:06:11 +01:00
setListDependencies '.$type.' ();
2020-12-14 20:40:47 +01:00
});
2020-07-08 15:25:37 +02:00
</ script > ' . " \n " ;
2014-09-23 19:37:59 +02:00
return $out ;
}
2015-03-23 01:39:12 +01:00
/**
* Returns the rights used for this class
* @ return stdClass
*/
public function getRights ()
{
global $user ;
2016-04-24 18:55:19 +02:00
$element = $this -> element ;
2021-02-23 22:03:23 +01:00
if ( $element == 'facturerec' ) {
$element = 'facture' ;
}
2016-04-24 18:55:19 +02:00
return $user -> rights -> { $element };
2015-03-23 01:39:12 +01:00
}
2015-04-18 18:11:01 +02:00
/**
* Function used to replace a thirdparty id with another one .
* This function is meant to be called from replaceThirdparty with the appropiate tables
* Column name fk_soc MUST be used to identify thirdparties
*
2016-06-20 15:42:16 +02:00
* @ param DoliDB $db Database handler
* @ param int $origin_id Old thirdparty id ( the thirdparty to delete )
* @ param int $dest_id New thirdparty id ( the thirdparty that will received element of the other )
* @ param string [] $tables Tables that need to be changed
2016-06-30 17:28:13 +02:00
* @ param int $ignoreerrors Ignore errors . Return true even if errors . We need this when replacement can fails like for categories ( categorie of old thirdparty may already exists on new one )
2018-01-31 18:01:56 +01:00
* @ return bool True if success , False if error
2015-04-18 18:11:01 +02:00
*/
2019-01-27 15:20:16 +01:00
public static function commonReplaceThirdparty ( DoliDB $db , $origin_id , $dest_id , array $tables , $ignoreerrors = 0 )
2015-04-18 18:11:01 +02:00
{
2021-02-23 22:03:23 +01:00
foreach ( $tables as $table ) {
2021-03-30 11:36:50 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $table . ' SET fk_soc = ' . (( int ) $dest_id ) . ' WHERE fk_soc = ' . (( int ) $origin_id );
2015-03-23 01:39:12 +01:00
2021-02-23 22:03:23 +01:00
if ( ! $db -> query ( $sql )) {
if ( $ignoreerrors ) {
return true ; // TODO Not enough. If there is A-B on kept thirdarty and B-C on old one, we must get A-B-C after merge. Not A-B.
}
2016-06-20 15:42:16 +02:00
//$this->errors = $db->lasterror();
2017-10-07 13:09:31 +02:00
return false ;
2015-04-18 18:11:01 +02:00
}
}
2015-03-23 01:39:12 +01:00
2015-04-18 18:11:01 +02:00
return true ;
}
2016-06-30 17:28:13 +02:00
2016-01-13 12:48:59 +01:00
/**
* Get buy price to use for margin calculation . This function is called when buy price is unknown .
2017-07-13 23:46:01 +02:00
* Set buy price = sell price if ForceBuyingPriceIfNull configured ,
2016-01-13 12:48:59 +01:00
* else if calculation MARGIN_TYPE = 'costprice' and costprice is defined , use costprice as buyprice
* else if calculation MARGIN_TYPE = 'pmp' and pmp is calculated , use pmp as buyprice
2015-10-26 20:33:42 +01:00
* else set min buy price as buy price
2016-06-30 17:28:13 +02:00
*
2017-07-13 23:46:01 +02:00
* @ param float $unitPrice Product unit price
* @ param float $discountPercent Line discount percent
* @ param int $fk_product Product id
* @ return float < 0 if KO , buyprice if OK
2015-10-26 20:33:42 +01:00
*/
2017-07-13 23:46:01 +02:00
public function defineBuyPrice ( $unitPrice = 0.0 , $discountPercent = 0.0 , $fk_product = 0 )
2015-10-26 20:33:42 +01:00
{
global $conf ;
2016-06-30 17:28:13 +02:00
2015-10-26 20:33:42 +01:00
$buyPrice = 0 ;
2016-06-30 17:28:13 +02:00
2021-03-24 21:58:06 +01:00
if (( $unitPrice > 0 ) && ( isset ( $conf -> global -> ForceBuyingPriceIfNull ) && $conf -> global -> ForceBuyingPriceIfNull > 0 )) {
// When ForceBuyingPriceIfNull is set
2015-10-26 20:33:42 +01:00
$buyPrice = $unitPrice * ( 1 - $discountPercent / 100 );
2020-05-21 15:05:19 +02:00
} else {
2016-01-13 12:48:59 +01:00
// Get cost price for margin calculation
2021-07-04 21:02:02 +02:00
if ( ! empty ( $fk_product ) && $fk_product > 0 ) {
2021-02-23 22:03:23 +01:00
if ( isset ( $conf -> global -> MARGIN_TYPE ) && $conf -> global -> MARGIN_TYPE == 'costprice' ) {
2016-01-13 12:48:59 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
$product = new Product ( $this -> db );
$result = $product -> fetch ( $fk_product );
2021-02-23 22:03:23 +01:00
if ( $result <= 0 ) {
2016-01-13 12:48:59 +01:00
$this -> errors [] = 'ErrorProductIdDoesNotExists' ;
return - 1 ;
}
2021-02-23 22:03:23 +01:00
if ( $product -> cost_price > 0 ) {
2016-01-13 12:48:59 +01:00
$buyPrice = $product -> cost_price ;
2021-02-23 22:03:23 +01:00
} elseif ( $product -> pmp > 0 ) {
2016-01-13 12:48:59 +01:00
$buyPrice = $product -> pmp ;
}
2021-02-23 22:03:23 +01:00
} elseif ( isset ( $conf -> global -> MARGIN_TYPE ) && $conf -> global -> MARGIN_TYPE == 'pmp' ) {
2015-10-27 15:50:55 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
2015-10-26 20:33:42 +01:00
$product = new Product ( $this -> db );
$result = $product -> fetch ( $fk_product );
2021-02-23 22:03:23 +01:00
if ( $result <= 0 ) {
2015-11-21 12:35:45 +01:00
$this -> errors [] = 'ErrorProductIdDoesNotExists' ;
2015-10-26 20:33:42 +01:00
return - 1 ;
}
2021-02-23 22:03:23 +01:00
if ( $product -> pmp > 0 ) {
2015-10-26 20:33:42 +01:00
$buyPrice = $product -> pmp ;
}
}
2016-06-30 17:28:13 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $buyPrice ) && isset ( $conf -> global -> MARGIN_TYPE ) && in_array ( $conf -> global -> MARGIN_TYPE , array ( '1' , 'pmp' , 'costprice' ))) {
2015-10-27 15:50:55 +01:00
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php' ;
2015-10-26 20:33:42 +01:00
$productFournisseur = new ProductFournisseur ( $this -> db );
2021-02-23 22:03:23 +01:00
if (( $result = $productFournisseur -> find_min_price_product_fournisseur ( $fk_product )) > 0 ) {
2016-04-27 16:42:59 +02:00
$buyPrice = $productFournisseur -> fourn_unitprice ;
2021-02-23 22:03:23 +01:00
} elseif ( $result < 0 ) {
2015-11-21 12:35:45 +01:00
$this -> errors [] = $productFournisseur -> error ;
return - 2 ;
2015-10-26 20:33:42 +01:00
}
}
}
}
return $buyPrice ;
}
2017-06-07 16:44:04 +02:00
2020-09-07 10:18:17 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-05-02 15:19:55 +02:00
/**
2018-03-22 11:46:35 +01:00
* Show photos of an object ( nbmax maximum ), into several columns
2017-05-02 15:19:55 +02:00
*
2018-06-04 21:49:29 +02:00
* @ param string $modulepart 'product' , 'ticket' , ...
2018-03-22 11:46:35 +01:00
* @ param string $sdir Directory to scan ( full absolute path )
* @ param int $size 0 = original size , 1 = 'small' use thumbnail if possible
* @ param int $nbmax Nombre maximum de photos ( 0 = pas de max )
* @ param int $nbbyrow Number of image per line or - 1 to use div . Used only if size = 1.
* @ param int $showfilename 1 = Show filename
* @ param int $showaction 1 = Show icon with action links ( resize , delete )
* @ param int $maxHeight Max height of original image when size = 'small' ( so we can use original even if small requested ) . If 0 , always use 'small' thumb image .
* @ param int $maxWidth Max width of original image when size = 'small'
* @ param int $nolink Do not add a href link to view enlarged imaged into a new tab
* @ param int $notitle Do not add title tag on image
* @ param int $usesharelink Use the public shared link of image ( if not available , the 'nophoto' image will be shown instead )
* @ return string Html code to show photo . Number of photos shown is saved in this -> nbphoto
2017-05-02 15:19:55 +02:00
*/
2019-02-25 22:27:04 +01:00
public function show_photos ( $modulepart , $sdir , $size = 0 , $nbmax = 0 , $nbbyrow = 5 , $showfilename = 0 , $showaction = 0 , $maxHeight = 120 , $maxWidth = 160 , $nolink = 0 , $notitle = 0 , $usesharelink = 0 )
2017-05-02 15:19:55 +02:00
{
2020-09-07 10:18:17 +02:00
// phpcs:enable
2019-11-13 19:34:37 +01:00
global $conf , $user , $langs ;
2018-03-22 11:46:35 +01:00
2019-11-13 19:34:37 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
include_once DOL_DOCUMENT_ROOT . '/core/lib/images.lib.php' ;
2018-03-22 11:46:35 +01:00
2019-11-13 19:34:37 +01:00
$sortfield = 'position_name' ;
$sortorder = 'asc' ;
2018-03-22 11:46:35 +01:00
2019-11-13 19:34:37 +01:00
$dir = $sdir . '/' ;
2018-03-22 11:46:35 +01:00
$pdir = '/' ;
2019-09-26 14:28:34 +02:00
2020-12-17 11:57:12 +01:00
$dir .= get_exdir ( 0 , 0 , 0 , 0 , $this , $modulepart );
$pdir .= get_exdir ( 0 , 0 , 0 , 0 , $this , $modulepart );
2018-03-22 11:46:35 +01:00
// For backward compatibility
2020-12-17 11:57:12 +01:00
if ( $modulepart == 'product' ) {
if ( ! empty ( $conf -> global -> PRODUCT_USE_OLD_PATH_FOR_PHOTO )) {
$dir = $sdir . '/' . get_exdir ( $this -> id , 2 , 0 , 0 , $this , $modulepart ) . $this -> id . " /photos/ " ;
$pdir = '/' . get_exdir ( $this -> id , 2 , 0 , 0 , $this , $modulepart ) . $this -> id . " /photos/ " ;
}
2018-03-22 11:46:35 +01:00
}
// Defined relative dir to DOL_DATA_ROOT
$relativedir = '' ;
2020-12-17 11:57:12 +01:00
if ( $dir ) {
2019-01-27 11:55:16 +01:00
$relativedir = preg_replace ( '/^' . preg_quote ( DOL_DATA_ROOT , '/' ) . '/' , '' , $dir );
$relativedir = preg_replace ( '/^[\\/]/' , '' , $relativedir );
$relativedir = preg_replace ( '/[\\/]$/' , '' , $relativedir );
2018-03-22 11:46:35 +01:00
}
$dirthumb = $dir . 'thumbs/' ;
$pdirthumb = $pdir . 'thumbs/' ;
2019-11-13 19:34:37 +01:00
$return = '<!-- Photo -->' . " \n " ;
$nbphoto = 0 ;
2018-03-22 11:46:35 +01:00
2019-11-13 19:34:37 +01:00
$filearray = dol_dir_list ( $dir , " files " , 0 , '' , '(\.meta|_preview.*\.png)$' , $sortfield , ( strtolower ( $sortorder ) == 'desc' ? SORT_DESC : SORT_ASC ), 1 );
2018-03-22 11:46:35 +01:00
/* if ( ! empty ( $conf -> global -> PRODUCT_USE_OLD_PATH_FOR_PHOTO )) // For backward compatiblity, we scan also old dirs
{
$filearrayold = dol_dir_list ( $dirold , " files " , 0 , '' , '(\.meta|_preview.*\.png)$' , $sortfield ,( strtolower ( $sortorder ) == 'desc' ? SORT_DESC : SORT_ASC ), 1 );
$filearray = array_merge ( $filearray , $filearrayold );
} */
completeFileArrayWithDatabaseInfo ( $filearray , $relativedir );
2020-12-17 11:57:12 +01:00
if ( count ( $filearray )) {
if ( $sortfield && $sortorder ) {
2019-11-13 19:34:37 +01:00
$filearray = dol_sort_array ( $filearray , $sortfield , $sortorder );
2018-03-22 11:46:35 +01:00
}
2020-12-17 11:57:12 +01:00
foreach ( $filearray as $key => $val ) {
2019-11-13 19:34:37 +01:00
$photo = '' ;
2018-03-22 11:46:35 +01:00
$file = $val [ 'name' ];
//if (! utf8_check($file)) $file=utf8_encode($file); // To be sure file is stored in UTF8 in memory
//if (dol_is_file($dir.$file) && image_format_supported($file) >= 0)
2020-12-17 11:57:12 +01:00
if ( image_format_supported ( $file ) >= 0 ) {
2018-03-22 11:46:35 +01:00
$nbphoto ++ ;
$photo = $file ;
$viewfilename = $file ;
if ( $size == 1 || $size == 'small' ) { // Format vignette
// Find name of thumb file
2019-11-13 19:34:37 +01:00
$photo_vignette = basename ( getImageFileNameForSize ( $dir . $file , '_small' ));
2021-02-23 22:03:23 +01:00
if ( ! dol_is_file ( $dirthumb . $photo_vignette )) {
$photo_vignette = '' ;
}
2018-03-22 11:46:35 +01:00
// Get filesize of original file
2019-11-13 19:34:37 +01:00
$imgarray = dol_getImageSize ( $dir . $photo );
2018-03-22 11:46:35 +01:00
2021-02-23 22:03:23 +01:00
if ( $nbbyrow > 0 ) {
if ( $nbphoto == 1 ) {
$return .= '<table class="valigntop center centpercent" style="border: 0; padding: 2px; border-spacing: 2px; border-collapse: separate;">' ;
}
2018-03-22 11:46:35 +01:00
2021-02-23 22:03:23 +01:00
if ( $nbphoto % $nbbyrow == 1 ) {
$return .= '<tr class="center valignmiddle" style="border: 1px">' ;
}
2019-11-13 19:34:37 +01:00
$return .= '<td style="width: ' . ceil ( 100 / $nbbyrow ) . '%" class="photo">' ;
2021-02-23 22:03:23 +01:00
} elseif ( $nbbyrow < 0 ) {
$return .= '<div class="inline-block">' ;
}
2018-03-22 11:46:35 +01:00
2019-11-13 19:34:37 +01:00
$return .= " \n " ;
2018-03-22 11:46:35 +01:00
2019-11-13 19:34:37 +01:00
$relativefile = preg_replace ( '/^\//' , '' , $pdir . $photo );
2021-02-23 22:03:23 +01:00
if ( empty ( $nolink )) {
2019-11-13 19:34:37 +01:00
$urladvanced = getAdvancedPreviewUrl ( $modulepart , $relativefile , 0 , 'entity=' . $this -> entity );
2021-02-23 22:03:23 +01:00
if ( $urladvanced ) {
$return .= '<a href="' . $urladvanced . '">' ;
} else {
$return .= '<a href="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $modulepart . '&entity=' . $this -> entity . '&file=' . urlencode ( $pdir . $photo ) . '" class="aphoto" target="_blank">' ;
}
2018-03-22 11:46:35 +01:00
}
// Show image (width height=$maxHeight)
// Si fichier vignette disponible et image source trop grande, on utilise la vignette, sinon on utilise photo origine
2019-11-13 19:34:37 +01:00
$alt = $langs -> transnoentitiesnoconv ( 'File' ) . ': ' . $relativefile ;
$alt .= ' - ' . $langs -> transnoentitiesnoconv ( 'Size' ) . ': ' . $imgarray [ 'width' ] . 'x' . $imgarray [ 'height' ];
2021-02-23 22:03:23 +01:00
if ( $notitle ) {
$alt = '' ;
}
2018-03-22 11:46:35 +01:00
2020-12-17 11:57:12 +01:00
if ( $usesharelink ) {
if ( $val [ 'share' ]) {
if ( empty ( $maxHeight ) || $photo_vignette && $imgarray [ 'height' ] > $maxHeight ) {
2019-11-13 19:34:37 +01:00
$return .= '<!-- Show original file (thumb not yet available with shared links) -->' ;
$return .= '<img class="photo photowithmargin" height="' . $maxHeight . '" src="' . DOL_URL_ROOT . '/viewimage.php?hashp=' . urlencode ( $val [ 'share' ]) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$return .= '<!-- Show original file -->' ;
$return .= '<img class="photo photowithmargin" height="' . $maxHeight . '" src="' . DOL_URL_ROOT . '/viewimage.php?hashp=' . urlencode ( $val [ 'share' ]) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2018-03-22 11:46:35 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$return .= '<!-- Show nophoto file (because file is not shared) -->' ;
$return .= '<img class="photo photowithmargin" height="' . $maxHeight . '" src="' . DOL_URL_ROOT . '/public/theme/common/nophoto.png" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2018-03-22 11:46:35 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2020-12-17 11:57:12 +01:00
if ( empty ( $maxHeight ) || $photo_vignette && $imgarray [ 'height' ] > $maxHeight ) {
2019-11-13 19:34:37 +01:00
$return .= '<!-- Show thumb -->' ;
2020-12-17 11:57:12 +01:00
$return .= '<img class="photo photowithmargin maxwidth150onsmartphone" height="' . $maxHeight . '" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $modulepart . '&entity=' . $this -> entity . '&file=' . urlencode ( $pdirthumb . $photo_vignette ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$return .= '<!-- Show original file -->' ;
$return .= '<img class="photo photowithmargin" height="' . $maxHeight . '" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $modulepart . '&entity=' . $this -> entity . '&file=' . urlencode ( $pdir . $photo ) . '" title="' . dol_escape_htmltag ( $alt ) . '">' ;
2018-03-22 11:46:35 +01:00
}
}
2021-02-23 22:03:23 +01:00
if ( empty ( $nolink )) {
$return .= '</a>' ;
}
2019-11-13 19:34:37 +01:00
$return .= " \n " ;
2018-03-22 11:46:35 +01:00
2021-02-23 22:03:23 +01:00
if ( $showfilename ) {
$return .= '<br>' . $viewfilename ;
}
if ( $showaction ) {
2019-11-13 19:34:37 +01:00
$return .= '<br>' ;
2018-03-22 11:46:35 +01:00
// On propose la generation de la vignette si elle n'existe pas et si la taille est superieure aux limites
2021-02-23 22:03:23 +01:00
if ( $photo_vignette && ( image_format_supported ( $photo ) > 0 ) && ( $this -> imgWidth > $maxWidth || $this -> imgHeight > $maxHeight )) {
2019-11-13 19:34:37 +01:00
$return .= '<a href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $this -> id . '&action=addthumb&file=' . urlencode ( $pdir . $viewfilename ) . '">' . img_picto ( $langs -> trans ( 'GenerateThumb' ), 'refresh' ) . ' </a>' ;
2018-03-22 11:46:35 +01:00
}
// Special cas for product
2021-02-23 22:03:23 +01:00
if ( $modulepart == 'product' && ( $user -> rights -> produit -> creer || $user -> rights -> service -> creer )) {
2018-03-22 11:46:35 +01:00
// Link to resize
2019-11-13 19:34:37 +01:00
$return .= '<a href="' . DOL_URL_ROOT . '/core/photos_resize.php?modulepart=' . urlencode ( 'produit|service' ) . '&id=' . $this -> id . '&file=' . urlencode ( $pdir . $viewfilename ) . '" title="' . dol_escape_htmltag ( $langs -> trans ( " Resize " )) . '">' . img_picto ( $langs -> trans ( " Resize " ), 'resize' , '' ) . '</a> ' ;
2018-03-22 11:46:35 +01:00
// Link to delete
2020-10-01 10:50:54 +02:00
$return .= '<a href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $this -> id . '&action=delete&token=' . newToken () . '&file=' . urlencode ( $pdir . $viewfilename ) . '">' ;
2019-11-13 19:34:37 +01:00
$return .= img_delete () . '</a>' ;
2018-03-22 11:46:35 +01:00
}
}
2019-11-13 19:34:37 +01:00
$return .= " \n " ;
2018-03-22 11:46:35 +01:00
2021-02-23 22:03:23 +01:00
if ( $nbbyrow > 0 ) {
2019-11-13 19:34:37 +01:00
$return .= '</td>' ;
2021-02-23 22:03:23 +01:00
if (( $nbphoto % $nbbyrow ) == 0 ) {
$return .= '</tr>' ;
}
} elseif ( $nbbyrow < 0 ) {
$return .= '</div>' ;
}
2018-03-22 11:46:35 +01:00
}
if ( empty ( $size )) { // Format origine
2019-11-13 19:34:37 +01:00
$return .= '<img class="photo photowithmargin" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $modulepart . '&entity=' . $this -> entity . '&file=' . urlencode ( $pdir . $photo ) . '">' ;
2018-03-22 11:46:35 +01:00
2021-02-23 22:03:23 +01:00
if ( $showfilename ) {
$return .= '<br>' . $viewfilename ;
}
if ( $showaction ) {
2018-03-22 11:46:35 +01:00
// Special case for product
2021-02-23 22:03:23 +01:00
if ( $modulepart == 'product' && ( $user -> rights -> produit -> creer || $user -> rights -> service -> creer )) {
2018-03-22 11:46:35 +01:00
// Link to resize
2019-11-13 19:34:37 +01:00
$return .= '<a href="' . DOL_URL_ROOT . '/core/photos_resize.php?modulepart=' . urlencode ( 'produit|service' ) . '&id=' . $this -> id . '&file=' . urlencode ( $pdir . $viewfilename ) . '" title="' . dol_escape_htmltag ( $langs -> trans ( " Resize " )) . '">' . img_picto ( $langs -> trans ( " Resize " ), 'resize' , '' ) . '</a> ' ;
2018-03-22 11:46:35 +01:00
// Link to delete
2020-10-01 10:50:54 +02:00
$return .= '<a href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $this -> id . '&action=delete&token=' . newToken () . '&file=' . urlencode ( $pdir . $viewfilename ) . '">' ;
2019-11-13 19:34:37 +01:00
$return .= img_delete () . '</a>' ;
2018-03-22 11:46:35 +01:00
}
}
}
// On continue ou on arrete de boucler ?
2021-02-23 22:03:23 +01:00
if ( $nbmax && $nbphoto >= $nbmax ) {
break ;
}
2018-03-22 11:46:35 +01:00
}
}
2021-02-23 22:03:23 +01:00
if ( $size == 1 || $size == 'small' ) {
if ( $nbbyrow > 0 ) {
2018-03-22 11:46:35 +01:00
// Ferme tableau
2021-02-23 22:03:23 +01:00
while ( $nbphoto % $nbbyrow ) {
2019-11-13 19:34:37 +01:00
$return .= '<td style="width: ' . ceil ( 100 / $nbbyrow ) . '%"> </td>' ;
2018-03-22 11:46:35 +01:00
$nbphoto ++ ;
}
2021-02-23 22:03:23 +01:00
if ( $nbphoto ) {
$return .= '</table>' ;
}
2018-03-22 11:46:35 +01:00
}
}
}
$this -> nbphoto = $nbphoto ;
return $return ;
2017-05-02 15:19:55 +02:00
}
2017-06-07 16:44:04 +02:00
2017-07-12 13:25:18 +02:00
2017-05-02 15:19:55 +02:00
/**
* Function test if type is array
*
* @ param array $info content informations of field
2019-10-14 11:38:36 +02:00
* @ return bool true if array
2017-05-02 15:19:55 +02:00
*/
2017-05-02 23:11:05 +02:00
protected function isArray ( $info )
2017-05-02 15:19:55 +02:00
{
2020-12-14 20:40:47 +01:00
if ( is_array ( $info )) {
2021-02-23 22:03:23 +01:00
if ( isset ( $info [ 'type' ]) && $info [ 'type' ] == 'array' ) {
return true ;
} else {
return false ;
}
2017-05-02 15:19:55 +02:00
}
2019-10-14 11:38:36 +02:00
return false ;
2017-05-02 15:19:55 +02:00
}
2017-06-07 16:44:04 +02:00
2017-05-02 15:19:55 +02:00
/**
2017-07-25 01:54:54 +02:00
* Function test if type is date
2017-05-02 15:19:55 +02:00
*
* @ param array $info content informations of field
2019-10-14 11:38:36 +02:00
* @ return bool true if date
2017-05-02 15:19:55 +02:00
*/
2017-07-25 01:54:54 +02:00
public function isDate ( $info )
2017-05-02 15:19:55 +02:00
{
2021-02-23 22:03:23 +01:00
if ( isset ( $info [ 'type' ]) && ( $info [ 'type' ] == 'date' || $info [ 'type' ] == 'datetime' || $info [ 'type' ] == 'timestamp' )) {
return true ;
}
2019-10-14 11:38:36 +02:00
return false ;
2017-05-02 15:19:55 +02:00
}
2017-06-07 16:44:04 +02:00
2017-07-25 01:54:54 +02:00
/**
2019-11-17 17:36:36 +01:00
* Function test if type is duration
2017-07-25 01:54:54 +02:00
*
* @ param array $info content informations of field
2019-11-17 17:36:36 +01:00
* @ return bool true if field of type duration
2017-07-25 01:54:54 +02:00
*/
2019-11-17 17:36:36 +01:00
public function isDuration ( $info )
2017-07-25 01:54:54 +02:00
{
2020-12-14 20:40:47 +01:00
if ( is_array ( $info )) {
2021-02-23 22:03:23 +01:00
if ( isset ( $info [ 'type' ]) && ( $info [ 'type' ] == 'duration' )) {
return true ;
} else {
return false ;
}
} else {
return false ;
}
2017-07-25 01:54:54 +02:00
}
2017-05-02 15:19:55 +02:00
/**
* Function test if type is integer
*
* @ param array $info content informations of field
2019-10-14 11:38:36 +02:00
* @ return bool true if integer
2017-05-02 15:19:55 +02:00
*/
2017-07-25 01:54:54 +02:00
public function isInt ( $info )
2017-05-02 15:19:55 +02:00
{
2020-12-14 20:40:47 +01:00
if ( is_array ( $info )) {
2021-02-23 22:03:23 +01:00
if ( isset ( $info [ 'type' ]) && ( $info [ 'type' ] == 'int' || preg_match ( '/^integer/i' , $info [ 'type' ]))) {
return true ;
} else {
return false ;
}
} else {
return false ;
}
2017-05-02 15:19:55 +02:00
}
2017-06-07 16:44:04 +02:00
2017-05-02 15:19:55 +02:00
/**
* Function test if type is float
*
* @ param array $info content informations of field
2019-10-14 11:38:36 +02:00
* @ return bool true if float
2017-05-02 15:19:55 +02:00
*/
2017-07-25 01:54:54 +02:00
public function isFloat ( $info )
2017-05-02 15:19:55 +02:00
{
2020-12-14 20:40:47 +01:00
if ( is_array ( $info )) {
2021-02-23 22:03:23 +01:00
if ( isset ( $info [ 'type' ]) && ( preg_match ( '/^(double|real|price)/i' , $info [ 'type' ]))) {
return true ;
} else {
return false ;
}
2017-05-02 15:19:55 +02:00
}
2019-10-14 11:38:36 +02:00
return false ;
2017-05-02 15:19:55 +02:00
}
2017-06-07 16:44:04 +02:00
2017-05-02 15:19:55 +02:00
/**
* Function test if type is text
*
* @ param array $info content informations of field
2019-10-14 11:38:36 +02:00
* @ return bool true if type text
2017-05-02 15:19:55 +02:00
*/
2017-07-25 01:54:54 +02:00
public function isText ( $info )
2017-05-02 15:19:55 +02:00
{
2020-12-14 20:40:47 +01:00
if ( is_array ( $info )) {
2021-02-23 22:03:23 +01:00
if ( isset ( $info [ 'type' ]) && $info [ 'type' ] == 'text' ) {
return true ;
} else {
return false ;
}
2017-05-02 15:19:55 +02:00
}
2019-10-14 11:38:36 +02:00
return false ;
}
/**
* Function test if field can be null
*
* @ param array $info content informations of field
* @ return bool true if it can be null
*/
protected function canBeNull ( $info )
{
2020-12-14 20:40:47 +01:00
if ( is_array ( $info )) {
2021-02-23 22:03:23 +01:00
if ( isset ( $info [ 'notnull' ]) && $info [ 'notnull' ] != '1' ) {
return true ;
} else {
return false ;
}
2019-10-14 11:38:36 +02:00
}
return true ;
}
/**
* Function test if field is forced to null if zero or empty
*
* @ param array $info content informations of field
* @ return bool true if forced to null
*/
protected function isForcedToNullIfZero ( $info )
{
2020-12-14 20:40:47 +01:00
if ( is_array ( $info )) {
2021-02-23 22:03:23 +01:00
if ( isset ( $info [ 'notnull' ]) && $info [ 'notnull' ] == '-1' ) {
return true ;
} else {
return false ;
}
2017-05-02 15:19:55 +02:00
}
2019-10-14 11:38:36 +02:00
return false ;
2017-05-02 15:19:55 +02:00
}
2017-06-07 16:44:04 +02:00
2017-05-02 15:19:55 +02:00
/**
* Function test if is indexed
*
* @ param array $info content informations of field
* @ return bool
*/
2017-05-02 23:11:05 +02:00
protected function isIndex ( $info )
2017-05-02 15:19:55 +02:00
{
2020-12-14 20:40:47 +01:00
if ( is_array ( $info )) {
2021-02-23 22:03:23 +01:00
if ( isset ( $info [ 'index' ]) && $info [ 'index' ] == true ) {
return true ;
} else {
return false ;
}
2017-05-02 15:19:55 +02:00
}
2019-10-14 11:38:36 +02:00
return false ;
2017-05-02 15:19:55 +02:00
}
2017-06-07 16:44:04 +02:00
2019-11-09 12:46:45 +01:00
2017-05-02 15:19:55 +02:00
/**
2019-11-09 12:46:45 +01:00
* Function to prepare a part of the query for insert .
2017-07-24 13:55:12 +02:00
* Note $this -> $ { field } are set by the page that make the createCommon or the updateCommon .
2019-11-09 12:46:45 +01:00
* $this -> $ { field } should be a clean value . The page can run
2017-05-02 15:19:55 +02:00
*
* @ return array
*/
2018-03-29 03:01:05 +02:00
protected function setSaveQuery ()
2017-05-02 15:19:55 +02:00
{
2017-07-24 13:55:12 +02:00
global $conf ;
2019-11-13 19:34:37 +01:00
$queryarray = array ();
2021-02-23 22:03:23 +01:00
foreach ( $this -> fields as $field => $info ) { // Loop on definition of fields
2017-07-24 13:55:12 +02:00
// Depending on field type ('datetime', ...)
2021-02-23 22:03:23 +01:00
if ( $this -> isDate ( $info )) {
2020-12-14 20:40:47 +01:00
if ( empty ( $this -> { $field })) {
2018-04-22 20:57:43 +02:00
$queryarray [ $field ] = null ;
2020-05-21 15:05:19 +02:00
} else {
2017-07-24 13:55:12 +02:00
$queryarray [ $field ] = $this -> db -> idate ( $this -> { $field });
2017-05-02 15:19:55 +02:00
}
2021-02-23 22:03:23 +01:00
} elseif ( $this -> isDuration ( $info )) {
2019-11-17 17:36:36 +01:00
// $this->{$field} may be null, '', 0, '0', 123, '123'
2020-12-14 20:40:47 +01:00
if (( isset ( $this -> { $field }) && $this -> { $field } != '' ) || ! empty ( $info [ 'notnull' ])) {
if ( ! isset ( $this -> { $field })) {
$queryarray [ $field ] = 0 ;
} else {
$queryarray [ $field ] = ( int ) $this -> { $field }; // If '0', it may be set to null later if $info['notnull'] == -1
}
2021-02-23 22:03:23 +01:00
} else {
$queryarray [ $field ] = null ;
2017-07-24 13:55:12 +02:00
}
2021-02-23 22:03:23 +01:00
} elseif ( $this -> isInt ( $info ) || $this -> isFloat ( $info )) {
if ( $field == 'entity' && is_null ( $this -> { $field })) {
$queryarray [ $field ] = $conf -> entity ;
} else {
2019-11-17 17:36:36 +01:00
// $this->{$field} may be null, '', 0, '0', 123, '123'
2020-12-14 20:40:47 +01:00
if (( isset ( $this -> { $field }) && $this -> { $field } != '' ) || ! empty ( $info [ 'notnull' ])) {
if ( ! isset ( $this -> { $field })) {
$queryarray [ $field ] = 0 ;
} elseif ( $this -> isInt ( $info )) {
$queryarray [ $field ] = ( int ) $this -> { $field }; // If '0', it may be set to null later if $info['notnull'] == -1
} elseif ( $this -> isFloat ( $info )) {
$queryarray [ $field ] = ( double ) $this -> { $field }; // If '0', it may be set to null later if $info['notnull'] == -1
}
2021-02-23 22:03:23 +01:00
} else {
$queryarray [ $field ] = null ;
}
2017-07-24 13:55:12 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2017-07-24 13:55:12 +02:00
$queryarray [ $field ] = $this -> { $field };
2017-05-02 15:19:55 +02:00
}
2017-07-24 13:55:12 +02:00
2021-02-23 22:03:23 +01:00
if ( $info [ 'type' ] == 'timestamp' && empty ( $queryarray [ $field ])) {
unset ( $queryarray [ $field ]);
}
if ( ! empty ( $info [ 'notnull' ]) && $info [ 'notnull' ] == - 1 && empty ( $queryarray [ $field ])) {
$queryarray [ $field ] = null ; // May force 0 to null
}
2017-05-02 15:19:55 +02:00
}
2017-06-07 16:44:04 +02:00
2017-07-24 13:55:12 +02:00
return $queryarray ;
2017-05-02 15:19:55 +02:00
}
2017-04-30 03:18:49 +02:00
2017-07-12 13:25:18 +02:00
/**
2017-11-03 17:20:08 +01:00
* Function to load data from a SQL pointer into properties of current object $this
2017-07-12 13:25:18 +02:00
*
* @ param stdClass $obj Contain data of object from database
2020-09-07 10:18:17 +02:00
* @ return void
2017-07-12 13:25:18 +02:00
*/
2020-01-09 22:14:39 +01:00
public function setVarsFromFetchObj ( & $obj )
2017-07-12 13:25:18 +02:00
{
2021-03-04 09:36:19 +01:00
global $db ;
2021-02-23 22:03:23 +01:00
foreach ( $this -> fields as $field => $info ) {
2020-12-14 20:40:47 +01:00
if ( $this -> isDate ( $info )) {
2021-03-05 19:29:09 +01:00
if ( is_null ( $obj -> { $field }) || $obj -> { $field } === '' || $obj -> { $field } === '0000-00-00 00:00:00' || $obj -> { $field } === '1000-01-01 00:00:00' ) {
$this -> { $field } = '' ;
2021-02-23 22:03:23 +01:00
} else {
2021-03-05 19:29:09 +01:00
$this -> { $field } = $db -> jdate ( $obj -> { $field });
2021-02-23 22:03:23 +01:00
}
2020-12-14 20:40:47 +01:00
} elseif ( $this -> isInt ( $info )) {
2021-02-23 22:03:23 +01:00
if ( $field == 'rowid' ) {
$this -> id = ( int ) $obj -> { $field };
} else {
2020-12-14 20:40:47 +01:00
if ( $this -> isForcedToNullIfZero ( $info )) {
2021-02-23 22:03:23 +01:00
if ( empty ( $obj -> { $field })) {
$this -> { $field } = null ;
} else {
$this -> { $field } = ( double ) $obj -> { $field };
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-19 09:30:26 +01:00
if ( ! is_null ( $obj -> { $field }) || ( isset ( $info [ 'notnull' ]) && $info [ 'notnull' ] == 1 )) {
2019-11-17 17:36:36 +01:00
$this -> { $field } = ( int ) $obj -> { $field };
} else {
$this -> { $field } = null ;
}
2019-10-14 11:38:36 +02:00
}
}
2020-12-14 20:40:47 +01:00
} elseif ( $this -> isFloat ( $info )) {
if ( $this -> isForcedToNullIfZero ( $info )) {
2021-02-23 22:03:23 +01:00
if ( empty ( $obj -> { $field })) {
$this -> { $field } = null ;
} else {
$this -> { $field } = ( double ) $obj -> { $field };
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-19 09:30:26 +01:00
if ( ! is_null ( $obj -> { $field }) || ( isset ( $info [ 'notnull' ]) && $info [ 'notnull' ] == 1 )) {
2019-11-17 17:36:36 +01:00
$this -> { $field } = ( double ) $obj -> { $field };
} else {
$this -> { $field } = null ;
}
2019-10-14 11:38:36 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> { $field } = $obj -> { $field };
}
}
2017-11-03 17:20:08 +01:00
// If there is no 'ref' field, we force property ->ref to ->id for a better compatibility with common functions.
2021-02-23 22:03:23 +01:00
if ( ! isset ( $this -> fields [ 'ref' ]) && isset ( $this -> id )) {
$this -> ref = $this -> id ;
}
2017-07-12 13:25:18 +02:00
}
/**
* Function to concat keys of fields
*
2021-03-09 15:13:20 +01:00
* @ param string $alias String of alias of table for fields . For example 't' .
* @ return string list of alias fields
2017-07-12 13:25:18 +02:00
*/
2021-03-11 19:09:42 +01:00
public function getFieldList ( $alias = '' )
2017-07-12 13:25:18 +02:00
{
2017-10-07 13:09:31 +02:00
$keys = array_keys ( $this -> fields );
2021-03-09 10:21:32 +01:00
if ( ! empty ( $alias )) {
$keys_with_alias = array ();
foreach ( $keys as $fieldname ) {
$keys_with_alias [] = $alias . '.' . $fieldname ;
}
return implode ( ',' , $keys_with_alias );
} else {
return implode ( ',' , $keys );
}
2017-07-12 13:25:18 +02:00
}
/**
* Add quote to field value if necessary
*
2017-07-24 13:55:12 +02:00
* @ param string | int $value Value to protect
* @ param array $fieldsentry Properties of field
* @ return string
2017-07-12 13:25:18 +02:00
*/
2019-05-13 22:02:33 +02:00
protected function quote ( $value , $fieldsentry )
{
2021-02-23 22:03:23 +01:00
if ( is_null ( $value )) {
return 'NULL' ;
} elseif ( preg_match ( '/^(int|double|real|price)/i' , $fieldsentry [ 'type' ])) {
return $this -> db -> escape ( " $value " );
} elseif ( $fieldsentry [ 'type' ] == 'boolean' ) {
if ( $value ) {
return 'true' ;
} else {
return 'false' ;
}
} else {
return " ' " . $this -> db -> escape ( $value ) . " ' " ;
2020-09-07 10:18:17 +02:00
}
2017-07-12 13:25:18 +02:00
}
2017-04-30 03:18:49 +02:00
/**
* Create object into database
*
* @ param User $user User that creates
* @ param bool $notrigger false = launch triggers after , true = disable triggers
2017-07-12 13:25:18 +02:00
* @ return int < 0 if KO , Id of created object if OK
2017-04-30 03:18:49 +02:00
*/
public function createCommon ( User $user , $notrigger = false )
{
2017-10-26 01:02:13 +02:00
global $langs ;
2020-04-13 19:02:19 +02:00
dol_syslog ( get_class ( $this ) . " ::createCommon create " , LOG_DEBUG );
2017-10-26 01:02:13 +02:00
2017-10-07 13:09:31 +02:00
$error = 0 ;
2017-06-07 16:44:04 +02:00
2019-11-16 23:24:29 +01:00
$now = dol_now ();
2017-07-24 13:55:12 +02:00
2018-03-29 03:01:05 +02:00
$fieldvalues = $this -> setSaveQuery ();
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( array_key_exists ( 'date_creation' , $fieldvalues ) && empty ( $fieldvalues [ 'date_creation' ])) {
$fieldvalues [ 'date_creation' ] = $this -> db -> idate ( $now );
}
if ( array_key_exists ( 'fk_user_creat' , $fieldvalues ) && ! ( $fieldvalues [ 'fk_user_creat' ] > 0 )) {
$fieldvalues [ 'fk_user_creat' ] = $user -> id ;
}
2019-11-16 23:24:29 +01:00
unset ( $fieldvalues [ 'rowid' ]); // The field 'rowid' is reserved field name for autoincrement field so we don't need it into insert.
2021-02-23 22:03:23 +01:00
if ( array_key_exists ( 'ref' , $fieldvalues )) {
$fieldvalues [ 'ref' ] = dol_string_nospecial ( $fieldvalues [ 'ref' ]); // If field is a ref, we sanitize data
}
2019-10-16 01:20:58 +02:00
2019-11-05 10:11:28 +01:00
$keys = array ();
2020-09-07 10:18:17 +02:00
$values = array (); // Array to store string forged for SQL syntax
2017-10-07 13:09:31 +02:00
foreach ( $fieldvalues as $k => $v ) {
2017-10-26 01:02:13 +02:00
$keys [ $k ] = $k ;
$value = $this -> fields [ $k ];
2020-09-07 10:18:17 +02:00
$values [ $k ] = $this -> quote ( $v , $value ); // May return string 'NULL' if $value is null
2017-07-12 13:25:18 +02:00
}
2017-06-07 16:44:04 +02:00
2017-10-26 01:02:13 +02:00
// Clean and check mandatory
2021-02-23 22:03:23 +01:00
foreach ( $keys as $key ) {
2017-11-24 16:24:42 +01:00
// If field is an implicit foreign key field
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/^integer:/i' , $this -> fields [ $key ][ 'type' ]) && $values [ $key ] == '-1' ) {
$values [ $key ] = '' ;
}
if ( ! empty ( $this -> fields [ $key ][ 'foreignkey' ]) && $values [ $key ] == '-1' ) {
$values [ $key ] = '' ;
}
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( isset ( $this -> fields [ $key ][ 'notnull' ]) && $this -> fields [ $key ][ 'notnull' ] == 1 && ( ! isset ( $values [ $key ]) || $values [ $key ] === 'NULL' ) && is_null ( $this -> fields [ $key ][ 'default' ])) {
2017-10-26 01:02:13 +02:00
$error ++ ;
2021-06-21 19:47:43 +02:00
$langs -> load ( " errors " );
dol_syslog ( " Mandatory field ' " . $key . " ' is empty and required into ->fields definition of class " );
2019-11-16 23:24:29 +01:00
$this -> errors [] = $langs -> trans ( " ErrorFieldRequired " , $this -> fields [ $key ][ 'label' ]);
2017-10-26 01:02:13 +02:00
}
2017-11-24 16:24:42 +01:00
2020-09-02 19:19:47 +02:00
// If value is null and there is a default value for field
2021-02-23 22:03:23 +01:00
if ( isset ( $this -> fields [ $key ][ 'notnull' ]) && $this -> fields [ $key ][ 'notnull' ] == 1 && ( ! isset ( $values [ $key ]) || $values [ $key ] === 'NULL' ) && ! is_null ( $this -> fields [ $key ][ 'default' ])) {
2021-06-21 19:47:43 +02:00
$values [ $key ] = $this -> quote ( $this -> fields [ $key ][ 'default' ], $this -> fields [ $key ]);
2017-10-26 01:02:13 +02:00
}
2017-06-07 16:44:04 +02:00
2017-11-24 16:24:42 +01:00
// If field is an implicit foreign key field
2020-01-20 18:38:53 +01:00
if ( preg_match ( '/^integer:/i' , $this -> fields [ $key ][ 'type' ]) && empty ( $values [ $key ])) {
2021-02-23 22:03:23 +01:00
if ( isset ( $this -> fields [ $key ][ 'default' ])) {
$values [ $key ] = $this -> fields [ $key ][ 'default' ];
} else {
$values [ $key ] = 'null' ;
}
}
if ( ! empty ( $this -> fields [ $key ][ 'foreignkey' ]) && empty ( $values [ $key ])) {
$values [ $key ] = 'null' ;
2020-01-20 18:38:53 +01:00
}
2017-10-07 13:09:31 +02:00
}
2017-07-12 13:25:18 +02:00
2021-02-23 22:03:23 +01:00
if ( $error ) {
return - 1 ;
}
2017-07-12 13:25:18 +02:00
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2017-07-12 13:25:18 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this -> table_element ;
2019-11-16 23:24:29 +01:00
$sql .= ' (' . implode ( " , " , $keys ) . ')' ;
$sql .= ' VALUES (' . implode ( " , " , $values ) . ')' ;
2017-07-24 13:55:12 +02:00
2017-08-20 18:50:51 +02:00
$res = $this -> db -> query ( $sql );
2019-11-16 23:24:29 +01:00
if ( $res === false ) {
2017-10-07 13:09:31 +02:00
$error ++ ;
$this -> errors [] = $this -> db -> lasterror ();
}
}
2017-07-12 13:25:18 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
2019-11-16 23:24:29 +01:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . $this -> table_element );
2017-11-12 20:45:06 +01:00
}
2017-07-12 13:25:18 +02:00
2019-03-12 19:20:01 +01:00
// If we have a field ref with a default value of (PROV)
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
2021-06-23 01:34:14 +02:00
if ( key_exists ( 'ref' , $this -> fields ) && $this -> fields [ 'ref' ][ 'notnull' ] > 0 && key_exists ( 'default' , $this -> fields [ 'ref' ]) && $this -> fields [ 'ref' ][ 'default' ] == '(PROV)' ) {
2021-03-30 03:37:54 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element . " SET ref = '(PROV " . $this -> id . " )' WHERE (ref = '(PROV)' OR ref = '') AND rowid = " . (( int ) $this -> id );
2020-09-07 10:18:17 +02:00
$resqlupdate = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resqlupdate === false ) {
2020-09-07 10:18:17 +02:00
$error ++ ;
$this -> errors [] = $this -> db -> lasterror ();
} else {
$this -> ref = '(PROV' . $this -> id . ')' ;
}
}
2019-03-12 19:20:01 +01:00
}
2018-02-01 19:03:21 +01:00
// Create extrafields
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
2019-11-13 19:34:37 +01:00
$result = $this -> insertExtraFields ();
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-11-12 20:45:06 +01:00
}
2017-07-12 13:25:18 +02:00
2019-06-03 22:07:34 +02:00
// Create lines
2021-02-23 22:03:23 +01:00
if ( ! empty ( $this -> table_element_line ) && ! empty ( $this -> fk_element )) {
2019-11-13 19:34:37 +01:00
$num = ( is_array ( $this -> lines ) ? count ( $this -> lines ) : 0 );
2021-02-23 22:03:23 +01:00
for ( $i = 0 ; $i < $num ; $i ++ ) {
2019-06-03 22:07:34 +02:00
$line = $this -> lines [ $i ];
$keyforparent = $this -> fk_element ;
$line -> $keyforparent = $this -> id ;
// Test and convert into object this->lines[$i]. When coming from REST API, we may still have an array
//if (! is_object($line)) $line=json_decode(json_encode($line), false); // convert recursively array into object.
2021-02-23 22:03:23 +01:00
if ( ! is_object ( $line )) {
$line = ( object ) $line ;
}
2019-06-03 22:07:34 +02:00
$result = $line -> create ( $user , 1 );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2021-06-22 17:09:02 +02:00
$this -> error = $line -> error ;
2019-06-03 22:07:34 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
}
2018-02-01 19:03:21 +01:00
// Triggers
2021-02-23 22:03:23 +01:00
if ( ! $error && ! $notrigger ) {
2017-11-12 20:45:06 +01:00
// Call triggers
2019-11-13 19:34:37 +01:00
$result = $this -> call_trigger ( strtoupper ( get_class ( $this )) . '_CREATE' , $user );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2017-11-12 20:45:06 +01:00
// End call triggers
2017-10-07 13:09:31 +02:00
}
2017-07-12 13:25:18 +02:00
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
return - 1 ;
} else {
$this -> db -> commit ();
return $this -> id ;
}
2017-05-02 15:19:55 +02:00
}
2017-06-07 16:44:04 +02:00
2017-04-30 03:18:49 +02:00
/**
* Load object in memory from the database
*
2021-06-22 17:09:02 +02:00
* @ param int $id Id object
* @ param string $ref Ref
2018-03-14 13:56:21 +01:00
* @ param string $morewhere More SQL filters ( ' AND ...' )
* @ return int < 0 if KO , 0 if not found , > 0 if OK
2017-04-30 03:18:49 +02:00
*/
2018-03-14 13:56:21 +01:00
public function fetchCommon ( $id , $ref = null , $morewhere = '' )
2017-04-30 03:18:49 +02:00
{
2021-02-23 22:03:23 +01:00
if ( empty ( $id ) && empty ( $ref ) && empty ( $morewhere )) {
return - 1 ;
}
2017-06-07 16:44:04 +02:00
2021-03-09 10:24:07 +01:00
$fieldlist = $this -> getFieldList ( 't' );
2021-02-23 22:03:23 +01:00
if ( empty ( $fieldlist )) {
return 0 ;
}
2020-05-14 18:14:55 +02:00
$sql = 'SELECT ' . $fieldlist ;
2021-03-09 10:21:32 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element . ' as t' ;
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $id )) {
2021-03-14 11:48:39 +01:00
$sql .= ' WHERE t.rowid = ' . (( int ) $id );
2021-02-23 22:03:23 +01:00
} elseif ( ! empty ( $ref )) {
2021-03-09 10:24:07 +01:00
$sql .= " WHERE t.ref = " . $this -> quote ( $ref , $this -> fields [ 'ref' ]);
2021-02-23 22:03:23 +01:00
} else {
$sql .= ' WHERE 1 = 1' ; // usage with empty id and empty ref is very rare
}
if ( empty ( $id ) && isset ( $this -> ismultientitymanaged ) && $this -> ismultientitymanaged == 1 ) {
2021-03-09 10:24:07 +01:00
$sql .= ' AND t.entity IN (' . getEntity ( $this -> table_element ) . ')' ;
2021-02-23 22:03:23 +01:00
}
if ( $morewhere ) {
$sql .= $morewhere ;
}
2019-11-13 19:34:37 +01:00
$sql .= ' LIMIT 1' ; // This is a fetch, to be sure to get only one record
2017-05-06 22:28:07 +02:00
2017-05-02 15:19:55 +02:00
$res = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $res ) {
2018-01-15 11:21:24 +01:00
$obj = $this -> db -> fetch_object ( $res );
2021-02-23 22:03:23 +01:00
if ( $obj ) {
2018-01-15 11:21:24 +01:00
$this -> setVarsFromFetchObj ( $obj );
2020-07-04 16:14:53 +02:00
2020-12-14 20:40:47 +01:00
// Retrieve all extrafield
2020-07-04 16:14:53 +02:00
// fetch optionals attributes and labels
$this -> fetch_optionals ();
2018-01-15 11:21:24 +01:00
return $this -> id ;
2020-05-21 15:05:19 +02:00
} else {
2018-01-15 11:21:24 +01:00
return 0 ;
2017-10-07 13:09:31 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> error = $this -> db -> lasterror ();
$this -> errors [] = $this -> error ;
return - 1 ;
2017-05-02 15:19:55 +02:00
}
2017-04-30 03:18:49 +02:00
}
2019-06-03 18:11:53 +02:00
/**
* Load object in memory from the database
*
* @ param string $morewhere More SQL filters ( ' AND ...' )
* @ return int < 0 if KO , 0 if not found , > 0 if OK
*/
public function fetchLinesCommon ( $morewhere = '' )
{
$objectlineclassname = get_class ( $this ) . 'Line' ;
2021-02-23 22:03:23 +01:00
if ( ! class_exists ( $objectlineclassname )) {
2019-06-03 18:11:53 +02:00
$this -> error = 'Error, class ' . $objectlineclassname . ' not found during call of fetchLinesCommon' ;
return - 1 ;
}
$objectline = new $objectlineclassname ( $this -> db );
2021-03-09 10:21:32 +01:00
$sql = 'SELECT ' . $objectline -> getFieldList ( 'l' );
2021-03-09 20:12:14 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $objectline -> table_element . ' as l' ;
2021-03-09 10:21:32 +01:00
$sql .= ' WHERE l.fk_' . $this -> element . ' = ' . $this -> id ;
2021-02-23 22:03:23 +01:00
if ( $morewhere ) {
$sql .= $morewhere ;
}
2020-07-09 16:00:15 +02:00
if ( isset ( $objectline -> fields [ 'position' ])) {
$sql .= $this -> db -> order ( 'position' , 'ASC' );
}
2019-06-03 18:11:53 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2019-06-03 18:11:53 +02:00
$num_rows = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num_rows ) {
2019-06-03 18:11:53 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-23 22:03:23 +01:00
if ( $obj ) {
2019-06-03 18:11:53 +02:00
$newline = new $objectlineclassname ( $this -> db );
$newline -> setVarsFromFetchObj ( $obj );
$this -> lines [] = $newline ;
}
$i ++ ;
}
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2019-06-03 18:11:53 +02:00
$this -> error = $this -> db -> lasterror ();
$this -> errors [] = $this -> error ;
return - 1 ;
}
}
2017-04-30 03:18:49 +02:00
/**
* Update object into database
*
2018-02-01 19:03:21 +01:00
* @ param User $user User that modifies
* @ param bool $notrigger false = launch triggers after , true = disable triggers
* @ return int < 0 if KO , > 0 if OK
2017-04-30 03:18:49 +02:00
*/
public function updateCommon ( User $user , $notrigger = false )
{
2018-02-01 19:03:21 +01:00
global $conf , $langs ;
2020-04-13 19:02:19 +02:00
dol_syslog ( get_class ( $this ) . " ::updateCommon update " , LOG_DEBUG );
2017-10-26 01:02:13 +02:00
2017-10-07 13:09:31 +02:00
$error = 0 ;
2017-07-12 13:25:18 +02:00
2019-11-16 23:24:29 +01:00
$now = dol_now ();
2017-06-07 16:44:04 +02:00
2018-03-29 03:01:05 +02:00
$fieldvalues = $this -> setSaveQuery ();
2017-06-07 16:44:04 +02:00
2021-02-23 22:03:23 +01:00
if ( array_key_exists ( 'date_modification' , $fieldvalues ) && empty ( $fieldvalues [ 'date_modification' ])) {
$fieldvalues [ 'date_modification' ] = $this -> db -> idate ( $now );
}
if ( array_key_exists ( 'fk_user_modif' , $fieldvalues ) && ! ( $fieldvalues [ 'fk_user_modif' ] > 0 )) {
$fieldvalues [ 'fk_user_modif' ] = $user -> id ;
}
2019-11-16 23:24:29 +01:00
unset ( $fieldvalues [ 'rowid' ]); // The field 'rowid' is reserved field name for autoincrement field so we don't need it into update.
2021-02-23 22:03:23 +01:00
if ( array_key_exists ( 'ref' , $fieldvalues )) {
$fieldvalues [ 'ref' ] = dol_string_nospecial ( $fieldvalues [ 'ref' ]); // If field is a ref, we sanitize data
}
2017-06-07 16:44:04 +02:00
2020-12-14 20:40:47 +01:00
// Add quotes and escape on fields with type string
2019-11-05 10:11:28 +01:00
$keys = array ();
2017-11-05 03:39:27 +01:00
$values = array ();
2019-11-05 10:15:00 +01:00
$tmp = array ();
2017-07-24 13:55:12 +02:00
foreach ( $fieldvalues as $k => $v ) {
2017-10-26 14:49:30 +02:00
$keys [ $k ] = $k ;
2017-11-05 03:39:27 +01:00
$value = $this -> fields [ $k ];
$values [ $k ] = $this -> quote ( $v , $value );
2017-07-24 13:55:12 +02:00
$tmp [] = $k . '=' . $this -> quote ( $v , $this -> fields [ $k ]);
2017-05-02 15:19:55 +02:00
}
2017-10-26 01:02:13 +02:00
2020-12-14 20:40:47 +01:00
// Clean and check mandatory fields
2021-02-23 22:03:23 +01:00
foreach ( $keys as $key ) {
if ( preg_match ( '/^integer:/i' , $this -> fields [ $key ][ 'type' ]) && $values [ $key ] == '-1' ) {
$values [ $key ] = '' ; // This is an implicit foreign key field
}
if ( ! empty ( $this -> fields [ $key ][ 'foreignkey' ]) && $values [ $key ] == '-1' ) {
$values [ $key ] = '' ; // This is an explicit foreign key field
}
2017-10-26 01:02:13 +02:00
//var_dump($key.'-'.$values[$key].'-'.($this->fields[$key]['notnull'] == 1));
2017-10-26 14:49:30 +02:00
/*
2017-10-26 01:02:13 +02:00
if ( $this -> fields [ $key ][ 'notnull' ] == 1 && empty ( $values [ $key ]))
{
$error ++ ;
$this -> errors [] = $langs -> trans ( " ErrorFieldRequired " , $this -> fields [ $key ][ 'label' ]);
2017-10-26 14:49:30 +02:00
} */
2017-10-26 01:02:13 +02:00
}
2021-03-14 12:20:23 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element . ' SET ' . implode ( ', ' , $tmp ) . ' WHERE rowid=' . (( int ) $this -> id );
2017-06-07 16:44:04 +02:00
2017-07-12 13:25:18 +02:00
$this -> db -> begin ();
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
2017-10-07 13:09:31 +02:00
$res = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $res === false ) {
2017-10-07 13:09:31 +02:00
$error ++ ;
$this -> errors [] = $this -> db -> lasterror ();
}
2017-05-02 15:19:55 +02:00
}
2017-06-07 16:44:04 +02:00
2018-02-01 19:03:21 +01:00
// Update extrafield
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
2019-11-13 19:34:37 +01:00
$result = $this -> insertExtraFields ();
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2018-08-12 16:29:26 +02:00
$error ++ ;
2018-02-01 19:03:21 +01:00
}
2017-05-02 15:19:55 +02:00
}
2017-06-07 16:44:04 +02:00
2018-02-01 19:03:21 +01:00
// Triggers
2021-02-23 22:03:23 +01:00
if ( ! $error && ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call triggers
2019-11-13 19:34:37 +01:00
$result = $this -> call_trigger ( strtoupper ( get_class ( $this )) . '_MODIFY' , $user );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
} //Do also here what you must do to rollback action if trigger fail
2017-10-07 13:09:31 +02:00
// End call triggers
2017-07-12 13:25:18 +02:00
}
2017-06-07 16:44:04 +02:00
2017-07-12 13:25:18 +02:00
// Commit or rollback
if ( $error ) {
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
2017-07-12 13:25:18 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return $this -> id ;
2017-07-12 13:25:18 +02:00
}
2017-04-30 03:18:49 +02:00
}
2017-06-07 16:44:04 +02:00
2017-04-30 03:18:49 +02:00
/**
* Delete object in database
*
2018-11-19 21:11:26 +01:00
* @ param User $user User that deletes
* @ param bool $notrigger false = launch triggers after , true = disable triggers
* @ param int $forcechilddeletion 0 = no , 1 = Force deletion of children
* @ return int <= 0 if KO , > 0 if OK
2017-04-30 03:18:49 +02:00
*/
2019-01-27 15:20:16 +01:00
public function deleteCommon ( User $user , $notrigger = false , $forcechilddeletion = 0 )
2017-04-30 03:18:49 +02:00
{
2020-04-13 19:02:19 +02:00
dol_syslog ( get_class ( $this ) . " ::deleteCommon delete " , LOG_DEBUG );
2019-11-13 19:34:37 +01:00
$error = 0 ;
2017-10-07 13:09:31 +02:00
$this -> db -> begin ();
2021-02-23 22:03:23 +01:00
if ( $forcechilddeletion ) { // Force also delete of childtables that should lock deletion in standard case when option force is off
foreach ( $this -> childtables as $table ) {
2018-11-19 21:11:26 +01:00
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $table . ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( ! $resql ) {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> lasterror ();
$this -> errors [] = $this -> error ;
2018-11-19 21:11:26 +01:00
$this -> db -> rollback ();
return - 1 ;
}
}
2021-02-23 22:03:23 +01:00
} elseif ( ! empty ( $this -> fk_element ) && ! empty ( $this -> childtables )) { // If object has childs linked with a foreign key field, we check all child tables.
2018-11-19 21:11:26 +01:00
$objectisused = $this -> isObjectUsed ( $this -> id );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $objectisused )) {
2018-11-19 21:11:26 +01:00
dol_syslog ( get_class ( $this ) . " ::deleteCommon Can't delete record as it has some child " , LOG_WARNING );
2019-11-13 19:34:37 +01:00
$this -> error = 'ErrorRecordHasChildren' ;
$this -> errors [] = $this -> error ;
2018-11-19 21:11:26 +01:00
$this -> db -> rollback ();
return 0 ;
}
}
2019-06-22 20:17:40 +02:00
// Delete cascade first
2020-06-06 21:41:33 +02:00
if ( is_array ( $this -> childtablesoncascade ) && ! empty ( $this -> childtablesoncascade )) {
2021-02-23 22:03:23 +01:00
foreach ( $this -> childtablesoncascade as $table ) {
2020-09-08 21:27:28 +02:00
$deleteFromObject = explode ( ':' , $table );
if ( count ( $deleteFromObject ) >= 2 ) {
$className = str_replace ( '@' , '' , $deleteFromObject [ 0 ]);
$filePath = $deleteFromObject [ 1 ];
$columnName = $deleteFromObject [ 2 ];
if ( dol_include_once ( $filePath )) {
$childObject = new $className ( $this -> db );
if ( method_exists ( $childObject , 'deleteByParentField' )) {
$result = $childObject -> deleteByParentField ( $this -> id , $columnName );
if ( $result < 0 ) {
2020-06-06 21:37:53 +02:00
$error ++ ;
2020-09-08 21:27:28 +02:00
$this -> errors [] = $childObject -> error ;
break ;
}
} else {
2020-06-06 21:47:27 +02:00
$error ++ ;
$this -> errors [] = " You defined a cascade delete on an object $childObject but there is no method deleteByParentField for it " ;
break ;
}
2020-09-08 21:27:28 +02:00
} else {
$error ++ ;
2020-09-07 10:18:17 +02:00
$this -> errors [] = 'Cannot include child class file ' . $filePath ;
2020-09-08 21:27:28 +02:00
break ;
}
} else {
// Delete record in child table
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $table . ' WHERE ' . $this -> fk_element . ' = ' . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2020-06-06 21:37:53 +02:00
$error ++ ;
2020-09-08 21:27:28 +02:00
$this -> error = $this -> db -> lasterror ();
$this -> errors [] = $this -> error ;
2020-06-06 21:37:53 +02:00
break ;
2020-09-08 21:27:28 +02:00
}
}
}
}
2019-06-22 20:17:40 +02:00
2019-11-13 19:34:37 +01:00
if ( ! $error ) {
if ( ! $notrigger ) {
2017-10-07 13:09:31 +02:00
// Call triggers
2019-11-13 19:34:37 +01:00
$result = $this -> call_trigger ( strtoupper ( get_class ( $this )) . '_DELETE' , $user );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
} // Do also here what you must do to rollback action if trigger fail
2017-10-07 13:09:31 +02:00
// End call triggers
}
}
2018-02-01 19:03:21 +01:00
2020-12-14 20:40:47 +01:00
// Delete llx_ecm_files
if ( ! $error ) {
$res = $this -> deleteEcmFiles ( 1 ); // Deleting files physically is done later with the dol_delete_dir_recursive
if ( ! $res ) {
2018-02-01 19:03:21 +01:00
$error ++ ;
}
}
2017-10-07 13:09:31 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $error && ! empty ( $this -> isextrafieldmanaged )) {
2020-04-13 19:02:19 +02:00
$result = $this -> deleteExtraFields ();
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2018-02-15 00:01:25 +01:00
}
2017-10-07 13:09:31 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
2021-03-14 12:20:23 +01:00
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this -> table_element . ' WHERE rowid=' . (( int ) $this -> id );
2017-10-07 13:09:31 +02:00
2021-06-22 17:09:02 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2017-10-07 13:09:31 +02:00
$error ++ ;
$this -> errors [] = $this -> db -> lasterror ();
}
}
// Commit or rollback
2017-07-12 13:25:18 +02:00
if ( $error ) {
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
2017-07-12 13:25:18 +02:00
} else {
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return 1 ;
2017-07-12 13:25:18 +02:00
}
2017-04-30 03:18:49 +02:00
}
2017-04-30 14:04:01 +02:00
2020-06-04 20:14:19 +02:00
/**
2020-06-06 21:30:08 +02:00
* Delete all child object from a parent ID
*
2020-06-04 20:14:19 +02:00
* @ param int $parentId Parent Id
2020-06-04 20:20:43 +02:00
* @ param string $parentField Name of Foreign key parent column
2020-06-04 20:14:19 +02:00
* @ return int < 0 if KO , > 0 if OK
* @ throws Exception
*/
2020-06-04 20:20:48 +02:00
public function deleteByParentField ( $parentId = 0 , $parentField = '' )
2020-06-04 20:14:19 +02:00
{
global $user ;
$error = 0 ;
$deleted = 0 ;
if ( ! empty ( $parentId ) && ! empty ( $parentField )) {
$this -> db -> begin ();
2020-09-07 10:18:17 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= ' WHERE ' . $parentField . ' = ' . ( int ) $parentId ;
2020-06-06 21:30:08 +02:00
2020-06-04 20:14:19 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$this -> errors [] = $this -> db -> lasterror ();
$error ++ ;
} else {
while ( $obj = $this -> db -> fetch_object ( $resql )) {
$result = $this -> fetch ( $obj -> rowid );
if ( $result < 0 ) {
$error ++ ;
2020-06-04 21:39:23 +02:00
$this -> errors [] = $this -> error ;
2020-06-04 20:14:19 +02:00
} else {
2020-06-12 02:15:02 +02:00
if ( get_class ( $this ) == 'Contact' ) { // TODO special code because delete() for contact has not been standardized like other delete.
$result = $this -> delete ();
} else {
$result = $this -> delete ( $user );
}
2020-06-04 20:14:19 +02:00
if ( $result < 0 ) {
$error ++ ;
2020-06-04 21:38:44 +02:00
$this -> errors [] = $this -> error ;
2020-06-04 20:14:19 +02:00
} else {
$deleted ++ ;
}
}
}
}
if ( empty ( $error )) {
$this -> db -> commit ();
return $deleted ;
} else {
2020-06-06 21:30:08 +02:00
$this -> error = implode ( ', ' , $this -> errors );
2020-06-04 20:14:19 +02:00
$this -> db -> rollback ();
return $error * - 1 ;
}
}
return $deleted ;
}
2019-06-03 20:46:11 +02:00
/**
* Delete a line of object in database
*
* @ param User $user User that delete
* @ param int $idline Id of line to delete
* @ param bool $notrigger false = launch triggers after , true = disable triggers
* @ return int > 0 if OK , < 0 if KO
*/
public function deleteLineCommon ( User $user , $idline , $notrigger = false )
{
global $conf ;
2019-11-13 19:34:37 +01:00
$error = 0 ;
2019-06-03 20:46:11 +02:00
$tmpforobjectclass = get_class ( $this );
$tmpforobjectlineclass = ucfirst ( $tmpforobjectclass ) . 'Line' ;
// Call trigger
2019-11-13 19:34:37 +01:00
$result = $this -> call_trigger ( 'LINE' . strtoupper ( $tmpforobjectclass ) . '_DELETE' , $user );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
return - 1 ;
}
2019-06-03 20:46:11 +02:00
// End call triggers
$this -> db -> begin ();
$sql = " DELETE FROM " . MAIN_DB_PREFIX . $this -> table_element_line ;
2021-04-25 15:55:36 +02:00
$sql .= " WHERE rowid = " . (( int ) $idline );
2019-06-03 20:46:11 +02:00
dol_syslog ( get_class ( $this ) . " ::deleteLineCommon " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( ! $resql ) {
2019-11-13 19:34:37 +01:00
$this -> error = " Error " . $this -> db -> lasterror ();
2019-06-03 20:46:11 +02:00
$error ++ ;
}
if ( empty ( $error )) {
// Remove extrafields
2020-08-17 15:53:14 +02:00
$tmpobjectline = new $tmpforobjectlineclass ( $this -> db );
if ( ! isset ( $tmpobjectline -> isextrafieldmanaged ) || ! empty ( $tmpobjectline -> isextrafieldmanaged )) {
2019-11-13 19:34:37 +01:00
$tmpobjectline -> id = $idline ;
$result = $tmpobjectline -> deleteExtraFields ();
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2019-06-03 20:46:11 +02:00
$error ++ ;
2019-11-13 19:34:37 +01:00
$this -> error = " Error " . get_class ( $this ) . " ::deleteLineCommon deleteExtraFields error -4 " . $tmpobjectline -> error ;
2019-06-03 20:46:11 +02:00
}
}
}
if ( empty ( $error )) {
$this -> db -> commit ();
return 1 ;
} else {
dol_syslog ( get_class ( $this ) . " ::deleteLineCommon ERROR: " . $this -> error , LOG_ERR );
$this -> db -> rollback ();
return - 1 ;
}
}
2019-10-16 01:20:58 +02:00
/**
2019-12-20 17:39:42 +01:00
* Set to a status
2019-10-16 01:20:58 +02:00
*
* @ param User $user Object user that modify
* @ param int $status New status to set ( often a constant like self :: STATUS_XXX )
* @ param int $notrigger 1 = Does not execute triggers , 0 = Execute triggers
* @ param string $triggercode Trigger code to use
* @ return int < 0 if KO , > 0 if OK
*/
public function setStatusCommon ( $user , $status , $notrigger = 0 , $triggercode = '' )
{
2019-11-13 19:34:37 +01:00
$error = 0 ;
2019-10-16 01:20:58 +02:00
$this -> db -> begin ();
2020-12-14 20:40:47 +01:00
$statusfield = 'status' ;
2021-02-23 22:03:23 +01:00
if ( $this -> element == 'don' || $this -> element == 'donation' ) {
$statusfield = 'fk_statut' ;
}
2020-12-14 20:40:47 +01:00
2019-10-16 01:20:58 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
2020-12-14 20:40:47 +01:00
$sql .= " SET " . $statusfield . " = " . (( int ) $status );
2019-11-13 19:34:37 +01:00
$sql .= " WHERE rowid = " . $this -> id ;
2019-10-16 01:20:58 +02:00
2021-02-23 22:03:23 +01:00
if ( $this -> db -> query ( $sql )) {
if ( ! $error ) {
2019-11-13 19:34:37 +01:00
$this -> oldcopy = clone $this ;
2019-10-16 01:20:58 +02:00
}
2019-11-13 19:34:37 +01:00
if ( ! $error && ! $notrigger ) {
2019-10-16 01:20:58 +02:00
// Call trigger
2019-11-13 19:34:37 +01:00
$result = $this -> call_trigger ( $triggercode , $user );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2019-10-16 01:20:58 +02:00
}
if ( ! $error ) {
$this -> status = $status ;
$this -> db -> commit ();
return 1 ;
} else {
$this -> db -> rollback ();
return - 1 ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-13 19:34:37 +01:00
$this -> error = $this -> db -> error ();
2019-10-16 01:20:58 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
2017-04-30 14:04:01 +02:00
/**
2017-07-12 13:25:18 +02:00
* Initialise object with example values
* Id must be 0 if object instance is a specimen
2017-04-30 14:04:01 +02:00
*
2020-12-14 20:40:47 +01:00
* @ return int
2017-04-30 14:04:01 +02:00
*/
2017-07-12 13:25:18 +02:00
public function initAsSpecimenCommon ()
{
2020-09-07 10:18:17 +02:00
global $user ;
2017-06-07 16:44:04 +02:00
2019-03-04 14:01:30 +01:00
$this -> id = 0 ;
2020-12-14 20:40:47 +01:00
$this -> specimen = 1 ;
$fields = array (
'label' => 'This is label' ,
'ref' => 'ABCD1234' ,
'description' => 'This is a description' ,
'qty' => 123.12 ,
'note_public' => 'Public note' ,
'note_private' => 'Private note' ,
'date_creation' => ( dol_now () - 3600 * 48 ),
'date_modification' => ( dol_now () - 3600 * 24 ),
'fk_user_creat' => $user -> id ,
'fk_user_modif' => $user -> id ,
'date' => dol_now (),
);
foreach ( $fields as $key => $value ) {
2021-02-23 22:03:23 +01:00
if ( array_key_exists ( $key , $this -> fields )) {
$this -> { $key } = $value ;
}
2020-12-14 20:40:47 +01:00
}
return 1 ;
2017-04-30 14:04:01 +02:00
}
2017-06-07 16:44:04 +02:00
2018-03-13 22:43:50 +01:00
/* Part for comments */
2017-09-18 17:27:29 +02:00
/**
* Load comments linked with current task
* @ return boolean 1 if ok
*/
public function fetchComments ()
{
require_once DOL_DOCUMENT_ROOT . '/core/class/comment.class.php' ;
2017-06-07 16:44:04 +02:00
2017-09-18 17:27:29 +02:00
$comment = new Comment ( $this -> db );
2019-11-13 19:34:37 +01:00
$result = $comment -> fetchAllFor ( $this -> element , $this -> id );
if ( $result < 0 ) {
$this -> errors = array_merge ( $this -> errors , $comment -> errors );
2018-03-07 08:47:34 +01:00
return - 1 ;
} else {
$this -> comments = $comment -> comments ;
}
return count ( $this -> comments );
2017-09-18 17:27:29 +02:00
}
2017-10-04 16:08:22 +02:00
2020-09-07 10:18:17 +02:00
/**
* Return nb comments already posted
*
* @ return int
*/
public function getNbComments ()
{
return count ( $this -> comments );
}
2020-09-03 12:43:38 +02:00
/**
2020-09-07 10:18:17 +02:00
* Trim object parameters
2020-09-03 12:43:38 +02:00
*
2020-12-14 20:40:47 +01:00
* @ param string [] $parameters array of parameters to trim
2020-09-07 10:18:17 +02:00
* @ return void
2020-09-03 12:43:38 +02:00
*/
2020-09-07 10:18:17 +02:00
public function trimParameters ( $parameters )
{
2021-02-23 22:03:23 +01:00
if ( ! is_array ( $parameters )) {
return ;
}
2020-09-07 10:18:17 +02:00
foreach ( $parameters as $parameter ) {
if ( isset ( $this -> $parameter )) {
$this -> $parameter = trim ( $this -> $parameter );
}
}
}
/* Part for categories/tags */
/**
* Sets object to given categories .
*
* Deletes object from existing categories not supplied .
* Adds it to non existing supplied categories .
* Existing categories are left untouch .
*
* @ param string $type_categ Category type ( 'customer' , 'supplier' , 'website_page' , ... )
* @ return int Array of category objects or < 0 if KO
*/
public function getCategoriesCommon ( $type_categ )
{
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
// Get current categories
$c = new Categorie ( $this -> db );
$existing = $c -> containing ( $this -> id , $type_categ , 'id' );
return $existing ;
}
/**
* Sets object to given categories .
*
* Adds it to non existing supplied categories .
2020-12-23 23:32:50 +01:00
* Deletes object from existing categories not supplied ( if remove_existing == true ) .
2020-09-07 10:18:17 +02:00
* Existing categories are left untouch .
*
2020-12-23 23:32:50 +01:00
* @ param int [] | int $categories Category ID or array of Categories IDs
* @ param string $type_categ Category type ( 'customer' , 'supplier' , 'website_page' , ... ) definied into const class Categorie type
* @ param boolean $remove_existing True : Remove existings categories from Object if not supplies by $categories , False : let them
2020-09-07 10:18:17 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
2020-12-28 21:32:46 +01:00
public function setCategoriesCommon ( $categories , $type_categ = '' , $remove_existing = true )
2020-09-07 10:18:17 +02:00
{
2021-09-29 08:47:22 +02:00
// Handle single category
if ( ! is_array ( $categories )) {
$categories = array ( $categories );
}
2021-09-28 16:35:01 +02:00
2020-12-23 23:32:50 +01:00
dol_syslog ( get_class ( $this ) . " ::setCategoriesCommon Oject Id: " . $this -> id . ' type_categ:' . $type_categ . ' nb tag add:' . count ( $categories ), LOG_DEBUG );
2020-09-07 10:18:17 +02:00
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
2020-12-23 23:32:50 +01:00
if ( empty ( $type_categ )) {
dol_syslog ( __METHOD__ . ': Type ' . $type_categ . 'is an unknown category type. Done nothing.' , LOG_ERR );
return - 1 ;
}
2020-09-07 10:18:17 +02:00
// Get current categories
$c = new Categorie ( $this -> db );
$existing = $c -> containing ( $this -> id , $type_categ , 'id' );
2020-12-23 23:32:50 +01:00
if ( $remove_existing ) {
// Diff
if ( is_array ( $existing )) {
$to_del = array_diff ( $existing , $categories );
$to_add = array_diff ( $categories , $existing );
} else {
$to_del = array (); // Nothing to delete
$to_add = $categories ;
}
2020-09-07 10:18:17 +02:00
} else {
$to_del = array (); // Nothing to delete
2020-12-23 23:32:50 +01:00
$to_add = array_diff ( $categories , $existing );
2020-09-07 10:18:17 +02:00
}
$error = 0 ;
2020-12-23 23:32:50 +01:00
$ok = 0 ;
2020-09-07 10:18:17 +02:00
// Process
foreach ( $to_del as $del ) {
if ( $c -> fetch ( $del ) > 0 ) {
2020-12-23 23:32:50 +01:00
$result = $c -> del_type ( $this , $type_categ );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2020-12-23 23:32:50 +01:00
$error ++ ;
$this -> error = $c -> error ;
$this -> errors = $c -> errors ;
break ;
} else {
$ok += $result ;
}
2020-09-07 10:18:17 +02:00
}
}
foreach ( $to_add as $add ) {
2021-02-23 22:03:23 +01:00
if ( $c -> fetch ( $add ) > 0 ) {
2020-09-07 10:18:17 +02:00
$result = $c -> add_type ( $this , $type_categ );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2020-09-07 10:18:17 +02:00
$error ++ ;
$this -> error = $c -> error ;
$this -> errors = $c -> errors ;
break ;
2020-12-23 23:32:50 +01:00
} else {
$ok += $result ;
2020-09-07 10:18:17 +02:00
}
}
}
2020-12-23 23:32:50 +01:00
return $error ? - 1 * $error : $ok ;
2020-09-07 10:18:17 +02:00
}
/**
* Copy related categories to another object
*
* @ param int $fromId Id object source
* @ param int $toId Id object cible
* @ param string $type Type of category ( 'product' , ... )
2020-12-14 20:40:47 +01:00
* @ return int < 0 if error , > 0 if ok
2020-09-07 10:18:17 +02:00
*/
2019-09-05 16:19:35 +02:00
public function cloneCategories ( $fromId , $toId , $type = '' )
2020-09-04 09:55:02 +02:00
{
2020-09-03 12:43:38 +02:00
$this -> db -> begin ();
2021-02-23 22:03:23 +01:00
if ( empty ( $type )) {
$type = $this -> table_element ;
}
2019-04-05 11:30:51 +02:00
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
$categorystatic = new Categorie ( $this -> db );
2020-04-04 19:25:36 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " categorie_ " . ( empty ( $categorystatic -> MAP_CAT_TABLE [ $type ]) ? $type : $categorystatic -> MAP_CAT_TABLE [ $type ]) . " (fk_categorie, fk_product) " ;
$sql .= " SELECT fk_categorie, $toId FROM " . MAIN_DB_PREFIX . " categorie_ " . ( empty ( $categorystatic -> MAP_CAT_TABLE [ $type ]) ? $type : $categorystatic -> MAP_CAT_TABLE [ $type ]);
2020-09-19 21:19:04 +02:00
$sql .= " WHERE fk_product = " . (( int ) $fromId );
2019-04-05 11:30:51 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $this -> db -> query ( $sql )) {
2020-09-03 12:43:38 +02:00
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
2019-04-05 11:30:51 +02:00
return - 1 ;
}
$this -> db -> commit ();
return 1 ;
}
2020-09-12 04:25:54 +02:00
2020-09-03 12:43:38 +02:00
/**
* Delete related files of object in database
*
2020-12-14 20:40:47 +01:00
* @ param integer $mode 0 = Use path to find record , 1 = Use src_object_xxx fields ( Mode 1 is recommanded for new objects )
* @ return bool True if OK , False if KO
2020-09-03 12:43:38 +02:00
*/
2020-12-14 20:40:47 +01:00
public function deleteEcmFiles ( $mode = 0 )
2020-09-04 09:55:02 +02:00
{
2020-09-03 12:43:38 +02:00
global $conf ;
2020-09-12 04:25:54 +02:00
2020-09-03 12:43:38 +02:00
$this -> db -> begin ();
2020-12-14 20:40:47 +01:00
// Delete in database with mode 0
if ( $mode == 0 ) {
switch ( $this -> element ) {
case 'propal' :
$element = 'propale' ;
break ;
case 'product' :
$element = 'produit' ;
break ;
case 'order_supplier' :
$element = 'fournisseur/commande' ;
break ;
case 'invoice_supplier' :
$element = 'fournisseur/facture/' . get_exdir ( $this -> id , 2 , 0 , 1 , $this , 'invoice_supplier' );
break ;
case 'shipping' :
$element = 'expedition/sending' ;
break ;
default :
$element = $this -> element ;
}
// Delete ecm_files extrafields
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " ecm_files_extrafields WHERE fk_object IN ( " ;
$sql .= " SELECT rowid FROM " . MAIN_DB_PREFIX . " ecm_files WHERE filename LIKE ' " . $this -> db -> escape ( $this -> ref ) . " %' " ;
2021-08-23 18:56:46 +02:00
$sql .= " AND filepath = ' " . $this -> db -> escape ( $element ) . " / " . $this -> db -> escape ( $this -> ref ) . " ' AND entity = " . (( int ) $conf -> entity ); // No need of getEntity here
2020-12-14 20:40:47 +01:00
$sql .= " ) " ;
if ( ! $this -> db -> query ( $sql )) {
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return false ;
}
// Delete ecm_files
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " ecm_files " ;
$sql .= " WHERE filename LIKE ' " . $this -> db -> escape ( $this -> ref ) . " %' " ;
2021-08-23 18:56:46 +02:00
$sql .= " AND filepath = ' " . $this -> db -> escape ( $element ) . " / " . $this -> db -> escape ( $this -> ref ) . " ' AND entity = " . (( int ) $conf -> entity ); // No need of getEntity here
2020-12-14 20:40:47 +01:00
if ( ! $this -> db -> query ( $sql )) {
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return false ;
}
}
// Delete in database with mode 1
if ( $mode == 1 ) {
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . " ecm_files_extrafields " ;
2021-08-23 18:56:46 +02:00
$sql .= " WHERE fk_object IN (SELECT rowid FROM " . MAIN_DB_PREFIX . " ecm_files WHERE src_object_type = ' " . $this -> db -> escape ( $this -> table_element . ( empty ( $this -> module ) ? '' : '@' . $this -> module )) . " ' AND src_object_id = " . (( int ) $this -> id ) . " ) " ;
2020-12-14 20:40:47 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return false ;
}
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . " ecm_files " ;
2021-03-30 17:53:25 +02:00
$sql .= " WHERE src_object_type = ' " . $this -> db -> escape ( $this -> table_element . ( empty ( $this -> module ) ? '' : '@' . $this -> module )) . " ' AND src_object_id = " . (( int ) $this -> id );
2020-12-14 20:40:47 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
return false ;
}
2020-09-03 12:43:38 +02:00
}
$this -> db -> commit ();
return true ;
}
2013-04-19 13:30:08 +02:00
}