Просмотр исходного кода

#138 Orgs & Persons: allow to delete cover image

Alex Trofimov 11 лет назад
Родитель
Сommit
82f605e258

+ 1 - 1
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -278,7 +278,7 @@ class BxBaseModGeneralModule extends BxDolModule
 
     protected function _isModerator ($isPerformAction = false)
     {
-        return CHECK_ACTION_RESULT_ALLOWED == $this->checkAllowedEditAnyEntry ($isPerformAction);
+        return CHECK_ACTION_RESULT_ALLOWED === $this->checkAllowedEditAnyEntry ($isPerformAction);
     }
 
     protected function _serviceEntityForm ($sFormMethod, $iContentId = 0, $sDisplay = false)

+ 7 - 2
modules/base/profile/classes/BxBaseModProfileDb.php

@@ -29,8 +29,13 @@ class BxBaseModProfileDb extends BxBaseModGeneralDb
 
     public function updateContentPictureById($iContentId, $iProfileId, $iPictureId, $sFieldPicture)
     {
-        $sQuery = $this->prepare ("UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $sFieldPicture . "` = ? WHERE `id` = ? AND `author` = ?", $iPictureId, $iContentId, $iProfileId);
-        return $this->res($sQuery);
+        $sWhere = '';
+        if ($iProfileId)
+            $sWhere = $this->prepare (" AND `author` = ? ", $iProfileId);
+
+        $sQuery = $this->prepare ("UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $sFieldPicture . "` = ? WHERE `id` = ?", $iPictureId, $iContentId);
+
+        return $this->res($sQuery . $sWhere);
     }
 
     public function searchByTerm($sTerm, $iLimit)

+ 18 - 7
modules/base/profile/classes/BxBaseModProfileFormEntry.php

@@ -67,7 +67,7 @@ class BxBaseModProfileFormEntry extends BxBaseModGeneralFormEntry
                 $this->aInputs[$sField]['content_id'] = $aValues[$CNF['FIELD_ID']];
 
             $sErrorString = '';
-            $this->aInputs[$sField]['file_id'] = $this->_processFile ($sField, isset($aValues[$sField]) ? $aValues[$sField] : 0, $sErrorString);
+            $this->aInputs[$sField]['file_id'] = $this->_processFile (!empty($aValues[$CNF['FIELD_ID']]) ? $aValues[$CNF['FIELD_ID']] : 0, $sField, isset($aValues[$sField]) ? $aValues[$sField] : 0, $sErrorString);
             if ($sErrorString) {
                 $this->aInputs[$sField]['error'] = $sErrorString;
                 $this->setValid(false);
@@ -89,6 +89,10 @@ class BxBaseModProfileFormEntry extends BxBaseModGeneralFormEntry
                     'condition' => !$oTranscoder || !isset($aValues[$sField]) || !$aValues[$sField] ? true : false,
                     'content' => array (),
                 ),
+                'bx_if:delete' => array (
+                    'condition' => $oTranscoder && isset($aValues[$sField]) && $aValues[$sField] && $sField == $CNF['FIELD_COVER'] ? true : false,
+                    'content' => array ('action_ajax' => isset($aValues[$sField]) ? BX_DOL_URL_ROOT . $this->_oModule->_oConfig->getBaseUri() . 'delete_profile_img/' . $aValues[$sField] . '/' . (!empty($aValues[$CNF['FIELD_ID']]) ? $aValues[$CNF['FIELD_ID']] : 0) . '/' . $sField : ''),
+                ),
             );
             $this->aInputs[$aVals['field_preview']]['content'] = $this->_oModule->_oTemplate->parseHtmlByName('picture_preview.html', $aVars);
         }
@@ -132,13 +136,13 @@ class BxBaseModProfileFormEntry extends BxBaseModGeneralFormEntry
 
         foreach ($this->_aImageFields as $sField => $aVals) {
             if (isset($aContentInfo[$sField]) && $aContentInfo[$sField])
-                $this->_deleteFile ($aContentInfo[$sField], $aVals['storage_object']);
+                $this->_deleteFile ($iContentId, $sField, $aContentInfo[$sField]);
         }
 
         return parent::delete($iContentId, $aContentInfo);
     }
 
