BxResourcesFormEntry.php 4.6 KB

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