Browse Source

Ticket #4558 - Build-in API support: Files and Photos.

AntonLV 2 years ago
parent
commit
9777d86068

+ 17 - 0
modules/base/files/classes/BxBaseModFilesModule.php

@@ -15,6 +15,23 @@ class BxBaseModFilesModule extends BxBaseModTextModule
     {
         parent::__construct($oConfig);
     }
+    
+    public function serviceGetFile ($iContentId, $aParams = []) 
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
+        if(empty($aContentInfo) || !is_array($aContentInfo))
+            return false;
+
+        $sStorage = !empty($aParams['storage']) ? $aParams['storage'] : $CNF['OBJECT_STORAGE'];
+        $oStorage = BxDolStorage::getObjectInstance($sStorage);
+        if(!$oStorage)
+            return false;
+
+        $sFieldFileId = !empty($aParams['field']) ? $aParams['field'] : $CNF['FIELD_FILE_ID'];
+        return $oStorage->getFile($aContentInfo[$sFieldFileId]);
+    }
 }
 
 /** @} */

+ 1 - 0
modules/boonex/files/classes/BxFilesConfig.php

@@ -78,6 +78,7 @@ class BxFilesConfig extends BxBaseModTextConfig
             'URL_UPDATED' => 'page.php?i=files-updated',
             'URL_MANAGE_COMMON' => 'page.php?i=files-manage',
             'URL_MANAGE_ADMINISTRATION' => 'page.php?i=files-administration',
+            'URL_DOWNLOAD' => 'modules/?r=files/download/{file_download_token}/{content_id}.{file_ext}',
 
             // some params
             'PARAM_AUTO_APPROVE' => 'bx_files_enable_auto_approve',

+ 38 - 0
modules/boonex/files/classes/BxFilesModule.php

@@ -175,6 +175,34 @@ class BxFilesModule extends BxBaseModFilesModule
 
         return $this->_addFileFields(array_pop($aGhostFiles));
     }
+    
+    public function getDownloadLink($mixedData) 
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        if(!is_array($mixedData))
+            $mixedData = $this->_oDb->getContentInfoById((int)$mixedData);
+        if(empty($mixedData) || !is_array($mixedData))
+            return false;
+        
+        $aFile = $this->getContentFile($mixedData);
+        if($aFile === false)
+            return false;
+
+        $sFileExt = !empty($aFile['ext']) ? $aFile['ext'] : '';
+
+        $aMarkers = array_merge([
+            'content_id' => $mixedData[$CNF['FIELD_ID']]
+        ], ($aFile['private'] && ($oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE'])) ? [
+            'file_download_token' => $oStorage->genToken($aFile['id']),
+            'file_ext' => $sFileExt
+        ] : [
+            'file_download_token' => '0',
+            'file_ext' => $sFileExt,
+        ]));
+
+        return bx_replace_markers(BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_DOWNLOAD']), $aMarkers);
+    }
 
     protected function _addFileFields($aFile) 
     {
@@ -604,6 +632,16 @@ class BxFilesModule extends BxBaseModFilesModule
 
         return parent::serviceBrowseContext($iProfileId, $aParams);
     }
+
+    public function serviceGetFile ($iContentId, $aParams = []) 
+    {
+        $aResult = parent::serviceGetFile ($iContentId, $aParams);
+        if(!$aResult)
+            return $aResult;
+
+        $aResult['url_download'] = $this->getDownloadLink($iContentId);
+        return $aResult;
+    }
 }
 
 /** @} */

+ 10 - 0
modules/boonex/photos/classes/BxPhotosModule.php

@@ -24,6 +24,16 @@ class BxPhotosModule extends BxBaseModFilesModule
         echo BxDolPage::getObjectInstance($this->_oConfig->CNF['OBJECT_PAGE_VIEW_ENTRY_BRIEF'], $this->_oTemplate)->getCodeDynamic();
     }
 
+    public function serviceGetFile ($iContentId, $aParams = []) 
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        if(!isset($aParams['field']))
+            $aParams['field'] = $CNF['FIELD_THUMB'];
+
+        return parent::serviceGetFile($iContentId, $aParams);
+    }
+
     /**
      * Display entries of the author
      * @return HTML string

+ 10 - 4
template/scripts/BxBasePage.php

@@ -668,17 +668,22 @@ class BxBasePage extends BxDolPage
                     if(isset($aContent['module'], $aContent['method']))
                         $sSource = $aContent['module'] . ':' . $aContent['method'];
                 }
-                else{
+                else
                     $sSource = 'system:block_' . $aBlock['id'];
-                }
 
                 if($bBlocks && !in_array($sSource, $aBlocks)) {
                     unset($aCells[$sKey][$i]);
                     continue;
                 }
 
-                $sFunc = '_getBlock' . bx_gen_method_name($aBlock['type']);
-                $mBlock = method_exists($this, $sFunc) ? $this->$sFunc($aBlock) : $aBlock['content'];
+                $mBlock = $aBlock['content'];
+                if(($sFunc = '_getBlock' . bx_gen_method_name($aBlock['type'])) && method_exists($this, $sFunc))
+                    $mBlock = $this->$sFunc($aBlock); 
+
+                if(empty($mBlock)) {
+                    unset($aCells[$sKey][$i]);
+                    continue;
+                }
 
                 $aCells[$sKey][$i] = array_merge($aCells[$sKey][$i], [
                     'title' => isset($mBlock['title']) ? $mBlock['title'] : $this->getBlockTitle($aBlock),
@@ -686,6 +691,7 @@ class BxBasePage extends BxDolPage
                     'menu' => isset($mBlock['menu']) ? $mBlock['menu'] : '',
                     'source' => $sSource
                 ]);
+
                 $aCells[$sKey][$i] = array_diff_key($aCells[$sKey][$i], array_flip($aFieldsUnset));
             }
         }