Skip to content

Commit

Permalink
feat(questionparameter): make translatable
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Feb 15, 2021
1 parent 89b6221 commit 8de2948
Show file tree
Hide file tree
Showing 29 changed files with 235 additions and 77 deletions.
16 changes: 16 additions & 0 deletions inc/abstractfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,20 @@ protected function checkRegex($regex)

return $isValid;
}

public function getTranslatableStrings() {
$strings = [
'itemlink' => [],
'string' => [],
'text' => [],
];

foreach ($this->getParameters() as $fieldName => $parameter) {
foreach ($parameter->getTranslatableStrings() as $type => $subStrings) {
$strings[$type] = array_merge($strings[$type], $subStrings);
}
}

return $strings;
}
}
40 changes: 39 additions & 1 deletion inc/abstractquestionparameter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,27 @@ public function prepareInputforAdd($input) {
return $input;
}

public function deleteObsoleteItems(CommonDBTM $container, array $exclude) : bool {
public function rawSearchOptions() {
$tab = [];
$tab[] = [
'id' => 'common',
'name' => __('Parameter', 'formcreator')
];

$tab[] = [
'id' => '3',
'table' => $this::getTable(),
'field' => 'fieldname',
'name' => __('Field name', 'formcreator'),
'datatype' => 'itemlink',
'massiveaction' => false,
];

return $tab;
}

public function deleteObsoleteItems(CommonDBTM $container, array $exclude): bool
{
$keepCriteria = [
static::$items_id => $container->getID(),
];
Expand All @@ -90,4 +110,22 @@ public function deleteObsoleteItems(CommonDBTM $container, array $exclude) : boo
}
return $this->deleteByCriteria($keepCriteria);
}

public function getTranslatableStrings() {
$strings = [
'itemlink' => [],
'string' => [],
'text' => [],
];
foreach ($this->getTranslatableSearchOptions() as $searchOption) {
$strings[$searchOption['datatype']][] = $this->fields[$searchOption['field']];
}

foreach (array_keys($strings) as $type) {
$strings[$type] = array_unique($strings[$type]);
$strings[$type] = array_filter($strings[$type]);
}

return $strings;
}
}
14 changes: 14 additions & 0 deletions inc/abstracttarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ abstract class PluginFormcreatorAbstractTarget extends CommonDBChild implements
{
use PluginFormcreatorConditionnableTrait;
use PluginFormcreatorExportableTrait;
use PluginFormcreatorTranslatable;

static public $itemtype = PluginFormcreatorForm::class;
static public $items_id = 'plugin_formcreator_forms_id';
Expand Down Expand Up @@ -2070,4 +2071,17 @@ public function deleteObsoleteItems(CommonDBTM $container, array $exclude) : boo
}
return $this->deleteByCriteria($keepCriteria);
}

