/** * Output a combo list with projects qualified for a third party * * @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 Nom de la zone html * @param int $maxlength Maximum length of label * @param int $option_only Return only html options lines without the select tag * @param int $show_empty Add an empty line * @param int $discard_closed Discard closed projects (0=Keep,1=hide completely,2=Disable) * @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 * @return int Nber of project if OK, <0 if KO */ 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 = '') { global $langs, $conf, $form; if (!empty($conf->use_javascript_ajax) && !empty($conf->global->PROJECT_USE_SEARCH_TO_SELECT)) { $placeholder = ''; if ($selected && empty($selected_input_value)) { require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php'; $project = new Project($this->db); $project->fetch($selected); $selected_input_value = $project->ref; } $urloption = 'socid=' . $socid . '&htmlname=' . $htmlname; print ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT . '/projet/ajax/projects.php', $urloption, $conf->global->PROJECT_USE_SEARCH_TO_SELECT, 0, array()); print '<input type="text" size="20" name="search_' . $htmlname . '" id="search_' . $htmlname . '" value="' . $selected_input_value . '"' . $placeholder . ' />'; } else { print $this->select_projects_list($socid, $selected, $htmlname, $maxlength, $option_only, $show_empty, $discard_closed, $forcefocus, $disabled, 0, $filterkey); if ($discard_closed) { if (class_exists('Form')) { if (empty($form)) { $form = new Form($this->db); } print $form->textwithpicto('', $langs->trans("ClosedProjectsAreHidden")); } } } }
/** * \brief Return list of third parties * \param object Object we try to find contacts * \param var_id Name of id field * \param selected Pre-selected third party * \param htmlname Name of HTML form * \param limitto Disable answers that are not id in this array list */ function selectCompaniesForNewContact($object, $var_id, $selected='', $htmlname='newcompany', $limitto='') { global $conf, $langs; // On recherche les societes $sql = "SELECT s.rowid, s.nom FROM"; $sql.= " ".MAIN_DB_PREFIX."societe as s"; if ($selected && $conf->use_javascript_ajax && $conf->global->COMPANY_USE_SEARCH_TO_SELECT) $sql.= " WHERE rowid = ".$selected; else { // For ajax search we limit here. For combo list, we limit later if ($conf->use_javascript_ajax && $conf->global->COMPANY_USE_SEARCH_TO_SELECT && is_array($limitto) && sizeof($limitto)) { $sql.= " WHERE rowid in (".join(',',$limitto).")"; } } $sql .= " ORDER BY nom ASC"; //print $sql; $resql = $this->db->query($sql); if ($resql) { if ($conf->use_javascript_ajax && $conf->global->COMPANY_USE_SEARCH_TO_SELECT) { $minLength = (is_numeric($conf->global->COMPANY_USE_SEARCH_TO_SELECT)?$conf->global->COMPANY_USE_SEARCH_TO_SELECT:2); $socid=0; if ($selected) { $obj = $this->db->fetch_object($resql); $socid = $obj->rowid?$obj->rowid:''; } // We call a page after a small delay when a new input has been selected $javaScript = "window.location=\'./contact.php?".$var_id."=".$object->id."&".$htmlname."=\' + document.getElementById(\'".$htmlname."\').value;"; $htmloption = 'onChange="ac_delay(\''.$javaScript.'\',\'500\');"'; // When we select with mouse $htmloption.= 'onKeyUp="if (event.keyCode== 13) { ac_delay(\''.$javaScript.'\',\'500\'); }"'; // When we select with keyboard print "\n".'<!-- Input text for third party with Ajax.Autocompleter (selectCompaniesForNewContact) -->'."\n"; print '<table class="nobordernopadding"><tr class="nobordernopadding">'; print '<td class="nobordernopadding">'; if ($obj->rowid == 0) { //$langs->load("companies"); //print '<input type="text" size="30" id="'.$htmlname.'_label" name="'.$htmlname.'" value="'.$langs->trans("SelectCompany").'" '.$htmloption.' />'; print '<input type="text" size="30" id="search_'.$htmlname.'" name="search_'.$htmlname.'" value="" '.$htmloption.' />'; } else { print '<input type="text" size="30" id="search_'.$htmlname.'" name="search_'.$htmlname.'" value="'.$obj->nom.'" '.$htmloption.' />'; } print ajax_autocompleter(($socid?$socid:-1),$htmlname,DOL_URL_ROOT.'/societe/ajaxcompanies.php','',$minLength); print '</td>'; print '</tr>'; print '</table>'; print "\n"; return $socid; } else { $javaScript = "window.location='./contact.php?".$var_id."=".$object->id."&".$htmlname."=' + form.".$htmlname.".options[form.".$htmlname.".selectedIndex].value;"; print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'" onChange="'.$javaScript.'">'; $num = $this->db->num_rows($resql); $i = 0; if ($num) { while ($i < $num) { $obj = $this->db->fetch_object($resql); if ($i == 0) $firstCompany = $obj->rowid; $disabled=0; if (is_array($limitto) && sizeof($limitto) && ! in_array($obj->rowid,$limitto)) $disabled=1; if ($selected > 0 && $selected == $obj->rowid) { print '<option value="'.$obj->rowid.'"'; if ($disabled) print ' disabled="true"'; print ' selected="selected">'.dol_trunc($obj->nom,24).'</option>'; $firstCompany = $obj->rowid; } else { print '<option value="'.$obj->rowid.'"'; if ($disabled) print ' disabled="true"'; print '>'.dol_trunc($obj->nom,24).'</option>'; } $i ++; } } print "</select>\n"; return $firstCompany; } } else { dol_print_error($this->db); } }
/** * Output list of third parties. * * @param object $object Object we try to find contacts * @param string $var_id Name of id field * @param string $selected Pre-selected third party * @param string $htmlname Name of HTML form * @param array $limitto Disable answers that are not id in this array list * @param int $forceid This is to force another object id than object->id * @param array $events More js events option. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled'))) * @param string $moreparam String with more param to add into url when noajax search is used. * @return int The selected third party ID */ function selectCompaniesForNewContact($object, $var_id, $selected = '', $htmlname = 'newcompany', $limitto = '', $forceid = 0, $events = array(), $moreparam = '') { global $conf, $langs; // On recherche les societes $sql = "SELECT s.rowid, s.nom as name FROM"; $sql .= " " . MAIN_DB_PREFIX . "societe as s"; $sql .= " WHERE s.entity IN (" . getEntity('societe', 1) . ")"; if ($selected && $conf->use_javascript_ajax && !empty($conf->global->COMPANY_USE_SEARCH_TO_SELECT)) { $sql .= " AND s.rowid = " . $selected; } else { // For ajax search we limit here. For combo list, we limit later if ($conf->use_javascript_ajax && $conf->global->COMPANY_USE_SEARCH_TO_SELECT && is_array($limitto) && count($limitto)) { $sql .= " AND s.rowid IN (" . join(',', $limitto) . ")"; } } $sql .= " ORDER BY s.nom ASC"; $resql = $this->db->query($sql); if ($resql) { if (!empty($conf->use_javascript_ajax) && !empty($conf->global->COMPANY_USE_SEARCH_TO_SELECT)) { // Use Ajax search $minLength = is_numeric($conf->global->COMPANY_USE_SEARCH_TO_SELECT) ? $conf->global->COMPANY_USE_SEARCH_TO_SELECT : 2; $socid = 0; if ($selected) { $obj = $this->db->fetch_object($resql); $socid = $obj->rowid ? $obj->rowid : ''; } // We recall a page after a small delay when a new input has been selected $htmloption = ''; //$javaScript = "window.location=\'".$_SERVER['PHP_SELF']."?".$var_id."=".($forceid>0?$forceid:$object->id)."&".$htmlname."=\' + document.getElementById(\'".$htmlname."\').value;"; //$htmloption.= 'onChange="ac_delay(\''.$javaScript.'\',\'500\');"'; // When we select with mouse //$htmloption.= 'onKeyUp="if (event.keyCode== 13) { ac_delay(\''.$javaScript.'\',\'500\'); }"'; // When we select with keyboard if (count($events)) { print '<script type="text/javascript"> jQuery(document).ready(function() { $("#search_' . $htmlname . '").change(function() { var obj = ' . json_encode($events) . '; $.each(obj, function(key,values) { if (values.method.length) { runJsCodeForEvent' . $htmlname . '(values); } }); }); // Function used to execute events when search_htmlname change function runJsCodeForEvent' . $htmlname . '(obj) { var id = $("#' . $htmlname . '").val(); var method = obj.method; var url = obj.url; var htmlname = obj.htmlname; $.getJSON(url, { action: method, id: id, htmlname: htmlname }, function(response) { if (response != null) { $.each(obj.params, function(key,action) { if (key.length) { var num = response.num; if (num > 0) { $("#" + key).removeAttr(action); } else { $("#" + key).attr(action, action); } } }); $("select#" + htmlname).html(response.value); } }); }; }); </script>'; } print "\n" . '<!-- Input text for third party with Ajax.Autocompleter (selectCompaniesForNewContact) -->' . "\n"; if ($obj->rowid == 0) { print '<input type="text" size="30" id="search_' . $htmlname . '" name="search_' . $htmlname . '" value="" ' . $htmloption . ' />'; } else { print '<input type="text" size="30" id="search_' . $htmlname . '" name="search_' . $htmlname . '" value="' . $obj->name . '" ' . $htmloption . ' />'; } print ajax_autocompleter($socid ? $socid : -1, $htmlname, DOL_URL_ROOT . '/societe/ajaxcompanies.php', '', $minLength); print "\n"; return $socid; } else { // No Ajax search print '<select class="flat" id="' . $htmlname . '" name="' . $htmlname . '"'; if ($conf->use_javascript_ajax) { $javaScript = "window.location='" . $_SERVER['PHP_SELF'] . "?" . $var_id . "=" . ($forceid > 0 ? $forceid : $object->id) . $moreparam . "&" . $htmlname . "=' + form." . $htmlname . ".options[form." . $htmlname . ".selectedIndex].value;"; print ' onChange="' . $javaScript . '"'; } print '>'; $num = $this->db->num_rows($resql); $i = 0; if ($num) { while ($i < $num) { $obj = $this->db->fetch_object($resql); if ($i == 0) { $firstCompany = $obj->rowid; } $disabled = 0; if (is_array($limitto) && count($limitto) && !in_array($obj->rowid, $limitto)) { $disabled = 1; } if ($selected > 0 && $selected == $obj->rowid) { print '<option value="' . $obj->rowid . '"'; if ($disabled) { print ' disabled="disabled"'; } print ' selected="selected">' . dol_trunc($obj->name, 24) . '</option>'; $firstCompany = $obj->rowid; } else { print '<option value="' . $obj->rowid . '"'; if ($disabled) { print ' disabled="disabled"'; } print '>' . dol_trunc($obj->name, 24) . '</option>'; } $i++; } } print "</select>\n"; return $firstCompany; } } else { dol_print_error($this->db); } }
/** * Return list of products for customer in Ajax if Ajax activated or go to select_produits_fournisseurs_do * @param socid Id third party * @param selected Preselected product * @param htmlname Name of HTML Select * @param filtertype Filter on product type (''=nofilter, 0=product, 1=service) * @param filtre For a SQL filter */ function select_produits_fournisseurs($socid, $selected = '', $htmlname = 'productid', $filtertype = '', $filtre) { global $langs, $conf; if ($conf->global->PRODUIT_USE_SEARCH_TO_SELECT) { // mode=2 means suppliers products print ajax_autocompleter('', $htmlname, DOL_URL_ROOT . '/product/ajaxproducts.php', ($socid > 0 ? 'socid=' . $socid . '&' : '') . 'htmlname=' . $htmlname . '&outjson=1&price_level=' . $price_level . '&type=' . $filtertype . '&mode=2&status=' . $status . '&finished=' . $finished, $conf->global->PRODUIT_USE_SEARCH_TO_SELECT); print $langs->trans("RefOrLabel") . ' : <input type="text" size="16" name="search_' . $htmlname . '" id="search_' . $htmlname . '">'; print '<br>'; } else { $this->select_produits_fournisseurs_do($socid, $selected, $htmlname, $filtertype, $filtre, '', -1, 0); } }
/** * Return list of products for customer (in Ajax if Ajax activated or go to select_produits_fournisseurs_list) * * @param int $socid Id third party * @param string $selected Preselected product * @param string $htmlname Name of HTML Select * @param string $filtertype Filter on product type (''=nofilter, 0=product, 1=service) * @param string $filtre For a SQL filter * @param array $ajaxoptions Options for ajax_autocompleter * @param int $hidelabel Hide label (0=no, 1=yes) * @return void */ function select_produits_fournisseurs($socid, $selected = '', $htmlname = 'productid', $filtertype = '', $filtre = '', $ajaxoptions = array(), $hidelabel = 0) { global $langs, $conf; global $price_level, $status, $finished; if (!empty($conf->use_javascript_ajax) && !empty($conf->global->PRODUIT_USE_SEARCH_TO_SELECT)) { // mode=2 means suppliers products $urloption = ($socid > 0 ? 'socid=' . $socid . '&' : '') . 'htmlname=' . $htmlname . '&outjson=1&price_level=' . $price_level . '&type=' . $filtertype . '&mode=2&status=' . $status . '&finished=' . $finished; print ajax_autocompleter('', $htmlname, DOL_URL_ROOT . '/product/ajax/products.php', $urloption, $conf->global->PRODUIT_USE_SEARCH_TO_SELECT, 0, $ajaxoptions); print ($hidelabel ? '' : $langs->trans("RefOrLabel") . ' : ') . '<input type="text" size="16" name="search_' . $htmlname . '" id="search_' . $htmlname . '">'; } else { print $this->select_produits_fournisseurs_list($socid, $selected, $htmlname, $filtertype, $filtre, '', -1, 0); } }
/** * Output list of third parties. * * @param object $object Object we try to find contacts * @param string $var_id Name of id field * @param string $selected Pre-selected third party * @param string $htmlname Name of HTML form * @param array $limitto Disable answers that are not id in this array list * @param int $forceid This is to force another object id than object->id * @param string $moreparam String with more param to add into url when noajax search is used. * @return int The selected third party ID */ function selectCompaniesForNewContact($object, $var_id, $selected = '', $htmlname = 'newcompany', $limitto = '', $forceid = 0, $moreparam = '') { global $conf, $langs; if (!empty($conf->use_javascript_ajax) && !empty($conf->global->COMPANY_USE_SEARCH_TO_SELECT)) { // Use Ajax search $minLength = is_numeric($conf->global->COMPANY_USE_SEARCH_TO_SELECT) ? $conf->global->COMPANY_USE_SEARCH_TO_SELECT : 2; $socid = 0; $name = ''; if ($selected > 0) { $tmpthirdparty = new Societe($this->db); $result = $tmpthirdparty->fetch($selected); if ($result > 0) { $socid = $selected; $name = $tmpthirdparty->name; } } $events = array(); // Add an entry 'method' to say 'yes, we must execute url with param action = method'; // Add an entry 'url' to say which url to execute // Add an entry htmlname to say which element we must change once url is called // Add entry params => array('cssid' => 'attr') to say to remov or add attribute attr if answer of url return 0 or >0 lines // To refresh contacts list on thirdparty list change $events[] = array('method' => 'getContacts', 'url' => dol_buildpath('/core/ajax/contacts.php', 1), 'htmlname' => 'contactid', 'params' => array('add-customer-contact' => 'disabled')); if (count($events)) { print '<script type="text/javascript"> jQuery(document).ready(function() { $("#search_' . $htmlname . '").change(function() { console.log("Call runJsCodeForEvent' . $htmlname . '"); var obj = ' . json_encode($events) . '; $.each(obj, function(key,values) { if (values.method.length) { runJsCodeForEvent' . $htmlname . '(values); } }); /* Clean contact */ $("div#s2id_contactid>a>span").html(\'\'); }); // Function used to execute events when search_htmlname change function runJsCodeForEvent' . $htmlname . '(obj) { var id = $("#' . $htmlname . '").val(); var method = obj.method; var url = obj.url; var htmlname = obj.htmlname; $.getJSON(url, { action: method, id: id, htmlname: htmlname }, function(response) { if (response != null) { $.each(obj.params, function(key,action) { if (key.length) { var num = response.num; if (num > 0) { $("#" + key).removeAttr(action); } else { $("#" + key).attr(action, action); } } }); /* console.log("Change select#"+htmlname+" with content "+response.value) */ $("select#" + htmlname).html(response.value); } } ); }; }); </script>'; } print "\n" . '<!-- Input text for third party with Ajax.Autocompleter (selectCompaniesForNewContact) -->' . "\n"; print '<input type="text" size="30" id="search_' . $htmlname . '" name="search_' . $htmlname . '" value="' . $name . '" />'; print ajax_autocompleter($socid ? $socid : -1, $htmlname, DOL_URL_ROOT . '/societe/ajaxcompanies.php', '', $minLength, 0); return $socid; } else { // Search to list thirdparties $sql = "SELECT s.rowid, s.nom as name FROM"; $sql .= " " . MAIN_DB_PREFIX . "societe as s"; $sql .= " WHERE s.entity IN (" . getEntity('societe', 1) . ")"; // For ajax search we limit here. For combo list, we limit later if (is_array($limitto) && count($limitto)) { $sql .= " AND s.rowid IN (" . join(',', $limitto) . ")"; } $sql .= " ORDER BY s.nom ASC"; $resql = $this->db->query($sql); if ($resql) { print '<select class="flat" id="' . $htmlname . '" name="' . $htmlname . '"'; if ($conf->use_javascript_ajax) { $javaScript = "window.location='" . $_SERVER['PHP_SELF'] . "?" . $var_id . "=" . ($forceid > 0 ? $forceid : $object->id) . $moreparam . "&" . $htmlname . "=' + form." . $htmlname . ".options[form." . $htmlname . ".selectedIndex].value;"; print ' onChange="' . $javaScript . '"'; } print '>'; $num = $this->db->num_rows($resql); $i = 0; if ($num) { while ($i < $num) { $obj = $this->db->fetch_object($resql); if ($i == 0) { $firstCompany = $obj->rowid; } $disabled = 0; if (is_array($limitto) && count($limitto) && !in_array($obj->rowid, $limitto)) { $disabled = 1; } if ($selected > 0 && $selected == $obj->rowid) { print '<option value="' . $obj->rowid . '"'; if ($disabled) { print ' disabled'; } print ' selected>' . dol_trunc($obj->name, 24) . '</option>'; $firstCompany = $obj->rowid; } else { print '<option value="' . $obj->rowid . '"'; if ($disabled) { print ' disabled'; } print '>' . dol_trunc($obj->name, 24) . '</option>'; } $i++; } } print "</select>\n"; return $firstCompany; } else { dol_print_error($this->db); print 'Error sql'; } } }
/** * Override de la fonction classique de la class FormProject * Show a combo list with projects qualified for a third party * * @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 Nom de la zone html * @param int $maxlength Maximum length of label * @param int $option_only Option only * @param int $show_empty Add an empty line * @return string select or options if OK, void if KO */ function custom_select_projects($socid = -1, $selected = '', $htmlname = 'projectid', $type_aff = 'view', $maxlength = 25, $option_only = 0, $show_empty = 1) { global $user, $conf, $langs, $db; require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php'; $out = ''; if ($type_aff == 'view') { if ($selected > 0) { $project = new Project($db); $project->fetch($selected); //return dol_trunc($project->ref,18).' - '.dol_trunc($project->title,$maxlength); return $project->getNomUrl(1) . ' - ' . dol_trunc($project->title, $maxlength); } else { return $out; } } $hideunselectables = false; if (!empty($conf->global->PROJECT_HIDE_UNSELECTABLES)) { $hideunselectables = true; } $projectsListId = false; if (empty($user->rights->projet->all->lire)) { $projectstatic = new Project($db); $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1); } // Search all projects $sql = 'SELECT p.rowid, p.ref, p.title, p.fk_soc, p.fk_statut, p.public'; $sql .= ' FROM ' . MAIN_DB_PREFIX . 'projet as p'; $sql .= " WHERE p.entity IN (" . getEntity('project', 1) . ")"; if ($projectsListId !== false) { $sql .= " AND p.rowid IN (" . $projectsListId . ")"; } if ($socid == 0) { $sql .= " AND (p.fk_soc=0 OR p.fk_soc IS NULL)"; } if ($socid > 0) { $sql .= " AND (p.fk_soc=" . $socid . " OR p.fk_soc IS NULL)"; } $sql .= " ORDER BY p.ref ASC"; $resql = $db->query($sql); if ($resql) { if (empty($option_only)) { $out .= '<select class="flat" name="' . $htmlname . '">'; } if (!empty($show_empty)) { $out .= '<option value="0"> </option>'; } $num = $db->num_rows($resql); $i = 0; if ($num) { while ($i < $num) { $obj = $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. if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && !$user->rights->societe->lire) { // Do nothing } else { $labeltoshow = dol_trunc($obj->ref, 18); //if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')'; //else $labeltoshow.=' ('.$langs->trans("Private").')'; if (!empty($selected) && $selected == $obj->rowid && $obj->fk_statut > 0) { $out .= '<option value="' . $obj->rowid . '" selected="selected">' . $labeltoshow . ' - ' . dol_trunc($obj->title, $maxlength) . '</option>'; } else { $disabled = 0; $labeltoshow .= ' ' . dol_trunc($obj->title, $maxlength); if (!$obj->fk_statut > 0) { $disabled = 1; $labeltoshow .= ' - ' . $langs->trans("Draft"); } if ($socid > 0 && (!empty($obj->fk_soc) && $obj->fk_soc != $socid)) { $disabled = 1; $labeltoshow .= ' - ' . $langs->trans("LinkedToAnotherCompany"); } if ($hideunselectables && $disabled) { $resultat = ''; } else { $resultat = '<option value="' . $obj->rowid . '"'; if ($disabled) { $resultat .= ' disabled="disabled"'; } //if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')'; //else $labeltoshow.=' ('.$langs->trans("Private").')'; $resultat .= '>'; $resultat .= $labeltoshow; $resultat .= '</option>'; } $out .= $resultat; } } $i++; } } if (empty($option_only)) { $out .= '</select>'; } if ($conf->cliacropose->enabled) { // TODO c'est naze, à refaire en utilisant la vraie autocompletion dispo depuis dolibarr 3.8 pour utiliser l'auto complete projets de doli si active (j'avais rajouté un script ajax/projects.php pour acropose) // Autocomplétion if (isset($selected)) { $p = new Project($db); $p->fetch($selected); $selected_value = $p->ref; } $out = ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT . '/projet/ajax/projects.php', $urloption, 1); $out .= '<input type="text" size="20" name="search_' . $htmlname . '" id="search_' . $htmlname . '" value="' . $selected_value . '"' . $placeholder . ' />'; } $db->free($resql); return $out; } else { dol_print_error($db); return ''; } }