AntonLV 10 лет назад
Родитель
Сommit
ed8bd543cd

+ 16 - 0
inc/classes/BxDolStorage.php

@@ -604,6 +604,22 @@ abstract class BxDolStorage extends BxDol implements iBxDolFactoryObject
         return $this->queueFiles($aFiles);
     }
 
+    /**
+     * Download file.
+     * @param array $aFile downloading file info.
+     */
+    public function download ($aFile)
+    {
+    	$bRet = true;
+        bx_alert($this->_aObject['object'], 'file_downloaded', $aFile['id'], bx_get_logged_profile_id(), array(
+        	'profile_ip' => getVisitorIP(),
+        	'file_info' => $aFile, 
+        	'return_value' => &$bRet
+        ));
+
+        return $bRet;
+    }
+
     /**
      * Set file private or public.
      */

+ 1 - 1
inc/classes/BxDolStorageLocal.php

@@ -71,7 +71,7 @@ class BxDolStorageLocal extends BxDolStorage
             return false;
         }
 
-        return true;
+        return parent::download($aFile);
     }
 
     // ----------------

+ 24 - 10
modules/boonex/market/classes/BxMarketAlertsResponse.php

@@ -18,21 +18,35 @@ class BxMarketAlertsResponse extends BxBaseModTextAlertsResponse
     }
 
 	public function response($oAlert)
+    {
+    	$sMethod = 'process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
+    	if(method_exists($this, $sMethod))
+    		$this->$sMethod($oAlert);
+
+        parent::response($oAlert);
+    }
+
+    protected function processBxMarketFilesFileDeleted(&$oAlert)
+    {
+		BxDolModule::getInstance($this->MODULE)->_oDb->deassociateFileWithContent(0, $oAlert->iObject);
+    }
+
+    protected function processBxMarketFilesFileDownloaded(&$oAlert)
     {
     	$oModule = BxDolModule::getInstance($this->MODULE);
 
-    	if($oAlert->sAction == 'file_deleted')
-    		switch ($oAlert->sUnit) {
-    			case 'bx_market_files':
-    				$oModule->_oDb->deassociateFileWithContent(0, $oAlert->iObject);
-    				break;
+    	$iFile = $oAlert->iObject;
+    	$aFile = $oModule->_oDb->getFile(array('type' => 'file_id', 'file_id' => $iFile));
+    	if(empty($aFile) || !is_array($aFile))
+    		return;
 
-    			case 'bx_market_photos':
-    				$oModule->_oDb->deassociatePhotoWithContent(0, $oAlert->iObject);
-    				break;
-    		}
+    	$oModule->_oDb->updateFile(array('downloads' => $aFile['downloads'] + 1), array('file_id' => $iFile));
+    	$oModule->_oDb->insertDownload($iFile, $oAlert->iSender, ip2long($oAlert->aExtras['profile_ip']));
+    }
 
-        parent::response($oAlert);
+    protected function processBxMarketPhotosFileDeleted(&$oAlert)
+    {
+    	BxDolModule::getInstance($this->MODULE)->_oDb->deassociatePhotoWithContent(0, $oAlert->iObject);
     }
 }
 

+ 1 - 0
modules/boonex/market/classes/BxMarketConfig.php

@@ -31,6 +31,7 @@ class BxMarketConfig extends BxBaseModTextConfig
             'TABLE_ENTRIES' => $aModule['db_prefix'] . 'products',
         	'TABLE_PHOTOS2ENTRIES' => $aModule['db_prefix'] . 'photos2products',
         	'TABLE_FILES2ENTRIES' => $aModule['db_prefix'] . 'files2products',
+        	'TABLE_DOWNLOADS' => $aModule['db_prefix'] . 'downloads_track',
         	'TABLE_LICENSES' => $aModule['db_prefix'] . 'licenses',
 
             // database fields

+ 69 - 47
modules/boonex/market/classes/BxMarketDb.php

@@ -32,48 +32,19 @@ class BxMarketDb extends BxBaseModTextDb
     	return $this->_getAttachment($this->_oConfig->CNF['TABLE_PHOTOS2ENTRIES'], $aParams);
     }
 
