BxBaseModGeneralFormEntry.php 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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 (
  56. $this->_oModule->checkAllowedEditAnyEntry(false) === CHECK_ACTION_RESULT_ALLOWED &&
  57. isset($CNF['FIELD_AUTHOR']) &&
  58. (
  59. (isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT']) && $this->aParams['display'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT']) ||
  60. (isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']) && $this->aParams['display'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'])
  61. ) &&
  62. $this->_bAllowChangeUserForAdmins){
  63. $this->aInputs = array_merge([
  64. $CNF['FIELD_AUTHOR'] => [
  65. 'type' => 'custom',
  66. 'name' => $CNF['FIELD_AUTHOR'],
  67. 'db' => ['pass' => 'Xss'],
  68. 'caption' => _t('_sys_form_input_caption_author')
  69. ],
  70. ], $this->aInputs);
  71. }
  72. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  73. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_object'] = $CNF['OBJECT_STORAGE'];
  74. $this->aInputs[$CNF['FIELD_PHOTO']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_PHOTO']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_PHOTO']]['value']) : $CNF['OBJECT_UPLOADERS'];
  75. $this->aInputs[$CNF['FIELD_PHOTO']]['images_transcoder'] = $CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW'];
  76. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_private'] = 0;
  77. $this->aInputs[$CNF['FIELD_PHOTO']]['multiple'] = true;
  78. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = 0;
  79. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = '';
  80. }
  81. $aPrivacyFields = $this->_getPrivacyFields();
  82. foreach($aPrivacyFields as $sField => $sObject)
  83. $this->_preparePrivacyField($sField, $sObject);
  84. foreach($this->aInputs as $sKey => $aInput) {
  85. if ($aInput['type'] == 'nested_form'){
  86. $this->genGhostTemplateForInputNestedForm ($this->aInputs[$sKey]);
  87. }
  88. }
  89. }
  90. function getCode($bDynamicMode = false)
  91. {
  92. $CNF = &$this->_oModule->_oConfig->CNF;
  93. if(!$bDynamicMode && bx_is_dynamic_request())
  94. $bDynamicMode = true;
  95. $sResult = parent::getCode($bDynamicMode);
  96. $aPrivacyFields = $this->_getPrivacyFields();
  97. foreach($aPrivacyFields as $sField => $sObject)
  98. $this->_addCssJsPrivacyField($sField, $sObject, $bDynamicMode);
  99. if(isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true) {
  100. $sInclude2 = '';
  101. $sInclude2 .= $this->_oModule->_oTemplate->addJs(array('BxDolCategories.js'), $bDynamicMode);
  102. $sInclude2 .= $this->_oModule->_oTemplate->addCss(array('categories.css'), $bDynamicMode);
  103. $sResult .= ($bDynamicMode ? $sInclude2 : '') . $this->_oModule->_oTemplate->getJsCode('categories');
  104. }
  105. return $sResult;
  106. }
  107. /**
  108. * Nested forms processing
  109. */
  110. function genInputNestedForm (&$aInput, $sInfo = '', $sError = '')
  111. {
  112. $sUniqId = genRndPwd (8, false);
  113. $aNestedForms = array();
  114. if (!$this->isSubmitted()){
  115. $aGhostTemplateParams = is_object($aInput['ghost_template']) ? $aInput['ghost_template']->aParams : $aInput['ghost_template']['params'];
  116. $aNestedValues = $this->_oModule->_oDb->getNestedBy(array(
  117. 'type' => 'content_id',
  118. 'id' => $this->_iContentId,
  119. 'key_name' => $aGhostTemplateParams['db']['key']
  120. ), $aGhostTemplateParams['db']['table']);
  121. foreach($aNestedValues as $aNestedValue) {
  122. $aNestedValuesRv = array();
  123. $oForm = $this->getNestedFormObject($aInput);
  124. foreach($aNestedValue as $aNestedKey => $aNestedItem) {
  125. if ($oForm->aParams['db']['key'] == $aNestedKey)
  126. $aNestedValuesRv[$aNestedKey . '[]'] = $aNestedItem;
  127. else
  128. $aNestedValuesRv[$aNestedKey] = $aNestedItem;
  129. }
  130. $oForm->initChecker($aNestedValuesRv);
  131. array_push(
  132. $aNestedForms,
  133. array(
  134. 'key_value' => $aNestedValuesRv[$oForm->aParams['db']['key'] . '[]'],
  135. 'key_name' => $aInput['name'],
  136. 'form_code' => $oForm->genRows(),
  137. 'js_instance_name' => 'oBxNestedForm_' . $sUniqId,
  138. 'nested_type' => $aInput['name'],
  139. )
  140. );
  141. }
  142. }
  143. else{
  144. $aInput['ghost_templates'] = $aInput['ghost_template'];
  145. $this->genGhostTemplateForInputNestedForm ($aInput);
  146. foreach($aInput['ghost_templates'] as $oForm) {
  147. if (isset($oForm->aInputs)){
  148. array_push(
  149. $aNestedForms,
  150. array(
  151. 'key_value' => $oForm->aInputs[$aInput['name']]['value'],
  152. 'key_name' => $aInput['name'],
  153. 'form_code' => $oForm->genRows(),
  154. 'js_instance_name' => 'oBxNestedForm_' . $sUniqId,
  155. 'nested_type' => $aInput['name'],
  156. )
  157. );
  158. }
  159. }
  160. }
  161. return $this->oTemplate->parseHtmlByName('form_field_nested_form.html', array(
  162. 'bx_repeat:items' => $aNestedForms,
  163. 'info' => $sInfo,
  164. 'error' => $sError,
  165. 'uniq_id' => $sUniqId,
  166. 'js_instance_name' => 'oBxNestedForm_' . $sUniqId,
  167. 'options' => json_encode(array(
  168. 'uniq_id' => $sUniqId,
  169. 'js_instance_name' => 'oBxNestedForm_' . $sUniqId,
  170. 'template_ghost' => $this->genGhostTemplate($aInput),
  171. 'form_name' => $aInput['value'],
  172. 'action_uri' => $this->_oModule->_oConfig->getBaseUri(),
  173. 'nested_type' => $aInput['name'],
  174. )),
  175. ));
  176. }
  177. function genNestedForm (&$aInput)
  178. {
  179. $sResult = '';
  180. $aGhostTemplateParams = is_object($aInput['ghost_template']) ? $aInput['ghost_template']->aParams : $aInput['ghost_template']['params'];
  181. $aNestedValues = $this->_oModule->_oDb->getNestedBy(array(
  182. 'type' => 'content_id',
  183. 'id' => $this->_iContentId,
  184. 'key_name' => $aGhostTemplateParams['db']['key']
  185. ), $aGhostTemplateParams['db']['table']);
  186. foreach($aNestedValues as $aNestedValue) {
  187. $sValue = '';
  188. $aNestedValuesRv = array();
  189. $oForm = $this->getNestedFormObject($aInput, true);
  190. foreach($aNestedValue as $aNestedKey => $aNestedItem) {
  191. if ($oForm->aParams['db']['key'] == $aNestedKey)
  192. $aNestedValuesRv[$aNestedKey . '[]'] = $aNestedItem;
  193. else
  194. $aNestedValuesRv[$aNestedKey] = $aNestedItem;
  195. }
  196. $oForm->initChecker($aNestedValuesRv);
  197. if ($aInput['rateable']){
  198. $sVote = '';
  199. $sVoteBtn = '';
  200. $iId = BxDolFormQuery::getFormField($this->id, $aInput['name'], $this->_iContentId, $aNestedValuesRv[$oForm->aParams['db']['key'] . '[]']);
  201. $oVote = BxDolVote::getObjectInstance($aInput['rateable'], $iId, true, BxDolTemplate::getInstance());
  202. if ($oVote){
  203. $sVote = $oVote->getCounter(array('show_counter_empty' => true, 'show_counter' => true, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode));
  204. $sVoteBtn = $oVote->getElementInline(array('show_counter_empty' => true, 'show_counter' => false, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode));
  205. }
  206. $sValue = $this->oTemplate->parseHtmlByName('form_view_rateable_row.html', array(
  207. 'value' => $oForm->getCode(),
  208. 'rate' => $sVote,
  209. 'rate_btn' => $sVoteBtn
  210. ));
  211. }
  212. else{
  213. $sValue = $oForm->getCode();
  214. }
  215. $sResult .= $this->oTemplate->parseHtmlByName('form_view_nested_row.html', array(
  216. 'value' => $sValue
  217. ));
  218. }
  219. if ($sResult != ''){
  220. return $this->oTemplate->parseHtmlByName('form_view_row.html', array(
  221. 'type' => $aInput['type'],
  222. 'caption' => isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '',
  223. 'value' => $sResult
  224. ));
  225. }
  226. }
  227. protected function genGhostTemplateForInputNestedForm (&$aInput)
  228. {
  229. $oForm = $this->getNestedFormObject($aInput);
  230. $aInput['ghost_template'] = array (
  231. 'params' => array(
  232. 'nested_form_template' => 'form_field_nested_form_wrapper.html',
  233. 'db' => array(
  234. 'table' => $oForm->aParams['db']['table'],
  235. 'key' => $oForm->aParams['db']['key'],
  236. 'submit_name' => $oForm->aParams['db']['key'],
  237. ),
  238. ),
  239. 'inputs' => $oForm->aInputs
  240. );
  241. }
  242. function getNestedFormObject (&$aInput, $bViewMode = false)
  243. {
  244. $oForm = BxDolForm::getObjectInstance($aInput['value'], $aInput['value'] . ($bViewMode ? '_view' : ''));
  245. foreach($oForm->aInputs as $sKey => $aInput2) {
  246. $sName = $oForm->aInputs[$sKey]['name'];
  247. if (strpos($sName,'[]') === false)
  248. $oForm->aInputs[$sKey]['name'] = $sName . '[]';
  249. }
  250. return $oForm;
  251. }
  252. /**
  253. * Add field(s) which will be tracked during content update.
  254. */
  255. public function addTrackFields($mixedFields, $mixedContent)
  256. {
  257. if(empty($mixedContent) || empty($mixedFields))
  258. return;
  259. if(!is_array($mixedContent)) {
  260. $mixedContent = $this->_oModule->_oDb->getContentInfoById((int)$mixedContent);
  261. if(empty($mixedContent) || !is_array($mixedContent))
  262. return;
  263. }
  264. if(!is_array($mixedFields))
  265. $mixedFields = array($mixedFields);
  266. foreach($mixedFields as $sField) {
  267. if(!isset($mixedContent[$sField]))
  268. continue;
  269. $this->_aTrackFieldsChanges[$sField] = array(
  270. 'old' => $mixedContent[$sField],
  271. );
  272. }
  273. }
  274. /**
  275. * Checks if the field was changed.
  276. */
  277. public function isTrackFieldChanged($sField, $bReturnValues = false)
  278. {
  279. if(!isset($this->_aTrackFieldsChanges[$sField]) || $this->_aTrackFieldsChanges[$sField] === false)
  280. return false;
  281. return $bReturnValues ? $this->_aTrackFieldsChanges[$sField] : true;
  282. }
  283. function initChecker ($aValues = array (), $aSpecificValues = array())
  284. {
  285. $CNF = &$this->_oModule->_oConfig->CNF;
  286. $bValues = $aValues && !empty($aValues['id']);
  287. $this->_iContentId = isset($aValues['id']) ? $aValues['id'] : false;
  288. 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())
  289. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['value'] = $oMetatags->locationsString($aValues[$CNF['FIELD_ID']], false);
  290. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  291. $aContentInfo = false;
  292. if ($aValues && !empty($aValues['id'])) {
  293. $aContentInfo = $this->_oModule->_oDb->getContentInfoById ($aValues['id']);
  294. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = $aValues['id'];
  295. }
  296. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplate, $this->_getPhotoGhostTmplVars($aContentInfo));
  297. }
  298. 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'])) {
  299. $this->aInputs[$CNF['FIELD_LABELS']]['content_id'] = $aValues['id'];
  300. if(($aLabels = $oMetatags->keywordsGet($aValues['id'])))
  301. $this->aInputs[$CNF['FIELD_LABELS']]['value'] = array_intersect($aLabels, BxDolLabel::getInstance()->getLabels(array('type' => 'values')));
  302. }
  303. if (isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']]) && isset($CNF['FIELD_AUTHOR']) && isset($aValues[$CNF['FIELD_AUTHOR']])) {
  304. $this->aInputs[$CNF['FIELD_ANONYMOUS']]['checked'] = $aValues[$CNF['FIELD_AUTHOR']] < 0;
  305. }
  306. if (isset($CNF['FIELD_MULTICAT']) && isset($this->aInputs[$CNF['FIELD_MULTICAT']]) && $CNF['PARAM_MULTICAT_ENABLED']) {
  307. if ($bValues)
  308. $this->aInputs[$CNF['FIELD_MULTICAT']]['content_id'] = $aValues['id'];
  309. }
  310. $aPrivacyFields = $this->_getPrivacyFields();
  311. foreach($aPrivacyFields as $sField => $sObject)
  312. $this->_preloadPrivacyField($sField, $sObject, $aValues);
  313. if (isset($CNF['FIELD_AUTHOR']) && isset($this->aInputs[$CNF['FIELD_AUTHOR']])){
  314. if (isset($aValues[$CNF['FIELD_AUTHOR']]) && $aValues[$CNF['FIELD_AUTHOR']])
  315. $aValues[$CNF['FIELD_AUTHOR']] = $aValues[$CNF['FIELD_AUTHOR']];
  316. else
  317. $aValues[$CNF['FIELD_AUTHOR']] = bx_get_logged_profile_id();
  318. }
  319. parent::initChecker ($aValues, $aSpecificValues);
  320. foreach($aPrivacyFields as $sField => $sObject)
  321. $this->_validatePrivacyField($sField, $sObject, $aValues);
  322. }
  323. public function insert ($aValsToAdd = array(), $isIgnore = false)
  324. {
  325. $CNF = &$this->_oModule->_oConfig->CNF;
  326. $iAuthor = -1;
  327. if (isset($CNF['FIELD_AUTHOR'])){
  328. if (empty($aValsToAdd[$CNF['FIELD_AUTHOR']]) && (!isset($this->aInputs[$CNF['FIELD_AUTHOR']]) || empty($this->getCleanValue($CNF['FIELD_AUTHOR'])))){
  329. $aValsToAdd[$CNF['FIELD_AUTHOR']] = (isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']]) && $this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1) * bx_get_logged_profile_id ();
  330. $iAuthor = $aValsToAdd[$CNF['FIELD_AUTHOR']] ;
  331. }
  332. else{
  333. if(isset($this->aInputs[$CNF['FIELD_AUTHOR']]) && empty($this->getCleanValue($CNF['FIELD_AUTHOR']))){
  334. $aValsToAdd[$CNF['FIELD_AUTHOR']] = bx_get_logged_profile_id();
  335. $iAuthor = $aValsToAdd[$CNF['FIELD_AUTHOR']];
  336. }
  337. if(isset($this->aInputs[$CNF['FIELD_AUTHOR']]) && !empty($this->getCleanValue($CNF['FIELD_AUTHOR']))){
  338. $iAuthor = $this->getCleanValue($CNF['FIELD_AUTHOR']);
  339. }
  340. }
  341. }
  342. if (isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']]) && empty($this->getCleanValue($CNF['FIELD_ADDED'])))
  343. $aValsToAdd[$CNF['FIELD_ADDED']] = time();
  344. if (isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  345. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  346. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb() && isset($CNF['FIELD_THUMB'])) {
  347. $aThumb = isset($_POST[$CNF['FIELD_THUMB']]) ? bx_process_input ($_POST[$CNF['FIELD_THUMB']], BX_DATA_INT) : false;
  348. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  349. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  350. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  351. }
  352. if(!empty($CNF['OBJECT_METATAGS']))
  353. $this->_processMetas($aValsToAdd);
  354. if(isset($CNF['FIELD_STATUS']) && !empty($CNF['FIELDS_DELAYED_PROCESSING']) && !empty($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4'])) {
  355. $oTranscoder = BxDolTranscoder::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  356. $aFields = $CNF['FIELDS_DELAYED_PROCESSING'];
  357. if(is_string($aFields))
  358. $aFields = explode(',', $aFields);
  359. foreach($aFields as $sField) {
  360. $mixedFieldValues = $this->getCleanValue($sField);
  361. if(empty($mixedFieldValues))
  362. continue;
  363. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sField]['storage_object']);
  364. if(!$oStorage)
  365. continue;
  366. if(!is_array($mixedFieldValues))
  367. $mixedFieldValues = array($mixedFieldValues);
  368. foreach($mixedFieldValues as $mixedFieldValue) {
  369. $aInfo = $oStorage->getFile((int)$mixedFieldValue);
  370. if(empty($aInfo) || !is_array($aInfo))
  371. continue;
  372. if(strncmp($aInfo['mime_type'], 'video/', 6) != 0)
  373. continue;
  374. if($oTranscoder->isFileReady((int)$mixedFieldValue))
  375. continue;
  376. $aValsToAdd[$CNF['FIELD_STATUS']] = 'awaiting';
  377. break 2;
  378. }
  379. }
  380. }
  381. $bMulticatEnabled = $this->_isMulticatEnabled();
  382. if ($bMulticatEnabled)
  383. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  384. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  385. if(!empty($iContentId)) {
  386. foreach($this->aInputs as $aInput) {
  387. if ($aInput['type'] == 'nested_form') {
  388. if (is_array($aInput['ghost_template']) && !isset($aInput['ghost_template']['inputs'])) {
  389. foreach ($aInput['ghost_template'] as $oFormNested) {
  390. $iNestedContentId = $oFormNested->insert(array('content_id' => $iContentId));
  391. if ($aInput['rateable']) {
  392. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName(), $iNestedContentId);
  393. }
  394. }
  395. }
  396. }
  397. }
  398. if ($bMulticatEnabled)
  399. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  400. }
  401. foreach($this->aInputs as $aInput) {
  402. if (isset($aInput['rateable']) && $aInput['rateable'] && $aInput['type'] != 'nested_form'){
  403. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName());
  404. }
  405. }
  406. return $iContentId;
  407. }
  408. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  409. {
  410. $CNF = &$this->_oModule->_oConfig->CNF;
  411. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  412. $iAuthor = -1;
  413. $bFieldAuthor = isset($CNF['FIELD_AUTHOR']);
  414. if($bFieldAuthor && isset($aContentInfo[$CNF['FIELD_AUTHOR']])){
  415. $iAuthor = $aContentInfo[$CNF['FIELD_AUTHOR']];
  416. }
  417. if($bFieldAuthor) {
  418. if(isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']])){
  419. $aValsToAdd[$CNF['FIELD_AUTHOR']] = ($this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1) * abs($aContentInfo[$CNF['FIELD_AUTHOR']]);
  420. $iAuthor = $aValsToAdd[$CNF['FIELD_AUTHOR']];
  421. }
  422. else {
  423. if(isset($this->aInputs[$CNF['FIELD_AUTHOR']]) && empty($this->getCleanValue($CNF['FIELD_AUTHOR']))){
  424. $aValsToAdd[$CNF['FIELD_AUTHOR']] = bx_get_logged_profile_id();
  425. $iAuthor = $aValsToAdd[$CNF['FIELD_AUTHOR']];
  426. }
  427. if(isset($this->aInputs[$CNF['FIELD_AUTHOR']]) && !empty($this->getCleanValue($CNF['FIELD_AUTHOR']))){
  428. $iAuthor = $this->getCleanValue($CNF['FIELD_AUTHOR']);
  429. }
  430. }
  431. }
  432. if(isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  433. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  434. if(CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($iContentId) && isset($CNF['FIELD_THUMB'])) {
  435. $aThumb = bx_process_input (bx_get($CNF['FIELD_THUMB']), BX_DATA_INT);
  436. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  437. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  438. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  439. }
  440. if(!empty($CNF['OBJECT_METATAGS']))
  441. $this->_processMetas($aValsToAdd);
  442. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  443. $aValsToAdd[$CNF['FIELD_STATUS']] = 'active';
  444. $bMulticatEnabled = $this->_isMulticatEnabled();
  445. if ($bMulticatEnabled)
  446. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  447. $mixedResult = parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  448. foreach($this->aInputs as $aInput) {
  449. if ($aInput['type'] == 'nested_form'){
  450. if (is_array($aInput['ghost_template']) && !isset($aInput['ghost_template']['inputs'])) {
  451. foreach ($aInput['ghost_template'] as $oFormNested) {
  452. $aSpecificValues = $oFormNested->getSpecificValues();
  453. $iNestedContentId = $oFormNested->getSubmittedValue($aInput['name'], BX_DOL_FORM_METHOD_SPECIFIC, $aSpecificValues);
  454. if (empty($iNestedContentId)){
  455. $iNestedContentId = $oFormNested->insert(array('content_id' => $iContentId));
  456. if ($aInput['rateable']){
  457. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName(), $iNestedContentId);
  458. }
  459. }
  460. else{
  461. $oFormNested->update($iNestedContentId, array('content_id' => $iContentId));
  462. }
  463. }
  464. }
  465. }
  466. }
  467. if ($bMulticatEnabled)
  468. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  469. foreach($this->aInputs as $aInput) {
  470. if (isset($aInput['rateable']) && $aInput['rateable'] && $aInput['type'] != 'nested_form'){
  471. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName());
  472. }
  473. }
  474. if($mixedResult !== false)
  475. $this->_processTrackFields($iContentId);
  476. return $mixedResult;
  477. }
  478. function getHtmlEditorQueryParams($aInput)
  479. {
  480. $aQueryParams = parent::getHtmlEditorQueryParams($aInput);
  481. if (isset($this->MODULE)){
  482. $aQueryParams['m'] = $this->MODULE;
  483. }
  484. if (isset($this->_iContentId) && $this->_iContentId){
  485. $aQueryParams['cid'] = $this->_iContentId;
  486. }
  487. return $aQueryParams;
  488. }
  489. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  490. {
  491. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  492. }
  493. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  494. {
  495. $CNF = &$this->_oModule->_oConfig->CNF;
  496. return array (
  497. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  498. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  499. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  500. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  501. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  502. 'bx_if:set_thumb' => array (
  503. 'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($this->aInputs[$CNF['FIELD_PHOTO']]['content_id']),
  504. 'content' => array (
  505. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  506. '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')
  507. ),
  508. ),
  509. );
  510. }
  511. function delete ($iContentId, $aContentInfo = array())
  512. {
  513. $CNF = &$this->_oModule->_oConfig->CNF;
  514. // delete associated files
  515. $a = array('OBJECT_STORAGE', 'OBJECT_STORAGE_FILES', 'OBJECT_STORAGE_PHOTOS', 'OBJECT_STORAGE_VIDEOS', 'OBJECT_STORAGE_SOUNDS');
  516. foreach ($a as $k) {
  517. if (!empty($CNF[$k])) {
  518. $oStorage = BxDolStorage::getObjectInstance($CNF[$k]);
  519. if ($oStorage)
  520. $oStorage->queueFilesForDeletionFromGhosts($aContentInfo[$CNF['FIELD_AUTHOR']], $iContentId);
  521. }
  522. }
  523. // delete associated objects data
  524. if (!empty($CNF['OBJECT_VIEWS'])) {
  525. $o = BxDolView::getObjectInstance($CNF['OBJECT_VIEWS'], $iContentId);
  526. if ($o) $o->onObjectDelete();
  527. }
  528. if (!empty($CNF['OBJECT_VOTES'])) {
  529. $o = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $iContentId);
  530. if ($o) $o->onObjectDelete();
  531. }
  532. if (!empty($CNF['OBJECT_SCORES'])) {
  533. $o = BxDolScore::getObjectInstance($CNF['OBJECT_SCORES'], $iContentId);
  534. if ($o) $o->onObjectDelete();
  535. }
  536. if (!empty($CNF['OBJECT_COMMENTS'])) {
  537. $o = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  538. if ($o) $o->onObjectDelete();
  539. }
  540. if (!empty($CNF['OBJECT_METATAGS'])) {
  541. $o = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  542. if ($o) $o->onDeleteContent($iContentId);
  543. }
  544. // delete SEO links
  545. BxDolPage::deleteSeoLink ($this->_oModule->getName(), $this->_oModule->getName(), $iContentId);
  546. // delete db record
  547. BxDolFormQuery::removeFormField($this->id, $iContentId);
  548. return parent::delete($iContentId);
  549. }
  550. protected function _isAdmin ($iContentId = 0)
  551. {
  552. return $this->_oModule->_isModerator();
  553. }
  554. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  555. {
  556. $CNF = &$this->_oModule->_oConfig->CNF;
  557. if (!isset($this->aInputs[$sFieldFile]))
  558. return true;
  559. $mixedFileIds = $this->getCleanValue($sFieldFile);
  560. if(!$mixedFileIds)
  561. return true;
  562. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  563. if (!$oStorage)
  564. return false;
  565. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  566. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true, $this->_isAdmin($iContentId));
  567. if (!$aGhostFiles)
  568. return true;
  569. foreach ($aGhostFiles as $aFile) {
  570. if (is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  571. continue;
  572. if ($aFile['private'])
  573. $oStorage->setFilePrivate ($aFile['id'], 1);
  574. if ($iContentId)
  575. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  576. }
  577. return true;
  578. }
  579. function _deleteFile ($iFileId, $sStorage = '')
  580. {
  581. $CNF = &$this->_oModule->_oConfig->CNF;
  582. if (!$iFileId)
  583. return true;
  584. $sStorage = !empty($sStorage) ? $sStorage : $CNF['OBJECT_STORAGE'];
  585. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  586. if (!$oStorage)
  587. return false;
  588. if (!$oStorage->getFile($iFileId))
  589. return true;
  590. $iProfileId = bx_get_logged_profile_id();
  591. return $oStorage->deleteFile($iFileId, $iProfileId);
  592. }
  593. function addCssJs ()
  594. {
  595. if ((!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) && !self::$_isCssJsGeneralModuleAdded) {
  596. $this->_oModule->_oTemplate->addCss('forms.css');
  597. $this->_oModule->_oTemplate->addJs('modules/base/general/js/|forms.js');
  598. self::$_isCssJsGeneralModuleAdded = true;
  599. }
  600. return parent::addCssJs ();
  601. }
  602. function genViewRowValue(&$aInput)
  603. {
  604. $s = parent::genViewRowValue($aInput);
  605. if ($this->_oMetatagsObject && in_array($aInput['name'], $this->_aMetatagsFieldsWithKeywords) && $s)
  606. $s = $this->_oMetatagsObject->metaParse($this->_oMetatagsContentId, $s);
  607. return $s;
  608. }
  609. function genViewRowWrapped(&$aInput)
  610. {
  611. $sResult = parent::genViewRowWrapped($aInput);
  612. if (!$aInput['rateable']){
  613. return $sResult;
  614. }
  615. $sValue = $this->genViewRowValue($aInput);
  616. if (null === $sValue)
  617. return '';
  618. // process rateable fields
  619. $iId = BxDolFormQuery::getFormField($this->id, $aInput['name'], $this->_iContentId);
  620. $sVote = '';
  621. $oVote = BxDolVote::getObjectInstance($aInput['rateable'], $iId, true, BxDolTemplate::getInstance());
  622. if ($oVote){
  623. $sVote = $oVote->getCounter(array('show_counter_empty' => true, 'show_counter' => true, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode));
  624. $sVoteBtn = $oVote->getElementInline(array('show_counter_empty' => true, 'show_counter' => false, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode));
  625. return $this->oTemplate->parseHtmlByName('form_view_rateable_row.html', array(
  626. 'value' => $sResult,
  627. 'rate' => $sVote,
  628. 'rate_btn' => $sVoteBtn
  629. ));
  630. }
  631. return $sResult;
  632. }
  633. function setMetatagsKeywordsData($iId, $a, $o)
  634. {
  635. $this->_oMetatagsContentId = $iId;
  636. $this->_aMetatagsFieldsWithKeywords = $a;
  637. $this->_oMetatagsObject = $o;
  638. }
  639. function getContentOwnerProfileId ($iContentId)
  640. {
  641. return $this->_oModule->serviceGetContentOwnerProfileId($iContentId);
  642. }
  643. protected function _processTrackFields($mixedContent)
  644. {
  645. if(empty($mixedContent) || empty($this->_aTrackFieldsChanges))
  646. return;
  647. if(!is_array($mixedContent)) {
  648. $mixedContent = $this->_oModule->_oDb->getContentInfoById((int)$mixedContent);
  649. if(empty($mixedContent) || !is_array($mixedContent))
  650. return;
  651. }
  652. foreach($this->_aTrackFieldsChanges as $sField => $aValues)
  653. if($mixedContent[$sField] == $aValues['old'])
  654. $this->_aTrackFieldsChanges[$sField] = false;
  655. else
  656. $this->_aTrackFieldsChanges[$sField]['new'] = $mixedContent[$sField];
  657. }
  658. protected function _processMetas(&$aValsToAdd)
  659. {
  660. $CNF = &$this->_oModule->_oConfig->CNF;
  661. $sKey = 'FIELD_LABELS';
  662. if(isset($CNF[$sKey]) && isset($this->aInputs[$CNF[$sKey]]) && empty($aValsToAdd[$CNF[$sKey]])) {
  663. $aLabels = $this->getCleanValue($CNF[$sKey]);
  664. if(!empty($aLabels) && is_array($aLabels))
  665. $aValsToAdd[$CNF[$sKey]] = serialize($aLabels);
  666. else
  667. $aValsToAdd[$CNF[$sKey]] = '';
  668. }
  669. $sKey1 = 'FIELD_LOCATION';
  670. $sKey2 = 'FIELD_LOCATION_PREFIX';
  671. if(isset($CNF[$sKey1]) && isset($CNF[$sKey2]) && isset($this->aInputs[$CNF[$sKey1]]) && empty($aValsToAdd[$CNF[$sKey1]])) {
  672. $aLocation = BxDolMetatags::locationsRetrieveFromForm($CNF[$sKey2], $this);
  673. if(!empty($aLocation) && is_array($aLocation))
  674. $aValsToAdd[$CNF[$sKey1]] = serialize(BxDolMetatags::locationsParseComponents($aLocation));
  675. }
  676. }
  677. protected function _getPrivacyFields($aKeysF2O = array())
  678. {
  679. $CNF = &$this->_oModule->_oConfig->CNF;
  680. if(isset($CNF['PRIVACY_FIELD_TO_OBJECT']) && is_array($CNF['PRIVACY_FIELD_TO_OBJECT']))
  681. return $CNF['PRIVACY_FIELD_TO_OBJECT'];
  682. if(empty($aKeysF2O))
  683. $aKeysF2O = array('FIELD_ALLOW_VIEW_TO' => 'OBJECT_PRIVACY_VIEW');
  684. $aResult = array();
  685. foreach($aKeysF2O as $sKeyField => $sKeyObject) {
  686. if(!isset($CNF[$sKeyField]) || !isset($this->aInputs[$CNF[$sKeyField]]) || !isset($CNF[$sKeyObject]))
  687. continue;
  688. $aResult[$CNF[$sKeyField]] = $CNF[$sKeyObject];
  689. }
  690. return $aResult;
  691. }
  692. function _addCssJsPrivacyField($sField, $sPrivacyObject, $bDynamicMode = false)
  693. {
  694. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  695. if(!$oPrivacy)
  696. return;
  697. $this->aInputs[$sField]['content'] = $oPrivacy->addCssJs($bDynamicMode) . $this->aInputs[$sField]['content'];
  698. }
  699. protected function _preparePrivacyField($sField, $sPrivacyObject)
  700. {
  701. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  702. if(!$oPrivacy)
  703. return;
  704. $aSave = array('db' => array('pass' => 'Xss'));
  705. array_walk($this->aInputs[$sField], function ($a, $k, $aSave) {
  706. if (in_array($k, array('info', 'caption', 'value')))
  707. $aSave[0][$k] = $a;
  708. }, array(&$aSave));
  709. $aGroupChooser = $oPrivacy->getGroupChooser($sPrivacyObject, 0, array(
  710. 'object' => $this->aParams['object'],
  711. 'display' => $this->aParams['display']
  712. ));
  713. $this->aInputs[$sField] = array_merge($this->aInputs[$sField], $aGroupChooser, $aSave);
  714. }
  715. protected function _preloadPrivacyField($sField, $sPrivacyObject, $aValues)
  716. {
  717. $CNF = &$this->_oModule->_oConfig->CNF;
  718. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  719. if(!$oPrivacy)
  720. return;
  721. $iContentId = !empty($aValues[$CNF['FIELD_ID']]) ? (int)$aValues[$CNF['FIELD_ID']] : 0;
  722. $iProfileId = !empty($iContentId) ? (int)$this->getContentOwnerProfileId($iContentId) : bx_get_logged_profile_id();
  723. $iGroupId = !empty($aValues[$sField]) ? $aValues[$sField] : 0;
  724. if(!isset($this->aInputs[$sField]['content']))
  725. $this->aInputs[$sField]['content'] = '';
  726. $this->aInputs[$sField]['content'] .= $oPrivacy->initGroupChooser($sPrivacyObject, $iProfileId, array(
  727. 'content_id' => $iContentId,
  728. 'group_id' => $iGroupId,
  729. 'html_ids' => array(
  730. 'form' => $this->getId()
  731. )
  732. ));
  733. }
  734. protected function _validatePrivacyField($sField, $sPrivacyObject, $aValues)
  735. {
  736. $mixedValue = $this->aInputs[$sField]['value'];
  737. $bValue = false;
  738. foreach($this->aInputs[$sField]['values'] as $aValue)
  739. if(isset($aValue['key']) && $aValue['key'] == $mixedValue) {
  740. $bValue = true;
  741. break;
  742. }
  743. if(!$bValue) {
  744. $sTitle = '';
  745. if(is_numeric($mixedValue) && (int)$mixedValue < 0 && ($oContext = BxDolProfile::getInstance(abs((int)$mixedValue))) !== false)
  746. $sTitle = $oContext->getDisplayName();
  747. else
  748. $sTitle = _t('_sys_ps_group_title_unknown');
  749. $this->aInputs[$sField]['values'] = array_merge(array(
  750. array('key' => $mixedValue, 'value' => $sTitle),
  751. array('key' => '', 'value' => '----', 'attrs' => array('disabled' => 'disabled'))
  752. ), $this->aInputs[$sField]['values']);
  753. }
  754. }
  755. /**
  756. *
  757. * MultiCategories related methods.
  758. *
  759. */
  760. protected function processMulticatBefore($sFieldName, &$aValsToAdd){
  761. if (isset($this->aInputs[$sFieldName])){
  762. $this->aInputs[$sFieldName]['value'] = array_unique(array_filter($this->aInputs[$sFieldName]['value'], function($sTmp){
  763. return trim($sTmp);
  764. }));
  765. $aValsToAdd[$sFieldName] = implode(',', $this->aInputs[$sFieldName]['value']);
  766. }
  767. }
  768. protected function processMulticatAfter($sFieldName, $iContentId){
  769. $CNF = &$this->_oModule->_oConfig->CNF;
  770. $bAutoActivation = (isset($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) && getParam($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) == 'on') ? true : false;
  771. $oCategories = BxDolCategories::getInstance();
  772. if (isset($this->aInputs[$sFieldName])){
  773. $oCategories->delete($this->_oModule->getName(), $iContentId);
  774. foreach($this->aInputs[$sFieldName]['value'] as $sValue) {
  775. $oCategories->add($this->_oModule->getName(), bx_get_logged_profile_id(), $sValue, $iContentId, $bAutoActivation);
  776. }
  777. }
  778. }
  779. protected function genCustomViewRowValueMulticat(&$aInput)
  780. {
  781. $oCategories = BxDolCategories::getInstance();
  782. $aValues = $oCategories->getData(array('type' => 'by_module_and_object', 'module' => $this->_oModule->getName(), 'object_id' => (!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0 )));
  783. if (count($aValues) > 0){
  784. $aVars = array('bx_repeat:cats' => array());
  785. foreach ($aValues as $sValue) {
  786. $aVars['bx_repeat:cats'][] = array(
  787. 'url' => $oCategories->getUrl($this->_oModule->getName(), $sValue),
  788. 'name' => _t($sValue),
  789. 'bx_if:more' => array(
  790. 'condition' => $sValue === end($aValues) ? false : true,
  791. 'content' => array('1')
  792. ),
  793. );
  794. }
  795. if (!$aVars['bx_repeat:cats'])
  796. return '';
  797. return $this->_oModule->_oTemplate->parseHtmlByName('category_list_inline.html', $aVars);
  798. }
  799. return '';
  800. }
  801. protected function genCustomInputMulticat(&$aInput)
  802. {
  803. $sJsObject = $this->_oModule->_oConfig->getJsObject('categories');
  804. $aValuesForSelect = BxDolCategories::getInstance()->getData(array('type' => 'by_module_and_author', 'module' => $this->_oModule->getName(), 'author' => bx_get_logged_profile_id()));
  805. $aSelectedItems = array();
  806. if (isset($aInput['value']) && is_array($aInput['value']))
  807. $aInput['value'] = array_filter($aInput['value']);
  808. if(!empty($aInput['value'])) {
  809. if (!is_array($aInput['value']))
  810. $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 )));
  811. else
  812. $aValues = $aInput['value'];
  813. $aValues = array_filter($aValues);
  814. foreach($aValues as $sValue) {
  815. if (!array_key_exists($sValue, $aValuesForSelect)){
  816. $aValuesForSelect[$sValue] = array('key' => $sValue, 'value' => $sValue);
  817. }
  818. }
  819. foreach($aValues as $sValue) {
  820. $sInput = $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, $sValue);
  821. $aSelectedItems[] = array('js_object' => $sJsObject, 'select_cat' => $sInput);
  822. }
  823. }
  824. else{
  825. $aSelectedItems = array(
  826. array('js_object' => $sJsObject, 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect))
  827. );
  828. }
  829. return $this->_oModule->_oTemplate->parseHtmlByName('form_categories.html', array(
  830. 'bx_repeat:items' => $aSelectedItems,
  831. 'js_object' => $sJsObject,
  832. 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, -1),
  833. 'input_cat' => $this->genCustomInputMulticatInput($aInput),
  834. 'js_object' => $sJsObject,
  835. 'btn_add' => $this->genCustomInputMulticatButton($aInput),
  836. 'btn_add_new' => $this->genCustomInputMulticatButtonNew($aInput)
  837. ));
  838. }
  839. protected function genCustomInputMulticatSelect($aInput, $aValues, $mixedValue = '')
  840. {
  841. foreach($aValues as $sKey => $aValue) {
  842. $aValues[$sKey]['value'] = _t($aValue['key']);
  843. }
  844. $aValues = array_merge(array('1' => array('key' => false, 'value' => _t('_sys_please_select'))), $aValues);
  845. $aInput['type'] = 'select';
  846. $aInput['name'] .= '[]';
  847. $aInput['value'] = $mixedValue;
  848. $aInput['values'] = $aValues;
  849. return $this->genInput($aInput);
  850. }
  851. protected function genCustomInputMulticatInput($aInput)
  852. {
  853. $aInput['type'] = 'text';
  854. $aInput['name'] .= '[]';
  855. $aInput['value'] = '';
  856. return $this->genInput($aInput);
  857. }
  858. protected function genCustomInputMulticatButton($aInput)
  859. {
  860. $sName = $aInput['name'];
  861. $aInput['type'] = 'button';
  862. $aInput['name'] .= '_add';
  863. $aInput['value'] = _t('_sys_txt_categories_button_caption_add');
  864. $aInput['attrs']['class'] = 'bx-def-margin-right bx-def-margin-sec-top';
  865. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAdd(this, '" . $sName . "');";
  866. return $this->genInputButton($aInput);
  867. }
  868. protected function genCustomInputMulticatButtonNew($aInput)
  869. {
  870. $sName = $aInput['name'];
  871. $aInput['type'] = 'button';
  872. $aInput['name'] .= '_add';
  873. $aInput['value'] = _t('_sys_txt_categories_button_caption_addnew');
  874. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  875. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAddNew(this, '" . $sName . "');";
  876. return $this->genInputButton($aInput);
  877. }
  878. protected function genCustomInputAuthor ($aInput)
  879. {
  880. if(empty($aInput['custom']) || !is_array($aInput['custom']))
  881. $aInput['custom'] = array();
  882. $aInput['custom']['only_once'] = 1;
  883. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . "modules/?r=" . $this->_oModule->_oConfig->getUri() . "/ajax_get_profiles";
  884. return $this->genCustomInputUsernamesSuggestions($aInput);
  885. }
  886. protected function _isMulticatEnabled(){
  887. $CNF = $this->_oModule->_oConfig->CNF;
  888. return isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true && isset($CNF['FIELD_MULTICAT']);
  889. }
  890. }
  891. /** @} */