1
0

BxBaseModGeneralFormEntry.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 static $_isCssJsGeneralModuleAdded = false;
  17. protected $MODULE;
  18. protected $_oModule;
  19. protected $_aMetatagsFieldsWithKeywords = array();
  20. protected $_oMetatagsObject = null;
  21. protected $_oMetatagsContentId = 0;
  22. protected $_sGhostTemplate = 'form_ghost_template.html';
  23. protected $_aTrackFieldsChanges;
  24. public function __construct($aInfo, $oTemplate = false)
  25. {
  26. parent::__construct($aInfo, $oTemplate);
  27. $this->_oModule = BxDolModule::getInstance($this->MODULE);
  28. $this->_aTrackFieldsChanges = array();
  29. $CNF = &$this->_oModule->_oConfig->CNF;
  30. if (isset($CNF['FIELD_ADDED']) && isset($this->aInputs[$CNF['FIELD_ADDED']])) {
  31. $this->aInputs[$CNF['FIELD_ADDED']]['date_filter'] = BX_DATA_INT;
  32. $this->aInputs[$CNF['FIELD_ADDED']]['date_format'] = BX_FORMAT_DATE;
  33. }
  34. if (isset($CNF['FIELD_CHANGED']) && isset($this->aInputs[$CNF['FIELD_CHANGED']])) {
  35. $this->aInputs[$CNF['FIELD_CHANGED']]['date_filter'] = BX_DATA_INT;
  36. $this->aInputs[$CNF['FIELD_CHANGED']]['date_format'] = BX_FORMAT_DATE;
  37. }
  38. if (isset($CNF['FIELD_TEXT']) && isset($this->aInputs[$CNF['FIELD_TEXT']]) && isset($CNF['FIELD_TEXT_ID'])) {
  39. $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] = array_merge (
  40. array ('id' => $CNF['FIELD_TEXT_ID']),
  41. is_array($this->aInputs[$CNF['FIELD_TEXT']]['attrs']) ? $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] : array ()
  42. );
  43. }
  44. if (isset($CNF['FIELD_LOCATION_PREFIX']) && isset($this->aInputs[$CNF['FIELD_LOCATION_PREFIX']])) {
  45. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['manual_input'] = true;
  46. }
  47. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  48. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_object'] = $CNF['OBJECT_STORAGE'];
  49. $this->aInputs[$CNF['FIELD_PHOTO']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_PHOTO']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_PHOTO']]['value']) : $CNF['OBJECT_UPLOADERS'];
  50. $this->aInputs[$CNF['FIELD_PHOTO']]['images_transcoder'] = $CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW'];
  51. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_private'] = 0;
  52. $this->aInputs[$CNF['FIELD_PHOTO']]['multiple'] = true;
  53. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = 0;
  54. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = '';
  55. }
  56. if (isset($CNF['FIELD_ALLOW_VIEW_TO']) && isset($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']]) && $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) {
  57. $aSave = array('db' => array('pass' => 'Xss'));
  58. array_walk($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']], function ($a, $k, $aSave) {
  59. if (in_array($k, array('info', 'caption', 'value')))
  60. $aSave[0][$k] = $a;
  61. }, array(&$aSave));
  62. $aGroupChooser = $oPrivacy->getGroupChooser($CNF['OBJECT_PRIVACY_VIEW']);
  63. $this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']] = array_merge($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']], $aGroupChooser, $aSave);
  64. }
  65. }
  66. /**
  67. * Add field(s) which will be tracked during content update.
  68. */
  69. public function addTrackFields($mixedFields, $mixedContent)
  70. {
  71. if(empty($mixedContent) || empty($mixedFields))
  72. return;
  73. if(!is_array($mixedContent)) {
  74. $mixedContent = $this->_oModule->_oDb->getContentInfoById((int)$mixedContent);
  75. if(empty($mixedContent) || !is_array($mixedContent))
  76. return;
  77. }
  78. if(!is_array($mixedFields))
  79. $mixedFields = array($mixedFields);
  80. foreach($mixedFields as $sField) {
  81. if(!isset($mixedContent[$sField]))
  82. continue;
  83. $this->_aTrackFieldsChanges[$sField] = array(
  84. 'old' => $mixedContent[$sField],
  85. );
  86. }
  87. }
  88. /**
  89. * Checks if the field was changed.
  90. */
  91. public function isTrackFieldChanged($sField, $bReturnValues = false)
  92. {
  93. if(!isset($this->_aTrackFieldsChanges[$sField]) || $this->_aTrackFieldsChanges[$sField] === false)
  94. return false;
  95. return $bReturnValues ? $this->_aTrackFieldsChanges[$sField] : true;
  96. }
  97. function initChecker ($aValues = array (), $aSpecificValues = array())
  98. {
  99. $CNF = &$this->_oModule->_oConfig->CNF;
  100. 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())
  101. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['value'] = $oMetatags->locationsString($aValues[$CNF['FIELD_ID']], false);
  102. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  103. $aContentInfo = false;
  104. if ($aValues && !empty($aValues['id'])) {
  105. $aContentInfo = $this->_oModule->_oDb->getContentInfoById ($aValues['id']);
  106. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = $aValues['id'];
  107. }
  108. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplate, $this->_getPhotoGhostTmplVars($aContentInfo));
  109. }
  110. if (isset($CNF['FIELD_LABELS']) && isset($this->aInputs[$CNF['FIELD_LABELS']]) && !empty($aValues['id']) && ($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) && $oMetatags->keywordsIsEnabled() && ($aLabels = $oMetatags->keywordsGet($aValues['id']))) {
  111. $this->aInputs[$CNF['FIELD_LABELS']]['content_id'] = $aValues['id'];
  112. $this->aInputs[$CNF['FIELD_LABELS']]['meta_object'] = $CNF['OBJECT_METATAGS'];
  113. $this->aInputs[$CNF['FIELD_LABELS']]['value'] = $aLabels;
  114. }
  115. if (isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']]) && isset($CNF['FIELD_AUTHOR']) && isset($aValues[$CNF['FIELD_AUTHOR']])) {
  116. $this->aInputs[$CNF['FIELD_ANONYMOUS']]['checked'] = $aValues[$CNF['FIELD_AUTHOR']] < 0;
  117. }
  118. parent::initChecker ($aValues, $aSpecificValues);
  119. }
  120. public function insert ($aValsToAdd = array(), $isIgnore = false)
  121. {
  122. $CNF = &$this->_oModule->_oConfig->CNF;
  123. if (isset($CNF['FIELD_AUTHOR']) && empty($aValsToAdd[$CNF['FIELD_AUTHOR']]))
  124. $aValsToAdd[$CNF['FIELD_AUTHOR']] = (isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']]) && $this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1) * bx_get_logged_profile_id ();
  125. if (isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']]) && empty($this->getCleanValue($CNF['FIELD_ADDED'])))
  126. $aValsToAdd[$CNF['FIELD_ADDED']] = time();
  127. if (isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  128. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  129. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb() && isset($CNF['FIELD_THUMB'])) {
  130. $aThumb = isset($_POST[$CNF['FIELD_THUMB']]) ? bx_process_input ($_POST[$CNF['FIELD_THUMB']], BX_DATA_INT) : false;
  131. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  132. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  133. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  134. }
  135. if(isset($CNF['FIELD_STATUS']) && !empty($CNF['FIELDS_DELAYED_PROCESSING'])) {
  136. $aFields = $CNF['FIELDS_DELAYED_PROCESSING'];
  137. if(is_string($aFields))
  138. $aFields = explode(',', $aFields);
  139. foreach($aFields as $sField) {
  140. $mixedFieldValues = $this->getCleanValue($sField);
  141. if(!empty($mixedFieldValues))
  142. $aValsToAdd[$CNF['FIELD_STATUS']] = 'awaiting';
  143. }
  144. }
  145. return parent::insert ($aValsToAdd, $isIgnore);
  146. }
  147. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  148. {
  149. $CNF = &$this->_oModule->_oConfig->CNF;
  150. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  151. if(isset($CNF['FIELD_AUTHOR']) && isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']]))
  152. $aValsToAdd[$CNF['FIELD_AUTHOR']] = ($this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1) * abs($aContentInfo[$CNF['FIELD_AUTHOR']]);
  153. if(isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  154. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  155. if(CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($iContentId) && isset($CNF['FIELD_THUMB'])) {
  156. $aThumb = bx_process_input (bx_get($CNF['FIELD_THUMB']), BX_DATA_INT);
  157. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  158. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  159. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  160. }
  161. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  162. $aValsToAdd[$CNF['FIELD_STATUS']] = 'active';
  163. $mixedResult = parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  164. if($mixedResult !== false)
  165. $this->_processTrackFields($iContentId);
  166. return $mixedResult;
  167. }
  168. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  169. {
  170. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  171. }
  172. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  173. {
  174. $CNF = &$this->_oModule->_oConfig->CNF;
  175. return array (
  176. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  177. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  178. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  179. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  180. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  181. 'bx_if:set_thumb' => array (
  182. 'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($this->aInputs[$CNF['FIELD_PHOTO']]['content_id']),
  183. 'content' => array (
  184. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  185. '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')
  186. ),
  187. ),
  188. );
  189. }
  190. function delete ($iContentId, $aContentInfo = array())
  191. {
  192. $CNF = &$this->_oModule->_oConfig->CNF;
  193. // delete associated files
  194. if (!empty($CNF['OBJECT_STORAGE'])) {
  195. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  196. if ($oStorage)
  197. $oStorage->queueFilesForDeletionFromGhosts($aContentInfo[$CNF['FIELD_AUTHOR']], $iContentId);
  198. }
  199. // delete associated objects data
  200. if (!empty($CNF['OBJECT_VIEWS'])) {
  201. $o = BxDolView::getObjectInstance($CNF['OBJECT_VIEWS'], $iContentId);
  202. if ($o) $o->onObjectDelete();
  203. }
  204. if (!empty($CNF['OBJECT_VOTES'])) {
  205. $o = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $iContentId);
  206. if ($o) $o->onObjectDelete();
  207. }
  208. if (!empty($CNF['OBJECT_SCORES'])) {
  209. $o = BxDolScore::getObjectInstance($CNF['OBJECT_SCORES'], $iContentId);
  210. if ($o) $o->onObjectDelete();
  211. }
  212. if (!empty($CNF['OBJECT_COMMENTS'])) {
  213. $o = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  214. if ($o) $o->onObjectDelete();
  215. }
  216. if (!empty($CNF['OBJECT_METATAGS'])) {
  217. $o = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  218. if ($o) $o->onDeleteContent($iContentId);
  219. }
  220. // delete db record
  221. return parent::delete($iContentId);
  222. }
  223. protected function _isAdmin ($iContentId = 0)
  224. {
  225. return $this->_oModule->_isModerator();
  226. }
  227. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  228. {
  229. $CNF = &$this->_oModule->_oConfig->CNF;
  230. if (!isset($this->aInputs[$sFieldFile]))
  231. return true;
  232. $mixedFileIds = $this->getCleanValue($sFieldFile);
  233. if(!$mixedFileIds)
  234. return true;
  235. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  236. if (!$oStorage)
  237. return false;
  238. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  239. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true, $this->_isAdmin($iContentId));
  240. if (!$aGhostFiles)
  241. return true;
  242. foreach ($aGhostFiles as $aFile) {
  243. if (is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  244. continue;
  245. if ($aFile['private'])
  246. $oStorage->setFilePrivate ($aFile['id'], 1);
  247. if ($iContentId)
  248. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  249. }
  250. return true;
  251. }
  252. function _deleteFile ($iFileId, $sStorage = '')
  253. {
  254. $CNF = &$this->_oModule->_oConfig->CNF;
  255. if (!$iFileId)
  256. return true;
  257. $sStorage = !empty($sStorage) ? $sStorage : $CNF['OBJECT_STORAGE'];
  258. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  259. if (!$oStorage)
  260. return false;
  261. if (!$oStorage->getFile($iFileId))
  262. return true;
  263. $iProfileId = bx_get_logged_profile_id();
  264. return $oStorage->deleteFile($iFileId, $iProfileId);
  265. }
  266. function addCssJs ()
  267. {
  268. if ((!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) && !self::$_isCssJsGeneralModuleAdded) {
  269. $this->_oModule->_oTemplate->addCss('forms.css');
  270. $this->_oModule->_oTemplate->addJs('modules/base/general/js/|forms.js');
  271. self::$_isCssJsGeneralModuleAdded = true;
  272. }
  273. return parent::addCssJs ();
  274. }
  275. function genViewRowValue(&$aInput)
  276. {
  277. $s = parent::genViewRowValue($aInput);
  278. if ($this->_oMetatagsObject && in_array($aInput['name'], $this->_aMetatagsFieldsWithKeywords) && $s)
  279. $s = $this->_oMetatagsObject->metaParse($this->_oMetatagsContentId, $s);
  280. return $s;
  281. }
  282. function setMetatagsKeywordsData($iId, $a, $o)
  283. {
  284. $this->_oMetatagsContentId = $iId;
  285. $this->_aMetatagsFieldsWithKeywords = $a;
  286. $this->_oMetatagsObject = $o;
  287. }
  288. function getContentOwnerProfileId ($iContentId)
  289. {
  290. return $this->_oModule->serviceGetContentOwnerProfileId($iContentId);
  291. }
  292. protected function _processTrackFields($mixedContent)
  293. {
  294. if(empty($mixedContent) || empty($this->_aTrackFieldsChanges))
  295. return;
  296. if(!is_array($mixedContent)) {
  297. $mixedContent = $this->_oModule->_oDb->getContentInfoById((int)$mixedContent);
  298. if(empty($mixedContent) || !is_array($mixedContent))
  299. return;
  300. }
  301. foreach($this->_aTrackFieldsChanges as $sField => $aValues)
  302. if($mixedContent[$sField] == $aValues['old'])
  303. $this->_aTrackFieldsChanges[$sField] = false;
  304. else
  305. $this->_aTrackFieldsChanges[$sField]['new'] = $mixedContent[$sField];
  306. }
  307. }
  308. /** @} */