Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated the timeloop start hour and gql getters #34

Merged
merged 1 commit into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/fields/TimeloopField.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;

use percipiolondon\timeloop\Timeloop;
use yii\base\BaseObject;
use yii\db\Schema;

Expand Down Expand Up @@ -114,6 +115,14 @@ public function normalizeValue($value, ElementInterface $element = null)
$value['loopEnd'] = DateTimeHelper::toDateTime($value['loopEnd']);
}

if (isset($value['loopStartHour']) ) {
$value['loopStartHour'] = DateTimeHelper::toDateTime($value['loopStartHour']);
}

if (isset($value['loopEndHour']) ) {
$value['loopEndHour'] = DateTimeHelper::toDateTime($value['loopEndHour']);
}

if (isset($value['loopPeriod'])) {
$value['loopPeriod'] = Json::decodeIfJson($value['loopPeriod']);
}
Expand All @@ -138,6 +147,12 @@ public function serializeValue($value, ElementInterface $element = null)
if (isset($value['loopEnd']) && $value['loopEnd'] instanceof \DateTime) {
$value['loopEnd'] = $value['loopEnd']->format(\DateTime::ATOM);
}
if (isset($value['loopStartHour']) && $value['loopStartHour'] instanceof \DateTime) {
$value['loopStartHour'] = $value['loopStartHour']->format(\DateTime::ATOM);
}
if (isset($value['loopEndHour']) && $value['loopEndHour'] instanceof \DateTime) {
$value['loopEndHour'] = $value['loopEndHour']->format(\DateTime::ATOM);
}

if( isset($value['loopReminderPeriod']) && '' === $value['loopReminderPeriod']) {
$value['loopReminderValue'] = 0;
Expand Down Expand Up @@ -235,6 +250,11 @@ public function getContentGqlType()
'name' => 'loopPeriod',
'type' => Type::string(),
'description' => 'The loop repeater period (daily / weekly / monthly / yearly)',
'resolve' => function ($source, array $arguments, $context, ResolveInfo $resolveInfo) {
$fieldName = $resolveInfo->fieldName;
// Craft::dd($source[$fieldName]);
return "TO BE DEFINED WHAT SHOULD BE RETURNED HERE";
}
],
'loopReminder' => [
'name' => 'loopReminder',
Expand All @@ -251,10 +271,30 @@ public function getContentGqlType()
return Gql::applyDirectives($source, $resolveInfo, $value);
}
],
'loopStartHour' => [
'name' => 'loopStartHour',
'type' => DateTime::getType(),
'description' => 'The end date where the loop should stop',
'resolve' => function ($source, array $arguments, $context, ResolveInfo $resolveInfo) {
$fieldName = $resolveInfo->fieldName;
$value = DateTimeHelper::toDateTime($source[$fieldName]);
return $value ? $value->format('g:ia') : false;
}
],
'loopEnd' => [
'name' => 'loopEnd',
'type' => DateTime::getType(),
'description' => 'The end date where the loop should stop'
'description' => 'The start hour of the loop'
],
'loopEndHour' => [
'name' => 'loopEndHour',
'type' => DateTime::getType(),
'description' => 'The end hour of the loop',
'resolve' => function ($source, array $arguments, $context, ResolveInfo $resolveInfo) {
$fieldName = $resolveInfo->fieldName;
$value = DateTimeHelper::toDateTime($source[$fieldName]);
return $value ? $value->format('g:ia') : false;
}
],
'getReminder' => [
'name' => 'getReminder',
Expand Down
8 changes: 4 additions & 4 deletions src/models/TimeloopModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class TimeloopModel extends Model
public $loopEnd;

/**
* @var string
* @var DateTime
*/
public $loopStartHour;

/**
* @var string
* @var DateTime
*/
public $loopEndHour;

Expand Down Expand Up @@ -66,8 +66,8 @@ public function rules()
['loopStart', 'datetime'],
['loopEnd', 'datetime'],
['loopPeriod', 'array'],
['loopStartHour', 'string'],
['loopEndHour', 'string'],
['loopStartHour', 'datetime'],
['loopEndHour', 'datetime'],
['loopReminderValue', 'number'],
];
}
Expand Down
19 changes: 12 additions & 7 deletions src/services/TimeloopService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use DateInterval;
use DateTime;
use DatePeriod;
use percipiolondon\timeloop\models\TimeloopModel;

/**
* @author percipiolondon
Expand Down Expand Up @@ -37,12 +38,12 @@ public function showPeriod(array $data)
/**
* Returns the $limit upcoming dates from the timeloop start date
*
* @param array $data
* @param TimeloopModel $data
* @param bool $futureDates
* @param integer $limit
*
*/
public function getLoop(array $data, $limit = 0, bool $futureDates = true)
public function getLoop(TimeloopModel $data, $limit = 0, bool $futureDates = true)
{
// get start date from data object
// $startUnix = strtotime($data['loopStart']['date']);
Expand All @@ -66,10 +67,15 @@ public function getLoop(array $data, $limit = 0, bool $futureDates = true)
}

// return the array with dates
return $this->_fetchDates($data->loopStart, $end, $repeater, $limit, $futureDates);
return $this->_fetchDates($data->loopStart, $data->loopStartHour, $end, $repeater, $limit, $futureDates);
}

public function getReminder(array $data)
/**
* @param TimeloopModel $data
* @return mixed|null
* @throws \yii\base\Exception
*/
public function getReminder(TimeloopModel $data)
{
$date = $this->getLoop($data, 1);
$loopReminderValue = $data->loopReminderValue ?? 0;
Expand All @@ -92,10 +98,9 @@ public function getReminder(array $data)
/**
* @throws \Exception
*/
private function _fetchDates($start, $end, $period, $limit = 0, $futureDates = true)
private function _fetchDates($start, $startHour, $end, $period, $limit = 0, $futureDates = true)
{

$interval = _calculateInterval($period);
$interval = $this->_calculateInterval($period);

$today = new DateTime();

Expand Down
4 changes: 2 additions & 2 deletions src/templates/fields/timeloop-input.twig
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@
label: 'Start hour',
instructions: 'Provide a start hour if the recurring date has one',
name: name ~ '[loopStartHour]',
value: value.loopStartHour.time ?? ''
value: value.loopStartHour ?? null
}) }}
</div>
<div class="w-1/2 px-8">
{{ forms.timeField({
label: 'End hour',
instructions: 'Provide an end hour if the recurring date has one.',
name: name ~ '[loopEndHour]',
value: value.loopEndHour.time ?? ''
value: value.loopEndHour ?? null
}) }}
</div>
</div>
Expand Down