Răsfoiți Sursa

#24 - Share feature improvements.

AntonLV 12 ani în urmă
părinte
comite
9d980a26a3

+ 53 - 18
modules/boonex/timeline/classes/BxTimelineDb.php

@@ -109,16 +109,36 @@ class BxTimelineDb extends BxDolModuleDb
 
     public function deleteModuleEvents($aData)
     {
-    	foreach($aData['handlers'] as $aHandler)
+    	foreach($aData['handlers'] as $aHandler) {
+    		//Delete system events.
             $this->deleteEvent(array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
+
+            //Delete shared events.
+    		$aEvents = $this->getEvents(array('browse' => 'shared_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
+			foreach($aEvents as $aEvent) {
+				$aContent = unserialize($aEvent['content']);
+				if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
+					$this->deleteEvent(array('id' => (int)$aEvent['id']));
+			}
+    	}
     }
 
 	public function activateModuleEvents($aData, $bActivate = true)
     {
     	$iActivate = $bActivate ? 1 : 0;
 
-    	foreach($aData['handlers'] as $aHandler)
+    	foreach($aData['handlers'] as $aHandler) {
+    		//Activate (deactivate) system events.
             $this->updateEvent(array('active' => $iActivate), array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
+
+			//Activate (deactivate) shared events.
+			$aEvents = $this->getEvents(array('browse' => 'shared_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
+			foreach($aEvents as $aEvent) {
+				$aContent = unserialize($aEvent['content']);
+				if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
+					$this->updateEvent(array('active' => $iActivate), array('id' => (int)$aEvent['id']));
+			}
+    	}
     }
 
     public function getHandlers($aParams = array())
@@ -161,25 +181,13 @@ class BxTimelineDb extends BxDolModuleDb
         if(empty($aParamsSet) || empty($aParamsWhere))
             return false;
 
-        $aSet = array();
-        foreach($aParamsSet as $sKey => $sValue)
-           $aSet[] = $this->prepare("`" . $sKey . "`=?", $sValue);
-
-        $aWhere = array();
-        foreach($aParamsWhere as $sKey => $sValue)
-           $aWhere[] = $this->prepare("`" . $sKey . "`=?", $sValue);
-
-        $sSql = "UPDATE `{$this->_sTable}` SET " . implode(", ", $aSet) . " WHERE " . implode(" AND ", $aWhere);
+        $sSql = "UPDATE `{$this->_sTable}` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
         return $this->query($sSql);
     }
 
     public function deleteEvent($aParams, $sWhereAddon = "")
     {
-        $aWhere = array();
-        foreach($aParams as $sKey => $sValue)
-           $aWhere[] = $this->prepare("`" . $sKey . "`=?", $sValue);
-
-        $sSql = "DELETE FROM `{$this->_sTable}` WHERE " . implode(" AND ", $aWhere) . $sWhereAddon;
+        $sSql = "DELETE FROM `{$this->_sTable}` WHERE " . $this->arrayToSQL($aParams, " AND ") . $sWhereAddon;
         return $this->query($sSql);
     }
 
@@ -262,6 +270,14 @@ class BxTimelineDb extends BxDolModuleDb
                         break;
                 }
                 break;
+
+            case 'shared_by_descriptor':
+            	if(isset($aParams['type']))
+                	$sWhereClause .= "AND `{$this->_sTable}`.`content` LIKE '%" . $this->escape($aParams['type']) . "%'";
+
+                if(isset($aParams['action']))
+                	$sWhereClause .= "AND `{$this->_sTable}`.`content` LIKE '%" . $this->escape($aParams['action']) . "%'";
+                break;
         }
 
         $sSql = "SELECT
@@ -322,12 +338,31 @@ class BxTimelineDb extends BxDolModuleDb
 
     public function getShared($sType, $sAction, $iObjectId)
     {
-        if($this->_oConfig->isSystem($sType, $sAction))
+    	$bSystem = $this->_oConfig->isSystem($sType, $sAction);
+
+        if($bSystem)
             $aParams = array('browse' => 'descriptor', 'type' => $sType, 'action' => $sAction, 'object_id' => $iObjectId);
         else
             $aParams = array('browse' => 'id', 'value' => $iObjectId);
 
-        return $this->getEvents($aParams);
+		$aShared = $this->getEvents($aParams);
+		if($bSystem && (empty($aShared) || !is_array($aShared))) {
+			$iId = $this->insertEvent(array(
+				'owner_id' => 0,
+				'type' => $sType,
+				'action' => $sAction,
+				'object_id' => $iObjectId,
+				'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault(),
+				'content' => '',
+				'title' => '',
+				'description' => '',
+				'hidden' => 1
+			));
+
+			$aShared = $this->getEvents(array('browse' => 'id', 'value' => $iId));
+		}
+
+        return $aShared;
     }
 
     function getSharedBy($iSharedId)

+ 2 - 0
modules/boonex/timeline/classes/BxTimelineModule.php

@@ -114,6 +114,7 @@ class BxTimelineModule extends BxDolModule
             'object_id' => bx_process_input(bx_get('object_id'), BX_DATA_INT),
         );
 
+        //TODO: Share (from OUTSIDE) shouldn't depend on the original post in Timeline.
         $aShared = $this->_oDb->getShared($aContent['type'], $aContent['action'], $aContent['object_id']);
         if(empty($aShared) || !is_array($aShared)) {
             $this->_echoResultJson(array('code' => 1, 'msg' => _t('_bx_timeline_txt_err_cannot_share')));
@@ -850,6 +851,7 @@ class BxTimelineModule extends BxDolModule
         $aParams['modules'] = is_array($aModules) && !empty($aModules) ? $aModules : array();
         $aParams['timeline'] = (int)$iTimeline > 0 ? $iTimeline : 0;
         $aParams['active'] = 1;
+        $aParams['hidden'] = 0;
 
         return $aParams;
     }

+ 7 - 0
modules/boonex/timeline/classes/BxTimelineResponse.php

@@ -77,6 +77,13 @@ class BxTimelineResponse extends BxDolAlertsResponse
 
             case BX_TIMELINE_HANDLER_TYPE_DELETE:
                 $this->_oModule->_oDb->deleteEvent(array('type' => $oAlert->sUnit, 'object_id' => $oAlert->iObject));
+
+        		$aEvents = $this->_oModule->_oDb->getEvents(array('browse' => 'shared_by_descriptor', 'type' => $oAlert->sUnit));
+				foreach($aEvents as $aEvent) {
+					$aContent = unserialize($aEvent['content']);
+					if(isset($aContent['type']) && $aContent['type'] == $oAlert->sUnit && isset($aContent['object_id']) && (int)$aContent['object_id'] == $oAlert->iObject)
+						$this->_oModule->_oDb->deleteEvent(array('id' => (int)$aEvent['id']));
+				}
                 break;
         }
     }

