BxBaseModGeneralFormEntry.php 38 KB

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