BxBaseModGeneralFormEntry.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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. foreach($aPrivacyFields as $sField => $sObject)
  346. $this->_validatePrivacyField($sField, $sObject, $aValues);
  347. }
  348. public function insert ($aValsToAdd = array(), $isIgnore = false)
  349. {
  350. $CNF = &$this->_oModule->_oConfig->CNF;
  351. $iAuthor = -1;
  352. if(isset($CNF['FIELD_AUTHOR'])) {
  353. $sFa = $CNF['FIELD_AUTHOR'];
  354. $iAnonymous = isset($CNF['FIELD_ANONYMOUS'], $this->aInputs[$CNF['FIELD_ANONYMOUS']]) && $this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1;
  355. if(empty($aValsToAdd[$sFa])) {
  356. if(!isset($this->aInputs[$sFa]) || empty($this->getCleanValue($sFa)))
  357. $iAuthor = bx_get_logged_profile_id();
  358. else
  359. $iAuthor = $this->getCleanValue($sFa);
  360. $aValsToAdd[$sFa] = $iAnonymous * $iAuthor;
  361. }
  362. else {
  363. $iAuthor = $aValsToAdd[$sFa];
  364. $aValsToAdd[$sFa] *= $iAnonymous;
  365. }
  366. }
  367. if (isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']]) && empty($this->getCleanValue($CNF['FIELD_ADDED'])))
  368. $aValsToAdd[$CNF['FIELD_ADDED']] = time();
  369. if (isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  370. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  371. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb() && isset($CNF['FIELD_THUMB'])) {
  372. $aThumb = isset($_POST[$CNF['FIELD_THUMB']]) ? bx_process_input ($_POST[$CNF['FIELD_THUMB']], BX_DATA_INT) : false;
  373. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  374. if (!empty($aThumb)){
  375. if(is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  376. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  377. else
  378. $aValsToAdd[$CNF['FIELD_THUMB']] = $aThumb;
  379. }
  380. }
  381. if(!empty($CNF['OBJECT_METATAGS']))
  382. $this->_processMetas($aValsToAdd);
  383. if(isset($CNF['FIELD_STATUS']) && !empty($CNF['FIELDS_DELAYED_PROCESSING']) && !empty($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4'])) {
  384. $oTranscoder = BxDolTranscoder::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  385. $aFields = $CNF['FIELDS_DELAYED_PROCESSING'];
  386. if(is_string($aFields))
  387. $aFields = explode(',', $aFields);
  388. foreach($aFields as $sField) {
  389. $mixedFieldValues = $this->getCleanValue($sField);
  390. if(empty($mixedFieldValues))
  391. continue;
  392. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sField]['storage_object']);
  393. if(!$oStorage)
  394. continue;
  395. if(!is_array($mixedFieldValues))
  396. $mixedFieldValues = array($mixedFieldValues);
  397. foreach($mixedFieldValues as $mixedFieldValue) {
  398. $aInfo = $oStorage->getFile((int)$mixedFieldValue);
  399. if(empty($aInfo) || !is_array($aInfo))
  400. continue;
  401. if(strncmp($aInfo['mime_type'], 'video/', 6) != 0)
  402. continue;
  403. if($oTranscoder->isFileReady((int)$mixedFieldValue))
  404. continue;
  405. $aValsToAdd[$CNF['FIELD_STATUS']] = 'awaiting';
  406. break 2;
  407. }
  408. }
  409. }
  410. if(isset($CNF['FIELD_STATUS_ADMIN']) && empty($aValsToAdd[$CNF['FIELD_STATUS_ADMIN']]) && !$this->_oModule->_isModerator() && !$this->_oModule->_oConfig->isAutoApproveEnabled())
  411. $aValsToAdd[$CNF['FIELD_STATUS_ADMIN']] = BX_BASE_MOD_GENERAL_STATUS_PENDING;
  412. $bMulticatEnabled = $this->_isMulticatEnabled();
  413. if ($bMulticatEnabled)
  414. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  415. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  416. if(!empty($iContentId)) {
  417. foreach($this->aInputs as $aInput) {
  418. if ($aInput['type'] == 'nested_form') {
  419. if (is_array($aInput['ghost_template']) && !isset($aInput['ghost_template']['inputs'])) {
  420. foreach ($aInput['ghost_template'] as $oFormNested) {
  421. $iNestedContentId = $oFormNested->insert(array('content_id' => $iContentId));
  422. if ($aInput['rateable']) {
  423. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName(), $iNestedContentId);
  424. }
  425. }
  426. }
  427. }
  428. }
  429. if ($bMulticatEnabled)
  430. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  431. }
  432. foreach($this->aInputs as $aInput) {
  433. if (isset($aInput['rateable']) && $aInput['rateable'] && $aInput['type'] != 'nested_form'){
  434. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName());
  435. }
  436. }
  437. return $iContentId;
  438. }
  439. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  440. {
  441. $CNF = &$this->_oModule->_oConfig->CNF;
  442. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  443. $iAuthor = -1;
  444. if(isset($CNF['FIELD_AUTHOR'])) {
  445. $sFa = $CNF['FIELD_AUTHOR'];
  446. if(!empty($aValsToAdd[$sFa]))
  447. $iAuthor = $aValsToAdd[$sFa];
  448. else if(isset($this->aInputs[$sFa]) && !empty($this->getCleanValue($sFa)))
  449. $iAuthor = $this->getCleanValue($sFa);
  450. else if(!empty($aContentInfo[$sFa]))
  451. $iAuthor = $aContentInfo[$sFa];
  452. else
  453. $iAuthor = bx_get_logged_profile_id();
  454. $iAuthor = abs($iAuthor);
  455. $iAnonymous = isset($CNF['FIELD_ANONYMOUS'], $this->aInputs[$CNF['FIELD_ANONYMOUS']]) && $this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1;
  456. $aValsToAdd[$sFa] = $iAnonymous * $iAuthor;
  457. }
  458. if(isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  459. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  460. if(CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($iContentId) && isset($CNF['FIELD_THUMB']) && ($aThumb = bx_get($CNF['FIELD_THUMB'])) !== false) {
  461. $aThumb = bx_process_input($aThumb, BX_DATA_INT);
  462. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  463. if(!empty($aThumb)) {
  464. if(is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  465. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  466. else
  467. $aValsToAdd[$CNF['FIELD_THUMB']] = $aThumb;
  468. }
  469. }
  470. if(!empty($CNF['OBJECT_METATAGS']))
  471. $this->_processMetas($aValsToAdd);
  472. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  473. $aValsToAdd[$CNF['FIELD_STATUS']] = 'active';
  474. $bMulticatEnabled = $this->_isMulticatEnabled();
  475. if ($bMulticatEnabled)
  476. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  477. $mixedResult = parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  478. foreach($this->aInputs as $aInput) {
  479. if ($aInput['type'] == 'nested_form'){
  480. if (is_array($aInput['ghost_template']) && !isset($aInput['ghost_template']['inputs'])) {
  481. foreach ($aInput['ghost_template'] as $oFormNested) {
  482. $aSpecificValues = $oFormNested->getSpecificValues();
  483. $iNestedContentId = $oFormNested->getSubmittedValue($aInput['name'], BX_DOL_FORM_METHOD_SPECIFIC, $aSpecificValues);
  484. if (empty($iNestedContentId)){
  485. $iNestedContentId = $oFormNested->insert(array('content_id' => $iContentId));
  486. if ($aInput['rateable']){
  487. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName(), $iNestedContentId);
  488. }
  489. }
  490. else{
  491. $oFormNested->update($iNestedContentId, array('content_id' => $iContentId));
  492. }
  493. }
  494. }
  495. }
  496. }
  497. if ($bMulticatEnabled)
  498. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  499. foreach($this->aInputs as $aInput) {
  500. if (isset($aInput['rateable']) && $aInput['rateable'] && $aInput['type'] != 'nested_form'){
  501. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName());
  502. }
  503. }
  504. if($mixedResult !== false)
  505. $this->_processTrackFields($iContentId);
  506. return $mixedResult;
  507. }
  508. function getHtmlEditorQueryParams($aInput)
  509. {
  510. $aQueryParams = parent::getHtmlEditorQueryParams($aInput);
  511. if (isset($this->MODULE)){
  512. $aQueryParams['m'] = $this->MODULE;
  513. }
  514. if (isset($this->_iContentId) && $this->_iContentId){
  515. $aQueryParams['cid'] = $this->_iContentId;
  516. }
  517. $aQueryParams['fi'] = '';
  518. /**
  519. * @hooks
  520. * @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
  521. * - $unit_name - equals `system`
  522. * - $action - equals `editor_query_params`
  523. * - $object_id - not used
  524. * - $sender_id - not used
  525. * - $extra_params - array of additional params with the following array keys:
  526. * - `form` - [object] an instance of form, @see BxDolForm
  527. * - `override_result` - [array] by ref, query string params, can be overridden in hook processing
  528. * @hook @ref hook-system-editor_query_params
  529. */
  530. bx_alert('system', 'editor_query_params', 0, 0, [
  531. 'form' => $this,
  532. 'override_result' => &$aQueryParams
  533. ]);
  534. return $aQueryParams;
  535. }
  536. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  537. {
  538. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  539. }
  540. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  541. {
  542. $CNF = &$this->_oModule->_oConfig->CNF;
  543. return array (
  544. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  545. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  546. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  547. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  548. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  549. 'bx_if:set_thumb' => array (
  550. 'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($this->aInputs[$CNF['FIELD_PHOTO']]['content_id']),
  551. 'content' => array (
  552. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  553. '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')
  554. ),
  555. ),
  556. );
  557. }
  558. function delete ($iContentId, $aContentInfo = array())
  559. {
  560. $CNF = &$this->_oModule->_oConfig->CNF;
  561. // delete associated files
  562. $a = array('OBJECT_STORAGE', 'OBJECT_STORAGE_FILES', 'OBJECT_STORAGE_PHOTOS', 'OBJECT_STORAGE_VIDEOS', 'OBJECT_STORAGE_SOUNDS');
  563. foreach ($a as $k) {
  564. if (!empty($CNF[$k])) {
  565. $oStorage = BxDolStorage::getObjectInstance($CNF[$k]);
  566. if ($oStorage)
  567. $oStorage->queueFilesForDeletionFromGhosts($aContentInfo[$CNF['FIELD_AUTHOR']], $iContentId);
  568. }
  569. }
  570. // delete associated objects data
  571. if (!empty($CNF['OBJECT_VIEWS'])) {
  572. $o = BxDolView::getObjectInstance($CNF['OBJECT_VIEWS'], $iContentId);
  573. if ($o) $o->onObjectDelete();
  574. }
  575. if (!empty($CNF['OBJECT_VOTES'])) {
  576. $o = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $iContentId);
  577. if ($o) $o->onObjectDelete();
  578. }
  579. if (!empty($CNF['OBJECT_SCORES'])) {
  580. $o = BxDolScore::getObjectInstance($CNF['OBJECT_SCORES'], $iContentId);
  581. if ($o) $o->onObjectDelete();
  582. }
  583. if (!empty($CNF['OBJECT_COMMENTS'])) {
  584. $o = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  585. if ($o) $o->onObjectDelete();
  586. }
  587. if (!empty($CNF['OBJECT_REPORTS'])) {
  588. $o = BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS'], $iContentId);
  589. if ($o) $o->onObjectDelete();
  590. }
  591. if (!empty($CNF['OBJECT_METATAGS'])) {
  592. $o = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  593. if ($o) $o->onDeleteContent($iContentId);
  594. }
  595. // delete SEO links
  596. BxDolPage::deleteSeoLink ($this->_oModule->getName(), $this->_oModule->getName(), $iContentId);
  597. // delete db record
  598. BxDolFormQuery::removeFormField($this->id, $iContentId);
  599. return parent::delete($iContentId);
  600. }
  601. protected function _isAdmin ($iContentId = 0)
  602. {
  603. return $this->_oModule->_isModerator();
  604. }
  605. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  606. {
  607. $CNF = &$this->_oModule->_oConfig->CNF;
  608. if (!isset($this->aInputs[$sFieldFile]))
  609. return true;
  610. $mixedFileIds = $this->getCleanValue($sFieldFile);
  611. if(!$mixedFileIds)
  612. return true;
  613. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  614. if (!$oStorage)
  615. return false;
  616. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  617. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true, $this->_isAdmin($iContentId));
  618. if (!$aGhostFiles)
  619. return true;
  620. foreach ($aGhostFiles as $aFile) {
  621. if (is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  622. continue;
  623. if ($aFile['private'])
  624. $oStorage->setFilePrivate ($aFile['id'], 1);
  625. if ($iContentId)
  626. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  627. }
  628. return true;
  629. }
  630. function _deleteFile ($iFileId, $sStorage = '')
  631. {
  632. $CNF = &$this->_oModule->_oConfig->CNF;
  633. if (!$iFileId)
  634. return true;
  635. $sStorage = !empty($sStorage) ? $sStorage : $CNF['OBJECT_STORAGE'];
  636. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  637. if (!$oStorage)
  638. return false;
  639. if (!$oStorage->getFile($iFileId))
  640. return true;
  641. $iProfileId = bx_get_logged_profile_id();
  642. return $oStorage->deleteFile($iFileId, $iProfileId);
  643. }
  644. function addCssJs ()
  645. {
  646. if ((!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) && !self::$_isCssJsGeneralModuleAdded) {
  647. $this->_oModule->_oTemplate->addCss('forms.css');
  648. $this->_oModule->_oTemplate->addJs('modules/base/general/js/|forms.js');
  649. self::$_isCssJsGeneralModuleAdded = true;
  650. }
  651. return parent::addCssJs ();
  652. }
  653. function genViewRowValue(&$aInput)
  654. {
  655. $s = parent::genViewRowValue($aInput);
  656. if ($this->_oMetatagsObject && in_array($aInput['name'], $this->_aMetatagsFieldsWithKeywords) && $s)
  657. $s = $this->_oMetatagsObject->metaParse($this->_oMetatagsContentId, $s);
  658. return $s;
  659. }
  660. function genViewRowWrapped(&$aInput)
  661. {
  662. $sResult = parent::genViewRowWrapped($aInput);
  663. if(!$aInput['rateable'])
  664. return $sResult;
  665. $sValue = $this->genViewRowValue($aInput);
  666. if(null === $sValue)
  667. return '';
  668. // process rateable fields
  669. $iId = BxDolFormQuery::getFormField($this->id, $aInput['name'], $this->_iContentId);
  670. if(($oVote = BxDolVote::getObjectInstance($aInput['rateable'], $iId)) !== null) {
  671. $sVoteElement = $oVote->getElementInline(['show_counter_empty' => true, 'show_counter' => false, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode]);
  672. $sVoteCounter = $oVote->getCounter(['show_counter_empty' => true, 'show_counter' => true, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode]);
  673. return $this->oTemplate->parseHtmlByName('form_view_rateable_row.html', [
  674. 'value' => $sResult,
  675. 'rate_btn' => $sVoteElement,
  676. 'rate' => $sVoteCounter,
  677. ]);
  678. }
  679. return $sResult;
  680. }
  681. function setMetatagsKeywordsData($iId, $a, $o)
  682. {
  683. $this->_oMetatagsContentId = $iId;
  684. $this->_aMetatagsFieldsWithKeywords = $a;
  685. $this->_oMetatagsObject = $o;
  686. }
  687. function getContentOwnerProfileId ($iContentId)
  688. {
  689. return $this->_oModule->serviceGetContentOwnerProfileId($iContentId);
  690. }
  691. protected function _processTrackFields($mixedContent)
  692. {
  693. if(empty($mixedContent) || empty($this->_aTrackFieldsChanges))
  694. return;
  695. if(!is_array($mixedContent)) {
  696. $mixedContent = $this->_oModule->_oDb->getContentInfoById((int)$mixedContent);
  697. if(empty($mixedContent) || !is_array($mixedContent))
  698. return;
  699. }
  700. foreach($this->_aTrackFieldsChanges as $sField => $aValues)
  701. if($mixedContent[$sField] == $aValues['old'])
  702. $this->_aTrackFieldsChanges[$sField] = false;
  703. else
  704. $this->_aTrackFieldsChanges[$sField]['new'] = $mixedContent[$sField];
  705. }
  706. protected function _processMetas(&$aValsToAdd)
  707. {
  708. $CNF = &$this->_oModule->_oConfig->CNF;
  709. $sKey = 'FIELD_LABELS';
  710. if(isset($CNF[$sKey]) && isset($this->aInputs[$CNF[$sKey]]) && empty($aValsToAdd[$CNF[$sKey]])) {
  711. $aLabels = $this->getCleanValue($CNF[$sKey]);
  712. if(!empty($aLabels) && is_array($aLabels))
  713. $aValsToAdd[$CNF[$sKey]] = serialize($aLabels);
  714. else
  715. $aValsToAdd[$CNF[$sKey]] = '';
  716. }
  717. $sKey1 = 'FIELD_LOCATION';
  718. $sKey2 = 'FIELD_LOCATION_PREFIX';
  719. if(isset($CNF[$sKey1]) && isset($CNF[$sKey2]) && isset($this->aInputs[$CNF[$sKey1]]) && empty($aValsToAdd[$CNF[$sKey1]])) {
  720. $aLocation = BxDolMetatags::locationsRetrieveFromForm($CNF[$sKey2], $this);
  721. if(!empty($aLocation) && is_array($aLocation))
  722. $aValsToAdd[$CNF[$sKey1]] = serialize(BxDolMetatags::locationsParseComponents($aLocation));
  723. }
  724. }
  725. protected function _getPrivacyFields($aKeysF2O = array())
  726. {
  727. $CNF = &$this->_oModule->_oConfig->CNF;
  728. if(isset($CNF['PRIVACY_FIELD_TO_OBJECT']) && is_array($CNF['PRIVACY_FIELD_TO_OBJECT']))
  729. return $CNF['PRIVACY_FIELD_TO_OBJECT'];
  730. if(empty($aKeysF2O))
  731. $aKeysF2O = array('FIELD_ALLOW_VIEW_TO' => 'OBJECT_PRIVACY_VIEW');
  732. $aResult = array();
  733. foreach($aKeysF2O as $sKeyField => $sKeyObject) {
  734. if(!isset($CNF[$sKeyField]) || !isset($this->aInputs[$CNF[$sKeyField]]) || !isset($CNF[$sKeyObject]))
  735. continue;
  736. $aResult[$CNF[$sKeyField]] = $CNF[$sKeyObject];
  737. }
  738. return $aResult;
  739. }
  740. function _addCssJsPrivacyField($sField, $sPrivacyObject, $bDynamicMode = false)
  741. {
  742. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  743. if(!$oPrivacy)
  744. return;
  745. $this->aInputs[$sField]['content'] = $oPrivacy->addCssJs($bDynamicMode) . $this->aInputs[$sField]['content'];
  746. }
  747. protected function _preparePrivacyField($sField, $sPrivacyObject)
  748. {
  749. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  750. if(!$oPrivacy)
  751. return;
  752. $aSave = array('db' => array('pass' => 'Xss'));
  753. array_walk($this->aInputs[$sField], function ($a, $k, $aSave) {
  754. if (in_array($k, array('info', 'caption', 'value')))
  755. $aSave[0][$k] = $a;
  756. }, array(&$aSave));
  757. $aGroupChooser = $oPrivacy->getGroupChooser($sPrivacyObject, 0, array(
  758. 'object' => $this->aParams['object'],
  759. 'display' => $this->aParams['display']
  760. ));
  761. $this->aInputs[$sField] = array_merge($this->aInputs[$sField], $aGroupChooser, $aSave);
  762. }
  763. protected function _preloadPrivacyField($sField, $sPrivacyObject, $aValues)
  764. {
  765. $CNF = &$this->_oModule->_oConfig->CNF;
  766. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  767. if(!$oPrivacy)
  768. return;
  769. $iContentId = !empty($aValues[$CNF['FIELD_ID']]) ? (int)$aValues[$CNF['FIELD_ID']] : 0;
  770. $iProfileId = !empty($iContentId) ? (int)$this->getContentOwnerProfileId($iContentId) : bx_get_logged_profile_id();
  771. $iGroupId = !empty($aValues[$sField]) ? $aValues[$sField] : 0;
  772. if(!isset($this->aInputs[$sField]['content']))
  773. $this->aInputs[$sField]['content'] = '';
  774. $this->aInputs[$sField]['content'] .= $oPrivacy->initGroupChooser($sPrivacyObject, $iProfileId, array(
  775. 'content_id' => $iContentId,
  776. 'group_id' => $iGroupId,
  777. 'html_ids' => array(
  778. 'form' => $this->getId()
  779. )
  780. ));
  781. }
  782. protected function _validatePrivacyField($sField, $sPrivacyObject, $aValues)
  783. {
  784. $mixedValue = $this->aInputs[$sField]['value'];
  785. $bValue = false;
  786. foreach($this->aInputs[$sField]['values'] as $aValue)
  787. if(isset($aValue['key']) && $aValue['key'] == $mixedValue) {
  788. $bValue = true;
  789. break;
  790. }
  791. if(!$bValue) {
  792. $sTitle = '';
  793. if(is_numeric($mixedValue) && (int)$mixedValue < 0 && ($oContext = BxDolProfile::getInstance(abs((int)$mixedValue))) !== false)
  794. $sTitle = $oContext->getDisplayName();
  795. else
  796. $sTitle = _t('_sys_ps_group_title_unknown');
  797. $this->aInputs[$sField]['values'] = array_merge(array(
  798. array('key' => $mixedValue, 'value' => $sTitle),
  799. array('key' => '', 'value' => '----', 'attrs' => array('disabled' => 'disabled'))
  800. ), $this->aInputs[$sField]['values']);
  801. }
  802. }
  803. /**
  804. *
  805. * MultiCategories related methods.
  806. *
  807. */
  808. protected function processMulticatBefore($sFieldName, &$aValsToAdd){
  809. if (isset($this->aInputs[$sFieldName])){
  810. $this->aInputs[$sFieldName]['value'] = array_unique(array_filter($this->aInputs[$sFieldName]['value'], function($sTmp){
  811. return trim($sTmp);
  812. }));
  813. $aValsToAdd[$sFieldName] = implode(',', $this->aInputs[$sFieldName]['value']);
  814. }
  815. }
  816. protected function processMulticatAfter($sFieldName, $iContentId){
  817. $CNF = &$this->_oModule->_oConfig->CNF;
  818. $bAutoActivation = (isset($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) && getParam($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) == 'on') ? true : false;
  819. $oCategories = BxDolCategories::getInstance();
  820. if (isset($this->aInputs[$sFieldName])){
  821. $oCategories->delete($this->_oModule->getName(), $iContentId);
  822. foreach($this->aInputs[$sFieldName]['value'] as $sValue) {
  823. $oCategories->add($this->_oModule->getName(), bx_get_logged_profile_id(), $sValue, $iContentId, $bAutoActivation);
  824. }
  825. }
  826. }
  827. protected function genCustomViewRowValueMulticat(&$aInput)
  828. {
  829. $oCategories = BxDolCategories::getInstance();
  830. $aValues = $oCategories->getData(array('type' => 'by_module_and_object', 'module' => $this->_oModule->getName(), 'object_id' => (!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0 )));
  831. if (count($aValues) > 0){
  832. $aVars = array('bx_repeat:cats' => array());
  833. foreach ($aValues as $sValue) {
  834. $aVars['bx_repeat:cats'][] = array(
  835. 'url' => $oCategories->getUrl($this->_oModule->getName(), $sValue),
  836. 'name' => _t($sValue),
  837. 'bx_if:more' => array(
  838. 'condition' => $sValue === end($aValues) ? false : true,
  839. 'content' => array('1')
  840. ),
  841. );
  842. }
  843. if (!$aVars['bx_repeat:cats'])
  844. return '';
  845. return $this->_oModule->_oTemplate->parseHtmlByName('category_list_inline.html', $aVars);
  846. }
  847. return '';
  848. }
  849. protected function genCustomInputMulticat(&$aInput)
  850. {
  851. $sJsObject = $this->_oModule->_oConfig->getJsObject('categories');
  852. $aValuesForSelect = BxDolCategories::getInstance()->getData(array('type' => 'by_module_and_author', 'module' => $this->_oModule->getName(), 'author' => bx_get_logged_profile_id()));
  853. $aSelectedItems = array();
  854. if (isset($aInput['value']) && is_array($aInput['value']))
  855. $aInput['value'] = array_filter($aInput['value']);
  856. if(!empty($aInput['value'])) {
  857. if (!is_array($aInput['value']))
  858. $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 )));
  859. else
  860. $aValues = $aInput['value'];
  861. $aValues = array_filter($aValues);
  862. foreach($aValues as $sValue) {
  863. if (!array_key_exists($sValue, $aValuesForSelect)){
  864. $aValuesForSelect[$sValue] = array('key' => $sValue, 'value' => $sValue);
  865. }
  866. }
  867. foreach($aValues as $sValue) {
  868. $sInput = $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, $sValue);
  869. $aSelectedItems[] = array('js_object' => $sJsObject, 'select_cat' => $sInput);
  870. }
  871. }
  872. else{
  873. $aSelectedItems = array(
  874. array('js_object' => $sJsObject, 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect))
  875. );
  876. }
  877. return $this->_oModule->_oTemplate->parseHtmlByName('form_categories.html', array(
  878. 'bx_repeat:items' => $aSelectedItems,
  879. 'js_object' => $sJsObject,
  880. 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, -1),
  881. 'input_cat' => $this->genCustomInputMulticatInput($aInput),
  882. 'js_object' => $sJsObject,
  883. 'btn_add' => $this->genCustomInputMulticatButton($aInput),
  884. 'btn_add_new' => $this->genCustomInputMulticatButtonNew($aInput)
  885. ));
  886. }
  887. protected function genCustomInputMulticatSelect($aInput, $aValues, $mixedValue = '')
  888. {
  889. foreach($aValues as $sKey => $aValue) {
  890. $aValues[$sKey]['value'] = _t($aValue['key']);
  891. }
  892. $aValues = array_merge(array('1' => array('key' => false, 'value' => _t('_sys_please_select'))), $aValues);
  893. $aInput['type'] = 'select';
  894. $aInput['name'] .= '[]';
  895. $aInput['value'] = $mixedValue;
  896. $aInput['values'] = $aValues;
  897. return $this->genInput($aInput);
  898. }
  899. protected function genCustomInputMulticatInput($aInput)
  900. {
  901. $aInput['type'] = 'text';
  902. $aInput['name'] .= '[]';
  903. $aInput['value'] = '';
  904. return $this->genInput($aInput);
  905. }
  906. protected function genCustomInputMulticatButton($aInput)
  907. {
  908. $sName = $aInput['name'];
  909. $aInput['type'] = 'button';
  910. $aInput['name'] .= '_add';
  911. $aInput['value'] = _t('_sys_txt_categories_button_caption_add');
  912. $aInput['attrs']['class'] = 'bx-def-margin-right bx-def-margin-sec-top';
  913. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAdd(this, '" . $sName . "');";
  914. return $this->genInputButton($aInput);
  915. }
  916. protected function genCustomInputMulticatButtonNew($aInput)
  917. {
  918. $sName = $aInput['name'];
  919. $aInput['type'] = 'button';
  920. $aInput['name'] .= '_add';
  921. $aInput['value'] = _t('_sys_txt_categories_button_caption_addnew');
  922. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  923. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAddNew(this, '" . $sName . "');";
  924. return $this->genInputButton($aInput);
  925. }
  926. protected function genCustomInputAuthor ($aInput)
  927. {
  928. if(empty($aInput['custom']) || !is_array($aInput['custom']))
  929. $aInput['custom'] = array();
  930. $aInput['custom']['only_once'] = 1;
  931. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . "modules/?r=" . $this->_oModule->_oConfig->getUri() . "/ajax_get_profiles";
  932. if (bx_is_api()){
  933. $aInput['ajax_get_suggestions'] = $this->_oModule->_oConfig->getName() . "/get_profiles&params[]=";
  934. $aProfile = BxDolProfile::getInstance($aInput['value'])->getUnitAPI(0, ['template' => 'unit_wo_info']);
  935. $aInput['value_data'] = [$aProfile['author_data']];
  936. }
  937. return $this->genCustomInputUsernamesSuggestions($aInput);
  938. }
  939. protected function _isMulticatEnabled(){
  940. $CNF = $this->_oModule->_oConfig->CNF;
  941. return isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true && isset($CNF['FIELD_MULTICAT']);
  942. }
  943. }
  944. /** @} */