2017-05-27 13:46:34 +02:00
< ? php
2023-02-02 21:35:40 +01:00
/* Copyright ( C ) 2017 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2023 Frédéric France < frederic . france @ netlogic . fr >
2017-05-27 13:46:34 +02:00
* Copyright ( C ) --- Put here your own copyright and developer email ---
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2017-05-27 13:46:34 +02:00
*/
/**
* \file htdocs / modulebuilder / template / class / myobject . class . php
2017-06-17 21:23:52 +02:00
* \ingroup mymodule
* \brief This file is a CRUD class file for MyObject ( Create / Read / Update / Delete )
2017-05-27 13:46:34 +02:00
*/
// Put here all includes required by your class file
2019-11-13 18:32:11 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2017-05-27 13:46:34 +02:00
//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
/**
2017-06-17 21:23:52 +02:00
* Class for MyObject
2017-05-27 13:46:34 +02:00
*/
2017-06-17 19:40:48 +02:00
class MyObject extends CommonObject
2017-05-27 13:46:34 +02:00
{
/**
2020-08-25 12:47:38 +02:00
* @ var string ID of module .
*/
public $module = 'mymodule' ;
2017-05-27 13:46:34 +02:00
/**
2020-07-04 16:14:53 +02:00
* @ var string ID to identify managed object .
2017-05-27 13:46:34 +02:00
*/
2017-06-17 21:23:52 +02:00
public $element = 'myobject' ;
2018-09-02 18:49:23 +02:00
2017-05-27 13:46:34 +02:00
/**
2020-07-04 16:14:53 +02:00
* @ var string Name of table without prefix where object is stored . This is also the key used for extrafields management .
2017-05-27 13:46:34 +02:00
*/
2017-11-21 11:56:49 +01:00
public $table_element = 'mymodule_myobject' ;
2018-09-02 18:49:23 +02:00
2017-06-18 12:30:18 +02:00
/**
2020-02-08 14:40:30 +01:00
* @ var int Does this object support multicompany module ?
* 0 = No test on entity , 1 = Test with field entity , 'field@table' = Test with link by field @ table
2017-06-18 12:30:18 +02:00
*/
2017-10-25 22:02:07 +02:00
public $ismultientitymanaged = 0 ;
2018-09-02 18:49:23 +02:00
2018-02-15 00:01:25 +01:00
/**
2020-01-25 22:43:56 +01:00
* @ var int Does object support extrafields ? 0 = No , 1 = Yes
2018-02-15 00:01:25 +01:00
*/
public $isextrafieldmanaged = 1 ;
2018-09-02 18:49:23 +02:00
2017-06-18 12:30:18 +02:00
/**
2022-06-30 04:26:21 +02:00
* @ var string String with name of icon for myobject . Must be a 'fa-xxx' fontawesome code ( or 'fa-xxx_fa_color_size' ) or 'myobject@mymodule' if picto is file 'img/object_myobject.png' .
2017-07-11 20:47:49 +02:00
*/
2022-06-30 04:26:21 +02:00
public $picto = 'fa-file' ;
2017-06-07 16:44:04 +02:00
2017-06-18 12:30:18 +02:00
2019-03-12 19:20:01 +01:00
const STATUS_DRAFT = 0 ;
const STATUS_VALIDATED = 1 ;
2019-08-02 19:06:40 +02:00
const STATUS_CANCELED = 9 ;
2019-03-12 19:20:01 +01:00
2017-07-24 21:03:35 +02:00
/**
2022-07-24 22:13:36 +02:00
* 'type' field format :
* 'integer' , 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]' ,
* 'select' ( list of values are in 'options' ),
* 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter[:Sortfield]]]]' ,
* 'chkbxlst:...' ,
* 'varchar(x)' ,
* 'text' , 'text:none' , 'html' ,
* 'double(24,8)' , 'real' , 'price' ,
* 'date' , 'datetime' , 'timestamp' , 'duration' ,
* 'boolean' , 'checkbox' , 'radio' , 'array' ,
* 'mail' , 'phone' , 'url' , 'password' , 'ip'
2023-03-21 18:27:32 +01:00
* Note : Filter must be a Dolibarr Universal Filter syntax string . Example : " (t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.status:!=:0) or (t.nature:is:NULL) "
2017-10-25 22:02:07 +02:00
* 'label' the translation key .
2020-12-14 20:40:47 +01:00
* 'picto' is code of a picto to show before value in forms
2022-09-03 11:30:24 +02:00
* 'enabled' is a condition when the field must be managed ( Example : 1 or '$conf->global->MY_SETUP_PARAM' or 'isModEnabled("multicurrency")' ... )
2019-11-01 17:54:17 +01:00
* 'position' is the sort order of field .
* 'notnull' is set to 1 if not null in database . Set to - 1 if we must set data to null if empty ( '' or 0 ) .
2020-01-09 11:42:57 +01:00
* 'visible' says if field is visible in list ( Examples : 0 = Not visible , 1 = Visible on list and create / update / view forms , 2 = Visible on list only , 3 = Visible on create / update / view form only ( not list ), 4 = Visible on list and update / view form only ( not create ) . 5 = Visible on list and view only ( not create / not update ) . Using a negative value means field is not shown by default on list but can be selected for viewing )
2019-03-17 01:45:46 +01:00
* 'noteditable' says if field is not editable ( 1 or 0 )
2022-09-27 01:05:22 +02:00
* 'alwayseditable' says if field can be modified also when status is not draft ( '1' or '0' )
2019-10-27 16:28:03 +01:00
* 'default' is a default value for creation ( can still be overwrote by the Setup of Default Values if field is editable in creation form ) . Note : If default is set to '(PROV)' and field is 'ref' , the default value will be set to '(PROVid)' where id is rowid when a new record is created .
2017-10-25 22:02:07 +02:00
* 'index' if we want an index in database .
* 'foreignkey' => 'tablename.field' if the field is a foreign key ( it is recommanded to name the field fk_ ... ) .
* 'searchall' is 1 if we want to search in this field when making a search from the quick search button .
2021-10-19 16:26:43 +02:00
* 'isameasure' must be set to 1 or 2 if field can be used for measure . Field type must be summable like integer or double ( 24 , 8 ) . Use 1 in most cases , or 2 if you don ' t want to see the column total into list ( for example for percentage )
2021-07-04 20:08:34 +02:00
* 'css' and 'cssview' and 'csslist' is the CSS style to use on field . 'css' is used in creation and update . 'cssview' is used in view mode . 'csslist' is used for columns in lists . For example : 'css' => 'minwidth300 maxwidth500 widthcentpercentminusx' , 'cssview' => 'wordbreak' , 'csslist' => 'tdoverflowmax200'
2023-01-21 12:24:54 +01:00
* 'help' and 'helplist' is a 'TranslationString' to use to show a tooltip on field . You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click .
2018-03-27 23:01:54 +02:00
* 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
2019-10-21 14:44:39 +02:00
* 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute . In most cases , this is never set into the definition of $fields into class , but is set dynamically by some part of code .
2021-04-29 12:10:55 +02:00
* 'arrayofkeyval' to set a list of values if type is a list of predefined values . For example : array ( " 0 " => " Draft " , " 1 " => " Active " , " -1 " => " Cancel " ) . Note that type can be 'integer' or 'varchar'
2020-05-25 14:08:36 +02:00
* 'autofocusoncreate' to have field having the focus on a create form . Only 1 field should have this property set to 1.
2019-11-01 17:54:17 +01:00
* 'comment' is not used . You can store here any text of your choice . It is not used by application .
2021-06-12 12:58:20 +02:00
* 'validate' is 1 if need to validate with $this -> validateField ()
2021-08-24 13:19:53 +02:00
* 'copytoclipboard' is 1 or 2 to allow to add a picto to copy value into clipboard ( 1 = picto after label , 2 = picto after value )
*
2020-01-09 11:42:57 +01:00
* Note : To have value dynamic , you can set value to 0 in definition and edit the value on the fly into the constructor .
2017-07-24 13:55:12 +02:00
*/
2017-07-12 11:52:07 +02:00
// BEGIN MODULEBUILDER PROPERTIES
2017-05-27 13:46:34 +02:00
/**
2022-12-01 20:56:11 +01:00
* @ var array Array with all fields into database and their property . Do not use it as a static var . It may be modified by constructor .
2020-05-01 08:40:55 +02:00
*/
2019-11-13 18:32:11 +01:00
public $fields = array (
2020-09-11 16:16:04 +02:00
'rowid' => array ( 'type' => 'integer' , 'label' => 'TechnicalID' , 'enabled' => 1 , 'visible' =>- 2 , 'noteditable' => 1 , 'notnull' => 1 , 'index' => 1 , 'position' => 1 , 'comment' => 'Id' , 'css' => 'left' ),
2021-06-05 15:14:10 +02:00
'entity' => array ( 'type' => 'integer' , 'label' => 'Entity' , 'enabled' => 1 , 'visible' => 0 , 'notnull' => 1 , 'default' => 1 , 'index' => 1 , 'position' => 10 ),
2021-07-11 13:30:30 +02:00
'ref' => array ( 'type' => 'varchar(128)' , 'label' => 'Ref' , 'enabled' => 1 , 'visible' => 1 , 'noteditable' => 0 , 'default' => '' , 'notnull' => 1 , 'showoncombobox' => 1 , 'index' => 1 , 'position' => 20 , 'searchall' => 1 , 'comment' => 'Reference of object' , 'validate' => 1 ),
2022-09-27 13:37:38 +02:00
'label' => array ( 'type' => 'varchar(255)' , 'label' => 'Label' , 'enabled' => 1 , 'visible' => 1 , 'position' => 30 , 'searchall' => 1 , 'css' => 'minwidth300' , 'cssview' => 'wordbreak' , 'help' => 'Help text' , 'showoncombobox' => 2 , 'validate' => 1 , 'alwayseditable' => 1 ),
2021-07-11 13:30:30 +02:00
'amount' => array ( 'type' => 'price' , 'label' => 'Amount' , 'enabled' => 1 , 'visible' => 1 , 'default' => 'null' , 'position' => 40 , 'searchall' => 0 , 'isameasure' => 1 , 'help' => 'Help text for amount' , 'validate' => 1 ),
'qty' => array ( 'type' => 'real' , 'label' => 'Qty' , 'enabled' => 1 , 'visible' => 1 , 'default' => '0' , 'position' => 45 , 'searchall' => 0 , 'isameasure' => 1 , 'help' => 'Help text for quantity' , 'css' => 'maxwidth75imp' , 'validate' => 1 ),
2023-02-25 20:12:48 +01:00
'fk_soc' => array ( 'type' => 'integer:Societe:societe/class/societe.class.php:1:((status:=:1) AND (entity:IN:__SHARED_ENTITIES__))' , 'picto' => 'company' , 'label' => 'ThirdParty' , 'visible' => 1 , 'enabled' => 'isModEnabled("societe")' , 'position' => 50 , 'notnull' =>- 1 , 'index' => 1 , 'help' => 'OrganizationEventLinkToThirdParty' , 'validate' => 1 , 'css' => 'maxwidth500 widthcentpercentminusxx' , 'csslist' => 'tdoverflowmax150' ),
2023-04-18 20:33:36 +02:00
'fk_project' => array ( 'type' => 'integer:Project:projet/class/project.class.php:1' , 'label' => 'Project' , 'picto' => 'project' , 'enabled' => 'isModEnabled("project")' , 'visible' =>- 1 , 'position' => 52 , 'notnull' =>- 1 , 'index' => 1 , 'validate' => 1 , 'css' => 'maxwidth500 widthcentpercentminusxx' , 'csslist' => 'tdoverflowmax150' ),
2021-07-11 13:30:30 +02:00
'description' => array ( 'type' => 'text' , 'label' => 'Description' , 'enabled' => 1 , 'visible' => 3 , 'position' => 60 , 'validate' => 1 ),
2022-03-11 10:55:28 +01:00
'note_public' => array ( 'type' => 'html' , 'label' => 'NotePublic' , 'enabled' => 1 , 'visible' => 0 , 'position' => 61 , 'validate' => 1 , 'cssview' => 'wordbreak' ),
'note_private' => array ( 'type' => 'html' , 'label' => 'NotePrivate' , 'enabled' => 1 , 'visible' => 0 , 'position' => 62 , 'validate' => 1 , 'cssview' => 'wordbreak' ),
2019-12-17 12:06:09 +01:00
'date_creation' => array ( 'type' => 'datetime' , 'label' => 'DateCreation' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' => 1 , 'position' => 500 ),
'tms' => array ( 'type' => 'timestamp' , 'label' => 'DateModification' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' => 0 , 'position' => 501 ),
//'date_validation ' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>502),
2022-09-27 13:37:38 +02:00
'fk_user_creat' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'UserAuthor' , 'picto' => 'user' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' => 1 , 'position' => 510 , 'foreignkey' => 'user.rowid' , 'csslist' => 'tdoverflowmax150' ),
'fk_user_modif' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'UserModif' , 'picto' => 'user' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' =>- 1 , 'position' => 511 , 'csslist' => 'tdoverflowmax150' ),
2019-12-17 12:06:09 +01:00
//'fk_user_valid' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512),
2020-09-30 13:37:47 +02:00
'last_main_doc' => array ( 'type' => 'varchar(255)' , 'label' => 'LastMainDoc' , 'enabled' => 1 , 'visible' => 0 , 'notnull' => 0 , 'position' => 600 ),
2019-12-17 12:06:09 +01:00
'import_key' => array ( 'type' => 'varchar(14)' , 'label' => 'ImportId' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' =>- 1 , 'index' => 0 , 'position' => 1000 ),
'model_pdf' => array ( 'type' => 'varchar(255)' , 'label' => 'Model pdf' , 'enabled' => 1 , 'visible' => 0 , 'notnull' =>- 1 , 'position' => 1010 ),
2022-04-04 13:17:45 +02:00
'status' => array ( 'type' => 'integer' , 'label' => 'Status' , 'enabled' => 1 , 'visible' => 1 , 'notnull' => 1 , 'default' => 0 , 'index' => 1 , 'position' => 2000 , 'arrayofkeyval' => array ( 0 => 'Draft' , 1 => 'Validated' , 9 => 'Canceled' ), 'validate' => 1 ),
2017-07-11 20:47:49 +02:00
);
2017-09-05 20:42:34 +02:00
2018-09-01 22:55:10 +02:00
/**
* @ var int ID
*/
2017-09-05 20:42:34 +02:00
public $rowid ;
2018-08-31 18:47:25 +02:00
/**
* @ var string Ref
*/
2017-09-05 20:42:34 +02:00
public $ref ;
2018-08-31 18:27:16 +02:00
/**
* @ var int Entity
*/
2017-09-05 20:42:34 +02:00
public $entity ;
2018-08-31 18:27:16 +02:00
2018-09-28 14:49:34 +02:00
/**
2020-05-01 08:40:55 +02:00
* @ var string label
*/
public $label ;
2018-12-22 19:25:08 +01:00
2020-05-01 08:40:55 +02:00
/**
* @ var string amount
*/
2017-09-05 20:42:34 +02:00
public $amount ;
2018-10-05 15:51:08 +02:00
2022-05-22 00:39:57 +02:00
/**
* @ var int Thirdparty ID
*/
public $socid ; // both socid and fk_soc are used
public $fk_soc ; // both socid and fk_soc are used
2018-10-05 15:51:08 +02:00
/**
* @ var int Status
*/
2017-09-05 20:42:34 +02:00
public $status ;
2018-12-22 19:25:08 +01:00
2018-12-19 13:56:51 +01:00
/**
2020-05-01 08:40:55 +02:00
* @ var integer | string date_creation
*/
2017-09-05 20:42:34 +02:00
public $date_creation ;
2018-12-22 19:25:08 +01:00
2018-12-19 13:56:51 +01:00
/**
2020-05-01 08:40:55 +02:00
* @ var integer tms
*/
2017-09-05 20:42:34 +02:00
public $tms ;
2018-10-12 09:26:58 +02:00
/**
2020-05-01 08:40:55 +02:00
* @ var int ID
*/
2017-09-05 20:42:34 +02:00
public $fk_user_creat ;
2018-10-12 09:26:58 +02:00
/**
2020-05-01 08:40:55 +02:00
* @ var int ID
*/
2017-09-05 20:42:34 +02:00
public $fk_user_modif ;
2018-12-22 19:25:08 +01:00
2018-12-19 13:56:51 +01:00
/**
2020-09-30 13:37:47 +02:00
* @ var string public $last_main_doc
*/
public $last_main_doc ;
2018-12-19 13:56:51 +01:00
/**
2020-05-01 08:40:55 +02:00
* @ var string import_key
*/
2017-09-05 20:42:34 +02:00
public $import_key ;
2017-07-12 11:52:07 +02:00
// END MODULEBUILDER PROPERTIES
2017-07-25 00:00:08 +02:00
2017-06-18 12:30:18 +02:00
// If this object has a subtable with lines
2017-05-27 13:46:34 +02:00
2020-12-14 20:40:47 +01:00
// /**
// * @var string Name of subtable line
// */
// public $table_element_line = 'mymodule_myobjectline';
2018-09-02 18:49:23 +02:00
2020-12-14 20:40:47 +01:00
// /**
// * @var string Field with ID of parent key if this object has a parent
// */
// public $fk_element = 'fk_myobject';
2018-09-02 18:49:23 +02:00
2020-12-14 20:40:47 +01:00
// /**
// * @var string Name of subtable class that manage subtable lines
// */
// public $class_element_line = 'MyObjectline';
2018-09-02 18:49:23 +02:00
2020-12-14 20:40:47 +01:00
// /**
// * @var array List of child tables. To test if we can delete object.
// */
2023-04-03 19:51:40 +02:00
// protected $childtables = array('mychildtable' => array('name'=>'MyObject', 'fk_element'=>'fk_myobject'));
2019-06-22 20:17:40 +02:00
2020-12-14 20:40:47 +01:00
// /**
// * @var array List of child tables. To know object to delete on cascade.
// * If name matches '@ClassNAme:FilePathClass;ParentFkFieldName' it will
// * call method deleteByParentField(parentId, ParentFkFieldName) to fetch and delete child object
// */
// protected $childtablesoncascade = array('mymodule_myobjectdet');
2018-09-02 18:49:23 +02:00
2020-12-14 20:40:47 +01:00
// /**
// * @var MyObjectLine[] Array of subtable lines
// */
// public $lines = array();
2017-06-07 16:44:04 +02:00
2017-05-27 13:46:34 +02:00
/**
* Constructor
*
* @ param DoliDb $db Database handler
*/
public function __construct ( DoliDB $db )
{
2019-04-23 12:52:48 +02:00
global $conf , $langs ;
2017-07-27 01:47:27 +02:00
2017-05-27 13:46:34 +02:00
$this -> db = $db ;
2017-07-27 01:47:27 +02:00
2022-06-30 14:40:03 +02:00
if ( empty ( $conf -> global -> MAIN_SHOW_TECHNICAL_ID ) && isset ( $this -> fields [ 'rowid' ]) && ! empty ( $this -> fields [ 'ref' ])) {
2021-02-26 18:26:44 +01:00
$this -> fields [ 'rowid' ][ 'visible' ] = 0 ;
}
2022-08-28 14:19:25 +02:00
if ( ! isModEnabled ( 'multicompany' ) && isset ( $this -> fields [ 'entity' ])) {
2021-02-26 18:26:44 +01:00
$this -> fields [ 'entity' ][ 'enabled' ] = 0 ;
}
2018-03-13 22:43:50 +01:00
2020-01-09 11:42:57 +01:00
// Example to show how to set values of fields definition dynamically
2023-03-21 18:27:32 +01:00
/* if ( $user -> hasRights -> ( 'mymodule' , 'myobject' , 'read' )) {
2020-01-09 11:42:57 +01:00
$this -> fields [ 'myfield' ][ 'visible' ] = 1 ;
$this -> fields [ 'myfield' ][ 'noteditable' ] = 0 ;
} */
2018-03-13 22:43:50 +01:00
// Unset fields that are disabled
2021-02-26 18:26:44 +01:00
foreach ( $this -> fields as $key => $val ) {
if ( isset ( $val [ 'enabled' ]) && empty ( $val [ 'enabled' ])) {
2018-03-13 22:43:50 +01:00
unset ( $this -> fields [ $key ]);
}
}
2018-10-26 11:10:04 +02:00
2018-10-31 20:10:20 +01:00
// Translate some data of arrayofkeyval
2021-02-26 18:26:44 +01:00
if ( is_object ( $langs )) {
foreach ( $this -> fields as $key => $val ) {
if ( ! empty ( $val [ 'arrayofkeyval' ]) && is_array ( $val [ 'arrayofkeyval' ])) {
foreach ( $val [ 'arrayofkeyval' ] as $key2 => $val2 ) {
2020-01-30 01:48:28 +01:00
$this -> fields [ $key ][ 'arrayofkeyval' ][ $key2 ] = $langs -> trans ( $val2 );
2019-11-09 12:24:18 +01:00
}
2018-10-31 20:10:20 +01:00
}
}
}
2017-05-27 13:46:34 +02:00
}
2017-07-25 02:56:19 +02:00
/**
* Create object into database
*
* @ param User $user User that creates
* @ param bool $notrigger false = launch triggers after , true = disable triggers
* @ return int < 0 if KO , Id of created object if OK
*/
public function create ( User $user , $notrigger = false )
{
2021-04-06 14:59:03 +02:00
$resultcreate = $this -> createCommon ( $user , $notrigger );
//$resultvalidate = $this->validate($user, $notrigger);
return $resultcreate ;
2017-07-25 02:56:19 +02:00
}
2017-08-03 01:45:31 +02:00
/**
2018-12-20 10:17:52 +01:00
* Clone an object into another one
2017-08-03 01:45:31 +02:00
*
2017-08-21 00:40:45 +02:00
* @ param User $user User that creates
* @ param int $fromid Id of object to clone
* @ return mixed New object created , < 0 if KO
2017-08-03 01:45:31 +02:00
*/
public function createFromClone ( User $user , $fromid )
{
2019-04-23 12:52:48 +02:00
global $langs , $extrafields ;
2020-05-01 08:40:55 +02:00
$error = 0 ;
dol_syslog ( __METHOD__ , LOG_DEBUG );
$object = new self ( $this -> db );
$this -> db -> begin ();
// Load source object
$result = $object -> fetchCommon ( $fromid );
2021-02-26 18:26:44 +01:00
if ( $result > 0 && ! empty ( $object -> table_element_line )) {
$object -> fetchLines ();
}
2020-05-01 08:40:55 +02:00
// get lines so they will be clone
//foreach($this->lines as $line)
// $line->fetch_optionals();
// Reset some properties
unset ( $object -> id );
unset ( $object -> fk_user_creat );
unset ( $object -> import_key );
// Clear fields
2021-02-26 18:26:44 +01:00
if ( property_exists ( $object , 'ref' )) {
$object -> ref = empty ( $this -> fields [ 'ref' ][ 'default' ]) ? " Copy_Of_ " . $object -> ref : $this -> fields [ 'ref' ][ 'default' ];
}
if ( property_exists ( $object , 'label' )) {
$object -> label = empty ( $this -> fields [ 'label' ][ 'default' ]) ? $langs -> trans ( " CopyOf " ) . " " . $object -> label : $this -> fields [ 'label' ][ 'default' ];
}
if ( property_exists ( $object , 'status' )) {
$object -> status = self :: STATUS_DRAFT ;
}
if ( property_exists ( $object , 'date_creation' )) {
$object -> date_creation = dol_now ();
}
if ( property_exists ( $object , 'date_modification' )) {
$object -> date_modification = null ;
}
2020-05-01 08:40:55 +02:00
// ...
// Clear extrafields that are unique
2021-02-26 18:26:44 +01:00
if ( is_array ( $object -> array_options ) && count ( $object -> array_options ) > 0 ) {
2020-05-01 08:40:55 +02:00
$extrafields -> fetch_name_optionals_label ( $this -> table_element );
2021-02-26 18:26:44 +01:00
foreach ( $object -> array_options as $key => $option ) {
2020-05-01 08:40:55 +02:00
$shortkey = preg_replace ( '/options_/' , '' , $key );
2021-02-26 18:26:44 +01:00
if ( ! empty ( $extrafields -> attributes [ $this -> table_element ][ 'unique' ][ $shortkey ])) {
2022-05-17 14:55:38 +02:00
//var_dump($key);
//var_dump($clonedObj->array_options[$key]); exit;
2020-05-01 08:40:55 +02:00
unset ( $object -> array_options [ $key ]);
}
}
}
// Create clone
2017-08-03 01:45:31 +02:00
$object -> context [ 'createfromclone' ] = 'createfromclone' ;
2020-05-01 08:40:55 +02:00
$result = $object -> createCommon ( $user );
if ( $result < 0 ) {
$error ++ ;
2023-04-27 09:54:40 +02:00
$this -> setErrorsFromObject ( $object );
2020-05-01 08:40:55 +02:00
}
2021-02-26 18:26:44 +01:00
if ( ! $error ) {
2020-05-01 08:40:55 +02:00
// copy internal contacts
2021-02-26 18:26:44 +01:00
if ( $this -> copy_linked_contact ( $object , 'internal' ) < 0 ) {
2020-05-01 08:40:55 +02:00
$error ++ ;
}
}
2021-02-26 18:26:44 +01:00
if ( ! $error ) {
2020-05-01 08:40:55 +02:00
// copy external contacts if same company
2021-12-09 15:17:18 +01:00
if ( ! empty ( $object -> socid ) && property_exists ( $this , 'fk_soc' ) && $this -> fk_soc == $object -> socid ) {
2021-02-26 18:26:44 +01:00
if ( $this -> copy_linked_contact ( $object , 'external' ) < 0 ) {
2020-05-01 08:40:55 +02:00
$error ++ ;
2021-02-26 18:26:44 +01:00
}
2020-05-01 08:40:55 +02:00
}
}
unset ( $object -> context [ 'createfromclone' ]);
// End
if ( ! $error ) {
$this -> db -> commit ();
return $object ;
} else {
$this -> db -> rollback ();
return - 1 ;
}
2017-08-03 01:45:31 +02:00
}
2017-07-25 02:56:19 +02:00
/**
* Load object in memory from the database
*
* @ param int $id Id object
* @ param string $ref Ref
* @ return int < 0 if KO , 0 if not found , > 0 if OK
*/
public function fetch ( $id , $ref = null )
{
2017-08-03 01:45:31 +02:00
$result = $this -> fetchCommon ( $id , $ref );
2021-02-26 18:26:44 +01:00
if ( $result > 0 && ! empty ( $this -> table_element_line )) {
$this -> fetchLines ();
}
2017-08-03 01:45:31 +02:00
return $result ;
}
/**
* Load object lines in memory from the database
*
* @ return int < 0 if KO , 0 if not found , > 0 if OK
*/
2019-06-03 18:11:53 +02:00
public function fetchLines ()
2017-08-03 01:45:31 +02:00
{
2019-11-13 18:32:11 +01:00
$this -> lines = array ();
2017-08-03 01:45:31 +02:00
2019-06-03 18:11:53 +02:00
$result = $this -> fetchLinesCommon ();
return $result ;
}
2017-08-03 01:45:31 +02:00
2017-07-25 02:56:19 +02:00
/**
2018-11-20 18:29:29 +01:00
* Load list of objects in memory from the database .
*
* @ param string $sortorder Sort Order
* @ param string $sortfield Sort field
* @ param int $limit limit
* @ param int $offset Offset
2018-11-27 16:39:08 +01:00
* @ param array $filter Filter array . Example array ( 'field' => 'valueforlike' , 'customurl' =>... )
2018-11-20 18:29:29 +01:00
* @ param string $filtermode Filter mode ( AND or OR )
* @ return array | int int < 0 if KO , array of pages if OK
*/
2019-01-27 15:20:16 +01:00
public function fetchAll ( $sortorder = '' , $sortfield = '' , $limit = 0 , $offset = 0 , array $filter = array (), $filtermode = 'AND' )
2018-11-20 18:29:29 +01:00
{
global $conf ;
dol_syslog ( __METHOD__ , LOG_DEBUG );
2019-11-13 18:32:11 +01:00
$records = array ();
2018-11-20 18:29:29 +01:00
2021-09-30 15:59:47 +02:00
$sql = " SELECT " ;
2021-03-09 10:21:32 +01:00
$sql .= $this -> getFieldList ( 't' );
2022-10-07 12:02:53 +02:00
$sql .= " FROM " . $this -> db -> prefix () . $this -> table_element . " as t " ;
2021-02-26 18:26:44 +01:00
if ( isset ( $this -> ismultientitymanaged ) && $this -> ismultientitymanaged == 1 ) {
2022-06-15 14:53:44 +02:00
$sql .= " WHERE t.entity IN ( " . getEntity ( $this -> element ) . " ) " ;
2021-02-26 18:26:44 +01:00
} else {
2021-09-30 15:59:47 +02:00
$sql .= " WHERE 1 = 1 " ;
2021-02-26 18:26:44 +01:00
}
2018-11-20 18:29:29 +01:00
// Manage filter
$sqlwhere = array ();
if ( count ( $filter ) > 0 ) {
foreach ( $filter as $key => $value ) {
2019-11-13 18:32:11 +01:00
if ( $key == 't.rowid' ) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = $key . " = " . (( int ) $value );
2020-09-07 01:55:13 +02:00
} elseif ( in_array ( $this -> fields [ $key ][ 'type' ], array ( 'date' , 'datetime' , 'timestamp' ))) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = $key . " = ' " . $this -> db -> idate ( $value ) . " ' " ;
2020-05-21 09:35:30 +02:00
} elseif ( $key == 'customsql' ) {
2018-11-27 16:39:08 +01:00
$sqlwhere [] = $value ;
2020-09-07 01:55:13 +02:00
} elseif ( strpos ( $value , '%' ) === false ) {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = $key . " IN ( " . $this -> db -> sanitize ( $this -> db -> escape ( $value )) . " ) " ;
2020-05-21 09:35:30 +02:00
} else {
2021-08-27 22:42:04 +02:00
$sqlwhere [] = $key . " LIKE '% " . $this -> db -> escape ( $value ) . " %' " ;
2018-11-20 18:29:29 +01:00
}
}
}
if ( count ( $sqlwhere ) > 0 ) {
2021-08-27 22:42:04 +02:00
$sql .= " AND ( " . implode ( " " . $filtermode . " " , $sqlwhere ) . " ) " ;
2018-11-20 18:29:29 +01:00
}
if ( ! empty ( $sortfield )) {
$sql .= $this -> db -> order ( $sortfield , $sortorder );
}
if ( ! empty ( $limit )) {
2021-08-27 22:42:04 +02:00
$sql .= $this -> db -> plimit ( $limit , $offset );
2018-11-20 18:29:29 +01:00
}
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$num = $this -> db -> num_rows ( $resql );
2020-05-01 08:40:55 +02:00
$i = 0 ;
2021-02-26 18:26:44 +01:00
while ( $i < ( $limit ? min ( $limit , $num ) : $num )) {
2020-05-01 08:40:55 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2019-04-23 12:52:48 +02:00
2018-11-20 18:29:29 +01:00
$record = new self ( $this -> db );
2019-06-03 18:11:53 +02:00
$record -> setVarsFromFetchObj ( $obj );
2018-11-20 18:29:29 +01:00
$records [ $record -> id ] = $record ;
2019-04-23 12:52:48 +02:00
$i ++ ;
2018-11-20 18:29:29 +01:00
}
$this -> db -> free ( $resql );
return $records ;
} else {
2019-11-13 18:32:11 +01:00
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . join ( ',' , $this -> errors ), LOG_ERR );
2018-11-20 18:29:29 +01:00
return - 1 ;
}
}
/**
2017-07-25 02:56:19 +02:00
* Update object into database
*
* @ param User $user User that modifies
* @ param bool $notrigger false = launch triggers after , true = disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
public function update ( User $user , $notrigger = false )
{
return $this -> updateCommon ( $user , $notrigger );
}
/**
* Delete object in database
*
* @ param User $user User that deletes
2022-07-27 23:01:33 +02:00
* @ param bool $notrigger false = launch triggers , true = disable triggers
2017-07-25 02:56:19 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
public function delete ( User $user , $notrigger = false )
{
2017-10-13 13:10:36 +02:00
return $this -> deleteCommon ( $user , $notrigger );
2018-11-19 21:11:26 +01:00
//return $this->deleteCommon($user, $notrigger, 1);
2017-07-25 02:56:19 +02:00
}
2017-05-27 13:46:34 +02:00
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 deleteLine ( User $user , $idline , $notrigger = false )
{
2021-02-26 18:26:44 +01:00
if ( $this -> status < 0 ) {
2019-06-03 20:46:11 +02:00
$this -> error = 'ErrorDeleteLineNotAllowedByObjectStatus' ;
return - 2 ;
}
return $this -> deleteLineCommon ( $user , $idline , $notrigger );
}
2019-10-16 01:36:07 +02:00
2019-12-17 12:06:09 +01:00
/**
* Validate object
*
* @ param User $user User making status change
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
* @ return int <= 0 if OK , 0 = Nothing done , > 0 if KO
*/
public function validate ( $user , $notrigger = 0 )
{
global $conf , $langs ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
$error = 0 ;
// Protection
2021-02-26 18:26:44 +01:00
if ( $this -> status == self :: STATUS_VALIDATED ) {
2019-12-17 12:06:09 +01:00
dol_syslog ( get_class ( $this ) . " ::validate action abandonned: already validated " , LOG_WARNING );
return 0 ;
}
2022-08-31 22:14:20 +02:00
/* if ( ! (( empty ( $conf -> global -> MAIN_USE_ADVANCED_PERMS ) && ! empty ( $user -> rights -> mymodule -> myobject -> write ))
|| ( ! empty ( $conf -> global -> MAIN_USE_ADVANCED_PERMS ) && ! empty ( $user -> rights -> mymodule -> myobject -> myobject_advance -> validate ))))
2019-12-17 12:06:09 +01:00
{
$this -> error = 'NotEnoughPermissions' ;
dol_syslog ( get_class ( $this ) . " ::valid " . $this -> error , LOG_ERR );
return - 1 ;
} */
$now = dol_now ();
$this -> db -> begin ();
// Define new ref
2021-02-26 18:26:44 +01:00
if ( ! $error && ( preg_match ( '/^[\(]?PROV/i' , $this -> ref ) || empty ( $this -> ref ))) { // empty should not happened, but when it occurs, the test save life
2019-12-17 12:06:09 +01:00
$num = $this -> getNextNumRef ();
2020-05-21 09:35:30 +02:00
} else {
2019-12-17 12:06:09 +01:00
$num = $this -> ref ;
}
$this -> newref = $num ;
2020-04-10 10:59:32 +02:00
if ( ! empty ( $num )) {
2020-04-03 15:48:54 +02:00
// Validate
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " SET ref = ' " . $this -> db -> escape ( $num ) . " ', " ;
$sql .= " status = " . self :: STATUS_VALIDATED ;
2021-02-26 18:26:44 +01:00
if ( ! empty ( $this -> fields [ 'date_validation' ])) {
$sql .= " , date_validation = ' " . $this -> db -> idate ( $now ) . " ' " ;
}
if ( ! empty ( $this -> fields [ 'fk_user_valid' ])) {
2021-03-30 19:12:07 +02:00
$sql .= " , fk_user_valid = " . (( int ) $user -> id );
2021-02-26 18:26:44 +01:00
}
2021-03-30 19:12:07 +02:00
$sql .= " WHERE rowid = " . (( int ) $this -> id );
2020-04-03 15:48:54 +02:00
dol_syslog ( get_class ( $this ) . " ::validate() " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2021-02-26 18:26:44 +01:00
if ( ! $resql ) {
2020-04-03 15:48:54 +02:00
dol_print_error ( $this -> db );
$this -> error = $this -> db -> lasterror ();
$error ++ ;
}
2019-12-17 12:06:09 +01:00
2021-02-26 18:26:44 +01:00
if ( ! $error && ! $notrigger ) {
2020-04-03 15:48:54 +02:00
// Call trigger
$result = $this -> call_trigger ( 'MYOBJECT_VALIDATE' , $user );
2021-02-26 18:26:44 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2020-04-03 15:48:54 +02:00
// End call triggers
}
2019-12-17 12:06:09 +01:00
}
2021-02-26 18:26:44 +01:00
if ( ! $error ) {
2019-12-17 12:06:09 +01:00
$this -> oldref = $this -> ref ;
// Rename directory if dir was a temporary ref
2021-02-26 18:26:44 +01:00
if ( preg_match ( '/^[\(]?PROV/i' , $this -> ref )) {
2019-12-17 12:06:09 +01:00
// Now we rename also files into index
$sql = 'UPDATE ' . MAIN_DB_PREFIX . " ecm_files set filename = CONCAT(' " . $this -> db -> escape ( $this -> newref ) . " ', SUBSTR(filename, " . ( strlen ( $this -> ref ) + 1 ) . " )), filepath = 'myobject/ " . $this -> db -> escape ( $this -> newref ) . " ' " ;
$sql .= " WHERE filename LIKE ' " . $this -> db -> escape ( $this -> ref ) . " %' AND filepath = 'myobject/ " . $this -> db -> escape ( $this -> ref ) . " ' and entity = " . $conf -> entity ;
$resql = $this -> db -> query ( $sql );
2021-02-26 18:26:44 +01:00
if ( ! $resql ) {
$error ++ ; $this -> error = $this -> db -> lasterror ();
}
2019-12-17 12:06:09 +01:00
// We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
$oldref = dol_sanitizeFileName ( $this -> ref );
$newref = dol_sanitizeFileName ( $num );
2020-01-06 14:35:11 +01:00
$dirsource = $conf -> mymodule -> dir_output . '/myobject/' . $oldref ;
$dirdest = $conf -> mymodule -> dir_output . '/myobject/' . $newref ;
2021-02-26 18:26:44 +01:00
if ( ! $error && file_exists ( $dirsource )) {
2019-12-17 12:06:09 +01:00
dol_syslog ( get_class ( $this ) . " ::validate() rename dir " . $dirsource . " into " . $dirdest );
2021-02-26 18:26:44 +01:00
if ( @ rename ( $dirsource , $dirdest )) {
2019-12-17 12:06:09 +01:00
dol_syslog ( " Rename ok " );
// Rename docs starting with $oldref with $newref
2020-01-06 14:35:11 +01:00
$listoffiles = dol_dir_list ( $conf -> mymodule -> dir_output . '/myobject/' . $newref , 'files' , 1 , '^' . preg_quote ( $oldref , '/' ));
2021-02-26 18:26:44 +01:00
foreach ( $listoffiles as $fileentry ) {
2019-12-17 12:06:09 +01:00
$dirsource = $fileentry [ 'name' ];
$dirdest = preg_replace ( '/^' . preg_quote ( $oldref , '/' ) . '/' , $newref , $dirsource );
$dirsource = $fileentry [ 'path' ] . '/' . $dirsource ;
$dirdest = $fileentry [ 'path' ] . '/' . $dirdest ;
@ rename ( $dirsource , $dirdest );
}
}
}
}
}
// Set new ref and current status
2021-02-26 18:26:44 +01:00
if ( ! $error ) {
2019-12-17 12:06:09 +01:00
$this -> ref = $num ;
$this -> status = self :: STATUS_VALIDATED ;
}
2021-02-26 18:26:44 +01:00
if ( ! $error ) {
2019-12-17 12:06:09 +01:00
$this -> db -> commit ();
return 1 ;
2020-05-21 09:35:30 +02:00
} else {
2019-12-17 12:06:09 +01:00
$this -> db -> rollback ();
return - 1 ;
}
}
2019-10-16 01:36:07 +02:00
/**
* Set draft status
*
* @ param User $user Object user that modify
* @ param int $notrigger 1 = Does not execute triggers , 0 = Execute triggers
* @ return int < 0 if KO , > 0 if OK
*/
public function setDraft ( $user , $notrigger = 0 )
{
// Protection
2021-02-26 18:26:44 +01:00
if ( $this -> status <= self :: STATUS_DRAFT ) {
2019-10-16 01:36:07 +02:00
return 0 ;
}
2022-08-31 22:14:20 +02:00
/* if ( ! (( empty ( $conf -> global -> MAIN_USE_ADVANCED_PERMS ) && ! empty ( $user -> rights -> mymodule -> write ))
|| ( ! empty ( $conf -> global -> MAIN_USE_ADVANCED_PERMS ) && ! empty ( $user -> rights -> mymodule -> mymodule_advance -> validate ))))
2019-10-16 01:36:07 +02:00
{
$this -> error = 'Permission denied' ;
return - 1 ;
} */
2019-12-17 12:06:09 +01:00
return $this -> setStatusCommon ( $user , self :: STATUS_DRAFT , $notrigger , 'MYOBJECT_UNVALIDATE' );
2019-10-16 01:36:07 +02:00
}
/**
* Set cancel status
*
* @ param User $user Object user that modify
* @ param int $notrigger 1 = Does not execute triggers , 0 = Execute triggers
* @ return int < 0 if KO , 0 = Nothing done , > 0 if OK
*/
public function cancel ( $user , $notrigger = 0 )
{
// Protection
2021-02-26 18:26:44 +01:00
if ( $this -> status != self :: STATUS_VALIDATED ) {
2019-10-16 01:36:07 +02:00
return 0 ;
}
2022-08-31 22:14:20 +02:00
/* if ( ! (( empty ( $conf -> global -> MAIN_USE_ADVANCED_PERMS ) && ! empty ( $user -> rights -> mymodule -> write ))
|| ( ! empty ( $conf -> global -> MAIN_USE_ADVANCED_PERMS ) && ! empty ( $user -> rights -> mymodule -> mymodule_advance -> validate ))))
2019-10-16 01:36:07 +02:00
{
$this -> error = 'Permission denied' ;
return - 1 ;
} */
2021-01-02 18:05:18 +01:00
return $this -> setStatusCommon ( $user , self :: STATUS_CANCELED , $notrigger , 'MYOBJECT_CANCEL' );
2019-10-16 01:36:07 +02:00
}
/**
* Set back to validated status
*
* @ param User $user Object user that modify
* @ param int $notrigger 1 = Does not execute triggers , 0 = Execute triggers
* @ return int < 0 if KO , 0 = Nothing done , > 0 if OK
*/
public function reopen ( $user , $notrigger = 0 )
{
// Protection
2022-12-01 20:56:11 +01:00
if ( $this -> status == self :: STATUS_VALIDATED ) {
2019-10-16 01:36:07 +02:00
return 0 ;
}
2022-08-31 22:14:20 +02:00
/* if ( ! (( empty ( $conf -> global -> MAIN_USE_ADVANCED_PERMS ) && ! empty ( $user -> rights -> mymodule -> write ))
|| ( ! empty ( $conf -> global -> MAIN_USE_ADVANCED_PERMS ) && ! empty ( $user -> rights -> mymodule -> mymodule_advance -> validate ))))
2019-10-16 01:36:07 +02:00
{
$this -> error = 'Permission denied' ;
return - 1 ;
} */
2019-12-17 12:06:09 +01:00
return $this -> setStatusCommon ( $user , self :: STATUS_VALIDATED , $notrigger , 'MYOBJECT_REOPEN' );
2019-10-16 01:36:07 +02:00
}
2023-02-02 21:35:40 +01:00
/**
* getTooltipContentArray
2023-04-08 15:08:55 +02:00
*
* @ param array $params Params to construct tooltip data
* @ since v18
* @ return array
2023-02-02 21:35:40 +01:00
*/
public function getTooltipContentArray ( $params )
{
2023-04-08 15:08:55 +02:00
global $conf , $langs ;
2023-02-02 21:35:40 +01:00
$datas = [];
if ( ! empty ( $conf -> global -> MAIN_OPTIMIZEFORTEXTBROWSER )) {
return [ 'optimize' => $langs -> trans ( " ShowMyObject " )];
}
$datas [ 'picto' ] = img_picto ( '' , $this -> picto ) . ' <u>' . $langs -> trans ( " MyObject " ) . '</u>' ;
if ( isset ( $this -> status )) {
$datas [ 'picto' ] .= ' ' . $this -> getLibStatut ( 5 );
}
$datas [ 'ref' ] .= '<br><b>' . $langs -> trans ( 'Ref' ) . ':</b> ' . $this -> ref ;
return $datas ;
}
2020-05-01 08:40:55 +02:00
/**
* Return a link to the object card ( with optionaly the picto )
*
* @ param int $withpicto Include picto in link ( 0 = No picto , 1 = Include picto into link , 2 = Only picto )
* @ param string $option On what the link point to ( 'nolink' , ... )
* @ param int $notooltip 1 = Disable tooltip
* @ param string $morecss Add more css on link
* @ param int $save_lastsearch_value - 1 = Auto , 0 = No save of lastsearch_values when clicking , 1 = Save lastsearch_values whenclicking
* @ return string String with URL
*/
public function getNomUrl ( $withpicto = 0 , $option = '' , $notooltip = 0 , $morecss = '' , $save_lastsearch_value = - 1 )
{
global $conf , $langs , $hookmanager ;
2021-02-26 18:26:44 +01:00
if ( ! empty ( $conf -> dol_no_mouse_hover )) {
$notooltip = 1 ; // Force disable tooltips
}
2020-05-01 08:40:55 +02:00
$result = '' ;
2023-02-05 23:40:02 +01:00
$params = [
'id' => $this -> id ,
2023-03-27 10:00:12 +02:00
'objecttype' => $this -> element . ( $this -> module ? '@' . $this -> module : '' ),
2023-02-05 23:40:02 +01:00
'option' => $option ,
];
$classfortooltip = 'classfortooltip' ;
$dataparams = '' ;
if ( getDolGlobalInt ( 'MAIN_ENABLE_AJAX_TOOLTIP' )) {
$classfortooltip = 'classforajaxtooltip' ;
2023-04-03 19:51:40 +02:00
$dataparams = ' data-params="' . dol_escape_htmltag ( json_encode ( $params )) . '"' ;
$label = '' ;
} else {
$label = implode ( $this -> getTooltipContentArray ( $params ));
2020-05-01 08:40:55 +02:00
}
$url = dol_buildpath ( '/mymodule/myobject_card.php' , 1 ) . '?id=' . $this -> id ;
2021-02-26 18:26:44 +01:00
if ( $option != 'nolink' ) {
2020-05-01 08:40:55 +02:00
// Add param to save lastsearch_values or not
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
2021-02-26 18:26:44 +01:00
if ( $save_lastsearch_value == - 1 && preg_match ( '/list\.php/' , $_SERVER [ " PHP_SELF " ])) {
$add_save_lastsearch_values = 1 ;
}
2021-11-07 13:51:31 +01:00
if ( $url && $add_save_lastsearch_values ) {
2021-02-26 18:26:44 +01:00
$url .= '&save_lastsearch_values=1' ;
}
2020-05-01 08:40:55 +02:00
}
$linkclose = '' ;
2021-02-26 18:26:44 +01:00
if ( empty ( $notooltip )) {
if ( ! empty ( $conf -> global -> MAIN_OPTIMIZEFORTEXTBROWSER )) {
2020-05-01 08:40:55 +02:00
$label = $langs -> trans ( " ShowMyObject " );
$linkclose .= ' alt="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
}
2023-04-03 19:51:40 +02:00
$linkclose .= ( $label ? ' title="' . dol_escape_htmltag ( $label , 1 ) . '"' : ' title="tocomplete"' );
2023-02-04 21:34:48 +01:00
$linkclose .= $dataparams . ' class="' . $classfortooltip . ( $morecss ? ' ' . $morecss : '' ) . '"' ;
2021-02-26 18:26:44 +01:00
} else {
$linkclose = ( $morecss ? ' class="' . $morecss . '"' : '' );
}
2017-06-07 16:44:04 +02:00
2021-11-07 13:51:31 +01:00
if ( $option == 'nolink' || empty ( $url )) {
2021-02-17 17:56:08 +01:00
$linkstart = '<span' ;
} else {
$linkstart = '<a href="' . $url . '"' ;
}
2019-11-13 18:32:11 +01:00
$linkstart .= $linkclose . '>' ;
2021-11-07 13:51:31 +01:00
if ( $option == 'nolink' || empty ( $url )) {
2021-02-17 17:56:08 +01:00
$linkend = '</span>' ;
} else {
$linkend = '</a>' ;
}
2017-05-27 13:46:34 +02:00
2017-11-02 15:03:09 +01:00
$result .= $linkstart ;
2019-06-21 00:13:46 +02:00
2020-03-04 02:14:40 +01:00
if ( empty ( $this -> showphoto_on_popup )) {
2021-02-26 18:26:44 +01:00
if ( $withpicto ) {
2023-04-10 01:39:47 +02:00
$result .= img_object (( $notooltip ? '' : $label ), ( $this -> picto ? $this -> picto : 'generic' ), (( $withpicto != 2 ) ? 'class="paddingright"' : '' ), 0 , 0 , $notooltip ? 0 : 1 );
2021-02-26 18:26:44 +01:00
}
2020-03-04 02:14:40 +01:00
} else {
if ( $withpicto ) {
2020-03-12 12:45:44 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2020-03-04 02:14:40 +01:00
list ( $class , $module ) = explode ( '@' , $this -> picto );
2020-03-12 12:45:44 +01:00
$upload_dir = $conf -> $module -> multidir_output [ $conf -> entity ] . " / $class / " . dol_sanitizeFileName ( $this -> ref );
2020-03-04 02:14:40 +01:00
$filearray = dol_dir_list ( $upload_dir , " files " );
$filename = $filearray [ 0 ][ 'name' ];
2020-03-12 12:45:44 +01:00
if ( ! empty ( $filename )) {
2020-03-04 02:14:40 +01:00
$pospoint = strpos ( $filearray [ 0 ][ 'name' ], '.' );
2020-03-12 12:45:44 +01:00
$pathtophoto = $class . '/' . $this -> ref . '/thumbs/' . substr ( $filename , 0 , $pospoint ) . '_mini' . substr ( $filename , $pospoint );
2020-03-04 02:14:40 +01:00
if ( empty ( $conf -> global -> { strtoupper ( $module . '_' . $class ) . '_FORMATLISTPHOTOSASUSERS' })) {
2020-03-12 12:45:44 +01:00
$result .= '<div class="floatleft inline-block valignmiddle divphotoref"><div class="photoref"><img class="photo' . $module . '" alt="No photo" border="0" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $module . '&entity=' . $conf -> entity . '&file=' . urlencode ( $pathtophoto ) . '"></div></div>' ;
2020-05-21 09:35:30 +02:00
} else {
2020-03-12 12:45:44 +01:00
$result .= '<div class="floatleft inline-block valignmiddle divphotoref"><img class="photouserphoto userphoto" alt="No photo" border="0" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $module . '&entity=' . $conf -> entity . '&file=' . urlencode ( $pathtophoto ) . '"></div>' ;
2020-03-04 02:14:40 +01:00
}
$result .= '</div>' ;
2020-05-21 09:35:30 +02:00
} else {
2023-04-27 15:55:21 +02:00
$result .= img_object (( $notooltip ? '' : $label ), ( $this -> picto ? $this -> picto : 'generic' ), ( $notooltip ? (( $withpicto != 2 ) ? 'class="paddingright"' : '' ) : 'class="' . (( $withpicto != 2 ) ? 'paddingright ' : '' ) . '"' ), 0 , 0 , $notooltip ? 0 : 1 );
2020-03-04 02:14:40 +01:00
}
}
}
2020-03-02 21:15:46 +01:00
2021-02-26 18:26:44 +01:00
if ( $withpicto != 2 ) {
$result .= $this -> ref ;
}
2020-03-02 21:15:46 +01:00
2017-11-02 15:03:09 +01:00
$result .= $linkend ;
//if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
2019-11-13 18:32:11 +01:00
global $action , $hookmanager ;
2022-12-22 00:59:54 +01:00
$hookmanager -> initHooks ( array ( $this -> element . 'dao' ));
2022-02-15 18:31:05 +01:00
$parameters = array ( 'id' => $this -> id , 'getnomurl' => & $result );
2019-11-13 18:32:11 +01:00
$reshook = $hookmanager -> executeHooks ( 'getNomUrl' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
2021-02-26 18:26:44 +01:00
if ( $reshook > 0 ) {
$result = $hookmanager -> resPrint ;
} else {
$result .= $hookmanager -> resPrint ;
}
2018-06-04 12:55:55 +02:00
2017-05-27 13:46:34 +02:00
return $result ;
2020-05-01 08:40:55 +02:00
}
2017-05-27 13:46:34 +02:00
2022-12-08 11:03:35 +01:00
/**
* Return a thumb for kanban views
*
* @ param string $option Where point the link ( 0 => main card , 1 , 2 => shipment , 'nolink' => No link )
2023-01-18 22:27:52 +01:00
* @ param array $arraydata Array of data
2022-12-08 11:03:35 +01:00
* @ return string HTML Code for Kanban thumb .
*/
2023-01-18 22:27:52 +01:00
public function getKanbanView ( $option = '' , $arraydata = null )
2022-12-08 11:03:35 +01:00
{
2023-03-07 22:04:40 +01:00
global $conf , $langs ;
$selected = ( empty ( $arraydata [ 'selected' ]) ? 0 : $arraydata [ 'selected' ]);
2023-03-07 20:03:04 +01:00
2022-12-08 11:03:35 +01:00
$return = '<div class="box-flex-item box-flex-grow-zero">' ;
$return .= '<div class="info-box info-box-sm">' ;
$return .= '<span class="info-box-icon bg-infobox-action">' ;
$return .= img_picto ( '' , $this -> picto );
$return .= '</span>' ;
$return .= '<div class="info-box-content">' ;
2023-01-20 09:38:35 +01:00
$return .= '<span class="info-box-ref valignmiddle">' . ( method_exists ( $this , 'getNomUrl' ) ? $this -> getNomUrl () : $this -> ref ) . '</span>' ;
2023-03-07 20:03:04 +01:00
$return .= '<input id="cb' . $this -> id . '" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="' . $this -> id . '"' . ( $selected ? ' checked="checked"' : '' ) . '>' ;
2022-12-08 11:03:35 +01:00
if ( property_exists ( $this , 'label' )) {
2023-01-20 09:38:35 +01:00
$return .= ' <div class="inline-block opacitymedium valignmiddle tdoverflowmax100">' . $this -> label . '</div>' ;
}
if ( property_exists ( $this , 'amount' )) {
$return .= '<br>' ;
$return .= '<span class="info-box-label amount">' . price ( $this -> amount , 0 , $langs , 1 , - 1 , - 1 , $conf -> currency ) . '</span>' ;
2022-12-08 11:03:35 +01:00
}
if ( method_exists ( $this , 'getLibStatut' )) {
2023-03-01 20:10:37 +01:00
$return .= '<br><div class="info-box-status margintoponly">' . $this -> getLibStatut ( 3 ) . '</div>' ;
2022-12-08 11:03:35 +01:00
}
$return .= '</div>' ;
$return .= '</div>' ;
$return .= '</div>' ;
return $return ;
}
2021-09-18 12:09:08 +02:00
/**
* Return the label of the status
*
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto , 6 = Long label + Picto
* @ return string Label of status
*/
public function getLabelStatus ( $mode = 0 )
{
return $this -> LibStatut ( $this -> status , $mode );
}
2017-05-27 13:46:34 +02:00
/**
2020-09-15 14:45:51 +02:00
* Return the label of the status
2017-05-27 13:46:34 +02:00
*
2018-04-21 16:07:40 +02:00
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto , 6 = Long label + Picto
2017-05-27 13:46:34 +02:00
* @ return string Label of status
*/
2019-01-27 15:20:16 +01:00
public function getLibStatut ( $mode = 0 )
2017-05-27 13:46:34 +02:00
{
2018-04-21 16:07:40 +02:00
return $this -> LibStatut ( $this -> status , $mode );
2017-05-27 13:46:34 +02:00
}
2020-05-01 08:40:55 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2017-05-27 13:46:34 +02:00
/**
2023-04-08 15:08:55 +02:00
* Return the label of a given status
2017-05-27 13:46:34 +02:00
*
2018-04-21 16:07:40 +02:00
* @ param int $status Id status
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto , 6 = Long label + Picto
* @ return string Label of status
2017-05-27 13:46:34 +02:00
*/
2019-01-27 15:20:16 +01:00
public function LibStatut ( $status , $mode = 0 )
2017-05-27 13:46:34 +02:00
{
2018-09-02 18:49:23 +02:00
// phpcs:enable
2021-02-26 18:26:44 +01:00
if ( empty ( $this -> labelStatus ) || empty ( $this -> labelStatusShort )) {
2018-04-21 16:07:40 +02:00
global $langs ;
2020-07-24 11:53:44 +02:00
//$langs->load("mymodule@mymodule");
2021-10-16 19:37:57 +02:00
$this -> labelStatus [ self :: STATUS_DRAFT ] = $langs -> transnoentitiesnoconv ( 'Draft' );
$this -> labelStatus [ self :: STATUS_VALIDATED ] = $langs -> transnoentitiesnoconv ( 'Enabled' );
$this -> labelStatus [ self :: STATUS_CANCELED ] = $langs -> transnoentitiesnoconv ( 'Disabled' );
$this -> labelStatusShort [ self :: STATUS_DRAFT ] = $langs -> transnoentitiesnoconv ( 'Draft' );
$this -> labelStatusShort [ self :: STATUS_VALIDATED ] = $langs -> transnoentitiesnoconv ( 'Enabled' );
$this -> labelStatusShort [ self :: STATUS_CANCELED ] = $langs -> transnoentitiesnoconv ( 'Disabled' );
2018-04-21 16:07:40 +02:00
}
2017-05-27 13:46:34 +02:00
2019-10-24 13:09:50 +02:00
$statusType = 'status' . $status ;
2019-12-20 17:54:54 +01:00
//if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
2021-02-26 18:26:44 +01:00
if ( $status == self :: STATUS_CANCELED ) {
$statusType = 'status6' ;
}
2019-10-24 13:09:50 +02:00
2019-11-01 12:52:03 +01:00
return dolGetStatus ( $this -> labelStatus [ $status ], $this -> labelStatusShort [ $status ], '' , $statusType , $mode );
2017-05-27 13:46:34 +02:00
}
2017-07-25 12:44:55 +02:00
/**
2018-12-22 19:25:08 +01:00
* Load the info information in the object
2017-07-25 12:44:55 +02:00
*
2018-12-20 10:17:52 +01:00
* @ param int $id Id of object
2017-07-25 12:44:55 +02:00
* @ return void
*/
2018-10-31 17:30:08 +01:00
public function info ( $id )
2017-07-25 12:44:55 +02:00
{
2022-06-28 13:09:53 +02:00
$sql = " SELECT rowid, " ;
$sql .= " date_creation as datec, tms as datem, " ;
2021-09-30 15:59:47 +02:00
$sql .= " fk_user_creat, fk_user_modif " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " as t " ;
$sql .= " WHERE t.rowid = " . (( int ) $id );
2019-11-13 18:32:11 +01:00
$result = $this -> db -> query ( $sql );
2021-02-26 18:26:44 +01:00
if ( $result ) {
if ( $this -> db -> num_rows ( $result )) {
2017-07-25 12:44:55 +02:00
$obj = $this -> db -> fetch_object ( $result );
2022-06-08 20:07:16 +02:00
2017-07-25 12:44:55 +02:00
$this -> id = $obj -> rowid ;
2022-06-08 20:07:16 +02:00
2022-06-28 13:09:53 +02:00
$this -> user_creation_id = $obj -> fk_user_creat ;
$this -> user_modification_id = $obj -> fk_user_modif ;
2021-12-09 15:17:18 +01:00
if ( ! empty ( $obj -> fk_user_valid )) {
2022-06-28 13:09:53 +02:00
$this -> user_validation_id = $obj -> fk_user_valid ;
2017-07-25 12:44:55 +02:00
}
$this -> date_creation = $this -> db -> jdate ( $obj -> datec );
2022-06-28 13:09:53 +02:00
$this -> date_modification = empty ( $obj -> datem ) ? '' : $this -> db -> jdate ( $obj -> datem );
2022-06-08 20:07:16 +02:00
if ( ! empty ( $obj -> datev )) {
2022-06-28 13:09:53 +02:00
$this -> date_validation = empty ( $obj -> datev ) ? '' : $this -> db -> jdate ( $obj -> datev );
2022-06-08 20:07:16 +02:00
}
2017-07-25 12:44:55 +02:00
}
$this -> db -> free ( $result );
2020-05-21 09:35:30 +02:00
} else {
2017-07-25 12:44:55 +02:00
dol_print_error ( $this -> db );
}
}
2017-05-27 13:46:34 +02:00
/**
* Initialise object with example values
* Id must be 0 if object instance is a specimen
*
* @ return void
*/
public function initAsSpecimen ()
{
2021-06-21 19:47:43 +02:00
// Set here init that are not commonf fields
// $this->property1 = ...
// $this->property2 = ...
2017-07-12 13:25:18 +02:00
$this -> initAsSpecimenCommon ();
2017-05-27 13:46:34 +02:00
}
2019-05-19 16:21:11 +02:00
/**
* Create an array of lines
*
* @ return array | int array of lines if OK , < 0 if KO
*/
public function getLinesArray ()
{
2020-05-01 08:40:55 +02:00
$this -> lines = array ();
$objectline = new MyObjectLine ( $this -> db );
2021-09-15 20:03:38 +02:00
$result = $objectline -> fetchAll ( 'ASC' , 'position' , 0 , 0 , array ( 'customsql' => 'fk_myobject = ' . (( int ) $this -> id )));
2020-05-01 08:40:55 +02:00
2021-02-26 18:26:44 +01:00
if ( is_numeric ( $result )) {
2023-04-27 09:54:40 +02:00
$this -> setErrorsFromObject ( $objectline );
2020-05-01 08:40:55 +02:00
return $result ;
2020-05-21 09:35:30 +02:00
} else {
2020-05-01 08:40:55 +02:00
$this -> lines = $result ;
return $this -> lines ;
}
2019-05-19 16:21:11 +02:00
}
2017-08-03 11:42:22 +02:00
2020-01-07 12:39:47 +01:00
/**
* Returns the reference to the following non used object depending on the active numbering module .
*
* @ return string Object free reference
*/
public function getNextNumRef ()
{
global $langs , $conf ;
2020-07-24 11:53:44 +02:00
$langs -> load ( " mymodule@mymodule " );
2020-01-07 12:39:47 +01:00
if ( empty ( $conf -> global -> MYMODULE_MYOBJECT_ADDON )) {
2020-04-03 17:53:17 +02:00
$conf -> global -> MYMODULE_MYOBJECT_ADDON = 'mod_myobject_standard' ;
2020-01-07 12:39:47 +01:00
}
2021-02-26 18:26:44 +01:00
if ( ! empty ( $conf -> global -> MYMODULE_MYOBJECT_ADDON )) {
2020-01-07 12:39:47 +01:00
$mybool = false ;
$file = $conf -> global -> MYMODULE_MYOBJECT_ADDON . " .php " ;
$classname = $conf -> global -> MYMODULE_MYOBJECT_ADDON ;
// Include file with class
$dirmodels = array_merge ( array ( '/' ), ( array ) $conf -> modules_parts [ 'models' ]);
2021-02-26 18:26:44 +01:00
foreach ( $dirmodels as $reldir ) {
2020-01-07 12:39:47 +01:00
$dir = dol_buildpath ( $reldir . " core/modules/mymodule/ " );
// Load file with numbering class (if found)
$mybool |= @ include_once $dir . $file ;
}
2021-02-26 18:26:44 +01:00
if ( $mybool === false ) {
2020-01-07 12:39:47 +01:00
dol_print_error ( '' , " Failed to include file " . $file );
return '' ;
}
2020-04-03 15:48:54 +02:00
if ( class_exists ( $classname )) {
$obj = new $classname ();
$numref = $obj -> getNextValue ( $this );
2020-01-07 12:39:47 +01:00
2021-02-26 18:26:44 +01:00
if ( $numref != '' && $numref != '-1' ) {
2020-04-03 15:48:54 +02:00
return $numref ;
2020-05-21 09:35:30 +02:00
} else {
2020-04-03 15:48:54 +02:00
$this -> error = $obj -> error ;
//dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error);
return " " ;
}
} else {
2020-04-03 17:53:17 +02:00
print $langs -> trans ( " Error " ) . " " . $langs -> trans ( " ClassNotFound " ) . ' ' . $classname ;
2020-01-07 12:39:47 +01:00
return " " ;
}
2020-05-21 09:35:30 +02:00
} else {
2020-04-03 15:48:54 +02:00
print $langs -> trans ( " ErrorNumberingModuleNotSetup " , $this -> element );
2020-01-07 12:39:47 +01:00
return " " ;
}
}
2019-06-03 20:46:11 +02:00
/**
* Create a document onto disk according to template module .
*
* @ param string $modele Force template to use ( '' to not force )
* @ param Translate $outputlangs objet lang a utiliser pour traduction
* @ param int $hidedetails Hide details of lines
* @ param int $hidedesc Hide description
* @ param int $hideref Hide ref
* @ param null | array $moreparams Array to provide more information
* @ return int 0 if KO , 1 if OK
*/
public function generateDocument ( $modele , $outputlangs , $hidedetails = 0 , $hidedesc = 0 , $hideref = 0 , $moreparams = null )
{
2019-11-13 18:32:11 +01:00
global $conf , $langs ;
2019-06-03 20:46:11 +02:00
2020-04-03 14:02:37 +02:00
$result = 0 ;
$includedocgeneration = 0 ;
2019-06-03 20:46:11 +02:00
$langs -> load ( " mymodule@mymodule " );
2019-11-13 18:32:11 +01:00
if ( ! dol_strlen ( $modele )) {
2020-05-31 23:11:49 +02:00
$modele = 'standard_myobject' ;
2019-06-03 20:46:11 +02:00
2020-12-14 20:40:47 +01:00
if ( ! empty ( $this -> model_pdf )) {
2020-09-10 01:49:09 +02:00
$modele = $this -> model_pdf ;
2019-11-13 18:32:11 +01:00
} elseif ( ! empty ( $conf -> global -> MYOBJECT_ADDON_PDF )) {
2019-06-03 20:46:11 +02:00
$modele = $conf -> global -> MYOBJECT_ADDON_PDF ;
}
}
$modelpath = " core/modules/mymodule/doc/ " ;
2020-12-14 20:40:47 +01:00
if ( $includedocgeneration && ! empty ( $modele )) {
2020-04-03 14:02:37 +02:00
$result = $this -> commonGenerateDocument ( $modelpath , $modele , $outputlangs , $hidedetails , $hidedesc , $hideref , $moreparams );
}
return $result ;
2019-06-03 20:46:11 +02:00
}
2017-08-03 11:42:22 +02:00
/**
* Action executed by scheduler
2018-12-20 10:17:52 +01:00
* CAN BE A CRON TASK . In such a case , parameters come from the schedule job setup field 'Parameters'
2020-05-03 22:47:43 +02:00
* Use public function doScheduledJob ( $param1 , $param2 , ... ) to get parameters
2017-08-03 11:42:22 +02:00
*
* @ return int 0 if OK , <> 0 if KO ( this function is used also by cron so only 0 is OK )
*/
public function doScheduledJob ()
{
2023-02-25 20:12:48 +01:00
//global $conf, $langs;
2017-08-03 11:42:22 +02:00
2018-03-24 18:28:06 +01:00
//$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
$error = 0 ;
2017-08-04 11:25:10 +02:00
$this -> output = '' ;
2019-11-13 18:32:11 +01:00
$this -> error = '' ;
2017-08-04 11:25:10 +02:00
2017-08-03 11:42:22 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2018-03-24 18:28:06 +01:00
$now = dol_now ();
$this -> db -> begin ();
2017-08-04 11:25:10 +02:00
// ...
2017-08-03 11:42:22 +02:00
2018-03-24 18:28:06 +01:00
$this -> db -> commit ();
return $error ;
2017-08-03 11:42:22 +02:00
}
2017-05-27 13:46:34 +02:00
}
2020-07-28 02:54:37 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobjectline.class.php' ;
2017-05-27 13:46:34 +02:00
/**
2017-08-03 01:45:31 +02:00
* Class MyObjectLine . You can also remove this and generate a CRUD class for lines objects .
2017-05-27 13:46:34 +02:00
*/
2020-07-28 02:54:37 +02:00
class MyObjectLine extends CommonObjectLine
2017-05-27 13:46:34 +02:00
{
2019-06-03 18:11:53 +02:00
// To complete with content of an object MyObjectLine
2019-07-09 00:14:01 +02:00
// We should have a field rowid, fk_myobject and position
2020-07-28 02:54:37 +02:00
2020-08-17 15:53:14 +02:00
/**
* @ var int Does object support extrafields ? 0 = No , 1 = Yes
*/
public $isextrafieldmanaged = 0 ;
2020-07-28 02:54:37 +02:00
/**
* Constructor
*
* @ param DoliDb $db Database handler
*/
public function __construct ( DoliDB $db )
{
$this -> db = $db ;
}
2017-05-27 13:46:34 +02:00
}