File: D:/HostingSpaces/SBogers10/vebon.komma.pro/app/KommaApp/Translations/Kms/TranslationRepository.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma Mediadesign
*/
namespace KommaApp\Translations\Kms;
use App\Helpers\KommaHelpers;
use Illuminate\Database\Eloquent\Collection;
use KommaApp\Kms\Core\Kms;
use KommaApp\Kms\Core\KmsRepository;
use KommaApp\Kms\Core\Tree\Tree;
use KommaApp\Kms\SidebarListItem;
use Barryvdh\TranslationManager\Models\Translation;
class TranslationRepository extends KmsRepository
{
public $ts;
public function __construct(Kms $kms, TranslationService $translationService)
{
parent::__construct($kms);
$this->ts = $translationService;
}
/**
* This method will get a model based on the id.
* When the id is NULL, we will generate a new.
* This is Called in KmsSection@loadEntity.
*
* @param $group_slug
* @return Translation
*/
public function getModel($group_slug)
{
//If the group_slug is empty, make a newModel
if ($group_slug == null) return $this->newModel();
//update groupSlug by changing the underscores to slashes
$groupSlug = KommaHelpers::UnderScoreToSlash($group_slug);
//Get translations based on the groupSlug
$translations = $this->ts->getTranslationsByGroupsName($groupSlug)->sortBy('key');
//Create an empty groupObject
$group = (object)[];
//Set the group_slug (with underscores) to the id
$group->id = $group_slug;
//a status is needed
$group->status = 1;
//Create an translationCollection based on the models
$group->translations = $this->createTranslationCollection($translations);
//Set the groupSlug (without _) to the group
$group->group = $groupSlug;
//Set the extra form fields, eg. title, thumbnail
$this->setExtraModelFields($group);
return $group;
}
/**
* We build a translation Collection
* based on the key/locale of the $stringTranslations.
*
* @param $stringTranslations
* @return Collection
*/
private function createTranslationCollection($stringTranslations)
{
//First we are going to put the model in a multidimensional array
//With the key as first level and the locale (iso_2 of lang) as seccond
//Create an empty group
$group = [];
//Loop trough the stringTranslations
foreach ($stringTranslations as $stringTranslation) {
//Save in the group with the key as first and the locale as seccond level
$group[$stringTranslation->key][$stringTranslation->locale] = $stringTranslation;
}
//Create an new stringTranslationCollection collection
$stringTranslationCollection = new Collection();
//Loop trough the group, this gives an array of localItems grouped by key
foreach ($group as $key => $localeItems) {
//Create a new translations collection
$translations = new Collection();
//Loop trough the localeItems array, this gives an stringTranslation
foreach ($localeItems as $locale => $stringTranslation) {
//Put the stringTranslation in the translations collection with the locale as key
$translations->put($locale, $stringTranslation);
}
//Create an keyCollection, this is equal to the latest (or first if you want) stringTranslation
$keyCollection = $stringTranslation;
//Create an translations attribute and set the translations collection
$keyCollection->translations = $translations;
//Set the keyCollection tot the stringTranslationCollection based on the key
$stringTranslationCollection->put($key, $keyCollection);
}
//Return the collection
return $stringTranslationCollection;
}
/**
* This method will set extra modelFields.
* eg title and thumbnail for the form.
*
* @param $model
*/
public function setExtraModelFields(&$model, $new = false)
{
//Set the tille
$model->title = ucwords(str_replace('/', ' ', $model->group));
//Explode the parts on /
$parts = explode('/', $model->group);
//Get the last parts
$model->group_name = end($parts);
$model->parent_group = implode('/', array_slice($parts, 0, -1));
//Set the thumbnail
$this->setThumbnail($model);
}
/**
* Set the thumbnail for the model.
*
* @param $model
*/
public function setThumbnail(&$model)
{
//Set the thumbnail
$model->thumbnail = ['image_url' => '/images/kms/structure/translations.png'];
}
/**
* This method will genereate a new Customer.
* It is only used in the creation of the Form.
*
* @param null $siteId
* @return Translation
*/
public function newModel($siteId = null)
{
$translation = new Translation();
//Add an empty collection to translations
$translation->translations = new Collection();
//Set the thumbnail
$this->setThumbnail($translation);
return $translation;
}
/**
* This method will get all the models.
* And add these to the sidebarList.
*
* @return array $sidebarList
*/
public function getModels()
{
$translations = Translation::groupBy('group')
->orderBy('group')
->get();
return $this->makeTranslationTree($translations);
}
/**
* Make a tree for from the translations
* to load in the Kms sidebar.
*
* @param $translations
* @return array
*
*/
public function makeTranslationTree($translations)
{
//Create an empty sidebarList
$sidebarList = [];
//Loop trough then translations
foreach ($translations as $translation) {
//Create new SidebarListItem
$sidebarListItem = new SidebarListItem();
//Set the complete group as id, with slash changed to underscore
$sidebarListItem->setId(KommaHelpers::slashToUnderScore($translation->group));
//Set the group name
$sidebarListItem->setName($translation->group);
//Todo thumbnail for file, and for folder
$sidebarListItem->setThumbnail(\HTML::image('images/kms/structure/file.png'));
//Check if it is a group in a sub folder
if (preg_match('/([a-z0-9]+)\/(.*)/i', $translation->group, $matches)) {
//Check if the folderName does not exist in the sidebarList already
if (!isset($sidebarList[$matches[1]])) {
//Make a list item for a folder
$mainSidebarListItem = new SidebarListItem();
//Set the id to the first file in the subfolder
$mainSidebarListItem->setId(KommaHelpers::slashToUnderScore($translation->group));
//Set the main folder as name
$mainSidebarListItem->setName($matches[1]);
//todo folder thumbnail
$mainSidebarListItem->setThumbnail(\HTML::image('images/kms/structure/folder.png'));
//Add to the sidebar
$sidebarList[$matches[1]] = $mainSidebarListItem;
}
//change the subitem name to the fileName
$sidebarListItem->setName($matches[2]);
//Set the item as child of the folder
$sidebarList[$matches[1]]->childeren[] = $sidebarListItem;
//Continue tot the next item
continue;
}
//Add the item tho the sidebar
$sidebarList[] = $sidebarListItem;
}
return $sidebarList;
}
public function saveModel($input, $translation = null)
{
//Set the groupName
$groupName = \Input::get('group_name');
//Prepend the parent_group, it this is not "null"
if (\Input::has('parent_group') && \Input::get('parent_group') != 'null') $groupName = \Input::get('parent_group') . '/' . $groupName;
$translations = $this->ts->getTranslationsByGroupsName($groupName);
if (!\Input::has('translations_key')) return false;
$keys = \Input::get('translations_key');
foreach ($this->kms->getAvailableLanguages() as $language) {
if (!\Input::has('translations_value_' . $language->id)) continue;
$values = \Input::get('translations_value_' . $language->id);
foreach ($keys as $i => $key) {
//Skip empty keys
if (empty($key)) continue;
//Check for a translation with this key
if (!$translation = $translations->where('key', $key)->where('locale', $language->iso_2)->first()) {
$translation = new Translation();
$translation->key = $key;
$translation->group = $groupName;
$translation->locale = $language->iso_2;
}
if (!isset($values[$i])) continue;
$translation->value = $values[$i];
$translation->status = 1;
$translation->save();
}
}
$this->ts->cleanOldTranslations($groupName);
$this->ts->exportTranslationToFile($groupName);
return KommaHelpers::slashToUnderScore($groupName);
}
private function saveLocaleValues($keys, $values, $locale)
{
}
/**
* Delete the translations based on the group
* from the database and the disc
*
* @param $group_slug
*/
public function destroyModel($group_slug)
{
//Fix the groupSlug
$groupSlug = KommaHelpers::UnderScoreToSlash($group_slug);
//Load all translations
$translations = $this->ts->getTranslationsByGroupsName($groupSlug);
//Remove the translations from the database, and delete the files
$this->ts->removeTranslations($translations);
}
public function getTranslationsForBestFriendSelectBox()
{
$records = Translation::all();
$entities = [];
foreach ($records as $record) {
$entity = [];
$entity['value'] = $record->id;
$entity['content'] = $record->first_name . ' ' . $record->last_name;
$entity['htmlContent'] = $record->first_name . ' ' . $record->last_name . ' <i>(' . $record->email . ')</i>';
$entities[] = $entity;
}
return $entities;
}
/**
* @return array
* Get all the groups for the select
*/
public function getGroupsForSelect()
{
//Get the translations grouped by teh group
$groups = Translation::groupBy('group')->orderBy('group')->get();
//Empty entities
$entities = [];
$entities [] = $this->createEntityForList('null', 'Hoofdgroep', 'Hoofdgroep');
//Start looping trough the items
foreach ($groups as $item) {
//Create an item, or a group of items
$this->CreateItemsWithDepth($entities, $item->group);
}
//Only keep the values (ui-select fault)
$entities = array_values($entities);
return $entities;
}
/**
* Create items with Depth based on the group
*
* @param $entities
* @param $group
*/
private function CreateItemsWithDepth(&$entities, $group)
{
//Reset usedParts
$useParts = [];
//Reset dept to 0
$depth = 0;
//Explode the group by /
$groupParts = explode('/', $group);
//Loop trough the exploded parts
foreach ($groupParts as $part) {
//Add the current part to the used parts
$useParts[] = $part;
//Implode the parts for the groupName
$groupName = implode('/', $useParts);
//Check if there is already is an item with the given groupName
if (isset($entities[$groupName])) {
//Add one to the depth
$depth++;
//Continue to a deeper part
continue;
}
//Create the htmlContent text. repat (three spaces * $depth) and add the current part
$htmlContent = str_repeat(' ', $depth) . ' ' . $part;
//Create an entity for the list
$entities[$groupName] = $this->createEntityForList($groupName, $groupName, $htmlContent);
//Add one to the depth
$depth++;
}
}
/**
* Create an entity for a list
*
* @param $value
* @param $content
* @param $htmlContent
* @return array
*/
private function createEntityForList($value, $content, $htmlContent)
{
$entity = [];
//Value sent with the form
$entity['value'] = $value;
//content shown when item is selected
$entity['content'] = $content;
//Content shown in the drop down list
$entity['htmlContent'] = $htmlContent;
return $entity;
}
}