Skip to content

Commit

Permalink
2028 - Working with tags, categories, and handling multi value fields…
Browse files Browse the repository at this point in the history
… correctly for regular non-custom fields
  • Loading branch information
rowasc committed Sep 11, 2017
1 parent 6dac97c commit 18c5902
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 46 deletions.
66 changes: 36 additions & 30 deletions application/classes/Ushahidi/Formatter/Post/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ protected function generateCSVRecords($records)

foreach ($records as $record)
{
unset($record['attributes']);

// Transform post_date to a string
if ($record['post_date'] instanceof \DateTimeInterface) {
$record['post_date'] = $record['post_date']->format("Y-m-d H:i:s");
Expand All @@ -66,29 +64,26 @@ protected function generateCSVRecords($records)
$setValue = '';
$keySet = explode('.', $key); //contains key + index of the key, if any
$headingKey = $keySet[0];
if (isset($record[$headingKey]) && $headingKey !== 'values'){
$setValue = $record[$headingKey];
} else if (isset($record['values'][$headingKey])) {
if (count($keySet) > 1){
if($keySet[1] === 'lat' || $keySet[1] === 'lon'){
/*
* Lat/Lon are never multivalue fields so we can get the first index only
*/
$setValue = isset($record['values'][$headingKey][0][$keySet[1]])? ($record['values'][$headingKey][0][$keySet[1]]): '';
} else {
/**
* we work with multiple posts which means our actual count($record[$key])
* value might not exist in all of the posts we are posting in the CSV
*/
$setValue = isset($record['values'][$headingKey][$keySet[1]])? ($record['values'][$headingKey][$keySet[1]]): '';
}
} else{
$setValue = $record['values'][$headingKey];
$recordVal = isset ($record['attributes']) && isset($record['attributes'][$headingKey])? $record['values']: $record;
if (count($keySet) > 1){
if($keySet[1] === 'lat' || $keySet[1] === 'lon'){
/*
* Lat/Lon are never multivalue fields so we can get the first index only
*/
$setValue = isset($recordVal[$headingKey][0][$keySet[1]])? ($recordVal[$headingKey][0][$keySet[1]]): '';
} else {
/**
* we work with multiple posts which means our actual count($record[$key])
* value might not exist in all of the posts we are posting in the CSV
*/
$setValue = isset($recordVal[$headingKey][$keySet[1]])? ($recordVal[$headingKey][$keySet[1]]): '';
}
} else{
if (is_array($record[$headingKey]) && empty($record[$headingKey])) {
$record[$headingKey] = '';
}
} else {
$setValue = '';
$setValue = isset($record[$headingKey]) ? $record[$headingKey] : '';
}
$setValue = is_array($setValue) ? json_encode($setValue) : $setValue;
$values[] = $setValue;
}
fputcsv($fp, $values);
Expand All @@ -112,9 +107,16 @@ private function createSortedHeading($fields){
/**
* Separate by fields that have custom priority and fields that do not have custom priority assigned
*/
foreach ($fields as $fieldKey => $fieldAttr){
if (!is_array($fieldAttr)){
foreach ($fields as $fieldKey => $fieldAttr) {
if (!is_array($fieldAttr)) {
$headingResult[$fieldKey] = $fieldAttr;
} else if (is_array($fieldAttr) && isset($fieldAttr['nativeField'])){
if ($fieldAttr['count'] === 0) {
$headingResult[$fieldKey] = $fieldAttr['label'];
}
for ($i = 0 ; $i < $fieldAttr['count']; $i++){
$headingResult[$fieldKey.'.'.$i] = $fieldAttr['label'].'.'.$i;
}
} else {
$fieldsWithPriorityValue[$fieldKey] = $fieldAttr;
}
Expand Down Expand Up @@ -164,10 +166,10 @@ private function sortGroupedFieldsByPriority($groupedFields){
* If the attribute has a count key, it means we want to show that as key.index in the header.
* This is to make sure we don't miss values in multi-value fields
*/
for ($i = 0 ; $i<$attribute['count']; $i++){
for ($i = 0 ; $i < $attribute['count']; $i++){
$attributeKeysWithStageFlat[$attributeKey.'.'.$i] = $attribute['label'].'.'.$i;
}
} else if ($attribute['type'] === 'point'){
} else if (isset($attribute['type']) && $attribute['type'] === 'point'){
$attributeKeysWithStageFlat[$attributeKey.'.lat'] = $attribute['label'].'.lat';
$attributeKeysWithStageFlat[$attributeKey.'.lon'] = $attribute['label'].'.lon';
}
Expand All @@ -194,20 +196,24 @@ private function groupFieldsByStage($fields) {


/**
* @DEVNOTE : think about possibility of dropping the reference based param. It's way too easy to mess things up with that ref
* @param $columns by reference .
* @param $key
* @param $label
* @param $value
*/
private function assignColumnHeading(&$columns, $key, $labelObject, $value)
{
$prevColumnValue = isset($columns[$key]) ? $columns[$key]: ['count' => 0];
/**
* If $value is an array, then that might mean it has multiple values.
* We want to count the values to make sure we use the right key format and can return all results in the CSV
*/
if (is_array($value) && is_array($labelObject)){
$labelObject['count'] = count($value);
if (is_array($value)){
$headingCount = $prevColumnValue['count'] < count($value)? count($value) : $prevColumnValue['count'] ;
if (!is_array($labelObject)){
$labelObject = ['label' => $labelObject, 'count' => $headingCount, 'type' => null, 'nativeField' => true ];
}
$labelObject['count'] = $headingCount;
}
$columns[$key] = $labelObject;
}
Expand Down
22 changes: 6 additions & 16 deletions application/classes/Ushahidi/Repository/Post/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,15 @@ public function retrieveColumnNameData($data) {
* @DEVNOTE form_stage_id can be NULL. Why is that? solve that scenario and get back here.
*/
$attributes[$key] = ['label' => $attribute->label, 'priority'=> $attribute->priority, 'stage' => $attribute->form_stage_id, 'type'=> $attribute->type];
/**
* @DEVNOTE what happens when we export tags ? check/debug that scenario a bit more
*/
// Set attribute names
if ($attribute->type === 'tags') {
$data['values'][$key] = $this->retrieveTagNames($val);
}
}
/**
* @DEVNOTE Why are we doing this in this specific way? 100% easier if we just go for $a[x]
*/

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

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

// Set Set names
if (!empty($data['sets'])) {
$data['sets'] = $this->retrieveSetNames($data['sets']);
Expand All @@ -57,13 +53,7 @@ public function retrieveColumnNameData($data) {
$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) {
Expand Down

0 comments on commit 18c5902

Please sign in to comment.