Skip to content

Commit

Permalink
Merge pull request #2015 from ushahidi/1975-fixes-csv-export
Browse files Browse the repository at this point in the history
1975 Returning Entity names as well as ids in CSV
  • Loading branch information
rjmackay authored Aug 22, 2017
2 parents 1b616bb + e96ecef commit 1ac9c07
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
67 changes: 64 additions & 3 deletions application/classes/Ushahidi/Repository/Post/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,74 @@

class Ushahidi_Repository_Post_Export extends Ushahidi_Repository_Post
{
public function getFormAttributes($values) {

public function retrieveColumnNameData($data) {

// Set attribute keys
$attributes = [];
foreach ($values as $key => $val)
foreach ($data['values'] as $key => $val)
{
$attribute = $this->form_attribute_repo->getByKey($key);
$attributes[$key] = $attribute->label;

// Set attribute names
if ($attribute->type === 'tags') {
$data['values'][$key] = $this->retrieveTagNames($val);
}
}

$data += ['attributes' => $attributes];

// Set Set names
if (!empty($data['sets'])) {
$data['sets'] = $this->retrieveSetNames($data['sets']);
}

// Set Completed Stage names
if(!empty($data['completed_stages'])) {
$data['completed_stages'] = $this->retrieveCompletedStageNames($data['completed_stages']);
}

// Set Form name
if (!empty($data['form_id'])) {
$form = $this->form_repo->get($data['form_id']);
$data['form_name'] = $form->name;
}

if (!empty($data['tags'])) {
$data['tags'] = $this->retrieveTagNames($data['tags']);
}

return $data;

}

public function retrieveTagNames($tag_ids) {
$tag_repo = service('repository.tag');
$names = [];
foreach($tag_ids as $tag_id) {
$tag = $tag_repo->get($tag_id);
array_push($names, $tag->tag);
}
return $names;
}

public function retrieveSetNames($set_ids) {
$set_repo = service('repository.set');
$names = [];
foreach($set_ids as $set_id) {
$set = $set_repo->get($set_id);
array_push($names, $set->name);
}
return $names;
}

public function retrieveCompletedStageNames($stage_ids) {
$names = [];
foreach($stage_ids as $stage_id) {
$stage = $this->form_stage_repo->get($stage_id);
array_push($names, $stage->label);
}
return $attributes;
return $names;
}
}
1 change: 0 additions & 1 deletion application/classes/Ushahidi/Repository/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public function update(Entity $entity)
return $count;
}


// UpdatePostTagRepository
public function getByTag($tag)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Usecase/Post/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function interact()

// Retrieved Attribute Labels for Entity's values
$data = $entity->asArray();
$data += ['attributes' => $this->repo->getFormAttributes($data['values'])];
$data = $this->repo->retrieveColumnNameData($data);

$results[$idx] = $data;
}
Expand Down

0 comments on commit 1ac9c07

Please sign in to comment.