17 nov 2018

This commit is contained in:
Kevin Adametz 2018-11-17 02:03:59 +01:00
parent 0c9a118281
commit 765d6a2f6b
52 changed files with 3200 additions and 229 deletions

View file

@ -3,43 +3,413 @@
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Models\BookingDraftItem;
use App\Models\Draft;
use App\Models\TravelProgram;
use Carbon\Carbon;
use HTMLHelper;
class DraftController extends Controller
{
public $successStatus = 200;
public $data = "";
public $room_str = "";
public $room_name = "";
public $className = "";
public $option = "";
public function draft($action)
{
if($action == "get_draft_list"){
if ($action == "get_draft_list") {
$drafts = Draft::where('active', true)->get()->sortByDesc("id");
return response()->json(['success' => $drafts], $this->successStatus);
}
if($action == "get_draft_list_for_table"){
if ($action == "get_draft_list_for_table") {
$ret = [];
if(request('program_id') && request('program_id') > 0){
if (request('program_id') && request('program_id') > 0) {
$travel_program = TravelProgram::find(request('program_id'));
if(count($travel_program->travel_program_drafts)){
foreach ($travel_program->travel_program_drafts as $travel_program_draft){
if ($travel_program && count($travel_program->travel_program_drafts)) {
foreach ($travel_program->travel_program_drafts as $travel_program_draft) {
$key = $travel_program_draft->id;
$ret[$key]['name'] = $travel_program_draft->draft->name;
if($travel_program_draft->travel_class){
if ($travel_program_draft->travel_class) {
$ret[$key]['travel_class'] = $travel_program_draft->travel_class->name;
}else{
} else {
$ret[$key]['travel_class'] = "alle Kategorien";
}
$ret[$key]['weekdays'] = HTMLHelper::getWeekdaysString($travel_program_draft->weekdays);
$ret[$key]['weekdays'] = HTMLHelper::getWeekdaysString($travel_program_draft->weekdays);
}
}
}
return response()->json(['success' => $ret], $this->successStatus);
}
if ($action == "create_drafts_from_booking") {
$data = \Input::all();
$this->data = $data;
$weekday = date('w', strtotime($data['startDateStr']));
$travel_program = TravelProgram::find($data['travel_program_id']);
$draft = $this->getDraftByTravelProgramData($travel_program, $data, $weekday);
$start_date = Carbon::parse($data['startDateStr']);
$end_date = Carbon::parse($data['endDateStr']);
if($draft){
//------------------------------------------------------------------------------
//prepare rooms
//request('rooms'); // //["rooms"]=> array(1) { [0]=> object(stdClass)#7284 (5) { ["name"]=> string(12) "Einzelzimmer" ["price_adult"]=> float(1682.16) ["adult"]=> int(1) ["children"]=> int(0) ["price_children"]=> int(0) }
$price_info_rooms = [];
$room_names = [];
$room_adult = 0;
$room_children = 0;
$room_price_adult = 0;
$room_price_children = 0;
$data['rooms'] = array_reverse($data['rooms']);
foreach ($data['rooms'] as $room){
$room_adult += $room['adult'];
$room_children += $room['children'];
$room_price_adult += ($room['price_adult'] * $room['adult']);
$room_price_children += ($room['price_children'] * $room['children']);
if(isset($room_names[$room['name']])){
$room_names[$room['name']] = $room_names[$room['name']]+1;
}else{
$room_names[$room['name']] = 1;
}
$service = trans('_vorlagen.grundpreis_reise');
$this->room_name = $room['name'];
$service = $this->replaceService($service);
$price_info_rooms[] = [
'booking_id' => $data['booking_id'],
'travel_program_id' => $data['travel_program_id'],
'travel_class_id' => null,
'draft_item_id' => null,
'draft_type_id' => 30,
'request_date' => $data['request_date'],
'days_start' => null,
'days_duration' => null,
'start_date' => $data['startDateStr'],
'end_date' => $data['endDateStr'],
'service' => $service,
'price_adult' => $room['price_adult'],
'adult' => $room['adult'],
'price_children' => $room['price_children'],
'children' => $room['children'],
'pos' => 0,
'in_pdf' => true,
'comfort' => $data['comfort']
];
}
$this->room_str = "";
foreach ($room_names as $name=>$count){
$this->room_str .= $count.' x '.$name.', ';
}
$this->room_str = rtrim($this->room_str, ', ');
//------------------------------------------------------------------------------
//prepare extra_charge
$price_info_extra_charge = [];
if($data['departure_extra_charge'] > 0){
$service = trans('_vorlagen.aufpreis_flug');
$service = $this->replaceService($service);
$price_info_extra_charge[] = [
'booking_id' => $data['booking_id'],
'travel_program_id' => $data['travel_program_id'],
'travel_class_id' => null,
'draft_item_id' => null,
'draft_type_id' => 33,
'request_date' => $data['request_date'],
'days_start' => null,
'days_duration' => null,
'start_date' => null,
'end_date' => null,
'service' => $service,
'price_adult' => $data['departure_extra_charge'] ,
'adult' => $data['traveler'] ,
'price_children' => 0,
'children' => 0,
'pos' => 0,
'in_pdf' => true,
'comfort' => $data['comfort']
];
}
//------------------------------------------------------------------------------
//prepare $class_options
$price_info_class_options = [];
foreach ($data['class_options'] as $class_option){
$service = trans('_vorlagen.aufpreis_kategorie');
$this->className = $class_option['name'];
$service = $this->replaceService($service);
$price_info_class_options[] = [
'booking_id' => $data['booking_id'],
'travel_program_id' => $data['travel_program_id'],
'travel_class_id' => null,
'draft_item_id' => null,
'draft_type_id' => 32,
'request_date' => $data['request_date'],
'days_start' => null,
'days_duration' => null,
'start_date' => null,
'end_date' => null,
'service' => $service,
'price_adult' => $class_option['price'],
'adult' => $class_option['count'],
'price_children' => 0,
'children' => 0,
'pos' => 0,
'in_pdf' => true,
'comfort' => $data['comfort']
];
}
//------------------------------------------------------------------------------
//prepare $travel_options
$price_info_travel_options = [];
foreach ($data['travel_options'] as $travel_option){
$service = trans('_vorlagen.aufpreis_option');
$this->option = $travel_option['name'];
$service = $this->replaceService($service);
$price_info_travel_options[] = [
'booking_id' => $data['booking_id'],
'travel_program_id' => $data['travel_program_id'],
'travel_class_id' => null,
'draft_item_id' => null,
'draft_type_id' => 34,
'request_date' => $data['request_date'],
'days_start' => null,
'days_duration' => null,
'start_date' => null,
'end_date' => null,
'service' => $service,
'price_adult' => $travel_option['price_adult'],
'adult' => $travel_option['adult'],
'price_children' => $travel_option['price_children'],
'children' => $travel_option['children'],
'pos' => 0,
'in_pdf' => true,
'comfort' => $data['comfort']
];
}
$info_texts = [];
$last_post = 0;
foreach ($draft->draft_items as $draft_item){
$service = $draft_item->service;
$price_adult = 0;
$adult = 0;
$price_children = 0;
$children = 0;
switch ($draft_item->draft_type->id){
case 24: //rundreise
$price_adult = $room_price_adult;
$adult = $room_adult;
$price_children = $room_price_children;
$children = $room_children;
break;
case 4: //flug
// "Hinflug: Rückflug: Flugzeiten ?"
break;
}
$start_date_temp = null;
if($draft_item->days_start > 0){
$start_date_temp = clone $start_date;
$start_date_temp->addDay($draft_item->days_start-1);
}
$end_date_temp = null;
if($draft_item->days_duration > 0){
$end_date_temp = clone $start_date;
$end_date_temp->addDay($draft_item->days_duration-1);
}
$service = $this->replaceService($service);
if($draft_item->draft_type->id == 27){
//text -at last
$info_texts[] = [
'booking_id' => $data['booking_id'],
'travel_program_id' => $data['travel_program_id'],
'travel_class_id' => null,
'draft_item_id' => $draft_item->id,
'draft_type_id' => $draft_item->draft_type->id,
'request_date' => $data['request_date'],
'days_start' => $draft_item->days_start,
'days_duration' => $draft_item->days_duration,
'start_date' => $start_date_temp ? $start_date_temp->format("Y-m-d") : null,
'end_date' => $end_date_temp ? $end_date_temp->format("Y-m-d") : null,
'service' => $service,
'price_adult' => $price_adult,
'adult' => $adult,
'price_children' => $price_children,
'children' => $children,
'pos' => $draft_item->pos,
'in_pdf' => $draft_item->in_pdf,
'comfort' => $data['comfort']
];
}else{
//create new from draft items
BookingDraftItem::create([
'booking_id' => $data['booking_id'],
'travel_program_id' => $data['travel_program_id'],
'travel_class_id' => null,
'draft_item_id' => $draft_item->id,
'draft_type_id' => $draft_item->draft_type->id,
'request_date' => $data['request_date'],
'days_start' => $draft_item->days_start,
'days_duration' => $draft_item->days_duration,
'start_date' => $start_date_temp ? $start_date_temp->format("Y-m-d") : null,
'end_date' => $end_date_temp ? $end_date_temp->format("Y-m-d") : null,
'service' => $service,
'price_adult' => $price_adult,
'adult' => $adult,
'price_children' => $price_children,
'children' => $children,
'pos' => $draft_item->pos,
'in_pdf' => $draft_item->in_pdf,
'comfort' => $data['comfort']
]);
}
$last_post = $draft_item->pos;
}
//set room prices
if(count($price_info_rooms)){
foreach ($price_info_rooms as $price_info_room){
$last_post ++;
$price_info_room['pos'] = $last_post;
BookingDraftItem::create($price_info_room);
}
}
//set extra charge price
if(count($price_info_extra_charge)){
foreach ($price_info_extra_charge as $price_info_extra){
$last_post ++;
$price_info_extra['pos'] = $last_post;
BookingDraftItem::create($price_info_extra);
}
}
//set class options prices
if(count($price_info_class_options)){
foreach ($price_info_class_options as $price_info_class_option){
$last_post ++;
$price_info_class_option['pos'] = $last_post;
BookingDraftItem::create($price_info_class_option);
}
}
//set travel options prices
if(count($price_info_travel_options)){
foreach ($price_info_travel_options as $price_info_travel_option){
$last_post ++;
$price_info_travel_option['pos'] = $last_post;
BookingDraftItem::create($price_info_travel_option);
}
}
//set travel options prices
if(count($info_texts)){
foreach ($info_texts as $info_text){
$last_post ++;
$info_text['pos'] = $last_post;
BookingDraftItem::create($info_text);
}
}
}
$ret = [];
/*
$travel_program_id = request('travel_program_id');
$comfort = request('comfort');
$booking_id = request('booking_id');
$startDateStr = request('startDateStr');
$endDateStr = request('endDateStr');
$departure = request('departure');
$departure_extra_charge = request('departure_extra_charge');
$traveler = request('traveler');
$title = request('title');
$number = request('number');
$rooms = request('rooms'); // //["rooms"]=> array(1) { [0]=> object(stdClass)#7284 (5) { ["name"]=> string(12) "Einzelzimmer" ["price_adult"]=> float(1682.16) ["adults"]=> int(1) ["children"]=> int(0) ["price_children"]=> int(0) }
$class_options = request('class_options'); //["travel_options"]=> array(2) { [0]=> array(5) { ["name"]=> string(20) "Aufpreis Halbpension" ["price_adult"]=> float(75) ["adult"]=> int(5) ["children"]=> int(0) ["price_children"]=> int(0) }
$travel_options = request('travel_options'); //["class_options"]=> array(1) { [0]=> array(3) { ["name"]=> string(39) "zugebuchte Leistung: Komfort (4 Sterne)" ["price"]=> float(152) ["count"]=> int(1)
*/
return response()->json(['success' => $draft->id], $this->successStatus);
}
}
}
protected function replaceService($service){
$service = str_replace('#Name#', $this->data['title'], $service);
$service = str_replace('#Nummer#', $this->data['number'], $service);
$service = str_replace('#Zimmer#', $this->room_str, $service);
$service = str_replace('#Raumname#', $this->room_name, $service);
$service = str_replace('#Kategorie#', $this->className, $service);
$service = str_replace('#Option#', $this->option, $service);
$service = str_replace('#Flughafen#', $this->data['departure'], $service);
return $service;
}
protected function getDraftByTravelProgramData($travel_program, $data, $weekday){
if ($travel_program && count($travel_program->travel_program_drafts)) {
foreach ($travel_program->travel_program_drafts as $travel_program_draft) {
//this need an realation to travel class by booking and price
if(in_array($weekday, $travel_program_draft->weekdays)){
if($data['comfort'] == false && strpos(strtolower($travel_program_draft->travel_class->name), 'standard') !== false){
return $travel_program_draft->draft;
}
if($data['comfort'] == true && strpos(strtolower($travel_program_draft->travel_class->name), 'komfort') !== false){
return $travel_program_draft->draft;
}
}
}
}
return null;
}
}
/*
array(14) {
["travel_program_id"]=> int(197) ["comfort"]=> bool(true) ["booking_id"]=> string(4) "8435" ["request_date"]=> string(10) "2018-10-29" ["startDateStr"]=> string(10) "2018-11-03" ["endDateStr"]=> string(10) "2018-11-10" ["departure"]=> string(7) "Hamburg" ["departure_extra_charge"]=> float(55) ["traveler"]=> int(3) ["title"]=> string(35) "Höhepunkte Jordaniens ab Nov. 2018" ["number"]=> string(16) "JOR-HOE1920018B1" ["rooms"]=> array(2) {
[0]=> array(5) {
["name"]=> string(12) "Doppelzimmer" ["price_adult"]=> float(1473.85) ["adults"]=> int(2) ["children"]=> int(0) ["price_children"]=> int(0) } [1]=> array(5) {
["name"]=> string(12) "Einzelzimmer" ["price_adult"]=> float(1682.16) ["adults"]=> int(1) ["children"]=> int(0) ["price_children"]=> int(0) } } ["class_options"]=> array(2) {
[0]=> array(3) {
["name"]=> string(39) "zugebuchte Leistung: Komfort (4 Sterne)" ["price"]=> float(122) ["count"]=> int(2) } [1]=> array(3) {
["name"]=> string(39) "zugebuchte Leistung: Komfort (4 Sterne)" ["price"]=> float(171) ["count"]=> int(1) } } ["travel_options"]=> array(1) {
[0]=> array(5) {
["name"]=> string(20) "Aufpreis Halbpension" ["price_adult"]=> float(75) ["adults"]=> int(3) ["children"]=> int(0) ["price_children"]=> int(0) } } }
*/

View file

@ -0,0 +1,37 @@
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Models\BookingDraftItem;
use App\Models\Draft;
use App\Models\TravelCountry;
use App\Models\TravelProgram;
use Carbon\Carbon;
use HTMLHelper;
class LoaderController extends Controller
{
public $successStatus = 200;
public function load($action)
{
if ($action == "travel_program") {
$program = TravelProgram::where('status', 1)->get()->pluck('title', 'id');
return response()->json(['success' => $program], $this->successStatus);
}
if ($action == "travel_country") {
$program = TravelCountry::all()->pluck('name', 'id');;
return response()->json(['success' => $program], $this->successStatus);
}
$ret = [];
return response()->json(['success' => $ret], $this->successStatus);
}
}

View file

@ -0,0 +1,186 @@
<?php
namespace App\Http\Controllers;
use App\Models\Booking;
use App\Models\BookingDraftItem;
use App\Repositories\BookingRepository;
use Input;
class BookingController extends Controller
{
protected $bookingRepo;
public function __construct(BookingRepository $bookingRepo)
{
$this->middleware('admin');
$this->bookingRepo = $bookingRepo;
}
public function index($step = false)
{
$data = [
//'bookings' => Booking::all()->sortByDesc("id"),
'step' => $step
];
return view('booking.index', $data);
}
public function detail($id)
{
if($id == "new") {
$booking = new Booking();
$id = 'new';
}else{
$booking = Booking::findOrFail($id);
$id = $booking->id;
}
$data = [
'booking' => $booking,
'id' => $id,
];
return view('booking.detail', $data);
}
public function store($id)
{
// \Session()->flash('alert-save', '1');
$data = Input::all();
if($id == "new") {
$booking = new Booking();
}else{
$booking = Booking::findOrFail($id);
}
$booking->merlin_order_number = $data['merlin_order_number'];
$booking->save();
$i = 1;
if($data['action'] == 'addItemUp'){
$travel_program_id = null;
$request_date = null;
$comfort = 0;
if(count($booking->booking_draft_items)){
$first_booking_draft_item = $booking->booking_draft_items()->first();
$travel_program_id = $first_booking_draft_item->travel_program_id;
$request_date = $first_booking_draft_item->request_date;
$comfort = $first_booking_draft_item->comfort;
}
$booking->booking_draft_items()->create([
'booking_id' => $booking->id,
'travel_program_id' => $travel_program_id,
'travel_class_id' => null,
'draft_item_id' => null,
'draft_type_id' => null,
'request_date' => $request_date,
'days_start' => null,
'days_duration' => null,
'start_date' => null,
'end_date' => null,
'service' => '',
'price_adult' => null,
'adult' => null ,
'price_children' => 0,
'children' => 0,
'pos' => $i,
'in_pdf' => true,
'comfort' => $comfort
]);
$i++;
}
if(isset($data['draft_item'])){
foreach ($data['draft_item'] as $booking_draft_item_id => $draft_item){
$di = BookingDraftItem::findOrFail($booking_draft_item_id);
$di->draft_type_id = $draft_item['draft_type_id'];
$di->start_date = $draft_item['start_date'];
$di->end_date = $draft_item['end_date'];
$di->service = $draft_item['service'];
$di->in_pdf = isset($draft_item['in_pdf']) ? true : false;
$di->pos = $i++;
$di->save();
}
}
if($data['action'] == 'addItemDown'){
$travel_program_id = null;
$request_date = null;
$comfort = 0;
if(count($booking->booking_draft_items)){
$first_booking_draft_item = $booking->booking_draft_items()->first();
$travel_program_id = $first_booking_draft_item->travel_program_id;
$request_date = $first_booking_draft_item->request_date;
$comfort = $first_booking_draft_item->comfort;
}
$booking->booking_draft_items()->create([
'booking_id' => $booking->id,
'travel_program_id' => $travel_program_id,
'travel_class_id' => null,
'draft_item_id' => null,
'draft_type_id' => null,
'request_date' => $request_date,
'days_start' => null,
'days_duration' => null,
'start_date' => null,
'end_date' => null,
'service' => '',
'price_adult' => null,
'adult' => null ,
'price_children' => 0,
'children' => 0,
'pos' => $i,
'in_pdf' => true,
'comfort' => $comfort
]);
}
if(strpos($data['action'], 'up_') !== false) {
$reId = intval(str_replace('up_', '', $data['action']));
$d_from = BookingDraftItem::findOrFail($reId);
$d_to = $booking->findBeforeDraftItemRelation($reId);
if($d_to) {
$t_pos = $d_from->pos;
$d_from->pos = $d_to->pos;
$d_to->pos = $t_pos;
$d_from->save();
$d_to->save();
}
}
if(strpos($data['action'], 'down_') !== false) {
$reId = intval(str_replace('down_', '', $data['action']));
$d_from = BookingDraftItem::findOrFail($reId);
$d_to = $booking->findAfterDraftItemRelation($reId);
if($d_to) {
$t_pos = $d_from->pos;
$d_from->pos = $d_to->pos;
$d_to->pos = $t_pos;
$d_from->save();
$d_to->save();
}
}
\Session()->flash('alert-save', '1');
return redirect(route('booking_detail', [$booking->id]));
}
public function draftItemDelete($id){
$boking_draft_item = BookingDraftItem::findOrFail($id);
$booking_id = $boking_draft_item->booking_id;
$boking_draft_item->delete();
\Session()->flash('alert-success', 'Eintrag gelöscht');
return redirect(route('booking_detail', [$booking_id]));
}
}

View file

@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Models\Booking;
use App\Services\HTMLHelper;
use DataTables;
use App\User;
@ -68,6 +69,26 @@ class DataTableController extends Controller
->make(true);
}
public function getBookings()
{
//confirmation_code_remider is delete 2
$query = Booking::orderBy("id", "DESC");
return \DataTables::eloquent($query)
->addColumn('action_edit', function (Booking $booking) {
return '<a href="' . route('booking_detail', [$booking->id]) . '" class="btn icon-btn btn-sm btn-primary"><span class="far fa-edit"></span></a>';
})
->addColumn('id', function (Booking $booking) {
return '<a href="' . route('booking_detail', [$booking->id]) . '" data-id="'.$booking->id.'">'.$booking->id.'</a>';
})
->rawColumns(['action_edit', 'id'])
->make(true);
}
}

View file

@ -151,6 +151,8 @@ class DraftController extends Controller
$draft_type = DraftType::find($data['id']);
$draft_type->name = $data['name'];
$draft_type->active = isset($data['active']) ? true : false;
$draft_type->color = $data['color'];
$draft_type->save();
}

View file

@ -66,7 +66,7 @@ class HomeController extends Controller
abort(404);
}
public function checkLogin($identify, $token)
public function checkLogin($identify, $token, $show = false)
{
if($identify){
//user find by $identify
@ -96,14 +96,27 @@ class HomeController extends Controller
$sf_guard_user->token = null;
$sf_guard_user->token_at = null;
$sf_guard_user->save();
if(!Auth::check()){
$user->last_login = now();
$user->save();
Auth::login($user);
}
if(Auth::check()){
return redirect(route('drafts'));
if(!$show){
return redirect(route('home'));
}
if($show == 'drafts'){
return redirect(route('drafts'));
}
if(strpos($show, 'drafts_edit_booking_') !== false){
$booking_id = str_replace('drafts_edit_booking_', '', $show);
return redirect(route('booking_detail', [$booking_id]));
}
}
}
return abort(404);
}

View file

@ -33,7 +33,7 @@ class TranslationController extends Controller
*/
public function index()
{
return redirect('admin/translate/edit/de');
return redirect('admin/translate/all/edit/de');
}
@ -79,7 +79,7 @@ class TranslationController extends Controller
$jsonData = json_encode($ret, TRUE);
file_put_contents($file, $jsonData);
return redirect()
->route('admin_translate_edit', [$language])
->route('admin_translate_all_edit', [$language])
->with('message', 'Translation added successfully');
}

