BxBaseModGeneralFormEntry.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. protected $_iContentId;
  25. public function __construct($aInfo, $oTemplate = false)
  26. {
  27. parent::__construct($aInfo, $oTemplate);
  28. $this->_oModule = BxDolModule::getInstance($this->MODULE);
  29. $this->_aTrackFieldsChanges = array();
  30. $CNF = &$this->_oModule->_oConfig->CNF;
  31. if (isset($CNF['FIELD_ADDED']) && isset($this->aInputs[$CNF['FIELD_ADDED']])) {
  32. $this->aInputs[$CNF['FIELD_ADDED']]['date_filter'] = BX_DATA_INT;
  33. $this->aInputs[$CNF['FIELD_ADDED']]['date_format'] = BX_FORMAT_DATE;
  34. }
  35. if (isset($CNF['FIELD_CHANGED']) && isset($this->aInputs[$CNF['FIELD_CHANGED']])) {
  36. $this->aInputs[$CNF['FIELD_CHANGED']]['date_filter'] = BX_DATA_INT;
  37. $this->aInputs[$CNF['FIELD_CHANGED']]['date_format'] = BX_FORMAT_DATE;
  38. }
  39. if (isset($CNF['FIELD_TEXT']) && isset($this->aInputs[$CNF['FIELD_TEXT']]) && isset($CNF['FIELD_TEXT_ID'])) {
  40. $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] = array_merge (
  41. array ('id' => $CNF['FIELD_TEXT_ID']),
  42. is_array($this->aInputs[$CNF['FIELD_TEXT']]['attrs']) ? $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] : array ()
  43. );
  44. }
  45. if (isset($CNF['FIELD_LABELS']) && isset($this->aInputs[$CNF['FIELD_LABELS']]) && !empty($CNF['OBJECT_METATAGS']) && ($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) && $oMetatags->keywordsIsEnabled()) {
  46. $this->aInputs[$CNF['FIELD_LABELS']]['meta_object'] = $CNF['OBJECT_METATAGS'];
  47. }
  48. if (isset($CNF['FIELD_LOCATION_PREFIX']) && isset($this->aInputs[$CNF['FIELD_LOCATION_PREFIX']])) {
  49. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['manual_input'] = true;
  50. }
  51. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  52. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_object'] = $CNF['OBJECT_STORAGE'];
  53. $this->aInputs[$CNF['FIELD_PHOTO']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_PHOTO']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_PHOTO']]['value']) : $CNF['OBJECT_UPLOADERS'];
  54. $this->aInputs[$CNF['FIELD_PHOTO']]['images_transcoder'] = $CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW'];
  55. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_private'] = 0;
  56. $this->aInputs[$CNF['FIELD_PHOTO']]['multiple'] = true;
  57. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = 0;
  58. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = '';
  59. }
  60. $aPrivacyFields = $this->_getPrivacyFields();
  61. foreach($aPrivacyFields as $sField => $sObject)
  62. $this->_preparePrivacyField($sField, $sObject);
  63. }
  64. function getCode($bDynamicMode = false)
  65. {
  66. $CNF = &$this->_oModule->_oConfig->CNF;
  67. if(!$bDynamicMode && bx_is_dynamic_request())
  68. $bDynamicMode = true;
  69. $sResult = parent::getCode($bDynamicMode);
  70. $aPrivacyFields = $this->_getPrivacyFields();
  71. foreach($aPrivacyFields as $sField => $sObject)
  72. $this->_addCssJsPrivacyField($sField, $sObject, $bDynamicMode);
  73. if(isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true) {
  74. $sInclude2 = '';
  75. $sInclude2 .= $this->_oModule->_oTemplate->addJs(array('BxDolCategories.js'), $bDynamicMode);
  76. $sInclude2 .= $this->_oModule->_oTemplate->addCss(array('categories.css'), $bDynamicMode);
  77. $sResult .= ($bDynamicMode ? $sInclude2 : '') . $this->_oModule->_oTemplate->getJsCode('categories');
  78. }
  79. return $sResult;
  80. }
  81. /**
  82. * Add field(s) which will be tracked during content update.
  83. */
  84. public function addTrackFields($mixedFields, $mixedContent)
  85. {
  86. if(empty($mixedContent) || empty($mixedFields))
  87. return;
  88. if(!is_array($mixedContent)) {
  89. $mixedContent = $this->_oModule->_oDb->getContentInfoById((int)$mixedContent);
  90. if(empty($mixedContent) || !is_array($mixedContent))
  91. return;
  92. }
  93. if(!is_array($mixedFields))
  94. $mixedFields = array($mixedFields);
  95. foreach($mixedFields as $sField) {
  96. if(!isset($mixedContent[$sField]))
  97. continue;
  98. $this->_aTrackFieldsChanges[$sField] = array(
  99. 'old' => $mixedContent[$sField],
  100. );
  101. }
  102. }
  103. /**
  104. * Checks if the field was changed.
  105. */
  106. public function isTrackFieldChanged($sField, $bReturnValues = false)
  107. {
  108. if(!isset($this->_aTrackFieldsChanges[$sField]) || $this->_aTrackFieldsChanges[$sField] === false)
  109. return false;
  110. return $bReturnValues ? $this->_aTrackFieldsChanges[$sField] : true;
  111. }
  112. function initChecker ($aValues = array (), $aSpecificValues = array())
  113. {
  114. $CNF = &$this->_oModule->_oConfig->CNF;
  115. $bValues = $aValues && !empty($aValues['id']);
  116. $this->_iContentId = isset($aValues['id']) ? $aValues['id'] : false;
  117. 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())
  118. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['value'] = $oMetatags->locationsString($aValues[$CNF['FIELD_ID']], false);
  119. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  120. $aContentInfo = false;
  121. if ($aValues && !empty($aValues['id'])) {
  122. $aContentInfo = $this->_oModule->_oDb->getContentInfoById ($aValues['id']);
  123. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = $aValues['id'];
  124. }
  125. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplate, $this->_getPhotoGhostTmplVars($aContentInfo));
  126. }
  127. if (isset($CNF['FIELD_LABELS']) && isset($this->aInputs[$CNF['FIELD_LABELS']]) && !empty($CNF['OBJECT_METATAGS']) && ($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) && $oMetatags->keywordsIsEnabled() && !empty($aValues['id'])) {
  128. $this->aInputs[$CNF['FIELD_LABELS']]['content_id'] = $aValues['id'];
  129. if(($aLabels = $oMetatags->keywordsGet($aValues['id'])))
  130. $this->aInputs[$CNF['FIELD_LABELS']]['value'] = array_intersect($aLabels, BxDolLabel::getInstance()->getLabels(array('type' => 'values')));
  131. }
  132. if (isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']]) && isset($CNF['FIELD_AUTHOR']) && isset($aValues[$CNF['FIELD_AUTHOR']])) {
  133. $this->aInputs[$CNF['FIELD_ANONYMOUS']]['checked'] = $aValues[$CNF['FIELD_AUTHOR']] < 0;
  134. }
  135. if (isset($CNF['FIELD_MULTICAT']) && isset($this->aInputs[$CNF['FIELD_MULTICAT']]) && $CNF['PARAM_MULTICAT_ENABLED']) {
  136. if ($bValues)
  137. $this->aInputs[$CNF['FIELD_MULTICAT']]['content_id'] = $aValues['id'];
  138. }
  139. $aPrivacyFields = $this->_getPrivacyFields();
  140. foreach($aPrivacyFields as $sField => $sObject)
  141. $this->_preloadPrivacyField($sField, $sObject, $aValues);
  142. parent::initChecker ($aValues, $aSpecificValues);
  143. }
  144. public function insert ($aValsToAdd = array(), $isIgnore = false)
  145. {
  146. $CNF = &$this->_oModule->_oConfig->CNF;
  147. if (isset($CNF['FIELD_AUTHOR']) && empty($aValsToAdd[$CNF['FIELD_AUTHOR']]))
  148. $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 ();
  149. if (isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']]) && empty($this->getCleanValue($CNF['FIELD_ADDED'])))
  150. $aValsToAdd[$CNF['FIELD_ADDED']] = time();
  151. if (isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  152. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  153. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb() && isset($CNF['FIELD_THUMB'])) {
  154. $aThumb = isset($_POST[$CNF['FIELD_THUMB']]) ? bx_process_input ($_POST[$CNF['FIELD_THUMB']], BX_DATA_INT) : false;
  155. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  156. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  157. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  158. }
  159. if(!empty($CNF['OBJECT_METATAGS']))
  160. $this->_processMetas($aValsToAdd);
  161. if(isset($CNF['FIELD_STATUS']) && !empty($CNF['FIELDS_DELAYED_PROCESSING']) && !empty($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4'])) {
  162. $oTranscoder = BxDolTranscoder::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  163. $aFields = $CNF['FIELDS_DELAYED_PROCESSING'];
  164. if(is_string($aFields))
  165. $aFields = explode(',', $aFields);
  166. foreach($aFields as $sField) {
  167. $mixedFieldValues = $this->getCleanValue($sField);
  168. if(empty($mixedFieldValues))
  169. continue;
  170. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sField]['storage_object']);
  171. if(!$oStorage)
  172. continue;
  173. if(!is_array($mixedFieldValues))
  174. $mixedFieldValues = array($mixedFieldValues);
  175. foreach($mixedFieldValues as $mixedFieldValue) {
  176. $aInfo = $oStorage->getFile((int)$mixedFieldValue);
  177. if(empty($aInfo) || !is_array($aInfo))
  178. continue;
  179. if(strncmp($aInfo['mime_type'], 'video/', 6) != 0)
  180. continue;
  181. if($oTranscoder->isFileReady((int)$mixedFieldValue))
  182. continue;
  183. $aValsToAdd[$CNF['FIELD_STATUS']] = 'awaiting';
  184. break 2;
  185. }
  186. }
  187. }
  188. $bMulticatEnabled = $this->_isMulticatEnabled();
  189. if ($bMulticatEnabled)
  190. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  191. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  192. if(!empty($iContentId)){
  193. if ($bMulticatEnabled)
  194. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  195. }
  196. foreach($this->aInputs as $aInput) {
  197. if ($aInput['rateable']){
  198. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $aValsToAdd[$CNF['FIELD_AUTHOR']], $this->_oModule->getName());
  199. }
  200. }
  201. return $iContentId;
  202. }
  203. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  204. {
  205. $CNF = &$this->_oModule->_oConfig->CNF;
  206. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  207. if(isset($CNF['FIELD_AUTHOR']) && isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']]))
  208. $aValsToAdd[$CNF['FIELD_AUTHOR']] = ($this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1) * abs($aContentInfo[$CNF['FIELD_AUTHOR']]);
  209. if(isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  210. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  211. if(CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($iContentId) && isset($CNF['FIELD_THUMB'])) {
  212. $aThumb = bx_process_input (bx_get($CNF['FIELD_THUMB']), BX_DATA_INT);
  213. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  214. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  215. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  216. }
  217. if(!empty($CNF['OBJECT_METATAGS']))
  218. $this->_processMetas($aValsToAdd);
  219. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  220. $aValsToAdd[$CNF['FIELD_STATUS']] = 'active';
  221. $bMulticatEnabled = $this->_isMulticatEnabled();
  222. if ($bMulticatEnabled)
  223. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  224. $mixedResult = parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  225. if ($bMulticatEnabled)
  226. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  227. if($mixedResult !== false)
  228. $this->_processTrackFields($iContentId);
  229. return $mixedResult;
  230. }
  231. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  232. {
  233. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  234. }
  235. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  236. {
  237. $CNF = &$this->_oModule->_oConfig->CNF;
  238. return array (
  239. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  240. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  241. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  242. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  243. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  244. 'bx_if:set_thumb' => array (
  245. 'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($this->aInputs[$CNF['FIELD_PHOTO']]['content_id']),
  246. 'content' => array (
  247. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  248. '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')
  249. ),
  250. ),
  251. );
  252. }
  253. function delete ($iContentId, $aContentInfo = array())
  254. {
  255. $CNF = &$this->_oModule->_oConfig->CNF;
  256. // delete associated files
  257. if (!empty($CNF['OBJECT_STORAGE'])) {
  258. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  259. if ($oStorage)
  260. $oStorage->queueFilesForDeletionFromGhosts($aContentInfo[$CNF['FIELD_AUTHOR']], $iContentId);
  261. }
  262. // delete associated objects data
  263. if (!empty($CNF['OBJECT_VIEWS'])) {
  264. $o = BxDolView::getObjectInstance($CNF['OBJECT_VIEWS'], $iContentId);
  265. if ($o) $o->onObjectDelete();
  266. }
  267. if (!empty($CNF['OBJECT_VOTES'])) {
  268. $o = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $iContentId);
  269. if ($o) $o->onObjectDelete();
  270. }
  271. if (!empty($CNF['OBJECT_SCORES'])) {
  272. $o = BxDolScore::getObjectInstance($CNF['OBJECT_SCORES'], $iContentId);
  273. if ($o) $o->onObjectDelete();
  274. }
  275. if (!empty($CNF['OBJECT_COMMENTS'])) {
  276. $o = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  277. if ($o) $o->onObjectDelete();
  278. }
  279. if (!empty($CNF['OBJECT_METATAGS'])) {
  280. $o = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  281. if ($o) $o->onDeleteContent($iContentId);
  282. }
  283. // delete db record
  284. BxDolFormQuery::removeFormField($this->id, $iContentId);
  285. return parent::delete($iContentId);
  286. }
  287. protected function _isAdmin ($iContentId = 0)
  288. {
  289. return $this->_oModule->_isModerator();
  290. }
  291. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  292. {
  293. $CNF = &$this->_oModule->_oConfig->CNF;
  294. if (!isset($this->aInputs[$sFieldFile]))
  295. return true;
  296. $mixedFileIds = $this->getCleanValue($sFieldFile);
  297. if(!$mixedFileIds)
  298. return true;
  299. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  300. if (!$oStorage)
  301. return false;
  302. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  303. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true, $this->_isAdmin($iContentId));
  304. if (!$aGhostFiles)
  305. return true;
  306. foreach ($aGhostFiles as $aFile) {
  307. if (is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  308. continue;
  309. if ($aFile['private'])
  310. $oStorage->setFilePrivate ($aFile['id'], 1);
  311. if ($iContentId)
  312. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  313. }
  314. return true;
  315. }
  316. function _deleteFile ($iFileId, $sStorage = '')
  317. {
  318. $CNF = &$this->_oModule->_oConfig->CNF;
  319. if (!$iFileId)
  320. return true;
  321. $sStorage = !empty($sStorage) ? $sStorage : $CNF['OBJECT_STORAGE'];
  322. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  323. if (!$oStorage)
  324. return false;
  325. if (!$oStorage->getFile($iFileId))
  326. return true;
  327. $iProfileId = bx_get_logged_profile_id();
  328. return $oStorage->deleteFile($iFileId, $iProfileId);
  329. }
  330. function addCssJs ()
  331. {
  332. if ((!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) && !self::$_isCssJsGeneralModuleAdded) {
  333. $this->_oModule->_oTemplate->addCss('forms.css');
  334. $this->_oModule->_oTemplate->addJs('modules/base/general/js/|forms.js');
  335. self::$_isCssJsGeneralModuleAdded = true;
  336. }
  337. return parent::addCssJs ();
  338. }
  339. function genViewRowValue(&$aInput)
  340. {
  341. $s = parent::genViewRowValue($aInput);
  342. if ($this->_oMetatagsObject && in_array($aInput['name'], $this->_aMetatagsFieldsWithKeywords) && $s)
  343. $s = $this->_oMetatagsObject->metaParse($this->_oMetatagsContentId, $s);
  344. return $s;
  345. }
  346. function genViewRowWrapped(&$aInput)
  347. {
  348. if (!$aInput['rateable']){
  349. return parent::genViewRowWrapped($aInput);
  350. }
  351. $sValue = $this->genViewRowValue($aInput);
  352. if (null === $sValue)
  353. return '';
  354. // process rateable fields
  355. $iId = BxDolFormQuery::getFormField($this->id, $aInput['name'], $this->_iContentId);
  356. $sReactions = '';
  357. $oReaction = BxDolVote::getObjectInstance($aInput['rateable'], $iId, true, BxDolTemplate::getInstance());
  358. if ($oReaction)
  359. $sReactions = $oReaction->getElementInline(array('show_counter_empty' => true, 'show_counter' => true, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode));
  360. return $this->oTemplate->parseHtmlByName('form_view_row.html', array(
  361. 'type' => $aInput['type'],
  362. 'caption' => isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '',
  363. 'value' => $sValue . $sReactions
  364. ));
  365. }
  366. function setMetatagsKeywordsData($iId, $a, $o)
  367. {
  368. $this->_oMetatagsContentId = $iId;
  369. $this->_aMetatagsFieldsWithKeywords = $a;
  370. $this->_oMetatagsObject = $o;
  371. }
  372. function getContentOwnerProfileId ($iContentId)
  373. {
  374. return $this->_oModule->serviceGetContentOwnerProfileId($iContentId);
  375. }
  376. protected function _processTrackFields($mixedContent)
  377. {
  378. if(empty($mixedContent) || empty($this->_aTrackFieldsChanges))
  379. return;
  380. if(!is_array($mixedContent)) {
  381. $mixedContent = $this->_oModule->_oDb->getContentInfoById((int)$mixedContent);
  382. if(empty($mixedContent) || !is_array($mixedContent))
  383. return;
  384. }
  385. foreach($this->_aTrackFieldsChanges as $sField => $aValues)
  386. if($mixedContent[$sField] == $aValues['old'])
  387. $this->_aTrackFieldsChanges[$sField] = false;
  388. else
  389. $this->_aTrackFieldsChanges[$sField]['new'] = $mixedContent[$sField];
  390. }
  391. protected function _processMetas(&$aValsToAdd)
  392. {
  393. $CNF = &$this->_oModule->_oConfig->CNF;
  394. $sKey = 'FIELD_LABELS';
  395. if(isset($CNF[$sKey]) && isset($this->aInputs[$CNF[$sKey]]) && empty($aValsToAdd[$CNF[$sKey]])) {
  396. $aLabels = $this->getCleanValue($CNF[$sKey]);
  397. if(!empty($aLabels) && is_array($aLabels))
  398. $aValsToAdd[$CNF[$sKey]] = serialize($aLabels);
  399. else
  400. $aValsToAdd[$CNF[$sKey]] = '';
  401. }
  402. $sKey1 = 'FIELD_LOCATION';
  403. $sKey2 = 'FIELD_LOCATION_PREFIX';
  404. if(isset($CNF[$sKey1]) && isset($CNF[$sKey2]) && isset($this->aInputs[$CNF[$sKey1]]) && empty($aValsToAdd[$CNF[$sKey1]])) {
  405. $aLocation = BxDolMetatags::locationsRetrieveFromForm($CNF[$sKey2], $this);
  406. if(!empty($aLocation) && is_array($aLocation))
  407. $aValsToAdd[$CNF[$sKey1]] = serialize(BxDolMetatags::locationsParseComponents($aLocation));
  408. }
  409. }
  410. protected function _getPrivacyFields($aKeysF2O = array())
  411. {
  412. $CNF = &$this->_oModule->_oConfig->CNF;
  413. if(isset($CNF['PRIVACY_FIELD_TO_OBJECT']) && is_array($CNF['PRIVACY_FIELD_TO_OBJECT']))
  414. return $CNF['PRIVACY_FIELD_TO_OBJECT'];
  415. if(empty($aKeysF2O))
  416. $aKeysF2O = array('FIELD_ALLOW_VIEW_TO' => 'OBJECT_PRIVACY_VIEW');
  417. $aResult = array();
  418. foreach($aKeysF2O as $sKeyField => $sKeyObject) {
  419. if(!isset($CNF[$sKeyField]) || !isset($this->aInputs[$CNF[$sKeyField]]) || !isset($CNF[$sKeyObject]))
  420. continue;
  421. $aResult[$CNF[$sKeyField]] = $CNF[$sKeyObject];
  422. }
  423. return $aResult;
  424. }
  425. function _addCssJsPrivacyField($sField, $sPrivacyObject, $bDynamicMode = false)
  426. {
  427. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  428. if(!$oPrivacy)
  429. return;
  430. $this->aInputs[$sField]['content'] = $oPrivacy->addCssJs($bDynamicMode) . $this->aInputs[$sField]['content'];
  431. }
  432. protected function _preparePrivacyField($sField, $sPrivacyObject)
  433. {
  434. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  435. if(!$oPrivacy)
  436. return;
  437. $aSave = array('db' => array('pass' => 'Xss'));
  438. array_walk($this->aInputs[$sField], function ($a, $k, $aSave) {
  439. if (in_array($k, array('info', 'caption', 'value')))
  440. $aSave[0][$k] = $a;
  441. }, array(&$aSave));
  442. $aGroupChooser = $oPrivacy->getGroupChooser($sPrivacyObject, 0, array(
  443. 'object' => $this->aParams['object'],
  444. 'display' => $this->aParams['display']
  445. ));
  446. $this->aInputs[$sField] = array_merge($this->aInputs[$sField], $aGroupChooser, $aSave);
  447. }
  448. protected function _preloadPrivacyField($sField, $sPrivacyObject, $aValues)
  449. {
  450. $CNF = &$this->_oModule->_oConfig->CNF;
  451. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  452. if(!$oPrivacy)
  453. return;
  454. $iContentId = !empty($aValues[$CNF['FIELD_ID']]) ? (int)$aValues[$CNF['FIELD_ID']] : 0;
  455. $iProfileId = !empty($iContentId) ? (int)$this->getContentOwnerProfileId($iContentId) : bx_get_logged_profile_id();
  456. $iGroupId = !empty($aValues[$sField]) ? $aValues[$sField] : 0;
  457. if(!isset($this->aInputs[$sField]['content']))
  458. $this->aInputs[$sField]['content'] = '';
  459. $this->aInputs[$sField]['content'] .= $oPrivacy->initGroupChooser($sPrivacyObject, $iProfileId, array(
  460. 'content_id' => $iContentId,
  461. 'group_id' => $iGroupId,
  462. 'html_ids' => array(
  463. 'form' => $this->getId()
  464. )
  465. ));
  466. }
  467. /**
  468. *
  469. * MultiCategories related methods.
  470. *
  471. */
  472. protected function processMulticatBefore($sFieldName, &$aValsToAdd){
  473. if (isset($this->aInputs[$sFieldName])){
  474. $this->aInputs[$sFieldName]['value'] = array_unique(array_filter($this->aInputs[$sFieldName]['value'], function($sTmp){
  475. return trim($sTmp);
  476. }));
  477. $aValsToAdd[$sFieldName] = implode(',', $this->aInputs[$sFieldName]['value']);
  478. }
  479. }
  480. protected function processMulticatAfter($sFieldName, $iContentId){
  481. $CNF = &$this->_oModule->_oConfig->CNF;
  482. $bAutoActivation = (isset($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) && getParam($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) == 'on') ? true : false;
  483. $oCategories = BxDolCategories::getInstance();
  484. if (isset($this->aInputs[$sFieldName])){
  485. $oCategories->delete($this->_oModule->getName(), $iContentId);
  486. foreach($this->aInputs[$sFieldName]['value'] as $sValue) {
  487. $oCategories->add($this->_oModule->getName(), bx_get_logged_profile_id(), $sValue, $iContentId, $bAutoActivation);
  488. }
  489. }
  490. }
  491. protected function genCustomViewRowValueMulticat(&$aInput)
  492. {
  493. $oCategories = BxDolCategories::getInstance();
  494. $aValues = $oCategories->getData(array('type' => 'by_module_and_object', 'module' => $this->_oModule->getName(), 'object_id' => (!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0 )));
  495. if (count($aValues) > 0){
  496. $aVars = array('bx_repeat:cats' => array());
  497. foreach ($aValues as $sValue) {
  498. $aVars['bx_repeat:cats'][] = array(
  499. 'url' => $oCategories->getUrl($this->_oModule->getName(), $sValue),
  500. 'name' => _t($sValue),
  501. 'bx_if:more' => array(
  502. 'condition' => $sValue === end($aValues) ? false : true,
  503. 'content' => array('1')
  504. ),
  505. );
  506. }
  507. if (!$aVars['bx_repeat:cats'])
  508. return '';
  509. return $this->_oModule->_oTemplate->parseHtmlByName('category_list_inline.html', $aVars);
  510. }
  511. return '';
  512. }
  513. protected function genCustomInputMulticat(&$aInput)
  514. {
  515. $sJsObject = $this->_oModule->_oConfig->getJsObject('categories');
  516. $aValuesForSelect = BxDolCategories::getInstance()->getData(array('type' => 'by_module_and_author', 'module' => $this->_oModule->getName(), 'author' => bx_get_logged_profile_id()));
  517. $aSelectedItems = array();
  518. if (isset($aInput['value']) && is_array($aInput['value']))
  519. $aInput['value'] = array_filter($aInput['value']);
  520. if(!empty($aInput['value'])) {
  521. if (!is_array($aInput['value']))
  522. $aValues = BxDolCategories::getInstance()->getData(array('type' => 'by_module_and_object', 'module' => $this->_oModule->getName(), 'object_id' => (!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0 )));
  523. else
  524. $aValues = $aInput['value'];
  525. $aValues = array_filter($aValues);
  526. foreach($aValues as $sValue) {
  527. if (!array_key_exists($sValue, $aValuesForSelect)){
  528. $aValuesForSelect[$sValue] = array('key' => $sValue, 'value' => $sValue);
  529. }
  530. }
  531. foreach($aValues as $sValue) {
  532. $sInput = $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, $sValue);
  533. $aSelectedItems[] = array('js_object' => $sJsObject, 'select_cat' => $sInput);
  534. }
  535. }
  536. else{
  537. $aSelectedItems = array(
  538. array('js_object' => $sJsObject, 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect))
  539. );
  540. }
  541. return $this->_oModule->_oTemplate->parseHtmlByName('form_categories.html', array(
  542. 'bx_repeat:items' => $aSelectedItems,
  543. 'js_object' => $sJsObject,
  544. 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, -1),
  545. 'input_cat' => $this->genCustomInputMulticatInput($aInput),
  546. 'js_object' => $sJsObject,
  547. 'btn_add' => $this->genCustomInputMulticatButton($aInput),
  548. 'btn_add_new' => $this->genCustomInputMulticatButtonNew($aInput)
  549. ));
  550. }
  551. protected function genCustomInputMulticatSelect($aInput, $aValues, $mixedValue = '')
  552. {
  553. foreach($aValues as $sKey => $aValue) {
  554. $aValues[$sKey]['value'] = _t($aValue['key']);
  555. }
  556. $aValues = array_merge(array('1' => array('key' => false, 'value' => _t('_sys_please_select'))), $aValues);
  557. $aInput['type'] = 'select';
  558. $aInput['name'] .= '[]';
  559. $aInput['value'] = $mixedValue;
  560. $aInput['values'] = $aValues;
  561. return $this->genInput($aInput);
  562. }
  563. protected function genCustomInputMulticatInput($aInput)
  564. {
  565. $aInput['type'] = 'text';
  566. $aInput['name'] .= '[]';
  567. $aInput['value'] = '';
  568. return $this->genInput($aInput);
  569. }
  570. protected function genCustomInputMulticatButton($aInput)
  571. {
  572. $sName = $aInput['name'];
  573. $aInput['type'] = 'button';
  574. $aInput['name'] .= '_add';
  575. $aInput['value'] = _t('_sys_txt_categories_button_caption_add');
  576. $aInput['attrs']['class'] = 'bx-def-margin-right bx-def-margin-sec-top';
  577. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAdd(this, '" . $sName . "');";
  578. return $this->genInputButton($aInput);
  579. }
  580. protected function genCustomInputMulticatButtonNew($aInput)
  581. {
  582. $sName = $aInput['name'];
  583. $aInput['type'] = 'button';
  584. $aInput['name'] .= '_add';
  585. $aInput['value'] = _t('_sys_txt_categories_button_caption_addnew');
  586. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  587. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAddNew(this, '" . $sName . "');";
  588. return $this->genInputButton($aInput);
  589. }
  590. protected function _isMulticatEnabled(){
  591. $CNF = $this->_oModule->_oConfig->CNF;
  592. return isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true && isset($CNF['FIELD_MULTICAT']);
  593. }
  594. }
  595. /** @} */