BxPostsFormEntry.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 Posts Posts
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Create/Edit entry form
  13. */
  14. class BxPostsFormEntry extends BxBaseModTextFormEntry
  15. {
  16. protected $_sGhostTemplateCover = 'form_ghost_template_cover.html';
  17. public function __construct($aInfo, $oTemplate = false)
  18. {
  19. $this->MODULE = 'bx_posts';
  20. parent::__construct($aInfo, $oTemplate);
  21. }
  22. function initChecker ($aValues = array (), $aSpecificValues = array())
  23. {
  24. $CNF = &$this->_oModule->_oConfig->CNF;
  25. $bValues = $aValues && !empty($aValues['id']);
  26. $aContentInfo = $bValues ? $this->_oModule->_oDb->getContentInfoById($aValues['id']) : false;
  27. if($this->aParams['display'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'] && isset($CNF['FIELD_PUBLISHED']) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  28. if($bValues && in_array($aValues[$CNF['FIELD_STATUS']], array('active', 'hidden')))
  29. unset($this->aInputs[$CNF['FIELD_PUBLISHED']]);
  30. if (isset($CNF['FIELD_COVER']) && isset($this->aInputs[$CNF['FIELD_COVER']])) {
  31. if($bValues)
  32. $this->aInputs[$CNF['FIELD_COVER']]['content_id'] = $aValues['id'];
  33. $this->aInputs[$CNF['FIELD_COVER']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplateCover, $this->_getCoverGhostTmplVars($aContentInfo));
  34. }
  35. parent::initChecker ($aValues, $aSpecificValues);
  36. }
  37. public function insert ($aValsToAdd = array(), $isIgnore = false)
  38. {
  39. $CNF = &$this->_oModule->_oConfig->CNF;
  40. if(isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']])) {
  41. $iAdded = 0;
  42. if(isset($this->aInputs[$CNF['FIELD_ADDED']]))
  43. $iAdded = $this->getCleanValue($CNF['FIELD_ADDED']);
  44. if(empty($iAdded))
  45. $iAdded = time();
  46. $aValsToAdd[$CNF['FIELD_ADDED']] = $iAdded;
  47. }
  48. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']])) {
  49. $iPublished = 0;
  50. if(isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  51. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  52. if(empty($iPublished))
  53. $iPublished = time();
  54. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  55. }
  56. $aValsToAdd[$CNF['FIELD_STATUS']] = $aValsToAdd[$CNF['FIELD_PUBLISHED']] > $aValsToAdd[$CNF['FIELD_ADDED']] ? 'awaiting' : 'active';
  57. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  58. if(!empty($iContentId)){
  59. $this->processFiles($CNF['FIELD_COVER'], $iContentId, true);
  60. }
  61. return $iContentId;
  62. }
  63. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  64. {
  65. $CNF = &$this->_oModule->_oConfig->CNF;
  66. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']]) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']])) {
  67. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  68. if(empty($iPublished))
  69. $iPublished = time();
  70. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  71. }
  72. $iResult = parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  73. $this->processFiles($CNF['FIELD_COVER'], $iContentId, false);
  74. return $iResult;
  75. }
  76. protected function _getCoverGhostTmplVars($aContentInfo = array())
  77. {
  78. $CNF = &$this->_oModule->_oConfig->CNF;
  79. return array (
  80. 'name' => $this->aInputs[$CNF['FIELD_COVER']]['name'],
  81. 'content_id' => $this->aInputs[$CNF['FIELD_COVER']]['content_id'],
  82. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  83. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  84. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  85. );
  86. }
  87. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  88. {
  89. $CNF = &$this->_oModule->_oConfig->CNF;
  90. return array (
  91. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  92. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  93. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  94. 'bx_if:set_thumb' => [
  95. 'condition' => false,
  96. 'content' => []
  97. ],
  98. );
  99. }
  100. }
  101. /** @} */