]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Group.php
Misc cleanups (#5417)
[friendica.git] / src / Model / Group.php
index 857b972d62b4c81c4f1d02ff64e54b689f0355a8..79604e2ca0b7e2948c071ca833c1993507da52bd 100644 (file)
@@ -1,16 +1,16 @@
 <?php
-
 /**
  * @file src/Model/Group.php
  */
-
 namespace Friendica\Model;
 
 use Friendica\BaseObject;
+use Friendica\Core\L10n;
 use Friendica\Database\DBM;
 use dba;
 
 require_once 'boot.php';
+require_once 'include/dba.php';
 require_once 'include/text.php';
 
 /**
@@ -38,31 +38,44 @@ class Group extends BaseObject
                                // all the old members are gone, but the group remains so we don't break any security
                                // access lists. What we're doing here is reviving the dead group, but old content which
                                // was restricted to this group may now be seen by the new group members.
-                               $group = dba::select('group', ['deleted'], ['id' => $gid], ['limit' => 1]);
+                               $group = dba::selectFirst('group', ['deleted'], ['id' => $gid]);
                                if (DBM::is_result($group) && $group['deleted']) {
-                                       dba::update('group', ['deleted' => 0], ['gid' => $gid]);
-                                       notice(t('A deleted group with this name was revived. Existing item permissions <strong>may</strong> apply to this group and any future members. If this is not what you intended, please create another group with a different name.') . EOL);
+                                       dba::update('group', ['deleted' => 0], ['id' => $gid]);
+                                       notice(L10n::t('A deleted group with this name was revived. Existing item permissions <strong>may</strong> apply to this group and any future members. If this is not what you intended, please create another group with a different name.') . EOL);
                                }
                                return true;
                        }
 
                        $return = dba::insert('group', ['uid' => $uid, 'name' => $name]);
+                       if ($return) {
+                               $return = dba::lastInsertId();
+                       }
                }
                return $return;
        }
 
        /**
-        * @brief Get a list of group ids a contact belongs to
+        * Update group information.
         *
-        * @todo Get rid of $uid, the contact id already bears the information
+        * @param  int    $id   Group ID
+        * @param  string $name Group name
+        *
+        * @return bool Was the update successful?
+        */
+       public static function update($id, $name)
+       {
+               return dba::update('group', ['name' => $name], ['id' => $id]);
+       }
+
+       /**
+        * @brief Get a list of group ids a contact belongs to
         *
-        * @param int $uid
         * @param int $cid
         * @return array
         */
-       private static function getByContactIdForUserId($uid, $cid)
+       public static function getIdsByContactId($cid)
        {
-               $condition = ['uid' => $uid, 'contact-id' => $cid];
+               $condition = ['contact-id' => $cid];
                $stmt = dba::select('group_member', ['gid'], $condition);
 
                $return = [];
@@ -93,13 +106,11 @@ class Group extends BaseObject
                                        AND `contact-id` IN
                                                (SELECT `contact-id`
                                                FROM `group_member`
-                                               WHERE `group_member`.`gid` = `group`.`id`
-                                               AND `group_member`.`uid` = ?)
+                                               WHERE `group_member`.`gid` = `group`.`id`)
                                        ) AS `count`
                                FROM `group`
                                WHERE `group`.`uid` = ?;",
                        local_user(),
-                       local_user(),
                        local_user()
                );
 
@@ -121,7 +132,7 @@ class Group extends BaseObject
                        return false;
                }
 
-               $group = dba::select('group', ['id'], ['uid' => $uid, 'name' => $name], ['limit' => 1]);
+               $group = dba::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
                if (DBM::is_result($group)) {
                        return $group['id'];
                }
@@ -132,7 +143,7 @@ class Group extends BaseObject
        /**
         * @brief Mark a group as deleted
         *
-        * @param type $gid
+        * @param int $gid
         * @return boolean
         */
        public static function remove($gid) {
@@ -140,8 +151,13 @@ class Group extends BaseObject
                        return false;
                }
 
+               $group = dba::selectFirst('group', ['uid'], ['id' => $gid]);
+               if (!DBM::is_result($group)) {
+                       return false;
+               }
+
                // remove group from default posting lists
