DateArgumentWrapper.php in Calendar 8
File
src/DateArgumentWrapper.php
View source
<?php
namespace Drupal\calendar;
use Drupal\views\Plugin\views\argument\Date;
class DateArgumentWrapper {
protected $dateArg;
protected $minDate;
protected $maxDate;
protected $position;
public function getPosition() {
return $this->position;
}
public function setPosition($position) {
$this->position = $position;
}
public function getDateArg() {
return $this->dateArg;
}
public function __construct(Date $dateArg) {
$this->dateArg = $dateArg;
}
public function getArgFormat() {
$class = get_class($this->dateArg);
if (stripos($class, 'YearMonthDate') !== FALSE) {
return 'Ym';
}
if (stripos($class, 'FullDate') !== FALSE) {
return 'Ymd';
}
if (stripos($class, 'YearDate') !== FALSE) {
return 'Y';
}
if (stripos($class, 'YearWeekDate') !== FALSE) {
return 'oW';
}
}
public function createDateTime() {
if ($value = $this->dateArg
->getValue()) {
if (!$this
->validateValue()) {
return FALSE;
}
return $this
->createFromFormat($value);
}
return NULL;
}
protected function createFromFormat($value) {
$format = $this
->getArgFormat();
if ($format == 'oW') {
$date = new \DateTime();
$year = (int) substr($value, 0, 4);
$month = (int) substr($value, 4, 2);
$date
->setISODate($year, $month);
}
else {
$format = '!' . $this
->getArgFormat();
$date = \DateTime::createFromFormat($format, $value);
}
return $date;
}
public function format($format) {
if ($date = $this
->createDateTime()) {
return $date
->format($format);
}
return NULL;
}
public function getGranularity() {
$plugin_id = $this->dateArg
->getPluginId();
$plugin_granularity = str_replace('datetime_', '', $plugin_id);
$plugin_granularity = str_replace('date_', '', $plugin_granularity);
switch ($plugin_granularity) {
case 'year_month':
return 'month';
break;
case 'full_date':
case 'fulldate':
return 'day';
break;
case 'year':
return 'year';
break;
case 'year_week':
return 'week';
break;
}
}
public function getMinDate() {
if (!$this->minDate) {
$date = $this
->createDateTime();
$granularity = $this
->getGranularity();
if ($granularity == 'month') {
$date
->modify("first day of this month");
}
elseif ($granularity == 'week') {
$date
->modify('this week');
}
elseif ($granularity == 'year') {
$date
->modify("first day of January");
}
$date
->setTime(0, 0, 0);
$this->minDate = $date;
}
return $this->minDate;
}
public function getMaxDate() {
if (!$this->maxDate) {
$date = $this
->createDateTime();
$granularity = $this
->getGranularity();
if ($granularity == 'month') {
$date
->modify("last day of this month");
}
elseif ($granularity == 'week') {
$date
->modify('this week +6 days');
}
elseif ($granularity == 'year') {
$date
->modify("last day of December");
}
$date
->setTime(23, 59, 59);
$this->maxDate = $date;
}
return $this->maxDate;
}
public function validateValue() {
$value = $this->dateArg
->getValue();
if (empty($value)) {
return FALSE;
}
if ($this
->getArgFormat() == 'oW') {
$info = $this
->getYearWeek($value);
$max_week = gmdate("W", strtotime("28 December {$info['year']}"));
return $info['week'] >= 1 && $info['week'] <= $max_week;
}
else {
$created_date = $this
->createFromFormat($value);
return $created_date && $created_date
->format($this
->getArgFormat()) == $value;
}
}
protected function getYearWeek($value) {
if (is_numeric($value) && strlen($value) == 6) {
$return['year'] = (int) substr($value, 0, 4);
$return['week'] = (int) substr($value, 4, 2);
return $return;
}
return FALSE;
}
}