Fix management of visibility of export/import templates.

This commit is contained in:
Laurent Destailleur 2021-06-21 15:03:45 +02:00
parent 5697e86c09
commit 31db1dc412
6 changed files with 89 additions and 36 deletions

View File

@ -86,13 +86,14 @@ print '<td class="center" width="20">&nbsp;</td>';
print '<td class="center" width="100"></td>'."\n";
print '</tr>';
// Example with a yes / no select
/* No more need for this, you can set that a profile is public when saving it.
print '<tr class="oddeven">';
print '<td>'.$langs->trans("EXPORTS_SHARE_MODELS").'</td>';
print '<td class="center" width="20">&nbsp;</td>';
print '<td class="center" width="100">';
print ajax_constantonoff('EXPORTS_SHARE_MODELS');
print '</td></tr>';
*/
print '<tr class="oddeven">';
print '<td>'.$langs->trans("ExportCsvSeparator").'</td>';

View File

@ -110,7 +110,7 @@ class FormOther
* @param string $htmlname Nom de la zone select
* @param string $type Type des modeles recherches
* @param int $useempty Show an empty value in list
* @param int $fk_user User that has created the template (this is set to null to get all export model when EXPORTS_SHARE_MODELS is on)
* @param int $fk_user User we want templates
* @return void
*/
public function select_export_model($selected = '', $htmlname = 'exportmodelid', $type = '', $useempty = 0, $fk_user = null)
@ -121,8 +121,8 @@ class FormOther
$sql = "SELECT rowid, label, fk_user";
$sql .= " FROM ".MAIN_DB_PREFIX."export_model";
$sql .= " WHERE type = '".$this->db->escape($type)."'";
if (!empty($fk_user)) {
$sql .= " AND fk_user IN (0, ".$fk_user.")"; // An export model
if (empty($conf->global->EXPORTS_SHARE_MODELS)) { // EXPORTS_SHARE_MODELS means all templates are visible, whatever is owner.
$sql .= " AND fk_user IN (0, ".((int) $fk_user).")";
}
$sql .= " ORDER BY label";
$result = $this->db->query($sql);
@ -132,6 +132,8 @@ class FormOther
print '<option value="-1">&nbsp;</option>';
}
$tmpuser = new User($this->db);
$num = $this->db->num_rows($result);
$i = 0;
while ($i < $num) {
@ -140,8 +142,7 @@ class FormOther
$label = $obj->label;
if ($obj->fk_user == 0) {
$label .= ' <span class="opacitymedium">('.$langs->trans("Everybody").')</span>';
} elseif (!empty($conf->global->EXPORTS_SHARE_MODELS) && empty($fk_user) && is_object($user) && $user->id != $obj->fk_user) {
$tmpuser = new User($this->db);
} elseif ($obj->fk_user > 0) {
$tmpuser->fetch($obj->fk_user);
$label .= ' <span class="opacitymedium">('.$tmpuser->getFullName($langs).')</span>';
}
@ -171,7 +172,7 @@ class FormOther
* @param string $htmlname Nom de la zone select
* @param string $type Type des modeles recherches
* @param int $useempty Affiche valeur vide dans liste
* @param int $fk_user User that has created the template (this is set to null to get all export model when EXPORTS_SHARE_MODELS is on)
* @param int $fk_user User that has created the template
* @return void
*/
public function select_import_model($selected = '', $htmlname = 'importmodelid', $type = '', $useempty = 0, $fk_user = null)
@ -182,10 +183,10 @@ class FormOther
$sql = "SELECT rowid, label, fk_user";
$sql .= " FROM ".MAIN_DB_PREFIX."import_model";
$sql .= " WHERE type = '".$this->db->escape($type)."'";
if (!empty($fk_user)) {
$sql .= " AND fk_user IN (0, ".$fk_user.")"; // An export model
if (empty($conf->global->EXPORTS_SHARE_MODELS)) { // EXPORTS_SHARE_MODELS means all templates are visible, whatever is owner.
$sql .= " AND fk_user IN (0, ".((int) $fk_user).")";
}
$sql .= " ORDER BY rowid";
$sql .= " ORDER BY label";
$result = $this->db->query($sql);
if ($result) {
print '<select class="flat minwidth200" name="'.$htmlname.'" id="'.$htmlname.'">';
@ -193,6 +194,8 @@ class FormOther
print '<option value="-1">&nbsp;</option>';
}
$tmpuser = new User($this->db);
$num = $this->db->num_rows($result);
$i = 0;
while ($i < $num) {
@ -201,8 +204,7 @@ class FormOther
$label = $obj->label;
if ($obj->fk_user == 0) {
$label .= ' <span class="opacitymedium">('.$langs->trans("Everybody").')</span>';
} elseif (!empty($conf->global->EXPORTS_SHARE_MODELS) && empty($fk_user) && is_object($user) && $user->id != $obj->fk_user) {
$tmpuser = new User($this->db);
} elseif ($obj->fk_user > 0) {
$tmpuser->fetch($obj->fk_user);
$label .= ' <span class="opacitymedium">('.$tmpuser->getFullName($langs).')</span>';
}

View File

@ -54,11 +54,12 @@ class Export
public $array_export_examplevalues = array(); // array with examples for fields
public $array_export_help = array(); // array with tooltip help for fields
// To store export modules
// To store export templates
public $hexa; // List of fields in the export profile
public $hexafiltervalue; // List of search criteria in the export profile
public $datatoexport;
public $model_name; // Name of export profile
public $fk_user;
public $sqlusedforexport;
@ -720,8 +721,6 @@ class Export
$this->db->begin();
$filter = '';
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'export_model (';
$sql .= 'label,';
$sql .= 'type,';
@ -732,11 +731,10 @@ class Export
$sql .= "'".$this->db->escape($this->model_name)."',";
$sql .= " '".$this->db->escape($this->datatoexport)."',";
$sql .= " '".$this->db->escape($this->hexa)."',";
$sql .= ' '.($user->id > 0 ? $user->id : 'null').",";
$sql .= ' '.(isset($this->fk_user) ? (int) $this->fk_user : 'null').",";
$sql .= " '".$this->db->escape($this->hexafiltervalue)."'";
$sql .= ")";
dol_syslog(get_class($this)."::create", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
$this->db->commit();

View File

@ -335,6 +335,7 @@ if ($action == 'add_export_model') {
$objexport->datatoexport = $datatoexport;
$objexport->hexa = $hexa;
$objexport->hexafiltervalue = $hexafiltervalue;
$objexport->fk_user = (GETPOST('visibility', 'aZ09') == 'all' ? 0 : $user->id);
$result = $objexport->create($user);
if ($result >= 0) {
@ -516,11 +517,7 @@ if ($step == 2 && $datatoexport) {
print '<input type="hidden" name="datatoexport" value="'.$datatoexport.'">';
print '<div class="valignmiddle marginbottomonly">';
print '<span class="opacitymedium">'.$langs->trans("SelectExportFields").'</span> ';
if (empty($conf->global->EXPORTS_SHARE_MODELS)) {
$htmlother->select_export_model($exportmodelid, 'exportmodelid', $datatoexport, 1, $user->id);
} else {
$htmlother->select_export_model($exportmodelid, 'exportmodelid', $datatoexport, 1);
}
$htmlother->select_export_model($exportmodelid, 'exportmodelid', $datatoexport, 1, $user->id);
print ' ';
print '<input type="submit" class="button" value="'.$langs->trans("Select").'">';
print '</div>';
@ -1007,20 +1004,28 @@ if ($step == 4 && $datatoexport) {
print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("ExportModelName").'</td>';
print '<td>&nbsp;</td>';
print '<td>'.$langs->trans("Visibility").'</td>';
print '<td></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td><input name="export_name" size="32" value=""></td><td class="right">';
print '<td><input name="export_name" size="32" value=""></td>';
print '<td>';
$arrayvisibility = array('private'=>$langs->trans("Private"), 'all'=>$langs->trans("Everybody"));
print $form->selectarray('visibility', $arrayvisibility, 'private');
print '</td>';
print '<td class="right">';
print '<input type="submit" class="button reposition button-save" value="'.$langs->trans("Save").'">';
print '</td></tr>';
$tmpuser = new User($db);
// List of existing export profils
$sql = "SELECT rowid, label";
$sql = "SELECT rowid, label, fk_user, entity";
$sql .= " FROM ".MAIN_DB_PREFIX."export_model";
$sql .= " WHERE type = '".$db->escape($datatoexport)."'";
if (empty($conf->global->EXPORTS_SHARE_MODELS)) {
$sql .= " AND fk_user=".$user->id;
if (empty($conf->global->EXPORTS_SHARE_MODELS)) { // EXPORTS_SHARE_MODELS means all templates are visible, whatever is owner.
$sql .= " AND fk_user IN (0, ".((int) $user->id).")";
}
$sql .= " ORDER BY rowid";
$resql = $db->query($sql);
@ -1029,9 +1034,19 @@ if ($step == 4 && $datatoexport) {
$i = 0;
while ($i < $num) {
$obj = $db->fetch_object($resql);
print '<tr class="oddeven"><td>';
print $obj->label;
print '</td><td class="right">';
print '</td>';
print '<td>';
if (empty($obj->fk_user)) {
print $langs->trans("Everybody");
} else {
$tmpuser->fetch($obj->fk_user);
print $tmpuser->getNomUrl(1);
}
print '</td>';
print '<td class="right">';
print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?step='.$step.'&datatoexport='.$datatoexport.'&action=deleteprof&token='.newToken().'&id='.$obj->rowid.'">';
print img_delete();
print '</a>';

View File

@ -55,6 +55,12 @@ class Import
*/
public $errors = array();
// To store import templates
public $hexa; // List of fields in the export profile
public $datatoimport;
public $model_name; // Name of export profile
public $fk_user;
/**
* Constructor
@ -266,11 +272,18 @@ class Import
$this->db->begin();
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'import_model (';
$sql .= 'fk_user, label, type, field';
$sql .= 'fk_user,';
$sql .= ' label,';
$sql .= ' type,';
$sql .= ' field';
$sql .= ')';
$sql .= " VALUES (".($user->id > 0 ? $user->id : 0).", '".$this->db->escape($this->model_name)."', '".$this->db->escape($this->datatoimport)."', '".$this->db->escape($this->hexa)."')";
$sql .= " VALUES (";
$sql .= (isset($this->fk_user) ? (int) $this->fk_user : 'null').",";
$sql .= " '".$this->db->escape($this->model_name)."',";
$sql .= " '".$this->db->escape($this->datatoimport)."',";
$sql .= " '".$this->db->escape($this->hexa)."'";
$sql .= ")";
dol_syslog(get_class($this)."::create", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
$this->db->commit();

View File

@ -226,6 +226,7 @@ if ($action == 'add_import_model') {
$objimport->model_name = $import_name;
$objimport->datatoimport = $datatoimport;
$objimport->hexa = $hexa;
$objimport->fk_user = (GETPOST('visibility', 'aZ09') == 'all' ? 0 : $user->id);
$result = $objimport->create($user);
if ($result >= 0) {
@ -968,7 +969,7 @@ if ($step == 4 && $datatoimport) {
$s = str_replace('{s1}', img_picto('', 'grip_title', '', false, 0, 0, '', '', 0), $s);
print $s;
print '</span> ';
$htmlother->select_import_model($importmodelid, 'importmodelid', $datatoimport, 1);
$htmlother->select_import_model($importmodelid, 'importmodelid', $datatoimport, 1, $user->id);
print '<input type="submit" class="button" value="'.$langs->trans("Select").'">';
print '</div>';
print '</form>';
@ -1249,28 +1250,51 @@ if ($step == 4 && $datatoimport) {
print '<table summary="selectofimportprofil" class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("ImportModelName").'</td>';
print '<td>&nbsp;</td>';
print '<td>'.$langs->trans("Visibility").'</td>';
print '<td></td>';
print '</tr>';
print '<tr class="oddeven">';
print '<td><input name="import_name" size="48" value=""></td><td style="text-align:right">';
print '<td><input name="import_name" size="48" value=""></td>';
print '<td>';
$arrayvisibility = array('private'=>$langs->trans("Private"), 'all'=>$langs->trans("Everybody"));
print $form->selectarray('visibility', $arrayvisibility, 'private');
print '</td>';
print '<td class="right">';
print '<input type="submit" class="button" value="'.$langs->trans("SaveImportProfile").'">';
print '</td></tr>';
// List of existing import profils
$sql = "SELECT rowid, label";
$sql = "SELECT rowid, label, fk_user, entity";
$sql .= " FROM ".MAIN_DB_PREFIX."import_model";
$sql .= " WHERE type = '".$db->escape($datatoimport)."'";
if (empty($conf->global->EXPORTS_SHARE_MODELS)) { // EXPORTS_SHARE_MODELS means all templates are visible, whatever is owner.
$sql .= " AND fk_user IN (0, ".((int) $user->id).")";
}
$sql .= " ORDER BY rowid";
$resql = $db->query($sql);
if ($resql) {
$num = $db->num_rows($resql);
$tmpuser = new user($db);
$i = 0;
while ($i < $num) {
$obj = $db->fetch_object($resql);
print '<tr class="oddeven"><td>';
print $obj->label;
print '</td><td style="text-align:right">';
print '</td>';
print '<td>';
if (empty($obj->fk_user)) {
print $langs->trans("Everybody");
} else {
$tmpuser->fetch($obj->fk_user);
print $tmpuser->getNomUrl(1);
}
print '</td>';
print '<td class="right">';
print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?step='.$step.$param.'&action=deleteprof&token='.newToken().'&id='.$obj->rowid.'&filetoimport='.urlencode($filetoimport).'">';
print img_delete();
print '</a>';