BxEventsFormEntry.php 10 KB

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