BxBaseModGeneralFormEntry.php 46 KB

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