BxBaseModGeneralFormEntry.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 BaseGeneral Base classes for modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Create/edit entry form
  13. */
  14. class BxBaseModGeneralFormEntry extends BxTemplFormView
  15. {
  16. protected $MODULE;
  17. protected $_oModule;
  18. protected $_aMetatagsFieldsWithKeywords = array();
  19. protected $_oMetatagsObject = null;
  20. protected $_oMetatagsContentId = 0;
  21. protected $_sGhostTemplate = 'form_ghost_template.html';
  22. public function __construct($aInfo, $oTemplate = false)
  23. {
  24. parent::__construct($aInfo, $oTemplate);
  25. $this->_oModule = BxDolModule::getInstance($this->MODULE);
  26. $CNF = &$this->_oModule->_oConfig->CNF;
  27. if (isset($CNF['FIELD_ADDED']) && isset($this->aInputs[$CNF['FIELD_ADDED']])) {
  28. $this->aInputs[$CNF['FIELD_ADDED']]['date_filter'] = BX_DATA_INT;
  29. $this->aInputs[$CNF['FIELD_ADDED']]['date_format'] = BX_FORMAT_DATE;
  30. }
  31. if (isset($CNF['FIELD_CHANGED']) && isset($this->aInputs[$CNF['FIELD_CHANGED']])) {
  32. $this->aInputs[$CNF['FIELD_CHANGED']]['date_filter'] = BX_DATA_INT;
  33. $this->aInputs[$CNF['FIELD_CHANGED']]['date_format'] = BX_FORMAT_DATE;
  34. }
  35. if (isset($CNF['FIELD_TEXT']) && isset($this->aInputs[$CNF['FIELD_TEXT']]) && isset($CNF['FIELD_TEXT_ID'])) {
  36. $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] = array_merge (
  37. array ('id' => $CNF['FIELD_TEXT_ID']),
  38. is_array($this->aInputs[$CNF['FIELD_TEXT']]['attrs']) ? $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] : array ()
  39. );
  40. }
  41. if (isset($CNF['FIELD_LOCATION_PREFIX']) && isset($this->aInputs[$CNF['FIELD_LOCATION_PREFIX']])) {
  42. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['manual_input'] = true;
  43. }
  44. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  45. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_object'] = $CNF['OBJECT_STORAGE'];
  46. $this->aInputs[$CNF['FIELD_PHOTO']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_PHOTO']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_PHOTO']]['value']) : $CNF['OBJECT_UPLOADERS'];
  47. $this->aInputs[$CNF['FIELD_PHOTO']]['images_transcoder'] = $CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW'];
  48. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_private'] = 0;
  49. $this->aInputs[$CNF['FIELD_PHOTO']]['multiple'] = true;
  50. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = 0;
  51. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = '';
  52. }
  53. }
  54. function initChecker ($aValues = array (), $aSpecificValues = array())
  55. {
  56. $CNF = &$this->_oModule->_oConfig->CNF;
  57. if (!empty($CNF['FIELD_LOCATION_PREFIX']) && isset($this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]) && isset($aValues[$CNF['FIELD_ID']]) && !empty($CNF['OBJECT_METATAGS']) && ($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) && $oMetatags->locationsIsEnabled())
  58. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['value'] = $oMetatags->locationsString($aValues[$CNF['FIELD_ID']], false);
  59. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  60. $aContentInfo = false;
  61. if ($aValues && !empty($aValues['id'])) {
  62. $aContentInfo = $this->_oModule->_oDb->getContentInfoById ($aValues['id']);
  63. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = $aValues['id'];
  64. }
  65. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplate, $this->_getPhotoGhostTmplVars($aContentInfo));
  66. }
  67. parent::initChecker ($aValues, $aSpecificValues);
  68. }
  69. public function insert ($aValsToAdd = array(), $isIgnore = false)
  70. {
  71. $CNF = &$this->_oModule->_oConfig->CNF;
  72. if (isset($CNF['FIELD_AUTHOR']) && empty($aValsToAdd[$CNF['FIELD_AUTHOR']]))
  73. $aValsToAdd[$CNF['FIELD_AUTHOR']] = bx_get_logged_profile_id ();
  74. if (isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']]) && empty($this->getCleanValue($CNF['FIELD_ADDED'])))
  75. $aValsToAdd[$CNF['FIELD_ADDED']] = time();
  76. if (isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  77. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  78. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb() && isset($CNF['FIELD_THUMB'])) {
  79. $aThumb = isset($_POST[$CNF['FIELD_THUMB']]) ? bx_process_input ($_POST[$CNF['FIELD_THUMB']], BX_DATA_INT) : false;
  80. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  81. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  82. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  83. }
  84. return parent::insert ($aValsToAdd, $isIgnore);
  85. }
  86. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  87. {
  88. $CNF = &$this->_oModule->_oConfig->CNF;
  89. if (isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  90. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  91. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($iContentId) && isset($CNF['FIELD_THUMB'])) {
  92. $aThumb = bx_process_input (bx_get($CNF['FIELD_THUMB']), BX_DATA_INT);
  93. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  94. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  95. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  96. }
  97. return parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  98. }
  99. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  100. {
  101. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  102. }
  103. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  104. {
  105. $CNF = &$this->_oModule->_oConfig->CNF;
  106. return array (
  107. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  108. 'content_id' => $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  109. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  110. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  111. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  112. 'bx_if:set_thumb' => array (
  113. 'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($this->aInputs[$CNF['FIELD_PHOTO']]['content_id']),
  114. 'content' => array (
  115. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  116. 'txt_pict_use_as_thumb' => _t(!empty($CNF['T']['txt_pict_use_as_thumb']) ? $CNF['T']['txt_pict_use_as_thumb'] : '_sys_txt_form_entry_input_picture_use_as_thumb')
  117. ),
  118. ),
  119. );
  120. }
  121. function delete ($iContentId, $aContentInfo = array())
  122. {
  123. $CNF = &$this->_oModule->_oConfig->CNF;
  124. // delete associated files
  125. if (!empty($CNF['OBJECT_STORAGE'])) {
  126. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  127. if ($oStorage)
  128. $oStorage->queueFilesForDeletionFromGhosts($aContentInfo[$CNF['FIELD_AUTHOR']], $iContentId);
  129. }
  130. // delete associated objects data
  131. if (!empty($CNF['OBJECT_VIEWS'])) {
  132. $o = BxDolView::getObjectInstance($CNF['OBJECT_VIEWS'], $iContentId);
  133. if ($o) $o->onObjectDelete();
  134. }
  135. if (!empty($CNF['OBJECT_VOTES'])) {
  136. $o = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $iContentId);
  137. if ($o) $o->onObjectDelete();
  138. }
  139. if (!empty($CNF['OBJECT_SCORES'])) {
  140. $o = BxDolScore::getObjectInstance($CNF['OBJECT_SCORES'], $iContentId);
  141. if ($o) $o->onObjectDelete();
  142. }
  143. if (!empty($CNF['OBJECT_COMMENTS'])) {
  144. $o = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  145. if ($o) $o->onObjectDelete();
  146. }
  147. if (!empty($CNF['OBJECT_METATAGS'])) {
  148. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  149. $oMetatags->onDeleteContent($iContentId);
  150. }
  151. // delete db record
  152. return parent::delete($iContentId);
  153. }
  154. protected function _isAdmin ($iContentId = 0)
  155. {
  156. return $this->_oModule->_isModerator();
  157. }
  158. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  159. {
  160. $CNF = &$this->_oModule->_oConfig->CNF;
  161. if (!isset($this->aInputs[$sFieldFile]))
  162. return true;
  163. $mixedFileIds = $this->getCleanValue($sFieldFile);
  164. if(!$mixedFileIds)
  165. return true;
  166. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  167. if (!$oStorage)
  168. return false;
  169. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  170. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true, $this->_isAdmin($iContentId));
  171. if (!$aGhostFiles)
  172. return true;
  173. foreach ($aGhostFiles as $aFile) {
  174. if (is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  175. continue;
  176. if ($aFile['private'])
  177. $oStorage->setFilePrivate ($aFile['id'], 1);
  178. if ($iContentId)
  179. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  180. }
  181. return true;
  182. }
  183. function _deleteFile ($iFileId, $sStorage = '')
  184. {
  185. $CNF = &$this->_oModule->_oConfig->CNF;
  186. if (!$iFileId)
  187. return true;
  188. $sStorage = !empty($sStorage) ? $sStorage : $CNF['OBJECT_STORAGE'];
  189. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  190. if (!$oStorage)
  191. return false;
  192. if (!$oStorage->getFile($iFileId))
  193. return true;
  194. $iProfileId = bx_get_logged_profile_id();
  195. return $oStorage->deleteFile($iFileId, $iProfileId);
  196. }
  197. function addCssJs ()
  198. {
  199. if (!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) {
  200. if (self::$_isCssJsAdded)
  201. return;
  202. $this->_oModule->_oTemplate->addCss('forms.css');
  203. $this->_oModule->_oTemplate->addJs('modules/base/general/js/|forms.js');
  204. }
  205. return parent::addCssJs ();
  206. }
  207. function genViewRowValue(&$aInput)
  208. {
  209. $s = parent::genViewRowValue($aInput);
  210. if ($this->_oMetatagsObject && in_array($aInput['name'], $this->_aMetatagsFieldsWithKeywords) && $s)
  211. $s = $this->_oMetatagsObject->metaParse($this->_oMetatagsContentId, $s);
  212. return $s;
  213. }
  214. function setMetatagsKeywordsData($iId, $a, $o)
  215. {
  216. $this->_oMetatagsContentId = $iId;
  217. $this->_aMetatagsFieldsWithKeywords = $a;
  218. $this->_oMetatagsObject = $o;
  219. }
  220. function getContentOwnerProfileId ($iContentId)
  221. {
  222. return $this->_oModule->serviceGetContentOwnerProfileId($iContentId);
  223. }
  224. }
  225. /** @} */