BxBaseModGeneralFormEntry.php 29 KB

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