Преглед изворни кода

Ticket #4971 - Build-in API support: Attached polls in Timeline.

AntonLV пре 1 година
родитељ
комит
f130443a01

+ 15 - 0
modules/base/general/classes/BxBaseModGeneralDb.php

@@ -301,6 +301,21 @@ class BxBaseModGeneralDb extends BxDolModuleDb
         return (int)$this->getOne("SELECT `object_id` FROM `" . $CNF['TABLE_POLLS_ANSWERS_VOTES_TRACK'] . "` WHERE `object_id` IN (" . $this->implode_escape(array_keys($aAnswers)) . ") " . $sWhereClause . " LIMIT 1", $aBindings) != 0;
     }
 
+    public function getPollPerformedValue($iPollId, $iAuthorId)
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        $aBindings = ['author_id' => $iAuthorId];
+        $sWhereClause = "AND `author_id`=:author_id";
+
+        $aSubentries = $this->getPollAnswers([
+            'type' => 'poll_id_pairs', 
+            'entry_id' => $iPollId
+        ]);
+
+        return (int)$this->getOne("SELECT `object_id` FROM `" . $CNF['TABLE_POLLS_ANSWERS_VOTES_TRACK'] . "` WHERE `object_id` IN (" . $this->implode_escape(array_keys($aSubentries)) . ") " . $sWhereClause . " LIMIT 1", $aBindings);
+    }
+
     public function getPolls($aParams)
     {
         $CNF = &$this->_oConfig->CNF;

+ 21 - 1
modules/base/general/classes/BxBaseModGeneralFormEntry.php

@@ -675,7 +675,17 @@ class BxBaseModGeneralFormEntry extends BxTemplFormView
 
         return $mixedResult;
     }
-    
+
+    public function getCleanValue($sName)
+    {
+        $mixedValue = parent::getCleanValue($sName);
+
+        if($this->_bIsApi && !empty($mixedValue) && isset($this->aInputs[$sName]['type']) && in_array($this->aInputs[$sName]['type'], ['polls']))
+            $mixedValue = explode(',', $mixedValue);
+
+        return $mixedValue;
+    }
+
     function getHtmlEditorQueryParams($aInput)
     {
         $aQueryParams = parent::getHtmlEditorQueryParams($aInput);
@@ -1328,6 +1338,16 @@ class BxBaseModGeneralFormEntry extends BxTemplFormView
 
     protected function genCustomInputPolls ($aInput)
     {
+        if($this->_bIsApi) {
+            $sModule = $this->_oModule->getName();
+
+            return array_merge($aInput, [
+                'type' => 'polls',
+                'form_get' => '/api.php?r=' . $sModule . '/get_poll_form',
+                'form_submit' => '/api.php?r=' . $sModule . '/submit_poll_form'
+            ]);
+        }
+
         return $this->_oModule->_oTemplate->getPollField(!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0);
     }
 

+ 16 - 0
modules/base/general/classes/BxBaseModGeneralFormPoll.php

@@ -65,6 +65,16 @@ class BxBaseModGeneralFormPoll extends BxTemplFormView
         return $iResult;
     }
 
