PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/attributes/date_time/controller.php

http://github.com/concrete5/concrete5
PHP | 414 lines | 348 code | 42 blank | 24 comment | 56 complexity | 6919ffcb911f9ff546a86311bff4979d MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. namespace Concrete\Attribute\DateTime;
  3. use Concrete\Core\Attribute\Controller as AttributeTypeController;
  4. use Concrete\Core\Attribute\FontAwesomeIconFormatter;
  5. use Concrete\Core\Attribute\SimpleTextExportableAttributeInterface;
  6. use Concrete\Core\Entity\Attribute\Key\Settings\DateTimeSettings;
  7. use Concrete\Core\Entity\Attribute\Value\Value\DateTimeValue;
  8. use Concrete\Core\Error\ErrorList\ErrorList;
  9. use DateTime;
  10. use Exception;
  11. class Controller extends AttributeTypeController implements SimpleTextExportableAttributeInterface
  12. {
  13. public $helpers = ['form', 'date', 'form/date_time'];
  14. protected $searchIndexFieldDefinition = ['type' => 'datetime', 'options' => ['notnull' => false]];
  15. protected $akUseNowIfEmpty = null;
  16. protected $akDateDisplayMode = null;
  17. protected $akTextCustomFormat = null;
  18. protected $akTimeResolution = null;
  19. public function getIconFormatter()
  20. {
  21. return new FontAwesomeIconFormatter('clock-o');
  22. }
  23. public function saveKey($data)
  24. {
  25. if (!is_array($data)) {
  26. $data = [];
  27. }
  28. $data += [
  29. 'akUseNowIfEmpty' => false,
  30. ];
  31. $type = $this->getAttributeKeySettings();
  32. $type->setUseNowIfEmpty($data['akUseNowIfEmpty']);
  33. $type->setMode($data['akDateDisplayMode']);
  34. $type->setTextCustomFormat(isset($data['akTextCustomFormat']) ? $data['akTextCustomFormat'] : '');
  35. if (isset($data['akTimeResolution'])) {
  36. $type->setTimeResolution($data['akTimeResolution']);
  37. }
  38. return $type;
  39. }
  40. public function type_form()
  41. {
  42. $this->load();
  43. }
  44. public function getSearchIndexValue()
  45. {
  46. $datetime = $this->getDateTime();
  47. return ($datetime === null) ? null : $datetime->format('Y-m-d H:i:s');
  48. }
  49. public function searchForm($list)
  50. {
  51. $dateFrom = $this->request('from');
  52. $dateTo = $this->request('to');
  53. if ($dateFrom) {
  54. $dateFrom = date('Y-m-d', strtotime($dateFrom));
  55. $list->filterByAttribute($this->attributeKey->getAttributeKeyHandle(), $dateFrom, '>=');
  56. }
  57. if ($dateTo) {
  58. $dateTo = date('Y-m-d', strtotime($dateTo));
  59. $list->filterByAttribute($this->attributeKey->getAttributeKeyHandle(), $dateTo, '<=');
  60. }
  61. return $list;
  62. }
  63. public function form()
  64. {
  65. $this->load();
  66. $datetime = $this->getDateTime();
  67. if ($datetime === null && $this->akUseNowIfEmpty) {
  68. $datetime = new DateTime();
  69. }
  70. $this->set('value', $datetime);
  71. $this->set('displayMode', $this->akDateDisplayMode);
  72. $this->set('textCustomFormat', $this->akTextCustomFormat);
  73. $this->set('timeResolution', $this->akTimeResolution);
  74. }
  75. public function exportKey($akey)
  76. {
  77. $this->load();
  78. $type = $akey->addChild('type');
  79. $type->addAttribute('use-now-if-empty', $this->akUseNowIfEmpty ? 1 : 0);
  80. $type->addAttribute('mode', $this->akDateDisplayMode);
  81. $type->addAttribute('text-custom-format', $this->akTextCustomFormat);
  82. $type->addAttribute('time-resolution', $this->akTimeResolution);
  83. return $akey;
  84. }
  85. public function importKey(\SimpleXMLElement $akey)
  86. {
  87. $type = $this->getAttributeKeySettings();
  88. if (isset($akey->type)) {
  89. $type->setUseNowIfEmpty($akey->type['use-now-if-empty']);
  90. $type->setMode($akey->type['mode']);
  91. $type->setTextCustomFormat(isset($akey->type['text-custom-format']) ? $akey->type['text-custom-format'] : '');
  92. $type->setTimeResolution($akey->type['time-resolution']);
  93. }
  94. return $type;
  95. }
  96. public function validateValue()
  97. {
  98. $v = $this->getAttributeValue()->getValue();
  99. return $v != false;
  100. }
  101. public function validateForm($data)
  102. {
  103. if ($this->akDateDisplayMode === null) {
  104. $this->load();
  105. }
  106. switch ($this->akDateDisplayMode) {
  107. case 'date_time':
  108. if (empty($data['value_dt']) || (!is_numeric($data['value_h'])) || (!is_numeric($data['value_m']))) {
  109. return false;
  110. }
  111. $dh = $this->app->make('helper/date'); /* @var $dh \Concrete\Core\Localization\Service\Date */
  112. switch ($dh->getTimeFormat()) {
  113. case 12:
  114. if (empty($data['value_a'])) {
  115. return false;
  116. }
  117. break;
  118. }
  119. return true;
  120. default:
  121. return $data['value'] != '';
  122. }
  123. }
  124. public function search()
  125. {
  126. $dt = $this->app->make('helper/form/date_time');
  127. $html = $dt->date($this->field('from'), $this->request('from'), true);
  128. $html .= ' ' . t('to') . ' ';
  129. $html .= $dt->date($this->field('to'), $this->request('to'), true);
  130. echo $html;
  131. }
  132. public function getAttributeValueClass()
  133. {
  134. return DateTimeValue::class;
  135. }
  136. public function exportValue(\SimpleXMLElement $akv)
  137. {
  138. $value = null;
  139. if (isset($this->attributeValue)) {
  140. $object = $this->attributeValue->getValueObject();
  141. if ($object) {
  142. $datetime = $object->getValue();
  143. if ($datetime) {
  144. $value = $datetime->format('Y-m-d H:i:s');
  145. }
  146. }
  147. }
  148. $akv->addChild('value', $value);
  149. }
  150. public function createAttributeValue($value)
  151. {
  152. if ($value) {
  153. if (!($value instanceof DateTime)) {
  154. $timestamp = strtotime($value);
  155. $value = new DateTime(date('Y-m-d H:i:s', $timestamp));
  156. }
  157. } else {
  158. $value = null;
  159. }
  160. $av = new DateTimeValue();
  161. $av->setValue($value);
  162. return $av;
  163. }
  164. public function createAttributeValueFromRequest()
  165. {
  166. $this->load();
  167. $data = $this->post();
  168. $datetime = null;
  169. $dh = $this->app->make('helper/date');
  170. switch ($this->akDateDisplayMode) {
  171. case 'text':
  172. case 'date_text':
  173. if (isset($data['value']) && is_string($data['value']) && $data['value'] !== '') {
  174. if ($this->akTextCustomFormat !== '') {
  175. $format = $this->akTextCustomFormat;
  176. } elseif ($this->akDateDisplayMode === 'date_text') {
  177. $format = $dh->getPHPDatePattern();
  178. } else {
  179. $format = $dh->getPHPDateTimePattern();
  180. }
  181. try {
  182. $parsed = DateTime::createFromFormat(
  183. $format,
  184. $data['value'],
  185. $dh->getTimezone('user')
  186. );
  187. if ($parsed) {
  188. if ($this->akDateDisplayMode !== 'date_text') {
  189. $parsed->setTimezone($dh->getTimezone('system'));
  190. }
  191. $datetime = $parsed;
  192. }
  193. } catch (Exception $x) {
  194. }
  195. }
  196. break;
  197. case 'date':
  198. case 'date_time':
  199. default:
  200. $dt = $this->app->make('helper/form/date_time');
  201. /* @var \Concrete\Core\Form\Service\Widget\DateTime $dt */
  202. $datetime = $dt->translate('value', $data, true);
  203. break;
  204. }
  205. return $this->createAttributeValue($datetime);
  206. }
  207. public function getAttributeKeySettingsClass()
  208. {
  209. return DateTimeSettings::class;
  210. }
  211. public function getPlainTextValue()
  212. {
  213. $datetime = $this->getDateTime();
  214. if ($datetime !== null) {
  215. $dh = $this->app->make('helper/date');
  216. return $dh->formatCustom(\DateTime::ATOM, $datetime);
  217. }
  218. return '';
  219. }
  220. /**
  221. * {@inheritdoc}
  222. *
  223. * @see AttributeTypeController::getDisplayValue()
  224. */
  225. public function getDisplayValue()
  226. {
  227. $result = '';
  228. $datetime = $this->getDateTime();
  229. if ($datetime !== null) {
  230. if ($this->akDateDisplayMode === null) {
  231. $this->load();
  232. }
  233. $dh = $this->app->make('helper/date');
  234. /* @var \Concrete\Core\Localization\Service\Date $dh */
  235. switch ($this->akDateDisplayMode) {
  236. case 'date':
  237. $result = $dh->formatDate($datetime, 'short', $datetime->getTimezone());
  238. break;
  239. case 'date_text':
  240. if ($this->akTextCustomFormat === '') {
  241. $result = $dh->formatDate($datetime, 'short', $datetime->getTimezone());
  242. } else {
  243. $result = $dh->formatCustom($this->akTextCustomFormat, $datetime, $datetime->getTimezone());
  244. }
  245. break;
  246. case 'text':
  247. if ($this->akTextCustomFormat === '') {
  248. $result = $dh->formatDateTime($datetime);
  249. } else {
  250. $result = $dh->formatCustom($this->akTextCustomFormat, $datetime);
  251. }
  252. break;
  253. case 'date_time':
  254. default:
  255. $result = $dh->formatDateTime($datetime);
  256. break;
  257. }
  258. }
  259. return $result;
  260. }
  261. /**
  262. * {@inheritdoc}
  263. *
  264. * @see \Concrete\Core\Attribute\SimpleTextExportableAttributeInterface::getAttributeValueTextRepresentation()
  265. */
  266. public function getAttributeValueTextRepresentation()
  267. {
  268. $dateTime = $this->getDateTime();
  269. if ($dateTime === null) {
  270. $result = '';
  271. } else {
  272. if (!isset($this->akDateDisplayMode)) {
  273. $this->load();
  274. }
  275. switch ($this->akDateDisplayMode) {
  276. case 'date':
  277. case 'date_text':
  278. $result = $dateTime->format('Y-m-d');
  279. break;
  280. case 'date_time':
  281. case 'text':
  282. default:
  283. // Let's convert the date/time from the system timezone to the website default timezone
  284. $toTimezone = $this->app->make('date')->getTimezone('app');
  285. $dateTime = clone $dateTime;
  286. $dateTime->setTimezone($toTimezone);
  287. $result = $dateTime->format('Y-m-d H:i:s');
  288. break;
  289. }
  290. }
  291. return $result;
  292. }
  293. /**
  294. * {@inheritdoc}
  295. *
  296. * @see \Concrete\Core\Attribute\SimpleTextExportableAttributeInterface::updateAttributeValueFromTextRepresentation()
  297. */
  298. public function updateAttributeValueFromTextRepresentation($textRepresentation, ErrorList $warnings)
  299. {
  300. $value = $this->getAttributeValueObject();
  301. if ($textRepresentation === '') {
  302. if ($value !== null) {
  303. $value->setValue(null);
  304. }
  305. } else {
  306. if (!isset($this->akDateDisplayMode)) {
  307. $this->load();
  308. }
  309. switch ($this->akDateDisplayMode) {
  310. case 'date':
  311. case 'date_text':
  312. $dateTime = @DateTime::createFromFormat('Y-m-d', $textRepresentation);
  313. break;
  314. case 'date_time':
  315. case 'text':
  316. default:
  317. $dateTime = @DateTime::createFromFormat('Y-m-d H:i:s', $textRepresentation);
  318. if ($dateTime) {
  319. $toTimezone = $this->app->make('date')->getTimezone('system');
  320. $dateTime->setTimezone($toTimezone);
  321. }
  322. break;
  323. }
  324. if (!$dateTime) {
  325. $warnings->add(t('"%1$s" is not a valid date value for the attribute with handle %2$s', $textRepresentation, $this->attributeKey->getAttributeKeyHandle()));
  326. } else {
  327. if ($value === null) {
  328. $value = $this->createAttributeValue($dateTime);
  329. } else {
  330. $value->setValue($dateTime);
  331. }
  332. }
  333. }
  334. return $value;
  335. }
  336. protected function load()
  337. {
  338. $ak = $this->getAttributeKey();
  339. if (!is_object($ak)) {
  340. return false;
  341. }
  342. $type = $ak->getAttributeKeySettings();
  343. /* @var DateTimeType $type */
  344. $this->akUseNowIfEmpty = $type->getUseNowIfEmpty();
  345. $this->set('akUseNowIfEmpty', $this->akUseNowIfEmpty);
  346. $this->akDateDisplayMode = (string) $type->getMode();
  347. $this->set('akDateDisplayMode', $this->akDateDisplayMode);
  348. $this->akTextCustomFormat = $type->getTextCustomFormat();
  349. $this->set('akTextCustomFormat', $this->akTextCustomFormat);
  350. $this->akTimeResolution = $type->getTimeResolution();
  351. $this->set('akTimeResolution', $this->akTimeResolution);
  352. }
  353. /**
  354. * Retrieve the date/time value.
  355. *
  356. * @return DateTime|null
  357. */
  358. protected function getDateTime()
  359. {
  360. $result = null;
  361. if ($this->attributeValue) {
  362. $valueObject = $this->getAttributeValue();
  363. if ($valueObject !== null) {
  364. $dateTime = $valueObject->getValue();
  365. if ($dateTime instanceof DateTime) {
  366. $result = $dateTime;
  367. }
  368. }
  369. }
  370. return $result;
  371. }
  372. }