BxBaseModGeneralFormEntry.php 58 KB

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