Work on extralanguages

This commit is contained in:
Laurent Destailleur 2020-04-01 16:21:03 +02:00
parent b407797ca3
commit 33d2b61c19
4 changed files with 174 additions and 11 deletions

View File

@ -5054,8 +5054,9 @@ abstract class CommonObject
* This method is NOT called by method fetch of objects but must be called separately.
*
* @return int <0 if error, 0 if no values of alternative languages to find nor found, 1 if a value was found and loaded
* @see fetch_optionnals()
*/
public function fetchValueForAlternateLanguages()
public function fetchValuesForExtraLanguages()
{
// To avoid SQL errors. Probably not the better solution though
if (!$this->element) {
@ -5126,7 +5127,7 @@ abstract class CommonObject
* @param string $onlykey Only the following key is filled. When we make update of only one language field ($action = 'update_languages'), calling page must set this to avoid to have other languages being reset.
* @return int 1 if array_options set, 0 if no value, -1 if error (field required missing for example)
*/
public function setValuesForAlternateLanguages($onlykey = '')
public function setValuesForExtraLanguages($onlykey = '')
{
global $_POST, $langs;
@ -5214,6 +5215,7 @@ abstract class CommonObject
* @param int $rowid Id of line. Use the id of object if not defined. Deprecated. Function must be called without parameters.
* @param array $optionsArray Array resulting of call of extrafields->fetch_name_optionals_label(). Deprecated. Function must be called without parameters.
* @return int <0 if error, 0 if no values of extrafield to find nor found, 1 if an attribute is found and value loaded
* @see fetchValuesForExtraLanguages()
*/
public function fetch_optionals($rowid = null, $optionsArray = null)
{
@ -5357,7 +5359,7 @@ abstract class CommonObject
* @param string $trigger If defined, call also the trigger (for example COMPANY_MODIFY)
* @param User $userused Object user
* @return int -1=error, O=did nothing, 1=OK
* @see updateExtraField(), setValueFrom()
* @see insertExtraLanguages(), updateExtraField(), setValueFrom()
*/
public function insertExtraFields($trigger = '', $userused = null)
{
@ -5635,6 +5637,130 @@ abstract class CommonObject
else return 0;
}
/**
* Add/Update all extra fields values for the current object.
* Data to describe values to insert/update are stored into $this->array_options=array('options_codeforfield1'=>'valueforfield1', 'options_codeforfield2'=>'valueforfield2', ...)
* This function delete record with all extrafields and insert them again from the array $this->array_options.
*
* @param string $trigger If defined, call also the trigger (for example COMPANY_MODIFY)
* @param User $userused Object user
* @return int -1=error, O=did nothing, 1=OK
* @see insertExtraFields(), updateExtraField(), setValueFrom()
*/
public function insertExtraLanguages($trigger = '', $userused = null)
{
global $conf, $langs, $user;
if (empty($userused)) $userused = $user;
$error = 0;
if (!empty($conf->global->MAIN_EXTRALANGUAGES_DISABLED)) return 0; // For avoid conflicts if trigger used
if (is_array($this->array_languages))
{
$new_array_languages = $this->array_languages;
foreach ($new_array_languages as $key => $value)
{
$attributeKey = $key;
$attributeType = $this->fields[$attributeKey]['type'];
$attributeLabel = $this->fields[$attributeKey]['label'];
//dol_syslog("attributeLabel=".$attributeLabel, LOG_DEBUG);
//dol_syslog("attributeType=".$attributeType, LOG_DEBUG);
switch ($attributeType)
{
case 'int':
if (!is_numeric($value) && $value != '')
{
$this->errors[] = $langs->trans("ExtraLanguageHasWrongValue", $attributeLabel);
return -1;
}
elseif ($value == '')
{
$new_array_languages[$key] = null;
}
break;
case 'double':
$value = price2num($value);
if (!is_numeric($value) && $value != '')
{
dol_syslog($langs->trans("ExtraLanguageHasWrongValue")." sur ".$attributeLabel."(".$value."is not '".$attributeType."')", LOG_DEBUG);
$this->errors[] = $langs->trans("ExtraLanguageHasWrongValue", $attributeLabel);
return -1;
}
elseif ($value == '')
{
$new_array_languages[$key] = null;
}
//dol_syslog("double value"." sur ".$attributeLabel."(".$value." is '".$attributeType."')", LOG_DEBUG);
$new_array_languages[$key] = $value;
break;
/*case 'select': // Not required, we chosed value='0' for undefined values
if ($value=='-1')
{
$this->array_options[$key] = null;
}
break;*/
}
}
$this->db->begin();
$table_element = $this->table_element;
if ($table_element == 'categorie') $table_element = 'categories'; // For compatibility
dol_syslog(get_class($this)."::insertExtraLanguages delete then insert", LOG_DEBUG);
foreach($new_array_languages as $key => $langcodearray) { // $key = 'name', 'town', ...
foreach($langcodearray as $langcode => $value) {
$sql_del = "DELETE FROM ".MAIN_DB_PREFIX."object_lang";
$sql_del .= " WHERE fk_object = ".$this->id." AND property = '".$this->db->escape($key)."' AND type_object = '".$this->db->escape($table_element)."'";
$sql_del .= " AND lang = '".$this->db->escape($langcode)."'";
$this->db->query($sql_del);
if ($value !== '') {
$sql = "INSERT INTO ".MAIN_DB_PREFIX."object_lang (fk_object, property, type_object, lang, value";
$sql .= ") VALUES (".$this->id.", '".$this->db->escape($key)."', '".$this->db->escape($table_element)."', '".$this->db->escape($langcode)."', '".$this->db->escape($value)."'";
$sql .= ")";
$resql = $this->db->query($sql);
if (!$resql)
{
$this->error = $this->db->lasterror();
$error++;
break;
}
}
}
}
if (!$error && $trigger)
{
// Call trigger
$this->context = array('extralanguagesaddupdate'=>1);
$result = $this->call_trigger($trigger, $userused);
if ($result < 0) $error++;
// End call trigger
}
if ($error)
{
$this->db->rollback();
return -1;
}
else
{
$this->db->commit();
return 1;
}
}
else return 0;
}
/**
* Update an extra field value for the current object.
* Data to describe values to update are stored into $this->array_options=array('options_codeforfield1'=>'valueforfield1', 'options_codeforfield2'=>'valueforfield2', ...)
@ -5643,7 +5769,7 @@ abstract class CommonObject
* @param string $trigger If defined, call also the trigger (for example COMPANY_MODIFY)
* @param User $userused Object user
* @return int -1=error, O=did nothing, 1=OK
* @see setValueFrom(), insertExtraFields()
* @see updateExtraLanguages(), setValueFrom(), insertExtraFields()
*/
public function updateExtraField($key, $trigger = null, $userused = null)
{
@ -5661,7 +5787,7 @@ abstract class CommonObject
$langs->load('admin');
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
$extrafields = new ExtraFields($this->db);
$target_extrafields = $extrafields->fetch_name_optionals_label($this->table_element);
$extrafields->fetch_name_optionals_label($this->table_element);
$value = $this->array_options["options_".$key];
@ -5765,6 +5891,29 @@ abstract class CommonObject
else return 0;
}
/**
* Update an extra language value for the current object.
* Data to describe values to update are stored into $this->array_options=array('options_codeforfield1'=>'valueforfield1', 'options_codeforfield2'=>'valueforfield2', ...)
*
* @param string $key Key of the extrafield (without starting 'options_')
* @param string $trigger If defined, call also the trigger (for example COMPANY_MODIFY)
* @param User $userused Object user
* @return int -1=error, O=did nothing, 1=OK
* @see updateExtraFields(), insertExtraLanguages()
*/
public function updateExtraLanguages($key, $trigger = null, $userused = null)
{
global $conf, $langs, $user;
if (empty($userused)) $userused = $user;
$error = 0;
if (!empty($conf->global->MAIN_EXTRALANGUAGES_DISABLED)) return 0; // For avoid conflicts if trigger used
return 0;
}
/**
* Return HTML string to put an input field into a page

View File

@ -345,12 +345,12 @@ class Form
$valuetoshow = GETPOSTISSET('field-'.$object->element."-".$fieldname."-".$langcode) ? GETPOST('field-'.$object->element.'-'.$fieldname."-".$langcode, $check) : '';
if (empty($valuetoshow)) {
$object->fetchValueForAlternateLanguages();
$object->fetchValuesForExtraLanguages();
//var_dump($object->array_languages);
$valuetoshow = $object->array_languages[$fieldname][$langcode];
}
$s=picto_from_langcode($conf->global->PDF_USE_ALSO_LANGUAGE_CODE, 'class="pictoforlang"');
$s=picto_from_langcode($conf->global->PDF_USE_ALSO_LANGUAGE_CODE, 'class="pictoforlang paddingright"');
$result .= $s;
if ($typeofdata == 'textarea') {
$result .= '<textarea name="field-'.$object->element."-".$fieldname."-".$langcode.'" id="'.$fieldname."-".$langcode.'" class="'.$morecss.'" rows="'.ROWS_2.'" wrap="soft">';

View File

@ -491,7 +491,7 @@ if (empty($reshook))
}
// Fill array 'array_languages' with data from add form
$ret = $object->setValuesForAlternateLanguages();
$ret = $object->setValuesForExtraLanguages();
if ($ret < 0)
{
$error++;
@ -1852,7 +1852,9 @@ else
// Name
print '<tr><td class="titlefieldcreate">'.$form->editfieldkey('ThirdPartyName', 'name', '', $object, 0, 'string', '', 1).'</td>';
print '<td colspan="3"><input type="text" class="minwidth300" maxlength="128" name="name" id="name" value="'.dol_escape_htmltag($object->name).'" autofocus="autofocus"></td></tr>';
print '<td colspan="3"><input type="text" class="minwidth300" maxlength="128" name="name" id="name" value="'.dol_escape_htmltag($object->name).'" autofocus="autofocus">';
print $form->widgetForTranslation("name", $object, $permissiontoadd, 'string', 'alpahnohtml', 'minwidth300');
print '</td></tr>';
// Alias names (commercial, trademark or alias names)
print '<tr id="name_alias"><td><label for="name_alias_input">'.$langs->trans('AliasNames').'</label></td>';
@ -1961,18 +1963,21 @@ else
print '<tr><td class="tdtop">'.$form->editfieldkey('Address', 'address', '', $object, 0).'</td>';
print '<td colspan="3"><textarea name="address" id="address" class="quatrevingtpercent" rows="3" wrap="soft">';
print $object->address;
print '</textarea></td></tr>';
print '</textarea>';
print $form->widgetForTranslation("address", $object, $permissiontoadd, 'textarea', 'alphanohtml', 'quatrevingtpercent');
print '</td></tr>';
// Zip / Town
print '<tr><td>'.$form->editfieldkey('Zip', 'zipcode', '', $object, 0).'</td><td>';
print $formcompany->select_ziptown($object->zip, 'zipcode', array('town', 'selectcountry_id', 'state_id'), 0, 0, '', 'maxwidth50onsmartphone');
print '</td><td>'.$form->editfieldkey('Town', 'town', '', $object, 0).'</td><td>';
print $formcompany->select_ziptown($object->town, 'town', array('zipcode', 'selectcountry_id', 'state_id'));
print $form->widgetForTranslation("town", $object, $permissiontoadd, 'string', 'alphanohtml', 'maxwidth100 quatrevingtpercent');
print '</td></tr>';
// Country
print '<tr><td>'.$form->editfieldkey('Country', 'selectcounty_id', '', $object, 0).'</td><td colspan="3">';
print $form->select_country((GETPOST('country_id') != '' ?GETPOST('country_id') : $object->country_id), 'country_id');
print $form->select_country((GETPOSTISSET('country_id') ? GETPOST('country_id') : $object->country_id), 'country_id');
if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
print '</td></tr>';

View File

@ -1403,6 +1403,15 @@ class Societe extends CommonObject
$error++;
}
}
// Actions on extra languages
if (!$error && empty($conf->global->MAIN_EXTRALANGUAGES_DISABLED)) // For avoid conflicts if trigger used
{
$result = $this->insertExtraLanguages();
if ($result < 0)
{
$error++;
}
}
if (!$error && $call_trigger)
{