1
0

BxBaseModGeneralFormEntry.php 46 KB

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