]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Group.php
Merge pull request #8063 from MrPetovan/bug/8058-use-default-user-acl-events
[friendica.git] / src / Model / Group.php
index a9d2f462e653dfc3e07be5c54ecf973f8f8c978c..b87458756a94e14d487a593704a78a4034abcb6b 100644 (file)
@@ -2,10 +2,10 @@
 /**
  * @file src/Model/Group.php
  */
+
 namespace Friendica\Model;
 
 use Friendica\BaseModule;
-use Friendica\BaseObject;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
@@ -15,24 +15,20 @@ use Friendica\Database\DBA;
 /**
  * @brief functions for interacting with the group database table
  */
-class Group extends BaseObject
+class Group
 {
        const FOLLOWERS = '~';
        const MUTUALS = '&';
 
        public static function getByUserId($uid, $includesDeleted = false)
        {
-               $DB = self::getApp()->getDatabase();
-
                $conditions = ['uid' => $uid];
 
                if (!$includesDeleted) {
                        $conditions['deleted'] = false;
                }
 
-               $groupsStmt = $DB->select('group', [], $conditions);
-
-               return $DB->toArray($groupsStmt);
+               return DBA::selectToArray('group', [], $conditions);
        }
 
        /**
@@ -93,8 +89,8 @@ class Group extends BaseObject
        /**
         * Update group information.
         *
-        * @param  int    $id   Group ID
-        * @param  string $name Group name
+        * @param int    $id   Group ID
+        * @param string $name Group name
         *
         * @return bool Was the update successful?
         * @throws \Exception
@@ -113,14 +109,13 @@ class Group extends BaseObject
         */
        public static function getIdsByContactId($cid)
        {
-               $condition = ['contact-id' => $cid];
-               $stmt = DBA::select('group_member', ['gid'], $condition);
-
                $return = [];
 
+               $stmt = DBA::select('group_member', ['gid'], ['contact-id' => $cid]);
                while ($group = DBA::fetch($stmt)) {
                        $return[] = $group['gid'];
                }
+               DBA::close($stmt);
 
                return $return;
        }
@@ -187,8 +182,9 @@ class Group extends BaseObject
         * @return boolean
         * @throws \Exception
         */
-       public static function remove($gid) {
-               if (! $gid) {
+       public static function remove($gid)
+       {
+               if (!$gid) {
                        return false;
                }
 
@@ -232,14 +228,15 @@ class Group extends BaseObject
        /**
         * @brief      Mark a group as deleted based on its name
         *
-        * @deprecated Use Group::remove instead
-        *
         * @param int    $uid
         * @param string $name
         * @return bool
         * @throws \Exception
+        * @deprecated Use Group::remove instead
+        *
         */
-       public static function removeByName($uid, $name) {
+       public static function removeByName($uid, $name)
+       {
                $return = false;
                if (!empty($uid) && !empty($name)) {
                        $gid = self::getIdByName($uid, $name);
@@ -297,13 +294,13 @@ class Group extends BaseObject
        /**
         * @brief      Removes a contact from a group based on its name
         *
-        * @deprecated Use Group::removeMember instead
-        *
         * @param int    $uid
         * @param string $name
         * @param int    $cid
         * @return boolean
         * @throws \Exception
+        * @deprecated Use Group::removeMember instead
+        *
         */
        public static function removeMemberByName($uid, $name, $cid)
        {
@@ -330,16 +327,31 @@ class Group extends BaseObject
                }
 
                $return = [];
+               $pubmail = false;
+               $networks = Protocol::SUPPORT_PRIVATE;
+
+               $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $uid]);
+               if (DBA::isResult($mailacct)) {
+                       $pubmail = $mailacct['pubmail'];
+               }
+
+               if (!$pubmail) {
+                       $networks = array_diff($networks, [Protocol::MAIL]);
+               }
 
                $key = array_search(self::FOLLOWERS, $group_ids);
                if ($key !== false) {
-                       $followersStmt = Contact::select(['id'], [
+                       $followers = Contact::selectToArray(['id'], [
                                'uid' => $uid,
                                'rel' => [Contact::FOLLOWER, Contact::FRIEND],
-                               'protocol' => Protocol::NATIVE_SUPPORT,
+                               'network' => $networks,
+                               'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
+                               'archive' => false,
+                               'pending' => false,
+                               'blocked' => false,
                        ]);
 
-                       while($follower = DBA::fetch($followersStmt)) {
+                       foreach ($followers as $follower) {
                                $return[] = $follower['id'];
                        }
 
@@ -348,13 +360,17 @@ class Group extends BaseObject
 
                $key = array_search(self::MUTUALS, $group_ids);
                if ($key !== false) {
-                       $mutualsStmt = Contact::select(['id'], [
+                       $mutuals = Contact::selectToArray(['id'], [
                                'uid' => $uid,
                                'rel' => [Contact::FRIEND],
-                               'protocol' => Protocol::NATIVE_SUPPORT,
+                               'network' => $networks,
+                               'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
+                               'archive' => false,
+                               'pending' => false,
+                               'blocked' => false,
                        ]);
 
-                       while($mutual = DBA::fetch($mutualsStmt)) {
+                       foreach ($mutuals as $mutual) {
                                $return[] = $mutual['id'];
                        }
 
@@ -362,12 +378,13 @@ class Group extends BaseObject
                }
 
                $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
-               while($group_member = DBA::fetch($stmt)) {
+               while ($group_member = DBA::fetch($stmt)) {
                        $return[] = $group_member['contact-id'];
                }
+               DBA::close($stmt);
 
                if ($check_dead) {
-                       Contact::pruneUnavailable($return);
+                       $return = Contact::pruneUnavailable($return);
                }
 
                return $return;
@@ -384,8 +401,6 @@ class Group extends BaseObject
         */
        public static function displayGroupSelection($uid, $gid = 0, $label = '')
        {
-               $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
-
                $display_groups = [
                        [
                                'name' => '',
@@ -393,6 +408,8 @@ class Group extends BaseObject
                                'selected' => ''
                        ]
                ];
+
+               $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
                while ($group = DBA::fetch($stmt)) {
                        $display_groups[] = [
                                'name' => $group['name'],
@@ -400,7 +417,9 @@ class Group extends BaseObject
                                'selected' => $gid == $group['id'] ? 'true' : ''
                        ];
                }
-               Logger::log('groups: ' . print_r($display_groups, true));
+               DBA::close($stmt);
+
+               Logger::info('Got groups', $display_groups);
 
                if ($label == '') {
                        $label = L10n::t('Default privacy group for new contacts');
@@ -442,13 +461,12 @@ class Group extends BaseObject
                        ]
                ];
 
-               $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
-
                $member_of = [];
                if ($cid) {
                        $member_of = self::getIdsByContactId($cid);
                }
 
+               $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
                while ($group = DBA::fetch($stmt)) {
                        $selected = (($group_id == $group['id']) ? ' group-selected' : '');
 
@@ -471,6 +489,7 @@ class Group extends BaseObject
                                'ismember' => in_array($group['id'], $member_of),
                        ];
                }
+               DBA::close($stmt);
 
                // Don't show the groups on the network page when there is only one
                if ((count($display_groups) <= 2) && ($each == 'network')) {
@@ -493,7 +512,6 @@ class Group extends BaseObject
                        '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
                ]);
 
-
                return $o;
        }
 }