-    function _processFile ($sField, $iFileIdOld, &$sErrorString)
+    function _processFile ($iContentId, $sField, $iFileIdOld, &$sErrorString)
     {
         if (empty($_FILES[$sField]['tmp_name']))
             return $iFileIdOld;
@@ -148,7 +152,7 @@ class BxBaseModProfileFormEntry extends BxBaseModGeneralFormEntry
             return $iFileIdOld;
 
         // delete previous file
-        $this->_deleteFile($iFileIdOld, $this->_aImageFields[$sField]['storage_object']);
+        $this->_deleteFile($iContentId, $sField, $iFileIdOld);
 
         // process new file and return new file id
         if (!($iFileId = $oStorage->storeFileFromForm($_FILES[$sField], false, $this->_iAccountProfileId))) {
@@ -159,18 +163,25 @@ class BxBaseModProfileFormEntry extends BxBaseModGeneralFormEntry
         return $iFileId;
     }
 
-    function _deleteFile ($iFileId, $sStorageObject)
+    function _deleteFile ($iContentId, $sFieldPicture, $iFileId, $bForceFieldUpdate = false)
     {
         if (!$iFileId)
             return true;
 
-        if (!($oStorage = BxDolStorage::getObjectInstance($sStorageObject)))
+        if (!$this->_aImageFields[$sFieldPicture]['storage_object'])
+            return false;
+
+        if (!($oStorage = BxDolStorage::getObjectInstance($this->_aImageFields[$sFieldPicture]['storage_object'])))
             return false;
 
         if (!$oStorage->getFile($iFileId))
             return true;
 
-        return $oStorage->deleteFile($iFileId, $this->_iAccountProfileId);
+        if (($bRet = $oStorage->deleteFile($iFileId, $this->_iAccountProfileId)) && $bForceFieldUpdate) {
+            $this->_oModule->_oDb->updateContentPictureById($iContentId, 0, 0, $sFieldPicture);
+        }
+
+        return $bRet;
     }
 
 }

+ 25 - 0
modules/base/profile/classes/BxBaseModProfileModule.php

@@ -25,6 +25,31 @@ class BxBaseModProfileModule extends BxBaseModGeneralModule implements iBxDolPro
         $this->_iAccountId = getLoggedId();
     }
 
+    public function actionDeleteProfileImg($iFileId, $iContentId, $sFieldPicture) 
+    {
+        $aResult = array();
+        $CNF = &$this->_oConfig->CNF;
+
+        bx_import('BxDolStorage');
+        $oSrorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
+        if (!($aFile = $oSrorage->getFile((int)$iFileId)) || !($aContentInfo = $this->_oDb->getContentInfoById($iContentId)) || $aContentInfo[$sFieldPicture] != (int)$iFileId)
+            $aResult = array('error' => 1, 'msg' => _t('_sys_storage_err_file_not_found'));
+
+        if ((!$aResult && !isLogged()) || (!$aResult && $aFile['profile_id'] != bx_get_logged_profile_id() && !$this->_isModerator()))           
+            $aResult = array('error' => 2, 'msg' => _t('_Access denied'));
+
+        bx_import('BxDolForm');
+        $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'], $this->_oTemplate);
+
+        if (!$aResult && !$oForm->_deleteFile($iContentId, $sFieldPicture, (int)$iFileId, true))
+            $aResult = array('error' => 3, 'msg' => _t('_Failed'));
+        elseif (!$aResult)            
+            $aResult = array('error' => 0, 'msg' => '');
+
+        header('Content-Type: application/json; charset=utf-8');
+        echo json_encode($aResult);
+    }
+
     // ====== SERVICE METHODS
 	public function serviceGetMenuAddonManageTools()
 	{

+ 20 - 0
modules/base/profile/template/picture_preview.html

@@ -1,5 +1,25 @@
 <bx_if:picture>
+<div id="bx-base-profile-img">
     <img class="bx-def-round-corners" src="__picture_url__" />
+    <bx_if:delete>
+        <br />
+        <button id="bx-base-profile-delete"><bx_text:_Delete /></button>
+        <script>
+$(document).ready (function () {
+    $('#bx-base-profile-delete').click(function (event) {
+        event.preventDefault();
+        $.post('__action_ajax__', function (oData) {
+            if (parseInt(oData.error) > 0)
+                alert(oData.msg);
+            else
+                $('#bx-base-profile-img').replaceWith('<i class="bx-def-font-small"><bx_text:_sys_txt_no_picture_uploaded /></i>');
+        }, 'json');
+        return false;
+    });
+});
+        </script>
+</div>
+    </bx_if:delete>
 </bx_if:picture>
 <bx_if:no_picture>
     <i class="bx-def-font-small"><bx_text:_sys_txt_no_picture_uploaded /></i>