BxEventsFormEntry.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Events Events
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Create/Edit Group Form.
  13. */
  14. class BxEventsFormEntry extends BxBaseModGroupsFormEntry
  15. {
  16. protected static $_isCssJsEventsAdded = false;
  17. protected $_iContentId = 0;
  18. public function __construct($aInfo, $oTemplate = false)
  19. {
  20. $this->MODULE = 'bx_events';
  21. parent::__construct($aInfo, $oTemplate);
  22. if (isset($this->aInputs['reoccurring'])) {
  23. $this->aInputs['reoccurring']['ghost_template'] = array (
  24. 'params' => array(
  25. 'nested_form_template' => 'form_ghost_template_wrapper.html',
  26. 'db' => array(
  27. 'table' => 'bx_events_intervals',
  28. 'key' => 'interval_id',
  29. 'submit_name' => 'interval_id',
  30. ),
  31. ),
  32. 'inputs' => array (
  33. 'interval_id' => array (
  34. 'type' => 'hidden',
  35. 'name' => 'interval_id[]',
  36. 'value' => '{interval_id}',
  37. 'caption' => _t('Interval ID'),
  38. 'db' => array (
  39. 'pass' => 'Int',
  40. ),
  41. ),
  42. 'repeat_year' => array (
  43. 'type' => 'select',
  44. 'name' => 'repeat_year[]',
  45. 'value' => '{repeat_year}',
  46. 'values' => BxDolForm::getDataItems('bx_events_repeat_year'),
  47. 'caption' => _t('_bx_events_form_input_repeat_year'),
  48. 'db' => array (
  49. 'pass' => 'Int',
  50. ),
  51. ),
  52. 'repeat_month' => array (
  53. 'type' => 'select',
  54. 'name' => 'repeat_month[]',
  55. 'value' => '{repeat_month}',
  56. 'values' => BxDolForm::getDataItems('bx_events_repeat_month'),
  57. 'caption' => _t('_bx_events_form_input_repeat_month'),
  58. 'db' => array (
  59. 'pass' => 'Int',
  60. ),
  61. ),
  62. 'repeat_week_of_month' => array (
  63. 'type' => 'select',
  64. 'name' => 'repeat_week_of_month[]',
  65. 'value' => '{repeat_week_of_month}',
  66. 'values' => BxDolForm::getDataItems('bx_events_repeat_week_of_month'),
  67. 'caption' => _t('_bx_events_form_input_repeat_week_of_month'),
  68. 'db' => array (
  69. 'pass' => 'Int',
  70. ),
  71. ),
  72. 'repeat_day_of_month' => array (
  73. 'type' => 'select',
  74. 'name' => 'repeat_day_of_month[]',
  75. 'value' => '{repeat_day_of_month}',
  76. 'values' => BxDolForm::getDataItems('bx_events_repeat_day_of_month'),
  77. 'caption' => _t('_bx_events_form_input_repeat_day_of_month'),
  78. 'db' => array (
  79. 'pass' => 'Int',
  80. ),
  81. ),
  82. 'repeat_day_of_week' => array (
  83. 'type' => 'select',
  84. 'name' => 'repeat_day_of_week[]',
  85. 'value' => '{repeat_day_of_week}',
  86. 'values' => BxDolForm::getDataItems('bx_events_repeat_day_of_week'),
  87. 'caption' => _t('_bx_events_form_input_repeat_day_of_week'),
  88. 'db' => array (
  89. 'pass' => 'Int',
  90. ),
  91. ),
  92. 'repeat_stop' => array (
  93. 'type' => 'datepicker',
  94. 'name' => 'repeat_stop[]',
  95. 'value' => '{repeat_stop}',
  96. 'caption' => _t('_bx_events_form_input_stop_repeating'),
  97. 'db' => array (
  98. 'pass' => 'DateTs',
  99. ),
  100. ),
  101. ),
  102. );
  103. }
  104. if (isset($this->aInputs['timezone']) && !$this->aParams['view_mode']) {
  105. $this->aInputs['timezone']['values'] = array_combine(timezone_identifiers_list(), timezone_identifiers_list());
  106. }
  107. }
  108. function getCode($bDynamicMode = false)
  109. {
  110. return $this->_oModule->_oTemplate->getJsCode('entry') . parent::getCode($bDynamicMode);
  111. }
  112. function initChecker ($aValues = array (), $aSpecificValues = array())
  113. {
  114. if (isset($aValues[$this->_oModule->_oConfig->CNF['FIELD_ID']]))
  115. $this->_iContentId = $aValues[$this->_oModule->_oConfig->CNF['FIELD_ID']];
  116. parent::initChecker($aValues, $aSpecificValues);
  117. }
  118. protected function genCustomRowDateEnd (&$aInput)
  119. {
  120. if ($this->aParams['view_mode'])
  121. return $this->_isSameDayEvent() ? '' : $this->genViewRowWrapped($aInput);
  122. return $this->genRowStandard($aInput);
  123. }
  124. protected function _isSameDayEvent()
  125. {
  126. $aStartEnd = $this->_getStartEnd();
  127. if($aStartEnd === false || (is_array($aStartEnd) && empty($aStartEnd)))
  128. return false;
  129. else
  130. return $aStartEnd['start']->format('Y-m-d') == $aStartEnd['end']->format('Y-m-d');
  131. }
  132. protected function _getStartEnd()
  133. {
  134. if(!isset($this->aInputs['timezone']['value']) || !isset($this->aInputs['date_start']['value']) || !isset($this->aInputs['date_end']['value']))
  135. return false;
  136. if(empty($this->aInputs['date_start']['value']) && empty($this->aInputs['date_end']['value']))
  137. return array();
  138. $iTimeStart = bx_process_input ($this->aInputs['date_start']['value'], isset($this->aInputs['date_start']['date_filter']) ? $this->aInputs['date_start']['date_filter'] : BX_DATA_DATETIME_TS, false, false);
  139. $iTimeEnd = bx_process_input ($this->aInputs['date_end']['value'], isset($this->aInputs['date_end']['date_filter']) ? $this->aInputs['date_end']['date_filter'] : BX_DATA_DATETIME_TS, false, false);
  140. $oDateStart = date_create(date('Y-m-d H:i:s', $iTimeStart), new DateTimeZone($this->aInputs['timezone']['value'] ? $this->aInputs['timezone']['value'] : 'UTC'));
  141. $oDateEnd = date_create(date('Y-m-d H:i:s', $iTimeEnd), new DateTimeZone($this->aInputs['timezone']['value'] ? $this->aInputs['timezone']['value'] : 'UTC'));
  142. return array('start' => $oDateStart, 'end' => $oDateEnd);
  143. }
  144. protected function genCustomRowTime (&$aInput)
  145. {
  146. if (!$this->aParams['view_mode'])
  147. return '';
  148. $aStartEnd = $this->_getStartEnd();
  149. if ($aStartEnd === false)
  150. $aInput['value'] = "Timezone, date start & date end fields are required to display this field";
  151. else if(is_array($aStartEnd) && empty($aStartEnd))
  152. $aInput['value'] = '';
  153. else {
  154. if ($aStartEnd['start']->format('Y-m-d') == $aStartEnd['end']->format('Y-m-d'))
  155. $aInput['value'] = _t('_bx_events_txt_from_time_to_time', $aStartEnd['start']->format(getParam('bx_events_time_format')), $aStartEnd['end']->format(getParam('bx_events_time_format')));
  156. else
  157. $aInput['value'] = '';
  158. }
  159. return $this->genViewRowWrapped($aInput);
  160. }
  161. function genViewRowValue(&$aInput)
  162. {
  163. if (in_array($aInput['name'], array('date_start', 'date_end'))) {
  164. $aStartEnd = $this->_getStartEnd();
  165. if($aStartEnd === false)
  166. return "Timezone, date start & date end fields are required to display this field";
  167. if(is_array($aStartEnd) && empty($aStartEnd))
  168. return null;
  169. $sFormat = $aStartEnd['start']->format('Y-m-d') == $aStartEnd['end']->format('Y-m-d') ? BX_FORMAT_DATE : BX_FORMAT_DATE_TIME;
  170. $oDate = 'date_start' == $aInput['name'] ? $aStartEnd['start'] : $aStartEnd['end'];
  171. return bx_time_js ($oDate->getTimestamp(), $sFormat, true);
  172. }
  173. return parent::genViewRowValue($aInput);
  174. }
  175. protected function genCustomRowReoccurring (&$aInput)
  176. {
  177. return $this->genRowCustom($aInput, 'genInputReoccurring');
  178. }
  179. protected function genInputReoccurring (&$aInput, $sInfo = '', $sError = '')
  180. {
  181. $sUniqId = genRndPwd (8, false);
  182. return $this->oTemplate->parseHtmlByName('form_field_reoccurring.html', array(
  183. 'info' => $sInfo,
  184. 'error' => $sError,
  185. 'uniq_id' => $sUniqId,
  186. 'js_instance_name' => 'oBxEventsIntervals_' . $sUniqId,
  187. 'options' => json_encode(array(
  188. 'uniq_id' => $sUniqId,
  189. 'js_instance_name' => 'oBxEventsIntervals_' . $sUniqId,
  190. 'template_ghost' => $this->genGhostTemplate($aInput),
  191. 'content_id' => $this->_iContentId,
  192. )),
  193. ));
  194. }
  195. protected function genCustomViewRowValueTimezone ($aInput)
  196. {
  197. return $aInput['value'];
  198. }
  199. public function insert ($aValsToAdd = array(), $isIgnore = false)
  200. {
  201. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  202. if (isset($this->aInputs['reoccurring']) && is_array($this->aInputs['reoccurring']['ghost_template']) && !isset($this->aInputs['reoccurring']['ghost_template']['inputs'])) {
  203. foreach ($this->aInputs['reoccurring']['ghost_template'] as $oFormNested) {
  204. $oFormNested->insert(array('event_id' => $iContentId));
  205. }
  206. }
  207. return $iContentId;
  208. }
  209. function update ($val, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  210. {
  211. $iAffectedRows = parent::update($val, $aValsToAdd, $aTrackTextFieldsChanges);
  212. if (isset($this->aInputs['reoccurring']) && is_array($this->aInputs['reoccurring']['ghost_template']) && !isset($this->aInputs['reoccurring']['ghost_template']['inputs'])) {
  213. foreach ($this->aInputs['reoccurring']['ghost_template'] as $oFormNested) {
  214. $aSpecificValues = $oFormNested->getSpecificValues();
  215. $iIntervalId = $oFormNested->getSubmittedValue('interval_id', BX_DOL_FORM_METHOD_SPECIFIC, $aSpecificValues);
  216. if ($iIntervalId) {
  217. $oFormNested->update($iIntervalId);
  218. } else {
  219. $oFormNested->insert(array('event_id' => $this->_iContentId));
  220. }
  221. }
  222. }
  223. return $iAffectedRows;
  224. }
  225. function addCssJs ()
  226. {
  227. if ((!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) && !self::$_isCssJsEventsAdded) {
  228. $this->oTemplate->addJs(array('moment-timezone-with-data.js', 'entry.js', 'intervals.js'));
  229. $this->oTemplate->addCss('informer.css');
  230. self::$_isCssJsEventsAdded = true;
  231. }
  232. parent::addCssJs ();
  233. }
  234. }
  235. /** @} */