Przeglądaj źródła

#5635 Add pin/unpin promote/unpromote to API calls

romanlesnikov 1 miesiąc temu
rodzic
commit
a97b1fde6d

+ 30 - 3
modules/boonex/timeline/classes/BxTimelineMenuItemActions.php

@@ -432,9 +432,6 @@ class BxTimelineMenuItemActions extends BxTemplMenuCustom
 
     protected function _getMenuItemDefaultApi ($aItem)
     {
-        $sType = $this->_aEvent['type'];
-        $sAction = $this->_aEvent['action'];
-
         if(!empty($aItem['submenu']) && ($oSubmenu = BxDolMenu::getObjectInstance($aItem['submenu'])) !== false) {
             $sMethod = 'setEventById';
             if(method_exists($oSubmenu, $sMethod))
@@ -446,9 +443,39 @@ class BxTimelineMenuItemActions extends BxTemplMenuCustom
             ]);
         }
 
+        switch($aItem['name'] ?? '') {
+            case 'item-pin':
+            case 'item-unpin':
+                $aItem = $this->_getMenuItemCallbackApi($aItem, 'pin');
+                break;
+
+            case 'item-stick':
+            case 'item-unstick':
+                $aItem = $this->_getMenuItemCallbackApi($aItem, 'stick');
+                break;
+
+            case 'item-promote':
+            case 'item-unpromote':
+                $aItem = $this->_getMenuItemCallbackApi($aItem, 'promote');
+                break;
+        }
+
         return $aItem;
     }
 
+    protected function _getMenuItemCallbackApi($aItem, $sService)
+    {
+        if(!empty($aItem['link']) && strpos($aItem['link'], 'javascript:') === 0)
+            $aItem['link'] = '';
+
+        return array_merge($aItem, [
+            'display_type' => 'callback',
+            'data' => [
+                'request_url' => $this->_sModule . '/' . $sService . '/&params[]=' . $this->_iEvent
+            ]
+        ]);
+    }
+
     protected function _getMenuItemElementApi($aItem, $aElement)
     {
         if(!$this->_bIsApi)

+ 2 - 2
modules/boonex/timeline/classes/BxTimelineMenuItemActionsAll.php

@@ -173,7 +173,7 @@ class BxTimelineMenuItemActionsAll extends BxTimelineMenuItemActions
         if(empty($aItem) || !is_array($aItem))
             return false;
 
-        return $aItem['item'];
+        return $this->_bIsApi ? $aItem : $aItem['item'];
     }
 
     protected function _getMenuItemByNameManage($aItem, $aParams = array())
@@ -186,7 +186,7 @@ class BxTimelineMenuItemActionsAll extends BxTimelineMenuItemActions
         if(empty($aItem) || !is_array($aItem))
             return false;
 
-        return $aItem['item'];
+        return $this->_bIsApi ? $aItem : $aItem['item'];
     }
 
     private function _getUniquePart($sDelimiter = '')

+ 20 - 4
modules/boonex/timeline/classes/BxTimelineMenuItemManage.php

@@ -57,7 +57,12 @@ class BxTimelineMenuItemManage extends BxTimelineMenuItemActions
         if(!isset($aParams['content_id']))
             return false;
 
-        $this->setEventById($aParams['content_id']);
+        $aBrowseParams = [];
+        foreach(['name', 'view', 'type'] as $sKey)
+            if(isset($aParams[$sKey]) && $aParams[$sKey] !== '')
+                $aBrowseParams[$sKey] = $aParams[$sKey];
+
+        $this->setEventById($aParams['content_id'], $aBrowseParams);
 
         return parent::setContentParams($aParams);
     }
@@ -66,10 +71,21 @@ class BxTimelineMenuItemManage extends BxTimelineMenuItemActions
     {
         $bResult = parent::setEvent($aEvent, $aBrowseParams);
 
-        if($bResult) 
-            $this->_aContentParams = array_merge($this->_aContentParams, [
+        if($bResult) {
+            $aContentParams = [
                 'content_id' => $this->_iEvent
-            ]);
+            ];
+            foreach(['name', 'view', 'type'] as $sKey) {
+                if(!empty($aBrowseParams[$sKey]))
+                    $aContentParams[$sKey] = $aBrowseParams[$sKey];
+            }
+            if(empty($aContentParams['type']) && !empty($this->_sType))
+                $aContentParams['type'] = $this->_sType;
+            if(empty($aContentParams['view']) && !empty($this->_sView))
+                $aContentParams['view'] = $this->_sView;
+
+            $this->_aContentParams = array_merge($this->_aContentParams, $aContentParams);
+        }
 
         return $bResult;
     }

