Skip to content
This repository was archived by the owner on Sep 15, 2018. It is now read-only.

Commit

Permalink
Refactor package
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderPoellmann committed Jul 26, 2017
1 parent 7562f1f commit 0a4b53c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 48 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015-2016 Alexander Manfred Poellmann
Copyright (c) 2015-2017 Alexander Manfred Poellmann

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 4 additions & 1 deletion database/migrations/create_translations_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTranslatableTable extends Migration
/**
* Class CreateTranslationsTable
*/
class CreateTranslationsTable extends Migration
{
/**
* Table names.
Expand Down
6 changes: 3 additions & 3 deletions src/Contracts/TranslatableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
interface TranslatableInterface
{
/**
* @param Model $model
* @param string $locale The locale $model is translated to
* @param Model $model
* @param string $locale The locale $model is translated to.
* @return $this
*/
public function translates( Model $model, $locale );
public function translates(Model $model, $locale);
}
70 changes: 70 additions & 0 deletions src/Models/Translation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php namespace Lecturize\Translatable\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
* Class Translation
* @package Lecturize\Translatable\Models
*/
class Translation extends Model
{
/**
* @inheritdoc
*/
protected $fillable = [
'language_id',
'translatable_id',
'translatable_type',
'translation_id',
'translation_type',
];

/**
* @inheritdoc
*/
public $timestamps = false;

/**
* @inheritdoc
*/
public function __construct()
{
parent::__construct();

$this->table = config('lecturize.translations.table', 'translations');
}

/**
* Get the translateable.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function translatable()
{
return $this->morphTo();
}

/**
* Get the translation.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function translation()
{
return $this->morphTo();
}

/**
* Scope translatables.
*
* @param mixed $query
* @param Model $model
* @return mixed
*/
public function scopeTranslatables($query, Model $model)
{
return $query->where('translatable_id', $model->id)
->where('translatable_type', get_class($model));
}
}
58 changes: 15 additions & 43 deletions src/Traits/CanBeTranslated.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
<?php namespace Lecturize\Translatable\Traits;

use Illuminate\Database\Eloquent\Model;
use Lecturize\Translatable\Models\Translatable;
use Lecturize\Translatable\Models\Translation;

/**
* Class TranslatableTrait
* @package Lecturize\Translatable\Contracts
* Trait CanBeTranslated
* @package Lecturize\Translatable\Traits
*/
trait TranslatableTrait
trait CanBeTranslated
{
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function translations()
{
return $this->morphMany(Translatable::class, 'translatable');
return $this->morphMany(Translation::class, 'translatable');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public function translatable()
{
return $this->morphOne(Translatable::class, 'translation');
return $this->morphOne(Translation::class, 'translation');
}

/**
* @param Model $model
* @param string $locale The locale $model is translated to
* @return $this
*/
public function translates( Model $model, $locale )
public function translates(Model $model, $locale)
{
// check if target translation is source translation
if ( $this->language == $model->language )
if ($this->language == $model->language)
return $this;

// associate the translations
if ( Translatable::translatables($this)->first() ) {
if (Translation::translatables($this)->first()) {
// $this is the translatable model
Translatable::firstOrCreate([
Translation::firstOrCreate([
'locale' => $locale,
'translatable_id' => $this->id,
'translatable_type' => get_class($this),
Expand All @@ -48,7 +48,7 @@ public function translates( Model $model, $locale )
]);
} else {
// $this is the translation model
Translatable::firstOrCreate([
Translation::firstOrCreate([
'locale' => $locale,
'translatable_id' => $model->id,
'translatable_type' => get_class($model),
Expand All @@ -65,14 +65,14 @@ public function translates( Model $model, $locale )
*/
public function getTranslations()
{
if ( $translatable = $this->getTranslatable() ) {
if ($translatable = $this->getTranslatable()) {
$translations[] = $translatable;
foreach ( $translatable->translations as $t ) {
foreach ($translatable->translations as $t) {
$translations[] = $t->translation()->first();
}
} else {
$translations = [];
foreach ( $this->translations as $t ) {
foreach ($this->translations as $t) {
$translations[] = $t->translation()->first();
}
}
Expand All @@ -85,37 +85,9 @@ public function getTranslations()
*/
public function getTranslatable()
{
if ( $translatable = $this->translatable )
if ($translatable = $this->translatable)
return $translatable->translatable()->first();

return null;
}

/**
* Get translations as linked array
*
* @return null|array
*/
public function getTranslationsArray()
{
$translations = $this->getTranslations();

if ( count($translations) > 0 ) {
$list = [];
foreach ( $translations as $translation ) {
if ( $translation->language == $this->language )
continue;

$t = '<a href="'. get_post_url($translation) .'" title="'. $translation->title .'">';
$t.= $translation->language->name;
$t.= '</a>';

$list[] = $t;
}

return $list;
}

return null;
}
}

0 comments on commit 0a4b53c

Please sign in to comment.