BxBaseModGeneralFormEntry.php 58 KB

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