-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the ability to add trackfields by trackfield code over multiple…
… tracks
- Loading branch information
1 parent
04e974b
commit b4a800f
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
classes/Gems/Tracker/Model/AddTrackFieldsByCodeTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,72 @@ | |||
<?php | |||
|
|||
namespace Gems\Tracker\Model; | |||
|
|||
class AddTrackFieldsByCodeTransformer extends \MUtil_Model_ModelTransformerAbstract | |||
{ | |||
|
|||
|
|||
protected $includeCodes; | |||
|
|||
/** | |||
* @var \Gems_Tracker_TrackerInterface | |||
*/ | |||
protected $tracker; | |||
|
|||
protected $trackFieldsByRespondentTrack; | |||
|
|||
public function __construct(\Gems_Tracker_TrackerInterface $tracker, array $includeCodes) | |||
{ | |||
$this->includeCodes = $includeCodes; | |||
$this->tracker = $tracker; | |||
} | |||
|
|||
/** | |||
* The transform function performs the actual transformation of the data and is called after | |||
* the loading of the data in the source model. | |||
* | |||
* @param \MUtil_Model_ModelAbstract $model The parent model | |||
* @param array $data Nested array | |||
* @param boolean $new True when loading a new item | |||
* @param boolean $isPostData With post data, unselected multiOptions values are not set so should be added | |||
* @return array Nested array containing (optionally) transformed data | |||
*/ | |||
public function transformLoad(\MUtil_Model_ModelAbstract $model, array $data, $new = false, $isPostData = false) | |||
{ | |||
foreach($data as $tokenId=>$row) { | |||
if (isset($row['gto_id_respondent_track'])) { | |||
if (!isset($this->trackFieldsByRespondentTrack[$row['gto_id_respondent_track']])) { | |||
$trackData = array_filter($row, function($key) { | |||
return strpos($key, 'gtr_') === 0; | |||
}, ARRAY_FILTER_USE_KEY); | |||
$this->trackFieldsByRespondentTrack[$row['gto_id_respondent_track']] = $this->getTrackFields($trackData, $row['gto_id_respondent_track']); | |||
} | |||
$newData = $this->trackFieldsByRespondentTrack[$row['gto_id_respondent_track']]; | |||
$data[$tokenId] = array_merge($data[$tokenId], $newData); | |||
} | |||
|
|||
} | |||
|
|||
return $data; | |||
} | |||
|
|||
protected function getTrackfields(array $trackData, $respondentTrackId) | |||
{ | |||
$engine = $this->tracker->getTrackEngine($trackData); | |||
|
|||
$fieldCodes = $engine->getFieldCodes(); | |||
$filteredFieldCodes = array_intersect($fieldCodes, $this->includeCodes); | |||
|
|||
if (empty($filteredFieldCodes)) { | |||
return []; | |||
} | |||
$fieldData = $engine->getFieldsData($respondentTrackId); | |||
|
|||
$filteredFieldData = []; | |||
foreach($filteredFieldCodes as $fieldId=>$fieldCode) { | |||
$filteredFieldData[$fieldCode] = $fieldData[$fieldId]; | |||
} | |||
|
|||
return $filteredFieldData; | |||
} | |||
} |
b4a800f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice new feature! Please don't forget to create an issue for this so we can link it to the next release and don't forget it for testing and documentation updates.