BxBaseModProfileFormEntry.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 BaseProfile Base classes for profile modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Create/edit profile form.
  13. */
  14. class BxBaseModProfileFormEntry extends BxBaseModGeneralFormEntry
  15. {
  16. protected $_iAccountProfileId = 0;
  17. protected $_aImageFields = array ();
  18. public function __construct($aInfo, $oTemplate = false)
  19. {
  20. parent::__construct($aInfo, $oTemplate);
  21. $CNF = &$this->_oModule->_oConfig->CNF;
  22. if (isset($CNF['FIELD_ALLOW_POST_TO']) && isset($this->aInputs[$CNF['FIELD_ALLOW_POST_TO']]) && isset($CNF['OBJECT_PRIVACY_POST']) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) !== false) {
  23. $aSave = array('db' => array('pass' => 'Xss'));
  24. array_walk($this->aInputs[$CNF['FIELD_ALLOW_POST_TO']], function ($a, $k, $aSave) {
  25. if (in_array($k, array('info', 'caption', 'value')))
  26. $aSave[0][$k] = $a;
  27. }, array(&$aSave));
  28. $aGroupChooser = $oPrivacy->getGroupChooser($CNF['OBJECT_PRIVACY_POST']);
  29. $this->aInputs[$CNF['FIELD_ALLOW_POST_TO']] = array_merge($this->aInputs[$CNF['FIELD_ALLOW_POST_TO']], $aGroupChooser, $aSave);
  30. }
  31. if (!empty($CNF['FIELD_PICTURE']) && isset($this->aInputs[$CNF['FIELD_PICTURE']])) {
  32. $this->_aImageFields[$CNF['FIELD_PICTURE']] = array (
  33. 'storage_object' => $CNF['OBJECT_STORAGE'],
  34. 'images_transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_THUMB'],
  35. 'uploaders' => $CNF['OBJECT_UPLOADERS_PICTURE'],
  36. );
  37. }
  38. if (!empty($CNF['FIELD_COVER']) && isset($this->aInputs[$CNF['FIELD_COVER']])) {
  39. $this->_aImageFields[$CNF['FIELD_COVER']] = array (
  40. 'storage_object' => $CNF['OBJECT_STORAGE_COVER'],
  41. 'images_transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_COVER_THUMB'],
  42. 'uploaders' => $CNF['OBJECT_UPLOADERS_COVER'],
  43. );
  44. }
  45. foreach ($this->_aImageFields as $sField => $aParams) {
  46. $this->aInputs[$sField]['storage_object'] = $aParams['storage_object'];
  47. $this->aInputs[$sField]['uploaders'] = !empty($this->aInputs[$sField]['value']) ? unserialize($this->aInputs[$sField]['value']) : $aParams['uploaders'];
  48. $this->aInputs[$sField]['images_transcoder'] = $aParams['images_transcoder'];
  49. $this->aInputs[$sField]['storage_private'] = 0;
  50. $this->aInputs[$sField]['multiple'] = false;
  51. $this->aInputs[$sField]['content_id'] = 0;
  52. $this->aInputs[$sField]['ghost_template'] = '';
  53. }
  54. $oAccountProfile = BxDolProfile::getInstanceAccountProfile();
  55. if ($oAccountProfile)
  56. $this->_iAccountProfileId = $oAccountProfile->id();
  57. }
  58. function initChecker ($aValues = array (), $aSpecificValues = array())
  59. {
  60. $CNF = &$this->_oModule->_oConfig->CNF;
  61. $aContentInfo = isset($CNF['FIELD_ID']) && isset($aValues[$CNF['FIELD_ID']]) ? $this->_oModule->_oDb->getContentInfoById ($aValues[$CNF['FIELD_ID']]) : array();
  62. foreach ($this->_aImageFields as $sField => $aParams) {
  63. if ($aValues && !empty($aValues[$CNF['FIELD_ID']]))
  64. $this->aInputs[$sField]['content_id'] = $aValues[$CNF['FIELD_ID']];
  65. $this->aInputs[$sField]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName('form_ghost_template.html', $this->_getProfilePhotoGhostTmplVars($sField, $aContentInfo));
  66. }
  67. if (isset($CNF['FIELD_ALLOW_POST_TO']) && isset($this->aInputs[$CNF['FIELD_ALLOW_POST_TO']]) && $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_POST'])) {
  68. $iContentId = !empty($aValues[$CNF['FIELD_ID']]) ? (int)$aValues[$CNF['FIELD_ID']] : 0;
  69. $iProfileId = !empty($iContentId) ? (int)$this->getContentOwnerProfileId($iContentId) : bx_get_logged_profile_id();
  70. $iGroupId = !empty($aValues[$CNF['FIELD_ALLOW_POST_TO']]) ? $aValues[$CNF['FIELD_ALLOW_POST_TO']] : 0;
  71. $oPrivacy->loadGroupCustom($iProfileId, $iContentId, $iGroupId);
  72. }
  73. parent::initChecker($aValues, $aSpecificValues);
  74. }
  75. function delete ($iContentId, $aContentInfo = array())
  76. {
  77. $CNF = &$this->_oModule->_oConfig->CNF;
  78. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  79. foreach ($this->_aImageFields as $sField => $aParams) {
  80. $oStorage = BxDolStorage::getObjectInstance($aParams['storage_object']);
  81. $aFiles = $oStorage->getGhosts($iProfileId, $iContentId);
  82. foreach ($aFiles as $aFile) {
  83. if (!$oStorage->getFile($aFile['id']))
  84. continue;
  85. $bRet = $oStorage->deleteFile($aFile['id'], $this->_iAccountProfileId);
  86. }
  87. }
  88. return parent::delete($iContentId, $aContentInfo);
  89. }
  90. protected function genCustomViewRowValueProfileEmail($aInput)
  91. {
  92. return $this->genCustomViewRowValueProfileEmailOrIp($aInput);
  93. }
  94. protected function genCustomViewRowValueProfileIp($aInput)
  95. {
  96. return $this->genCustomViewRowValueProfileEmailOrIp($aInput);
  97. }
  98. protected function genCustomViewRowValueFriendsCount($aInput)
  99. {
  100. if (isset($this->_oModule->_oConfig->CNF['URI_VIEW_FRIENDS'])){
  101. $oProfile = $this->_oModule->getProfileByCurrentUrl();
  102. if ($oProfile){
  103. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  104. $iCount = $oConnection->getConnectedContentCount($oProfile->id(), true);
  105. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', array(
  106. 'href' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $this->_oModule->_oConfig->CNF['URI_VIEW_FRIENDS'] . '&profile_id=' . $oProfile->id()),
  107. 'title' => '',
  108. 'content' => $iCount
  109. ));
  110. }
  111. }
  112. return '';
  113. }
  114. protected function genCustomViewRowValueFollowersCount($aInput)
  115. {
  116. if (isset($this->_oModule->_oConfig->CNF['URI_VIEW_SUBSCRIPTIONS'])){
  117. $oProfile = $this->_oModule->getProfileByCurrentUrl();
  118. if ($oProfile){
  119. $oConnectionFollow = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  120. $iCount = $oConnectionFollow->getConnectedInitiatorsCount($oProfile->id());
  121. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', array(
  122. 'href' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $this->_oModule->_oConfig->CNF['URI_VIEW_SUBSCRIPTIONS'] . '&profile_id=' . $oProfile->id()),
  123. 'title' => '',
  124. 'content' => $iCount
  125. ));
  126. }
  127. }
  128. return '';
  129. }
  130. private function genCustomViewRowValueProfileEmailOrIp($aInput)
  131. {
  132. $CNF = &$this->_oModule->_oConfig->CNF;
  133. if(empty($aInput['value']))
  134. return '';
  135. $sValue = $aInput['value'];
  136. $sModuleAccounts = 'bx_accounts';
  137. if(!BxDolModuleQuery::getInstance()->isEnabledByName($sModuleAccounts))
  138. return $sValue;
  139. $oModuleAccounts = BxDolModule::getInstance($sModuleAccounts);
  140. if(!$oModuleAccounts || empty($oModuleAccounts->_oConfig->CNF['URL_MANAGE_ADMINISTRATION']))
  141. return $sValue;
  142. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', array(
  143. 'href' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($oModuleAccounts->_oConfig->CNF['URL_MANAGE_ADMINISTRATION'], array(
  144. 'filter' => urlencode($sValue)
  145. )),
  146. 'title' => '',
  147. 'content' => $sValue
  148. ));
  149. }
  150. protected function genCustomViewRowValueProfileStatus($aInput)
  151. {
  152. $CNF = &$this->_oModule->_oConfig->CNF;
  153. if(empty($aInput['value']))
  154. return '';
  155. $sStatus = _t('_sys_profile_status_' . $aInput['value']);
  156. if(empty($CNF['URL_MANAGE_ADMINISTRATION']) || empty($CNF['FIELD_TITLE']) && !empty($this->aInputs[$CNF['FIELD_TITLE']]['value']))
  157. return $sStatus;
  158. return $this->_oModule->_oTemplate->parseHtmlByName('name_link.html', array(
  159. 'href' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_MANAGE_ADMINISTRATION'], array(
  160. 'filter' => urlencode($this->aInputs[$CNF['FIELD_TITLE']]['value'])
  161. )),
  162. 'title' => '',
  163. 'content' => $sStatus
  164. ));
  165. }
  166. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  167. {
  168. $CNF = &$this->_oModule->_oConfig->CNF;
  169. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  170. $bResult = (int)$this->_oModule->_oDb->updateContentPictureById($iContentId, 0/*$iProfileId*/, $iFileId, $sPictureField) > 0;
  171. if($bResult) {
  172. $sModule = $this->_oModule->getName();
  173. switch($sPictureField) {
  174. case $CNF['FIELD_PICTURE']:
  175. bx_alert($sModule, 'profile_picture_changed', $iFileId, $iProfileId, array('content' => $iContentId, 'field' => $CNF['FIELD_PICTURE']));
  176. break;
  177. case $CNF['FIELD_COVER']:
  178. bx_alert($sModule, 'profile_cover_changed', $iFileId, $iProfileId, array('content' => $iContentId, 'field' => $CNF['FIELD_COVER']));
  179. break;
  180. }
  181. }
  182. }
  183. protected function _getProfilePhotoGhostTmplVars($sField, $aContentInfo = array())
  184. {
  185. $CNF = &$this->_oModule->_oConfig->CNF;
  186. return array (
  187. 'name' => $this->aInputs[$sField]['name'],
  188. 'content_id' => $this->aInputs[$sField]['content_id'],
  189. 'bx_if:set_thumb' => array (
  190. 'condition' => false,
  191. 'content' => array (),
  192. ),
  193. );
  194. }
  195. protected function _isAdmin ($iContentId = 0)
  196. {
  197. if (parent::_isAdmin ($iContentId))
  198. return true;
  199. if (!$iContentId || !($aDataEntry = $this->_oModule->_oDb->getContentInfoById((int)$iContentId)))
  200. return false;
  201. return CHECK_ACTION_RESULT_ALLOWED == $this->_oModule->checkAllowedEdit ($aDataEntry);
  202. }
  203. }
  204. /** @} */