Skip to content

Commit

Permalink
Przeniesienie flattenCollection do CustomCollection z Helpera
Browse files Browse the repository at this point in the history
  • Loading branch information
adampiotrowski committed Mar 19, 2014
1 parent 1483ea5 commit 6a6f923
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 83 deletions.
33 changes: 0 additions & 33 deletions application/Gekosale/Core/DataGrid/Renderer.php

This file was deleted.

39 changes: 0 additions & 39 deletions application/Gekosale/Core/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,6 @@
*/
class Helper extends Component
{
/**
* Flatten Collection to key-value pairs
*
* @param Collection $collection
* @param $idKey
* @param $translationPath
*
* @return array
*/
public function flattenCollection(Collection $collection, $idKey, $translationPath)
{
$language = $this->getCurrentLanguage();
$select = [];
$translationPath = explode('.', $translationPath);

if (count($translationPath) == 2) {
$translationNode = $translationPath[0];
$translationKey = $translationPath[1];
} else {
$translationNode = null;
$translationKey = $translationPath[0];
}

$collection->each(function ($item) use (&$select, $idKey, $translationNode, $translationKey, $collection, $language) {

if (null === $translationNode) {
$select[$item->$idKey] = $item->$translationKey;
} else {
foreach ($item->$translationNode as $translation) {
if ($translation->language_id === $language) {
$select[$item->$idKey] = $translation->$translationKey;
}
}
}
});

return $select;
}

/**
* Returns current translation from translation Collection
*
Expand Down
13 changes: 13 additions & 0 deletions application/Gekosale/Core/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,17 @@ public function sync(BelongsToMany $relation, $values)
$relation->detach();
}
}

/**
* Gekosale custom Eloquent Collection
*
* @param array $models
*
* @return Model\CustomCollection|Collection
*/
public function newCollection(array $models = Array())
{
return new Model\CustomCollection($models);
}

}
48 changes: 48 additions & 0 deletions application/Gekosale/Core/Model/CustomCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Gekosale\Core\Model;

use Illuminate\Database\Eloquent\Collection;

class CustomCollection extends Collection
{
/**
* Flatten Collection to key-value pairs used in forms
*
* @param $idKey
* @param $translationPath
* @param $language
*
* @return array
*/

public function toSelect($idKey, $translationPath, $language)
{
$items = $this;
$select = [];
$translationPath = explode('.', $translationPath);

if (count($translationPath) == 2) {
$translationNode = $translationPath[0];
$translationKey = $translationPath[1];
} else {
$translationNode = null;
$translationKey = $translationPath[0];
}

$items->each(function ($item) use (&$select, $idKey, $translationNode, $translationKey, $items, $language) {

if (null === $translationNode) {
$select[$item->$idKey] = $item->$translationKey;
} else {
foreach ($item->$translationNode as $translation) {
if ($translation->language_id === $language) {
$select[$item->$idKey] = $translation->$translationKey;
}
}
}
});

return $select;
}
}
9 changes: 2 additions & 7 deletions application/Gekosale/Core/Model/TaxTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,8 @@ class TaxTranslation extends Model
public $timestamps = true;

protected $softDelete = false;

protected $fillable
= array(
'tax_id',
'language_id',
'name'
);

protected $fillable = ['tax_id', 'language_id', 'name'];

public function tax()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ public function getPopulateData($id)
*/
public function getAllDelivererToSelect()
{
return $this->getHelper()->flattenCollection($this->all(), 'id', 'translation.name');
return $this->all()->toSelect('id', 'translation.name', $this->getCurrentLanguage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@ public function getPopulateData($id)
*/
public function getAllProducerToSelect()
{
return $this->getHelper()->flattenCollection($this->all(), 'id', 'translation.name');
return $this->all()->toSelect('id', 'translation.name', $this->getCurrentLanguage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function getPopulateData($id)
*/
public function getAllShopToSelect()
{
return $this->getHelper()->flattenCollection($this->all(), 'id', 'translation.name');
return $this->all()->toSelect('id', 'translation.name', $this->getCurrentLanguage());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ public function getPopulateData($id)
*/
public function getAllTaxToSelect()
{
return $this->getHelper()->flattenCollection($this->all(), 'id', 'translation.name');
return $this->all()->toSelect('id', 'translation.name', $this->getCurrentLanguage());
}
}

0 comments on commit 6a6f923

Please sign in to comment.