1
0

BxBaseModGeneralFormEntry.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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($this->getCleanValue($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 (!is_array($aThumb))
  443. $aThumb =[$aThumb];
  444. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  445. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  446. }
  447. if(!empty($CNF['OBJECT_METATAGS']))
  448. $this->_processMetas($aValsToAdd);
  449. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  450. $aValsToAdd[$CNF['FIELD_STATUS']] = 'active';
  451. $bMulticatEnabled = $this->_isMulticatEnabled();
  452. if ($bMulticatEnabled)
  453. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  454. $mixedResult = parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  455. foreach($this->aInputs as $aInput) {
  456. if ($aInput['type'] == 'nested_form'){
  457. if (is_array($aInput['ghost_template']) && !isset($aInput['ghost_template']['inputs'])) {
  458. foreach ($aInput['ghost_template'] as $oFormNested) {
  459. $aSpecificValues = $oFormNested->getSpecificValues();
  460. $iNestedContentId = $oFormNested->getSubmittedValue($aInput['name'], BX_DOL_FORM_METHOD_SPECIFIC, $aSpecificValues);
  461. if (empty($iNestedContentId)){
  462. $iNestedContentId = $oFormNested->insert(array('content_id' => $iContentId));
  463. if ($aInput['rateable']){
  464. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName(), $iNestedContentId);
  465. }
  466. }
  467. else{
  468. $oFormNested->update($iNestedContentId, array('content_id' => $iContentId));
  469. }
  470. }
  471. }
  472. }
  473. }
  474. if ($bMulticatEnabled)
  475. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  476. foreach($this->aInputs as $aInput) {
  477. if (isset($aInput['rateable']) && $aInput['rateable'] && $aInput['type'] != 'nested_form'){
  478. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName());
  479. }
  480. }
  481. if($mixedResult !== false)
  482. $this->_processTrackFields($iContentId);
  483. return $mixedResult;
  484. }
  485. function getHtmlEditorQueryParams($aInput)
  486. {
  487. $aQueryParams = parent::getHtmlEditorQueryParams($aInput);
  488. if (isset($this->MODULE)){
  489. $aQueryParams['m'] = $this->MODULE;
  490. }
  491. if (isset($this->_iContentId) && $this->_iContentId){
  492. $aQueryParams['cid'] = $this->_iContentId;
  493. }
  494. $aQueryParams['fi'] = '';
  495. bx_alert('system', 'editor_query_params', 0, 0, array(
  496. 'form' => $this,
  497. 'override_result' => &$aQueryParams
  498. ));
  499. return $aQueryParams;
  500. }
  501. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  502. {
  503. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  504. }
  505. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  506. {
  507. $CNF = &$this->_oModule->_oConfig->CNF;
  508. return array (
  509. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  510. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  511. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  512. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  513. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  514. 'bx_if:set_thumb' => array (
  515. 'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($this->aInputs[$CNF['FIELD_PHOTO']]['content_id']),
  516. 'content' => array (
  517. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  518. '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')
  519. ),
  520. ),
  521. );
  522. }
  523. function delete ($iContentId, $aContentInfo = array())
  524. {
  525. $CNF = &$this->_oModule->_oConfig->CNF;
  526. // delete associated files
  527. $a = array('OBJECT_STORAGE', 'OBJECT_STORAGE_FILES', 'OBJECT_STORAGE_PHOTOS', 'OBJECT_STORAGE_VIDEOS', 'OBJECT_STORAGE_SOUNDS');
  528. foreach ($a as $k) {
  529. if (!empty($CNF[$k])) {
  530. $oStorage = BxDolStorage::getObjectInstance($CNF[$k]);
  531. if ($oStorage)
  532. $oStorage->queueFilesForDeletionFromGhosts($aContentInfo[$CNF['FIELD_AUTHOR']], $iContentId);
  533. }
  534. }
  535. // delete associated objects data
  536. if (!empty($CNF['OBJECT_VIEWS'])) {
  537. $o = BxDolView::getObjectInstance($CNF['OBJECT_VIEWS'], $iContentId);
  538. if ($o) $o->onObjectDelete();
  539. }
  540. if (!empty($CNF['OBJECT_VOTES'])) {
  541. $o = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $iContentId);
  542. if ($o) $o->onObjectDelete();
  543. }
  544. if (!empty($CNF['OBJECT_SCORES'])) {
  545. $o = BxDolScore::getObjectInstance($CNF['OBJECT_SCORES'], $iContentId);
  546. if ($o) $o->onObjectDelete();
  547. }
  548. if (!empty($CNF['OBJECT_COMMENTS'])) {
  549. $o = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  550. if ($o) $o->onObjectDelete();
  551. }
  552. if (!empty($CNF['OBJECT_REPORTS'])) {
  553. $o = BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS'], $iContentId);
  554. if ($o) $o->onObjectDelete();
  555. }
  556. if (!empty($CNF['OBJECT_METATAGS'])) {
  557. $o = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  558. if ($o) $o->onDeleteContent($iContentId);
  559. }
  560. // delete SEO links
  561. BxDolPage::deleteSeoLink ($this->_oModule->getName(), $this->_oModule->getName(), $iContentId);
  562. // delete db record
  563. BxDolFormQuery::removeFormField($this->id, $iContentId);
  564. return parent::delete($iContentId);
  565. }
  566. protected function _isAdmin ($iContentId = 0)
  567. {
  568. return $this->_oModule->_isModerator();
  569. }
  570. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  571. {
  572. $CNF = &$this->_oModule->_oConfig->CNF;
  573. if (!isset($this->aInputs[$sFieldFile]))
  574. return true;
  575. $mixedFileIds = $this->getCleanValue($sFieldFile);
  576. if(!$mixedFileIds)
  577. return true;
  578. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  579. if (!$oStorage)
  580. return false;
  581. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  582. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true, $this->_isAdmin($iContentId));
  583. if (!$aGhostFiles)
  584. return true;
  585. foreach ($aGhostFiles as $aFile) {
  586. if (is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  587. continue;
  588. if ($aFile['private'])
  589. $oStorage->setFilePrivate ($aFile['id'], 1);
  590. if ($iContentId)
  591. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  592. }
  593. return true;
  594. }
  595. function _deleteFile ($iFileId, $sStorage = '')
  596. {
  597. $CNF = &$this->_oModule->_oConfig->CNF;
  598. if (!$iFileId)
  599. return true;
  600. $sStorage = !empty($sStorage) ? $sStorage : $CNF['OBJECT_STORAGE'];
  601. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  602. if (!$oStorage)
  603. return false;
  604. if (!$oStorage->getFile($iFileId))
  605. return true;
  606. $iProfileId = bx_get_logged_profile_id();
  607. return $oStorage->deleteFile($iFileId, $iProfileId);
  608. }
  609. function addCssJs ()
  610. {
  611. if ((!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) && !self::$_isCssJsGeneralModuleAdded) {
  612. $this->_oModule->_oTemplate->addCss('forms.css');
  613. $this->_oModule->_oTemplate->addJs('modules/base/general/js/|forms.js');
  614. self::$_isCssJsGeneralModuleAdded = true;
  615. }
  616. return parent::addCssJs ();
  617. }
  618. function genViewRowValue(&$aInput)
  619. {
  620. $s = parent::genViewRowValue($aInput);
  621. if ($this->_oMetatagsObject && in_array($aInput['name'], $this->_aMetatagsFieldsWithKeywords) && $s)
  622. $s = $this->_oMetatagsObject->metaParse($this->_oMetatagsContentId, $s);
  623. return $s;
  624. }
  625. function genViewRowWrapped(&$aInput)
  626. {
  627. $sResult = parent::genViewRowWrapped($aInput);
  628. if (!$aInput['rateable']){
  629. return $sResult;
  630. }
  631. $sValue = $this->genViewRowValue($aInput);
  632. if (null === $sValue)
  633. return '';
  634. // process rateable fields
  635. $iId = BxDolFormQuery::getFormField($this->id, $aInput['name'], $this->_iContentId);
  636. $sVote = '';
  637. $oVote = BxDolVote::getObjectInstance($aInput['rateable'], $iId, true, BxDolTemplate::getInstance());
  638. if ($oVote){
  639. $sVote = $oVote->getCounter(array('show_counter_empty' => true, 'show_counter' => true, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode));
  640. $sVoteBtn = $oVote->getElementInline(array('show_counter_empty' => true, 'show_counter' => false, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode));
  641. return $this->oTemplate->parseHtmlByName('form_view_rateable_row.html', array(
  642. 'value' => $sResult,
  643. 'rate' => $sVote,
  644. 'rate_btn' => $sVoteBtn
  645. ));
  646. }
  647. return $sResult;
  648. }
  649. function setMetatagsKeywordsData($iId, $a, $o)
  650. {
  651. $this->_oMetatagsContentId = $iId;
  652. $this->_aMetatagsFieldsWithKeywords = $a;
  653. $this->_oMetatagsObject = $o;
  654. }
  655. function getContentOwnerProfileId ($iContentId)
  656. {
  657. return $this->_oModule->serviceGetContentOwnerProfileId($iContentId);
  658. }
  659. protected function _processTrackFields($mixedContent)
  660. {
  661. if(empty($mixedContent) || empty($this->_aTrackFieldsChanges))
  662. return;
  663. if(!is_array($mixedContent)) {
  664. $mixedContent = $this->_oModule->_oDb->getContentInfoById((int)$mixedContent);
  665. if(empty($mixedContent) || !is_array($mixedContent))
  666. return;
  667. }
  668. foreach($this->_aTrackFieldsChanges as $sField => $aValues)
  669. if($mixedContent[$sField] == $aValues['old'])
  670. $this->_aTrackFieldsChanges[$sField] = false;
  671. else
  672. $this->_aTrackFieldsChanges[$sField]['new'] = $mixedContent[$sField];
  673. }
  674. protected function _processMetas(&$aValsToAdd)
  675. {
  676. $CNF = &$this->_oModule->_oConfig->CNF;
  677. $sKey = 'FIELD_LABELS';
  678. if(isset($CNF[$sKey]) && isset($this->aInputs[$CNF[$sKey]]) && empty($aValsToAdd[$CNF[$sKey]])) {
  679. $aLabels = $this->getCleanValue($CNF[$sKey]);
  680. if(!empty($aLabels) && is_array($aLabels))
  681. $aValsToAdd[$CNF[$sKey]] = serialize($aLabels);
  682. else
  683. $aValsToAdd[$CNF[$sKey]] = '';
  684. }
  685. $sKey1 = 'FIELD_LOCATION';
  686. $sKey2 = 'FIELD_LOCATION_PREFIX';
  687. if(isset($CNF[$sKey1]) && isset($CNF[$sKey2]) && isset($this->aInputs[$CNF[$sKey1]]) && empty($aValsToAdd[$CNF[$sKey1]])) {
  688. $aLocation = BxDolMetatags::locationsRetrieveFromForm($CNF[$sKey2], $this);
  689. if(!empty($aLocation) && is_array($aLocation))
  690. $aValsToAdd[$CNF[$sKey1]] = serialize(BxDolMetatags::locationsParseComponents($aLocation));
  691. }
  692. }
  693. protected function _getPrivacyFields($aKeysF2O = array())
  694. {
  695. $CNF = &$this->_oModule->_oConfig->CNF;
  696. if(isset($CNF['PRIVACY_FIELD_TO_OBJECT']) && is_array($CNF['PRIVACY_FIELD_TO_OBJECT']))
  697. return $CNF['PRIVACY_FIELD_TO_OBJECT'];
  698. if(empty($aKeysF2O))
  699. $aKeysF2O = array('FIELD_ALLOW_VIEW_TO' => 'OBJECT_PRIVACY_VIEW');
  700. $aResult = array();
  701. foreach($aKeysF2O as $sKeyField => $sKeyObject) {
  702. if(!isset($CNF[$sKeyField]) || !isset($this->aInputs[$CNF[$sKeyField]]) || !isset($CNF[$sKeyObject]))
  703. continue;
  704. $aResult[$CNF[$sKeyField]] = $CNF[$sKeyObject];
  705. }
  706. return $aResult;
  707. }
  708. function _addCssJsPrivacyField($sField, $sPrivacyObject, $bDynamicMode = false)
  709. {
  710. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  711. if(!$oPrivacy)
  712. return;
  713. $this->aInputs[$sField]['content'] = $oPrivacy->addCssJs($bDynamicMode) . $this->aInputs[$sField]['content'];
  714. }
  715. protected function _preparePrivacyField($sField, $sPrivacyObject)
  716. {
  717. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  718. if(!$oPrivacy)
  719. return;
  720. $aSave = array('db' => array('pass' => 'Xss'));
  721. array_walk($this->aInputs[$sField], function ($a, $k, $aSave) {
  722. if (in_array($k, array('info', 'caption', 'value')))
  723. $aSave[0][$k] = $a;
  724. }, array(&$aSave));
  725. $aGroupChooser = $oPrivacy->getGroupChooser($sPrivacyObject, 0, array(
  726. 'object' => $this->aParams['object'],
  727. 'display' => $this->aParams['display']
  728. ));
  729. $this->aInputs[$sField] = array_merge($this->aInputs[$sField], $aGroupChooser, $aSave);
  730. }
  731. protected function _preloadPrivacyField($sField, $sPrivacyObject, $aValues)
  732. {
  733. $CNF = &$this->_oModule->_oConfig->CNF;
  734. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  735. if(!$oPrivacy)
  736. return;
  737. $iContentId = !empty($aValues[$CNF['FIELD_ID']]) ? (int)$aValues[$CNF['FIELD_ID']] : 0;
  738. $iProfileId = !empty($iContentId) ? (int)$this->getContentOwnerProfileId($iContentId) : bx_get_logged_profile_id();
  739. $iGroupId = !empty($aValues[$sField]) ? $aValues[$sField] : 0;
  740. if(!isset($this->aInputs[$sField]['content']))
  741. $this->aInputs[$sField]['content'] = '';
  742. $this->aInputs[$sField]['content'] .= $oPrivacy->initGroupChooser($sPrivacyObject, $iProfileId, array(
  743. 'content_id' => $iContentId,
  744. 'group_id' => $iGroupId,
  745. 'html_ids' => array(
  746. 'form' => $this->getId()
  747. )
  748. ));
  749. }
  750. protected function _validatePrivacyField($sField, $sPrivacyObject, $aValues)
  751. {
  752. $mixedValue = $this->aInputs[$sField]['value'];
  753. $bValue = false;
  754. foreach($this->aInputs[$sField]['values'] as $aValue)
  755. if(isset($aValue['key']) && $aValue['key'] == $mixedValue) {
  756. $bValue = true;
  757. break;
  758. }
  759. if(!$bValue) {
  760. $sTitle = '';
  761. if(is_numeric($mixedValue) && (int)$mixedValue < 0 && ($oContext = BxDolProfile::getInstance(abs((int)$mixedValue))) !== false)
  762. $sTitle = $oContext->getDisplayName();
  763. else
  764. $sTitle = _t('_sys_ps_group_title_unknown');
  765. $this->aInputs[$sField]['values'] = array_merge(array(
  766. array('key' => $mixedValue, 'value' => $sTitle),
  767. array('key' => '', 'value' => '----', 'attrs' => array('disabled' => 'disabled'))
  768. ), $this->aInputs[$sField]['values']);
  769. }
  770. }
  771. /**
  772. *
  773. * MultiCategories related methods.
  774. *
  775. */
  776. protected function processMulticatBefore($sFieldName, &$aValsToAdd){
  777. if (isset($this->aInputs[$sFieldName])){
  778. $this->aInputs[$sFieldName]['value'] = array_unique(array_filter($this->aInputs[$sFieldName]['value'], function($sTmp){
  779. return trim($sTmp);
  780. }));
  781. $aValsToAdd[$sFieldName] = implode(',', $this->aInputs[$sFieldName]['value']);
  782. }
  783. }
  784. protected function processMulticatAfter($sFieldName, $iContentId){
  785. $CNF = &$this->_oModule->_oConfig->CNF;
  786. $bAutoActivation = (isset($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) && getParam($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) == 'on') ? true : false;
  787. $oCategories = BxDolCategories::getInstance();
  788. if (isset($this->aInputs[$sFieldName])){
  789. $oCategories->delete($this->_oModule->getName(), $iContentId);
  790. foreach($this->aInputs[$sFieldName]['value'] as $sValue) {
  791. $oCategories->add($this->_oModule->getName(), bx_get_logged_profile_id(), $sValue, $iContentId, $bAutoActivation);
  792. }
  793. }
  794. }
  795. protected function genCustomViewRowValueMulticat(&$aInput)
  796. {
  797. $oCategories = BxDolCategories::getInstance();
  798. $aValues = $oCategories->getData(array('type' => 'by_module_and_object', 'module' => $this->_oModule->getName(), 'object_id' => (!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0 )));
  799. if (count($aValues) > 0){
  800. $aVars = array('bx_repeat:cats' => array());
  801. foreach ($aValues as $sValue) {
  802. $aVars['bx_repeat:cats'][] = array(
  803. 'url' => $oCategories->getUrl($this->_oModule->getName(), $sValue),
  804. 'name' => _t($sValue),
  805. 'bx_if:more' => array(
  806. 'condition' => $sValue === end($aValues) ? false : true,
  807. 'content' => array('1')
  808. ),
  809. );
  810. }
  811. if (!$aVars['bx_repeat:cats'])
  812. return '';
  813. return $this->_oModule->_oTemplate->parseHtmlByName('category_list_inline.html', $aVars);
  814. }
  815. return '';
  816. }
  817. protected function genCustomInputMulticat(&$aInput)
  818. {
  819. $sJsObject = $this->_oModule->_oConfig->getJsObject('categories');
  820. $aValuesForSelect = BxDolCategories::getInstance()->getData(array('type' => 'by_module_and_author', 'module' => $this->_oModule->getName(), 'author' => bx_get_logged_profile_id()));
  821. $aSelectedItems = array();
  822. if (isset($aInput['value']) && is_array($aInput['value']))
  823. $aInput['value'] = array_filter($aInput['value']);
  824. if(!empty($aInput['value'])) {
  825. if (!is_array($aInput['value']))
  826. $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 )));
  827. else
  828. $aValues = $aInput['value'];
  829. $aValues = array_filter($aValues);
  830. foreach($aValues as $sValue) {
  831. if (!array_key_exists($sValue, $aValuesForSelect)){
  832. $aValuesForSelect[$sValue] = array('key' => $sValue, 'value' => $sValue);
  833. }
  834. }
  835. foreach($aValues as $sValue) {
  836. $sInput = $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, $sValue);
  837. $aSelectedItems[] = array('js_object' => $sJsObject, 'select_cat' => $sInput);
  838. }
  839. }
  840. else{
  841. $aSelectedItems = array(
  842. array('js_object' => $sJsObject, 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect))
  843. );
  844. }
  845. return $this->_oModule->_oTemplate->parseHtmlByName('form_categories.html', array(
  846. 'bx_repeat:items' => $aSelectedItems,
  847. 'js_object' => $sJsObject,
  848. 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, -1),
  849. 'input_cat' => $this->genCustomInputMulticatInput($aInput),
  850. 'js_object' => $sJsObject,
  851. 'btn_add' => $this->genCustomInputMulticatButton($aInput),
  852. 'btn_add_new' => $this->genCustomInputMulticatButtonNew($aInput)
  853. ));
  854. }
  855. protected function genCustomInputMulticatSelect($aInput, $aValues, $mixedValue = '')
  856. {
  857. foreach($aValues as $sKey => $aValue) {
  858. $aValues[$sKey]['value'] = _t($aValue['key']);
  859. }
  860. $aValues = array_merge(array('1' => array('key' => false, 'value' => _t('_sys_please_select'))), $aValues);
  861. $aInput['type'] = 'select';
  862. $aInput['name'] .= '[]';
  863. $aInput['value'] = $mixedValue;
  864. $aInput['values'] = $aValues;
  865. return $this->genInput($aInput);
  866. }
  867. protected function genCustomInputMulticatInput($aInput)
  868. {
  869. $aInput['type'] = 'text';
  870. $aInput['name'] .= '[]';
  871. $aInput['value'] = '';
  872. return $this->genInput($aInput);
  873. }
  874. protected function genCustomInputMulticatButton($aInput)
  875. {
  876. $sName = $aInput['name'];
  877. $aInput['type'] = 'button';
  878. $aInput['name'] .= '_add';
  879. $aInput['value'] = _t('_sys_txt_categories_button_caption_add');
  880. $aInput['attrs']['class'] = 'bx-def-margin-right bx-def-margin-sec-top';
  881. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAdd(this, '" . $sName . "');";
  882. return $this->genInputButton($aInput);
  883. }
  884. protected function genCustomInputMulticatButtonNew($aInput)
  885. {
  886. $sName = $aInput['name'];
  887. $aInput['type'] = 'button';
  888. $aInput['name'] .= '_add';
  889. $aInput['value'] = _t('_sys_txt_categories_button_caption_addnew');
  890. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  891. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAddNew(this, '" . $sName . "');";
  892. return $this->genInputButton($aInput);
  893. }
  894. protected function genCustomInputAuthor ($aInput)
  895. {
  896. if(empty($aInput['custom']) || !is_array($aInput['custom']))
  897. $aInput['custom'] = array();
  898. $aInput['custom']['only_once'] = 1;
  899. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . "modules/?r=" . $this->_oModule->_oConfig->getUri() . "/ajax_get_profiles";
  900. return $this->genCustomInputUsernamesSuggestions($aInput);
  901. }
  902. protected function _isMulticatEnabled(){
  903. $CNF = $this->_oModule->_oConfig->CNF;
  904. return isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true && isset($CNF['FIELD_MULTICAT']);
  905. }
  906. }
  907. /** @} */