BxNotesFormNote.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Notes Notes
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxTemplFormView');
  12. /**
  13. * Create/Edit Note Form.
  14. */
  15. class BxNotesFormNote extends BxTemplFormView {
  16. protected $_oModule;
  17. public function __construct($aInfo, $oTemplate = false) {
  18. parent::__construct($aInfo, $oTemplate);
  19. $this->_oModule = BxDolModule::getInstance('bx_notes');
  20. if (isset($this->aInputs[BxNotesConfig::$FIELD_TEXT])) {
  21. $this->aInputs[BxNotesConfig::$FIELD_TEXT]['attrs'] = array_merge (
  22. array ('id' => BxNotesConfig::$FIELD_TEXT_ID),
  23. is_array($this->aInputs[BxNotesConfig::$FIELD_TEXT]['attrs']) ? $this->aInputs[BxNotesConfig::$FIELD_TEXT]['attrs'] : array ()
  24. );
  25. }
  26. if (isset($this->aInputs[BxNotesConfig::$FIELD_SUMMARY])) {
  27. $this->aInputs[BxNotesConfig::$FIELD_SUMMARY]['attrs'] = array_merge (
  28. array ('id' => BxNotesConfig::$FIELD_SUMMARY_ID),
  29. is_array($this->aInputs[BxNotesConfig::$FIELD_SUMMARY]['attrs']) ? $this->aInputs[BxNotesConfig::$FIELD_SUMMARY]['attrs'] : array ()
  30. );
  31. }
  32. if (isset($this->aInputs[BxNotesConfig::$FIELD_PHOTO])) {
  33. $this->aInputs[BxNotesConfig::$FIELD_PHOTO]['storage_object'] = BxNotesConfig::$OBJECT_STORAGE;
  34. $this->aInputs[BxNotesConfig::$FIELD_PHOTO]['uploaders'] = array('Simple', 'HTML5');
  35. $this->aInputs[BxNotesConfig::$FIELD_PHOTO]['images_transcoder'] = BxNotesConfig::$OBJECT_IMAGES_TRANSCODER_PREVIEW;
  36. $this->aInputs[BxNotesConfig::$FIELD_PHOTO]['multiple'] = true;
  37. $this->aInputs[BxNotesConfig::$FIELD_PHOTO]['content_id'] = 0;
  38. //$this->aInputs[BxNotesConfig::$FIELD_PHOTO]['upload_buttons_titles'] = _t('_bx_notes_form_note_input_photo_btn_upload');
  39. $this->aInputs[BxNotesConfig::$FIELD_PHOTO]['ghost_template'] = '';
  40. }
  41. }
  42. function initChecker ($aValues = array (), $aSpecificValues = array()) {
  43. if (isset($this->aInputs[BxNotesConfig::$FIELD_PHOTO])) {
  44. $aContentInfo = false;
  45. if ($aValues && !empty($aValues['id'])) {
  46. $aContentInfo = $this->_oModule->_oDb->getContentInfoById ($aValues['id']);
  47. $this->aInputs[BxNotesConfig::$FIELD_PHOTO]['content_id'] = $aValues['id'];
  48. }
  49. $aVars = array (
  50. 'name' => $this->aInputs[BxNotesConfig::$FIELD_PHOTO]['name'],
  51. 'content_id' => $this->aInputs[BxNotesConfig::$FIELD_PHOTO]['content_id'],
  52. 'editor_id' => BxNotesConfig::$FIELD_TEXT_ID,
  53. 'summary_id' => BxNotesConfig::$FIELD_SUMMARY_ID,
  54. 'thumb_id' => $aContentInfo[BxNotesConfig::$FIELD_THUMB],
  55. 'bx_if:set_thumb' => array (
  56. 'condition' => CHECK_ACTION_RESULT_ALLOWED == $this->_oModule->isAllowedSetThumb(),
  57. 'content' => array (
  58. 'name_thumb' => BxNotesConfig::$FIELD_THUMB,
  59. ),
  60. ),
  61. );
  62. $this->aInputs[BxNotesConfig::$FIELD_PHOTO]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName('form_ghost_template.html', $aVars);
  63. }
  64. return parent::initChecker($aValues, $aSpecificValues);
  65. }
  66. public function insert ($aValsToAdd = array(), $isIgnore = false) {
  67. $aValsToAdd[BxNotesConfig::$FIELD_AUTHOR] = bx_get_logged_profile_id ();
  68. $aValsToAdd[BxNotesConfig::$FIELD_ADDED] = time();
  69. $aValsToAdd[BxNotesConfig::$FIELD_CHANGED] = time();
  70. if (CHECK_ACTION_RESULT_ALLOWED == $this->_oModule->isAllowedSetThumb()) {
  71. $aThumb = isset($_POST[BxNotesConfig::$FIELD_THUMB]) ? bx_process_input ($_POST[BxNotesConfig::$FIELD_THUMB], BX_DATA_INT) : false;
  72. $aValsToAdd[BxNotesConfig::$FIELD_THUMB] = 0;
  73. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  74. $aValsToAdd[BxNotesConfig::$FIELD_THUMB] = $iFileThumb;
  75. }
  76. if ($iContentId = parent::insert ($aValsToAdd, $isIgnore))
  77. $this->_processFiles ($this->getCleanValue(BxNotesConfig::$FIELD_PHOTO), $iContentId, true);
  78. return $iContentId;
  79. }
  80. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null) {
  81. $aValsToAdd[BxNotesConfig::$FIELD_CHANGED] = time();
  82. if (CHECK_ACTION_RESULT_ALLOWED == $this->_oModule->isAllowedSetThumb()) {
  83. $aThumb = bx_process_input ($_POST[BxNotesConfig::$FIELD_THUMB], BX_DATA_INT);
  84. $aValsToAdd[BxNotesConfig::$FIELD_THUMB] = 0;
  85. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  86. $aValsToAdd[BxNotesConfig::$FIELD_THUMB] = $iFileThumb;
  87. }
  88. if ($iRet = parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges))
  89. $this->_processFiles ($this->getCleanValue(BxNotesConfig::$FIELD_PHOTO), $iContentId, false);
  90. return $iRet;
  91. }
  92. function delete ($iContentId) {
  93. // delete associated files
  94. bx_import('BxDolStorage');
  95. $oStorage = BxDolStorage::getObjectInstance(BxNotesConfig::$OBJECT_STORAGE);
  96. if (!$oStorage)
  97. return false;
  98. $aGhostFiles = $oStorage->getGhosts (bx_get_logged_profile_id(), $iContentId);
  99. if ($aGhostFiles)
  100. foreach ($aGhostFiles as $aFile)
  101. $this->_deleteFile($aFile['id']);
  102. // delete db record
  103. return parent::delete($iContentId);
  104. }
  105. function _processFiles ($mixedFileIds, $iContentId = 0, $isAssociateWithContent = false) {
  106. if (!$mixedFileIds)
  107. return true;
  108. bx_import('BxDolStorage');
  109. $oStorage = BxDolStorage::getObjectInstance(BxNotesConfig::$OBJECT_STORAGE);
  110. if (!$oStorage)
  111. return false;
  112. $iProfileId = bx_get_logged_profile_id();
  113. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId);
  114. if (!$aGhostFiles)
  115. return true;
  116. foreach ($aGhostFiles as $aFile) {
  117. if ($aFile['private'])
  118. $oStorage->setFilePrivate ($aFile['id'], 0);
  119. if ($isAssociateWithContent && $iContentId)
  120. $oStorage->updateGhostsContentId ($aFile['id'], $iProfileId, $iContentId);
  121. }
  122. return true;
  123. }
  124. function _deleteFile ($iFileId) {
  125. if (!$iFileId)
  126. return true;
  127. bx_import('BxDolStorage');
  128. if (!($oStorage = BxDolStorage::getObjectInstance(BxNotesConfig::$OBJECT_STORAGE)))
  129. return false;
  130. if (!$oStorage->getFile($iFileId))
  131. return true;
  132. $iProfileId = bx_get_logged_profile_id();
  133. return $oStorage->deleteFile($iFileId, $iProfileId);
  134. }
  135. function addCssJs () {
  136. if (!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) {
  137. if (self::$_isCssJsAdded)
  138. return;
  139. $this->_oModule->_oTemplate->addJs('forms.js');
  140. $this->_oModule->_oTemplate->addCss('forms.css');
  141. }
  142. return parent::addCssJs ();
  143. }
  144. }
  145. /** @} */