+    public function getCleanValue ($sName)
+    {
+        $mixedValue = parent::getCleanValue($sName);
+
+        if($this->_bIsApi && !empty($mixedValue) && in_array($sName, ['answers', 'answers_ids']))
+            $mixedValue = explode(',', $mixedValue);
+
+        return $mixedValue;
+    }
+
     public function processAnswersAdd ($sField, $iContentId = 0)
     {
         $CNF = &$this->_oModule->_oConfig->CNF;
@@ -129,6 +139,12 @@ class BxBaseModGeneralFormPoll extends BxTemplFormView
 
     protected function genCustomInputAnswers(&$aInput)
     {
+        if($this->_bIsApi)
+            return array_merge($aInput, [
+                'type' => 'multi_field',
+                'subtype' => 'text'
+            ]);
+
         $sJsObject = $this->_oModule->_oConfig->getJsObject('poll');
 
         $aTmplVarsAnswers = array(

+ 49 - 1
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -387,6 +387,43 @@ class BxBaseModGeneralModule extends BxDolModule
         echoJson($this->getPollForm());
     }
 
+    public function serviceGetPollForm()
+    {
+        if(!$this->_bIsApi)
+            return false;
+
+        $aResult = $this->getPollForm();
+
+        if(isset($aResult['code'])) {
+            if((int)$aResult['code'] == 0) {
+                return [
+                    'id' => $aResult['id'],
+                    'item' => 'TODO: return newly created poll'
+                ];
+            }
+            else
+                return isset($aResult['message']) ? [bx_api_get_msg($aResult['message'])] : [];
+        }
+        else if(isset($aResult['form'])) {
+            $sModule = $this->getName();
+
+            return [bx_api_get_block('form', $aResult['form'], [
+                'ext' => [
+                    'name' => $sModule, 
+                    'request' => ['url' => '/api.php?r=' . $sModule . '/submit_poll_form', 'immutable' => true]
+                ]
+            ])];
+        }
+    }
+
+    public function serviceSubmitPollForm()
+    {
+        if(!$this->_bIsApi)
+            return false;
+
+        return $this->serviceGetPollForm();
+    }
+
     public function getPollForm()
     {
         $CNF = &$this->_oConfig->CNF;
@@ -410,7 +447,7 @@ class BxBaseModGeneralModule extends BxDolModule
                 return array('code' => 2, 'message' => '_sys_txt_error_entry_creation');
         }
 
-        return array('form' => $oForm->getCode(), 'form_id' => $oForm->id);
+        return array('form' => $oForm->{'getCode' . ($this->_bIsApi ? 'API' : '')}(), 'form_id' => $oForm->id);
     }
 
     public function actionGetCreatePostForm()
@@ -673,6 +710,9 @@ class BxBaseModGeneralModule extends BxDolModule
             'EntityEdit' => '',
             'EntityDelete' => '',
             'UpdateImage' => '',
+            // polls
+            'GetPollForm' => '',
+            'SubmitPollForm' => '',
             // page blocks
             'EntityTextBlock' => '',
             'EntityInfo' => '',
@@ -3706,6 +3746,14 @@ class BxBaseModGeneralModule extends BxDolModule
         return $this->_oDb->isPollPerformed($iObjectId, $iAuthorId, $iAuthorIp);
     }
 
+    public function getPollPerformedValue($iObjectId, $iAuthorId = 0)
+    {
+        if(empty($iAuthorId))
+            $iAuthorId = bx_get_logged_profile_id();
+
+        return $this->_oDb->getPollPerformedValue($iObjectId, $iAuthorId);
+    }
+
     public function onPublished($iContentId)
     {
         $CNF = &$this->_oConfig->CNF;

+ 33 - 12
modules/base/general/classes/BxBaseModGeneralTemplate.php

@@ -839,7 +839,24 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
 
         $aPoll = is_array($mixedPoll) ? $mixedPoll : $this->_oDb->getPolls(array('type' => 'id', 'id' => (int)$mixedPoll));
         if(empty($aPoll) || !is_array($aPoll))
-            return '';
+            return $this->_bIsApi ? [] : '';
+
+        $iPollId = (int)$aPoll[$CNF['FIELD_POLL_ID']];
+        $sPollText = bx_process_output($aPoll[$CNF['FIELD_POLL_TEXT']], BX_DATA_TEXT);
+
+        if($this->_bIsApi) {
+            $bPerformed = $oModule->isPollPerformed($iPollId, $iProfileId);
+
+            return [
+                'id' => $iPollId,
+                'title' => $sPollText,
+                'object' => $CNF['OBJECT_VOTES_POLL_ANSWERS'],
+                'subentries' => $this->_getPollAnswers($aPoll),
+                'results' => $this->_getPollResults($aPoll),
+                'is_performed' => $bPerformed,
+                'value' => $bPerformed ? $oModule->getPollPerformedValue($iPollId) : 0
+            ];
+        }
 
         $sJsObject = $this->_oConfig->getJsObject('poll');
 
@@ -848,7 +865,6 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
         $bSwitchMenu = isset($aParams['switch_menu']) ? (bool)$aParams['switch_menu'] : true;
         $bForceDisplayAnswers = isset($aParams['force_display_answers']) && (bool)$aParams['force_display_answers'] === true;
 
-        $iPollId = (int)$aPoll[$CNF['FIELD_POLL_ID']];
         $sPollView = !$bForceDisplayAnswers && $oModule->isPollPerformed($iPollId, $iProfileId) ? 'results' : 'answers';
         
         $sMethod = '_getPoll' . ucfirst($sPollView);
@@ -884,7 +900,7 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
                     'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : ''
                 )
             ),
-            'text' => bx_process_output($aPoll[$CNF['FIELD_POLL_TEXT']], BX_DATA_TEXT),
+            'text' => $sPollText,
             'content' => $this->$sMethod($aPoll, $bDynamic)
         ));
     }
@@ -982,6 +998,9 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
         $CNF = &$this->getModule()->_oConfig->CNF;
         
         $aAnswers = $this->_oDb->getPollAnswers(array('type' => 'poll_id', 'poll_id' => $aPoll[$CNF['FIELD_POLL_ID']]));
