Explorar o código

Ticket #4558 - Build-in API support: Selected Friends/Relations/Memberships in Privacy form field.

AntonLV hai 1 ano
pai
achega
f4f2c315b2

+ 8 - 4
inc/classes/BxDolForm.php

@@ -720,6 +720,8 @@ class BxDolForm extends BxDol implements iBxDolReplaceable
     static $LOCATION_INDEXES = array ('lat', 'lng', 'country', 'state', 'city', 'zip', 'street', 'street_number');
     static $LOCATION_INDEXES_MANDATORY = array ('lat', 'lng');
 
+    protected $_bIsApi;
+
     protected $_aMarkers = array ();
 
     protected $oTemplate;
@@ -748,6 +750,8 @@ class BxDolForm extends BxDol implements iBxDolReplaceable
     {
         parent::__construct();
 
+        $this->_bIsApi = bx_is_api();
+
         if ($oTemplate)
             $this->oTemplate = $oTemplate;
         else
@@ -1018,16 +1022,16 @@ class BxDolForm extends BxDol implements iBxDolReplaceable
         $oChecker->setFormMethod($this->aFormAttrs['method'], $this->_aSpecificValues);
         $a = isset($this->aInputs[$sName]) ? $this->aInputs[$sName] : false;
         $oRv = null;
-        
+
         if ($a && isset($a['db']['pass']))
             $oRv = $oChecker->get ($sName, $a['db']['pass'], isset($a['db']['params']) && $a['db']['params'] ? $a['db']['params'] : array());
         else
             $oRv =  $oChecker->get ($sName);
-        
+
         // process comma separated string for api values
-        if (bx_is_api() && isset($this->aInputs[$sName]['type']) && !empty($oRv) && (in_array($this->aInputs[$sName]['type'], ['checkbox_set', 'files', 'select_multiple']) || in_array($sName, ['labels'])))
+        if ($this->_bIsApi && isset($this->aInputs[$sName]['type']) && !empty($oRv) && (in_array($this->aInputs[$sName]['type'], ['checkbox_set', 'files', 'select_multiple']) || in_array($sName, ['labels'])))
             $oRv = explode(',', $oRv);
-            
+
         return $oRv;
     }
 

+ 52 - 0
inc/classes/BxDolPrivacy.php

@@ -237,6 +237,36 @@ class BxDolPrivacy extends BxDolFactory implements iBxDolFactoryObject
         return $oPrivacy->getJsScript($sJsCodeAdd, $bDynamicMode);
     }
     
+    public static function initGroupChooserAPI($sObject, $iOwnerId = 0, $aParams = [])
+    {
+        $sResult = '';
+
+        $oPrivacy = BxDolPrivacy::getObjectInstance($sObject);
+        if(empty($oPrivacy))
+            return $sResult;
+
+        $iOwnerId = !empty($iOwnerId) ? (int)$iOwnerId : bx_get_logged_profile_id();
+        $iContentId = !empty($aParams['content_id']) ? (int)$aParams['content_id'] : 0;
+        $iGroupId = !empty($aParams['group_id']) ? (int)$aParams['group_id'] : 0;
+
+        if($oPrivacy->isGroupsCustom() && ($aGroupsSettings = $oPrivacy->getGroupSettings($iGroupId)) !== false) {
+            $aGroupCustom = $oPrivacy->getGroupCustom([
+                'type' => 'pcog_ext', 
+                'profile_id' => $iOwnerId, 
+                'content_id' => $iContentId, 
+                'object' => $sObject, 
+                'group_id' => $iGroupId,
+                'group_items_table' => $aGroupsSettings['db_table_items'],
+                'group_items_field' => $aGroupsSettings['db_field_item']
+            ]);
+
+            if(is_array($aGroupCustom) && !empty($aGroupCustom['items']) && is_array($aGroupCustom['items']))
+                $sResult = implode(',', $aGroupCustom['items']);
+        }
+
+        return $sResult;
+    }
+    
     public static function getIcon($iVisibility)
     {
         $aIcons =array(
@@ -367,6 +397,28 @@ class BxDolPrivacy extends BxDolFactory implements iBxDolFactoryObject
         return $this->_oDb->getGroupCustom($aParams);
     }
 
+    public function insertGroupCustom($aGroup, $aItems = [])
+    {
+        $iGroupId = (int)$aGroup['group_id'];
+
+        $this->deleteGroupCustom([
+            'profile_id' => $aGroup['profile_id'],
+            'content_id' => $aGroup['content_id'],
+            'object' => $aGroup['object']
+        ]);
+
+        $iGroupCustomId = $this->_oDb->insertGroupCustom($aGroup);
+        if($iGroupCustomId !== false && !empty($aItems) && is_array($aItems)) {
+            $bMembers = $iGroupId == BX_DOL_PG_FRIENDS_SELECTED || $iGroupId == BX_DOL_PG_RELATIONS_SELECTED;
+            $bMemberships = $iGroupId == BX_DOL_PG_MEMBERSHIPS_SELECTED;
+            if(($bMembers || $bMemberships) && ($sType = $bMembers ? 'member' : ($bMemberships ? 'membership' : '')) !== '')
+                foreach($aItems as $iItem)
+                    $this->_oDb->{'insertGroupCustom' . ucfirst($sType)}(['group_id' => $iGroupCustomId, $sType . '_id' => $iItem]);
+        }
+
+        return $iGroupCustomId;
+    }
+
     public function updateGroupCustom($aParamsSet, $aParamsWhere)
     {
         return $this->_oDb->updateGroupCustom($aParamsSet, $aParamsWhere);

+ 28 - 9
modules/base/general/classes/BxBaseModGeneralFormEntry.php

@@ -530,6 +530,18 @@ class BxBaseModGeneralFormEntry extends BxTemplFormView
             }
         }
 
