Skip to content
This repository was archived by the owner on Oct 29, 2020. It is now read-only.

Commit d345b17

Browse files
committed
creates convertDate helper function and adds field_mobile_app_date to api response for a campaign
1 parent 0d37657 commit d345b17

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

lib/modules/dosomething/dosomething_api/includes/Transformer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ protected function transformCollection($items, $method = 'transform') {
227227
* - timing: (array)
228228
* - services: (array)
229229
* - reportback_info: (array)
230+
* - mobile_app: (array)
230231
* @return array
231232
*/
232233
protected function transformCampaign($data) {
@@ -281,6 +282,8 @@ protected function transformCampaign($data) {
281282
$output['timing']['low_season'] = $data->timing['low_season'];
282283

283284
$output['services'] = $data->services;
285+
286+
$output['mobile_app']['dates'] = $data->mobile_app['dates'];
284287
}
285288

286289
$output['reportback_info'] = $data->reportback_info;

lib/modules/dosomething/dosomething_campaign/includes/Campaign.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Campaign {
2626
public $timing;
2727
public $reportback_info;
2828
public $services;
29-
29+
public $mobile_app;
3030

3131
/**
3232
* Convenience method to retrieve a single campaign from supplied id.
@@ -144,6 +144,9 @@ private function build($data, $display = 'teaser') {
144144
$this->timing = $timing;
145145

146146
$this->services = $this->getServices();
147+
148+
$mobile_app = $this->getMobileAppDate();
149+
$this->mobile_app = $mobile_app;
147150
}
148151

149152
$this->reportback_info = $this->getReportbackInfo();
@@ -363,6 +366,25 @@ protected function getMobileCommonsData() {
363366
}
364367

365368

369+
/**
370+
* Get the start and end dates for when campaign will be displayed on the mobile app if available.
371+
* Dates formatted as ISO-8601 datetime.
372+
*
373+
* @return array
374+
*/
375+
376+
protected function getMobileAppDate() {
377+
$timezone = new DateTimeZone('UTC');
378+
379+
$timing = [];
380+
$timing['dates'] = (dosomething_helpers_extract_field_data($this->node->field_mobile_app_date));
381+
382+
dosomething_helpers_convert_date($timing);
383+
384+
return $timing;
385+
}
386+
387+
366388
/**
367389
* Get Reportback content info used in the campaign.
368390
*
@@ -531,14 +553,7 @@ protected function getTiming() {
531553
$timing['high_season'] = dosomething_helpers_extract_field_data($this->node->field_high_season);
532554
$timing['low_season'] = dosomething_helpers_extract_field_data($this->node->field_low_season);
533555

534-
foreach ($timing as $season => $dates) {
535-
if ($timing[$season]) {
536-
foreach ($timing[$season] as $key => $date) {
537-
$date = new DateTime($date, $timezone);
538-
$timing[$season][$key] = $date->format(DateTime::ISO8601);
539-
}
540-
}
541-
}
556+
dosomething_helpers_convert_date($timing);
542557

543558
return $timing;
544559
}

lib/modules/dosomething/dosomething_helpers/dosomething_helpers.module

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ function dosomething_helpers_menu() {
4949
return $items;
5050
}
5151

52+
/**
53+
* Utility function to convert dates to ISO-8601 datetime.
54+
*/
55+
function dosomething_helpers_convert_date($timing) {
56+
foreach ($timing as $season => $dates) {
57+
if ($timing[$season]) {
58+
foreach ($timing[$season] as $key => $date) {
59+
$date = new DateTime($date, $timezone);
60+
$timing[$season][$key] = $date->format(DateTime::ISO8601);
61+
}
62+
}
63+
}
64+
}
65+
5266
/**
5367
* Utility function to delete automated test data.
5468
*/

0 commit comments

Comments
 (0)