PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/Field/src/Field/PublishDateField.php

http://github.com/QuickAppsCMS/QuickApps-CMS
PHP | 215 lines | 143 code | 22 blank | 50 comment | 9 complexity | d0de29c2d16774d296cab85457603747 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?php
  2. /**
  3. * Licensed under The GPL-3.0 License
  4. * For full copyright and license information, please see the LICENSE.txt
  5. * Redistributions of files must retain the above copyright notice.
  6. *
  7. * @since 2.0.0
  8. * @author Christopher Castro <chris@quickapps.es>
  9. * @link http://www.quickappscms.org
  10. * @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License
  11. */
  12. namespace Field\Field;
  13. use Cake\Routing\Router;
  14. use Cake\Validation\Validator;
  15. use CMS\View\View;
  16. use Field\Handler;
  17. use Field\Model\Entity\Field;
  18. use Field\Model\Entity\FieldInstance;
  19. use Field\Utility\DateToolbox;
  20. /**
  21. * Publish Date Field Handler.
  22. *
  23. * Allows scheduling of contents by making them available only between
  24. * certain dates.
  25. */
  26. class PublishDateField extends Handler
  27. {
  28. /**
  29. * {@inheritDoc}
  30. */
  31. public function info()
  32. {
  33. return [
  34. 'type' => 'text',
  35. 'name' => __d('field', 'Publishing Date'),
  36. 'description' => __d('field', 'Allows scheduling of contents by making them available only between certain dates.'),
  37. 'hidden' => false,
  38. 'maxInstances' => 1,
  39. 'searchable' => false,
  40. ];
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function render(Field $field, View $view)
  46. {
  47. $extra = array_merge([
  48. 'from' => ['string' => null, 'timestamp' => null],
  49. 'to' => ['string' => null, 'timestamp' => null],
  50. ], (array)$field->extra);
  51. $field->set('extra', $extra);
  52. return $view->element('Field.PublishDateField/display', compact('field'));
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function edit(Field $field, View $view)
  58. {
  59. return $view->element('Field.PublishDateField/edit', compact('field'));
  60. }
  61. /**
  62. * {@inheritDoc}
  63. *
  64. * The entity to which this instance is attached to will be removed from
  65. * resulting result set if its publishing date does not match current date, this
  66. * constraint is not applied on the backend section of the site.
  67. */
  68. public function beforeFind(Field $field, array $options, $primary)
  69. {
  70. if ($primary &&
  71. !Router::getRequest()->isAdmin() &&
  72. !empty($field->extra['from']['timestamp']) &&
  73. !empty($field->extra['to']['timestamp'])
  74. ) {
  75. $now = time();
  76. if ($field->extra['from']['timestamp'] > $now ||
  77. $now > $field->extra['to']['timestamp']
  78. ) {
  79. return null; // remove entity from result set
  80. }
  81. }
  82. return true;
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function validate(Field $field, Validator $validator)
  88. {
  89. if ($field->metadata->required) {
  90. $validator->notEmpty($field->name, __d('field', 'You must select a date/time range.'));
  91. }
  92. $validator
  93. ->add($field->name, [
  94. 'validRange' => [
  95. 'rule' => function ($value, $context) {
  96. if (!empty($value['from']['string']) &&
  97. !empty($value['from']['format']) &&
  98. !empty($value['to']['string']) &&
  99. !empty($value['to']['format'])
  100. ) {
  101. $from = DateToolbox::createFromFormat($value['from']['format'], $value['from']['string']);
  102. $to = DateToolbox::createFromFormat($value['to']['format'], $value['to']['string']);
  103. return date_timestamp_get($from) < date_timestamp_get($to);
  104. ;
  105. }
  106. return false;
  107. },
  108. 'message' => __d('field', 'Invalid date/time range, "Start" date must be before "Finish" date.')
  109. ]
  110. ]);
  111. return true;
  112. }
  113. /**
  114. * {@inheritDoc}
  115. */
  116. public function beforeSave(Field $field, $post)
  117. {
  118. $values = [];
  119. $extra = [
  120. 'from' => ['string' => null, 'timestamp' => null],
  121. 'to' => ['string' => null, 'timestamp' => null],
  122. ];
  123. foreach (['from', 'to'] as $type) {
  124. if (!empty($post[$type]['string']) &&
  125. !empty($post[$type]['format'])
  126. ) {
  127. $date = $post[$type]['string'];
  128. $format = $post[$type]['format'];
  129. if ($date = DateToolbox::createFromFormat($format, $date)) {
  130. $extra[$type]['string'] = $post[$type]['string'];
  131. $extra[$type]['timestamp'] = date_timestamp_get($date);
  132. $values[] = $extra[$type]['timestamp'] . ' ' . $post[$type]['string'];
  133. } else {
  134. $typeLabel = $type == 'from' ? __d('field', 'Start') : __d('field', 'Finish');
  135. $field->metadata->entity->errors($field->name, __d('field', 'Invalid date/time range, "{0}" date must match the the pattern: {1}', $typeLabel, $format));
  136. return false;
  137. }
  138. }
  139. }
  140. $field->set('value', implode(' ', $values));
  141. $field->set('extra', $extra);
  142. return true;
  143. }
  144. /**
  145. * {@inheritDoc}
  146. */
  147. public function settings(FieldInstance $instance, View $view)
  148. {
  149. return $view->element('Field.PublishDateField/settings_form', compact('instance'));
  150. }
  151. /**
  152. * {@inheritDoc}
  153. */
  154. public function validateSettings(FieldInstance $instance, array $settings, Validator $validator)
  155. {
  156. $validator
  157. ->allowEmpty('time_format')
  158. ->add('time_format', 'validTimeFormat', [
  159. 'rule' => function ($value, $context) use ($settings) {
  160. if (empty($settings['timepicker'])) {
  161. return true;
  162. }
  163. return DateToolbox::validateTimeFormat($value);
  164. },
  165. 'message' => __d('field', 'Invalid time format.')
  166. ])
  167. ->allowEmpty('format')
  168. ->add('format', 'validDateFormat', [
  169. 'rule' => function ($value, $context) {
  170. return DateToolbox::validateDateFormat($value);
  171. },
  172. 'message' => __d('field', 'Invalid date format.')
  173. ]);
  174. }
  175. /**
  176. * {@inheritDoc}
  177. */
  178. public function viewModeSettings(FieldInstance $instance, View $view, $viewMode)
  179. {
  180. return $view->element('Field.PublishDateField/view_mode_form', compact('instance', 'viewMode'));
  181. }
  182. /**
  183. * {@inheritDoc}
  184. */
  185. public function defaultViewModeSettings(FieldInstance $instance, $viewMode)
  186. {
  187. return [
  188. 'label_visibility' => 'above',
  189. 'shortcodes' => false,
  190. 'hidden' => false,
  191. ];
  192. }
  193. }