+        if($this->_bIsApi && isset($CNF['FIELD_ALLOW_VIEW_TO']) && ($mixedAllowViewTo = $this->getCleanValue($CNF['FIELD_ALLOW_VIEW_TO'])) !== false && ($mixedAllowViewToItems = $this->getCleanValue($CNF['FIELD_ALLOW_VIEW_TO'] . '_items')) !== false) {
+            $aAllowViewToItems = explode(',', $mixedAllowViewToItems);
+
+            if(($sKey = 'OBJECT_PRIVACY_VIEW') && isset($CNF[$sKey]) && ($sObject = $CNF[$sKey]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($sObject)) !== false)
+                $oPrivacy->insertGroupCustom([
+                    'profile_id' => $iAuthor,
+                    'content_id' => 0,
+                    'object' => $sObject,
+                    'group_id' => (int)$mixedAllowViewTo
+                ], $aAllowViewToItems);
+        }
+
         if($bMulticatEnabled)
             $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
 
@@ -965,16 +977,23 @@ class BxBaseModGeneralFormEntry extends BxTemplFormView
         $iProfileId = !empty($iContentId) ? (int)$this->getContentOwnerProfileId($iContentId) : bx_get_logged_profile_id();
         $iGroupId = !empty($aValues[$sField]) ? $aValues[$sField] : 0;
 
-        if(!isset($this->aInputs[$sField]['content']))
-            $this->aInputs[$sField]['content'] = '';
+        if(!$this->_bIsApi) {
+            if(!isset($this->aInputs[$sField]['content']))
+                $this->aInputs[$sField]['content'] = '';
 
-        $this->aInputs[$sField]['content'] .= $oPrivacy->initGroupChooser($sPrivacyObject, $iProfileId, array(
-            'content_id' => $iContentId,
-            'group_id' => $iGroupId,
-            'html_ids' => array(
-                'form' => $this->getId()
-            )
-        ));
+            $this->aInputs[$sField]['content'] .= $oPrivacy->initGroupChooser($sPrivacyObject, $iProfileId, [
+                'content_id' => $iContentId,
+                'group_id' => $iGroupId,
+                'html_ids' => [
+                    'form' => $this->getId()
+                ]
+            ]);
+        }
+        else 
+            $this->aInputs[$sField]['subvalue'] = $oPrivacy->initGroupChooserAPI($sPrivacyObject, $iProfileId, [
+                'content_id' => $iContentId,
+                'group_id' => $iGroupId,
+            ]);
     }
     
     protected function _validatePrivacyField($sField, $sPrivacyObject, $aValues)

+ 17 - 1
modules/boonex/timeline/classes/BxTimelineFormPost.php

@@ -148,7 +148,23 @@ class BxTimelineFormPost extends BxBaseModGeneralFormEntry
 
         $aValsToAdd[$CNF['FIELD_STATUS']] = $aValsToAdd[$CNF['FIELD_PUBLISHED']] > $aValsToAdd[$CNF['FIELD_ADDED']] ? BX_TIMELINE_STATUS_AWAITING : BX_TIMELINE_STATUS_ACTIVE;
 
-        return parent::insert ($aValsToAdd, $isIgnore);
+        $iContentId = parent::insert ($aValsToAdd, $isIgnore);
+        if(empty($iContentId))
+            return $iContentId;
+
+        if($this->_bIsApi && ($mixedAllowViewTo = $this->getCleanValue($CNF['FIELD_OBJECT_PRIVACY_VIEW'])) !== false && ($mixedAllowViewToItems = $this->getCleanValue($CNF['FIELD_OBJECT_PRIVACY_VIEW'] . '_items')) !== false) {
+            $aAllowViewToItems = explode(',', $mixedAllowViewToItems);
+
+            if(($sKey = 'OBJECT_PRIVACY_VIEW') && ($sObject = $CNF[$sKey]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($sObject)) !== false)
+                $oPrivacy->insertGroupCustom([
+                    'profile_id' => $aValsToAdd['object_owner_id'],
+                    'content_id' => 0,
+                    'object' => $sObject,
+                    'group_id' => (int)$mixedAllowViewTo
+                ], $aAllowViewToItems);
+        }
+
+        return $iContentId;
     }
 
     function update($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)

+ 2 - 2
template/scripts/BxBaseFormView.php

@@ -435,7 +435,7 @@ class BxBaseFormView extends BxDolForm
                     }, array_keys($aInput['values']));
                 }
 
-                if(isset($aInput['name']) && $aInput['name'] == 'allow_view_to') {
+                if(isset($aInput['name']) && in_array($aInput['name'], ['allow_view_to', 'object_privacy_view'])) {
                     $aParams = bx_get('params');
                     if(empty($aParams) || !is_array($aParams))
                         $aParams = [];
@@ -460,7 +460,7 @@ class BxBaseFormView extends BxDolForm
                                 if(!empty($aIds) && is_array($aIds)) {
                                     $aInput['values_relations'] = [];
                                     foreach($aIds as $iId)
-                                        $aInput['values_friends'][] = ['key' => $iId, 'value' => BxDolProfile::getData($iId)];
+                                        $aInput['values_relations'][] = ['key' => $iId, 'value' => BxDolProfile::getData($iId)];
                                 }
                             }