+	public function updatePhoto($aSet, $aWhere)
+    {
+    	return $this->_updateAttachment($this->_oConfig->CNF['TABLE_PHOTOS2ENTRIES'], $aSet, $aWhere);
+    }
+
     public function getFile($aParams = array())
     {
     	return $this->_getAttachment($this->_oConfig->CNF['TABLE_FILES2ENTRIES'], $aParams);
     }
 
-    protected function _getAttachment($sTable, $aParams = array())
+	public function updateFile($aSet, $aWhere)
     {
-    	$aMethod = array('name' => 'getRow', 'params' => array(0 => 'query'));
-    	
-    	$sWhereClause = "";
-        switch($aParams['type']) {
-            case 'id':
-                $sWhereClause = $this->prepare(" AND `tfe`.`id`=?", $aParams['id']);
-                break;
-
-			case 'file_id':
-                $sWhereClause = $this->prepare(" AND `tfe`.`file_id`=?", $aParams['file_id']);
-                break;
-
-            case 'content_id':
-            	$aMethod['name'] = 'getAll';
-
-            	$sWhereClause = $this->prepare(" AND `tfe`.`content_id`=?", $aParams['content_id']);
-            	if(!empty($aParams['except']))
-            		$sWhereClause .= " AND `tfe`.`file_id` NOT IN (" . $this->implode_escape($aParams['except']) . ")";
-
-            	break;
-
-			case 'content_id_key_file_id':
-				$aMethod['name'] = 'getAllWithKey';
-				$aMethod['params'][1] = 'file_id';
-
-                $sWhereClause = $this->prepare(" AND `tfe`.`content_id`=?", $aParams['content_id']);
-                break;
-        }
-
-        $aMethod['params'][0] = "SELECT
-        		`tfe`.*
-            FROM `" . $sTable . "` AS `tfe`
-            WHERE 1" . $sWhereClause;
-
-        return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
+    	return $this->_updateAttachment($this->_oConfig->CNF['TABLE_FILES2ENTRIES'], $aSet, $aWhere);
     }
 
 	public function associatePhotoWithContent($iContentId, $iFileId, $sTitle)
@@ -103,18 +74,11 @@ class BxMarketDb extends BxBaseModTextDb
     {
     	return $this->_deassociateAttachmentWithContent($this->_oConfig->CNF['TABLE_FILES2ENTRIES'], $iContentId, $iFileId);
     }
