Completed
Pull Request — master (#264)
by Robbie
13:29
created

Transformation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getOriginal() 0 4 1
A transformFormField() 0 6 2
A transformCheckboxField() 0 16 2
A baseTransform() 0 20 1
1
<?php
2
3
namespace SilverStripe\Translatable\Model\Translatable;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\CheckboxField_Readonly;
7
use SilverStripe\Forms\CompositeField;
8
use SilverStripe\Forms\FormField;
9
use SilverStripe\Forms\FormTransformation;
10
use SilverStripe\ORM\DataObject;
11
12
/**
13
 * Transform a formfield to a "translatable" representation,
14
 * consisting of the original formfield plus a readonly-version
15
 * of the original value, wrapped in a CompositeField.
16
 *
17
 * @param DataObject $original Needs the original record as we populate
18
 *                   the readonly formfield with the original value
19
 *
20
 * @package translatable
21
 * @subpackage misc
22
 */
23
class Transformation extends FormTransformation
24
{
25
    /**
26
     * @var DataObject
27
     */
28
    private $original = null;
29
30
    public function __construct(DataObject $original)
31
    {
32
        $this->original = $original;
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Returns the original DataObject attached to the Transformation
38
     *
39
     * @return DataObject
40
     */
41
    public function getOriginal()
42
    {
43
        return $this->original;
44
    }
45
46
    public function transformFormField(FormField $field)
47
    {
48
        $newfield = $field->performReadOnlyTransformation();
49
        $fn = 'transform' . get_class($field);
50
        return $this->hasMethod($fn) ? $this->$fn($newfield, $field) : $this->baseTransform($newfield, $field);
51
    }
52
53
    /**
54
     * Transform a translatable CheckboxField to show the field value from the default language
55
     * in the label.
56
     *
57
     * @param FormField $nonEditableField The readonly field to contain the original value
58
     * @param FormField $originalField The original editable field containing the translated value
59
     * @return CheckboxField The field with a modified label
60
     */
61
    protected function transformCheckboxField(CheckboxField_Readonly $nonEditableField, CheckboxField $originalField)
0 ignored issues
show
Unused Code introduced by
The parameter $nonEditableField is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        $label = $originalField->Title();
64
        $fieldName = $originalField->getName();
65
        $value = ($this->original->$fieldName)
66
            ? _t('Translatable_Transform.CheckboxValueYes', 'Yes')
67
            : _t('Translatable_Transform.CheckboxValueNo', 'No');
68
        $originalLabel = _t(
69
            'Translatable_Transform.OriginalCheckboxLabel',
70
            'Original: {value}',
71
            'Addition to a checkbox field label showing the original value of the translatable field.',
72
            array('value'=>$value)
73
        );
74
        $originalField->setTitle($label . ' <span class="originalvalue">(' . $originalLabel . ')</span>');
75
        return $originalField;
76
    }
77
78
    /**
79
     * Transform a translatable field to show the field value from the default language
80
     * DataObject below the translated field.
81
     *
82
     * This is a fallback function which handles field types that aren't transformed by
83
     * $this->transform{FieldType} functions.
84
     *
85
     * @param FormField $nonEditableField The readonly field to contain the original value
86
     * @param FormField $originalField The original editable field containing the translated value
87
     * @return CompositeField The transformed field
88
     */
89
    protected function baseTransform($nonEditableField, $originalField)
90
    {
91
        $fieldname = $originalField->getName();
92
93
        $nonEditableField_holder = new CompositeField($nonEditableField);
94
        $nonEditableField_holder->setName($fieldname.'_holder');
95
        $nonEditableField_holder->addExtraClass('originallang_holder');
96
        $nonEditableField->setValue($this->original->$fieldname);
97
        $nonEditableField->setName($fieldname.'_original');
98
        $nonEditableField->addExtraClass('originallang');
99
        $nonEditableField->setTitle(_t(
100
            'Translatable_Transform.OriginalFieldLabel',
101
            'Original {title}',
102
            'Label for the original value of the translatable field.',
103
            array('title'=>$originalField->Title())
104
        ));
105
106
        $nonEditableField_holder->insertBefore($originalField, $fieldname.'_original');
0 ignored issues
show
Documentation introduced by
$fieldname . '_original' is of type string, but the function expects a object<SilverStripe\Forms\FormField>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
        return $nonEditableField_holder;
108
    }
109
}
110