Skip to content

Commit

Permalink
Added a flag to disallow edits when importing entries from a file.
Browse files Browse the repository at this point in the history
Signed-off-by: William <[email protected]>
  • Loading branch information
William committed Oct 25, 2013
1 parent 274d2d5 commit adf8193
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
34 changes: 23 additions & 11 deletions src/Waavi/Translation/Models/LanguageEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ class LanguageEntry extends WaaviModel {
* @var array
*/
public $rules = array(
'language_id' => 'required',
'namespace' => '',
'group' => 'required',
'item' => 'required',
'text' => 'required',
'unstable' => '',
'language_id' => 'required', // Language FK
'namespace' => '', // Language Entry namespace. Default is *
'group' => 'required', // Entry group, references the name of the file the translation was originally stored in.
'item' => 'required', // Entry code.
'text' => 'required', // Translation text.
'unstable' => '', // If this flag is set to true, the text in the default language has changed since this entry was last updated.
'overwrite' => '', // If this flag is set to false, then this entry may not be overwritten by a file entry.
);

/**
Expand Down Expand Up @@ -62,18 +63,29 @@ public function updateText($text, $isDefault = false)
$this->text = $text;
if ($this->save()) {
if ($isDefault) {
LanguageEntry::where('namespace', '=', $this->namespace)
->where('group', '=', $this->group)
->where('item', '=', $this->item)
->where('language_id', '!=', $this->language_id)
->update(array('unstable' => '1'));
$this->flagSiblingsUnstable();
}
return true;
} else {
return false;
}
}

/**
* Flag all siblings as unstable.
*
*/
public function flagSiblingsUnstable()
{
if ($this->id) {
LanguageEntry::where('namespace', '=', $this->namespace)
->where('group', '=', $this->group)
->where('item', '=', $this->item)
->where('language_id', '!=', $this->language_id)
->update(array('unstable' => '1'));
}
}

/**
* Returns a list of entries that contain a translation for this item in the given language.
*
Expand Down
14 changes: 3 additions & 11 deletions src/Waavi/Translation/Providers/LanguageEntryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,12 @@ public function loadArray(array $lines, $language, $group, $namespace = null, $i
->where('language_id', '=', $language->id)
->first();

// If the entry already exists and its text is different from the parameters:
// If the entry already exists, its text is different from the parameters, and it may be overwritten, we do so:
if ($entry) {
if ($entry->text != $text) {
if ($entry->text != $text && $entry->overwrite) {
$entry->text = $text;
if($entry->save() && $isDefault) {
// If we just updated a line from the default language, flag all translations as unstable.
$this
->createModel()
->newQuery()
->where('namespace', '=', $namespace)
->where('group', '=', $group)
->where('item', '=', $item)
->where('language_id', '!=', $language->id)
->update(array('unstable' => '1'));
$entry->flagSiblingsUnstable();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Waavi/Translation/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected function registerLoader()
case 'mixed':
return new MixedLoader($languageProvider, $langEntryProvider, $app);

case 'filesystem':
default: case 'filesystem':
return new FileLoader($languageProvider, $langEntryProvider, $app);

case 'database':
Expand Down
29 changes: 29 additions & 0 deletions src/migrations/2013_10_25_140034_user_edited.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;

class UserEdited extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('language_entries', function($table){
$table->boolean('overwrite')->after('unstable')->default(1);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}

}

0 comments on commit adf8193

Please sign in to comment.