Explorar el Código

Ticket #5273 - Get rid of duplicates in code.

AntonLV hace 11 meses
padre
commit
99bbb007c8

+ 5 - 66
modules/base/profile/classes/BxBaseModProfileUploaderBadgeCrop.php

@@ -9,79 +9,18 @@
  * @{
  */
 
-class BxBaseModProfileUploaderBadgeCrop extends BxTemplUploaderCrop
-{
-    protected $_sModule;
-    protected $_oModule;
+require_once('BxBaseModProfileUploaderCrop.php');
 
+class BxBaseModProfileUploaderBadgeCrop extends BxBaseModProfileUploaderCrop
+{
     public function __construct ($aObject, $sStorageObject, $sUniqId, $oTemplate)
     {
-        if(!empty($this->_sModule) && empty($this->_oModule))
-            $this->_oModule = BxDolModule::getInstance($this->_sModule);
-
         parent::__construct($aObject, $sStorageObject, $sUniqId, $oTemplate ? $oTemplate : $this->_oModule->_oTemplate);
-    }
-
-    public function deleteGhostsForProfile($iProfileId, $iContentId = false)
-    {
-        if (!$iContentId)
-            return parent::deleteGhostsForProfile($iProfileId, $iContentId);
-
-        $CNF = $this->_oModule->_oConfig->CNF;
-        $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
-        
-        $iCount = 0;
-
-        $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
 
-        $aGhosts = $oStorage->getGhosts($iProfileId, $iContentId, $iContentId ? true : false);
-        foreach ($aGhosts as $aFile) {
-            // for cover image delete only unassigned ghosts and currently set covers
-            if ($aFile['id'] == $aContentInfo[$CNF['FIELD_BADGE']] || !in_array($aFile['id'], [$aContentInfo[$CNF['FIELD_PICTURE']], $aContentInfo[$CNF['FIELD_COVER']]]))
-                $iCount += $oStorage->deleteFile($aFile['id']);
-        }
-
-        return $iCount;
-    }
-    
-    public function getGhosts($iProfileId, $sFormat, $sImagesTranscoder = false, $iContentId = false)
-    {
-        $s = parent::getGhosts($iProfileId, $sFormat, $sImagesTranscoder, $iContentId);
-        if (!$s || !$iContentId) // if we're creating new profile return all ghosts
-            return $s;
-
-        $a = array();
-        if ($sFormat == 'array')
-            $a = $s;
-        else if ($sFormat == 'json')
-            $a = json_decode($s, true);
-
-        if (!$a)
-            return $s;
-
-        // filter out thumbnails
         $CNF = $this->_oModule->_oConfig->CNF;
-        $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
-        $aResult = array();
-        foreach ($a as $aFile) {
-            // for cover image show only unassigned ghosts and currently set covers
-            if ($aFile['file_id'] == $aContentInfo[$CNF['FIELD_BADGE']] || !in_array($aFile['file_id'], [$aContentInfo[$CNF['FIELD_PICTURE']], $aContentInfo[$CNF['FIELD_COVER']]]))
-                $aResult[$aFile['file_id']] = $aFile;
-        }
-
-        if ('array' == $sFormat) {
-            return $aResult;
-        }
-        else if ('json' == $sFormat) {
-            return json_encode($aResult);
-        } else { // html format is not suported for this data type
-            return false;
-        }
-    }
 
-    protected function isAdmin ($iContentId = 0)
-    {
-        return $this->_oModule->_isModerator (false);
+        $this->_sImage = $CNF['FIELD_BADGE'];
+        $this->_aOtherImages = [$CNF['FIELD_PICTURE'], $CNF['FIELD_COVER']];
     }
 }
 

+ 4 - 74
modules/base/profile/classes/BxBaseModProfileUploaderCoverCrop.php

@@ -9,93 +9,23 @@
  * @{
  */
 
-class BxBaseModProfileUploaderCoverCrop extends BxTemplUploaderHTML5
-{
-    protected $_sModule;
-    protected $_oModule;
-
-    protected $_aOtherImages;
+require_once('BxBaseModProfileUploaderCrop.php');
 
+class BxBaseModProfileUploaderCoverCrop extends BxBaseModProfileUploaderCrop
+{
     public function __construct ($aObject, $sStorageObject, $sUniqId, $oTemplate)
     {
-        if(!empty($this->_sModule) && empty($this->_oModule))
-            $this->_oModule = BxDolModule::getInstance($this->_sModule);
-
         parent::__construct($aObject, $sStorageObject, $sUniqId, $oTemplate ? $oTemplate : $this->_oModule->_oTemplate);
 
         $this->_sUploaderFormTemplate = 'uploader_form_crop_cover.html';
         
         $CNF = $this->_oModule->_oConfig->CNF;
 
+        $this->_sImage = $CNF['FIELD_COVER'];
         $this->_aOtherImages = [$CNF['FIELD_PICTURE']];
         if(!empty($CNF['FIELD_BADGE']))
             $this->_aOtherImages[] = $CNF['FIELD_BADGE'];
     }
-
-    public function deleteGhostsForProfile($iProfileId, $iContentId = false)
-    {
-        if (!$iContentId)
-            return parent::deleteGhostsForProfile($iProfileId, $iContentId);
-
-        $CNF = $this->_oModule->_oConfig->CNF;
-        $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
-        
-        $iCount = 0;
-
-        $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
-        $aGhosts = $oStorage->getGhosts($iProfileId, $iContentId, $iContentId ? true : false);
-        foreach ($aGhosts as $aFile) {
-            // for cover image delete only unassigned ghosts and currently set covers
-            if ($aFile['id'] == $aContentInfo[$CNF['FIELD_COVER']] || !in_array($aFile['id'], array_map(function($sField) use ($aContentInfo) {
-                return $aContentInfo[$sField];
-            }, $this->_aOtherImages)))
-                $iCount += $oStorage->deleteFile($aFile['id']);
-        }
-
-        return $iCount;
-    }
-    
-    public function getGhosts($iProfileId, $sFormat, $sImagesTranscoder = false, $iContentId = false)
-    {
-        $s = parent::getGhosts($iProfileId, $sFormat, $sImagesTranscoder, $iContentId);
-        if (!$s || !$iContentId) // if we're creating new profile return all ghosts
-            return $s;
-
-        $a = array();
-        if ($sFormat == 'array')
-            $a = $s;
-        else if ($sFormat == 'json')
-            $a = json_decode($s, true);
-
-        if (!$a)
-            return $s;
-
-        // filter out thumbnails
-        $CNF = $this->_oModule->_oConfig->CNF;
-        $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
-        $aResult = array();
-        foreach ($a as $aFile) {
-            // for cover image show only unassigned ghosts and currently set covers
-            if ($aFile['file_id'] == $aContentInfo[$CNF['FIELD_COVER']] || !in_array($aFile['file_id'], array_map(function($sField) use ($aContentInfo) {
-                return $aContentInfo[$sField];
-            }, $this->_aOtherImages)))
-                $aResult[$aFile['file_id']] = $aFile;
-        }
-
-        if ('array' == $sFormat) {
-            return $aResult;
-        }
-        else if ('json' == $sFormat) {
-            return json_encode($aResult);
-        } else { // html format is not suported for this data type
-            return false;
-        }
-    }
-
-    protected function isAdmin ($iContentId = 0)
-    {
-        return $this->_oModule->_isModerator (false);
-    }
 }
 
 /** @} */

+ 94 - 0
modules/base/profile/classes/BxBaseModProfileUploaderCrop.php

@@ -0,0 +1,94 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    BaseProfile Base classes for profile modules
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+class BxBaseModProfileUploaderCrop extends BxTemplUploaderCrop
+{
+    protected $_sModule;
+    protected $_oModule;
+
+    protected $_sImage;
+    protected $_aOtherImages;
+
+    public function __construct ($aObject, $sStorageObject, $sUniqId, $oTemplate)
+    {
+        if(!empty($this->_sModule) && empty($this->_oModule))
+            $this->_oModule = BxDolModule::getInstance($this->_sModule);
+
+        parent::__construct($aObject, $sStorageObject, $sUniqId, $oTemplate ? $oTemplate : $this->_oModule->_oTemplate);
+    }
+
+    public function deleteGhostsForProfile($iProfileId, $iContentId = false)
+    {
+        if (!$iContentId)
+            return parent::deleteGhostsForProfile($iProfileId, $iContentId);
+
+        $CNF = $this->_oModule->_oConfig->CNF;
+        $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
+        
+        $iCount = 0;
+
+        $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
+        $aGhosts = $oStorage->getGhosts($iProfileId, $iContentId, $iContentId ? true : false);
+        foreach ($aGhosts as $aFile) {
+            // for requested image type delete only unassigned ghosts and currently set images
+            if ($aFile['id'] == $aContentInfo[$this->_sImage] || !in_array($aFile['id'], array_map(function($sField) use ($aContentInfo) {
+                return $aContentInfo[$sField];
+            }, $this->_aOtherImages)))
+                $iCount += $oStorage->deleteFile($aFile['id']);
+        }
+
+        return $iCount;
+    }
+    
+    public function getGhosts($iProfileId, $sFormat, $sImagesTranscoder = false, $iContentId = false)
+    {
+        $s = parent::getGhosts($iProfileId, $sFormat, $sImagesTranscoder, $iContentId);
+        if (!$s || !$iContentId) // if we're creating new profile return all ghosts
+            return $s;
+
+        $a = array();
+        if ($sFormat == 'array')
+            $a = $s;
+        else if ($sFormat == 'json')
+            $a = json_decode($s, true);
+
+        if (!$a)
+            return $s;
+
+        // filter out thumbnails
+        $CNF = $this->_oModule->_oConfig->CNF;
+        $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
+        $aResult = array();
+        foreach ($a as $aFile) {
+            // for requested image type show only unassigned ghosts and currently set images
+            if ($aFile['file_id'] == $aContentInfo[$this->_sImage] || !in_array($aFile['file_id'], array_map(function($sField) use ($aContentInfo) {
+                return $aContentInfo[$sField];
+            }, $this->_aOtherImages)))
+                $aResult[$aFile['file_id']] = $aFile;
+        }
+
+        if ('array' == $sFormat) {
+            return $aResult;
+        }
+        else if ('json' == $sFormat) {
+            return json_encode($aResult);
+        } else { // html format is not suported for this data type
+            return false;
+        }
+    }
+
+    protected function isAdmin ($iContentId = 0)
+    {
+        return $this->_oModule->_isModerator (false);
+    }
+}
+
+/** @} */

+ 5 - 76
modules/base/profile/classes/BxBaseModProfileUploaderPictureCrop.php

@@ -9,92 +9,21 @@
  * @{
  */
 
-class BxBaseModProfileUploaderPictureCrop extends BxTemplUploaderCrop
-{
-    protected $_sModule;
-    protected $_oModule;
-    
-    protected $_aOtherImages;
+require_once('BxBaseModProfileUploaderCrop.php');
 
+class BxBaseModProfileUploaderPictureCrop extends BxBaseModProfileUploaderCrop
+{
     public function __construct ($aObject, $sStorageObject, $sUniqId, $oTemplate)
     {
-        if(!empty($this->_sModule))
-            $this->_oModule = BxDolModule::getInstance($this->_sModule);
-
         parent::__construct($aObject, $sStorageObject, $sUniqId, $oTemplate);
-        
+
         $CNF = $this->_oModule->_oConfig->CNF;
 
+        $this->_sImage = $CNF['FIELD_PICTURE'];
         $this->_aOtherImages = [$CNF['FIELD_COVER']];
         if(!empty($CNF['FIELD_BADGE']))
             $this->_aOtherImages[] = $CNF['FIELD_BADGE'];
     }
-
-    public function deleteGhostsForProfile($iProfileId, $iContentId = false)
-    {
-        if (!$iContentId)
-            return parent::deleteGhostsForProfile($iProfileId, $iContentId);
-
-        $CNF = $this->_oModule->_oConfig->CNF;
-        $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
-        
-        $iCount = 0;
-
-        $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
-        $aGhosts = $oStorage->getGhosts($iProfileId, $iContentId, $iContentId ? true : false);
-        foreach ($aGhosts as $aFile) {
-            // for profile image delete only unassigned ghosts and currently set profile pictures
-            if ($aFile['id'] == $aContentInfo[$CNF['FIELD_PICTURE']] || !in_array($aFile['id'], array_map(function($sField) use ($aContentInfo) {
-                return $aContentInfo[$sField];
-            }, $this->_aOtherImages)))
-                $iCount += $oStorage->deleteFile($aFile['id']);
-        }
-
-        return $iCount;
-    }
-    
-    public function getGhosts($iProfileId, $sFormat, $sImagesTranscoder = false, $iContentId = false)
-    {
-        $s = parent::getGhosts($iProfileId, $sFormat, $sImagesTranscoder, $iContentId);
-        if (!$s || !$iContentId) // if we're creating new profile return all ghosts
-            return $s;
-
-        $a = array();
-        if ($sFormat == 'array')
-            $a = $s;
-        else if ($sFormat == 'json')
-            $a = json_decode($s, true);
-
-        if (!$a)
-            return $s;
-
-        // filter out thumbnails
-        $CNF = $this->_oModule->_oConfig->CNF;
-        $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
-        $aResult = array();
-        foreach ($a as $aFile) {
-
-            // for profile pictures show only unassigned ghosts and currently set profile pictures
-            if (($aFile['file_id'] == $aContentInfo[$CNF['FIELD_PICTURE']] || !in_array($aFile['file_id'], array_map(function($sField) use ($aContentInfo) {
-                return $aContentInfo[$sField];
-            }, $this->_aOtherImages))))
-                $aResult[$aFile['file_id']] = $aFile;            
-        }
-
-        if ('array' == $sFormat) {
-            return $aResult;
-        }
-        else if ('json' == $sFormat) {
-            return json_encode($aResult);
-        } else { // html format is not suported for this data type
-            return false;
-        }
-    }
-
-    protected function isAdmin ($iContentId = 0)
-    {
-        return $this->_oModule->_isModerator (false);
-    }
 }
 
 /** @} */