HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/vebon.komma.pro/app/KommaApp/Translations/Kms/TranslationService.php
<?php

/**
 * Short description for the file.
 *
 * @author      Tim Van Samang <timvansamang@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace KommaApp\Translations\Kms;

use App\Helpers\KommaHelpers;
use Barryvdh\TranslationManager\Models\Translation;
use Illuminate\Support\Facades\Artisan;
use KommaApp\Kms\Core\Kms;

class TranslationService
{
    private $kms;

    private $resourcePath;

    public function __construct(Kms $kms)
    {
        $this->kms = $kms;
        $this->resourceTranslationPath = base_path() . '/resources/lang/';
    }

    public function getTranslationsBy($key, $value, $operator = '=')
    {
        return Translation::where($key, $operator, $value)->get()->keyBy('id');
    }

    public function getTranslationsByGroupsName($groupName)
    {
        return $this->getTranslationsBy('group', $groupName);

    }

    public function exportTranslationToFile($group)
    {
        //Check if the file exist, and create if necessary
        $this->createTranslationFile($group);

        Artisan::call('translations:export', ['group' => $group]);

    }

    /**
     * This creates the needed translation files
     * for the given group
     *
     * @param $group
     */
    private function createTranslationFile($group)
    {
        /**
         * Loop trough all the available languages
         */
        foreach ($this->kms->getAvailableLanguages() as $language) {

            /**
             * Create the path based on:
             * - The resources location
             * - The language iso_2
             * - The group (and add .php)
             */
            $filePath = $this->resourceTranslationPath . $language->iso_2 . '/' . $group . '.php';

            //Create the file if needed
            KommaHelpers::createFile($filePath);
        }
    }


    /**
     * Remove the given translations based on a collection
     * Afterwords remove the file name based on the group
     *
     * @param $translations
     * @param $group
     */
    public function removeTranslations($translations)
    {
        //Remove the translations from the database
        foreach ($translations as $translation) {

            //Remove the translationFile based on the group
            $this->removeTranslationsFile($translation->group);

            //Delete the translation from db
            $translation->delete();
        }

    }

    /**
     * Remove the translationFile from the disc
     * based on the group
     *
     * @param $group
     */
    private function removeTranslationsFile($group)
    {
        //add .php to the group
        $folderAndFile = $group . '.php';

        // Loop trough all the available languages
        foreach ($this->kms->getAvailableLanguages() as $language) {
            //Create the full file path
            $fullPath = $filePath = $this->resourceTranslationPath . $language->iso_2 . '/' . $folderAndFile;

            //Remove the file from the disc
            KommaHelpers::removeFile($fullPath);

        }

    }


    public function cleanOldTranslations($group)
    {
        //GEt translations based on the groupsName
        $translations = $this->getTranslationsByGroupsName($group);
        //Filter the items with status 0
        $translations = $translations->where('status', 0);
        //Delete these form the database
        foreach ($translations as $translation) {
            $translation->delete();
        }

        //Clean the translations
        Artisan::call('translations:clean');
    }


}