1
0

BxBaseModGeneralFormEntry.php 51 KB

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