View file

@ -0,0 +1,288 @@
<?php
//use Input;
//use Request;
namespace App\Http\Controllers;
use App;
use File;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Collection;
use App\Requests\TranslationRequest;
class TranslationFileController extends Controller
{
/**
* Translator
*
* @var \Illuminate\Translation\Translator
*/
protected $translator;
/**
* Translation loader
*
* @var \Illuminate\Translation\LoaderInterface
*/
protected $loader;
/**
* @var \League\Flysystem\Adapter\Local
*/
protected $filesystem;
/**
* @var string
*/
protected $languagesPath;
protected $languageRead;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->directory_separator = DIRECTORY_SEPARATOR;
$this->translator = App::make('translator');
$this->loader = Lang::getLoader();
$this->languagesPath = App::langPath();
$this->directory_separator = DIRECTORY_SEPARATOR;
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$language = App::getLocale();
$langsource = 'de';
$this->languageRead = $language;
$langs = array_keys(config('localization.supportedLocales'));
$files = $this->files();
$translations = null;
$edit = false;
$show = 'all';
return view('translation.index_file', compact('files', 'translations', 'language', 'langsource', 'langs', 'edit', 'show'));
//return view('admin.transitions', $data);
}
/**
* Display edit form page
*
* @param string $language
* @param string $file
* @param string|null $namespace
*
* @return \Illuminate\Http\Response
*/
public function edit($file, $language = 'en', $langsource = 'de', $show = 'all')
{
$this->languageRead = $language;
$langs = array_keys(config('localization.supportedLocales'));
$files = $this->files();
$translations = $this->translations($file, $langsource);
$prefix = $this->groupName($file);
$langsource = $langsource;
$edit = $file;
$show = $show;
return view('translation.index_file', compact('files', 'language', 'langsource', 'file', 'translations', 'prefix', 'langs', 'edit', 'show'));
}
/**
* Save translation file
*
* @param \GeniusTS\TranslationManager\Requests\TranslationRequest $request
* @param string $language
* @param string $file
*
* @return \Illuminate\Http\Response
*/
public function update(TranslationRequest $request, $file, $language, $langsource, $show)
{
$keys = array_keys($this->translations($file));
$this->exportFile($request->only($keys), $file, $language);
return redirect()
->route('admin_translate_file_edit', [$file, $language, $langsource, $show])
->with('message', 'Translation added successfully');
}
/**
* Save a translation file
*
* @param array $translation
* @param $filename
* @param $language
*
* @return bool
*/
public function exportFile($translation, $filename, $language)
{
$path = "{$this->languagesPath}{$this->directory_separator}{$language}{$this->directory_separator}{$filename}.php";
$this->backup($path, $language, $filename);
$content = "<?php \n\n return " . var_export($translation, true) . ";";
return (bool) file_put_contents($path, $content);
//return (bool) $this->filesystem->write($path, $content, new Config);
}
/**
* Backup the existing translation files
*/
private function backup($path, $language, $filename)
{
if(!File::exists($path)){
return;
}
if (!File::exists(storage_path('language/'.time().'/'.$language))) {
File::makeDirectory(storage_path('language/'.time().'/'.$language), 0755, true);
}
return File::copy($path, storage_path('language/'.time().'/'.$language.'/'.$filename.'.php'));
}
/**
* Get the translation of a group and name space
*
* @param string $file
* @param string|null $namespace
* @param string|null $language
*
* @return array
*/
public function translations($file, $language = null)
{
$group = $this->groupName($file);
$key = $group;
return $this->translator->trans($key, [], $language ?: $this->defaultLanguage());
}
public function files($lang = false)
{
$path = $this->namespacePath($this->languagesPath, $lang);
$content = $this->pathContent($path);
return $content
->map(function ($file) use ($path) {
$path = ltrim($path . DIRECTORY_SEPARATOR, '/');
//read file empty entries
$count = $this->countEmptyEntries(Str::replaceLast($path, '', $file));
//var_dump($translations);
return array(ltrim($this->groupName(Str::replaceLast($path, '', $file)), '/') => ltrim($this->groupName(Str::replaceLast($path, '', $file)), '/')." (".$count.")");
})
->flatten();
}
public function countEmptyEntries($file){
$translation = $this->translations($file);
$group = $this->groupName($file);
$entries = 0;
$count = 0;
foreach ($translation as $key => $value)
{
$this->searchForEmpty($key, $value, null, $count, $entries, $group);
}
return $entries."/".$count;
}
protected function searchForEmpty($key, $value, $prefix, &$count, &$entries, $group)
{
$prefix = $prefix ? "{$prefix}.{$key}" : $group.".".$key;
if (is_array($value))
{
foreach ($value as $subKey => $subValue)
{
$this->searchForEmpty($subKey, $subValue, $prefix, $count,$entries, $group);
}
}
else
{
if(Lang::has($prefix, $this->languageRead, false)){
$count++;
}
if(Lang::has($prefix, 'de', false)){
$entries ++;
}
}
}
/**
* Get default language
*
* @return string
*/
public function defaultLanguage()
{
return config('app.fallback_locale', 'de');
}
/**
* Get the group name from a filename
*
* @param $filename
*
* @return mixed
*/
public function groupName($filename)
{
return preg_replace('/\.php$/', '', $filename);
}
/**
* Get default language
*
* @param string $path
* @param string $language
*
* @return string
*/
protected function namespacePath($path, $language = null)
{
return "{$path}{$this->directory_separator}" . ($language ?: $this->defaultLanguage());
}
/**
* List content of a path
*
* @param null $path
* @param bool $recursive
*
* @return \Illuminate\Support\Collection
*/
protected function pathContent($path = null, $recursive = false)
{
//var_dump($this->filesystem->listContents($path, $recursive));
//return new Collection(($this->filesystem->listContents($path, $recursive)));
return new Collection(File::files($path));
}
}