Skip to content

Commit 5a3d030

Browse files
committed
Set minimum date allowed to be in field
1 parent 3dc676e commit 5a3d030

5 files changed

Lines changed: 62 additions & 5 deletions

File tree

adm/style/manage_ads.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ <h3>{{ lang('WARNING') }}</h3>
130130
</dl>
131131
<dl>
132132
<dt><label for="ad_start_date">{{ lang('AD_START_DATE') ~ lang('COLON') }}</label><br /><span>{{ lang('AD_START_DATE_EXPLAIN') }}</span></dt>
133-
<dd><input class="phpbb-ads-datepicker" id="ad_start_date" name="ad_start_date" type="date" value="{{ AD_START_DATE ? AD_START_DATE|date(constant('\\phpbb\\ads\\ext::DATE_FORMAT'), 'UTC') }}" size="20" maxlength="20" /></dd>
133+
<dd><input class="phpbb-ads-datepicker" id="ad_start_date" name="ad_start_date" type="date" min="{{ DATE_MINIMUM }}" value="{{ AD_START_DATE ? AD_START_DATE|date(constant('\\phpbb\\ads\\ext::DATE_FORMAT'), 'UTC') }}" size="20" maxlength="20" /></dd>
134134
</dl>
135135
<dl>
136136
<dt><label for="ad_end_date">{{ lang('AD_END_DATE') ~ lang('COLON') }}</label><br /><span>{{ lang('AD_END_DATE_EXPLAIN') }}</span></dt>
137-
<dd><input class="phpbb-ads-datepicker" id="ad_end_date" name="ad_end_date" type="date" value="{{ AD_END_DATE ? AD_END_DATE|date(constant('\\phpbb\\ads\\ext::DATE_FORMAT'), 'UTC') }}" size="20" maxlength="20" /></dd>
137+
<dd><input class="phpbb-ads-datepicker" id="ad_end_date" name="ad_end_date" type="date" min="{{ DATE_MINIMUM }}" value="{{ AD_END_DATE ? AD_END_DATE|date(constant('\\phpbb\\ads\\ext::DATE_FORMAT'), 'UTC') }}" size="20" maxlength="20" /></dd>
138138
</dl>
139139
<dl>
140140
<dt><label for="ad_groups">{{ lang('HIDE_GROUPS') ~ lang('COLON') }}</label><br /><span>{{ lang('HIDE_GROUPS_EXPLAIN') }}</span></dt>

controller/admin_controller.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ protected function action_add()
188188
'U_ACTION' => "{$this->u_action}&amp;action=add",
189189
'U_FIND_USERNAME' => $this->helper->get_find_username_link(),
190190
'U_ENABLE_VISUAL_DEMO' => $this->controller_helper->route('phpbb_ads_visual_demo', array('action' => 'enable')),
191+
'DATE_MINIMUM' => $this->helper->get_date('tomorrow'),
191192
));
192193
}
193194

@@ -224,6 +225,7 @@ protected function action_edit()
224225
'U_ACTION' => "{$this->u_action}&amp;action=edit&amp;id=$ad_id",
225226
'U_FIND_USERNAME' => $this->helper->get_find_username_link(),
226227
'U_ENABLE_VISUAL_DEMO' => $this->controller_helper->route('phpbb_ads_visual_demo', array('action' => 'enable')),
228+
'DATE_MINIMUM' => $this->helper->get_date('tomorrow'),
227229
));
228230
$this->helper->assign_data($this->data, $this->input->get_errors());
229231
}

controller/helper.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace phpbb\ads\controller;
1212

13+
use phpbb\ads\ext;
14+
1315
/**
1416
* Helper
1517
*/
@@ -180,6 +182,17 @@ public function get_find_username_link()
180182
return append_sid("{$this->root_path}memberlist.{$this->php_ext}", 'mode=searchuser&amp;form=acp_admanagement_add&amp;field=ad_owner&amp;select_single=true');
181183
}
182184

