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) |
|
|
|
|
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'); |
|
|
|
|
107
|
|
|
return $nonEditableField_holder; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.