2016-12-08 20:58:52 +00:00
< ? php
/* Copyright ( C ) 2007 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2014 - 2016 Juanjo Menent < jmenent @ 2 byte . es >
* Copyright ( C ) 2015 Florian Henry < florian . henry @ open - concept . pro >
* Copyright ( C ) 2015 Raphaël Doursenaud < rdoursenaud @ gpcsolutions . fr >
2018-04-13 13:12:36 +00:00
* Copyright ( C ) 2018 Francis Appels < francis . appels @ yahoo . com >
Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in ecm (#39557)
* Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in ecm
Replaces manual '?key='.urlencode($val).'&key2='.urlencode($val2)
string-building with dolBuildUrl($path, $params) across the ECM
directory-tree/file-manager code, for consistency with the rest of
the codebase (see htdocs/core/lib/ecm.lib.php, which already uses
this pattern for the same page) and to benefit from dolBuildUrl()'s
buildurl hook.
- core/ajax/ajaxdirtree.php: the dir_card.php edit link built from
the sql tree loop.
- ecm/dir_card.php: the edit/add-section action buttons and the two
delete confirmation URLs. Also switches the three buttons that used
to manually concatenate '&token='.newToken() to dolBuildUrl()'s own
$addtoken parameter.
- ecm/class/ecmfiles.class.php: EcmFiles::getNomUrl()'s document.php
and file_card.php URLs.
- ecm/tpl/enablefiletreeajax.tpl.php: the ajaxdirtree.php script URL
and the ajaxdirpreview.php URL. The token here intentionally stays
currentToken() (not dolBuildUrl()'s own newToken()-based
$addtoken), per the existing comment: ajaxdirtree.php has
NOTOKENRENEWAL defined, so the token must match the one already
valid on the calling page. $paramwithoutsection is a pre-built raw
query-string fragment from an external caller and is appended as-is
after the dolBuildUrl() result rather than folded into it.
Verified all five refactored URL-building expressions produce byte-
identical output to the original code for representative inputs
(including values with '/', '&' and spaces), except for query
parameter order (which has no effect) and one real, minor pre-
existing bug this incidentally fixes: the delete-section confirm URL
in dir_card.php was building '&module='.$module without urlencode(),
now correctly encoded by dolBuildUrl()/http_build_query().
Could not do a live browser check (no Chrome available for Playwright
in this environment) - verified via php -l, phpcs, and a standalone
script comparing old vs new output for each call site instead.
* Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in filemanager.tpl.php
Same refactor as the previous commit, applied to the 7 remaining
manually-concatenated URLs in core/tpl/filemanager.tpl.php (used by
the ECM/medias file manager): the delete-file/delete-section/
convert-to-webp confirm URLs, the create-directory and refresh-list
toolbar buttons (now using dolBuildUrl()'s $addtoken instead of a
manual '&token='.newToken()), the two generate-webp buttons, and the
"Root" link.
$websitekeyandpageid is kept as a helper to build the raw sub-query
string embedded once (single-encoded) as the create-directory
button's 'backtopage' value - it is not itself passed to dolBuildUrl.
Verified all 7 refactored URL-building expressions produce the same
query parameters as the original code for representative inputs
(compared as parsed, order-independent query strings, since
http_build_query() does not preserve insertion order the same way as
the original manual concatenation), including one case
(convertimgwebp confirm with sortfield/sortorder) where the original
code had a harmless but sloppy leading '?&' that dolBuildUrl() no
longer produces.
* fix
* Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in index_auto.php
Same refactor as the previous commits, applied to the 4 manually-
concatenated URLs in ecm/index_auto.php: the delete-file and
delete-section confirm URLs, the refresh-list toolbar link, and the
per-directory link in the auto-directories list.
Verified all 4 refactored URL-building expressions produce the same
query parameters as the original code for representative inputs
(including an empty-module/empty-section case for the refresh link,
and values with '/' and spaces for the others).
* Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in ecm (file_card, dir_add_card, index_medias)
Same refactor as the previous commits, applied to the remaining
manually-concatenated URLs in:
- ecm/file_card.php: the cancel and rename-file redirects, the
internal download link (document.php), the delete-file confirm URL
and the edit button.
- ecm/dir_add_card.php: the delete-section confirm URL and the delete
button (now using dolBuildUrl()'s $addtoken instead of a manual
'&token='.newToken()).
- ecm/index_medias.php: the $backtopage URL used by
core/actions_linkedfiles.inc.php after a confirm_deletefile.
Left ecm/search.php's '$param = "§ion=".urlencode($section)'
alone: it is a raw query-string fragment (starting with '&', no
leading path) passed into FormFile::list_of_documents(), not a
base+params URL build, so it does not fit the dolBuildUrl($path,
$params) shape - same reasoning as $paramwithoutsection in the
already-refactored enablefiletreeajax.tpl.php.
Verified all 8 refactored URL-building expressions produce the same
query parameters as the original code for representative inputs
(order-independent comparison, since http_build_query() does not
preserve the original insertion order).
2026-08-17 02:41:05 +00:00
* Copyright ( C ) 2019 - 2026 Frédéric France < frederic . france @ free . fr >
2026-06-11 21:44:12 +00:00
* Copyright ( C ) 2024 - 2026 MDW < mdeweerd @ users . noreply . github . com >
2016-12-08 20:58:52 +00:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 19:55:30 +00:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2016-12-08 20:58:52 +00:00
*/
/**
2019-12-15 09:02:20 +00:00
* \file htdocs / ecm / class / ecmfiles . class . php
2016-12-08 20:58:52 +00:00
* \ingroup ecm
* \brief Class to manage ECM Files ( Create / Read / Update / Delete )
*/
// Put here all includes required by your class file
2020-04-10 08:59:32 +00:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2024-11-19 02:05:06 +00:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobjectline.class.php' ;
2016-12-08 20:58:52 +00:00
/**
* Class to manage ECM files
*/
2018-04-13 13:12:36 +00:00
class EcmFiles extends CommonObject
2016-12-08 20:58:52 +00:00
{
/**
* @ var string Id to identify managed objects
*/
public $element = 'ecmfiles' ;
2018-09-02 08:43:34 +00:00
2016-12-08 20:58:52 +00:00
/**
* @ var string Name of table without prefix where object is stored
*/
public $table_element = 'ecm_files' ;
2018-09-02 08:43:34 +00:00
2018-09-05 10:21:13 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var string String with name of icon for ecmfiles . Must be the part after the 'object_' into object_myobject . png
2018-09-05 10:21:13 +00:00
*/
2021-02-15 19:13:38 +00:00
public $picto = 'folder-open' ;
2016-12-08 20:58:52 +00:00
2018-09-28 12:44:47 +00:00
/**
* hash of file content ( md5_file ( dol_osencode ( $destfull ))
2025-09-11 10:08:29 +00:00
* @ var ? string Ecm Files label , null until fetched or set
2020-06-18 13:07:43 +00:00
*/
public $label ;
2018-09-28 12:44:47 +00:00
2020-11-24 20:28:57 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var ? string hash for file sharing , empty by default ( example : getRandomPassword ( true ))
2020-11-24 20:28:57 +00:00
*/
public $share ;
2018-08-31 16:27:16 +00:00
2026-07-23 14:09:22 +00:00
/**
* @ var EcmFilesLine []
*/
public $lines = array ();
2020-11-24 20:28:57 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var ? string filename , Note : Into ecm database record , the entry never ends with . noexe
2020-11-24 20:28:57 +00:00
*/
public $filename ;
/**
2025-09-11 10:08:29 +00:00
* @ var ? string filepath
2020-11-24 20:28:57 +00:00
*/
2016-12-08 20:58:52 +00:00
public $filepath ;
2020-11-24 20:28:57 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var ? string fullpath origin
2020-11-24 20:28:57 +00:00
*/
2016-12-08 20:58:52 +00:00
public $fullpath_orig ;
2018-08-31 17:26:08 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var ? string description
2018-08-31 17:26:08 +00:00
*/
2016-12-08 20:58:52 +00:00
public $description ;
2018-08-31 17:26:08 +00:00
2020-11-24 20:28:57 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var ? string keywords
2020-11-24 20:28:57 +00:00
*/
2016-12-08 20:58:52 +00:00
public $keywords ;
2020-11-27 08:11:30 +00:00
2024-11-09 12:28:50 +00:00
/**
2024-11-10 10:16:23 +00:00
* @ var ? string content
2024-11-09 12:28:50 +00:00
*/
public $content ;
2020-11-27 08:11:30 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var ? string cover
2020-11-27 08:11:30 +00:00
*/
2016-12-08 20:58:52 +00:00
public $cover ;
2020-11-27 08:11:30 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var ? int position
2020-11-27 08:11:30 +00:00
*/
2016-12-08 20:58:52 +00:00
public $position ;
2020-11-24 20:28:57 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var 'generated' | 'uploaded' | 'unknown' | 'api' | 'copy' | '' | null
2020-11-24 20:28:57 +00:00
*/
public $gen_or_uploaded ;
/**
2025-03-06 03:56:54 +00:00
* @ var array < string , string >| string Extra parameters . Try to store here the array of parameters . Old code is sometimes storing a string .
2020-11-24 20:28:57 +00:00
*/
2016-12-08 20:58:52 +00:00
public $extraparams ;
2020-11-24 20:28:57 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var null | int | '' date create
2020-11-24 20:28:57 +00:00
*/
2016-12-08 20:58:52 +00:00
public $date_c = '' ;
2020-11-24 20:28:57 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var null | int | '' date modify
2020-11-24 20:28:57 +00:00
*/
2016-12-08 20:58:52 +00:00
public $date_m = '' ;
2018-10-10 07:23:55 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var ? int ID
2020-06-18 13:07:43 +00:00
*/
2016-12-08 20:58:52 +00:00
public $fk_user_c ;
2018-10-10 07:23:55 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var ? int ID
2020-06-18 13:07:43 +00:00
*/
2016-12-08 20:58:52 +00:00
public $fk_user_m ;
2018-10-10 07:23:55 +00:00
2020-11-24 20:28:57 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var ? string acl , null until fetched or set
2020-11-24 20:28:57 +00:00
*/
2016-12-08 20:58:52 +00:00
public $acl ;
2020-11-27 08:11:30 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var ? string src object type
2020-11-27 08:11:30 +00:00
*/
2018-04-13 13:12:36 +00:00
public $src_object_type ;
2020-11-27 08:11:30 +00:00
/**
2025-09-11 10:08:29 +00:00
* @ var ? int src object id
2020-11-27 08:11:30 +00:00
*/
2018-04-13 13:12:36 +00:00
public $src_object_id ;
2018-09-02 08:43:34 +00:00
2025-04-22 10:44:57 +00:00
/**
* @ var int ID of linked agenda event
*/
public $agenda_id ;
2020-12-27 19:43:18 +00:00
/**
* @ var int section_id ID of section = ID of EcmDirectory , directory of manual ECM ( not stored into database )
*/
public $section_id ;
2023-03-18 15:14:57 +00:00
public $fields = array (
2024-03-11 20:57:50 +00:00
'rowid' => array ( 'type' => 'integer' , 'label' => 'TechnicalID' , 'enabled' => 1 , 'position' => 1 , 'notnull' => 1 , 'visible' => 0 , 'noteditable' => 1 , 'index' => 1 , 'css' => 'left' , 'comment' => " Id " ),
'ref' => array ( 'type' => 'varchar(128)' , 'label' => 'Ref' , 'enabled' => 1 , 'position' => 20 , 'notnull' => 1 , 'visible' => - 1 , 'index' => 1 , 'searchall' => 1 , 'showoncombobox' => 1 , 'validate' => 1 , 'comment' => " contains hash from filename+filepath " ),
2024-11-09 12:28:50 +00:00
'label' => array ( 'type' => 'varchar(128)' , 'label' => 'Label' , 'enabled' => 1 , 'position' => 30 , 'notnull' => 0 , 'visible' => - 1 , 'searchall' => 1 , 'css' => 'minwidth300' , 'cssview' => 'wordbreak' , 'showoncombobox' => 2 , 'validate' => 1 , 'comment' => " contains hash of file content " ),
'share' => array ( 'type' => 'varchar(128)' , 'label' => 'Share' , 'enabled' => 1 , 'position' => 40 , 'notnull' => 0 , 'visible' => - 1 , 'searchall' => 1 , 'css' => 'minwidth300' , 'cssview' => 'wordbreak' , 'showoncombobox' => 2 , 'validate' => 1 , 'comment' => " contains hash for file sharing " ),
2024-03-11 21:54:04 +00:00
'entity' => array ( 'type' => 'integer' , 'label' => 'Entity' , 'default' => '1' , 'enabled' => 1 , 'visible' => - 2 , 'notnull' => - 1 , 'position' => 50 , 'index' => 1 ),
2024-11-09 12:28:50 +00:00
'filepath' => array ( 'type' => 'varchar(255)' , 'label' => 'FilePath' , 'enabled' => 1 , 'position' => 60 , 'notnull' => 0 , 'visible' => 0 , 'searchall' => 0 , 'css' => 'minwidth300' , 'cssview' => 'wordbreak' , 'showoncombobox' => 2 , 'validate' => 1 , 'comment' => " relative to dolibarr document dir. Example module/def " ),
'filename' => array ( 'type' => 'varchar(255)' , 'label' => 'FileName' , 'enabled' => 1 , 'position' => 70 , 'notnull' => 0 , 'visible' => 1 , 'searchall' => 1 , 'css' => 'minwidth300' , 'cssview' => 'wordbreak' , 'showoncombobox' => 2 , 'validate' => 1 , 'comment' => " file name only without any directory " ),
'src_object_type' => array ( 'type' => 'varchar(64)' , 'label' => 'SourceType' , 'enabled' => 1 , 'position' => 80 , 'notnull' => 0 , 'visible' => 0 , 'searchall' => 1 , 'css' => 'minwidth300' , 'cssview' => 'wordbreak' , 'showoncombobox' => 2 , 'validate' => 1 , 'comment' => " Source object type ('proposal', 'invoice', ...) " ),
2024-03-11 21:54:04 +00:00
'src_object_id' => array ( 'type' => 'integer' , 'label' => 'SourceID' , 'default' => '1' , 'enabled' => 1 , 'visible' => 0 , 'notnull' => 1 , 'position' => 90 , 'index' => 1 , 'comment' => " Source object id " ),
2024-11-09 12:28:50 +00:00
'fullpath_orig' => array ( 'type' => 'varchar(750)' , 'label' => 'FullPathOrig' , 'enabled' => 1 , 'position' => 100 , 'notnull' => 0 , 'visible' => 0 , 'searchall' => 0 , 'css' => 'minwidth300' , 'cssview' => 'wordbreak' , 'showoncombobox' => 2 , 'validate' => 1 , 'comment' => " full path of original filename, when file is uploaded from a local computer " ),
2023-03-18 15:14:57 +00:00
'description' => array ( 'type' => 'text' , 'label' => 'Description' , 'enabled' => 1 , 'visible' => 0 , 'position' => 110 ),
2024-11-09 12:28:50 +00:00
'keywords' => array ( 'type' => 'varchar(750)' , 'label' => 'Keywords' , 'enabled' => 1 , 'position' => 120 , 'notnull' => 0 , 'visible' => 1 , 'searchall' => 1 , 'css' => 'minwidth300' , 'cssview' => 'wordbreak' , 'showoncombobox' => 2 , 'validate' => 1 , 'comment' => " list of keywords, separated with comma. Must be limited to most important keywords. " ),
2024-11-12 23:01:18 +00:00
'content' => array ( 'type' => 'html' , 'label' => 'Content' , 'enabled' => 'getDolGlobalString("MAIN_SAVE_FILE_CONTENT_AS_TEXT")' , 'position' => 120 , 'notnull' => 0 , 'visible' => 1 , 'searchall' => 1 , 'css' => 'minwidth300' , 'cssview' => 'wordbreak' , 'csslist' => 'tdoverflowmax200' , 'help' => " Text content of file " , 'showoncombobox' => 2 , 'validate' => 1 , 'comment' => " Text content if option to store txt content was set. " ),
2024-03-11 11:55:55 +00:00
'cover' => array ( 'type' => 'text' , 'label' => 'Cover' , 'enabled' => 1 , 'visible' => 0 , 'position' => 130 , 'comment' => " is this file a file to use for a cover " ),
2024-03-11 21:54:04 +00:00
'position' => array ( 'type' => 'integer' , 'label' => 'Position' , 'default' => '1' , 'enabled' => 1 , 'visible' => - 2 , 'notnull' => 1 , 'position' => 140 , 'index' => 1 , 'comment' => " position of file among others " ),
2024-11-09 12:28:50 +00:00
'gen_or_uploaded' => array ( 'type' => 'varchar(12)' , 'label' => 'GenOrUpload' , 'enabled' => 1 , 'position' => 150 , 'notnull' => 0 , 'visible' => - 1 , 'searchall' => 1 , 'css' => 'minwidth300' , 'cssview' => 'wordbreak' , 'showoncombobox' => 2 , 'validate' => 1 , 'comment' => " 'generated' or 'uploaded' " ),
'extraparams' => array ( 'type' => 'varchar(255)' , 'label' => 'ExtraParams' , 'enabled' => 1 , 'position' => 160 , 'notnull' => 0 , 'visible' => 1 , 'searchall' => 1 , 'css' => 'minwidth300' , 'cssview' => 'wordbreak' , 'showoncombobox' => 2 , 'validate' => 1 , 'comment' => " for stocking other parameters with json format " ),
2023-03-18 15:14:57 +00:00
'date_c' => array ( 'type' => 'datetime' , 'label' => 'DateCreation' , 'enabled' => 1 , 'visible' => - 1 , 'position' => 170 ),
'tms' => array ( 'type' => 'timestamp' , 'label' => 'DateModification' , 'enabled' => 1 , 'visible' => - 1 , 'notnull' => 1 , 'position' => 175 ),
2024-03-11 20:57:50 +00:00
'fk_user_c' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'UserAuthor' , 'enabled' => 1 , 'position' => 510 , 'notnull' => 1 , 'visible' => - 2 , 'foreignkey' => 'user.rowid' ,),
'fk_user_m' => array ( 'type' => 'integer:User:user/class/user.class.php' , 'label' => 'UserModif' , 'enabled' => 1 , 'position' => 511 , 'notnull' => - 1 , 'visible' => - 2 ,),
2023-03-18 15:14:57 +00:00
'note_public' => array ( 'type' => 'text' , 'label' => 'NotePublic' , 'enabled' => 1 , 'visible' => 0 , 'position' => 155 ),
'note_private' => array ( 'type' => 'text' , 'label' => 'NotePrivate' , 'enabled' => 1 , 'visible' => 0 , 'position' => 160 ),
'acl' => array ( 'type' => 'text' , 'label' => 'NotePrivate' , 'enabled' => 1 , 'visible' => 0 , 'position' => 160 , 'comment' => " for future permission 'per file' " ),
2025-04-22 10:44:57 +00:00
'agenda_id' => array ( 'type' => 'integer' , 'label' => 'IdAgenda' , 'enabled' => 1 , 'visible' => 0 , 'position' => 180 , 'comment' => " Link to an actioncomm " ),
2023-03-18 15:14:57 +00:00
);
2020-12-27 19:43:18 +00:00
2016-12-08 20:58:52 +00:00
/**
* Constructor
*
2024-01-13 14:50:02 +00:00
* @ param DoliDB $db Database handler
2016-12-08 20:58:52 +00:00
*/
public function __construct ( DoliDB $db )
{
$this -> db = $db ;
}
/**
* Create object into database
*
2017-10-05 22:26:24 +00:00
* @ param User $user User that creates
2024-01-12 18:48:18 +00:00
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
2023-12-01 18:51:32 +00:00
* @ return int Return integer < 0 if KO , Id of created object if OK
2016-12-08 20:58:52 +00:00
*/
2024-01-12 17:07:53 +00:00
public function create ( User $user , $notrigger = 0 )
2016-12-08 20:58:52 +00:00
{
2017-10-07 11:09:31 +00:00
global $conf ;
2017-07-05 09:24:24 +00:00
2016-12-08 20:58:52 +00:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
$error = 0 ;
// Clean parameters
2017-09-21 12:02:42 +00:00
if ( isset ( $this -> ref )) {
2020-06-18 13:07:43 +00:00
$this -> ref = trim ( $this -> ref );
2017-09-21 12:02:42 +00:00
}
2016-12-08 20:58:52 +00:00
if ( isset ( $this -> label )) {
2020-06-18 13:07:43 +00:00
$this -> label = trim ( $this -> label );
2016-12-08 20:58:52 +00:00
}
2017-09-21 12:02:42 +00:00
if ( isset ( $this -> share )) {
2020-06-18 13:07:43 +00:00
$this -> share = trim ( $this -> share );
2017-09-21 12:02:42 +00:00
}
2016-12-08 20:58:52 +00:00
if ( isset ( $this -> entity )) {
2020-11-24 20:28:57 +00:00
$this -> entity = ( int ) $this -> entity ;
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> filename )) {
2020-06-18 13:07:43 +00:00
$this -> filename = preg_replace ( '/\.noexe$/' , '' , trim ( $this -> filename ));
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> filepath )) {
2020-06-18 13:07:43 +00:00
$this -> filepath = trim ( $this -> filepath );
$this -> filepath = preg_replace ( '/[\\/]+$/' , '' , $this -> filepath ); // Remove last /
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> fullpath_orig )) {
2020-06-18 13:07:43 +00:00
$this -> fullpath_orig = trim ( $this -> fullpath_orig );
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> description )) {
2020-06-18 13:07:43 +00:00
$this -> description = trim ( $this -> description );
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> keywords )) {
2020-06-18 13:07:43 +00:00
$this -> keywords = trim ( $this -> keywords );
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> cover )) {
2020-06-18 13:07:43 +00:00
$this -> cover = trim ( $this -> cover );
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> gen_or_uploaded )) {
2020-06-18 13:07:43 +00:00
$this -> gen_or_uploaded = trim ( $this -> gen_or_uploaded );
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> fk_user_c )) {
2020-11-24 20:28:57 +00:00
$this -> fk_user_c = ( int ) $this -> fk_user_c ;
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> fk_user_m )) {
2020-11-24 20:28:57 +00:00
$this -> fk_user_m = ( int ) $this -> fk_user_m ;
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> acl )) {
2020-06-18 13:07:43 +00:00
$this -> acl = trim ( $this -> acl );
2016-12-08 20:58:52 +00:00
}
2018-04-13 13:12:36 +00:00
if ( isset ( $this -> src_object_type )) {
$this -> src_object_type = trim ( $this -> src_object_type );
}
2020-11-24 20:28:57 +00:00
if ( empty ( $this -> date_c )) {
$this -> date_c = dol_now ();
}
if ( empty ( $this -> date_m )) {
$this -> date_m = dol_now ();
}
2017-07-05 09:24:24 +00:00
2017-10-07 11:09:31 +00:00
// If ref not defined
2023-01-11 15:49:59 +00:00
if ( empty ( $this -> ref )) {
2018-12-09 23:19:46 +00:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/security.lib.php' ;
2024-09-05 14:05:37 +00:00
$this -> ref = dol_hash ( $this -> filepath . '/' . $this -> filename , '3' );
2018-12-07 18:35:38 +00:00
}
2017-09-21 12:02:42 +00:00
2020-04-10 08:59:32 +00:00
$maxposition = 0 ;
2020-11-24 20:28:57 +00:00
if ( empty ( $this -> position )) {
// Get max used
2020-04-10 08:59:32 +00:00
$sql = " SELECT MAX(position) as maxposition FROM " . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= " WHERE filepath =' " . $this -> db -> escape ( $this -> filepath ) . " ' " ;
2017-07-05 09:24:24 +00:00
2017-10-07 11:09:31 +00:00
$resql = $this -> db -> query ( $sql );
2020-11-27 08:11:30 +00:00
if ( $resql ) {
2017-10-07 11:09:31 +00:00
$obj = $this -> db -> fetch_object ( $resql );
$maxposition = ( int ) $obj -> maxposition ;
2020-05-21 13:05:19 +00:00
} else {
2020-04-10 08:59:32 +00:00
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
2018-04-13 13:12:36 +00:00
return -- $error ;
}
2024-08-07 00:53:45 +00:00
$maxposition += 1 ;
2020-05-21 13:05:19 +00:00
} else {
2020-04-10 08:59:32 +00:00
$maxposition = $this -> position ;
2016-12-12 23:15:21 +00:00
}
2016-12-08 20:58:52 +00:00
// Check parameters
2020-11-27 08:11:30 +00:00
if ( empty ( $this -> filename ) || empty ( $this -> filepath )) {
2017-10-29 23:45:47 +00:00
$this -> errors [] = 'Bad property filename or filepath' ;
2018-04-13 13:12:36 +00:00
return -- $error ;
}
2020-11-27 08:11:30 +00:00
if ( ! isset ( $this -> entity )) {
2018-04-13 13:12:36 +00:00
$this -> entity = $conf -> entity ;
2017-10-29 23:45:47 +00:00
}
2025-03-06 03:56:54 +00:00
$extraparams = ( ! empty ( $this -> extraparams ) ? json_encode ( $this -> extraparams ) : null );
$extraparams = dol_trunc ( $extraparams , 250 );
2016-12-08 20:58:52 +00:00
// Put here code to add control on parameters values
2025-04-22 10:44:57 +00:00
if ( ! empty ( $this -> agenda_id )) {
$this -> agenda_id = ( int ) $this -> agenda_id ;
}
2016-12-08 20:58:52 +00:00
// Insert request
2020-04-10 08:59:32 +00:00
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this -> table_element . '(' ;
$sql .= 'ref,' ;
$sql .= 'label,' ;
$sql .= 'share,' ;
$sql .= 'entity,' ;
$sql .= 'filename,' ;
$sql .= 'filepath,' ;
$sql .= 'fullpath_orig,' ;
$sql .= 'description,' ;
$sql .= 'keywords,' ;
2024-11-12 23:01:18 +00:00
if ( getDolGlobalString ( " MAIN_SAVE_FILE_CONTENT_AS_TEXT " )) {
2024-11-09 12:28:50 +00:00
$sql .= 'content,' ;
}
2020-04-10 08:59:32 +00:00
$sql .= 'cover,' ;
$sql .= 'position,' ;
$sql .= 'gen_or_uploaded,' ;
$sql .= 'extraparams,' ;
$sql .= 'date_c,' ;
2020-08-23 13:15:47 +00:00
$sql .= 'tms,' ;
2020-04-10 08:59:32 +00:00
$sql .= 'fk_user_c,' ;
$sql .= 'fk_user_m,' ;
$sql .= 'acl,' ;
$sql .= 'src_object_type,' ;
2025-04-22 10:44:57 +00:00
$sql .= 'src_object_id,' ;
$sql .= 'agenda_id' ;
2016-12-08 20:58:52 +00:00
$sql .= ') VALUES (' ;
2023-01-11 15:49:59 +00:00
$sql .= " ' " . $this -> db -> escape ( $this -> ref ) . " ', " ;
2020-04-10 08:59:32 +00:00
$sql .= ' ' . ( ! isset ( $this -> label ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> label ) . " ' " ) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> share ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> share ) . " ' " ) . ',' ;
2021-08-27 23:45:53 +00:00
$sql .= ' ' . (( int ) $this -> entity ) . ',' ;
2020-04-10 08:59:32 +00:00
$sql .= ' ' . ( ! isset ( $this -> filename ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> filename ) . " ' " ) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> filepath ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> filepath ) . " ' " ) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> fullpath_orig ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> fullpath_orig ) . " ' " ) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> description ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> description ) . " ' " ) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> keywords ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> keywords ) . " ' " ) . ',' ;
2024-11-12 23:01:18 +00:00
if ( getDolGlobalString ( " MAIN_SAVE_FILE_CONTENT_AS_TEXT " )) {
2024-11-09 12:28:50 +00:00
$sql .= ' ' . ( ! isset ( $this -> content ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> content ) . " ' " ) . ',' ;
}
2020-04-10 08:59:32 +00:00
$sql .= ' ' . ( ! isset ( $this -> cover ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> cover ) . " ' " ) . ',' ;
2021-08-27 23:45:53 +00:00
$sql .= ' ' . (( int ) $maxposition ) . ',' ;
2020-04-10 08:59:32 +00:00
$sql .= ' ' . ( ! isset ( $this -> gen_or_uploaded ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> gen_or_uploaded ) . " ' " ) . ',' ;
2025-03-06 03:56:54 +00:00
$sql .= ' ' . ( ! isset ( $extraparams ) ? 'NULL' : " ' " . $this -> db -> escape ( $extraparams ) . " ' " ) . ',' ;
2021-08-27 23:45:53 +00:00
$sql .= " ' " . $this -> db -> idate ( $this -> date_c ) . " ', " ;
2024-09-13 15:40:03 +00:00
$sql .= ' ' . ( ! isset ( $this -> date_m ) || dol_strlen (( string ) $this -> date_m ) == 0 ? 'NULL' : " ' " . $this -> db -> idate ( $this -> date_m ) . " ' " ) . ',' ;
2026-06-11 21:44:12 +00:00
$sql .= ' ' . ( ! isset ( $this -> fk_user_c ) ? (( int ) $user -> id ) : (( int ) $this -> fk_user_c )) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> fk_user_m ) ? 'NULL' : (( int ) $this -> fk_user_m )) . ',' ;
2020-04-10 08:59:32 +00:00
$sql .= ' ' . ( ! isset ( $this -> acl ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> acl ) . " ' " ) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> src_object_type ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> src_object_type ) . " ' " ) . ',' ;
2026-06-11 21:44:12 +00:00
$sql .= ' ' . ( ! isset ( $this -> src_object_id ) ? 'NULL' : (( int ) $this -> src_object_id )) . ',' ;
$sql .= ' ' . ( empty ( $this -> agenda_id ) ? 'NULL' : (( int ) $this -> agenda_id ));
2016-12-08 20:58:52 +00:00
$sql .= ')' ;
$this -> db -> begin ();
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2020-04-10 08:59:32 +00:00
$error ++ ;
2022-03-07 12:21:32 +00:00
if ( $this -> db -> lasterrno () == 'DB_ERROR_RECORD_ALREADY_EXISTS' ) {
$this -> errors [] = 'Error DB_ERROR_RECORD_ALREADY_EXISTS : ' . $this -> db -> lasterror ();
} else {
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
}
2020-04-10 08:59:32 +00:00
dol_syslog ( __METHOD__ . ' ' . implode ( ',' , $this -> errors ), LOG_ERR );
2016-12-08 20:58:52 +00:00
}
if ( ! $error ) {
2020-04-10 08:59:32 +00:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . $this -> table_element );
2017-10-07 11:09:31 +00:00
$this -> position = $maxposition ;
2017-07-05 09:24:24 +00:00
2018-04-13 13:12:36 +00:00
// Triggers
2020-11-27 08:11:30 +00:00
if ( ! $notrigger ) {
2018-04-13 13:12:36 +00:00
// Call triggers
2020-04-10 08:59:32 +00:00
$result = $this -> call_trigger ( strtoupper ( get_class ( $this )) . '_CREATE' , $user );
2020-11-27 08:11:30 +00:00
if ( $result < 0 ) {
$error ++ ;
}
2018-04-13 13:12:36 +00:00
// End call triggers
2016-12-08 20:58:52 +00:00
}
}
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
2020-04-10 08:59:32 +00:00
return - 1 * $error ;
2016-12-08 20:58:52 +00:00
} else {
$this -> db -> commit ();
return $this -> id ;
}
}
/**
* Load object in memory from the database
*
2025-11-11 07:56:33 +00:00
* @ param int $id Id object
* @ param string $ref Hash of file name ( filename + filepath ) . Not always defined on some version .
* @ param string $relativepath Relative path of file from document directory . Example : 'path/path2/file' or 'path/path2/*'
* @ param string $hashoffile Hash of file content . Take the first one found if same file is at different places . This hash will also change if file content is changed .
* @ param string $hashforshare Hash of file sharing , or 'shared'
* @ param string $src_object_type src_object_type to search ( value of object -> table_element )
* @ param int $src_object_id src_object_id to search
* @ param int | null $entity entity ( it ' s null by default to avoid problem with entity = 0 )
* @ return int Return integer < 0 if KO , 0 if not found , > 0 if OK
2016-12-08 20:58:52 +00:00
*/
2025-11-11 07:56:33 +00:00
public function fetch ( $id , $ref = '' , $relativepath = '' , $hashoffile = '' , $hashforshare = '' , $src_object_type = '' , $src_object_id = 0 , $entity = null )
2016-12-08 20:58:52 +00:00
{
2018-07-10 10:00:09 +00:00
global $conf ;
2016-12-08 20:58:52 +00:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
$sql = 'SELECT' ;
$sql .= ' t.rowid,' ;
2017-01-03 10:06:24 +00:00
$sql .= " t.ref, " ;
2016-12-08 20:58:52 +00:00
$sql .= " t.label, " ;
2017-09-21 12:02:42 +00:00
$sql .= " t.share, " ;
2016-12-08 20:58:52 +00:00
$sql .= " t.entity, " ;
$sql .= " t.filename, " ;
$sql .= " t.filepath, " ;
$sql .= " t.fullpath_orig, " ;
$sql .= " t.description, " ;
$sql .= " t.keywords, " ;
2024-11-12 23:01:18 +00:00
if ( getDolGlobalString ( " MAIN_SAVE_FILE_CONTENT_AS_TEXT " )) {
2024-11-09 12:28:50 +00:00
$sql .= " t.content, " ;
}
2016-12-08 20:58:52 +00:00
$sql .= " t.cover, " ;
$sql .= " t.position, " ;
$sql .= " t.gen_or_uploaded, " ;
$sql .= " t.extraparams, " ;
$sql .= " t.date_c, " ;
2020-08-23 13:15:47 +00:00
$sql .= " t.tms as date_m, " ;
2016-12-08 20:58:52 +00:00
$sql .= " t.fk_user_c, " ;
$sql .= " t.fk_user_m, " ;
2020-11-24 17:54:59 +00:00
$sql .= ' t.note_private,' ;
$sql .= ' t.note_public,' ;
2018-04-13 13:12:36 +00:00
$sql .= " t.acl, " ;
$sql .= " t.src_object_type, " ;
2025-04-22 10:44:57 +00:00
$sql .= " t.src_object_id, " ;
$sql .= " t.agenda_id " ;
2020-04-10 08:59:32 +00:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element . ' as t' ;
$sql .= ' WHERE 1 = 1' ;
2020-05-27 11:12:18 +00:00
/* Fetching this table depends on filepath + filename , it must not depends on entity because filesystem on disk does not know what is Dolibarr entities
2022-08-28 12:08:27 +00:00
if ( isModEnabled ( 'multicompany' )) {
2020-06-18 13:07:43 +00:00
$sql .= " AND entity IN ( " . getEntity ( 'ecmfiles' ) . " ) " ;
} */
2023-10-19 11:11:44 +00:00
$filterfound = 0 ;
2017-08-18 21:16:40 +00:00
if ( $relativepath ) {
2020-10-31 13:32:18 +00:00
$relativepathwithnoexe = preg_replace ( '/\.noexe$/' , '' , $relativepath ); // We must never have the .noexe into the database
2020-06-18 13:07:43 +00:00
$sql .= " AND t.filepath = ' " . $this -> db -> escape ( dirname ( $relativepath )) . " ' " ;
$filename = basename ( $relativepathwithnoexe );
if ( $filename != '*' ) {
$sql .= " AND t.filename = ' " . $this -> db -> escape ( $filename ) . " ' " ;
}
2025-11-11 07:56:33 +00:00
if ( isset ( $entity )) {
2025-09-10 08:59:09 +00:00
$sql .= " AND t.entity = " . ( int ) $entity ;
} else {
2026-07-20 00:50:44 +00:00
$sql .= " AND t.entity = " . (( int ) $conf -> entity ); // unique key include the entity so each company has its own index
2025-09-10 08:59:09 +00:00
}
2023-10-19 11:11:44 +00:00
$filterfound ++ ;
2023-10-19 11:07:36 +00:00
}
if ( ! empty ( $ref )) { // hash of file path
2017-01-03 10:06:24 +00:00
$sql .= " AND t.ref = ' " . $this -> db -> escape ( $ref ) . " ' " ;
2025-11-11 07:56:33 +00:00
if ( isset ( $entity )) {
2025-09-10 08:59:09 +00:00
$sql .= " AND t.entity = " . ( int ) $entity ;
} else {
2026-07-20 00:50:44 +00:00
$sql .= " AND t.entity = " . (( int ) $conf -> entity ); // unique key include the entity so each company has its own index
2025-09-10 08:59:09 +00:00
}
2023-10-19 11:11:44 +00:00
$filterfound ++ ;
2023-10-19 11:07:36 +00:00
}
if ( ! empty ( $hashoffile )) { // hash of content
2017-09-21 10:48:07 +00:00
$sql .= " AND t.label = ' " . $this -> db -> escape ( $hashoffile ) . " ' " ;
2025-11-11 07:56:33 +00:00
if ( isset ( $entity )) {
2025-09-10 08:59:09 +00:00
$sql .= " AND t.entity = " . ( int ) $entity ;
} else {
2026-07-20 00:50:44 +00:00
$sql .= " AND t.entity = " . (( int ) $conf -> entity ); // unique key include the entity so each company has its own index
2025-09-10 08:59:09 +00:00
}
2023-10-19 11:11:44 +00:00
$filterfound ++ ;
2023-10-19 11:07:36 +00:00
}
if ( ! empty ( $hashforshare )) {
2022-03-14 15:20:57 +00:00
if ( $hashforshare != 'shared' ) {
$sql .= " AND t.share = ' " . $this -> db -> escape ( $hashforshare ) . " ' " ;
} else {
$sql .= " AND t.share IS NOT NULL AND t.share <> '' " ;
}
2026-07-20 00:50:44 +00:00
//$sql .= " AND t.entity = ".((int) $conf->entity); // hashforshare already unique
2023-10-19 11:11:44 +00:00
$filterfound ++ ;
2023-10-19 11:07:36 +00:00
}
if ( $src_object_type && $src_object_id ) {
2021-06-09 13:36:47 +00:00
$sql .= " AND t.src_object_type = ' " . $this -> db -> escape ( $src_object_type ) . " ' AND t.src_object_id = " . (( int ) $src_object_id );
2025-11-11 07:56:33 +00:00
if ( isset ( $entity )) {
2025-09-10 08:59:09 +00:00
$sql .= " AND t.entity = " . ( int ) $entity ;
} else {
2026-07-20 00:50:44 +00:00
$sql .= " AND t.entity = " . (( int ) $conf -> entity ); // unique key include the entity so each company has its own index
2025-09-10 08:59:09 +00:00
}
2023-10-19 11:11:44 +00:00
$filterfound ++ ;
2023-10-19 11:07:36 +00:00
}
2023-10-19 11:11:44 +00:00
if ( $id > 0 || empty ( $filterfound )) {
2021-04-26 17:12:23 +00:00
$sql .= ' AND t.rowid = ' . (( int ) $id ); // rowid already unique
2016-12-08 20:58:52 +00:00
}
2018-08-13 15:26:32 +00:00
2023-10-19 11:07:36 +00:00
// Warning: May return several record, and only first one is returned !
$this -> db -> plimit ( 1 ); // When we search on src, or on hash of content (hashforfile), we take first one only
2017-09-21 12:02:42 +00:00
$this -> db -> order ( 't.rowid' , 'ASC' );
2016-12-08 20:58:52 +00:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$numrows = $this -> db -> num_rows ( $resql );
if ( $numrows ) {
$obj = $this -> db -> fetch_object ( $resql );
$this -> id = $obj -> rowid ;
2017-01-03 10:06:24 +00:00
$this -> ref = $obj -> ref ;
2016-12-08 20:58:52 +00:00
$this -> label = $obj -> label ;
2017-09-21 12:02:42 +00:00
$this -> share = $obj -> share ;
2016-12-08 20:58:52 +00:00
$this -> entity = $obj -> entity ;
$this -> filename = $obj -> filename ;
$this -> filepath = $obj -> filepath ;
$this -> fullpath_orig = $obj -> fullpath_orig ;
$this -> description = $obj -> description ;
$this -> keywords = $obj -> keywords ;
2024-11-12 23:01:18 +00:00
if ( getDolGlobalString ( " MAIN_SAVE_FILE_CONTENT_AS_TEXT " )) {
2024-11-09 12:28:50 +00:00
$this -> content = $obj -> content ;
}
2016-12-08 20:58:52 +00:00
$this -> cover = $obj -> cover ;
$this -> position = $obj -> position ;
$this -> gen_or_uploaded = $obj -> gen_or_uploaded ;
$this -> date_c = $this -> db -> jdate ( $obj -> date_c );
$this -> date_m = $this -> db -> jdate ( $obj -> date_m );
$this -> fk_user_c = $obj -> fk_user_c ;
$this -> fk_user_m = $obj -> fk_user_m ;
2020-11-24 17:54:59 +00:00
$this -> note_private = $obj -> note_private ;
$this -> note_public = $obj -> note_public ;
2016-12-08 20:58:52 +00:00
$this -> acl = $obj -> acl ;
2018-04-13 13:12:36 +00:00
$this -> src_object_type = $obj -> src_object_type ;
$this -> src_object_id = $obj -> src_object_id ;
2025-04-22 10:44:57 +00:00
$this -> agenda_id = $obj -> agenda_id ;
2025-03-06 10:34:27 +00:00
$this -> extraparams = ( isset ( $obj -> extraparams ) ? ( array ) json_decode ( $obj -> extraparams , true ) : null );
2016-12-08 20:58:52 +00:00
}
2017-07-05 09:24:24 +00:00
2020-10-01 22:21:13 +00:00
// Retrieve all extrafields for ecm_files
2016-12-08 20:58:52 +00:00
// fetch optionals attributes and labels
2020-10-01 22:21:13 +00:00
$this -> fetch_optionals ();
2018-04-20 12:50:11 +00:00
2016-12-08 20:58:52 +00:00
// $this->fetch_lines();
2017-07-05 09:24:24 +00:00
2016-12-08 20:58:52 +00:00
$this -> db -> free ( $resql );
if ( $numrows ) {
return 1 ;
} else {
return 0 ;
}
} else {
2020-04-10 08:59:32 +00:00
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . implode ( ',' , $this -> errors ), LOG_ERR );
2016-12-08 20:58:52 +00:00
2017-10-29 23:45:47 +00:00
return - 1 ;
2016-12-08 20:58:52 +00:00
}
}
/**
* Load object in memory from the database
*
2024-03-11 10:23:34 +00:00
* @ param string $sortorder Sort Order
* @ param string $sortfield Sort field
* @ param int $limit Limit
* @ param int $offset Offset limit
2024-11-06 22:57:45 +00:00
* @ param string | array < string , mixed > $filter filter array
2024-03-11 10:23:34 +00:00
* @ param string $filtermode filter mode ( AND or OR )
* @ return int Return integer < 0 if KO , > 0 if OK
2016-12-08 20:58:52 +00:00
*/
2024-03-11 10:23:34 +00:00
public function fetchAll ( $sortorder = '' , $sortfield = '' , $limit = 0 , $offset = 0 , $filter = '' , $filtermode = 'AND' )
2016-12-08 20:58:52 +00:00
{
dol_syslog ( __METHOD__ , LOG_DEBUG );
$sql = 'SELECT' ;
$sql .= ' t.rowid,' ;
$sql .= " t.label, " ;
2017-09-21 12:02:42 +00:00
$sql .= " t.share, " ;
2016-12-08 20:58:52 +00:00
$sql .= " t.entity, " ;
$sql .= " t.filename, " ;
$sql .= " t.filepath, " ;
$sql .= " t.fullpath_orig, " ;
$sql .= " t.description, " ;
$sql .= " t.keywords, " ;
2024-11-12 23:01:18 +00:00
if ( getDolGlobalString ( " MAIN_SAVE_FILE_CONTENT_AS_TEXT " )) {
2024-11-09 12:28:50 +00:00
$sql .= " t.content, " ;
}
2016-12-08 20:58:52 +00:00
$sql .= " t.cover, " ;
$sql .= " t.position, " ;
$sql .= " t.gen_or_uploaded, " ;
$sql .= " t.extraparams, " ;
$sql .= " t.date_c, " ;
2020-08-23 13:15:47 +00:00
$sql .= " t.tms as date_m, " ;
2016-12-08 20:58:52 +00:00
$sql .= " t.fk_user_c, " ;
$sql .= " t.fk_user_m, " ;
2018-04-13 13:12:36 +00:00
$sql .= " t.acl, " ;
$sql .= " t.src_object_type, " ;
2025-04-22 10:44:57 +00:00
$sql .= " t.src_object_id, " ;
$sql .= " t.agenda_id " ;
2020-04-10 08:59:32 +00:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element . ' as t' ;
2024-03-11 10:23:34 +00:00
$sql .= ' WHERE 1 = 1' ;
2016-12-08 20:58:52 +00:00
// Manage filter
2024-03-11 10:23:34 +00:00
if ( is_array ( $filter )) {
$sqlwhere = array ();
if ( count ( $filter ) > 0 ) {
foreach ( $filter as $key => $value ) {
if ( $key == 't.src_object_id' ) {
$sqlwhere [] = $this -> db -> sanitize ( $key ) . " = " . (( int ) $value );
} else {
$sqlwhere [] = $this -> db -> sanitize ( $key ) . " LIKE '% " . $this -> db -> escape ( $this -> db -> escapeforlike ( $value )) . " %' " ;
}
2021-05-08 20:03:08 +00:00
}
2016-12-08 20:58:52 +00:00
}
2024-03-11 10:23:34 +00:00
if ( count ( $sqlwhere ) > 0 ) {
2026-08-04 16:40:04 +00:00
$sql .= ' AND ' . implode ( ' ' . $this -> db -> sanitize ( $filtermode ) . ' ' , $sqlwhere );
2024-03-11 10:23:34 +00:00
}
$filter = '' ;
}
// Manage filter
$errormessage = '' ;
$sql .= forgeSQLFromUniversalSearchCriteria ( $filter , $errormessage );
if ( $errormessage ) {
$this -> errors [] = $errormessage ;
2024-03-11 11:38:21 +00:00
dol_syslog ( __METHOD__ . ' ' . implode ( ',' , $this -> errors ), LOG_ERR );
2024-03-11 10:23:34 +00:00
return - 1 ;
2016-12-08 20:58:52 +00:00
}
2024-03-05 02:51:36 +00:00
2017-01-03 10:06:24 +00:00
/* Fetching this table depends on filepath + filename , it must not depends on entity
2022-08-28 12:08:27 +00:00
if ( isModEnabled ( 'multicompany' )) {
2020-06-18 13:07:43 +00:00
$sql .= " AND entity IN ( " . getEntity ( 'ecmfiles' ) . " ) " ;
} */
2016-12-08 20:58:52 +00:00
if ( ! empty ( $sortfield )) {
2019-01-27 10:55:16 +00:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
2016-12-08 20:58:52 +00:00
}
if ( ! empty ( $limit )) {
2021-08-27 20:42:04 +00:00
$sql .= $this -> db -> plimit ( $limit , $offset );
2016-12-08 20:58:52 +00:00
}
$this -> lines = array ();
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$num = $this -> db -> num_rows ( $resql );
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2024-11-06 22:57:45 +00:00
$line = new EcmFilesLine ( $this -> db );
2016-12-08 20:58:52 +00:00
$line -> id = $obj -> rowid ;
2022-06-12 12:23:24 +00:00
$line -> ref = $obj -> rowid ;
2016-12-08 20:58:52 +00:00
$line -> label = $obj -> label ;
2017-09-21 12:02:42 +00:00
$line -> share = $obj -> share ;
2016-12-08 20:58:52 +00:00
$line -> entity = $obj -> entity ;
$line -> filename = $obj -> filename ;
$line -> filepath = $obj -> filepath ;
$line -> fullpath_orig = $obj -> fullpath_orig ;
$line -> description = $obj -> description ;
$line -> keywords = $obj -> keywords ;
2024-11-12 23:01:18 +00:00
if ( getDolGlobalString ( " MAIN_SAVE_FILE_CONTENT_AS_TEXT " )) {
2024-11-09 12:28:50 +00:00
$line -> content = $obj -> content ;
}
2016-12-08 20:58:52 +00:00
$line -> cover = $obj -> cover ;
$line -> position = $obj -> position ;
$line -> gen_or_uploaded = $obj -> gen_or_uploaded ;
$line -> extraparams = $obj -> extraparams ;
$line -> date_c = $this -> db -> jdate ( $obj -> date_c );
$line -> date_m = $this -> db -> jdate ( $obj -> date_m );
$line -> fk_user_c = $obj -> fk_user_c ;
$line -> fk_user_m = $obj -> fk_user_m ;
$line -> acl = $obj -> acl ;
2018-04-13 13:12:36 +00:00
$line -> src_object_type = $obj -> src_object_type ;
$line -> src_object_id = $obj -> src_object_id ;
2025-04-22 10:44:57 +00:00
$line -> agenda_id = $obj -> agenda_id ;
2026-07-20 12:58:04 +00:00
2026-01-19 19:53:30 +00:00
$line -> fetch_optionals ();
2026-07-20 12:58:04 +00:00
2018-04-13 13:12:36 +00:00
$this -> lines [] = $line ;
2016-12-08 20:58:52 +00:00
}
$this -> db -> free ( $resql );
return $num ;
} else {
2020-04-10 08:59:32 +00:00
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . implode ( ',' , $this -> errors ), LOG_ERR );
2016-12-08 20:58:52 +00:00
2020-04-10 08:59:32 +00:00
return - 1 ;
2016-12-08 20:58:52 +00:00
}
}
/**
* Update object into database
*
2024-01-12 18:48:18 +00:00
* @ param User $user User that modifies
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int Return integer < 0 if KO , > 0 if OK
2016-12-08 20:58:52 +00:00
*/
2024-01-12 17:07:53 +00:00
public function update ( User $user , $notrigger = 0 )
2016-12-08 20:58:52 +00:00
{
2018-04-13 13:12:36 +00:00
global $conf ;
2016-12-08 20:58:52 +00:00
$error = 0 ;
dol_syslog ( __METHOD__ , LOG_DEBUG );
// Clean parameters
2017-07-05 09:24:24 +00:00
2017-09-21 12:02:42 +00:00
if ( isset ( $this -> ref )) {
2020-05-27 11:12:18 +00:00
$this -> ref = trim ( $this -> ref );
2017-09-21 12:02:42 +00:00
}
2016-12-08 20:58:52 +00:00
if ( isset ( $this -> label )) {
2020-05-27 11:12:18 +00:00
$this -> label = trim ( $this -> label );
2016-12-08 20:58:52 +00:00
}
2017-09-21 12:02:42 +00:00
if ( isset ( $this -> share )) {
2020-05-27 11:12:18 +00:00
$this -> share = trim ( $this -> share );
2017-09-21 12:02:42 +00:00
}
2016-12-08 20:58:52 +00:00
if ( isset ( $this -> entity )) {
2024-01-30 21:05:15 +00:00
$this -> entity = ( int ) $this -> entity ;
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> filename )) {
2020-05-27 11:12:18 +00:00
$this -> filename = preg_replace ( '/\.noexe$/' , '' , trim ( $this -> filename ));
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> filepath )) {
2020-05-27 11:12:18 +00:00
$this -> filepath = trim ( $this -> filepath );
2020-10-31 13:32:18 +00:00
$this -> filepath = preg_replace ( '/[\\/]+$/' , '' , $this -> filepath ); // Remove last /
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> fullpath_orig )) {
2020-05-27 11:12:18 +00:00
$this -> fullpath_orig = trim ( $this -> fullpath_orig );
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> description )) {
2020-05-27 11:12:18 +00:00
$this -> description = trim ( $this -> description );
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> keywords )) {
2020-05-27 11:12:18 +00:00
$this -> keywords = trim ( $this -> keywords );
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> cover )) {
2020-05-27 11:12:18 +00:00
$this -> cover = trim ( $this -> cover );
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> gen_or_uploaded )) {
2020-05-27 11:12:18 +00:00
$this -> gen_or_uploaded = trim ( $this -> gen_or_uploaded );
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> fk_user_m )) {
2024-01-30 21:05:15 +00:00
$this -> fk_user_m = ( int ) $this -> fk_user_m ;
2016-12-08 20:58:52 +00:00
}
if ( isset ( $this -> acl )) {
2020-05-27 11:12:18 +00:00
$this -> acl = trim ( $this -> acl );
2016-12-08 20:58:52 +00:00
}
2018-04-13 13:12:36 +00:00
if ( isset ( $this -> src_object_type )) {
$this -> src_object_type = trim ( $this -> src_object_type );
}
2025-04-22 10:44:57 +00:00
if ( ! empty ( $this -> agenda_id )) {
$this -> agenda_id = ( int ) $this -> agenda_id ;
}
2025-03-06 10:34:27 +00:00
$extraparams = ( ! empty ( $this -> extraparams ) ? json_encode ( $this -> extraparams ) : null );
$extraparams = dol_trunc ( $extraparams , 250 );
2016-12-08 20:58:52 +00:00
// Update request
2020-04-10 08:59:32 +00:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element . ' SET' ;
2024-09-05 14:05:37 +00:00
$sql .= " ref = ' " . $this -> db -> escape ( dol_hash ( $this -> filepath . " / " . $this -> filename , '3' )) . " ', " ;
2020-04-10 08:59:32 +00:00
$sql .= ' label = ' . ( isset ( $this -> label ) ? " ' " . $this -> db -> escape ( $this -> label ) . " ' " : " null " ) . ',' ;
$sql .= ' share = ' . ( ! empty ( $this -> share ) ? " ' " . $this -> db -> escape ( $this -> share ) . " ' " : " null " ) . ',' ;
2026-07-20 00:50:44 +00:00
$sql .= ' entity = ' . ( isset ( $this -> entity ) ? (( int ) $this -> entity ) : (( int ) $conf -> entity )) . ',' ;
2020-04-10 08:59:32 +00:00
$sql .= ' filename = ' . ( isset ( $this -> filename ) ? " ' " . $this -> db -> escape ( $this -> filename ) . " ' " : " null " ) . ',' ;
$sql .= ' filepath = ' . ( isset ( $this -> filepath ) ? " ' " . $this -> db -> escape ( $this -> filepath ) . " ' " : " null " ) . ',' ;
$sql .= ' fullpath_orig = ' . ( isset ( $this -> fullpath_orig ) ? " ' " . $this -> db -> escape ( $this -> fullpath_orig ) . " ' " : " null " ) . ',' ;
$sql .= ' description = ' . ( isset ( $this -> description ) ? " ' " . $this -> db -> escape ( $this -> description ) . " ' " : " null " ) . ',' ;
$sql .= ' keywords = ' . ( isset ( $this -> keywords ) ? " ' " . $this -> db -> escape ( $this -> keywords ) . " ' " : " null " ) . ',' ;
2024-11-12 23:01:18 +00:00
if ( getDolGlobalString ( " MAIN_SAVE_FILE_CONTENT_AS_TEXT " )) {
2024-11-09 12:28:50 +00:00
$sql .= ' content = ' . ( isset ( $this -> content ) ? " ' " . $this -> db -> escape ( $this -> content ) . " ' " : " null " ) . ',' ;
}
2020-04-10 08:59:32 +00:00
$sql .= ' cover = ' . ( isset ( $this -> cover ) ? " ' " . $this -> db -> escape ( $this -> cover ) . " ' " : " null " ) . ',' ;
2025-03-19 14:21:48 +00:00
$sql .= ' position = ' . ( isset ( $this -> position ) ? $this -> db -> escape (( string ) $this -> position ) : " 0 " ) . ',' ;
2020-04-10 08:59:32 +00:00
$sql .= ' gen_or_uploaded = ' . ( isset ( $this -> gen_or_uploaded ) ? " ' " . $this -> db -> escape ( $this -> gen_or_uploaded ) . " ' " : " null " ) . ',' ;
2025-03-06 10:34:27 +00:00
$sql .= ' extraparams = ' . ( isset ( $extraparams ) ? " ' " . $this -> db -> escape ( $extraparams ) . " ' " : " null " ) . ',' ;
2020-04-10 08:59:32 +00:00
$sql .= ' date_c = ' . ( ! isset ( $this -> date_c ) || dol_strlen ( $this -> date_c ) != 0 ? " ' " . $this -> db -> idate ( $this -> date_c ) . " ' " : 'null' ) . ',' ;
2024-09-13 15:40:03 +00:00
//$sql .= ' tms = '.(! isset($this->date_m) || dol_strlen((string) $this->date_m) != 0 ? "'".$this->db->idate($this->date_m)."'" : 'null').','; // Field automatically updated
2026-06-11 21:44:12 +00:00
$sql .= ' fk_user_m = ' . ( $this -> fk_user_m > 0 ? (( int ) $this -> fk_user_m ) : (( int ) $user -> id )) . ',' ;
2020-04-10 08:59:32 +00:00
$sql .= ' acl = ' . ( isset ( $this -> acl ) ? " ' " . $this -> db -> escape ( $this -> acl ) . " ' " : " null " ) . ',' ;
2026-06-11 21:44:12 +00:00
$sql .= ' src_object_id = ' . ( $this -> src_object_id > 0 ? (( int ) $this -> src_object_id ) : " null " ) . ',' ;
2025-04-22 10:44:57 +00:00
$sql .= ' src_object_type = ' . ( isset ( $this -> src_object_type ) ? " ' " . $this -> db -> escape ( $this -> src_object_type ) . " ' " : " null " ) . ',' ;
2026-06-11 21:44:12 +00:00
$sql .= ' agenda_id = ' . ( $this -> agenda_id > 0 ? (( int ) $this -> agenda_id ) : " null " );
2021-03-14 11:20:23 +00:00
$sql .= ' WHERE rowid=' . (( int ) $this -> id );
2016-12-08 20:58:52 +00:00
$this -> db -> begin ();
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2020-04-10 08:59:32 +00:00
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . implode ( ',' , $this -> errors ), LOG_ERR );
2016-12-08 20:58:52 +00:00
}
2024-06-03 11:46:31 +00:00
if ( ! $error ) {
// Update extrafields
$result = $this -> insertExtraFields ();
if ( $result < 0 ) {
$error ++ ;
}
}
2018-04-13 13:12:36 +00:00
// Triggers
2020-11-27 08:11:30 +00:00
if ( ! $error && ! $notrigger ) {
2018-04-13 13:12:36 +00:00
// Call triggers
2020-04-10 08:59:32 +00:00
$result = $this -> call_trigger ( strtoupper ( get_class ( $this )) . '_MODIFY' , $user );
2020-11-27 08:11:30 +00:00
if ( $result < 0 ) {
$error ++ ;
} //Do also here what you must do to rollback action if trigger fail
2018-04-13 13:12:36 +00:00
// End call triggers
2016-12-08 20:58:52 +00:00
}
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
2020-04-10 08:59:32 +00:00
return - 1 * $error ;
2016-12-08 20:58:52 +00:00
} else {
$this -> db -> commit ();
return 1 ;
}
}
/**
* Delete object in database
*
2024-01-12 18:48:18 +00:00
* @ param User $user User that deletes
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int Return integer < 0 if KO , > 0 if OK
2016-12-08 20:58:52 +00:00
*/
2024-01-12 17:07:53 +00:00
public function delete ( User $user , $notrigger = 0 )
2016-12-08 20:58:52 +00:00
{
dol_syslog ( __METHOD__ , LOG_DEBUG );
$error = 0 ;
$this -> db -> begin ();
2018-04-13 13:12:36 +00:00
// Triggers
2020-11-27 08:11:30 +00:00
if ( ! $notrigger ) {
2018-04-13 13:12:36 +00:00
// Call triggers
2020-04-10 08:59:32 +00:00
$result = $this -> call_trigger ( strtoupper ( get_class ( $this )) . '_DELETE' , $user );
2020-11-27 08:11:30 +00:00
if ( $result < 0 ) {
$error ++ ;
} //Do also here what you must do to rollback action if trigger fail
2018-04-13 13:12:36 +00:00
// End call triggers
2016-12-08 20:58:52 +00:00
}
// If you need to delete child tables to, you can insert them here
2022-11-09 13:45:26 +00:00
if ( ! $error ) {
2022-11-09 13:53:42 +00:00
$result = $this -> deleteExtraFields ();
if ( ! $result ) {
2022-11-09 13:55:41 +00:00
dol_syslog ( get_class ( $this ) . " ::delete error " . $this -> error , LOG_ERR );
2022-11-09 13:45:26 +00:00
$error ++ ;
}
}
2016-12-08 20:58:52 +00:00
if ( ! $error ) {
2020-04-10 08:59:32 +00:00
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this -> table_element ;
2021-03-14 11:20:23 +00:00
$sql .= ' WHERE rowid=' . (( int ) $this -> id );
2016-12-08 20:58:52 +00:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2020-04-10 08:59:32 +00:00
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . implode ( ',' , $this -> errors ), LOG_ERR );
2016-12-08 20:58:52 +00:00
}
}
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
2020-04-10 08:59:32 +00:00
return - 1 * $error ;
2016-12-08 20:58:52 +00:00
} else {
$this -> db -> commit ();
return 1 ;
}
}
/**
* Load an object from its id and create a new one in database
*
2019-04-25 10:11:32 +00:00
* @ param User $user User making the clone
* @ param int $fromid Id of object to clone
* @ return int New id of clone
2016-12-08 20:58:52 +00:00
*/
2019-04-25 10:11:32 +00:00
public function createFromClone ( User $user , $fromid )
2016-12-08 20:58:52 +00:00
{
dol_syslog ( __METHOD__ , LOG_DEBUG );
$error = 0 ;
2023-08-05 11:27:56 +00:00
$object = new EcmFiles ( $this -> db );
2016-12-08 20:58:52 +00:00
$this -> db -> begin ();
// Load source object
$object -> fetch ( $fromid );
// Reset object
$object -> id = 0 ;
// Clear fields
// ...
// Create clone
2019-01-19 11:46:38 +00:00
$object -> context [ 'createfromclone' ] = 'createfromclone' ;
2016-12-08 20:58:52 +00:00
$result = $object -> create ( $user );
// Other options
if ( $result < 0 ) {
2020-04-10 08:59:32 +00:00
$error ++ ;
2016-12-08 20:58:52 +00:00
$this -> errors = $object -> errors ;
2020-04-10 08:59:32 +00:00
dol_syslog ( __METHOD__ . ' ' . implode ( ',' , $this -> errors ), LOG_ERR );
2016-12-08 20:58:52 +00:00
}
2019-01-19 11:46:38 +00:00
unset ( $object -> context [ 'createfromclone' ]);
2016-12-08 20:58:52 +00:00
// End
if ( ! $error ) {
$this -> db -> commit ();
return $object -> id ;
} else {
$this -> db -> rollback ();
2020-04-10 08:59:32 +00:00
return - 1 ;
2016-12-08 20:58:52 +00:00
}
}
2024-07-06 12:34:22 +00:00
/**
* updateAfterRename update entries in ecmfiles if exist to avoid losing info
*
* @ param string $olddir old directory
* @ param string $newdir new directory
* @ return void
*/
public function updateAfterRename ( $olddir , $newdir )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'ecm_files SET' ;
$sql .= ' filepath = "' . $this -> db -> escape ( $newdir ) . '"' ;
//$sql .= ', fullpath_orig = "'.$dbs->escape($newdir)."'";
$sql .= ' WHERE ' ;
$sql .= ' filepath = "' . $this -> db -> escape ( $olddir ) . '"' ;
// $sql .= ' AND fullpath_orig = "'.$dbs->escape($olddir).'"';
$this -> db -> query ( $sql );
}
2024-11-07 04:07:41 +00:00
/**
* getTooltipContentArray
2024-11-10 00:28:16 +00:00
*
* @ param array < string , mixed > $params params to construct tooltip data
* @ return array { picto ? : string , ref ? : string , gen_or_upload ? : string } | array { optimize : string }
2024-11-07 04:07:41 +00:00
* @ since v21
*/
public function getTooltipContentArray ( $params )
{
2024-11-10 00:28:16 +00:00
global $langs ;
2024-11-07 04:07:41 +00:00
$langs -> load ( 'ecm' );
$datas = [];
$nofetch = ! empty ( $params [ 'nofetch' ]);
if ( getDolGlobalString ( 'MAIN_OPTIMIZEFORTEXTBROWSER' )) {
return [ 'optimize' => $langs -> trans ( " ShowFile " )];
}
2024-11-10 00:28:16 +00:00
$datas [ 'picto' ] = img_picto ( '' , $this -> picto , '' , 0 , 0 , 0 , '' , 'paddingrightonly' ) . '<u>' . $langs -> trans ( " File " ) . '</u>' ;
if ( ! empty ( $this -> filename )) {
$datas [ 'name' ] = '<br><b>' . $langs -> trans ( 'Name' ) . ':</b> ' . basename ( $this -> filename );
}
2024-11-07 04:07:41 +00:00
if ( ! empty ( $this -> ref )) {
2024-11-10 00:28:16 +00:00
$datas [ 'ref' ] = '<br><b>' . $langs -> trans ( 'HashOfFileContent' ) . ':</b> ' . $this -> ref ;
}
if ( ! empty ( $this -> share )) {
$datas [ 'share' ] = '<br>' . $langs -> trans ( " FileSharedViaALink " );
} else {
$datas [ 'share' ] = '<br>' . $langs -> trans ( " FileNotShared " );
2024-11-07 04:07:41 +00:00
}
if ( ! empty ( $this -> gen_or_uploaded )) {
2024-11-08 13:41:01 +00:00
$datas [ 'gen_or_upload' ] = '<br><b>' . $langs -> trans ( 'GenOrUpload' ) . ':</b> ' . $this -> gen_or_uploaded ;
2024-11-07 04:07:41 +00:00
}
2024-11-10 00:28:16 +00:00
if ( ! empty ( $this -> content )) {
$datas [ 'content' ] = '<br>' . $langs -> trans ( 'FileHasAnIndexedTextContent' );
}
2024-11-07 04:07:41 +00:00
return $datas ;
}
2016-12-08 20:58:52 +00:00
/**
2024-01-13 18:48:41 +00:00
* Return a link to the object card ( with optionally the picto )
2016-12-08 20:58:52 +00:00
*
2026-02-12 17:56:00 +00:00
* @ 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 ( propal , etc ) module name
* @ param int $notooltip 1 = Disable tooltip
* @ param int $maxlen Max length of visible user name
* @ param string $morecss Add more css on link
* @ return string String with URL
2016-12-08 20:58:52 +00:00
*/
2019-02-25 23:02:39 +00:00
public function getNomUrl ( $withpicto = 0 , $option = '' , $notooltip = 0 , $maxlen = 24 , $morecss = '' )
2016-12-08 20:58:52 +00:00
{
2024-11-07 04:07:41 +00:00
global $conf , $hookmanager , $langs ;
2016-12-08 20:58:52 +00:00
2020-11-27 08:11:30 +00:00
if ( ! empty ( $conf -> dol_no_mouse_hover )) {
$notooltip = 1 ; // Force disable tooltips
}
2017-07-05 09:24:24 +00:00
2017-10-07 11:09:31 +00:00
$result = '' ;
2016-12-08 20:58:52 +00:00
2024-11-07 04:07:41 +00:00
$params = [
'id' => $this -> id ,
'objecttype' => $this -> element ,
'option' => $option ,
'nofetch' => 1 ,
];
$classfortooltip = 'classfortooltip' ;
$dataparams = '' ;
if ( getDolGlobalInt ( 'MAIN_ENABLE_AJAX_TOOLTIP' )) {
$classfortooltip = 'classforajaxtooltip' ;
$dataparams = ' data-params="' . dol_escape_htmltag ( json_encode ( $params )) . '"' ;
$label = '' ;
} else {
$label = implode ( $this -> getTooltipContentArray ( $params ));
}
2016-12-08 20:58:52 +00:00
2024-11-07 04:07:41 +00:00
if ( $option ) {
2024-12-09 21:26:56 +00:00
if ( $option == 'facture_fournisseur' ) {
2025-03-03 14:20:39 +00:00
$tmppath = preg_replace ( '/^(\d+\/)?fournisseur\/facture\//' , '' , $this -> filepath );
2024-12-09 21:26:56 +00:00
} elseif ( $option == 'commande_fournisseur' ) {
2025-03-03 14:20:39 +00:00
$tmppath = preg_replace ( '/^(\d+\/)?fournisseur\/commande\//' , '' , $this -> filepath );
2025-03-17 19:30:02 +00:00
} elseif ( $option == 'tax-vat' ) { // Remove part "tax/vat/"
$tmppath = preg_replace ( '/^(\d+\/)?tax\/vat\//' , '' , $this -> filepath );
2025-03-31 17:34:57 +00:00
} elseif ( $option == 'remisecheque' ) { // Remove part "tax/vat/"
$tmppath = preg_replace ( '/^bank\/checkdeposits\//' , '' , $this -> filepath );
2024-12-09 21:26:56 +00:00
} else {
2025-02-12 15:22:06 +00:00
if (( int ) $this -> entity > 1 ) {
// Remove the part "entityid/commande/" into "entityid/commande/REFXXX" to get only the ref
$tmppath = preg_replace ( '/^\d+\/[^\/]+\//' , '' , $this -> filepath );
} else {
// Remove the part "commande/" into "commande/REFXXX" to get only the ref
$tmppath = preg_replace ( '/^[^\/]+\//' , '' , $this -> filepath );
}
2024-12-09 21:26:56 +00:00
}
Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in ecm (#39557)
* Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in ecm
Replaces manual '?key='.urlencode($val).'&key2='.urlencode($val2)
string-building with dolBuildUrl($path, $params) across the ECM
directory-tree/file-manager code, for consistency with the rest of
the codebase (see htdocs/core/lib/ecm.lib.php, which already uses
this pattern for the same page) and to benefit from dolBuildUrl()'s
buildurl hook.
- core/ajax/ajaxdirtree.php: the dir_card.php edit link built from
the sql tree loop.
- ecm/dir_card.php: the edit/add-section action buttons and the two
delete confirmation URLs. Also switches the three buttons that used
to manually concatenate '&token='.newToken() to dolBuildUrl()'s own
$addtoken parameter.
- ecm/class/ecmfiles.class.php: EcmFiles::getNomUrl()'s document.php
and file_card.php URLs.
- ecm/tpl/enablefiletreeajax.tpl.php: the ajaxdirtree.php script URL
and the ajaxdirpreview.php URL. The token here intentionally stays
currentToken() (not dolBuildUrl()'s own newToken()-based
$addtoken), per the existing comment: ajaxdirtree.php has
NOTOKENRENEWAL defined, so the token must match the one already
valid on the calling page. $paramwithoutsection is a pre-built raw
query-string fragment from an external caller and is appended as-is
after the dolBuildUrl() result rather than folded into it.
Verified all five refactored URL-building expressions produce byte-
identical output to the original code for representative inputs
(including values with '/', '&' and spaces), except for query
parameter order (which has no effect) and one real, minor pre-
existing bug this incidentally fixes: the delete-section confirm URL
in dir_card.php was building '&module='.$module without urlencode(),
now correctly encoded by dolBuildUrl()/http_build_query().
Could not do a live browser check (no Chrome available for Playwright
in this environment) - verified via php -l, phpcs, and a standalone
script comparing old vs new output for each call site instead.
* Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in filemanager.tpl.php
Same refactor as the previous commit, applied to the 7 remaining
manually-concatenated URLs in core/tpl/filemanager.tpl.php (used by
the ECM/medias file manager): the delete-file/delete-section/
convert-to-webp confirm URLs, the create-directory and refresh-list
toolbar buttons (now using dolBuildUrl()'s $addtoken instead of a
manual '&token='.newToken()), the two generate-webp buttons, and the
"Root" link.
$websitekeyandpageid is kept as a helper to build the raw sub-query
string embedded once (single-encoded) as the create-directory
button's 'backtopage' value - it is not itself passed to dolBuildUrl.
Verified all 7 refactored URL-building expressions produce the same
query parameters as the original code for representative inputs
(compared as parsed, order-independent query strings, since
http_build_query() does not preserve insertion order the same way as
the original manual concatenation), including one case
(convertimgwebp confirm with sortfield/sortorder) where the original
code had a harmless but sloppy leading '?&' that dolBuildUrl() no
longer produces.
* fix
* Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in index_auto.php
Same refactor as the previous commits, applied to the 4 manually-
concatenated URLs in ecm/index_auto.php: the delete-file and
delete-section confirm URLs, the refresh-list toolbar link, and the
per-directory link in the auto-directories list.
Verified all 4 refactored URL-building expressions produce the same
query parameters as the original code for representative inputs
(including an empty-module/empty-section case for the refresh link,
and values with '/' and spaces for the others).
* Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in ecm (file_card, dir_add_card, index_medias)
Same refactor as the previous commits, applied to the remaining
manually-concatenated URLs in:
- ecm/file_card.php: the cancel and rename-file redirects, the
internal download link (document.php), the delete-file confirm URL
and the edit button.
- ecm/dir_add_card.php: the delete-section confirm URL and the delete
button (now using dolBuildUrl()'s $addtoken instead of a manual
'&token='.newToken()).
- ecm/index_medias.php: the $backtopage URL used by
core/actions_linkedfiles.inc.php after a confirm_deletefile.
Left ecm/search.php's '$param = "§ion=".urlencode($section)'
alone: it is a raw query-string fragment (starting with '&', no
leading path) passed into FormFile::list_of_documents(), not a
base+params URL build, so it does not fit the dolBuildUrl($path,
$params) shape - same reasoning as $paramwithoutsection in the
already-refactored enablefiletreeajax.tpl.php.
Verified all 8 refactored URL-building expressions produce the same
query parameters as the original code for representative inputs
(order-independent comparison, since http_build_query() does not
preserve the original insertion order).
2026-08-17 02:41:05 +00:00
$url = dolBuildUrl ( DOL_URL_ROOT . '/document.php' , array ( 'modulepart' => $option , 'file' => $tmppath . '/' . $this -> filename , 'entity' => ( int ) $this -> entity ));
2024-11-07 04:07:41 +00:00
} else {
Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in ecm (#39557)
* Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in ecm
Replaces manual '?key='.urlencode($val).'&key2='.urlencode($val2)
string-building with dolBuildUrl($path, $params) across the ECM
directory-tree/file-manager code, for consistency with the rest of
the codebase (see htdocs/core/lib/ecm.lib.php, which already uses
this pattern for the same page) and to benefit from dolBuildUrl()'s
buildurl hook.
- core/ajax/ajaxdirtree.php: the dir_card.php edit link built from
the sql tree loop.
- ecm/dir_card.php: the edit/add-section action buttons and the two
delete confirmation URLs. Also switches the three buttons that used
to manually concatenate '&token='.newToken() to dolBuildUrl()'s own
$addtoken parameter.
- ecm/class/ecmfiles.class.php: EcmFiles::getNomUrl()'s document.php
and file_card.php URLs.
- ecm/tpl/enablefiletreeajax.tpl.php: the ajaxdirtree.php script URL
and the ajaxdirpreview.php URL. The token here intentionally stays
currentToken() (not dolBuildUrl()'s own newToken()-based
$addtoken), per the existing comment: ajaxdirtree.php has
NOTOKENRENEWAL defined, so the token must match the one already
valid on the calling page. $paramwithoutsection is a pre-built raw
query-string fragment from an external caller and is appended as-is
after the dolBuildUrl() result rather than folded into it.
Verified all five refactored URL-building expressions produce byte-
identical output to the original code for representative inputs
(including values with '/', '&' and spaces), except for query
parameter order (which has no effect) and one real, minor pre-
existing bug this incidentally fixes: the delete-section confirm URL
in dir_card.php was building '&module='.$module without urlencode(),
now correctly encoded by dolBuildUrl()/http_build_query().
Could not do a live browser check (no Chrome available for Playwright
in this environment) - verified via php -l, phpcs, and a standalone
script comparing old vs new output for each call site instead.
* Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in filemanager.tpl.php
Same refactor as the previous commit, applied to the 7 remaining
manually-concatenated URLs in core/tpl/filemanager.tpl.php (used by
the ECM/medias file manager): the delete-file/delete-section/
convert-to-webp confirm URLs, the create-directory and refresh-list
toolbar buttons (now using dolBuildUrl()'s $addtoken instead of a
manual '&token='.newToken()), the two generate-webp buttons, and the
"Root" link.
$websitekeyandpageid is kept as a helper to build the raw sub-query
string embedded once (single-encoded) as the create-directory
button's 'backtopage' value - it is not itself passed to dolBuildUrl.
Verified all 7 refactored URL-building expressions produce the same
query parameters as the original code for representative inputs
(compared as parsed, order-independent query strings, since
http_build_query() does not preserve insertion order the same way as
the original manual concatenation), including one case
(convertimgwebp confirm with sortfield/sortorder) where the original
code had a harmless but sloppy leading '?&' that dolBuildUrl() no
longer produces.
* fix
* Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in index_auto.php
Same refactor as the previous commits, applied to the 4 manually-
concatenated URLs in ecm/index_auto.php: the delete-file and
delete-section confirm URLs, the refresh-list toolbar link, and the
per-directory link in the auto-directories list.
Verified all 4 refactored URL-building expressions produce the same
query parameters as the original code for representative inputs
(including an empty-module/empty-section case for the refresh link,
and values with '/' and spaces for the others).
* Qual: Use dolBuildUrl() instead of manual urlencode() concatenation in ecm (file_card, dir_add_card, index_medias)
Same refactor as the previous commits, applied to the remaining
manually-concatenated URLs in:
- ecm/file_card.php: the cancel and rename-file redirects, the
internal download link (document.php), the delete-file confirm URL
and the edit button.
- ecm/dir_add_card.php: the delete-section confirm URL and the delete
button (now using dolBuildUrl()'s $addtoken instead of a manual
'&token='.newToken()).
- ecm/index_medias.php: the $backtopage URL used by
core/actions_linkedfiles.inc.php after a confirm_deletefile.
Left ecm/search.php's '$param = "§ion=".urlencode($section)'
alone: it is a raw query-string fragment (starting with '&', no
leading path) passed into FormFile::list_of_documents(), not a
base+params URL build, so it does not fit the dolBuildUrl($path,
$params) shape - same reasoning as $paramwithoutsection in the
already-refactored enablefiletreeajax.tpl.php.
Verified all 8 refactored URL-building expressions produce the same
query parameters as the original code for representative inputs
(order-independent comparison, since http_build_query() does not
preserve the original insertion order).
2026-08-17 02:41:05 +00:00
$url = dolBuildUrl ( DOL_URL_ROOT . '/ecm/file_card.php' , array ( 'id' => $this -> id ));
2024-11-07 04:07:41 +00:00
}
2017-07-05 09:24:24 +00:00
2020-04-10 08:59:32 +00:00
$linkclose = '' ;
2020-11-27 08:11:30 +00:00
if ( empty ( $notooltip )) {
2023-11-27 10:41:05 +00:00
if ( getDolGlobalString ( 'MAIN_OPTIMIZEFORTEXTBROWSER' )) {
2024-11-07 04:07:41 +00:00
$label = $langs -> trans ( " ShowFile " );
2025-01-09 00:41:24 +00:00
$linkclose .= ' alt="' . dolPrintHTMLForAttribute ( $label ) . '"' ;
2017-10-07 11:09:31 +00:00
}
2025-01-09 00:41:24 +00:00
$linkclose .= ( $label ? ' title="' . dolPrintHTMLForAttribute ( $label ) . '"' : ' title="tocomplete"' );
2024-11-07 04:07:41 +00:00
$linkclose .= $dataparams . ' class="' . $classfortooltip . ' ' . ( $morecss ? ' ' . $morecss : '' ) . '"' ;
2020-11-27 08:11:30 +00:00
} else {
$linkclose = ( $morecss ? ' class="' . $morecss . '"' : '' );
}
2017-07-05 09:24:24 +00:00
2016-12-08 20:58:52 +00:00
$linkstart = '<a href="' . $url . '"' ;
2024-11-07 04:07:41 +00:00
if ( getDolGlobalInt ( 'MAIN_DISABLE_FORCE_SAVEAS' ) == 2 ) {
$linkstart .= 'target="_blank" ' ;
}
2020-04-10 08:59:32 +00:00
$linkstart .= $linkclose . '>' ;
$linkend = '</a>' ;
2016-12-08 20:58:52 +00:00
2020-11-27 08:11:30 +00:00
if ( $withpicto ) {
2024-11-07 04:07:41 +00:00
if ( empty ( $this -> filename )) {
$result .= ( $linkstart . img_object (( $notooltip ? '' : $label ), 'label' , ( $notooltip ? '' : 'class="paddingright"' )) . $linkend );
} else {
2024-11-08 13:41:01 +00:00
$result .= ( $linkstart . img_mime ( $this -> filename , ( $notooltip ? '' : dol_escape_htmltag ( $label , 1 )), ( $notooltip ? '' : ' paddingright' )) . $linkend );
2024-11-07 04:07:41 +00:00
}
2016-12-08 20:58:52 +00:00
}
2024-11-07 04:07:41 +00:00
$result .= $linkstart . $this -> filename . $linkend ;
2021-11-22 18:57:26 +00:00
global $action ;
2022-02-15 18:04:18 +00:00
$hookmanager -> initHooks ( array ( $this -> element . 'dao' ));
2024-03-11 11:55:55 +00:00
$parameters = array ( 'id' => $this -> id , 'getnomurl' => & $result );
2021-11-22 18:57:26 +00:00
$reshook = $hookmanager -> executeHooks ( 'getNomUrl' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook > 0 ) {
$result = $hookmanager -> resPrint ;
} else {
$result .= $hookmanager -> resPrint ;
}
2024-11-07 04:07:41 +00:00
2016-12-08 20:58:52 +00:00
return $result ;
}
/**
2023-04-08 13:08:55 +00:00
* Return the label of the status
2016-12-08 20:58:52 +00:00
*
2023-04-08 13:08:55 +00: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
2016-12-08 20:58:52 +00:00
* @ return string Label of status
*/
2019-02-25 23:02:39 +00:00
public function getLibStatut ( $mode = 0 )
2016-12-08 20:58:52 +00:00
{
2019-01-27 10:55:16 +00:00
return $this -> LibStatut ( $this -> status , $mode );
2016-12-08 20:58:52 +00:00
}
2020-06-18 13:07:43 +00:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2016-12-08 20:58:52 +00:00
/**
* Return the status
*
* @ 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 , 5 = Long label + Picto
* @ return string Label of status
*/
2019-02-25 23:02:39 +00:00
public static function LibStatut ( $status , $mode = 0 )
2016-12-08 20:58:52 +00:00
{
2020-06-18 13:07:43 +00:00
// phpcs:enable
2016-12-08 20:58:52 +00:00
global $langs ;
2017-11-10 12:45:38 +00:00
return '' ;
2016-12-08 20:58:52 +00:00
}
/**
* Initialise object with example values
* Id must be 0 if object instance is a specimen
*
2024-03-02 15:38:35 +00:00
* @ return int
2016-12-08 20:58:52 +00:00
*/
public function initAsSpecimen ()
{
2020-04-10 08:59:32 +00:00
global $conf , $user ;
2017-07-05 09:24:24 +00:00
2016-12-08 20:58:52 +00:00
$this -> id = 0 ;
2020-11-28 16:07:08 +00:00
$this -> specimen = 1 ;
2016-12-08 20:58:52 +00:00
$this -> label = '0a1b2c3e4f59999999' ;
2020-11-28 16:07:08 +00:00
$this -> entity = 1 ;
2016-12-08 20:58:52 +00:00
$this -> filename = 'myspecimenfilefile.pdf' ;
$this -> filepath = '/aaa/bbb' ;
$this -> fullpath_orig = 'c:/file on my disk.pdf' ;
2024-11-09 12:28:50 +00:00
$this -> description = 'This is a description of the file' ;
2016-12-08 20:58:52 +00:00
$this -> keywords = 'key1,key2' ;
2024-11-09 12:28:50 +00:00
$this -> content = 'This is the text content of the file' ;
2016-12-08 20:58:52 +00:00
$this -> cover = '1' ;
2020-11-28 16:07:08 +00:00
$this -> position = 5 ;
2016-12-08 20:58:52 +00:00
$this -> gen_or_uploaded = 'uploaded' ;
$this -> extraparams = '' ;
$this -> date_c = ( dol_now () - 3600 * 24 * 10 );
$this -> date_m = '' ;
$this -> fk_user_c = $user -> id ;
2024-01-30 21:05:15 +00:00
$this -> fk_user_m = $user -> id ;
2016-12-08 20:58:52 +00:00
$this -> acl = '' ;
2018-04-13 13:12:36 +00:00
$this -> src_object_type = 'product' ;
$this -> src_object_id = 1 ;
2024-03-02 15:38:35 +00:00
return 1 ;
2016-12-08 20:58:52 +00:00
}
}
2017-07-05 09:24:24 +00:00
2020-05-03 20:47:43 +00:00
/**
* Class of an index line of a document
*/
2024-11-06 22:57:45 +00:00
class EcmFilesLine extends CommonObjectLine
2017-07-05 09:24:24 +00:00
{
2018-09-28 12:44:47 +00:00
/**
2020-06-18 13:07:43 +00:00
* @ var string ECM files line label
*/
public $label ;
2018-08-31 16:27:16 +00:00
/**
* @ var int Entity
*/
2017-07-05 09:24:24 +00:00
public $entity ;
2018-08-31 16:27:16 +00:00
2026-01-19 19:53:30 +00:00
/**
* @ var string Name of table without prefix where object is stored
*/
public $table_element = 'ecm_files' ;
2024-11-06 22:57:45 +00:00
/**
* @ var string
*/
2017-07-05 09:24:24 +00:00
public $filename ;
2024-11-06 22:57:45 +00:00
/**
* @ var string
*/
2017-07-05 09:24:24 +00:00
public $filepath ;
2024-11-06 22:57:45 +00:00
/**
* @ var string
*/
2017-07-05 09:24:24 +00:00
public $fullpath_orig ;
2018-08-31 17:26:08 +00:00
/**
* @ var string description
*/
2017-07-05 09:24:24 +00:00
public $description ;
2018-08-31 17:26:08 +00:00
2024-11-06 22:57:45 +00:00
/**
* @ var string
*/
2017-07-05 09:24:24 +00:00
public $keywords ;
2024-11-09 12:28:50 +00:00
/**
* @ var string
*/
public $content ;
2024-11-06 22:57:45 +00:00
/**
* @ var string
*/
2017-07-05 09:24:24 +00:00
public $cover ;
2024-11-06 22:57:45 +00:00
/**
* @ var int
*/
2017-07-05 09:24:24 +00:00
public $position ;
2024-11-06 22:57:45 +00:00
/**
* @ var 'generated' | 'uploaded' | 'unknown' | 'copy' | ''
*/
2020-04-10 08:59:32 +00:00
public $gen_or_uploaded ; // can be 'generated', 'uploaded', 'unknown'
2024-11-06 22:57:45 +00:00
/**
* @ var string
*/
2017-07-05 09:24:24 +00:00
public $extraparams ;
2024-11-06 22:57:45 +00:00
/**
* @ var int | ''
*/
2017-07-05 09:24:24 +00:00
public $date_c = '' ;
2024-11-06 22:57:45 +00:00
/**
* @ var int | ''
*/
2017-07-05 09:24:24 +00:00
public $date_m = '' ;
2018-10-10 07:23:55 +00:00
/**
2020-06-18 13:07:43 +00:00
* @ var int ID
*/
2017-07-05 09:24:24 +00:00
public $fk_user_c ;
2018-10-10 07:23:55 +00:00
/**
2020-06-18 13:07:43 +00:00
* @ var int ID
*/
2017-07-05 09:24:24 +00:00
public $fk_user_m ;
2018-10-10 07:23:55 +00:00
2024-11-06 22:57:45 +00:00
/**
* @ var string
*/
2017-07-05 09:24:24 +00:00
public $acl ;
2024-11-06 22:57:45 +00:00
/**
* @ var string
*/
2018-04-13 13:12:36 +00:00
public $src_object_type ;
2024-11-06 22:57:45 +00:00
/**
* @ var int
*/
2018-04-13 13:12:36 +00:00
public $src_object_id ;
2026-07-23 14:09:22 +00:00
/**
* @ var int
*/
public $agenda_id ;
/**
* @ var ? string Hash for file sharing
*/
public $share ;
2017-07-05 09:24:24 +00:00
}