+ 225 - 89
modules/boonex/timeline/classes/BxTimelineModule.php

@@ -205,119 +205,50 @@ class BxTimelineModule extends BxBaseModNotificationsModule implements iBxDolCon
     function actionPin()
     {
         $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
-        $aEvent = $this->_oDb->getEvents(array('browse' => 'id', 'value' => $iId));
-        if(empty($aEvent) || !is_array($aEvent))
-            return echoJson(array('code' => 1));
-
         $aParams = $this->_prepareParamsGet();
         $this->_iOwnerId = $aParams['owner_id'];
-        
-        $mixedAllowed = $this->{'isAllowed' . ((int)$aEvent['pinned'] == 0 ? 'Pin' : 'Unpin')}($aEvent, true);
-        if($mixedAllowed !== true)
-            return echoJson(array('code' => 2, 'message' => strip_tags($mixedAllowed)));
 
-        $aEvent['pinned'] = (int)$aEvent['pinned'] == 0 ? time() : 0;
-        if(!$this->_oDb->updateEvent(array('pinned' => $aEvent['pinned']), array('id' => $iId)))
-            return echoJson(array('code' => 3));
-
-        $this->rebuildSlice();
+        $a = $this->_performPin($iId);
+        if ((int)$a['code'] !== 0)
+            return echoJson($a);
 
-        echoJson(array(
-            'code' => 0, 
-            'id' => $iId, 
+        echoJson([
+            'code' => 0,
+            'id' => $a['id'],
             'eval' => $this->_oConfig->getJsObjectView($aParams) . '.onPinPost(oData)'
-        ));
+        ]);
     }
 
     function actionStick()
     {
-    	$iId = bx_process_input(bx_get('id'), BX_DATA_INT);
-    	$aEvent = $this->_oDb->getEvents(array('browse' => 'id', 'value' => $iId));
-        if(empty($aEvent) || !is_array($aEvent))
-            return echoJson(array('code' => 1));
-
+        $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
         $aParams = $this->_prepareParamsGet();
         $this->_iOwnerId = $aParams['owner_id'];
 
-    	$mixedAllowed = $this->{'isAllowed' . ((int)$aEvent['sticked'] == 0 ? 'Stick' : 'Unstick')}($aEvent, true);
-    	if($mixedAllowed !== true)
-            return echoJson(array('code' => 2, 'message' => strip_tags($mixedAllowed)));
-
-    	$aEvent['sticked'] = (int)$aEvent['sticked'] == 0 ? time() : 0;
-    	if(!$this->_oDb->updateEvent(array('sticked' => $aEvent['sticked']), array('id' => $iId)))
-            return echoJson(array('code' => 3));
+        $a = $this->_performStick($iId);
+        if ((int)$a['code'] !== 0)
+            return echoJson($a);
 
-        $this->rebuildSlice();
-
-        bx_audit(
-            $iId, 
-            $this->getName(), 
-            '_sys_audit_action_' . ((int)$aEvent['sticked'] == 0 ? 'stick' : 'unstick'),  
-            $this->_prepareAuditParams($aEvent, false)
-        );
-        
-    	echoJson(array(
+        echoJson([
             'code' => 0,
-            'id' => $iId,
+            'id' => $a['id'],
             'eval' => $this->_oConfig->getJsObjectView($aParams) . '.onStickPost(oData)'
-        ));
+        ]);
     }
 
     function actionPromote()
     {
         $this->_iOwnerId = bx_process_input(bx_get('owner_id'), BX_DATA_INT);
-
         $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
-        $aEvent = $this->_oDb->getEvents(array('browse' => 'id', 'value' => $iId));
-
-        $sAction = (int)$aEvent['promoted'] == 0 ? 'promote' : 'unpromote';
-
-        $mixedAllowed = $this->{'isAllowed' . ucfirst($sAction)}($aEvent, true);
-        if($mixedAllowed !== true)
-            return echoJson(array('code' => 1, 'message' => strip_tags($mixedAllowed)));
-
-        $aEvent['promoted'] = (int)$aEvent['promoted'] == 0 ? time() : 0;
-        if(!$this->_oDb->updateEvent(array('promoted' => $aEvent['promoted']), array('id' => $iId)))
-            return echoJson(array('code' => 2));
-
-        $this->rebuildSlice();
 
-        /**
-         * @hooks
-         * @hookdef hook-bx_timeline-promoted 'bx_timeline', 'promoted' - hook after an event was promoted
-         * - $unit_name - equals `bx_timeline`
-         * - $action - equals `promoted`
-         * - $object_id - event id
-         * - $sender_id - performer profile id
-         * - $extra_params - array of additional params with the following array keys:
-         *      - `owner_id` - [int] owner profile id
-         *      - `object_id` - [int] object id
-         *      - `object_author_id` - [int] object author profile id
-         * @hook @ref hook-bx_timeline-promoted
-         */
-        /**
-         * @hooks
-         * @hookdef hook-bx_timeline-unpromoted 'bx_timeline', 'unpromoted' - hook after an event was unpromoted
-         * It's equivalent to @ref hook-bx_timeline-promoted
-         * @hook @ref hook-bx_timeline-unpromoted
-         */
-        bx_alert($this->_oConfig->getObject('alert'), $sAction . 'd', $iId, (int)$this->getUserId(), [
-            'owner_id' => $aEvent['owner_id'],
-            'object_id' => $aEvent['object_id'],
-            'object_author_id' => $this->_oConfig->isSystem($aEvent['type'], $aEvent['action']) ? $aEvent['owner_id'] : $aEvent['object_id']
-        ]);
-        
-        bx_audit(
-            $iId, 
-            $this->getName(), 
-            '_sys_audit_action_' . $sAction,  
-            $this->_prepareAuditParams($aEvent, false)
-        );
+        $a = $this->_performPromote($iId);
+        if ((int)$a['code'] !== 0)
+            return echoJson($a);
 
-        echoJson(array(
-            'code' => 0, 
+        echoJson([
+            'code' => 0,
             'message' => _t('_bx_timeline_txt_msg_performed_action')
-        ));
+        ]);
     }
 
     function actionMarkAsRead()
