BxBaseModGeneralFormEntry.php 45 KB

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