Skip to content

Commit

Permalink
Code styling improvement.
Browse files Browse the repository at this point in the history
  • Loading branch information
bummzack committed Nov 28, 2018
1 parent 24b94ac commit 047d8bf
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
4 changes: 2 additions & 2 deletions _config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* translatable module is a requirement: https://github.com/silverstripe/silverstripe-translatable
*/

if(!defined('TRANSLATABLE_COLUMN_SEPARATOR')){
define('TRANSLATABLE_COLUMN_SEPARATOR', '__');
if (!defined('TRANSLATABLE_COLUMN_SEPARATOR')) {
define('TRANSLATABLE_COLUMN_SEPARATOR', '__');
}

SiteTree::add_extension('TranslatableUtility');
15 changes: 8 additions & 7 deletions code/TranslatableFormFieldTransformation.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

class TranslatableFormFieldTransformation extends FormTransformation
{

Expand Down Expand Up @@ -58,19 +59,19 @@ protected function baseTransform($nonEditableField, $originalField, $fieldname)
{
/** @var CompositeField $nonEditableField_holder */
$nonEditableField_holder = CompositeField::create($nonEditableField);
$nonEditableField_holder->setName($fieldname.'_holder');
$nonEditableField_holder->setName($fieldname . '_holder');
$nonEditableField_holder->addExtraClass('originallang_holder');
$nonEditableField->setValue($this->original->$fieldname);
$nonEditableField->setName($fieldname.'_original');
$nonEditableField->setName($fieldname . '_original');
$nonEditableField->addExtraClass('originallang');
$nonEditableField->setTitle(_t(
'Translatable_Transform.OriginalFieldLabel',
'Original {title}',
'Label for the original value of the translatable field.',
array('title'=>$originalField->Title())
'Translatable_Transform.OriginalFieldLabel',
'Original {title}',
'Label for the original value of the translatable field.',
array('title' => $originalField->Title())
));

$nonEditableField_holder->insertBefore($originalField, $fieldname.'_original');
$nonEditableField_holder->insertBefore($originalField, $fieldname . '_original');
return $nonEditableField_holder;
}
}
46 changes: 26 additions & 20 deletions code/extensions/TranslatableDataObject.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Translatable extension, inspired by Uncle Cheese's implementation
* https://github.com/unclecheese/TranslatableDataObject but tailored to be used
Expand Down Expand Up @@ -241,7 +242,8 @@ public function getLocalizedFormField($fieldName, $locale)
* @param string $locale
* @return FormField
*/
public function getLocalizedRelationField($fieldName, $locale){
public function getLocalizedRelationField($fieldName, $locale)
{
$baseName = $this->getBasename($fieldName);
$localizedFieldName = self::localized_field($fieldName, $locale);

Expand Down Expand Up @@ -325,7 +327,8 @@ public function getLocalizedFieldName($fieldName)
* @param string $fieldName the name of the relation without any locale extension. Eg. "Attachment"
* @return bool
*/
public function isLocalizedRelation($fieldName){
public function isLocalizedRelation($fieldName)
{
return isset(self::$localizedFields[$this->ownerBaseClass . '_has_one'][$fieldName]);
}

Expand All @@ -334,15 +337,16 @@ public function isLocalizedRelation($fieldName){
* @param string $fieldName the name of the field without any locale extension. Eg. "Title"
* @param boolean $strict if false, this will fallback to the master version of the field!
*/
public function getLocalizedRelation($fieldName, $strict = true){
public function getLocalizedRelation($fieldName, $strict = true)
{
$localizedField = $this->getLocalizedFieldName($fieldName);

if($strict){
if ($strict) {
return $this->owner->getRelation($localizedField);
}

// if not strict, check localized first and fallback to fieldname
if($value = $this->owner->getRelation($localizedField)){
if ($value = $this->owner->getRelation($localizedField)) {
return $value;
}

Expand Down Expand Up @@ -663,12 +667,13 @@ protected static function collectDBFields($class)
* @param string $class
* @return array
*/
protected static function collectHasOneRelations($class){
if(isset(self::$collectorCache[$class . '_has_one'])){
protected static function collectHasOneRelations($class)
{
if (isset(self::$collectorCache[$class . '_has_one'])) {
return self::$collectorCache[$class . '_has_one'];
}

if(isset(self::$collectorLock[$class . '_has_one']) && self::$collectorLock[$class . '_has_one']){
if (isset(self::$collectorLock[$class . '_has_one']) && self::$collectorLock[$class . '_has_one']) {
return null;
}
self::$collectorLock[$class . '_has_one'] = true;
Expand All @@ -683,26 +688,26 @@ protected static function collectHasOneRelations($class){
$locales = self::get_target_locales();

// remove the default locale
if(($index = array_search(Translatable::default_locale(), $locales)) !== false) {
if (($index = array_search(Translatable::default_locale(), $locales)) !== false) {
array_splice($locales, $index, 1);
}

// fields that should be translated
$fieldsToTranslate = array();

// validate the arguments
if($arguments){
foreach($arguments as $field){
if ($arguments) {
foreach ($arguments as $field) {
// only allow fields that are actually in our field list
if(array_key_exists($field, $fields)){
if (array_key_exists($field, $fields)) {
$fieldsToTranslate[] = $field;
}
}
} else {
// check for the given default field types and add all fields of that type
foreach($fields as $field => $type){
foreach ($fields as $field => $type) {
$typeClean = (($p = strpos($type, '(')) !== false) ? substr($type, 0, $p) : $type;
if(in_array($typeClean, self::$default_field_types)){
if (in_array($typeClean, self::$default_field_types)) {
$fieldsToTranslate[] = $field;
}
}
Expand All @@ -712,9 +717,9 @@ protected static function collectHasOneRelations($class){
// gather all the DB fields
$additionalFields = array();
self::$localizedFields[$class . '_has_one'] = array();
foreach($fieldsToTranslate as $field){
foreach ($fieldsToTranslate as $field) {
self::$localizedFields[$class . '_has_one'][$field] = array();
foreach($locales as $locale){
foreach ($locales as $locale) {
$localizedName = self::localized_field($field, $locale);
self::$localizedFields[$class . '_has_one'][$field][] = $localizedName;
$additionalFields[$localizedName] = $fields[$field];
Expand All @@ -727,7 +732,6 @@ protected static function collectHasOneRelations($class){
}



/**
* Get the locales that should be translated
* @return array containing the locales to use
Expand Down Expand Up @@ -770,10 +774,11 @@ protected static function get_arguments($class)

/**
* Setter for relation specific FormField. Will be cloned in scaffolding functions
* @param string $fieldName
* @param string $fieldName
* @param FormField $field
*/
public function setFieldForRelation($fieldName, FormField $field) {
public function setFieldForRelation($fieldName, FormField $field)
{
if (self::isLocalizedRelation($fieldName)) {
self::$localizedFields[$this->ownerBaseClass . '_has_one'][$fieldName]['FormField'] = $field;
}
Expand All @@ -782,7 +787,8 @@ public function setFieldForRelation($fieldName, FormField $field) {
/**
* @param $fieldName
*/
public function getFieldForRelation($fieldName){
public function getFieldForRelation($fieldName)
{
if (self::isLocalizedRelation($fieldName)
&& array_key_exists('FormField', self::$localizedFields[$this->ownerBaseClass . '_has_one'][$fieldName])) {
return self::$localizedFields[$this->ownerBaseClass . '_has_one'][$fieldName]['FormField'];
Expand Down
13 changes: 7 additions & 6 deletions code/extensions/TranslatableUtility.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

class TranslatableUtility extends DataExtension
{
/**
Expand Down Expand Up @@ -26,7 +27,7 @@ public static function get_content_languages()
{
$table = Versioned::current_stage() == 'Live' ? 'SiteTree_Live' : 'SiteTree';

if(class_exists('SQLSelect')){
if (class_exists('SQLSelect')) {
$query = new SQLSelect("Distinct \"Locale\"", "\"$table\"");
} else {
// SS 3.1 compat
Expand Down Expand Up @@ -61,9 +62,9 @@ public static function get_content_languages()
* <code>
* <!-- in your template -->
* <ul class="langNav">
* <% loop Languages %>
* <li><a href="$Link" class="$LinkingMode" title="$Title.ATT">$Language</a></li>
* <% end_loop %>
* <% loop Languages %>
* <li><a href="$Link" class="$LinkingMode" title="$Title.ATT">$Language</a></li>
* <% end_loop %>
* </ul>
* </code>
*
Expand Down Expand Up @@ -98,9 +99,9 @@ public function Languages()
'RFC1766' => i18n::convert_rfc1766($locale),
// the language 2 letter code (eg. EN)
'Language' => DBField::create_field('Varchar',
strtoupper(i18n::get_lang_from_locale($locale))),
strtoupper(i18n::get_lang_from_locale($locale))),
// the language as written in its native language
'Title' => DBField::create_field('Varchar', html_entity_decode(
'Title' => DBField::create_field('Varchar', html_entity_decode(
i18n::get_language_name(i18n::get_lang_from_locale($locale), true), ENT_NOQUOTES, 'UTF-8'
)),
// linking mode (useful for css class)
Expand Down
3 changes: 2 additions & 1 deletion code/extensions/TranslatedFile.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Extension that creates translated form fields for files
* @author bummzack
Expand Down Expand Up @@ -31,7 +32,7 @@ public static function translatable_uploadfield($name, SS_List $collection, $tit
} else {
$fieldName = 'Translate' . $name;
// for all other languages, access the files in read-only
if (!empty($sortField) && $collection instanceof SS_Sortable){
if (!empty($sortField) && $collection instanceof SS_Sortable) {
/** @var UploadField $uploadField */
$uploadField = UploadField::create($fieldName, $title, $collection->sort($sortField));
} else {
Expand Down

0 comments on commit 047d8bf

Please sign in to comment.