@@ -975,6 +906,9 @@ class BxTimelineModule extends BxBaseModNotificationsModule implements iBxDolCon
             'Get' => '',
             'Repost' => '',
             'Delete' => '',
+            'Pin' => '',
+            'Stick' => '',
+            'Promote' => '',
             'GetEditForm' => '',
             'GetLiveUpdate' => ''
         ));
@@ -3363,6 +3297,106 @@ class BxTimelineModule extends BxBaseModNotificationsModule implements iBxDolCon
         return true;
     }
 
+    /**
+     * @page service Service Calls
+     * @section bx_timeline Timeline
+     * @subsection bx_timeline-other Other
+     * @subsubsection bx_timeline-pin pin
+     * 
+     * @code bx_srv('bx_timeline', 'pin', [...]); @endcode
+     * 
+     * Pin or unpin a timeline event by ID. Pin is shown on the profile timeline for the owner.
+     * 
+     * @param $iId integer value with event ID.
+     * @return array with the result of operation.
+     * 
+     * @see BxTimelineModule::servicePin
+     */
+    /** 
+     * @ref bx_timeline-pin "pin"
+     */
+    public function servicePin($iId)
+    {
+        $a = $this->_performPin((int)$iId);
+        if ((int)$a['code'] !== 0)
+            return $a;
+
+        return [
+            'code' => 0,
+            'id' => $a['id'],
+            'pinned' => $a['pinned'],
+            'title' => _t('_bx_timeline_menu_item_title_item_' . $a['next']),
+            'request_url' => $this->getName() . '/pin/&params[]=' . $a['id'],
+        ];
+    }
+
+    /**
+     * @page service Service Calls
+     * @section bx_timeline Timeline
+     * @subsection bx_timeline-other Other
+     * @subsubsection bx_timeline-stick stick
+     * 
+     * @code bx_srv('bx_timeline', 'stick', [...]); @endcode
+     * 
+     * Stick or unstick a timeline event by ID. Stick is shown on the public timeline for everybody.
+     * 
+     * @param $iId integer value with event ID.
+     * @return array with the result of operation.
+     * 
+     * @see BxTimelineModule::serviceStick
+     */
+    /** 
+     * @ref bx_timeline-stick "stick"
+     */
+    public function serviceStick($iId)
+    {
+        $a = $this->_performStick((int)$iId);
+        if ((int)$a['code'] !== 0)
+            return $a;
+
+        return [
+            'code' => 0,
+            'id' => $a['id'],
+            'sticked' => $a['sticked'],
+            'title' => _t('_bx_timeline_menu_item_title_item_' . $a['next']),
+            'request_url' => $this->getName() . '/stick/&params[]=' . $a['id'],
+        ];
+    }
+
+    /**
+     * @page service Service Calls
+     * @section bx_timeline Timeline
+     * @subsection bx_timeline-other Other
+     * @subsubsection bx_timeline-promote promote
+     * 
+     * @code bx_srv('bx_timeline', 'promote', [...]); @endcode
+     * 
+     * Promote or unpromote a timeline event by ID.
+     * 
+     * @param $iId integer value with event ID.
+     * @return array with the result of operation.
+     * 
+     * @see BxTimelineModule::servicePromote
+     */
+    /** 
+     * @ref bx_timeline-promote "promote"
+     */
+    public function servicePromote($iId)
+    {
+        $a = $this->_performPromote((int)$iId);
+        if ((int)$a['code'] !== 0)
+            return $a;
+
+        return [
+            'code' => 0,
+            'id' => $a['id'],
+            'promoted' => $a['promoted'],
+            'title' => _t('_bx_timeline_menu_item_title_item_' . $a['next']),
+            'request_url' => $this->getName() . '/promote/&params[]=' . $a['id'],
+            'message' => _t('_bx_timeline_txt_msg_performed_action')
+        ];
+    }
+
     /**
      * @page service Service Calls
      * @section bx_timeline Timeline
@@ -5847,6 +5881,108 @@ class BxTimelineModule extends BxBaseModNotificationsModule implements iBxDolCon
         $this->_oDb->rebuildSlice();
     }
 
+    protected function _performPin($iId)
+    {
+        $iId = (int)$iId;
+        $aEvent = $this->_oDb->getEvents(['browse' => 'id', 'value' => $iId]);
+        if(empty($aEvent) || !is_array($aEvent))
+            return ['code' => 1];
+
+        $bPin = (int)$aEvent['pinned'] == 0;
+        $mixedAllowed = $this->{'isAllowed' . ($bPin ? 'Pin' : 'Unpin')}($aEvent, true);
+        if($mixedAllowed !== true)
+            return ['code' => 2, 'message' => strip_tags($mixedAllowed)];
+
+        $aEvent['pinned'] = $bPin ? time() : 0;
+        if(!$this->_oDb->updateEvent(['pinned' => $aEvent['pinned']], ['id' => $iId]))
+            return ['code' => 3];
+
+        $this->rebuildSlice();
+        $this->onEdit($iId);
+
+        return [
+            'code' => 0,
+            'id' => $iId,
+            'pinned' => (int)$aEvent['pinned'],
+            'next' => $bPin ? 'unpin' : 'pin',
+        ];
+    }
+
+    protected function _performStick($iId)
+    {
+        $iId = (int)$iId;
+        $aEvent = $this->_oDb->getEvents(['browse' => 'id', 'value' => $iId]);
+        if(empty($aEvent) || !is_array($aEvent))
+            return ['code' => 1];
+
+        $bStick = (int)$aEvent['sticked'] == 0;
+        $mixedAllowed = $this->{'isAllowed' . ($bStick ? 'Stick' : 'Unstick')}($aEvent, true);
+        if($mixedAllowed !== true)
+            return ['code' => 2, 'message' => strip_tags($mixedAllowed)];
+
+        $aEvent['sticked'] = $bStick ? time() : 0;
+        if(!$this->_oDb->updateEvent(['sticked' => $aEvent['sticked']], ['id' => $iId]))
+            return ['code' => 3];
+
+        $this->rebuildSlice();
+        $this->onEdit($iId);
+
+        $sAction = $bStick ? 'stick' : 'unstick';
+        bx_audit(
+            $iId,
+            $this->getName(),
+            '_sys_audit_action_' . $sAction,
+            $this->_prepareAuditParams($aEvent, false)
+        );
+
+        return [
+            'code' => 0,
+            'id' => $iId,
+            'sticked' => (int)$aEvent['sticked'],
+            'next' => $bStick ? 'unstick' : 'stick',
+        ];
+    }
+
+    protected function _performPromote($iId)
+    {
+        $iId = (int)$iId;
+        $aEvent = $this->_oDb->getEvents(['browse' => 'id', 'value' => $iId]);
+        if(empty($aEvent) || !is_array($aEvent))
+            return ['code' => 1];
+
+        $sAction = (int)$aEvent['promoted'] == 0 ? 'promote' : 'unpromote';
+        $mixedAllowed = $this->{'isAllowed' . ucfirst($sAction)}($aEvent, true);
+        if($mixedAllowed !== true)
+            return ['code' => 1, 'message' => strip_tags($mixedAllowed)];
+
+        $aEvent['promoted'] = (int)$aEvent['promoted'] == 0 ? time() : 0;
+        if(!$this->_oDb->updateEvent(['promoted' => $aEvent['promoted']], ['id' => $iId]))
+            return ['code' => 2];
+
+        $this->rebuildSlice();
+        $this->onEdit($iId);
+
+        bx_alert($this->_oConfig->getObject('alert'), $sAction . 'd', $iId, (int)$this->getUserId(), [
+            'owner_id' => $aEvent['owner_id'],
+            'object_id' => $aEvent['object_id'],
+            'object_author_id' => $this->_oConfig->isSystem($aEvent['type'], $aEvent['action']) ? $aEvent['owner_id'] : $aEvent['object_id']
+        ]);
+
+        bx_audit(
+            $iId,
+            $this->getName(),
+            '_sys_audit_action_' . $sAction,
+            $this->_prepareAuditParams($aEvent, false)
+        );
+
+        return [
+            'code' => 0,
+            'id' => $iId,
+            'promoted' => (int)$aEvent['promoted'],
+            'next' => (int)$aEvent['promoted'] == 0 ? 'promote' : 'unpromote',
+        ];
+    }
+
 
     /**
      * Protected Methods 

+ 2 - 2
modules/boonex/timeline/classes/BxTimelineTemplate.php

@@ -2344,7 +2344,7 @@ class BxTimelineTemplate extends BxBaseModNotificationsTemplate
         else
             $oMenuManage = $aEvent['menu_manage'];
 
-        $aEvent['menu_manage'] = $oMenuManage !== false && $oMenuManage->setEvent($aEvent) && $oMenuManage->isVisible() ? $oMenuManage->getShortCodeAPI() : [];
+        $aEvent['menu_manage'] = $oMenuManage !== false && $oMenuManage->setEvent($aEvent, $aParams) && $oMenuManage->isVisible() ? $oMenuManage->getShortCodeAPI() : [];
 
         //--- Menu: Counters
         if(empty($aEvent['menu_counters']))
@@ -2388,7 +2388,7 @@ class BxTimelineTemplate extends BxBaseModNotificationsTemplate
          */
 
         return array_intersect_key($aEvent, array_flip([
-            'id', 'type', 'object_privacy_view', 'content', 'labels', 'date', 'menu_actions', 'menu_counters', 'menu_manage', 'author_data', 'author_badges', 'author_actions', 'context_data', 'url', 'owners', 'cmts', 'module', 'title'
+            'id', 'type', 'object_privacy_view', 'content', 'labels', 'date', 'menu_actions', 'menu_counters', 'menu_manage', 'author_data', 'author_badges', 'author_actions', 'context_data', 'url', 'owners', 'cmts', 'module', 'title', 'pinned', 'sticked', 'promoted'
         ]));
     }