2013-06-14 22:33:01 +02:00
< ? php
2024-10-03 15:43:04 +02:00
/* Copyright ( c ) 2013 Florian Henry < florian . henry @ open - concept . pro >
* Copyright ( C ) 2015 Marcos García < marcosgdf @ gmail . com >
* Copyright ( C ) 2018 Charlene Benke < charlie @ patas - monkey . com >
2024-02-28 23:42:23 +01:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2025-02-10 21:20:09 +01:00
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
2024-04-04 15:41:08 +02:00
* Copyright ( C ) 2024 Benjamin Falière < benjamin . faliere @ altairis . fr >
2013-06-14 22:33:01 +02:00
*
2015-06-15 10:04:36 +02: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 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2015-06-15 10:04:36 +02:00
*/
2013-06-14 22:33:01 +02:00
/**
* \file htdocs / core / class / html . formprojet . class . php
* \ingroup core
* \brief Class file for html component project
*/
2023-10-02 22:57:53 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/html.form.class.php' ;
2013-06-14 22:33:01 +02:00
/**
* Class to manage building of HTML components
*/
2023-10-02 22:57:53 +02:00
class FormProjets extends Form
2013-06-14 22:33:01 +02:00
{
2018-08-22 11:06:34 +02:00
/**
2020-04-16 00:42:57 +02:00
* @ var DoliDB Database handler .
*/
public $db ;
2018-09-02 22:05:19 +02:00
2018-08-22 10:37:16 +02:00
/**
* @ var string Error code ( or message )
*/
2020-04-10 10:59:32 +02:00
public $error = '' ;
2013-06-14 22:33:01 +02:00
2023-01-31 21:17:06 +01:00
public $errors = array ();
2024-10-26 18:24:40 +02:00
/**
* @ var int
*/
2021-09-23 13:20:42 +02:00
public $nboftasks ;
2013-06-14 22:33:01 +02:00
/**
2023-03-03 11:23:49 +01:00
* Constructor
2013-06-14 22:33:01 +02:00
*
2023-03-03 11:23:49 +01:00
* @ param DoliDB $db Database handler
2013-06-14 22:33:01 +02:00
*/
2019-02-27 20:45:07 +01:00
public function __construct ( $db )
2013-06-14 22:33:01 +02:00
{
$this -> db = $db ;
}
2020-04-16 00:42:57 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2023-03-03 11:23:49 +01:00
2013-06-14 22:33:01 +02:00
/**
2023-03-19 10:19:35 +01:00
* Output a combo list with projects qualified for a third party / user
2013-06-14 22:33:01 +02:00
*
2023-04-24 01:32:47 +02:00
* @ param int $socid Id third party ( - 1 = all , 0 = only projects not linked to a third party , id = projects not linked or linked to third party id )
2023-03-03 11:23:49 +01:00
* @ param string | Project $selected Id of preselected project or Project ( or '' ) . Note : If you know the ref , you can also provide it into $selected_input_value to save one request in some cases .
2023-04-24 01:32:47 +02:00
* @ param string $htmlname Name of HTML field
* @ param int $maxlength Maximum length of label
* @ param int $option_only Return only html options lines without the select tag
* @ param int | string $show_empty Add an empty line
* @ param int $discard_closed Discard closed projects ( 0 = Keep , 1 = hide completely , 2 = Disable ) . Use a negative value to not show the " discarded " tooltip .
* @ param int $forcefocus Force focus on field ( works with javascript only )
* @ param int $disabled Disabled
* @ param int $mode 0 for HTML mode and 1 for JSON mode
* @ param string $filterkey Key to filter on ref or title
* @ param int $nooutput No print output . Return it only .
* @ param int $forceaddid Force to add project id in list , event if not qualified
* @ param string $morecss More css
2024-02-28 16:19:38 +01:00
* @ param string $htmlid Html id to use instead of htmlname , by example id = " htmlid "
2023-04-24 01:32:47 +02:00
* @ param string $morefilter More filters ( Must be a sql sanitized string )
* @ return string Return html content
2015-06-15 10:04:36 +02:00
*/
2022-11-23 21:28:33 +01:00
public function select_projects ( $socid = - 1 , $selected = '' , $htmlname = 'projectid' , $maxlength = 16 , $option_only = 0 , $show_empty = 1 , $discard_closed = 0 , $forcefocus = 0 , $disabled = 0 , $mode = 0 , $filterkey = '' , $nooutput = 0 , $forceaddid = 0 , $morecss = '' , $htmlid = '' , $morefilter = '' )
2020-04-16 00:42:57 +02:00
{
// phpcs:enable
2020-04-10 10:59:32 +02:00
global $langs , $conf , $form ;
2015-06-15 10:04:36 +02:00
2022-04-29 13:04:29 +02:00
$selected_input_value = '' ;
if ( is_object ( $selected )) {
$selected_input_value = $selected -> ref ;
$selected = $selected -> id ;
}
2020-04-10 10:59:32 +02:00
$out = '' ;
2017-12-12 10:37:51 +01:00
2023-11-27 11:24:19 +01:00
if ( ! empty ( $conf -> use_javascript_ajax ) && getDolGlobalString ( 'PROJECT_USE_SEARCH_TO_SELECT' )) {
2020-04-10 10:59:32 +02:00
$placeholder = '' ;
2015-06-15 10:04:36 +02:00
2021-02-23 22:03:23 +01:00
if ( $selected && empty ( $selected_input_value )) {
2024-12-29 17:12:50 +01:00
require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php' ;
2015-06-15 10:04:36 +02:00
$project = new Project ( $this -> db );
$project -> fetch ( $selected );
2020-04-10 10:59:32 +02:00
$selected_input_value = $project -> ref ;
2015-06-15 10:04:36 +02:00
}
2023-03-03 11:23:49 +01:00
$urloption = 'socid=' . (( int ) $socid ) . '&htmlname=' . urlencode ( $htmlname ) . '&discardclosed=' . (( int ) $discard_closed );
2022-11-23 21:28:33 +01:00
if ( $morefilter == 'usage_organize_event=1' ) {
$urloption .= '&usage_organize_event=1' ;
}
2023-03-03 11:23:49 +01:00
$out .= '<input type="text" class="minwidth200' . ( $morecss ? ' ' . $morecss : '' ) . '" name="search_' . $htmlname . '" id="search_' . $htmlname . '" value="' . $selected_input_value . '"' . $placeholder . ' />' ;
2022-04-29 13:04:29 +02:00
2025-02-10 21:20:09 +01:00
$out .= ajax_autocompleter ( $selected , $htmlname , DOL_URL_ROOT . '/projet/ajax/projects.php' , $urloption , getDolGlobalInt ( 'PROJECT_USE_SEARCH_TO_SELECT' ), 0 , array ());
2020-05-21 15:05:19 +02:00
} else {
2022-11-23 21:28:33 +01:00
$out .= $this -> select_projects_list ( $socid , $selected , $htmlname , $maxlength , $option_only , $show_empty , abs ( $discard_closed ), $forcefocus , $disabled , 0 , $filterkey , 1 , $forceaddid , $htmlid , $morecss , $morefilter );
2017-12-12 10:37:51 +01:00
}
2021-02-23 22:03:23 +01:00
if ( $discard_closed > 0 ) {
2022-04-29 13:04:29 +02:00
if ( ! empty ( $form )) {
2020-04-10 10:59:32 +02:00
$out .= $form -> textwithpicto ( '' , $langs -> trans ( " ClosedProjectsAreHidden " ));
2016-01-28 11:59:18 +01:00
}
2015-06-15 10:04:36 +02:00
}
2017-06-02 12:27:46 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $nooutput )) {
2020-04-16 00:42:57 +02:00
print $out ;
return '' ;
2021-02-23 22:03:23 +01:00
} else {
return $out ;
}
2015-06-15 10:04:36 +02:00
}
2020-04-16 00:42:57 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2023-03-03 11:23:49 +01:00
2015-06-15 10:04:36 +02:00
/**
2015-10-04 16:02:50 +02:00
* Returns an array with projects qualified for a third party
2015-06-15 10:04:36 +02:00
*
2024-02-23 17:47:27 +01:00
* @ param int $socid Id third party ( - 1 = all , 0 = only projects not linked to a third party , id = projects not linked or linked to third party id )
* @ param int $selected Id project preselected
* @ param string $htmlname Name of html component
* @ param int $maxlength Maximum length of label
* @ param int $option_only Return only html options lines without the select tag
2023-04-24 01:32:47 +02:00
* @ param int | string $show_empty Add an empty line
* @ param int $discard_closed Discard closed projects ( 0 = Keep , 1 = hide completely , 2 = Disable )
2024-02-23 17:47:27 +01:00
* @ param int $forcefocus Force focus on field ( works with javascript only )
* @ param int $disabled Disabled
* @ param int $mode 0 for HTML mode and 1 for array return ( to be used by json_encode for example )
* @ param string $filterkey Key to filter on title or ref
* @ param int $nooutput No print output . Return it only .
* @ param int $forceaddid Force to add project id in list , event if not qualified
2024-02-28 23:42:23 +01:00
* @ param string $htmlid Html id to use instead of htmlname
2024-02-23 17:47:27 +01:00
* @ param string $morecss More CSS
* @ param string $morefilter More filters ( Must be a sql sanitized string )
2024-10-26 18:24:40 +02:00
* @ return int | string | array < array { key : int , value : string , ref : string , labelx : string , label : string , disabled : bool } > HTML string or array of option or < 0 if KO
2013-06-14 22:33:01 +02:00
*/
2024-02-28 23:42:23 +01:00
public function select_projects_list ( $socid = - 1 , $selected = 0 , $htmlname = 'projectid' , $maxlength = 24 , $option_only = 0 , $show_empty = 1 , $discard_closed = 0 , $forcefocus = 0 , $disabled = 0 , $mode = 0 , $filterkey = '' , $nooutput = 0 , $forceaddid = 0 , $htmlid = '' , $morecss = 'maxwidth500' , $morefilter = '' )
2020-04-16 00:42:57 +02:00
{
// phpcs:enable
2020-04-10 10:59:32 +02:00
global $user , $conf , $langs ;
2013-07-07 13:14:32 +02:00
2024-12-29 17:12:50 +01:00
require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php' ;
2013-06-14 22:33:01 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $htmlid )) {
$htmlid = $htmlname ;
}
2017-06-02 12:27:46 +02:00
2020-04-10 10:59:32 +02:00
$out = '' ;
$outarray = array ();
2017-06-02 12:27:46 +02:00
2013-06-14 22:33:01 +02:00
$hideunselectables = false ;
2023-11-27 11:24:19 +01:00
if ( getDolGlobalString ( 'PROJECT_HIDE_UNSELECTABLES' )) {
2021-02-23 22:03:23 +01:00
$hideunselectables = true ;
}
2024-04-04 16:09:19 +02:00
if ( getDolGlobalInt ( 'PROJECT_ALWAYS_DISCARD_CLOSED_PROJECTS_IN_SELECT' )) {
2024-04-04 15:41:08 +02:00
$discard_closed = 1 ;
}
2013-06-14 22:33:01 +02:00
$projectsListId = false ;
2023-10-15 15:32:35 +02:00
if ( ! $user -> hasRight ( 'projet' , 'all' , 'lire' )) {
2020-04-10 10:59:32 +02:00
$projectstatic = new Project ( $this -> db );
2019-01-27 11:55:16 +01:00
$projectsListId = $projectstatic -> getProjectsAuthorizedForUser ( $user , 0 , 1 );
2013-06-14 22:33:01 +02:00
}
// Search all projects
2022-01-27 12:52:13 +01:00
$sql = " SELECT p.rowid, p.ref, p.title, p.fk_soc, p.fk_statut, p.public, s.nom as name, s.name_alias " ;
2023-03-03 11:23:49 +01:00
$sql .= " FROM " . $this -> db -> prefix () . " projet as p LEFT JOIN " . $this -> db -> prefix () . " societe as s ON s.rowid = p.fk_soc " ;
$sql .= " WHERE p.entity IN ( " . getEntity ( 'project' ) . " ) " ;
2021-02-23 22:03:23 +01:00
if ( $projectsListId !== false ) {
2023-03-03 11:23:49 +01:00
$sql .= " AND p.rowid IN ( " . $this -> db -> sanitize ( $projectsListId ) . " ) " ;
2021-02-23 22:03:23 +01:00
}
if ( $socid == 0 ) {
$sql .= " AND (p.fk_soc=0 OR p.fk_soc IS NULL) " ;
}
if ( $socid > 0 ) {
2023-11-27 11:24:19 +01:00
if ( ! getDolGlobalString ( 'PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY' )) {
2023-03-03 11:23:49 +01:00
$sql .= " AND (p.fk_soc= " . (( int ) $socid ) . " OR p.fk_soc IS NULL) " ;
2023-12-13 15:20:53 +01:00
} elseif ( getDolGlobalString ( 'PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY' ) != 'all' ) { // PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY is 'all' or a list of ids separated by coma.
2023-10-15 15:32:35 +02:00
$sql .= " AND (p.fk_soc IN ( " . $this -> db -> sanitize ((( int ) $socid ) . " , " . getDolGlobalString ( 'PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY' )) . " ) OR p.fk_soc IS NULL) " ;
2020-04-16 00:42:57 +02:00
}
2017-03-15 12:33:06 +01:00
}
2021-02-23 22:03:23 +01:00
if ( ! empty ( $filterkey )) {
$sql .= natural_search ( array ( 'p.title' , 'p.ref' ), $filterkey );
}
2022-11-23 21:28:33 +01:00
if ( $morefilter ) {
2023-03-03 11:23:49 +01:00
$sql .= ' AND (' . $this -> db -> sanitize ( $morefilter , 0 , 1 ) . ')' ;
2022-11-23 21:28:33 +01:00
}
2020-04-10 10:59:32 +02:00
$sql .= " ORDER BY p.ref ASC " ;
2013-06-14 22:33:01 +02:00
2020-04-10 10:59:32 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
if ( ! empty ( $conf -> use_javascript_ajax )) {
2020-04-16 00:42:57 +02:00
$morecss .= ' minwidth100' ;
2015-01-30 19:57:38 +01:00
}
2013-06-14 22:33:01 +02:00
if ( empty ( $option_only )) {
2023-03-03 11:23:49 +01:00
$out .= '<select class="flat' . ( $morecss ? ' ' . $morecss : '' ) . '"' . ( $disabled ? ' disabled="disabled"' : '' ) . ' id="' . $htmlid . '" name="' . $htmlname . '">' ;
2013-06-14 22:33:01 +02:00
}
if ( ! empty ( $show_empty )) {
2023-04-24 01:32:47 +02:00
if ( is_numeric ( $show_empty )) {
$out .= '<option value="0"> </option>' ;
} else {
$out .= '<option value="-1">' . $show_empty . '</option>' ;
}
2013-06-14 22:33:01 +02:00
}
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 22:03:23 +01:00
if ( $num ) {
while ( $i < $num ) {
2013-06-14 22:33:01 +02:00
$obj = $this -> db -> fetch_object ( $resql );
// If we ask to filter on a company and user has no permission to see all companies and project is linked to another company, we hide project.
2022-09-10 11:14:25 +02:00
if ( $socid > 0 && ( empty ( $obj -> fk_soc ) || $obj -> fk_soc == $socid ) && ! $user -> hasRight ( 'societe' , 'lire' )) {
2013-06-14 22:33:01 +02:00
// Do nothing
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 22:03:23 +01:00
if ( $discard_closed == 1 && $obj -> fk_statut == 2 && $obj -> rowid != $selected ) { // We discard closed except if selected
2014-11-20 15:32:10 +01:00
$i ++ ;
continue ;
}
2020-04-10 10:59:32 +02:00
$labeltoshow = dol_trunc ( $obj -> ref , 18 );
2013-06-14 22:33:01 +02:00
//if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
//else $labeltoshow.=' ('.$langs->trans("Private").')';
2023-03-03 11:23:49 +01:00
$labeltoshow .= ', ' . dol_trunc ( $obj -> title , $maxlength );
2021-02-23 22:03:23 +01:00
if ( $obj -> name ) {
2023-03-03 11:23:49 +01:00
$labeltoshow .= ' - ' . $obj -> name ;
2021-02-23 22:03:23 +01:00
if ( $obj -> name_alias ) {
2023-03-03 11:23:49 +01:00
$labeltoshow .= ' (' . $obj -> name_alias . ')' ;
2021-02-23 22:03:23 +01:00
}
2017-04-05 11:51:55 +02:00
}
2017-06-02 12:27:46 +02:00
2020-04-10 10:59:32 +02:00
$disabled = 0 ;
2021-02-23 22:03:23 +01:00
if ( $obj -> fk_statut == 0 ) {
2020-04-10 10:59:32 +02:00
$disabled = 1 ;
2023-03-03 11:23:49 +01:00
$labeltoshow .= ' - ' . $langs -> trans ( " Draft " );
2021-02-23 22:03:23 +01:00
} elseif ( $obj -> fk_statut == 2 ) {
if ( $discard_closed == 2 ) {
$disabled = 1 ;
}
2023-03-03 11:23:49 +01:00
$labeltoshow .= ' - ' . $langs -> trans ( " Closed " );
2023-11-27 11:24:19 +01:00
} elseif ( ! getDolGlobalString ( 'PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY' ) && $socid > 0 && ( ! empty ( $obj -> fk_soc ) && $obj -> fk_soc != $socid )) {
2020-04-10 10:59:32 +02:00
$disabled = 1 ;
2023-03-03 11:23:49 +01:00
$labeltoshow .= ' - ' . $langs -> trans ( " LinkedToAnotherCompany " );
2015-01-28 19:27:56 +01:00
}
2015-01-30 16:33:39 +01:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $selected ) && $selected == $obj -> rowid ) {
2023-03-03 11:23:49 +01:00
$out .= '<option value="' . $obj -> rowid . '" selected' ;
2015-06-03 21:01:50 +02:00
//if ($disabled) $out.=' disabled'; // with select2, field can't be preselected if disabled
2023-03-03 11:23:49 +01:00
$out .= '>' . $labeltoshow . '</option>' ;
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 22:03:23 +01:00
if ( $hideunselectables && $disabled && ( $selected != $obj -> rowid )) {
2020-04-10 10:59:32 +02:00
$resultat = '' ;
2020-05-21 15:05:19 +02:00
} else {
2023-03-03 11:23:49 +01:00
$resultat = '<option value="' . $obj -> rowid . '"' ;
2021-02-23 22:03:23 +01:00
if ( $disabled ) {
$resultat .= ' disabled' ;
}
2013-06-14 22:33:01 +02:00
//if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
//else $labeltoshow.=' ('.$langs->trans("Private").')';
2020-04-10 10:59:32 +02:00
$resultat .= '>' ;
$resultat .= $labeltoshow ;
$resultat .= '</option>' ;
2013-06-14 22:33:01 +02:00
}
2020-04-10 10:59:32 +02:00
$out .= $resultat ;
2015-06-15 10:04:36 +02:00
$outarray [] = array (
2015-06-16 07:19:31 +02:00
'key' => ( int ) $obj -> rowid ,
2015-06-15 10:04:36 +02:00
'value' => $obj -> ref ,
'ref' => $obj -> ref ,
2020-10-16 20:17:54 +02:00
'labelx' => $labeltoshow ,
2023-03-03 11:23:49 +01:00
'label' => ( $disabled ? '<span class="opacitymedium">' . $labeltoshow . '</span>' : $labeltoshow ),
2015-06-16 07:19:31 +02:00
'disabled' => ( bool ) $disabled
2015-06-15 10:04:36 +02:00
);
2013-06-14 22:33:01 +02:00
}
}
$i ++ ;
}
}
2015-01-30 16:33:39 +01:00
2013-06-14 22:33:01 +02:00
$this -> db -> free ( $resql );
2015-06-15 10:04:36 +02:00
if ( ! $mode ) {
2021-02-23 22:03:23 +01:00
if ( empty ( $option_only )) {
$out .= '</select>' ;
}
2022-05-22 16:16:17 +02:00
// Use select2 selector
if ( ! empty ( $conf -> use_javascript_ajax )) {
2023-03-03 11:23:49 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php' ;
2022-05-22 16:16:17 +02:00
$comboenhancement = ajax_combobox ( $htmlid , array (), 0 , $forcefocus );
$out .= $comboenhancement ;
$morecss .= ' minwidth100' ;
}
2021-02-23 22:03:23 +01:00
if ( empty ( $nooutput )) {
2020-04-16 00:42:57 +02:00
print $out ;
return '' ;
2021-02-23 22:03:23 +01:00
} else {
return $out ;
}
2015-06-15 10:04:36 +02:00
} else {
return $outarray ;
}
2020-05-21 15:05:19 +02:00
} else {
2013-06-14 22:33:01 +02:00
dol_print_error ( $this -> db );
return - 1 ;
}
}
2013-07-07 13:14:32 +02:00
2015-06-11 18:27:59 +02:00
/**
2019-02-27 20:45:07 +01:00
* Output a combo list with tasks qualified for a third party
2015-06-11 18:27:59 +02:00
*
2024-06-14 19:39:26 +02:00
* @ param int $socid Id third party ( - 1 = all , 0 = only projects not linked to a third party , id = projects not linked or linked to third party id )
* @ param int $selected Id task preselected
* @ param string $htmlname Name of HTML select
* @ param int $maxlength Maximum length of label
2025-02-10 21:20:09 +01:00
* @ param int < 0 , 1 > $option_only Return only html options lines without the select tag
2024-06-14 19:39:26 +02:00
* @ param string $show_empty Add an empty line ( '1' or string to show for empty line )
2025-02-10 21:20:09 +01:00
* @ param int < 0 , 2 > $discard_closed Discard closed projects ( 0 = Keep , 1 = hide completely , 2 = Disable )
* @ param int < 0 , 1 > $forcefocus Force focus on field ( works with javascript only )
* @ param int < 0 , 1 > $disabled Disabled
2024-06-14 19:39:26 +02:00
* @ param string $morecss More css added to the select component
* @ param string $projectsListId '' = Automatic filter on project allowed . List of id = Filter on project ids .
2025-02-10 21:20:09 +01:00
* @ param 'all' | 'progress' | '' $showmore 'all' = Show project info , 'progress' = Show task progression , '' = Show nothing more
* @ param ? User $usertofilter User object to use for filtering
* @ param int < 0 , 1 > $nooutput 1 = Return string , do not send to output
2024-04-08 20:50:16 +02:00
*
* @ return int | string Nbr of tasks if OK , < 0 if KO . If nooutput = 1 : Return a HTML select string .
2015-06-11 18:27:59 +02:00
*/
2024-04-08 20:50:16 +02:00
public function selectTasks ( $socid = - 1 , $selected = 0 , $htmlname = 'taskid' , $maxlength = 24 , $option_only = 0 , $show_empty = '1' , $discard_closed = 0 , $forcefocus = 0 , $disabled = 0 , $morecss = 'maxwidth500' , $projectsListId = '' , $showmore = 'all' , $usertofilter = null , $nooutput = 0 )
2015-06-11 18:27:59 +02:00
{
2020-04-10 10:59:32 +02:00
global $user , $conf , $langs ;
2015-06-11 18:27:59 +02:00
2024-12-29 17:12:50 +01:00
require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php' ;
2015-06-11 18:27:59 +02:00
2021-02-23 22:03:23 +01:00
if ( is_null ( $usertofilter )) {
2018-10-30 12:28:04 +01:00
$usertofilter = $user ;
}
2020-04-10 10:59:32 +02:00
$out = '' ;
2015-06-11 18:27:59 +02:00
$hideunselectables = false ;
2023-11-27 11:24:19 +01:00
if ( getDolGlobalString ( 'PROJECT_HIDE_UNSELECTABLES' )) {
2021-02-23 22:03:23 +01:00
$hideunselectables = true ;
}
2015-06-11 18:27:59 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $projectsListId )) {
2023-10-15 15:32:35 +02:00
if ( ! $usertofilter -> hasRight ( 'projet' , 'all' , 'lire' )) {
2020-04-10 10:59:32 +02:00
$projectstatic = new Project ( $this -> db );
2019-01-27 11:55:16 +01:00
$projectsListId = $projectstatic -> getProjectsAuthorizedForUser ( $usertofilter , 0 , 1 );
2018-02-26 11:57:29 +01:00
}
2015-06-11 18:27:59 +02:00
}
// Search all projects
2022-01-27 12:52:13 +01:00
$sql = " SELECT t.rowid, t.ref as tref, t.label as tlabel, t.progress, " ;
$sql .= " p.rowid as pid, p.ref, p.title, p.fk_soc, p.fk_statut, p.public, p.usage_task, " ;
$sql .= " s.nom as name " ;
2023-03-03 11:23:49 +01:00
$sql .= " FROM " . $this -> db -> prefix () . " projet as p " ;
$sql .= " LEFT JOIN " . $this -> db -> prefix () . " societe as s ON s.rowid = p.fk_soc, " ;
$sql .= " " . $this -> db -> prefix () . " projet_task as t " ;
$sql .= " WHERE p.entity IN ( " . getEntity ( 'project' ) . " ) " ;
2020-04-10 10:59:32 +02:00
$sql .= " AND t.fk_projet = p.rowid " ;
2021-02-23 22:03:23 +01:00
if ( $projectsListId ) {
2023-03-03 11:23:49 +01:00
$sql .= " AND p.rowid IN ( " . $this -> db -> sanitize ( $projectsListId ) . " ) " ;
2021-02-23 22:03:23 +01:00
}
if ( $socid == 0 ) {
$sql .= " AND (p.fk_soc=0 OR p.fk_soc IS NULL) " ;
}
if ( $socid > 0 ) {
2023-03-03 11:23:49 +01:00
$sql .= " AND (p.fk_soc= " . (( int ) $socid ) . " OR p.fk_soc IS NULL) " ;
2021-02-23 22:03:23 +01:00
}
2020-04-10 10:59:32 +02:00
$sql .= " ORDER BY p.ref, t.ref ASC " ;
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2015-06-11 18:27:59 +02:00
// Use select2 selector
2021-02-23 22:03:23 +01:00
if ( empty ( $option_only ) && ! empty ( $conf -> use_javascript_ajax )) {
2023-03-03 11:23:49 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php' ;
2024-10-20 10:13:40 +02:00
$comboenhancement = ajax_combobox ( $htmlname , [], 0 , $forcefocus );
2020-04-16 00:42:57 +02:00
$out .= $comboenhancement ;
2023-09-15 15:04:40 +02:00
$morecss .= ' minwidth150imp' ;
2015-06-11 18:27:59 +02:00
}
if ( empty ( $option_only )) {
2023-03-03 11:23:49 +01:00
$out .= '<select class="valignmiddle flat' . ( $morecss ? ' ' . $morecss : '' ) . '"' . ( $disabled ? ' disabled="disabled"' : '' ) . ' id="' . $htmlname . '" name="' . $htmlname . '">' ;
2015-06-11 18:27:59 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! empty ( $show_empty )) {
$out .= '<option value="0" class="optiongrey">' ;
if ( ! is_numeric ( $show_empty )) {
2024-10-03 15:43:04 +02:00
//if (!empty($conf->use_javascript_ajax)) $out .= '<span class="opacitymedium">';
2020-04-10 10:59:32 +02:00
$out .= $show_empty ;
2022-08-31 22:14:20 +02:00
//if (!empty($conf->use_javascript_ajax)) $out .= '</span>';
2021-02-23 22:03:23 +01:00
} else {
$out .= ' ' ;
}
2020-04-10 10:59:32 +02:00
$out .= '</option>' ;
2015-06-11 18:27:59 +02:00
}
2020-10-08 17:49:26 +02:00
2015-06-11 18:27:59 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 22:03:23 +01:00
if ( $num ) {
while ( $i < $num ) {
2015-06-11 18:27:59 +02:00
$obj = $this -> db -> fetch_object ( $resql );
// If we ask to filter on a company and user has no permission to see all companies and project is linked to another company, we hide project.
2023-10-15 15:32:35 +02:00
if ( $socid > 0 && ( empty ( $obj -> fk_soc ) || $obj -> fk_soc == $socid ) && ! $usertofilter -> hasRight ( 'societe' , 'lire' )) {
2015-06-11 18:27:59 +02:00
// Do nothing
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 22:03:23 +01:00
if ( $discard_closed == 1 && $obj -> fk_statut == Project :: STATUS_CLOSED ) {
2015-06-11 18:27:59 +02:00
$i ++ ;
continue ;
}
2021-03-01 20:37:16 +01:00
$labeltoshow = '' ;
2024-05-24 14:11:51 +02:00
$labeltoshowhtml = '' ;
2015-07-04 02:08:06 +02:00
2020-10-08 17:49:26 +02:00
$disabled = 0 ;
2021-02-23 22:03:23 +01:00
if ( $obj -> fk_statut == Project :: STATUS_DRAFT ) {
2020-10-08 17:49:26 +02:00
$disabled = 1 ;
2021-02-23 22:03:23 +01:00
} elseif ( $obj -> fk_statut == Project :: STATUS_CLOSED ) {
if ( $discard_closed == 2 ) {
$disabled = 1 ;
}
} elseif ( $socid > 0 && ( ! empty ( $obj -> fk_soc ) && $obj -> fk_soc != $socid )) {
2020-10-08 17:49:26 +02:00
$disabled = 1 ;
}
2021-09-23 13:20:42 +02:00
if ( preg_match ( '/all/' , $showmore )) {
2020-04-10 10:59:32 +02:00
$labeltoshow .= dol_trunc ( $obj -> ref , 18 ); // Project ref
2018-02-26 11:57:29 +01:00
//if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
//else $labeltoshow.=' ('.$langs->trans("Private").')';
2023-03-03 11:23:49 +01:00
$labeltoshow .= ' ' . dol_trunc ( $obj -> title , $maxlength );
2024-05-24 14:11:51 +02:00
$labeltoshowhtml = $labeltoshow ;
2018-02-26 11:57:29 +01:00
2021-01-18 12:07:40 +01:00
if ( $obj -> name ) {
2023-03-03 11:23:49 +01:00
$labeltoshow .= ' (' . $obj -> name . ')' ;
2024-05-24 14:11:51 +02:00
$labeltoshowhtml .= ' <span class="opacitymedium">(' . $obj -> name . ')</span>' ;
2021-01-18 12:07:40 +01:00
}
2018-02-26 11:57:29 +01:00
2020-04-10 10:59:32 +02:00
$disabled = 0 ;
2021-01-18 12:07:40 +01:00
if ( $obj -> fk_statut == Project :: STATUS_DRAFT ) {
2020-04-10 10:59:32 +02:00
$disabled = 1 ;
2023-03-03 11:23:49 +01:00
$labeltoshow .= ' - ' . $langs -> trans ( " Draft " );
2024-05-24 14:11:51 +02:00
$labeltoshowhtml .= ' - <span class="opacitymedium">' . $langs -> trans ( " Draft " ) . '</span>' ;
2021-01-18 12:07:40 +01:00
} elseif ( $obj -> fk_statut == Project :: STATUS_CLOSED ) {
2021-02-23 22:03:23 +01:00
if ( $discard_closed == 2 ) {
$disabled = 1 ;
}
2023-03-03 11:23:49 +01:00
$labeltoshow .= ' - ' . $langs -> trans ( " Closed " );
2024-05-24 14:11:51 +02:00
$labeltoshowhtml .= ' - <span class="opacitymedium">' . $langs -> trans ( " Closed " ) . '</span>' ;
2021-01-18 12:07:40 +01:00
} elseif ( $socid > 0 && ( ! empty ( $obj -> fk_soc ) && $obj -> fk_soc != $socid )) {
2020-04-10 10:59:32 +02:00
$disabled = 1 ;
2023-03-03 11:23:49 +01:00
$labeltoshow .= ' - ' . $langs -> trans ( " LinkedToAnotherCompany " );
2024-05-24 14:11:51 +02:00
$labeltoshowhtml .= ' - <span class="opacitymedium">' . $langs -> trans ( " LinkedToAnotherCompany " ) . '</span>' ;
2018-02-26 11:57:29 +01:00
}
2020-04-10 10:59:32 +02:00
$labeltoshow .= ' - ' ;
2024-05-24 14:11:51 +02:00
$labeltoshowhtml .= ' - ' ;
2015-06-11 18:27:59 +02:00
}
2018-02-26 11:57:29 +01:00
2015-06-11 18:27:59 +02:00
// Label for task
2023-03-03 11:23:49 +01:00
$labeltoshow .= $obj -> tref . ' ' . dol_trunc ( $obj -> tlabel , $maxlength );
2024-05-24 14:11:51 +02:00
$labeltoshowhtml .= $obj -> tref . ' - ' . dol_trunc ( $obj -> tlabel , $maxlength );
2021-09-23 13:20:42 +02:00
if ( $obj -> usage_task && preg_match ( '/progress/' , $showmore )) {
2023-03-03 11:23:49 +01:00
$labeltoshow .= ' <span class="opacitymedium">(' . $obj -> progress . '%)</span>' ;
2024-05-24 14:11:51 +02:00
$labeltoshowhtml .= ' <span class="opacitymedium">(' . $obj -> progress . '%)</span>' ;
2021-09-23 13:20:42 +02:00
}
2015-06-29 16:57:32 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $selected ) && $selected == $obj -> rowid ) {
2023-03-03 11:23:49 +01:00
$out .= '<option value="' . $obj -> rowid . '" selected' ;
2024-05-24 14:11:51 +02:00
$out .= ' data-html="' . dol_escape_htmltag ( $labeltoshowhtml ) . '"' ;
2015-06-11 18:27:59 +02:00
//if ($disabled) $out.=' disabled'; // with select2, field can't be preselected if disabled
2023-03-03 11:23:49 +01:00
$out .= '>' . $labeltoshow . '</option>' ;
2020-05-21 15:05:19 +02:00
} else {
2021-02-23 22:03:23 +01:00
if ( $hideunselectables && $disabled && ( $selected != $obj -> rowid )) {
2020-04-10 10:59:32 +02:00
$resultat = '' ;
2020-05-21 15:05:19 +02:00
} else {
2023-03-03 11:23:49 +01:00
$resultat = '<option value="' . $obj -> rowid . '"' ;
2021-02-23 22:03:23 +01:00
if ( $disabled ) {
$resultat .= ' disabled' ;
}
2015-06-11 18:27:59 +02:00
//if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
//else $labeltoshow.=' ('.$langs->trans("Private").')';
2024-05-24 14:11:51 +02:00
$resultat .= ' data-html="' . dol_escape_htmltag ( $labeltoshowhtml ) . '"' ;
2020-04-10 10:59:32 +02:00
$resultat .= '>' ;
$resultat .= $labeltoshow ;
$resultat .= '</option>' ;
2015-06-11 18:27:59 +02:00
}
2020-04-10 10:59:32 +02:00
$out .= $resultat ;
2015-06-11 18:27:59 +02:00
}
}
$i ++ ;
}
}
if ( empty ( $option_only )) {
2020-04-10 10:59:32 +02:00
$out .= '</select>' ;
2015-06-11 18:27:59 +02:00
}
2021-09-23 13:20:42 +02:00
$this -> nboftasks = $num ;
2024-04-08 20:50:16 +02:00
$this -> db -> free ( $resql );
2021-09-23 13:20:42 +02:00
2024-04-08 20:50:16 +02:00
// Output or return
if ( empty ( $nooutput )) {
print $out ;
} else {
return $out ;
}
2015-06-11 18:27:59 +02:00
return $num ;
2020-05-21 15:05:19 +02:00
} else {
2015-06-11 18:27:59 +02:00
dol_print_error ( $this -> db );
return - 1 ;
}
}
2015-06-29 16:57:32 +02:00
2020-04-16 00:42:57 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2023-03-03 11:23:49 +01:00
2013-06-17 14:25:33 +02:00
/**
2014-10-29 20:21:24 +01:00
* Build a HTML select list of element of same thirdparty to suggest to link them to project
2013-06-17 14:25:33 +02:00
*
2023-03-03 11:23:49 +01:00
* @ param string $table_element Table of the element to update
2024-02-28 23:42:23 +01:00
* @ param int | string $socid If of thirdparty to use as filter or 'id1,id2,...'
2023-03-03 11:23:49 +01:00
* @ param string $morecss More CSS
* @ param int $limitonstatus Add filters to limit length of list to opened status ( for example to avoid ERR_RESPONSE_HEADERS_TOO_BIG on project / element . php page ) . TODO To implement
* @ param string $projectkey Equivalent key to fk_projet for actual table_element
* @ param string $placeholder Placeholder
* @ return int | string The HTML select list of element or '' if nothing or - 1 if KO
2013-06-17 14:25:33 +02:00
*/
2022-01-01 18:15:22 +01:00
public function select_element ( $table_element , $socid = 0 , $morecss = '' , $limitonstatus = - 2 , $projectkey = " fk_projet " , $placeholder = '' )
2013-06-17 14:25:33 +02:00
{
2020-04-16 00:42:57 +02:00
// phpcs:enable
2014-10-29 20:21:24 +01:00
global $conf , $langs ;
2013-07-07 13:14:32 +02:00
2021-02-23 22:03:23 +01:00
if ( $table_element == 'projet_task' ) {
2024-10-13 17:17:37 +02:00
return '' ; // Special case of element we never link to a project (already always done)
2021-02-23 22:03:23 +01:00
}
2015-06-03 21:01:50 +02:00
2020-04-10 10:59:32 +02:00
$linkedtothirdparty = false ;
2023-12-04 12:04:36 +01:00
if ( ! in_array (
$table_element ,
array (
2023-03-03 11:23:49 +01:00
'don' ,
'expensereport_det' ,
'expensereport' , 'loan' ,
'stock_mouvement' ,
'payment_salary' ,
'payment_various' ,
'salary' ,
'chargesociales' ,
'entrepot' )
2021-10-31 15:14:15 +01:00
)) {
2021-02-23 22:03:23 +01:00
$linkedtothirdparty = true ;
}
2015-06-30 01:34:17 +02:00
2020-04-10 10:59:32 +02:00
$sqlfilter = '' ;
2018-08-13 17:26:32 +02:00
2015-11-26 00:47:17 +01:00
//print $table_element;
2021-02-23 22:03:23 +01:00
switch ( $table_element ) {
2017-01-07 17:54:47 +01:00
case " loan " :
$sql = " SELECT t.rowid, t.label as ref " ;
break ;
2013-06-17 14:25:33 +02:00
case " facture " :
2018-12-02 14:31:45 +01:00
$sql = " SELECT t.rowid, t.ref as ref " ;
2013-06-17 14:25:33 +02:00
break ;
case " facture_fourn " :
2015-06-30 01:34:17 +02:00
$sql = " SELECT t.rowid, t.ref, t.ref_supplier " ;
2014-10-29 20:21:24 +01:00
break ;
case " commande_fourn " :
2015-11-26 00:47:17 +01:00
case " commande_fournisseur " :
2020-04-16 00:42:57 +02:00
$sql = " SELECT t.rowid, t.ref, t.ref_supplier " ;
2013-06-17 14:25:33 +02:00
break ;
case " facture_rec " :
2015-06-30 01:34:17 +02:00
$sql = " SELECT t.rowid, t.titre as ref " ;
2013-06-17 14:25:33 +02:00
break ;
case " actioncomm " :
2015-06-30 01:34:17 +02:00
$sql = " SELECT t.id as rowid, t.label as ref " ;
2020-04-10 10:59:32 +02:00
$projectkey = " fk_project " ;
2013-06-17 14:25:33 +02:00
break ;
2016-07-01 17:49:13 +02:00
case " expensereport " :
2015-02-21 15:18:05 +01:00
return '' ;
2016-07-01 17:49:13 +02:00
case " expensereport_det " :
2015-02-21 15:18:05 +01:00
/* $sql = " SELECT rowid, '' as ref " ; // table is llx_expensereport_det
$projectkey = " fk_projet " ;
break ; */
2016-07-01 17:49:13 +02:00
return '' ;
case " commande " :
2020-04-16 00:42:57 +02:00
case " contrat " :
2015-11-26 00:47:17 +01:00
case " fichinter " :
2020-04-16 00:42:57 +02:00
$sql = " SELECT t.rowid, t.ref " ;
break ;
2017-01-29 13:36:33 +01:00
case 'stock_mouvement' :
2022-01-27 12:52:13 +01:00
$sql = " SELECT t.rowid, t.label as ref " ;
2020-04-10 10:59:32 +02:00
$projectkey = 'fk_origin' ;
2017-01-29 13:36:33 +01:00
break ;
2018-10-26 19:00:24 +02:00
case " payment_salary " :
2020-04-10 10:59:32 +02:00
$sql = " SELECT t.rowid, t.num_payment as ref " ; // TODO In a future fill and use real ref field
2018-10-26 19:00:24 +02:00
break ;
2017-09-11 14:29:27 +02:00
case " payment_various " :
$sql = " SELECT t.rowid, t.num_payment as ref " ;
break ;
2017-03-12 14:32:49 +01:00
case " chargesociales " :
2013-06-17 14:25:33 +02:00
default :
2015-06-30 01:34:17 +02:00
$sql = " SELECT t.rowid, t.ref " ;
2013-06-17 14:25:33 +02:00
break ;
}
2021-02-23 22:03:23 +01:00
if ( $linkedtothirdparty ) {
$sql .= " , s.nom as name " ;
}
2023-03-03 11:23:49 +01:00
$sql .= " FROM " . $this -> db -> prefix () . $table_element . " as t " ;
2021-02-23 22:03:23 +01:00
if ( $linkedtothirdparty ) {
2023-03-03 11:23:49 +01:00
$sql .= " , " . $this -> db -> prefix () . " societe as s " ;
2021-02-23 22:03:23 +01:00
}
2023-03-03 11:23:49 +01:00
$sql .= " WHERE " . $projectkey . " is null " ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $socid ) && $linkedtothirdparty ) {
if ( is_numeric ( $socid )) {
2023-03-03 11:23:49 +01:00
$sql .= " AND t.fk_soc = " . (( int ) $socid );
2021-02-23 22:03:23 +01:00
} else {
2023-03-03 11:23:49 +01:00
$sql .= " AND t.fk_soc IN ( " . $this -> db -> sanitize ( $socid ) . " ) " ;
2021-02-23 22:03:23 +01:00
}
}
if ( ! in_array ( $table_element , array ( 'expensereport_det' , 'stock_mouvement' ))) {
2023-03-03 11:23:49 +01:00
$sql .= ' AND t.entity IN (' . getEntity ( 'project' ) . ')' ;
2021-02-23 22:03:23 +01:00
}
if ( $linkedtothirdparty ) {
$sql .= " AND s.rowid = t.fk_soc " ;
}
if ( $sqlfilter ) {
2023-03-03 11:23:49 +01:00
$sql .= " AND " . $sqlfilter ;
2016-12-22 16:52:03 +01:00
}
2020-04-10 10:59:32 +02:00
$sql .= " ORDER BY ref DESC " ;
2013-07-07 13:14:32 +02:00
2023-03-03 11:23:49 +01:00
dol_syslog ( get_class ( $this ) . '::select_element' , LOG_DEBUG );
2020-04-10 10:59:32 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2013-06-17 14:25:33 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2023-01-31 21:17:06 +01:00
$sellist = '' ;
2021-02-23 22:03:23 +01:00
if ( $num > 0 ) {
2023-03-03 11:23:49 +01:00
$sellist = '<select class="flat elementselect css' . $table_element . ( $morecss ? ' ' . $morecss : '' ) . '" name="elementselect">' ;
$sellist .= '<option value="-1"' . ( $placeholder ? ' class="optiongrey"' : '' ) . '>' . $placeholder . '</option>' ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2013-06-17 14:25:33 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2020-04-10 10:59:32 +02:00
$ref = $obj -> ref ? $obj -> ref : $obj -> rowid ;
2021-02-23 22:03:23 +01:00
if ( ! empty ( $obj -> ref_supplier )) {
2023-03-03 11:23:49 +01:00
$ref .= ' (' . $obj -> ref_supplier . ')' ;
2021-02-23 22:03:23 +01:00
}
if ( ! empty ( $obj -> name )) {
2023-03-03 11:23:49 +01:00
$ref .= ' - ' . $obj -> name ;
2021-02-23 22:03:23 +01:00
}
2023-03-03 11:23:49 +01:00
$sellist .= '<option value="' . $obj -> rowid . '">' . $ref . '</option>' ;
2013-06-17 14:25:33 +02:00
$i ++ ;
}
2020-04-10 10:59:32 +02:00
$sellist .= '</select>' ;
2013-06-17 14:25:33 +02:00
}
2014-10-29 20:21:24 +01:00
/* else
{
$sellist = '<select class="flat" name="elementselect">' ;
2015-05-07 11:57:23 +02:00
$sellist .= '<option value="0" disabled>' . $langs -> trans ( " None " ) . '</option>' ;
2014-10-29 20:21:24 +01:00
$sellist .= '</select>' ;
} */
2013-06-17 14:25:33 +02:00
$this -> db -> free ( $resql );
2014-10-29 20:21:24 +01:00
2015-02-21 15:18:05 +01:00
return $sellist ;
2020-05-21 15:05:19 +02:00
} else {
2015-06-30 01:34:17 +02:00
dol_print_error ( $this -> db );
2020-04-10 10:59:32 +02:00
$this -> error = $this -> db -> lasterror ();
$this -> errors [] = $this -> db -> lasterror ();
2023-03-03 11:23:49 +01:00
dol_syslog ( get_class ( $this ) . " ::select_element " . $this -> error , LOG_ERR );
2014-06-13 15:54:37 +02:00
return - 1 ;
2013-06-17 14:25:33 +02:00
}
}
2013-07-07 13:14:32 +02:00
2013-06-14 22:33:01 +02:00
2015-06-29 16:57:32 +02:00
/**
* Build a HTML select list of element of same thirdparty to suggest to link them to project
*
2023-03-03 11:23:49 +01:00
* @ param string $htmlname HTML name
2025-02-10 21:20:09 +01:00
* @ param int | 'all' | 'none' | 'notopenedopp' $preselected Preselected ( int or 'all' or 'none' )
* @ param int < 0 , 1 > $showempty Add an empty line
* @ param int < 0 , 1 > $useshortlabel Use short label
* @ param int < 0 , 1 > $showallnone Add choice " All " and " None "
* @ param int < 0 , 1 > $showpercent Show default probability for status
* @ param string $morecss Add more css
* @ param int < 0 , 1 > $noadmininfo 0 = Add admin info , 1 = Disable admin info
* @ param int < 0 , 1 > $addcombojs 1 = Add a js combo
2024-03-20 17:59:48 +01:00
* @ return int <- 1 , - 1 >| string The HTML select list of element or '' if nothing or - 1 if KO
2015-06-29 16:57:32 +02:00
*/
2025-02-10 21:20:09 +01:00
public function selectOpportunityStatus ( $htmlname , $preselected = - 1 , $showempty = 1 , $useshortlabel = 0 , $showallnone = 0 , $showpercent = 0 , $morecss = '' , $noadmininfo = 0 , $addcombojs = 0 )
2015-06-29 16:57:32 +02:00
{
2020-05-15 15:50:50 +02:00
global $conf , $langs , $user ;
2015-06-29 16:57:32 +02:00
2015-06-30 01:34:17 +02:00
$sql = " SELECT rowid, code, label, percent " ;
2023-03-03 11:23:49 +01:00
$sql .= " FROM " . $this -> db -> prefix () . 'c_lead_status' ;
2020-04-10 10:59:32 +02:00
$sql .= " WHERE active = 1 " ;
$sql .= " ORDER BY position " ;
2015-06-29 16:57:32 +02:00
2020-04-10 10:59:32 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2015-06-29 16:57:32 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2024-03-20 17:59:48 +01:00
$sellist = '' ;
2021-02-23 22:03:23 +01:00
if ( $num > 0 ) {
2023-03-03 11:23:49 +01:00
$sellist = '<select class="flat oppstatus' . ( $morecss ? ' ' . $morecss : '' ) . '" id="' . $htmlname . '" name="' . $htmlname . '">' ;
2018-10-01 21:03:33 +02:00
if ( $showempty ) {
2020-04-16 00:42:57 +02:00
// Without  , strange move of screen when switching value
$sellist .= '<option value="-1"> </option>' ;
}
2018-10-01 21:03:33 +02:00
if ( $showallnone ) {
2023-03-03 11:23:49 +01:00
$sellist .= '<option value="all"' . ( $preselected == 'all' ? ' selected="selected"' : '' ) . '>-- ' . $langs -> trans ( " OnlyOpportunitiesShort " ) . '</option>' ;
$sellist .= '<option value="openedopp"' . ( $preselected == 'openedopp' ? ' selected="selected"' : '' ) . '>-- ' . $langs -> trans ( " OpenedOpportunitiesShort " ) . '</option>' ;
$sellist .= '<option value="notopenedopp"' . ( $preselected == 'notopenedopp' ? ' selected="selected"' : '' ) . '>-- ' . $langs -> trans ( " NotOpenedOpportunitiesShort " ) . '</option>' ;
$sellist .= '<option value="none"' . ( $preselected == 'none' ? ' selected="selected"' : '' ) . '>-- ' . $langs -> trans ( " NotAnOpportunityShort " ) . '</option>' ;
2020-04-16 00:42:57 +02:00
}
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2015-06-29 16:57:32 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2023-03-03 11:23:49 +01:00
$sellist .= '<option value="' . $obj -> rowid . '" defaultpercent="' . $obj -> percent . '" elemcode="' . $obj -> code . '"' ;
2021-02-23 22:03:23 +01:00
if ( $obj -> rowid == $preselected ) {
$sellist .= ' selected="selected"' ;
}
2015-06-30 01:34:17 +02:00
$sellist .= '>' ;
2021-02-23 22:03:23 +01:00
if ( $useshortlabel ) {
2023-03-03 11:23:49 +01:00
$finallabel = ( $langs -> transnoentitiesnoconv ( " OppStatus " . $obj -> code ) != " OppStatus " . $obj -> code ? $langs -> transnoentitiesnoconv ( " OppStatus " . $obj -> code ) : $obj -> label );
2020-05-21 15:05:19 +02:00
} else {
2023-03-03 11:23:49 +01:00
$finallabel = ( $langs -> transnoentitiesnoconv ( " OppStatus " . $obj -> code ) != " OppStatus " . $obj -> code ? $langs -> transnoentitiesnoconv ( " OppStatus " . $obj -> code ) : $obj -> label );
2021-02-23 22:03:23 +01:00
if ( $showpercent ) {
2023-03-03 11:23:49 +01:00
$finallabel .= ' (' . $obj -> percent . '%)' ;
2021-02-23 22:03:23 +01:00
}
2015-06-30 01:34:17 +02:00
}
$sellist .= $finallabel ;
2020-04-10 10:59:32 +02:00
$sellist .= '</option>' ;
2015-06-29 16:57:32 +02:00
$i ++ ;
}
2020-04-10 10:59:32 +02:00
$sellist .= '</select>' ;
2020-12-11 01:20:40 +01:00
2021-02-23 22:03:23 +01:00
if ( $user -> admin && ! $noadmininfo ) {
$sellist .= info_admin ( $langs -> trans ( " YouCanChangeValuesForThisListFromDictionarySetup " ), 1 );
}
2020-12-11 01:20:40 +01:00
2021-02-23 22:03:23 +01:00
if ( $addcombojs ) {
$sellist .= ajax_combobox ( $htmlname );
}
2015-06-29 16:57:32 +02:00
}
/* else
{
$sellist = '<select class="flat" name="elementselect">' ;
$sellist .= '<option value="0" disabled>' . $langs -> trans ( " None " ) . '</option>' ;
$sellist .= '</select>' ;
} */
$this -> db -> free ( $resql );
return $sellist ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$this -> error = $this -> db -> lasterror ();
$this -> errors [] = $this -> db -> lasterror ();
2023-03-03 11:23:49 +01:00
dol_syslog ( get_class ( $this ) . " ::selectOpportunityStatus " . $this -> error , LOG_ERR );
2015-06-29 16:57:32 +02:00
return - 1 ;
}
}
2023-03-03 11:18:19 +01:00
2023-10-02 22:57:53 +02:00
/**
2024-01-13 19:48:20 +01:00
* Return combo list of different statuses of orders
2023-10-02 22:57:53 +02:00
*
2025-02-10 21:20:09 +01:00
* @ param string $selected Preselected value
* @ param int < 0 , 1 > $short Use short labels
* @ param string $htmlname Name of HTML select element
2023-10-02 22:57:53 +02:00
* @ return void
*/
2025-02-10 21:20:09 +01:00
public function selectProjectsStatus ( $selected = '' , $short = 0 , $htmlname = 'order_status' )
2023-10-02 22:57:53 +02:00
{
$options = array ();
// 7 is same label than 6. 8 does not exists (billed is another field)
$statustohow = array (
'0' => '0' ,
'1' => '1' ,
'2' => '2' ,
);
2024-12-29 17:12:50 +01:00
require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php' ;
2023-10-02 22:57:53 +02:00
$tmpproject = new Project ( $this -> db );
foreach ( $statustohow as $key => $value ) {
2024-12-29 17:12:50 +01:00
$tmpproject -> statut = $key ; // deprecated
$tmpproject -> status = $key ;
2023-10-02 22:57:53 +02:00
$options [ $value ] = $tmpproject -> getLibStatut ( $short );
}
if ( is_array ( $selected )) {
$selectedarray = $selected ;
} elseif ( $selected == 99 ) {
$selectedarray = array ( 0 , 1 );
} else {
$selectedarray = explode ( ',' , $selected );
}
2025-02-10 21:20:09 +01:00
print Form :: multiselectarray ( $htmlname , $options , $selectedarray , 0 , 0 , 'minwidth100' );
2023-10-02 22:57:53 +02:00
}
2023-03-03 11:18:19 +01:00
/**
* Output a combo list with invoices and lines qualified for a project
*
* @ param int $selectedInvoiceId Id invoice preselected
* @ param int $selectedLineId Id invoice line preselected
2023-03-03 11:57:14 +01:00
* @ param string $htmlNameInvoice Name of HTML select for Invoice
2024-02-28 23:42:23 +01:00
* @ param string $htmlNameInvoiceLine Name of HTML select for Invoice Line
2023-03-03 11:18:19 +01:00
* @ param string $morecss More css added to the select component
2024-10-26 18:24:40 +02:00
* @ param array < string , int > $filters Array of filters
2023-03-03 11:57:14 +01:00
* @ param int $lineOnly return only option for line
2023-03-05 10:40:31 +01:00
* @ return string HTML Select
2023-03-03 11:18:19 +01:00
*/
2023-03-03 11:57:14 +01:00
public function selectInvoiceAndLine ( $selectedInvoiceId = 0 , $selectedLineId = 0 , $htmlNameInvoice = 'invoiceid' , $htmlNameInvoiceLine = 'invoicelineid' , $morecss = 'maxwidth500' , $filters = array (), $lineOnly = 0 )
2023-03-03 11:18:19 +01:00
{
global $user , $conf , $langs ;
require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php' ;
$out = '' ;
2023-03-03 11:57:14 +01:00
if ( empty ( $lineOnly )) {
// Search Invoice
$sql = " SELECT f.rowid, f.ref as fref, " ;
$sql .= ' s.nom as name' ;
$sql .= ' FROM ' . $this -> db -> prefix () . 'projet as p' ;
$sql .= ' INNER JOIN ' . $this -> db -> prefix () . 'societe as s ON s.rowid = p.fk_soc' ;
$sql .= ' INNER JOIN ' . $this -> db -> prefix () . 'facture as f ON f.fk_projet = p.rowid' ;
$sql .= " WHERE p.entity IN ( " . getEntity ( 'project' ) . " ) " ;
if ( ! empty ( $filters )) {
foreach ( $filters as $key => $value ) {
if ( $key == 'p.rowid' ) {
$sql .= " AND p.rowid= " . ( int ) $value ;
}
if ( $key == 'f.rowid' ) {
$sql .= " AND f.rowid= " . ( int ) $value ;
}
2023-03-03 11:18:19 +01:00
}
}
2023-03-03 11:57:14 +01:00
$sql .= " ORDER BY p.ref, f.ref ASC " ;
2023-03-03 11:18:19 +01:00
2023-03-03 11:57:14 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
// Use select2 selector
if ( ! empty ( $conf -> use_javascript_ajax )) {
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php' ;
2024-03-20 17:59:48 +01:00
$comboenhancement = ajax_combobox ( $htmlNameInvoice , array ( array ( 'method' => 'getLines' , 'url' => dol_buildpath ( '/core/ajax/ajaxinvoiceline.php' , 1 ), 'htmlname' => $htmlNameInvoiceLine )), 0 , 0 );
2023-03-03 11:57:14 +01:00
$out .= $comboenhancement ;
$morecss = 'minwidth200imp maxwidth500' ;
}
2023-03-03 11:18:19 +01:00
2023-03-03 11:57:14 +01:00
$out .= '<select class="valignmiddle flat' . ( $morecss ? ' ' . $morecss : '' ) . '" id="' . $htmlNameInvoice . '" name="' . $htmlNameInvoice . '">' ;
$num = $this -> db -> num_rows ( $resql );
if ( $num ) {
while ( $obj = $this -> db -> fetch_object ( $resql )) {
$labeltoshow = $obj -> fref ; // Invoice ref
if ( $obj -> name ) {
$labeltoshow .= ' - ' . $obj -> name ;
}
2023-03-03 11:18:19 +01:00
2023-03-03 11:57:14 +01:00
$out .= '<option value="' . $obj -> rowid . '" ' ;
if ( ! empty ( $selectedInvoiceId ) && $selectedInvoiceId == $obj -> rowid ) {
$out .= ' selected ' ;
}
$out .= '>' . $labeltoshow . '</option>' ;
2023-03-03 11:18:19 +01:00
}
}
2023-03-03 11:57:14 +01:00
$out .= '</select>' ;
} else {
2024-07-03 02:30:53 +02:00
dol_print_error ( $this -> db , $this -> db -> lasterror );
2023-03-05 10:40:31 +01:00
return '' ;
2023-03-03 11:18:19 +01:00
}
}
// Search Invoice Line
$sql = " SELECT fd.rowid, fd.label, fd.description " ;
$sql .= ' FROM ' . $this -> db -> prefix () . 'projet as p' ;
$sql .= ' INNER JOIN ' . $this -> db -> prefix () . 'societe as s ON s.rowid = p.fk_soc' ;
$sql .= ' INNER JOIN ' . $this -> db -> prefix () . 'facture as f ON f.fk_projet = p.rowid' ;
$sql .= ' INNER JOIN ' . $this -> db -> prefix () . 'facturedet as fd ON fd.fk_facture = f.rowid' ;
$sql .= " WHERE p.entity IN ( " . getEntity ( 'project' ) . " ) " ;
if ( ! empty ( $filters )) {
foreach ( $filters as $key => $value ) {
if ( $key == 'p.rowid' ) {
$sql .= " AND p.rowid= " . ( int ) $value ;
}
}
}
if ( ! empty ( $selectedInvoiceId )) {
$sql .= " AND f.rowid= " . ( int ) $selectedInvoiceId ;
}
$sql .= " ORDER BY p.ref, f.ref ASC " ;
2023-03-03 11:57:14 +01:00
$resql = $this -> db -> query ( $sql );
2023-03-03 11:18:19 +01:00
if ( $resql ) {
// Use select2 selector
2023-03-03 11:57:14 +01:00
if ( empty ( $lineOnly )) {
if ( ! empty ( $conf -> use_javascript_ajax )) {
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php' ;
2024-10-20 10:13:40 +02:00
$comboenhancement = ajax_combobox ( $htmlNameInvoiceLine , [], 0 , 0 );
2023-03-03 11:57:14 +01:00
$out .= $comboenhancement ;
$morecss = 'minwidth200imp maxwidth500' ;
}
2023-03-03 11:18:19 +01:00
2023-03-03 11:57:14 +01:00
$out .= '<select class="valignmiddle flat' . ( $morecss ? ' ' . $morecss : '' ) . '" id="' . $htmlNameInvoiceLine . '" name="' . $htmlNameInvoiceLine . '">' ;
}
2023-03-03 11:18:19 +01:00
$num = $this -> db -> num_rows ( $resql );
if ( $num ) {
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2023-03-03 11:57:14 +01:00
$labeltoshow .= $obj -> description ; // Invoice ref
2023-03-03 11:18:19 +01:00
$out .= '<option value="' . $obj -> rowid . '" ' ;
2023-03-03 11:57:14 +01:00
if ( ! empty ( $selectedLineId ) && $selectedLineId == $obj -> rowid ) {
2023-03-03 11:18:19 +01:00
$out .= ' selected ' ;
}
$out .= '>' . $labeltoshow . '</option>' ;
}
}
2023-03-03 11:57:14 +01:00
if ( empty ( $lineOnly )) {
$out .= '</select>' ;
}
2023-03-03 11:18:19 +01:00
} else {
2024-07-03 02:30:53 +02:00
dol_print_error ( $this -> db , $this -> db -> lasterror );
2023-03-05 10:40:31 +01:00
return '' ;
2023-03-03 11:18:19 +01:00
}
return $out ;
}
2024-02-09 15:58:49 +01:00
/**
* Output html select to select opportunity status
*
* @ param string $page Page
* @ param string $selected Id preselected
2025-02-10 21:20:09 +01:00
* @ param '' | int $percent_value percentage of the opportunity
2024-02-09 15:58:49 +01:00
* @ param string $htmlname_status name of HTML element for status select
* @ param string $htmlname_percent name of HTML element for percent input
* @ param string $filter optional filters criteras
2025-02-10 21:20:09 +01:00
* @ param int < 0 , 1 > $nooutput No print output . Return it only .
2024-02-09 15:58:49 +01:00
* @ return void | string
*/
public function formOpportunityStatus ( $page , $selected = '' , $percent_value = 0 , $htmlname_status = 'none' , $htmlname_percent = 'none' , $filter = '' , $nooutput = 0 )
{
// phpcs:enable
global $conf , $langs ;
$out = '' ;
if ( $htmlname_status != " none " && $htmlname_percent != 'none' ) {
$out .= '<form method="post" action="' . $page . '">' ;
$out .= '<input type="hidden" name="action" value="set_opp_status">' ;
$out .= '<input type="hidden" name="token" value="' . newToken () . '">' ;
$out .= $this -> selectOpportunityStatus ( $htmlname_status , $selected , 1 , 0 , 0 , 0 , 'minwidth150 inline-block valignmiddle' , 1 , 1 );
$out .= ' / <span title="' . $langs -> trans ( " OpportunityProbability " ) . '"> ' ;
$out .= '<input class="width50 right" type="text" id="' . $htmlname_percent . '" name="' . $htmlname_percent . '" title="' . dol_escape_htmltag ( $langs -> trans ( " OpportunityProbability " )) . '" value="' . $percent_value . '"> %' ;
$out .= '</span>' ;
$out .= '<input type="submit" class="button smallpaddingimp valignmiddle" value="' . $langs -> trans ( " Modify " ) . '">' ;
$out .= '</form>' ;
} else {
if ( $selected > 0 ) {
$code = dol_getIdFromCode ( $this -> db , $selected , 'c_lead_status' , 'rowid' , 'code' );
$out .= $langs -> trans ( " OppStatus " . $code );
// Opportunity percent
2024-03-20 17:59:48 +01:00
$out .= ' / <span title="' . $langs -> trans ( " OpportunityProbability " ) . '"> ' ;
$out .= price ( $percent_value , 0 , $langs , 1 , 0 ) . ' %' ;
$out .= '</span>' ;
2024-02-09 15:58:49 +01:00
} else {
$out .= " " ;
}
}
if ( $nooutput ) {
return $out ;
} else {
print $out ;
}
}
2014-07-14 23:36:10 +02:00
}