BxBaseModTextFormEntry.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup BaseText Base classes for text modules
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxTemplFormView');
  12. /**
  13. * Create/Edit entry form
  14. */
  15. class BxBaseModTextFormEntry extends BxTemplFormView
  16. {
  17. protected $MODULE;
  18. protected $_oModule;
  19. public function __construct($aInfo, $oTemplate = false)
  20. {
  21. parent::__construct($aInfo, $oTemplate);
  22. $this->_oModule = BxDolModule::getInstance($this->MODULE);
  23. $CNF = &$this->_oModule->_oConfig->CNF;
  24. if (isset($this->aInputs[$CNF['FIELD_TEXT']])) {
  25. $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] = array_merge (
  26. array ('id' => $CNF['FIELD_TEXT_ID']),
  27. is_array($this->aInputs[$CNF['FIELD_TEXT']]['attrs']) ? $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] : array ()
  28. );
  29. }
  30. if (isset($this->aInputs[$CNF['FIELD_SUMMARY']])) {
  31. $this->aInputs[$CNF['FIELD_SUMMARY']]['attrs'] = array_merge (
  32. array ('id' => $CNF['FIELD_SUMMARY_ID']),
  33. is_array($this->aInputs[$CNF['FIELD_SUMMARY']]['attrs']) ? $this->aInputs[$CNF['FIELD_SUMMARY']]['attrs'] : array ()
  34. );
  35. }
  36. if (isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  37. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_object'] = $CNF['OBJECT_STORAGE'];
  38. $this->aInputs[$CNF['FIELD_PHOTO']]['uploaders'] = array('sys_simple', 'sys_html5');
  39. $this->aInputs[$CNF['FIELD_PHOTO']]['images_transcoder'] = $CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW'];
  40. $this->aInputs[$CNF['FIELD_PHOTO']]['multiple'] = true;
  41. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = 0;
  42. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = '';
  43. }
  44. if (isset($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']])) {
  45. bx_import('BxDolPrivacy');
  46. $this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']] = BxDolPrivacy::getGroupChooser($CNF['OBJECT_PRIVACY_VIEW']);
  47. }
  48. }
  49. function initChecker ($aValues = array (), $aSpecificValues = array())
  50. {
  51. $CNF = &$this->_oModule->_oConfig->CNF;
  52. if (isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  53. $aContentInfo = false;
  54. if ($aValues && !empty($aValues['id'])) {
  55. $aContentInfo = $this->_oModule->_oDb->getContentInfoById ($aValues['id']);
  56. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = $aValues['id'];
  57. }
  58. $aVars = array (
  59. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  60. 'content_id' => $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  61. 'editor_id' => $CNF['FIELD_TEXT_ID'],
  62. 'summary_id' => $CNF['FIELD_SUMMARY_ID'],
  63. 'thumb_id' => isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  64. 'bx_if:set_thumb' => array (
  65. 'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb(),
  66. 'content' => array (
  67. 'name_thumb' => $CNF['FIELD_THUMB'],
  68. ),
  69. ),
  70. );
  71. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName('form_ghost_template.html', $aVars);
  72. }
  73. return parent::initChecker($aValues, $aSpecificValues);
  74. }
  75. public function insert ($aValsToAdd = array(), $isIgnore = false)
  76. {
  77. $CNF = &$this->_oModule->_oConfig->CNF;
  78. $aValsToAdd[$CNF['FIELD_AUTHOR']] = bx_get_logged_profile_id ();
  79. if (isset($CNF['FIELD_ADDED']))
  80. $aValsToAdd[$CNF['FIELD_ADDED']] = time();
  81. if (isset($CNF['FIELD_CHANGED']))
  82. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  83. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb()) {
  84. $aThumb = isset($_POST[$CNF['FIELD_THUMB']]) ? bx_process_input ($_POST[$CNF['FIELD_THUMB']], BX_DATA_INT) : false;
  85. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  86. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  87. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  88. }
  89. if ($iContentId = parent::insert ($aValsToAdd, $isIgnore))
  90. $this->_processFiles ($this->getCleanValue($CNF['FIELD_PHOTO']), $iContentId, true);
  91. return $iContentId;
  92. }
  93. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  94. {
  95. $CNF = &$this->_oModule->_oConfig->CNF;
  96. if (isset($CNF['FIELD_CHANGED']))
  97. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  98. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb()) {
  99. $aThumb = bx_process_input ($_POST[$CNF['FIELD_THUMB']], BX_DATA_INT);
  100. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  101. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  102. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  103. }
  104. if ($iRet = parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges))
  105. $this->_processFiles ($this->getCleanValue($CNF['FIELD_PHOTO']), $iContentId, false);
  106. return $iRet;
  107. }
  108. function delete ($iContentId)
  109. {
  110. $CNF = &$this->_oModule->_oConfig->CNF;
  111. // delete associated files
  112. bx_import('BxDolStorage');
  113. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  114. if (!$oStorage)
  115. return false;
  116. $aGhostFiles = $oStorage->getGhosts (bx_get_logged_profile_id(), $iContentId);
  117. if ($aGhostFiles)
  118. foreach ($aGhostFiles as $aFile)
  119. $this->_deleteFile($aFile['id']);
  120. // delete db record
  121. bx_import('BxDolView');
  122. $o = BxDolView::getObjectInstance($CNF['OBJECT_VIEWS'], $iContentId);
  123. if ($o) $o->onObjectDelete();
  124. bx_import('BxDolVote');
  125. $o = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $iContentId);
  126. if ($o) $o->onObjectDelete();
  127. bx_import('BxDolCmts');
  128. $o = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  129. if ($o) $o->onObjectDelete();
  130. return parent::delete($iContentId);
  131. }
  132. function _processFiles ($mixedFileIds, $iContentId = 0, $isAssociateWithContent = false)
  133. {
  134. $CNF = &$this->_oModule->_oConfig->CNF;
  135. if (!$mixedFileIds)
  136. return true;
  137. bx_import('BxDolStorage');
  138. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  139. if (!$oStorage)
  140. return false;
  141. $iProfileId = bx_get_logged_profile_id();
  142. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId);
  143. if (!$aGhostFiles)
  144. return true;
  145. foreach ($aGhostFiles as $aFile) {
  146. if ($aFile['private'])
  147. $oStorage->setFilePrivate ($aFile['id'], 0);
  148. if ($isAssociateWithContent && $iContentId)
  149. $oStorage->updateGhostsContentId ($aFile['id'], $iProfileId, $iContentId);
  150. }
  151. return true;
  152. }
  153. function _deleteFile ($iFileId)
  154. {
  155. $CNF = &$this->_oModule->_oConfig->CNF;
  156. if (!$iFileId)
  157. return true;
  158. bx_import('BxDolStorage');
  159. if (!($oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE'])))
  160. return false;
  161. if (!$oStorage->getFile($iFileId))
  162. return true;
  163. $iProfileId = bx_get_logged_profile_id();
  164. return $oStorage->deleteFile($iFileId, $iProfileId);
  165. }
  166. function addCssJs ()
  167. {
  168. if (!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) {
  169. if (self::$_isCssJsAdded)
  170. return;
  171. $this->_oModule->_oTemplate->addJs('modules/base/text/js/|forms.js');
  172. $this->_oModule->_oTemplate->addCss('forms.css');
  173. }
  174. return parent::addCssJs ();
  175. }
  176. }
  177. /** @} */