-               $user = dba::select('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $uid], ['limit' => 1]);
+               $user = dba::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
                if (DBM::is_result($user)) {
                        $change = false;
 
@@ -159,7 +175,7 @@ class Group extends BaseObject
                        }
 
                        if ($change) {
-                               dba::update('user', $user, ['uid' => $uid]);
+                               dba::update('user', $user, ['uid' => $group['uid']]);
                        }
                }
 
@@ -177,9 +193,9 @@ class Group extends BaseObject
         *
         * @deprecated Use Group::remove instead
         *
-        * @param type $uid
-        * @param type $name
-        * @return type
+        * @param int $uid
+        * @param string $name
+        * @return bool
         */
        public static function removeByName($uid, $name) {
                $return = false;
@@ -286,8 +302,7 @@ class Group extends BaseObject
                }
 
                if ($check_dead && !$use_gcontact) {
-                       require_once 'include/acl_selectors.php';
-                       $return = prune_deadguys($return);
+                       Contact::pruneUnavailable($return);
                }
                return $return;
        }
@@ -323,13 +338,13 @@ class Group extends BaseObject
                logger('groups: ' . print_r($display_groups, true));
 
                if ($label == '') {
-                       $label = t('Default privacy group for new contacts');
+                       $label = L10n::t('Default privacy group for new contacts');
                }
 
-               $o = replace_macros(get_markup_template('group_selection.tpl'), array(
+               $o = replace_macros(get_markup_template('group_selection.tpl'), [
                        '$label' => $label,
                        '$groups' => $display_groups
-               ));
+               ]);
                return $o;
        }
 
@@ -346,7 +361,7 @@ class Group extends BaseObject
         * @param int $cid
         * @return string
         */
-       public static function sidebarWidget($every = 'contacts', $each = 'group', $editmode = 'standard', $group_id = 0, $cid = 0)
+       public static function sidebarWidget($every = 'contacts', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
        {
                $o = '';
 
@@ -356,18 +371,18 @@ class Group extends BaseObject
 
                $display_groups = [
                        [
-                               'text' => t('Everybody'),
+                               'text' => L10n::t('Everybody'),
                                'id' => 0,
-                               'selected' => (($group_id == 0) ? 'group-selected' : ''),
+                               'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
                                'href' => $every,
                        ]
                ];
 
                $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
 
-               $member_of = array();
+               $member_of = [];
                if ($cid) {
-                       $member_of = self::getByContactIdForUserId(local_user(), $cid);
+                       $member_of = self::getIdsByContactId($cid);
                }
 
                while ($group = dba::fetch($stmt)) {
@@ -376,7 +391,7 @@ class Group extends BaseObject
                        if ($editmode == 'full') {
                                $groupedit = [
                                        'href' => 'group/' . $group['id'],
-                                       'title' => t('edit'),
+                                       'title' => L10n::t('edit'),
                                ];
                        } else {
                                $groupedit = null;
@@ -395,16 +410,17 @@ class Group extends BaseObject
 
                $tpl = get_markup_template('group_side.tpl');
                $o = replace_macros($tpl, [
-                       '$add' => t('add'),
-                       '$title' => t('Groups'),
+                       '$add' => L10n::t('add'),
+                       '$title' => L10n::t('Groups'),
                        '$groups' => $display_groups,
                        'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
                        'grouppage' => 'group/',
-                       '$edittext' => t('Edit group'),
-                       '$ungrouped' => $every === 'contacts' ? t('Contacts not in any group') : '',
-                       '$createtext' => t('Create a new group'),
-                       '$creategroup' => t('Group Name: '),
-                       '$editgroupstext' => t('Edit groups'),
+                       '$edittext' => L10n::t('Edit group'),
+                       '$ungrouped' => $every === 'contacts' ? L10n::t('Contacts not in any group') : '',
+                       '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
+                       '$createtext' => L10n::t('Create a new group'),
+                       '$creategroup' => L10n::t('Group Name: '),
+                       '$editgroupstext' => L10n::t('Edit groups'),
                        '$form_security_token' => get_form_security_token('group_edit'),
                ]);