public function getTranslatableStrings() {
$strings = [
'itemlink' => [],
'string' => [],
'text' => [],
];
foreach ($this->getTranslatableSearchOptions() as $searchOption) {
$strings[$searchOption['datatype']][] = $this->fields[$searchOption['field']];
}

return $strings;
}
}
5 changes: 0 additions & 5 deletions inc/field/actorfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,4 @@ public function isVisibleField(): bool {
public function isEditableField(): bool {
return true;
}

public function getTranslatableStrings()
{
return [];
}
}
4 changes: 3 additions & 1 deletion inc/field/checkboxesfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ public function isEditableField(): bool {

public function getTranslatableStrings()
{
return ['text' => array_values($this->getAvailableValues())];
$strings = parent::getTranslatableStrings();
$strings['text'] = array_merge($strings['text'], array_values($this->getAvailableValues()));
return $strings;
}
}
4 changes: 0 additions & 4 deletions inc/field/datefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,4 @@ public function isVisibleField(): bool {
public function isEditableField(): bool {
return true;
}

public function getTranslatableStrings() {
return [];
}
}
4 changes: 0 additions & 4 deletions inc/field/datetimefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,4 @@ public function isVisibleField(): bool {
public function isEditableField(): bool {
return true;
}

public function getTranslatableStrings() {
return [];
}
}
5 changes: 0 additions & 5 deletions inc/field/descriptionfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,4 @@ public function isVisibleField(): bool {
public function isEditableField(): bool {
return false;
}

public function getTranslatableStrings()
{
return [];
}
}
5 changes: 0 additions & 5 deletions inc/field/emailfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,4 @@ public function isVisibleField(): bool {
public function isEditableField(): bool {
return true;
}

public function getTranslatableStrings()
{
return [];
}
}
5 changes: 0 additions & 5 deletions inc/field/filefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,4 @@ public function isVisibleField(): bool {
public function isEditableField(): bool {
return true;
}

public function getTranslatableStrings()
{
return [];
}
}
5 changes: 0 additions & 5 deletions inc/field/floatfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,4 @@ public function isVisibleField(): bool {
public function isEditableField(): bool {
return true;
}

public function getTranslatableStrings()
{
return [];
}
}
5 changes: 0 additions & 5 deletions inc/field/hostnamefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,4 @@ public function isVisibleField(): bool {
public function isEditableField(): bool {
return false;
}

public function getTranslatableStrings()
{
return [];
}
}
5 changes: 0 additions & 5 deletions inc/field/ipfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,4 @@ public function isVisibleField(): bool {
public function isEditableField(): bool {
return false;
}

public function getTranslatableStrings()
{
return [];
}
}
4 changes: 0 additions & 4 deletions inc/field/ldapselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,4 @@ public function isVisibleField(): bool {
public function isEditableField(): bool {
return true;
}

public function getTranslatableStrings() {
return [];
}
}
4 changes: 3 additions & 1 deletion inc/field/radiosfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ public function isEditableField(): bool {

public function getTranslatableStrings()
{
return ['text' => array_values($this->getAvailableValues())];
$strings = parent::getTranslatableStrings();
$strings['text'] = array_merge($strings['text'], array_values($this->getAvailableValues()));
return $strings;
}
}
5 changes: 0 additions & 5 deletions inc/field/tagfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,4 @@ public function isVisibleField(): bool {
public function isEditableField(): bool {
return true;
}

public function getTranslatableStrings()
{
return [];
}
}
4 changes: 3 additions & 1 deletion inc/field/textareafield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ public function getHtmlIcon(): string {

public function getTranslatableStrings()
{
return ['string' => [$this->question->fields['default_values']]];
$strings = parent::getTranslatableStrings();
$strings['string'] = array_merge($strings['string'], array_values($this->getAvailableValues()));
return $strings;
}
}
4 changes: 3 additions & 1 deletion inc/field/textfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ public function isEditableField(): bool {

public function getTranslatableStrings()
{
return ['text' => [$this->question->fields['default_values']]];
$strings = parent::getTranslatableStrings();
$strings['text'][] = $this->question->fields['default_values'];
return $strings;
}
}
4 changes: 0 additions & 4 deletions inc/field/timefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,4 @@ public function isVisibleField(): bool {
public function isEditableField(): bool {
return true;
}

public function getTranslatableStrings() {
return [];
}
}
5 changes: 0 additions & 5 deletions inc/field/urgencyfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,4 @@ public function isVisibleField(): bool {
public function isEditableField(): bool {
return true;
}

public function getTranslatableStrings()
{
return [];
}
}
8 changes: 8 additions & 0 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2509,6 +2509,14 @@ public function getTranslatableStrings() {
}
}

foreach (self::getTargetTypes() as $targetType) {
foreach ((new $targetType())->getTargetsForForm($this->getID()) as $target) {
foreach ($target->getTranslatableStrings() as $type => $subStrings) {
$strings[$type] = array_merge($strings[$type], $subStrings);
}
}
}

// deduplicate strings and remove empty strings
foreach (array_keys($strings) as $type) {
$strings[$type] = array_unique($strings[$type]);
Expand Down
2 changes: 1 addition & 1 deletion inc/question.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ public function post_getFromDB() {
* @return void
*/
private function loadField($fieldType) {
if (!$this->field === null || $this->isNewItem()) {
if (!$this->field === null) {
return;
}
$this->field = PluginFormcreatorFields::getFieldInstance($fieldType, $this);
Expand Down
25 changes: 25 additions & 0 deletions inc/questionrange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,38 @@
class PluginFormcreatorQuestionRange
extends PluginFormcreatorAbstractQuestionParameter
{
use PluginFormcreatorTranslatable;

protected $domId = 'plugin_formcreator_questionRange';

public static function getTypeName($nb = 0) {
return _n('Question range', 'Question ranges', $nb, 'formcreator');
}

public function rawSearchOptions() {
$tab = parent::rawSearchOptions();

$tab[] = [
'id' => '4',
'table' => $this::getTable(),
'field' => 'range_min',
'name' => __('Minimum range', 'formcreator'),
'datatype' => 'integer',
'massiveaction' => false,
];

$tab[] = [
'id' => '4',
'table' => $this::getTable(),
'field' => 'range_max',
'name' => __('maximum range', 'formcreator'),
'datatype' => 'integer',
'massiveaction' => false,
];

return $tab;
}

public function getParameterFormSize() {
return 0;
}
Expand Down
17 changes: 17 additions & 0 deletions inc/questionregex.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,30 @@
class PluginFormcreatorQuestionRegex
extends PluginFormcreatorAbstractQuestionParameter
{
use PluginFormcreatorTranslatable;

protected $domId = 'plugin_formcreator_questionRegex';

public static function getTypeName($nb = 0) {
return _n('Question regular expression', 'Question regular expressions', $nb, 'formcreator');
}

public function rawSearchOptions() {
$tab = parent::rawSearchOptions();

$tab[] = [
'id' => '4',
'table' => $this::getTable(),
'field' => 'regex',
'name' => __('Regular expression', 'formcreator'),
'datatype' => 'text',
'massiveaction' => false,
];

return $tab;
}


public function getParameterFormSize() {
return 1;
}
Expand Down
Loading

0 comments on commit 8de2948

Please sign in to comment.