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

Commit f742d69

Browse files
committed
Merge pull request #4754 from chloealee/date-fields
creates convertDate helper function and adds field_mobile_app_date to…
2 parents da96255 + 40f2835 commit f742d69

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
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: 23 additions & 5 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,8 @@ private function build($data, $display = 'teaser') {
144144
$this->timing = $timing;
145145

146146
$this->services = $this->getServices();
147+
148+
$this->mobile_app = $this->getMobileAppDate();
147149
}
148150

149151
$this->reportback_info = $this->getReportbackInfo();
@@ -363,6 +365,25 @@ protected function getMobileCommonsData() {
363365
}
364366

365367

368+
/**
369+
* Get the start and end dates for when campaign will be displayed on the mobile app if available.
370+
* Dates formatted as ISO-8601 datetime.
371+
*
372+
* @return array
373+
*/
374+
375+
protected function getMobileAppDate() {
376+
$timing = [];
377+
$timing['dates'] = (dosomething_helpers_extract_field_data($this->node->field_mobile_app_date));
378+
379+
foreach ($timing['dates'] as $key => $date) {
380+
$timing['dates'][$key] = dosomething_helpers_convert_date($date);
381+
}
382+
383+
return $timing;
384+
}
385+
386+
366387
/**
367388
* Get Reportback content info used in the campaign.
368389
*
@@ -525,17 +546,14 @@ protected function getTimeCommitment() {
525546
* @return array
526547
*/
527548
protected function getTiming() {
528-
$timezone = new DateTimeZone('UTC');
529-
530549
$timing = [];
531550
$timing['high_season'] = dosomething_helpers_extract_field_data($this->node->field_high_season);
532551
$timing['low_season'] = dosomething_helpers_extract_field_data($this->node->field_low_season);
533552

534553
foreach ($timing as $season => $dates) {
535554
if ($timing[$season]) {
536555
foreach ($timing[$season] as $key => $date) {
537-
$date = new DateTime($date, $timezone);
538-
$timing[$season][$key] = $date->format(DateTime::ISO8601);
556+
$timing[$season][$key] = dosomething_helpers_convert_date($date);
539557
}
540558
}
541559
}

lib/modules/dosomething/dosomething_helpers/dosomething_helpers.module

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

52+
/**
53+
* Utility function to convert dates, defaults to ISO-8601 datetime.
54+
*/
55+
function dosomething_helpers_convert_date($date, $format = 'ISO-8601') {
56+
$timezone = new DateTimeZone('UTC');
57+
$datetime = new DateTime($date, $timezone);
58+
59+
if ($format === 'ISO-8601') {
60+
return $datetime->format(DateTime::ISO8601);
61+
}
62+
}
63+
5264
/**
5365
* Utility function to delete automated test data.
5466
*/

0 commit comments

Comments
 (0)