/
DurationSelector.php
99 lines (80 loc) · 3.01 KB
/
DurationSelector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Concrete\Core\Form\Service\Widget;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\Foundation\Repetition\BasicRepetition;
use Concrete\Core\Http\ResponseAssetGroup;
use Concrete\Core\Localization\Service\Date;
use Concrete\Core\User\User;
use Concrete\Core\View\View;
use Punic\Calendar as PunicCalendar;
class DurationSelector
{
protected function getDefaultRepetition($timezone = null)
{
$app = Application::getFacadeApplication();
if (!$timezone) {
if (\Config::get('concrete.misc.user_timezones')) {
$user = $app->make(User::class);
$userInfo = $user->getUserInfoObject();
$timezone = $userInfo->getUserTimezone();
} else {
$site = \Core::make('site')->getSite();
$timezone = $site->getConfigRepository()->get('timezone');
}
}
$service = \Core::make('helper/date');
$now = $service->toDateTime('now', $timezone);
$startDate = $now->format('Y-m-d H:i:s');
$repetition = new BasicRepetition($timezone);
$repetition->setStartDate($startDate);
return $repetition;
}
public function selectDuration($namespace, $repetition = null, $timezone = null, $allowRepeat = true, $allowMultiple = true)
{
$repetitions = array();
$baseRepetition = $this->getDefaultRepetition($timezone);
if (is_array($repetition)) {
$repetitions = $repetition;
} else if (is_object($repetition)) {
$repetitions[] = $repetition;
} else {
$repetitions[] = $baseRepetition;
}
ob_start();
View::element('date_time/duration');
$contents = ob_get_contents();
ob_end_clean();
$identifier = new \Concrete\Core\Utility\Service\Identifier();
$identifier = $identifier->getString(32);
$date = new Date();
$format = $date->getJQueryUIDatePickerFormat();
$args = array(
'dateFormat' => $format,
'repetitions' => $repetitions,
'baseRepetition' => $baseRepetition,
'allowRepeat' => $allowRepeat,
'allowMultiple' => $allowMultiple,
'namespace' => $namespace,
'timeFormat' => PunicCalendar::has12HoursClock() ? 12 : 24,
);
$args = json_encode($args);
$add = t('Add Date/Time');
$html = <<<EOL
{$contents}
<div data-duration-selector-wrapper="{$identifier}">
<div data-duration-selector="{$identifier}"></div>
EOL;
if ($allowMultiple) {
$html .= '<button data-action="add-duration" type="button" class="btn btn-sm btn-secondary float-end">' . $add . '</button>';
}
$html .= <<<EOL
</div>
<script type="text/javascript">
$(function() {
$('[data-duration-selector-wrapper={$identifier}]').concreteDurationSelector({$args});
});
</script>
EOL;
return $html;
}
}