mirror of
https://github.com/Dolibarr/dolibarr.git
synced 2026-02-08 00:52:01 +01:00
add extrafield type, check box from table
This commit is contained in:
@@ -72,7 +72,7 @@ class ExtraFields
|
||||
'separate' => 'ExtrafieldSeparator',
|
||||
'checkbox' => 'ExtrafieldCheckBox',
|
||||
'radio' => 'ExtrafieldRadio',
|
||||
|
||||
'chkbxlst' => 'ExtrafieldCheckBoxFromList',
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -171,7 +171,7 @@ class ExtraFields
|
||||
}elseif($type=='mail') {
|
||||
$typedb='varchar';
|
||||
$lengthdb='128';
|
||||
} elseif (($type=='select') || ($type=='sellist') || ($type=='radio') ||($type=='checkbox')){
|
||||
} elseif (($type=='select') || ($type=='sellist') || ($type=='radio') ||($type=='checkbox') ||($type=='chkbxlst')){
|
||||
$typedb='text';
|
||||
$lengthdb='';
|
||||
} else {
|
||||
@@ -373,7 +373,7 @@ class ExtraFields
|
||||
}elseif($type=='mail') {
|
||||
$typedb='varchar';
|
||||
$lengthdb='128';
|
||||
} elseif (($type=='select') || ($type=='sellist') || ($type=='radio') ||($type=='checkbox')) {
|
||||
} elseif (($type=='select') || ($type=='sellist') || ($type=='radio') || ($type=='checkbox') || ($type=='chkbxlst')) {
|
||||
$typedb='text';
|
||||
$lengthdb='';
|
||||
} else {
|
||||
@@ -864,6 +864,140 @@ class ExtraFields
|
||||
$out.='/>'.$val.'<br>';
|
||||
}
|
||||
}
|
||||
elseif ($type == 'chkbxlst')
|
||||
{
|
||||
$value_arr = explode(',', $value);
|
||||
|
||||
if (is_array($param['options'])) {
|
||||
$param_list = array_keys($param['options']);
|
||||
$InfoFieldList = explode(":", $param_list[0]);
|
||||
// 0 : tableName
|
||||
// 1 : label field name
|
||||
// 2 : key fields name (if differ of rowid)
|
||||
// 3 : key field parent (for dependent lists)
|
||||
// 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value
|
||||
$keyList = (empty($InfoFieldList[2]) ? 'rowid' : $InfoFieldList[2] . ' as rowid');
|
||||
|
||||
if (count($InfoFieldList) > 3 && ! empty($InfoFieldList[3])) {
|
||||
list ( $parentName, $parentField ) = explode('|', $InfoFieldList[3]);
|
||||
$keyList .= ', ' . $parentField;
|
||||
}
|
||||
if (count($InfoFieldList) > 4 && ! empty($InfoFieldList[4])) {
|
||||
if (strpos($InfoFieldList[4], 'extra.') !== false) {
|
||||
$keyList = 'main.' . $InfoFieldList[2] . ' as rowid';
|
||||
} else {
|
||||
$keyList = $InfoFieldList[2] . ' as rowid';
|
||||
}
|
||||
}
|
||||
|
||||
$fields_label = explode('|', $InfoFieldList[1]);
|
||||
if (is_array($fields_label)) {
|
||||
$keyList .= ', ';
|
||||
$keyList .= implode(', ', $fields_label);
|
||||
}
|
||||
|
||||
$sqlwhere = '';
|
||||
$sql = 'SELECT ' . $keyList;
|
||||
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList[0];
|
||||
if (! empty($InfoFieldList[4])) {
|
||||
// We have to join on extrafield table
|
||||
if (strpos($InfoFieldList[4], 'extra') !== false) {
|
||||
$sql .= ' as main, ' . MAIN_DB_PREFIX . $InfoFieldList[0] . '_extrafields as extra';
|
||||
$sqlwhere .= ' WHERE extra.fk_object=main.' . $InfoFieldList[2] . ' AND ' . $InfoFieldList[4];
|
||||
} else {
|
||||
$sqlwhere .= ' WHERE ' . $InfoFieldList[4];
|
||||
}
|
||||
} else {
|
||||
$sqlwhere .= ' WHERE 1';
|
||||
}
|
||||
if (in_array($InfoFieldList[0], array (
|
||||
'tablewithentity'
|
||||
)))
|
||||
$sqlwhere .= ' AND entity = ' . $conf->entity; // Some tables may have field, some other not. For the moment we disable it.
|
||||
// $sql.=preg_replace('/^ AND /','',$sqlwhere);
|
||||
// print $sql;
|
||||
$sql .= $sqlwhere;
|
||||
dol_syslog(get_class($this) . '::showInputField type=chkbxlst',LOG_DEBUG);
|
||||
$resql = $this->db->query($sql);
|
||||
if ($resql) {
|
||||
$num = $this->db->num_rows($resql);
|
||||
$i = 0;
|
||||
while ( $i < $num ) {
|
||||
$labeltoshow = '';
|
||||
$obj = $this->db->fetch_object($resql);
|
||||
|
||||
// Several field into label (eq table:code|libelle:rowid)
|
||||
$fields_label = explode('|', $InfoFieldList[1]);
|
||||
if (is_array($fields_label)) {
|
||||
$notrans = true;
|
||||
foreach ( $fields_label as $field_toshow ) {
|
||||
$labeltoshow .= $obj->$field_toshow . ' ';
|
||||
}
|
||||
} else {
|
||||
$labeltoshow = $obj->$InfoFieldList[1];
|
||||
}
|
||||
$labeltoshow = dol_trunc($labeltoshow, 45);
|
||||
|
||||
if (is_array($value_arr) && in_array($obj->rowid, $value_arr)) {
|
||||
foreach ( $fields_label as $field_toshow ) {
|
||||
$translabel = $langs->trans($obj->$field_toshow);
|
||||
if ($translabel != $obj->$field_toshow) {
|
||||
$labeltoshow = dol_trunc($translabel, 18) . ' ';
|
||||
} else {
|
||||
$labeltoshow = dol_trunc($obj->$field_toshow, 18) . ' ';
|
||||
}
|
||||
}
|
||||
$out .= '<input class="flat" type="checkbox" name="options_' . $key . $keyprefix . '[]" ' . ($moreparam ? $moreparam : '');
|
||||
$out .= ' value="' . $obj->rowid . '"';
|
||||
|
||||
$out .= 'checked="checked"';
|
||||
|
||||
$out .= '/>' . $labeltoshow . '<br>';
|
||||
} else {
|
||||
if (! $notrans) {
|
||||
$translabel = $langs->trans($obj->$InfoFieldList[1]);
|
||||
if ($translabel != $obj->$InfoFieldList[1]) {
|
||||
$labeltoshow = dol_trunc($translabel, 18);
|
||||
} else {
|
||||
$labeltoshow = dol_trunc($obj->$InfoFieldList[1], 18);
|
||||
}
|
||||
}
|
||||
if (empty($labeltoshow))
|
||||
$labeltoshow = '(not defined)';
|
||||
|
||||
if (is_array($value_arr) && in_array($obj->rowid, $value_arr)) {
|
||||
$out .= '<input class="flat" type="checkbox" name="options_' . $key . $keyprefix . '[]" ' . ($moreparam ? $moreparam : '');
|
||||
$out .= ' value="' . $obj->rowid . '"';
|
||||
|
||||
$out .= 'checked="checked"';
|
||||
$out .= '';
|
||||
|
||||
$out .= '/>' . $labeltoshow . '<br>';
|
||||
}
|
||||
|
||||
if (! empty($InfoFieldList[3])) {
|
||||
$parent = $parentName . ':' . $obj->{$parentField};
|
||||
}
|
||||
|
||||
$out .= '<input class="flat" type="checkbox" name="options_' . $key . $keyprefix . '[]" ' . ($moreparam ? $moreparam : '');
|
||||
$out .= ' value="' . $obj->rowid . '"';
|
||||
|
||||
$out .= ((is_array($value_arr) && in_array($obj->rowid, $value_arr)) ? ' checked="checked" ' : '');
|
||||
;
|
||||
$out .= '';
|
||||
|
||||
$out .= '/>' . $labeltoshow . '<br>';
|
||||
}
|
||||
|
||||
$i ++;
|
||||
}
|
||||
$this->db->free($resql);
|
||||
} else {
|
||||
print 'Error in request ' . $sql . ' ' . $this->db->lasterror() . '. Check setup of extra parameters.<br>';
|
||||
}
|
||||
}
|
||||
$out .= '</select>';
|
||||
}
|
||||
/* Add comments
|
||||
if ($type == 'date') $out.=' (YYYY-MM-DD)';
|
||||
elseif ($type == 'datetime') $out.=' (YYYY-MM-DD HH:MM:SS)';
|
||||
@@ -1019,6 +1153,73 @@ class ExtraFields
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif ($type == 'chkbxlst')
|
||||
{
|
||||
$value_arr = explode(',', $value);
|
||||
|
||||
$param_list = array_keys($params['options']);
|
||||
$InfoFieldList = explode(":", $param_list[0]);
|
||||
|
||||
$selectkey = "rowid";
|
||||
$keyList = 'rowid';
|
||||
|
||||
if (count($InfoFieldList) >= 3) {
|
||||
$selectkey = $InfoFieldList[2];
|
||||
$keyList = $InfoFieldList[2] . ' as rowid';
|
||||
}
|
||||
|
||||
$fields_label = explode('|', $InfoFieldList[1]);
|
||||
if (is_array($fields_label)) {
|
||||
$keyList .= ', ';
|
||||
$keyList .= implode(', ', $fields_label);
|
||||
}
|
||||
|
||||
$sql = 'SELECT ' . $keyList;
|
||||
$sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList[0];
|
||||
if (strpos($InfoFieldList[4], 'extra') !== false) {
|
||||
$sql .= ' as main';
|
||||
}
|
||||
// $sql.= " WHERE ".$selectkey."='".$this->db->escape($value)."'";
|
||||
// $sql.= ' AND entity = '.$conf->entity;
|
||||
|
||||
dol_syslog(get_class($this) . ':showOutputField:$type=chkbxlst',LOG_DEBUG);
|
||||
$resql = $this->db->query($sql);
|
||||
if ($resql) {
|
||||
$value = ''; // value was used, so now we reste it to use it to build final output
|
||||
|
||||
while ( $obj = $this->db->fetch_object($resql) ) {
|
||||
|
||||
// Several field into label (eq table:code|libelle:rowid)
|
||||
$fields_label = explode('|', $InfoFieldList[1]);
|
||||
if (is_array($value_arr) && in_array($obj->rowid, $value_arr)) {
|
||||
if (is_array($fields_label) && count($fields_label) > 1) {
|
||||
foreach ( $fields_label as $field_toshow ) {
|
||||
$translabel = '';
|
||||
if (! empty($obj->$field_toshow)) {
|
||||
$translabel = $langs->trans($obj->$field_toshow);
|
||||
}
|
||||
if ($translabel != $field_toshow) {
|
||||
$value .= dol_trunc($translabel, 18) . '<BR>';
|
||||
} else {
|
||||
$value .= $obj->$field_toshow . '<BR>';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$translabel = '';
|
||||
if (! empty($obj->$InfoFieldList[1])) {
|
||||
$translabel = $langs->trans($obj->$InfoFieldList[1]);
|
||||
}
|
||||
if ($translabel != $obj->$InfoFieldList[1]) {
|
||||
$value .= dol_trunc($translabel, 18) . '<BR>';
|
||||
} else {
|
||||
$value .= $obj->$InfoFieldList[1] . '<BR>';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else
|
||||
dol_syslog(get_class($this) . '::showOutputField error ' . $this->db->lasterror(), LOG_WARNING);
|
||||
}
|
||||
else
|
||||
{
|
||||
$showsize=round($size);
|
||||
@@ -1074,7 +1275,7 @@ class ExtraFields
|
||||
// Clean parameters
|
||||
$value_key=dol_mktime($_POST["options_".$key."hour"], $_POST["options_".$key."min"], 0, $_POST["options_".$key."month"], $_POST["options_".$key."day"], $_POST["options_".$key."year"]);
|
||||
}
|
||||
else if (in_array($key_type,array('checkbox')))
|
||||
else if (in_array($key_type,array('checkbox','chkbxlst')))
|
||||
{
|
||||
$value_arr=GETPOST("options_".$key);
|
||||
if (!empty($value_arr)) {
|
||||
|
||||
Reference in New Issue
Block a user