1
0

BxBaseModGeneralFormEntry.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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. protected $_bAllowChangeUserForAdmins;
  26. public function __construct($aInfo, $oTemplate = false)
  27. {
  28. if (!isset($this->_bAllowChangeUserForAdmins))
  29. $this->_bAllowChangeUserForAdmins = true;
  30. parent::__construct($aInfo, $oTemplate);
  31. $this->_oModule = BxDolModule::getInstance($this->MODULE);
  32. $this->_aTrackFieldsChanges = array();
  33. $CNF = &$this->_oModule->_oConfig->CNF;
  34. if (isset($CNF['FIELD_ADDED']) && isset($this->aInputs[$CNF['FIELD_ADDED']])) {
  35. $this->aInputs[$CNF['FIELD_ADDED']]['date_filter'] = BX_DATA_INT;
  36. $this->aInputs[$CNF['FIELD_ADDED']]['date_format'] = BX_FORMAT_DATE;
  37. }
  38. if (isset($CNF['FIELD_CHANGED']) && isset($this->aInputs[$CNF['FIELD_CHANGED']])) {
  39. $this->aInputs[$CNF['FIELD_CHANGED']]['date_filter'] = BX_DATA_INT;
  40. $this->aInputs[$CNF['FIELD_CHANGED']]['date_format'] = BX_FORMAT_DATE;
  41. }
  42. if (isset($CNF['FIELD_TEXT']) && isset($this->aInputs[$CNF['FIELD_TEXT']]) && isset($CNF['FIELD_TEXT_ID'])) {
  43. $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] = array_merge (
  44. array ('id' => $CNF['FIELD_TEXT_ID']),
  45. is_array($this->aInputs[$CNF['FIELD_TEXT']]['attrs']) ? $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] : array ()
  46. );
  47. }
  48. if (isset($CNF['FIELD_LABELS']) && isset($this->aInputs[$CNF['FIELD_LABELS']]) && !empty($CNF['OBJECT_METATAGS']) && ($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) && $oMetatags->keywordsIsEnabled()) {
  49. $this->aInputs[$CNF['FIELD_LABELS']]['meta_object'] = $CNF['OBJECT_METATAGS'];
  50. }
  51. if (isset($CNF['FIELD_LOCATION_PREFIX']) && isset($this->aInputs[$CNF['FIELD_LOCATION_PREFIX']])) {
  52. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['manual_input'] = true;
  53. }
  54. // add ability to change author by admins in some apps
  55. if (!empty($CNF['FIELD_AUTHOR']) && ($this->_oModule->_isModerator() || $this->_oModule->_isAdministrator()) && $this->_isChangeUserForAdmins($this->aParams['display']))
  56. $this->aInputs = array_merge([
  57. $CNF['FIELD_AUTHOR'] => [
  58. 'type' => 'custom',
  59. 'name' => $CNF['FIELD_AUTHOR'],
  60. 'db' => ['pass' => 'Xss'],
  61. 'caption' => _t('_sys_form_input_caption_author')
  62. ],
  63. ], $this->aInputs);
  64. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  65. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_object'] = $CNF['OBJECT_STORAGE'];
  66. $this->aInputs[$CNF['FIELD_PHOTO']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_PHOTO']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_PHOTO']]['value']) : $CNF['OBJECT_UPLOADERS'];
  67. $this->aInputs[$CNF['FIELD_PHOTO']]['images_transcoder'] = $CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW'];
  68. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_private'] = 0;
  69. $this->aInputs[$CNF['FIELD_PHOTO']]['multiple'] = true;
  70. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = 0;
  71. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = '';
  72. }
  73. $aPrivacyFields = $this->_getPrivacyFields();
  74. foreach($aPrivacyFields as $sField => $sObject)
  75. $this->_preparePrivacyField($sField, $sObject);
  76. foreach($this->aInputs as $sKey => $aInput) {
  77. if ($aInput['type'] == 'nested_form'){
  78. $this->genGhostTemplateForInputNestedForm ($this->aInputs[$sKey]);
  79. }
  80. }
  81. }
  82. protected function _isChangeUserForAdmins($sDisplay)
  83. {
  84. $CNF = &$this->_oModule->_oConfig->CNF;
  85. if(!$this->_bAllowChangeUserForAdmins)
  86. return false;
  87. if(isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']) && $sDisplay == $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'])
  88. return true;
  89. if(isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT']) && $sDisplay == $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'])
  90. return true;
  91. return false;
  92. }
  93. function getCode($bDynamicMode = false)
  94. {
  95. $CNF = &$this->_oModule->_oConfig->CNF;
  96. if(!$bDynamicMode && bx_is_dynamic_request())
  97. $bDynamicMode = true;
  98. $this->_replaceMarkersInControls();
  99. $sResult = parent::getCode($bDynamicMode);
  100. $aPrivacyFields = $this->_getPrivacyFields();
  101. foreach($aPrivacyFields as $sField => $sObject)
  102. $this->_addCssJsPrivacyField($sField, $sObject, $bDynamicMode);
  103. if(isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true) {
  104. $sInclude2 = '';
  105. $sInclude2 .= $this->_oModule->_oTemplate->addJs(array('BxDolCategories.js'), $bDynamicMode);
  106. $sInclude2 .= $this->_oModule->_oTemplate->addCss(array('categories.css'), $bDynamicMode);
  107. $sResult .= ($bDynamicMode ? $sInclude2 : '') . $this->_oModule->_oTemplate->getJsCode('categories');
  108. }
  109. if(isset($CNF['PARAM_POLL_ENABLED']) && $CNF['PARAM_POLL_ENABLED'] === true)
  110. $sResult .= $this->addCssJsPolls($bDynamicMode) . $this->_oModule->_oTemplate->getJsCode('poll');
  111. return $sResult;
  112. }
  113. /**
  114. * Nested forms processing
  115. */
  116. function genInputNestedForm (&$aInput, $sInfo = '', $sError = '')
  117. {
  118. $sUniqId = genRndPwd (8, false);
  119. $aNestedForms = array();
  120. if (!$this->isSubmitted()){
  121. $aGhostTemplateParams = is_object($aInput['ghost_template']) ? $aInput['ghost_template']->aParams : $aInput['ghost_template']['params'];
  122. $aNestedValues = $this->_oModule->_oDb->getNestedBy(array(
  123. 'type' => 'content_id',
  124. 'id' => $this->_iContentId,
  125. 'key_name' => $aGhostTemplateParams['db']['key']
  126. ), $aGhostTemplateParams['db']['table']);
  127. foreach($aNestedValues as $aNestedValue) {
  128. $aNestedValuesRv = array();
  129. $oForm = $this->getNestedFormObject($aInput);
  130. foreach($aNestedValue as $aNestedKey => $aNestedItem) {
  131. if ($oForm->aParams['db']['key'] == $aNestedKey)
  132. $aNestedValuesRv[$aNestedKey . '[]'] = $aNestedItem;
  133. else
  134. $aNestedValuesRv[$aNestedKey] = $aNestedItem;
  135. }
  136. $oForm->initChecker($aNestedValuesRv);
  137. array_push(
  138. $aNestedForms,
  139. array(
  140. 'key_value' => $aNestedValuesRv[$oForm->aParams['db']['key'] . '[]'],
  141. 'key_name' => $aInput['name'],
  142. 'form_code' => $oForm->genRows(),
  143. 'js_instance_name' => 'oBxNestedForm_' . $sUniqId,
  144. 'nested_type' => $aInput['name'],
  145. )
  146. );
  147. }
  148. }
  149. else{
  150. $aInput['ghost_templates'] = $aInput['ghost_template'];
  151. $this->genGhostTemplateForInputNestedForm ($aInput);
  152. foreach($aInput['ghost_templates'] as $oForm) {
  153. if (isset($oForm->aInputs)){
  154. array_push(
  155. $aNestedForms,
  156. array(
  157. 'key_value' => $oForm->aInputs[$aInput['name']]['value'],
  158. 'key_name' => $aInput['name'],
  159. 'form_code' => $oForm->genRows(),
  160. 'js_instance_name' => 'oBxNestedForm_' . $sUniqId,
  161. 'nested_type' => $aInput['name'],
  162. )
  163. );
  164. }
  165. }
  166. }
  167. return $this->oTemplate->parseHtmlByName('form_field_nested_form.html', array(
  168. 'bx_repeat:items' => $aNestedForms,
  169. 'info' => $sInfo,
  170. 'error' => $sError,
  171. 'uniq_id' => $sUniqId,
  172. 'js_instance_name' => 'oBxNestedForm_' . $sUniqId,
  173. 'options' => json_encode(array(
  174. 'uniq_id' => $sUniqId,
  175. 'js_instance_name' => 'oBxNestedForm_' . $sUniqId,
  176. 'template_ghost' => $this->genGhostTemplate($aInput),
  177. 'form_name' => $aInput['value'],
  178. 'action_uri' => $this->_oModule->_oConfig->getBaseUri(),
  179. 'nested_type' => $aInput['name'],
  180. )),
  181. ));
  182. }
  183. function genNestedForm (&$aInput)
  184. {
  185. $sResult = '';
  186. $aGhostTemplateParams = is_object($aInput['ghost_template']) ? $aInput['ghost_template']->aParams : $aInput['ghost_template']['params'];
  187. $aNestedValues = $this->_oModule->_oDb->getNestedBy([
  188. 'type' => 'content_id',
  189. 'id' => $this->_iContentId,
  190. 'key_name' => $aGhostTemplateParams['db']['key']
  191. ], $aGhostTemplateParams['db']['table']);
  192. foreach($aNestedValues as $aNestedValue) {
  193. $oForm = $this->getNestedFormObject($aInput, true);
  194. $aNestedValuesRv = [];
  195. foreach($aNestedValue as $aNestedKey => $aNestedItem) {
  196. if ($oForm->aParams['db']['key'] == $aNestedKey)
  197. $aNestedValuesRv[$aNestedKey . '[]'] = $aNestedItem;
  198. else
  199. $aNestedValuesRv[$aNestedKey] = $aNestedItem;
  200. }
  201. $oForm->initChecker($aNestedValuesRv);
  202. $sValue = '';
  203. if($aInput['rateable']) {
  204. $iId = BxDolFormQuery::getFormField($this->id, $aInput['name'], $this->_iContentId, $aNestedValuesRv[$oForm->aParams['db']['key'] . '[]']);
  205. $sVoteElement = '';
  206. $sVoteCounter = '';
  207. if(($oVote = BxDolVote::getObjectInstance($aInput['rateable'], $iId)) !== null) {
  208. $sVoteElement = $oVote->getElementInline(['show_counter_empty' => true, 'show_counter' => false, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode]);
  209. $sVoteCounter = $oVote->getCounter(['show_counter_empty' => true, 'show_counter' => true, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode]);
  210. }
  211. $sValue = $this->oTemplate->parseHtmlByName('form_view_rateable_row.html', [
  212. 'value' => $oForm->getCode(),
  213. 'rate_btn' => $sVoteElement,
  214. 'rate' => $sVoteCounter,
  215. ]);
  216. }
  217. else
  218. $sValue = $oForm->getCode();
  219. $sResult .= $this->oTemplate->parseHtmlByName('form_view_nested_row.html', [
  220. 'value' => $sValue
  221. ]);
  222. }
  223. if(!$sResult)
  224. return '';
  225. $aTmplVarsIcon = [];
  226. if(!empty($aInput['icon'])) {
  227. list($sIcon, $sIconUrl, $sIconA, $sIconHtml) = BxTemplFunctions::getInstanceWithTemplate($this->oTemplate)->getIcon($aInput['icon']);
  228. $aTmplVarsIcon = [
  229. 'bx_if:icon' => [
  230. 'condition' => (bool)$sIcon,
  231. 'content' => ['icon' => $sIcon],
  232. ],
  233. 'bx_if:icon-html' => [
  234. 'condition' => (bool)$sIconHtml,
  235. 'content' => ['icon' => $sIconHtml],
  236. ],
  237. 'bx_if:image_inline' => [
  238. 'condition' => false,
  239. 'content' => ['image' => ''],
  240. ]
  241. ];
  242. }
  243. return $this->oTemplate->parseHtmlByName('form_view_row.html', [
  244. 'type' => $aInput['type'],
  245. 'class' => '',
  246. 'bx_if:show_icon' => [
  247. 'condition' => !empty($aTmplVarsIcon),
  248. 'content' => $aTmplVarsIcon
  249. ],
  250. 'caption' => isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '',
  251. 'class_value' => '',
  252. 'value' => $sResult
  253. ]);
  254. }
  255. protected function genGhostTemplateForInputNestedForm (&$aInput)
  256. {
  257. $oForm = $this->getNestedFormObject($aInput);
  258. $aInput['ghost_template'] = array (
  259. 'params' => array(
  260. 'nested_form_template' => 'form_field_nested_form_wrapper.html',
  261. 'db' => array(
  262. 'table' => $oForm->aParams['db']['table'],
  263. 'key' => $oForm->aParams['db']['key'],
  264. 'submit_name' => $oForm->aParams['db']['key'],
  265. ),
  266. ),
  267. 'inputs' => $oForm->aInputs
  268. );
  269. }
  270. function getNestedFormObject (&$aInput, $bViewMode = false)
  271. {
  272. $oForm = BxDolForm::getObjectInstance($aInput['value'], $aInput['value'] . ($bViewMode ? '_view' : ''));
  273. foreach($oForm->aInputs as $sKey => $aInput2) {
  274. $sName = $oForm->aInputs[$sKey]['name'];
  275. if (strpos($sName,'[]') === false)
  276. $oForm->aInputs[$sKey]['name'] = $sName . '[]';
  277. }
  278. return $oForm;
  279. }
  280. /**
  281. * Add field(s) which will be tracked during content update.
  282. */
  283. public function addTrackFields($mixedFields, $mixedContent)
  284. {
  285. if(empty($mixedContent) || empty($mixedFields))
  286. return;
  287. if(!is_array($mixedContent)) {
  288. $mixedContent = $this->_oModule->_oDb->getContentInfoById((int)$mixedContent);
  289. if(empty($mixedContent) || !is_array($mixedContent))
  290. return;
  291. }
  292. if(!is_array($mixedFields))
  293. $mixedFields = array($mixedFields);
  294. foreach($mixedFields as $sField) {
  295. if(!isset($mixedContent[$sField]))
  296. continue;
  297. $this->_aTrackFieldsChanges[$sField] = array(
  298. 'old' => $mixedContent[$sField],
  299. );
  300. }
  301. }
  302. /**
  303. * Checks if the field was changed.
  304. */
  305. public function isTrackFieldChanged($sField, $bReturnValues = false)
  306. {
  307. if(!isset($this->_aTrackFieldsChanges[$sField]) || $this->_aTrackFieldsChanges[$sField] === false)
  308. return false;
  309. return $bReturnValues ? $this->_aTrackFieldsChanges[$sField] : true;
  310. }
  311. function initChecker ($aValues = array (), $aSpecificValues = array())
  312. {
  313. $CNF = &$this->_oModule->_oConfig->CNF;
  314. $bValues = $aValues && !empty($aValues['id']);
  315. $this->_iContentId = isset($aValues['id']) ? $aValues['id'] : false;
  316. if (!empty($CNF['URI_VIEW_ENTRY']) && $this->_iContentId)
  317. $this->addMarkers([
  318. 'edit_cancel_url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $this->_iContentId))
  319. ]);
  320. 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())
  321. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['value'] = $oMetatags->locationsString($aValues[$CNF['FIELD_ID']], false);
  322. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  323. $aContentInfo = false;
  324. if ($aValues && !empty($aValues['id'])) {
  325. $aContentInfo = $this->_oModule->_oDb->getContentInfoById ($aValues['id']);
  326. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = $aValues['id'];
  327. }
  328. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplate, $this->_getPhotoGhostTmplVars($aContentInfo));
  329. }
  330. if (isset($CNF['FIELD_POLL']) && isset($this->aInputs[$CNF['FIELD_POLL']])) {
  331. if ($bValues)
  332. $this->aInputs[$CNF['FIELD_POLL']]['content_id'] = $aValues['id'];
  333. }
  334. 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'])) {
  335. $this->aInputs[$CNF['FIELD_LABELS']]['content_id'] = $aValues['id'];
  336. if(($aLabels = $oMetatags->keywordsGet($aValues['id'])))
  337. $this->aInputs[$CNF['FIELD_LABELS']]['value'] = array_intersect($aLabels, BxDolLabel::getInstance()->getLabels(array('type' => 'values')));
  338. }
  339. if (isset($CNF['FIELD_ANONYMOUS'], $this->aInputs[$CNF['FIELD_ANONYMOUS']]) && isset($CNF['FIELD_AUTHOR'], $aValues[$CNF['FIELD_AUTHOR']])) {
  340. $this->aInputs[$CNF['FIELD_ANONYMOUS']]['checked'] = $aValues[$CNF['FIELD_AUTHOR']] < 0;
  341. }
  342. if (isset($CNF['FIELD_MULTICAT']) && isset($this->aInputs[$CNF['FIELD_MULTICAT']]) && $CNF['PARAM_MULTICAT_ENABLED']) {
  343. if ($bValues)
  344. $this->aInputs[$CNF['FIELD_MULTICAT']]['content_id'] = $aValues['id'];
  345. }
  346. $aPrivacyFields = $this->_getPrivacyFields();
  347. foreach($aPrivacyFields as $sField => $sObject)
  348. $this->_preloadPrivacyField($sField, $sObject, $aValues);
  349. if(isset($CNF['FIELD_AUTHOR'], $this->aInputs[$CNF['FIELD_AUTHOR']])) {
  350. if (isset($aValues[$CNF['FIELD_AUTHOR']]) && $aValues[$CNF['FIELD_AUTHOR']])
  351. $aValues[$CNF['FIELD_AUTHOR']] = abs($aValues[$CNF['FIELD_AUTHOR']]);
  352. else
  353. $aValues[$CNF['FIELD_AUTHOR']] = bx_get_logged_profile_id();
  354. }
  355. parent::initChecker ($aValues, $aSpecificValues);
  356. $this->_processContextBeforeAdd();
  357. foreach($aPrivacyFields as $sField => $sObject)
  358. $this->_validatePrivacyField($sField, $sObject, $aValues);
  359. }
  360. public function insert ($aValsToAdd = array(), $isIgnore = false)
  361. {
  362. $CNF = &$this->_oModule->_oConfig->CNF;
  363. $iAuthor = -1;
  364. if(isset($CNF['FIELD_AUTHOR'])) {
  365. $sFa = $CNF['FIELD_AUTHOR'];
  366. $iAnonymous = isset($CNF['FIELD_ANONYMOUS'], $this->aInputs[$CNF['FIELD_ANONYMOUS']]) && $this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1;
  367. if(empty($aValsToAdd[$sFa])) {
  368. if(!isset($this->aInputs[$sFa]) || empty($this->getCleanValue($sFa)))
  369. $iAuthor = bx_get_logged_profile_id();
  370. else
  371. $iAuthor = $this->getCleanValue($sFa);
  372. $aValsToAdd[$sFa] = $iAnonymous * $iAuthor;
  373. }
  374. else {
  375. $iAuthor = $aValsToAdd[$sFa];
  376. $aValsToAdd[$sFa] *= $iAnonymous;
  377. }
  378. }
  379. if (isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']]) && empty($this->getCleanValue($CNF['FIELD_ADDED'])))
  380. $aValsToAdd[$CNF['FIELD_ADDED']] = time();
  381. if (isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  382. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  383. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb() && isset($CNF['FIELD_THUMB'])) {
  384. $aThumb = isset($_POST[$CNF['FIELD_THUMB']]) ? bx_process_input ($_POST[$CNF['FIELD_THUMB']], BX_DATA_INT) : false;
  385. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  386. if (!empty($aThumb)){
  387. if(is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  388. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  389. else
  390. $aValsToAdd[$CNF['FIELD_THUMB']] = $aThumb;
  391. }
  392. }
  393. if(!empty($CNF['OBJECT_METATAGS']))
  394. $this->_processMetas($aValsToAdd);
  395. if(isset($CNF['FIELD_STATUS']) && !empty($CNF['FIELDS_DELAYED_PROCESSING']) && !empty($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4'])) {
  396. $oTranscoder = BxDolTranscoder::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  397. $aFields = $CNF['FIELDS_DELAYED_PROCESSING'];
  398. if(is_string($aFields))
  399. $aFields = explode(',', $aFields);
  400. foreach($aFields as $sField) {
  401. $mixedFieldValues = $this->getCleanValue($sField);
  402. if(empty($mixedFieldValues))
  403. continue;
  404. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sField]['storage_object']);
  405. if(!$oStorage)
  406. continue;
  407. if(!is_array($mixedFieldValues))
  408. $mixedFieldValues = array($mixedFieldValues);
  409. foreach($mixedFieldValues as $mixedFieldValue) {
  410. $aInfo = $oStorage->getFile((int)$mixedFieldValue);
  411. if(empty($aInfo) || !is_array($aInfo))
  412. continue;
  413. if(strncmp($aInfo['mime_type'], 'video/', 6) != 0)
  414. continue;
  415. if($oTranscoder->isFileReady((int)$mixedFieldValue))
  416. continue;
  417. $aValsToAdd[$CNF['FIELD_STATUS']] = 'awaiting';
  418. break 2;
  419. }
  420. }
  421. }
  422. if(isset($CNF['FIELD_STATUS_ADMIN']) && empty($aValsToAdd[$CNF['FIELD_STATUS_ADMIN']]) && !$this->_oModule->_isModerator() && !$this->_oModule->_oConfig->isAutoApproveEnabled())
  423. $aValsToAdd[$CNF['FIELD_STATUS_ADMIN']] = BX_BASE_MOD_GENERAL_STATUS_PENDING;
  424. $bMulticatEnabled = $this->_isMulticatEnabled();
  425. if ($bMulticatEnabled)
  426. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  427. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  428. if(empty($iContentId))
  429. return $iContentId;
  430. foreach($this->aInputs as $aInput) {
  431. if ($aInput['type'] == 'nested_form') {
  432. if (is_array($aInput['ghost_template']) && !isset($aInput['ghost_template']['inputs'])) {
  433. foreach ($aInput['ghost_template'] as $oFormNested) {
  434. $iNestedContentId = $oFormNested->insert(array('content_id' => $iContentId));
  435. if ($aInput['rateable']) {
  436. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName(), $iNestedContentId);
  437. }
  438. }
  439. }
  440. }
  441. }
  442. if($this->_bIsApi && ($sKeyF = 'FIELD_ALLOW_VIEW_TO') && isset($CNF[$sKeyF]) && ($mixedAllowViewTo = $this->getCleanValue($CNF[$sKeyF])) !== false) {
  443. $aAllowViewToItems = [];
  444. if(($mixedAllowViewToItems = $this->getCleanValue($CNF[$sKeyF] . '_items')) !== false)
  445. $aAllowViewToItems = explode(',', $mixedAllowViewToItems);
  446. if(($sKeyO = 'OBJECT_PRIVACY_VIEW') && isset($CNF[$sKeyO]) && ($sObject = $CNF[$sKeyO]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($sObject)) !== false)
  447. $oPrivacy->addGroupCustom([
  448. 'profile_id' => $iAuthor,
  449. 'content_id' => 0,
  450. 'object' => $sObject,
  451. 'group_id' => (int)$mixedAllowViewTo
  452. ], $aAllowViewToItems);
  453. }
  454. if($bMulticatEnabled)
  455. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  456. foreach($this->aInputs as $aInput)
  457. if(isset($aInput['rateable']) && $aInput['rateable'] && $aInput['type'] != 'nested_form')
  458. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName());
  459. if(($iContextNid = $this->getCleanValue('context_nid')) !== false)
  460. $this->_processContextAfterAdd($iContentId, $iContextNid, (int)$this->getCleanValue('context_usage'));
  461. return $iContentId;
  462. }
  463. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  464. {
  465. $CNF = &$this->_oModule->_oConfig->CNF;
  466. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  467. $iAuthor = -1;
  468. if(isset($CNF['FIELD_AUTHOR'])) {
  469. $sFa = $CNF['FIELD_AUTHOR'];
  470. if(!empty($aValsToAdd[$sFa]))
  471. $iAuthor = $aValsToAdd[$sFa];
  472. else if(isset($this->aInputs[$sFa]) && !empty($this->getCleanValue($sFa)))
  473. $iAuthor = $this->getCleanValue($sFa);
  474. else if(!empty($aContentInfo[$sFa]))
  475. $iAuthor = $aContentInfo[$sFa];
  476. else
  477. $iAuthor = bx_get_logged_profile_id();
  478. $iAuthor = abs($iAuthor);
  479. $iAnonymous = isset($CNF['FIELD_ANONYMOUS'], $this->aInputs[$CNF['FIELD_ANONYMOUS']]) && $this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1;
  480. $aValsToAdd[$sFa] = $iAnonymous * $iAuthor;
  481. }
  482. if(isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  483. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  484. if(CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($iContentId) && isset($CNF['FIELD_THUMB']) && ($aThumb = bx_get($CNF['FIELD_THUMB'])) !== false) {
  485. $aThumb = bx_process_input($aThumb, BX_DATA_INT);
  486. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  487. if(!empty($aThumb)) {
  488. if(is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  489. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  490. else
  491. $aValsToAdd[$CNF['FIELD_THUMB']] = $aThumb;
  492. }
  493. }
  494. if(!empty($CNF['OBJECT_METATAGS']))
  495. $this->_processMetas($aValsToAdd);
  496. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  497. $aValsToAdd[$CNF['FIELD_STATUS']] = 'active';
  498. $bMulticatEnabled = $this->_isMulticatEnabled();
  499. if ($bMulticatEnabled)
  500. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  501. $mixedResult = parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  502. foreach($this->aInputs as $aInput) {
  503. if ($aInput['type'] == 'nested_form'){
  504. if (is_array($aInput['ghost_template']) && !isset($aInput['ghost_template']['inputs'])) {
  505. foreach ($aInput['ghost_template'] as $oFormNested) {
  506. $aSpecificValues = $oFormNested->getSpecificValues();
  507. $iNestedContentId = $oFormNested->getSubmittedValue($aInput['name'], BX_DOL_FORM_METHOD_SPECIFIC, $aSpecificValues);
  508. if (empty($iNestedContentId)){
  509. $iNestedContentId = $oFormNested->insert(array('content_id' => $iContentId));
  510. if ($aInput['rateable']){
  511. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName(), $iNestedContentId);
  512. }
  513. }
  514. else{
  515. $oFormNested->update($iNestedContentId, array('content_id' => $iContentId));
  516. }
  517. }
  518. }
  519. }
  520. }
  521. if ($bMulticatEnabled)
  522. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  523. foreach($this->aInputs as $aInput) {
  524. if (isset($aInput['rateable']) && $aInput['rateable'] && $aInput['type'] != 'nested_form'){
  525. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName());
  526. }
  527. }
  528. if($mixedResult !== false) {
  529. $this->_processTrackFields($iContentId);
  530. if($this->_bIsApi && ($sKeyF = 'FIELD_ALLOW_VIEW_TO') && isset($CNF[$sKeyF]) && ($mixedAllowViewTo = $this->getCleanValue($CNF[$sKeyF])) !== false) {
  531. $aAllowViewToItems = [];
  532. if(($mixedAllowViewToItems = $this->getCleanValue($CNF[$sKeyF] . '_items')) !== false)
  533. $aAllowViewToItems = explode(',', $mixedAllowViewToItems);
  534. if(($sKeyO = 'OBJECT_PRIVACY_VIEW') && isset($CNF[$sKeyO]) && ($sObject = $CNF[$sKeyO]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($sObject)) !== false) {
  535. $oPrivacy->editGroupCustom([
  536. 'profile_id' => $iAuthor,
  537. 'content_id' => $iContentId,
  538. 'object' => $sObject,
  539. 'group_id' => (int)$mixedAllowViewTo
  540. ], $aAllowViewToItems);
  541. }
  542. }
  543. }
  544. return $mixedResult;
  545. }
  546. function getHtmlEditorQueryParams($aInput)
  547. {
  548. $aQueryParams = parent::getHtmlEditorQueryParams($aInput);
  549. if (isset($this->MODULE)){
  550. $aQueryParams['m'] = $this->MODULE;
  551. }
  552. if (isset($this->_iContentId) && $this->_iContentId){
  553. $aQueryParams['cid'] = $this->_iContentId;
  554. }
  555. $aQueryParams['fi'] = '';
  556. /**
  557. * @hooks
  558. * @hookdef hook-system-editor_query_params 'system', 'editor_query_params' - hook to override http(s) request's query params, which is used in HTML editor
  559. * - $unit_name - equals `system`
  560. * - $action - equals `editor_query_params`
  561. * - $object_id - not used
  562. * - $sender_id - not used
  563. * - $extra_params - array of additional params with the following array keys:
  564. * - `form` - [object] an instance of form, @see BxDolForm
  565. * - `override_result` - [array] by ref, query string params, can be overridden in hook processing
  566. * @hook @ref hook-system-editor_query_params
  567. */
  568. bx_alert('system', 'editor_query_params', 0, 0, [
  569. 'form' => $this,
  570. 'override_result' => &$aQueryParams
  571. ]);
  572. return $aQueryParams;
  573. }
  574. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  575. {
  576. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  577. }
  578. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  579. {
  580. $CNF = &$this->_oModule->_oConfig->CNF;
  581. return array (
  582. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  583. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  584. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  585. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  586. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  587. 'bx_if:set_thumb' => array (
  588. 'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($this->aInputs[$CNF['FIELD_PHOTO']]['content_id']),
  589. 'content' => array (
  590. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  591. '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')
  592. ),
  593. ),
  594. );
  595. }
  596. function delete ($iContentId, $aContentInfo = array())
  597. {
  598. $CNF = &$this->_oModule->_oConfig->CNF;
  599. // delete associated files
  600. $a = array('OBJECT_STORAGE', 'OBJECT_STORAGE_FILES', 'OBJECT_STORAGE_PHOTOS', 'OBJECT_STORAGE_VIDEOS', 'OBJECT_STORAGE_SOUNDS');
  601. foreach ($a as $k) {
  602. if (!empty($CNF[$k])) {
  603. $oStorage = BxDolStorage::getObjectInstance($CNF[$k]);
  604. if ($oStorage)
  605. $oStorage->queueFilesForDeletionFromGhosts($aContentInfo[$CNF['FIELD_AUTHOR']], $iContentId);
  606. }
  607. }
  608. // delete associated objects data
  609. if (!empty($CNF['OBJECT_VIEWS'])) {
  610. $o = BxDolView::getObjectInstance($CNF['OBJECT_VIEWS'], $iContentId);
  611. if ($o) $o->onObjectDelete();
  612. }
  613. if (!empty($CNF['OBJECT_VOTES'])) {
  614. $o = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $iContentId);
  615. if ($o) $o->onObjectDelete();
  616. }
  617. if (!empty($CNF['OBJECT_SCORES'])) {
  618. $o = BxDolScore::getObjectInstance($CNF['OBJECT_SCORES'], $iContentId);
  619. if ($o) $o->onObjectDelete();
  620. }
  621. if (!empty($CNF['OBJECT_COMMENTS'])) {
  622. $o = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  623. if ($o) $o->onObjectDelete();
  624. }
  625. if (!empty($CNF['OBJECT_REPORTS'])) {
  626. $o = BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS'], $iContentId);
  627. if ($o) $o->onObjectDelete();
  628. }
  629. if (!empty($CNF['OBJECT_METATAGS'])) {
  630. $o = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  631. if ($o) $o->onDeleteContent($iContentId);
  632. }
  633. // delete SEO links
  634. BxDolPage::deleteSeoLink ($this->_oModule->getName(), $this->_oModule->getName(), $iContentId);
  635. // delete db record
  636. BxDolFormQuery::removeFormField($this->id, $iContentId);
  637. return parent::delete($iContentId);
  638. }
  639. protected function _isAdmin ($iContentId = 0)
  640. {
  641. return $this->_oModule->_isModerator();
  642. }
  643. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  644. {
  645. $CNF = &$this->_oModule->_oConfig->CNF;
  646. if (!isset($this->aInputs[$sFieldFile]))
  647. return true;
  648. $mixedFileIds = $this->getCleanValue($sFieldFile);
  649. if(!$mixedFileIds)
  650. return true;
  651. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  652. if (!$oStorage)
  653. return false;
  654. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  655. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true, $this->_isAdmin($iContentId));
  656. if(!empty($aGhostFiles) && is_array($aGhostFiles))
  657. foreach($aGhostFiles as $aFile) {
  658. if(is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  659. continue;
  660. if($aFile['private'])
  661. $oStorage->setFilePrivate ($aFile['id'], 1);
  662. if($iContentId)
  663. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  664. }
  665. if($this->_bAllowChangeUserForAdmins && ($this->_oModule->_isModerator() || $this->_oModule->_isAdministrator()) && ($iLoggedId = bx_get_logged_profile_id()) != $iProfileId) {
  666. $aGhostFiles = $oStorage->getGhosts($iLoggedId, $isAssociateWithContent ? 0 : $iContentId, true, $this->_isAdmin($iContentId));
  667. if(!empty($aGhostFiles) && is_array($aGhostFiles))
  668. foreach($aGhostFiles as $aFile) {
  669. if(is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  670. continue;
  671. if($aFile['private'])
  672. $oStorage->setFilePrivate ($aFile['id'], 1);
  673. if($iContentId)
  674. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  675. }
  676. }
  677. return true;
  678. }
  679. public function processPolls ($sFieldPoll, $iContentId = 0)
  680. {
  681. $CNF = &$this->_oModule->_oConfig->CNF;
  682. if (!isset($this->aInputs[$sFieldPoll]))
  683. return true;
  684. $aPollIds = $this->getCleanValue($sFieldPoll);
  685. if(empty($aPollIds) || !is_array($aPollIds))
  686. return true;
  687. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  688. $aPollsDbIds = $this->_oModule->_oDb->getPolls(array('type' => 'content_id_ids', 'content_id' => $iContentId));
  689. //--- Remove deleted
  690. $this->_oModule->_oDb->deletePollsByIds(array_diff($aPollsDbIds, $aPollIds));
  691. //--- Add new
  692. if($iContentId) {
  693. $aPollsAddIds = array_diff($aPollIds, $aPollsDbIds);
  694. foreach($aPollsAddIds as $iPollId)
  695. $this->_oModule->_oDb->updatePolls(array($CNF['FIELD_POLL_CONTENT_ID'] => $iContentId), array($CNF['FIELD_POLL_ID'] => $iPollId, $CNF['FIELD_POLL_CONTENT_ID'] => 0));
  696. }
  697. return true;
  698. }
  699. function _deleteFile ($iFileId, $sStorage = '')
  700. {
  701. $CNF = &$this->_oModule->_oConfig->CNF;
  702. if (!$iFileId)
  703. return true;
  704. $sStorage = !empty($sStorage) ? $sStorage : $CNF['OBJECT_STORAGE'];
  705. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  706. if (!$oStorage)
  707. return false;
  708. if (!$oStorage->getFile($iFileId))
  709. return true;
  710. $iProfileId = bx_get_logged_profile_id();
  711. return $oStorage->deleteFile($iFileId, $iProfileId);
  712. }
  713. function addCssJs ()
  714. {
  715. if ((!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) && !self::$_isCssJsGeneralModuleAdded) {
  716. $this->_oModule->_oTemplate->addCss('forms.css');
  717. $this->_oModule->_oTemplate->addJs('modules/base/general/js/|forms.js');
  718. self::$_isCssJsGeneralModuleAdded = true;
  719. }
  720. return parent::addCssJs ();
  721. }
  722. function addCssJsPolls($bDynamicMode = false)
  723. {
  724. $sInclude = '';
  725. $sInclude .= $this->_oModule->_oTemplate->addCss(['polls.css'], $bDynamicMode);
  726. $sInclude .= $this->_oModule->_oTemplate->addJs(['modules/base/general/js/|polls.js'], $bDynamicMode);
  727. return $bDynamicMode ? $sInclude : '';
  728. }
  729. function genViewRowValue(&$aInput)
  730. {
  731. $s = parent::genViewRowValue($aInput);
  732. if ($this->_oMetatagsObject && in_array($aInput['name'], $this->_aMetatagsFieldsWithKeywords) && $s)
  733. $s = $this->_oMetatagsObject->metaParse($this->_oMetatagsContentId, $s);
  734. return $s;
  735. }
  736. function genViewRowWrapped(&$aInput)
  737. {
  738. $sResult = parent::genViewRowWrapped($aInput);
  739. if(!$aInput['rateable'])
  740. return $sResult;
  741. $sValue = $this->genViewRowValue($aInput);
  742. if(null === $sValue)
  743. return '';
  744. // process rateable fields
  745. $iId = BxDolFormQuery::getFormField($this->id, $aInput['name'], $this->_iContentId);
  746. if(($oVote = BxDolVote::getObjectInstance($aInput['rateable'], $iId)) !== null) {
  747. $sVoteElement = $oVote->getElementInline(['show_counter_empty' => true, 'show_counter' => false, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode]);
  748. $sVoteCounter = $oVote->getCounter(['show_counter_empty' => true, 'show_counter' => true, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode]);
  749. return $this->oTemplate->parseHtmlByName('form_view_rateable_row.html', [
  750. 'value' => $sResult,
  751. 'rate_btn' => $sVoteElement,
  752. 'rate' => $sVoteCounter,
  753. ]);
  754. }
  755. return $sResult;
  756. }
  757. function setMetatagsKeywordsData($iId, $a, $o)
  758. {
  759. $this->_oMetatagsContentId = $iId;
  760. $this->_aMetatagsFieldsWithKeywords = $a;
  761. $this->_oMetatagsObject = $o;
  762. }
  763. function getContentOwnerProfileId ($iContentId)
  764. {
  765. return $this->_oModule->serviceGetContentOwnerProfileId($iContentId);
  766. }
  767. protected function _processTrackFields($mixedContent)
  768. {
  769. if(empty($mixedContent) || empty($this->_aTrackFieldsChanges))
  770. return;
  771. if(!is_array($mixedContent)) {
  772. $mixedContent = $this->_oModule->_oDb->getContentInfoById((int)$mixedContent);
  773. if(empty($mixedContent) || !is_array($mixedContent))
  774. return;
  775. }
  776. foreach($this->_aTrackFieldsChanges as $sField => $aValues)
  777. if($mixedContent[$sField] == $aValues['old'])
  778. $this->_aTrackFieldsChanges[$sField] = false;
  779. else
  780. $this->_aTrackFieldsChanges[$sField]['new'] = $mixedContent[$sField];
  781. }
  782. protected function _processMetas(&$aValsToAdd)
  783. {
  784. $CNF = &$this->_oModule->_oConfig->CNF;
  785. $sKey = 'FIELD_LABELS';
  786. if(isset($CNF[$sKey]) && isset($this->aInputs[$CNF[$sKey]]) && empty($aValsToAdd[$CNF[$sKey]])) {
  787. $aLabels = $this->getCleanValue($CNF[$sKey]);
  788. if(!empty($aLabels) && is_array($aLabels))
  789. $aValsToAdd[$CNF[$sKey]] = serialize($aLabels);
  790. else
  791. $aValsToAdd[$CNF[$sKey]] = '';
  792. }
  793. $sKey1 = 'FIELD_LOCATION';
  794. $sKey2 = 'FIELD_LOCATION_PREFIX';
  795. if(isset($CNF[$sKey1]) && isset($CNF[$sKey2]) && isset($this->aInputs[$CNF[$sKey1]]) && empty($aValsToAdd[$CNF[$sKey1]])) {
  796. $aLocation = BxDolMetatags::locationsRetrieveFromForm($CNF[$sKey2], $this);
  797. if(!empty($aLocation) && is_array($aLocation))
  798. $aValsToAdd[$CNF[$sKey1]] = serialize(BxDolMetatags::locationsParseComponents($aLocation));
  799. }
  800. }
  801. protected function _getPrivacyFields($aKeysF2O = array())
  802. {
  803. $CNF = &$this->_oModule->_oConfig->CNF;
  804. if(isset($CNF['PRIVACY_FIELD_TO_OBJECT']) && is_array($CNF['PRIVACY_FIELD_TO_OBJECT']))
  805. return $CNF['PRIVACY_FIELD_TO_OBJECT'];
  806. if(empty($aKeysF2O))
  807. $aKeysF2O = array('FIELD_ALLOW_VIEW_TO' => 'OBJECT_PRIVACY_VIEW');
  808. $aResult = array();
  809. foreach($aKeysF2O as $sKeyField => $sKeyObject) {
  810. if(!isset($CNF[$sKeyField]) || !isset($this->aInputs[$CNF[$sKeyField]]) || !isset($CNF[$sKeyObject]))
  811. continue;
  812. $aResult[$CNF[$sKeyField]] = $CNF[$sKeyObject];
  813. }
  814. return $aResult;
  815. }
  816. function _addCssJsPrivacyField($sField, $sPrivacyObject, $bDynamicMode = false)
  817. {
  818. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  819. if(!$oPrivacy)
  820. return;
  821. $this->aInputs[$sField]['content'] = $oPrivacy->addCssJs($bDynamicMode) . $this->aInputs[$sField]['content'];
  822. }
  823. protected function _preparePrivacyField($sField, $sPrivacyObject)
  824. {
  825. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  826. if(!$oPrivacy)
  827. return;
  828. $aSave = array('db' => array('pass' => 'Xss'));
  829. array_walk($this->aInputs[$sField], function ($a, $k, $aSave) {
  830. if (in_array($k, array('info', 'caption', 'value')))
  831. $aSave[0][$k] = $a;
  832. }, array(&$aSave));
  833. $aGroupChooser = $oPrivacy->getGroupChooser($sPrivacyObject, 0, array(
  834. 'object' => $this->aParams['object'],
  835. 'display' => $this->aParams['display']
  836. ));
  837. if (bx_get('context') && isset($aGroupChooser['values'])) {
  838. $bFound = false;
  839. foreach ($aGroupChooser['values'] as $k => $r) {
  840. if (!isset($r['key']) || abs((int)bx_get('context')) !== abs((int)$r['key']))
  841. continue;
  842. $bFound = true;
  843. break;
  844. }
  845. if ($bFound) {
  846. $aSave['type'] = 'hidden';
  847. $aSave['value'] = -abs((int)bx_get('context'));
  848. }
  849. }
  850. $this->aInputs[$sField] = array_merge($this->aInputs[$sField], $aGroupChooser, $aSave);
  851. }
  852. protected function _preloadPrivacyField($sField, $sPrivacyObject, $aValues)
  853. {
  854. $CNF = &$this->_oModule->_oConfig->CNF;
  855. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  856. if(!$oPrivacy)
  857. return;
  858. $iContentId = !empty($aValues[$CNF['FIELD_ID']]) ? (int)$aValues[$CNF['FIELD_ID']] : 0;
  859. $iProfileId = !empty($iContentId) ? (int)$this->getContentOwnerProfileId($iContentId) : bx_get_logged_profile_id();
  860. $iGroupId = !empty($aValues[$sField]) ? $aValues[$sField] : 0;
  861. if(!$this->_bIsApi) {
  862. if(!isset($this->aInputs[$sField]['content']))
  863. $this->aInputs[$sField]['content'] = '';
  864. $this->aInputs[$sField]['content'] .= $oPrivacy->initGroupChooser($sPrivacyObject, $iProfileId, [
  865. 'content_id' => $iContentId,
  866. 'group_id' => $iGroupId,
  867. 'html_ids' => [
  868. 'form' => $this->getId()
  869. ]
  870. ]);
  871. }
  872. else
  873. $this->aInputs[$sField]['subvalue'] = $oPrivacy->initGroupChooserAPI($sPrivacyObject, $iProfileId, [
  874. 'content_id' => $iContentId,
  875. 'group_id' => $iGroupId,
  876. ]);
  877. }
  878. protected function _validatePrivacyField($sField, $sPrivacyObject, $aValues)
  879. {
  880. $mixedValue = $this->aInputs[$sField]['value'];
  881. $bValue = false;
  882. foreach($this->aInputs[$sField]['values'] as $aValue)
  883. if(isset($aValue['key']) && $aValue['key'] == $mixedValue) {
  884. $bValue = true;
  885. break;
  886. }
  887. if(!$bValue) {
  888. $sTitle = '';
  889. if(is_numeric($mixedValue) && (int)$mixedValue < 0 && ($oContext = BxDolProfile::getInstance(abs((int)$mixedValue))) !== false)
  890. $sTitle = $oContext->getDisplayName();
  891. else
  892. $sTitle = _t('_sys_ps_group_title_unknown');
  893. $this->aInputs[$sField]['values'] = array_merge(array(
  894. array('key' => $mixedValue, 'value' => $sTitle),
  895. array('key' => '', 'value' => '----', 'attrs' => array('disabled' => 'disabled'))
  896. ), $this->aInputs[$sField]['values']);
  897. }
  898. }
  899. protected function _processContextBeforeAdd()
  900. {
  901. $CNF = &$this->_oModule->_oConfig->CNF;
  902. $sKey = 'FIELD_ALLOW_VIEW_TO';
  903. if(!isset($CNF[$sKey], $this->aInputs[$CNF[$sKey]]))
  904. return;
  905. if(($iContextId = bx_get('context_pid')) !== false)
  906. $this->aInputs[$CNF[$sKey]] = array_merge($this->aInputs[$CNF[$sKey]], [
  907. 'type' => 'hidden',
  908. 'value' => -abs((int)$iContextId)
  909. ]);
  910. $sKey = 'context_nid';
  911. if(($iNodeId = bx_get($sKey)) !== false && !isset($this->aInputs[$sKey])) {
  912. $this->aInputs[$sKey] = [
  913. 'name' => $sKey,
  914. 'type' => 'hidden',
  915. 'value' => (int)$iNodeId
  916. ];
  917. }
  918. $sKey = 'context_usage';
  919. if(($iUsage = bx_get($sKey)) !== false && !isset($this->aInputs[$sKey])) {
  920. $this->aInputs[$sKey] = [
  921. 'name' => $sKey,
  922. 'type' => 'hidden',
  923. 'value' => (int)$iUsage
  924. ];
  925. }
  926. }
  927. protected function _processContextAfterAdd($iContentId, $iContextNid, $iContextUsage = 0)
  928. {
  929. $CNF = &$this->_oModule->_oConfig->CNF;
  930. $sKey = 'FIELD_ALLOW_VIEW_TO';
  931. if(!isset($CNF[$sKey]))
  932. return;
  933. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  934. if(!is_numeric($aContentInfo[$CNF[$sKey]]) || (int)$aContentInfo[$CNF[$sKey]] > 0)
  935. return;
  936. $iContextPid = abs($aContentInfo[$CNF[$sKey]]);
  937. $oContext = BxDolProfile::getInstance($iContextPid);
  938. if(!$oContext)
  939. return;
  940. $sModule = $oContext->getModule();
  941. $sMethod = 'on_content_added';
  942. if(bx_is_srv($sModule, $sMethod))
  943. bx_srv($sModule, $sMethod, [$this->MODULE, $iContentId, $oContext->getContentId(), $iContextNid, $iContextUsage]);
  944. }
  945. /**
  946. *
  947. * MultiCategories related methods.
  948. *
  949. */
  950. protected function processMulticatBefore($sFieldName, &$aValsToAdd){
  951. if (isset($this->aInputs[$sFieldName])){
  952. $this->aInputs[$sFieldName]['value'] = array_unique(array_filter($this->aInputs[$sFieldName]['value'], function($sTmp){
  953. return trim($sTmp);
  954. }));
  955. $aValsToAdd[$sFieldName] = implode(',', $this->aInputs[$sFieldName]['value']);
  956. }
  957. }
  958. protected function processMulticatAfter($sFieldName, $iContentId){
  959. $CNF = &$this->_oModule->_oConfig->CNF;
  960. $bAutoActivation = (isset($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) && getParam($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) == 'on') ? true : false;
  961. $oCategories = BxDolCategories::getInstance();
  962. if (isset($this->aInputs[$sFieldName])){
  963. $oCategories->delete($this->_oModule->getName(), $iContentId);
  964. foreach($this->aInputs[$sFieldName]['value'] as $sValue) {
  965. $oCategories->add($this->_oModule->getName(), bx_get_logged_profile_id(), $sValue, $iContentId, $bAutoActivation);
  966. }
  967. }
  968. }
  969. protected function genCustomViewRowValueMulticat(&$aInput)
  970. {
  971. $oCategories = BxDolCategories::getInstance();
  972. $aValues = $oCategories->getData(array('type' => 'by_module_and_object', 'module' => $this->_oModule->getName(), 'object_id' => (!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0 )));
  973. if (count($aValues) > 0){
  974. $aVars = array('bx_repeat:cats' => array());
  975. foreach ($aValues as $sValue) {
  976. $aVars['bx_repeat:cats'][] = array(
  977. 'url' => $oCategories->getUrl($this->_oModule->getName(), $sValue),
  978. 'name' => _t($sValue),
  979. 'bx_if:more' => array(
  980. 'condition' => $sValue === end($aValues) ? false : true,
  981. 'content' => array('1')
  982. ),
  983. );
  984. }
  985. if (!$aVars['bx_repeat:cats'])
  986. return '';
  987. return $this->_oModule->_oTemplate->parseHtmlByName('category_list_inline.html', $aVars);
  988. }
  989. return '';
  990. }
  991. protected function genCustomInputMulticat(&$aInput)
  992. {
  993. $sJsObject = $this->_oModule->_oConfig->getJsObject('categories');
  994. $aValuesForSelect = BxDolCategories::getInstance()->getData(array('type' => 'by_module_and_author', 'module' => $this->_oModule->getName(), 'author' => bx_get_logged_profile_id()));
  995. $aSelectedItems = array();
  996. if (isset($aInput['value']) && is_array($aInput['value']))
  997. $aInput['value'] = array_filter($aInput['value']);
  998. if(!empty($aInput['value'])) {
  999. if (!is_array($aInput['value']))
  1000. $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 )));
  1001. else
  1002. $aValues = $aInput['value'];
  1003. $aValues = array_filter($aValues);
  1004. foreach($aValues as $sValue) {
  1005. if (!array_key_exists($sValue, $aValuesForSelect)){
  1006. $aValuesForSelect[$sValue] = array('key' => $sValue, 'value' => $sValue);
  1007. }
  1008. }
  1009. foreach($aValues as $sValue) {
  1010. $sInput = $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, $sValue);
  1011. $aSelectedItems[] = array('js_object' => $sJsObject, 'select_cat' => $sInput);
  1012. }
  1013. }
  1014. else{
  1015. $aSelectedItems = array(
  1016. array('js_object' => $sJsObject, 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect))
  1017. );
  1018. }
  1019. return $this->_oModule->_oTemplate->parseHtmlByName('form_categories.html', array(
  1020. 'bx_repeat:items' => $aSelectedItems,
  1021. 'js_object' => $sJsObject,
  1022. 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, -1),
  1023. 'input_cat' => $this->genCustomInputMulticatInput($aInput),
  1024. 'js_object' => $sJsObject,
  1025. 'btn_add' => $this->genCustomInputMulticatButton($aInput),
  1026. 'btn_add_new' => $this->genCustomInputMulticatButtonNew($aInput)
  1027. ));
  1028. }
  1029. protected function genCustomInputMulticatSelect($aInput, $aValues, $mixedValue = '')
  1030. {
  1031. foreach($aValues as $sKey => $aValue) {
  1032. $aValues[$sKey]['value'] = _t($aValue['key']);
  1033. }
  1034. $aValues = array_merge(array('1' => array('key' => false, 'value' => _t('_sys_please_select'))), $aValues);
  1035. $aInput['type'] = 'select';
  1036. $aInput['name'] .= '[]';
  1037. $aInput['value'] = $mixedValue;
  1038. $aInput['values'] = $aValues;
  1039. return $this->genInput($aInput);
  1040. }
  1041. protected function genCustomInputMulticatInput($aInput)
  1042. {
  1043. $aInput['type'] = 'text';
  1044. $aInput['name'] .= '[]';
  1045. $aInput['value'] = '';
  1046. return $this->genInput($aInput);
  1047. }
  1048. protected function genCustomInputMulticatButton($aInput)
  1049. {
  1050. $sName = $aInput['name'];
  1051. $aInput['type'] = 'button';
  1052. $aInput['name'] .= '_add';
  1053. $aInput['value'] = _t('_sys_txt_categories_button_caption_add');
  1054. $aInput['attrs']['class'] = 'bx-def-margin-right bx-def-margin-sec-top';
  1055. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAdd(this, '" . $sName . "');";
  1056. return $this->genInputButton($aInput);
  1057. }
  1058. protected function genCustomInputMulticatButtonNew($aInput)
  1059. {
  1060. $sName = $aInput['name'];
  1061. $aInput['type'] = 'button';
  1062. $aInput['name'] .= '_add';
  1063. $aInput['value'] = _t('_sys_txt_categories_button_caption_addnew');
  1064. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  1065. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAddNew(this, '" . $sName . "');";
  1066. return $this->genInputButton($aInput);
  1067. }
  1068. protected function genCustomInputAuthor ($aInput)
  1069. {
  1070. if(empty($aInput['custom']) || !is_array($aInput['custom']))
  1071. $aInput['custom'] = array();
  1072. $aInput['custom']['only_once'] = 1;
  1073. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . "modules/?r=" . $this->_oModule->_oConfig->getUri() . "/ajax_get_profiles";
  1074. if (bx_is_api()){
  1075. $aInput['ajax_get_suggestions'] = $this->_oModule->_oConfig->getName() . "/get_profiles&params[]=";
  1076. $aProfile = BxDolProfile::getInstance($aInput['value'])->getUnitAPI(0, ['template' => 'unit_wo_info']);
  1077. $aInput['value_data'] = [$aProfile['author_data']];
  1078. }
  1079. return $this->genCustomInputUsernamesSuggestions($aInput);
  1080. }
  1081. protected function genCustomInputPolls ($aInput)
  1082. {
  1083. return $this->_oModule->_oTemplate->getPollField(!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0);
  1084. }
  1085. protected function _isMulticatEnabled(){
  1086. $CNF = $this->_oModule->_oConfig->CNF;
  1087. return isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true && isset($CNF['FIELD_MULTICAT']);
  1088. }
  1089. protected function _replaceMarkersInControls($sKey = 'controls')
  1090. {
  1091. if(!isset($this->aInputs[$sKey]) || !is_array($this->aInputs[$sKey]))
  1092. return;
  1093. array_walk($this->aInputs[$sKey], function(&$aItem, $mKey) {
  1094. if(!is_int($mKey))
  1095. return;
  1096. if(!empty($aItem['attrs']) && is_array($aItem['attrs']))
  1097. $aItem['attrs'] = $this->_replaceMarkers($aItem['attrs']);
  1098. });
  1099. }
  1100. }
  1101. /** @} */