-    
-	protected function _deassociateAttachmentWithContent($sTable, $iContentId, $iFileId)
-    {
-        $sWhere = '';
-        if ($iContentId)
-            $sWhere .= $this->prepare (" AND `content_id` = ? ", $iContentId);
-
-        if ($iFileId)
-            $sWhere .= $this->prepare (" AND `file_id` = ? ", $iFileId);
 
-        $sQuery = "DELETE FROM `" . $sTable . "` WHERE 1 ";
-        return $this->query($sQuery . $sWhere);
+    public function insertDownload($iFileId, $iProfileId, $iProfileNip)
+    {
+    	$sQuery = $this->prepare("INSERT IGNORE INTO `" . $this->_oConfig->CNF['TABLE_DOWNLOADS'] . "` SET `file_id` = ?, `profile_id` = ?, `profile_nip` = ?, `date` = UNIX_TIMESTAMP()", $iFileId, $iProfileId, $iProfileNip);
+		return (int)$this->query($sQuery) > 0;
     }
 
     /**
@@ -191,6 +155,64 @@ class BxMarketDb extends BxBaseModTextDb
     	$sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_LICENSES'] . "` WHERE `profile_id` = ? AND `product_id` = ? AND `order` = ? AND `license` = ?", $iProfileId, $iProductId, $sOrder, $sLicense);
         return (int)$this->query($sQuery) > 0;
     }
+
+	protected function _deassociateAttachmentWithContent($sTable, $iContentId, $iFileId)
+    {
+        $sWhere = '';
+        if ($iContentId)
+            $sWhere .= $this->prepare (" AND `content_id` = ? ", $iContentId);
+
+        if ($iFileId)
+            $sWhere .= $this->prepare (" AND `file_id` = ? ", $iFileId);
+
+        $sQuery = "DELETE FROM `" . $sTable . "` WHERE 1 ";
+        return $this->query($sQuery . $sWhere);
+    }
+
+	protected function _getAttachment($sTable, $aParams = array())
+    {
+    	$aMethod = array('name' => 'getRow', 'params' => array(0 => 'query'));
+    	
+    	$sWhereClause = "";
+        switch($aParams['type']) {
+            case 'id':
+                $sWhereClause = $this->prepare(" AND `tfe`.`id`=?", $aParams['id']);
+                break;
+
+			case 'file_id':
+                $sWhereClause = $this->prepare(" AND `tfe`.`file_id`=?", $aParams['file_id']);
+                break;
+
+            case 'content_id':
+            	$aMethod['name'] = 'getAll';
+
+            	$sWhereClause = $this->prepare(" AND `tfe`.`content_id`=?", $aParams['content_id']);
+            	if(!empty($aParams['except']))
+            		$sWhereClause .= " AND `tfe`.`file_id` NOT IN (" . $this->implode_escape($aParams['except']) . ")";
+
+            	break;
+
+			case 'content_id_key_file_id':
+				$aMethod['name'] = 'getAllWithKey';
+				$aMethod['params'][1] = 'file_id';
+
+                $sWhereClause = $this->prepare(" AND `tfe`.`content_id`=?", $aParams['content_id']);
+                break;
+        }
+
+        $aMethod['params'][0] = "SELECT
+        		`tfe`.*
+            FROM `" . $sTable . "` AS `tfe`
+            WHERE 1" . $sWhereClause;
+
+        return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
+    }
+
+	protected function _updateAttachment($sTable, $aSet, $aWhere)
+    {
+        $sQuery = "UPDATE `" . $sTable . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1");
+        return (int)$this->query($sQuery) > 0;
+    }
 }
 
 /** @} */

+ 1 - 0
modules/boonex/market/install/sql/enable.sql

@@ -419,5 +419,6 @@ SET @iHandler := LAST_INSERT_ID();
 
 INSERT INTO `sys_alerts` (`unit`, `action`, `handler_id`) VALUES
 ('bx_market_files', 'file_deleted', @iHandler),
+('bx_market_files', 'file_downloaded', @iHandler),
 ('bx_market_photos', 'file_deleted', @iHandler),
 ('profile', 'delete', @iHandler);

+ 9 - 0
modules/boonex/market/install/sql/install.sql

@@ -75,6 +75,15 @@ CREATE TABLE IF NOT EXISTS `bx_market_files2products` (
   KEY `content_id` (`content_id`)
 );
 
+CREATE TABLE IF NOT EXISTS `bx_market_downloads_track` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `file_id` int(11) NOT NULL default '0',
+  `profile_id` int(11) NOT NULL default '0',
+  `profile_nip` int(11) unsigned NOT NULL default '0',
+  `date` int(11) NOT NULL default '0',
+  PRIMARY KEY (`id`)
+) ENGINE=MYISAM DEFAULT CHARSET=utf8;
+
 CREATE TABLE IF NOT EXISTS `bx_market_photos` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `profile_id` int(10) unsigned NOT NULL,

+ 1 - 1
modules/boonex/market/install/sql/uninstall.sql

@@ -1,6 +1,6 @@
 
 -- TABLES
-DROP TABLE IF EXISTS `bx_market_products`, `bx_market_licenses`, `bx_market_files`, `bx_market_files2products`, `bx_market_photos`, `bx_market_photos2products`, `bx_market_photos_resized`, `bx_market_cmts`, `bx_market_votes`, `bx_market_votes_track`, `bx_market_views_track`, `bx_market_meta_keywords`, `bx_market_meta_locations`, `bx_market_reports`, `bx_market_reports_track`;
+DROP TABLE IF EXISTS `bx_market_products`, `bx_market_licenses`, `bx_market_files`, `bx_market_files2products`, `bx_market_downloads_track`, `bx_market_photos`, `bx_market_photos2products`, `bx_market_photos_resized`, `bx_market_cmts`, `bx_market_votes`, `bx_market_votes_track`, `bx_market_views_track`, `bx_market_meta_keywords`, `bx_market_meta_locations`, `bx_market_reports`, `bx_market_reports_track`;
 
 -- STORAGES & TRANSCODERS
 DELETE FROM `sys_objects_storage` WHERE `object` IN('bx_market_files', 'bx_market_photos', 'bx_market_photos_resized');