use Cubist\Backpack\app\Console\Command\MigrateCommand;
use Cubist\Backpack\app\Console\Commands\GenerateCommand;
use Cubist\Backpack\app\Console\Commands\InstallCommand;
+use Cubist\Backpack\app\Console\Commands\LocaleCopy;
use Cubist\Backpack\app\Console\Commands\SearchIndexCommand;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
*/
public function register()
{
- $this->commands([InstallCommand::class, GenerateCommand::class, MigrateCommand::class, SearchIndexCommand::class]);
+ $this->commands([
+ InstallCommand::class,
+ GenerateCommand::class,
+ MigrateCommand::class,
+ SearchIndexCommand::class,
+ LocaleCopy::class
+ ]);
}
}
namespace Cubist\Backpack\app\Console\Commands;
-
use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel;
+use Cubist\Backpack\app\Magic\Models\CubistMagicTranslatableModel;
class LocaleCopy extends CubistCommand
{
*
* @var string
*/
- protected $signature = 'cubist:locale:copy {from} {to}';
+ protected $signature = 'cubist:locale:copy {from} {to} {--overwrite}';
/**
* The console command description.
public function handle()
{
-
-
- $this->call('backup:run');
-
+ // $this->call('backup:run');
$this->_handleMagicFolder([$this, '_copyModel']);
+ $this->call('cache:clear');
}
/**
*/
protected function _copyModel($model)
{
- $from = $this->argument('from');
- $to = $this->argument('to');
- echo 'Copy locale of model ' . $model . ' from ' . $from . ' to ' . $to;
+ if (!($model instanceof CubistMagicTranslatableModel)) {
+ return;
+ }
+ /** @var CubistMagicTranslatableModel $model */
+ $model->copyTranslations($this->argument('from'), $this->argument('to'), $this->option('overwrite'));
}
}
public function bulkPublish()
{
- return $this->_bulkChangeOnlineStatus(true);
+ return $this->_bulkChangeOnlineStatus(1);
}
public function bulkOffline()
{
- return $this->_bulkChangeOnlineStatus(false);
+ return $this->_bulkChangeOnlineStatus(0);
}
public function saveReorder()
}
/**
- * @param $template TemplateAbstract
+ * @param $template TemplateAbstract|string|null
* @param $controller CubistMagicController
* @throws \Exception
*/
- public function useTemplate($template)
+ public function useTemplate($template = null)
{
+ if (null === $template) {
+ $template = $this->getAttributeValue('template');
+ }
+
if (is_string($template)) {
$template = TemplateAbstract::getTemplateIntanceByName($template);
}
- if (null === $template) {
- return;
- }
$this->_usedTemplate = $template;
{
if ($key !== null) {
$this->guardAgainstNonTranslatableAttribute($key);
+ $v = $this->getAttributes()[$key];
+ if (is_string($v)) {
+ $v = json_decode($v ?? '' ?: '{}', true);
+ }
+ if (!is_array($v)) {
+ $v = [];
+ }
if ($key === 'slug') {
- return array_filter(json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: [], function ($value) {
+ return array_filter($v, function ($value) {
return $value !== null;
});
} else {
- return array_filter(json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: [], function ($value) {
+ return array_filter($v, function ($value) {
return $value !== null && $value !== '';
});
}
});
}
+ public function copyTranslations($from, $to, $overwrite = false)
+ {
+ $i = 0;
+ foreach (static::all() as $item) {
+ //echo 'instance ' . $item->id . ' ' . implode(', ', $item->translatable) . "\n";
+ if ($item instanceof CMSPage) {
+ $item->useTemplate();
+ }
+
+ /** @var $item self */
+
+ foreach ($item->translatable as $translatable) {
+ if (!$item->hasTranslation($translatable, $from)) {
+ //echo 'no translation for ' . $translatable . "\n";
+ continue;
+ }
+ if ($overwrite || !$item->hasTranslation($translatable, $to)) {
+ // echo "copy translation for " . $translatable . "\n";
+ $translated = $item->getTranslation($translatable, $from);
+ $item->setTranslation($translatable, $to, $translated);
+ } else {
+ // echo "no overwriting existing for " . $translatable . "\n";
+ }
+ }
+ $item->save();
+ $i++;
+ }
+ echo 'copy translation of ' . $i . ' instances of ' . get_class($this) . ' from ' . $from . ' to ' . $to . "\n";
+ }
+
public function update(array $attributes = [], array $options = [])
{
return $this->updateTranslations($this->_prepareData($attributes), $options);