185+
/**
186+
* Get a date in the current user's timezone and the correct format.
187+
*
188+
* @param string $time
189+
* @return string Formatted date time
190+
*/
191+
public function get_date($time = 'now')
192+
{
193+
return $this->user->create_datetime($time)->format(ext::DATE_FORMAT);
194+
}
195+
183196
/**
184197
* Is an ad expired?
185198
*

tests/controller/admin_controller_test.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ public function test_action_add_no_submit()
329329
->method('get_find_username_link')
330330
->willReturn('u_find_username');
331331

332+
$this->helper->expects(self::once())
333+
->method('get_date')
334+
->with('tomorrow')
335+
->willReturn('2000-12-16');
336+
332337
$this->template->expects(self::once())
333338
->method('assign_vars')
334339
->with(array(
@@ -337,6 +342,7 @@ public function test_action_add_no_submit()
337342
'U_ACTION' => "{$this->u_action}&amp;action=add",
338343
'U_FIND_USERNAME' => 'u_find_username',
339344
'U_ENABLE_VISUAL_DEMO' => null,
345+
'DATE_MINIMUM' => '2000-12-16',
340346
));
341347

342348
$this->request->expects(self::once())
@@ -679,6 +685,11 @@ public function test_action_edit_no_submit($ad_id)
679685
->method('get_find_username_link')
680686
->willReturn('u_find_username');
681687

688+
$this->helper->expects(self::once())
689+
->method('get_date')
690+
->with('tomorrow')
691+
->willReturn('2000-12-16');
692+
682693
$this->template->expects(self::once())
683694
->method('assign_vars')
684695
->with(array(
@@ -688,6 +699,7 @@ public function test_action_edit_no_submit($ad_id)
688699
'U_ACTION' => "{$this->u_action}&amp;action=edit&amp;id=" . $ad_id,
689700
'U_FIND_USERNAME' => 'u_find_username',
690701
'U_ENABLE_VISUAL_DEMO' => null,
702+
'DATE_MINIMUM' => '2000-12-16',
691703
));
692704

693705
$this->input->expects(self::once())
@@ -743,6 +755,11 @@ public function test_action_edit_preview()
743755
->method('assign_var')
744756
->with('PREVIEW', 'Ad Code #1');
745757

758+
$this->helper->expects(self::once())
759+
->method('get_date')
760+
->with('tomorrow')
761+
->willReturn('2000-12-16');
762+
746763
$this->template->expects(self::once())
747764
->method('assign_vars')
748765
->with(array(
@@ -752,6 +769,7 @@ public function test_action_edit_preview()
752769
'U_ACTION' => "{$this->u_action}&amp;action=edit&amp;id=1",
753770
'U_FIND_USERNAME' => 'u_find_username',
754771
'U_ENABLE_VISUAL_DEMO' => null,
772+
'DATE_MINIMUM' => '2000-12-16',
755773
));
756774

757775
$this->input->expects(self::once())
@@ -850,6 +868,11 @@ public function test_action_edit_submit($s_error, $success, $ad_owner)
850868
->method('get_find_username_link')
851869
->willReturn('u_find_username');
852870

871+
$this->helper->expects(self::once())
872+
->method('get_date')
873+
->with('tomorrow')
874+
->willReturn('2000-12-16');
875+
853876
$this->template->expects(self::once())
854877
->method('assign_vars')
855878
->with(array(
@@ -859,6 +882,7 @@ public function test_action_edit_submit($s_error, $success, $ad_owner)
859882
'U_ACTION' => "{$this->u_action}&amp;action=edit&amp;id=1",
860883
'U_FIND_USERNAME' => 'u_find_username',
861884
'U_ENABLE_VISUAL_DEMO' => null,
885+
'DATE_MINIMUM' =>'2000-12-16',
862886
));
863887

864888
$this->input->expects(self::once())

tests/controller/helper_test.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ protected function setUp(): void
117117
*/
118118
public function get_helper()
119119
{
120-
$helper = new \phpbb\ads\controller\helper(
120+
return new \phpbb\ads\controller\helper(
121121
$this->user,
122122
$this->user_loader,
123123
$this->language,
@@ -129,8 +129,6 @@ public function get_helper()
129129
$this->root_path,
130130
$this->php_ext
131131
);
132-
133-
return $helper;
134132
}
135133

136134
/**
@@ -396,6 +394,26 @@ public function test_get_find_username_link()
396394
self::assertEquals("{$this->root_path}memberlist.{$this->php_ext}?mode=searchuser&amp;form=acp_admanagement_add&amp;field=ad_owner&amp;select_single=true", $result);
397395
}
398396

397+
/**
398+
* Test get_date()
399+
*/
400+
public function test_get_date()
401+
{
402+
$helper = $this->get_helper();
403+
404+
// Test 'now' returns today's date in Y-m-d format
405+
$result = $helper->get_date('now');
406+
$this->assertEquals(date('Y-m-d'), $result);
407+
408+
// Test 'today' returns today's date
409+
$result = $helper->get_date('today');
410+
$this->assertEquals(date('Y-m-d'), $result);
411+
412+
// Test 'tomorrow' returns tomorrow's date
413+
$result = $helper->get_date('tomorrow');
414+
$this->assertEquals(date('Y-m-d', strtotime('+1 day')), $result);
415+
}
416+
399417
/**
400418
* Data for test_is_expired
401419
*

0 commit comments

Comments
 (0)