+        if($this->_bIsApi)
+            return $aAnswers;
+
         if(empty($aAnswers) || !is_array($aAnswers))
             return '';
 
@@ -1001,34 +1020,36 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
             'bx_repeat:answers' => $aTmplVarsAnswers
         ));
     }
-    
+
     protected function _getPollResults($aPoll, $bDynamic = false)
     {
         $CNF = &$this->getModule()->_oConfig->CNF;
 
         $aAnswers = $this->_oDb->getPollAnswers(array('type' => 'poll_id', 'poll_id' => $aPoll[$CNF['FIELD_POLL_ID']]));
         if(empty($aAnswers) || !is_array($aAnswers))
-            return '';
+            return $this->_bIsApi ? [] : '';
 
         $iTotal = 0;
         foreach($aAnswers as $aAnswer)
             $iTotal += $aAnswer['votes'];
 
-        $aTmplVarsAnswers = array();
+        $aTmplVarsAnswers = [];
         foreach($aAnswers as $aAnswer) {
             $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES_POLL_ANSWERS'], $aAnswer['id']);
-            $aVotesParams = array('show_counter_empty' => true, 'show_counter_in_brackets' => false, 'dynamic_mode' => $bDynamic);
 
             $fPercent = $iTotal > 0 ? 100 * (float)$aAnswer['votes']/$iTotal : 0;
-            $aTmplVarsAnswers[] = array(
+            $aTmplVarsAnswers[] = array_merge([
                 'title' => bx_process_output($aAnswer['title']),
                 'width' => (int)round($fPercent) . '%',
-                'votes' => $oVotes->getCounter($aVotesParams),
-                'percent' => _t($CNF['T']['txt_poll_answer_vote_percent'], $iTotal > 0 ? round($fPercent, 2) : 0)
-            );
+                'percent' => _t($CNF['T']['txt_poll_answer_vote_percent'], $iTotal > 0 ? round($fPercent, 2) : 0),
+            ], ($this->_bIsApi ? [
+                'votes' => $oVotes->getCounterAPI(['show_counter_empty' => true, 'show_counter_in_brackets' => false]),
+            ] : [
+                'votes' => $oVotes->getCounter(['show_counter_empty' => true, 'show_counter_in_brackets' => false, 'dynamic_mode' => $bDynamic]),
+            ]));
         }
 
-        return $this->parseHtmlByName('poll_item_results.html', array(
+        return $this->_bIsApi ? $aTmplVarsAnswers : $this->parseHtmlByName('poll_item_results.html', array(
             'html_id' => $this->_oConfig->getHtmlIds('poll_content') . $aPoll[$CNF['FIELD_POLL_ID']],
             'bx_repeat:answers' => $aTmplVarsAnswers,
         ));

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

@@ -5675,11 +5675,13 @@ class BxTimelineModule extends BxBaseModNotificationsModule implements iBxDolCon
             'content_id' => $iEventId, 
         ]);
         if(empty($aPolls) || !is_array($aPolls))
-            return;
+            return [];
+
+        $iProfile = bx_get_logged_profile_id();
 
         $aResult = [];
         foreach($aPolls as $aPoll)
-            $aResult[$aPoll['id']] = $aPoll;
+            $aResult[$aPoll['id']] = $this->_bIsApi ? $this->_oTemplate->getPollItem($aPoll, $iProfile) : $aPoll;
 
         return $aResult;
     }

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

@@ -3393,6 +3393,7 @@ class BxTimelineTemplate extends BxBaseModNotificationsTemplate
                 if($this->_bIsApi) {
                     $aResult['content']['videos_attach'] = array_values($aResult['content']['videos_attach']);
                     $aResult['content']['files_attach'] = array_values($aResult['content']['files_attach']);
+                    $aResult['content']['polls_attach'] = array_values($aResult['content']['polls_attach']);
                 }
                 break;
 
@@ -3457,7 +3458,7 @@ class BxTimelineTemplate extends BxBaseModNotificationsTemplate
 
         if($this->_bIsApi)
             $aResult['content'] = array_intersect_key($aResult['content'], array_flip([
-                'object_id', 'url', 'title', 'text', 'links', 'images', 'images_attach', 'videos', 'videos_attach', 'files', 'files_attach', 'parse_type', 'owner_name', 'embed'
+                'object_id', 'url', 'title', 'text', 'links', 'images', 'images_attach', 'videos', 'videos_attach', 'files', 'files_attach', 'polls_attach', 'parse_type', 'owner_name', 'embed'
             ]));
 
         return $aResult;