+ 7 - 3
modules/boonex/timeline/classes/BxTimelineTemplate.php

@@ -871,15 +871,19 @@ class BxTimelineTemplate extends BxDolModuleTemplate
 
             case BX_TIMELINE_PARSE_TYPE_SHARE:
                 if(empty($aEvent['content']))
-                    break;
+                    return array();
 
                 $aContent = unserialize($aEvent['content']);
 
                 if(!$this->_oConfig->isSystem($aContent['type'] , $aContent['action'])) {
                     $aShared = $this->_oDb->getEvents(array('browse' => 'id', 'value' => $aContent['object_id']));
                     $aShared = $this->_getCommonData($aShared);
-                } else
-                    $aShared = $this->_getSystemData($aContent);
+                } 
+                else
+                	$aShared = $this->_getSystemData($aContent);
+
+				if(empty($aShared) || !is_array($aShared))
+					return array();
 
                 $aResult['content'] = array_merge($aContent, $aShared['content']);
                 $aResult['content']['parse_type'] = !empty($aShared['content_type']) ? $aShared['content_type'] : BX_TIMELINE_PARSE_TYPE_DEFAULT;

+ 1 - 0
modules/boonex/timeline/install/sql/install.sql

@@ -17,6 +17,7 @@ CREATE TABLE IF NOT EXISTS `bx_timeline_events` (
   `shares` int(11) unsigned NOT NULL default '0',
   `date` int(11) NOT NULL default '0',
   `active` tinyint(4) NOT NULL default '1',
+  `hidden` tinyint(4) NOT NULL default '0',
   PRIMARY KEY  (`id`),
   KEY `owner_id` (`owner_id`)
 );