diff --git a/application/classes/Ushahidi/Formatter/Post/CSV.php b/application/classes/Ushahidi/Formatter/Post/CSV.php index d137c9c13c..50cbeefa1d 100644 --- a/application/classes/Ushahidi/Formatter/Post/CSV.php +++ b/application/classes/Ushahidi/Formatter/Post/CSV.php @@ -184,7 +184,8 @@ private function writeStreamToFS($stream) */ private function getValueFromRecord($record, $keyParam, $attributes){ // assume it's empty since we go through this for all attributes which might not be available - $return = ''; + $return = ''; + $should_return = false; // the $keyParam is the key=>label we get in createSortedHeading (keyLabel.index) $keySet = explode('.', $keyParam); //contains key + index of the key $headingKey = isset($keySet[0]) ? $keySet[0] : null; // the heading type (sets, contact, title) @@ -194,41 +195,60 @@ private function getValueFromRecord($record, $keyParam, $attributes){ // Ignore attributes that are not related to this Post by Form Id // Ensure that native attributes identified via id 0 are included - if (is_array($recordAttributes) && isset($recordAttributes['form_id']) && isset($record['form_id']) && $recordAttributes['form_id'] != 0 && ($record['form_id'] != $recordAttributes['form_id'])) { - return ''; + if (is_array($recordAttributes) && isset($recordAttributes['form_id']) && isset($record['form_id']) + && $recordAttributes['form_id'] != 0 && ($record['form_id'] != $recordAttributes['form_id'])) { + $should_return = true; } // If the returned attribute for the given heading key is the native form name attribute // Retrieve Form Name from the attribute rather than from the Post until the data model improves - if (is_array($recordAttributes) && isset($recordAttributes['type']) && $recordAttributes['type'] === 'form_name') { - return is_array($record) && isset($record['form_name']) ? $record['form_name'] : ''; - } + if (is_array($recordAttributes) && isset($recordAttributes['type']) + && $recordAttributes['type'] === 'form_name') { + $return = is_array($record) && isset($record['form_name']) ? $record['form_name'] : 'Unstructured'; + } + + // Check if we are dealing with a structured post but not a structured attribute + if (is_array($recordAttributes) && isset($recordAttributes['unstructured']) + && $recordAttributes['unstructured'] && isset($record['form_id'])) { + $should_return = true; + } + + // Check if we're dealing with an unstructured post but a structured attribute + if (!isset($record['form_id']) + && isset($recordAttributes['form_id']) && $recordAttributes['form_id'] != 0) { + $should_return = true; + } + + if ($should_return) { + return $return; + } // default format we will return. See $csvFieldFormat for a list of available formats $format = 'single_raw'; // if we have an attribute and can find a format for it in $csvFieldFormat, reset the $format - if (is_array($recordAttributes) && isset($recordAttributes['type']) && isset(self::$csvFieldFormat[$recordAttributes['type']])){ + if (is_array($recordAttributes) && isset($recordAttributes['type']) + && isset(self::$csvFieldFormat[$recordAttributes['type']])) { $format = self::$csvFieldFormat[$recordAttributes['type']]; } - /** check if the value is in [values] (user added attributes), - ** otherwise it'll be part of the record itself - **/ - $isInValuesArray = isset ($record['values']) && isset($record['values'][$headingKey]); /** * Remap Title and Description type attributes as these are a special case of attributes * since their labels are stored as attributes but their values are stored as fields on the record :/ * The Key UUID will not match the equivalent field on the Post so we must change to use the correct field names */ - if (is_array($recordAttributes) && isset($recordAttributes['type']) && ($recordAttributes['type'] === 'title' || $recordAttributes['type'] === 'description')) { + if (is_array($recordAttributes) && isset($recordAttributes['type']) + && ($recordAttributes['type'] === 'title' || $recordAttributes['type'] === 'description')) { // Description must be mapped to content // Title is title $headingKey = $recordAttributes['type'] === 'title' ? 'title' : 'content'; } - - $recordValue = $isInValuesArray ? $record['values']: $record; + + /** check if the value is in [values] (user added attributes), + ** otherwise it'll be part of the record itself + **/ + $recordValue = isset($record['values']) && isset($record['values'][$headingKey]) ? $record['values'] : $record; // handle values that are dates to have consistent formatting $isDateField = $recordAttributes['input'] === 'date' && $recordAttributes['type'] === 'datetime'; @@ -297,8 +317,7 @@ private function createSortedHeading($fields){ /** * sort each field by priority inside the stage */ - $headingResult = $this->sortGroupedFieldsByPriority($fields); - return $headingResult; + return $this->sortGroupedFieldsByPriority($fields); } /** @@ -359,8 +378,7 @@ private function sortGroupedFieldsByPriority($fields){ */ protected function isLocation($value) { - return is_array($value) && - array_key_exists('lon', $value) && + return is_array($value) && array_key_exists('lon', $value) && array_key_exists('lat', $value); } diff --git a/application/classes/Ushahidi/Repository/Form/Attribute.php b/application/classes/Ushahidi/Repository/Form/Attribute.php index 1e98e9f6ff..63356b9dc4 100644 --- a/application/classes/Ushahidi/Repository/Form/Attribute.php +++ b/application/classes/Ushahidi/Repository/Form/Attribute.php @@ -358,17 +358,18 @@ public function getByForms($form_ids, array $include_attributes = null) 'form_stage_id' => 0, 'form_stage_priority' => 0, 'priority' => 8 - ], + ], [ - 'label' => 'Sets', - 'key' => 'sets', - 'type' => 'sets', + 'label' => 'Unstructured Description', + 'key' => 'description', + 'type' => 'description', 'input' => 'text', 'form_id' => 0, 'form_stage_id' => 0, - 'form_stage_priority' => 0, + 'form_stage_priority' => 0, + 'unstructured' => true, 'priority' => 9 - ] + ] ]; return array_merge($native, $attributes);