]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Group.php
Fix docblocks types
[friendica.git] / src / Model / Group.php
index 51b709f5363191b708fac4e51b0f88e7202d4806..410f78b1564216ffb2dbd1ecd599ffccb3970492 100644 (file)
@@ -11,6 +11,7 @@ use Friendica\Database\DBM;
 use dba;
 
 require_once 'boot.php';
+require_once 'include/dba.php';
 require_once 'include/text.php';
 
 /**
@@ -47,6 +48,9 @@ class Group extends BaseObject
                        }
 
                        $return = dba::insert('group', ['uid' => $uid, 'name' => $name]);
+                       if ($return) {
+                               $return = dba::lastInsertId();
+                       }
                }
                return $return;
        }
@@ -54,29 +58,18 @@ class Group extends BaseObject
        /**
         * @brief Get a list of group ids a contact belongs to
         *
-        * @todo Get rid of $uid, the contact id already bears the information
-        *
-        * @param int $uid
         * @param int $cid
         * @return array
         */
-       private static function getByContactIdForUserId($uid, $cid)
+       private static function getIdsByContactId($cid)
        {
-               $stmt = dba::p('SELECT `id`
-                       FROM `group`
-                       INNER JOIN `group_member`
-                               ON `group_member`.`gid` = `group`.`id`
-                       WHERE `group`.`uid` = ?
-                       AND `group_member`.`contact-id` = ?',
-                       $uid,
-                       $cid
-               );
+               $condition = ['contact-id' => $cid];
+               $stmt = dba::select('group_member', ['gid'], $condition);
 
                $return = [];
-               if (DBM::is_result($stmt)) {
-                       while($group = dba::fetch($stmt)) {
-                               $return[] = $group['id'];
-                       }
+
+               while ($group = dba::fetch($stmt)) {
+                       $return[] = $group['gid'];
                }
 
                return $return;
@@ -101,13 +94,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()
                );
 
@@ -125,7 +116,7 @@ class Group extends BaseObject
         */
        public static function getIdByName($uid, $name)
        {
-               if ((! $uid) || (! strlen($name))) {
+               if (!$uid || !strlen($name)) {
                        return false;
                }
 
@@ -140,7 +131,7 @@ class Group extends BaseObject
        /**
         * @brief Mark a group as deleted
         *
-        * @param type $gid
+        * @param int $gid
         * @return boolean
         */
        public static function remove($gid) {
@@ -185,9 +176,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;
@@ -209,7 +200,7 @@ class Group extends BaseObject
         */
        public static function addMember($gid, $cid)
        {
-               if (!($gid && $cid)) {
+               if (!$gid || !$cid) {
                        return false;
                }
 
@@ -233,11 +224,7 @@ class Group extends BaseObject
         */
        public static function removeMember($gid, $cid)
        {
-               if (!$gid) {
-                       return false;
-               }
-
-               if (!($gid && $cid)) {
+               if (!$gid || !$cid) {
                        return false;
                }
 
@@ -275,7 +262,7 @@ class Group extends BaseObject
         */
        public static function expand($group_ids, $check_dead = false, $use_gcontact = false)
        {
-               if (!(is_array($group_ids) && count($group_ids))) {
+               if (!is_array($group_ids) || !count($group_ids)) {
                        return [];
                }
 
@@ -292,11 +279,9 @@ class Group extends BaseObject
                        $stmt = dba::select('group_member', ['contact-id'], $condition_array);
                }
 
-               $return = array();
-               if (DBM::is_result($stmt)) {
-                       while($group_member = dba::fetch($stmt)) {
-                               $return[] = $group_member['contact-id'];
-                       }
+               $return = [];
+               while($group_member = dba::fetch($stmt)) {
+                       $return[] = $group_member['contact-id'];
                }
 
                if ($check_dead && !$use_gcontact) {
@@ -318,7 +303,7 @@ class Group extends BaseObject
        {
                $o = '';
 
-               $groups = dba::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name' => 'ASC']]);
+               $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
 
                $display_groups = [
                        [
@@ -327,7 +312,7 @@ class Group extends BaseObject
                                'selected' => ''
                        ]
                ];
-               foreach ($groups as $group) {
+               while ($group = dba::fetch($stmt)) {
                        $display_groups[] = [
                                'name' => $group['name'],
                                'id' => $group['id'],
@@ -377,36 +362,34 @@ class Group extends BaseObject
                        ]
                ];
 
-               $groups = dba::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name' => 'ASC']]);
+               $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
 
                $member_of = array();
                if ($cid) {
-                       $member_of = self::getByContactIdForUserId(local_user(), $cid);
+                       $member_of = self::getIdsByContactId($cid);
                }
 
-               if (DBM::is_result($groups)) {
-                       foreach ($groups as $group) {
-                               $selected = (($group_id == $group['id']) ? ' group-selected' : '');
-
-                               if ($editmode == 'full') {
-                                       $groupedit = [
-                                               'href' => 'group/' . $group['id'],
-                                               'title' => t('edit'),
-                                       ];
-                               } else {
-                                       $groupedit = null;
-                               }
+               while ($group = dba::fetch($stmt)) {
+                       $selected = (($group_id == $group['id']) ? ' group-selected' : '');
 
-                               $display_groups[] = [
-                                       'id'   => $group['id'],
-                                       'cid'  => $cid,
-                                       'text' => $group['name'],
-                                       'href' => $each . '/' . $group['id'],
-                                       'edit' => $groupedit,
-                                       'selected' => $selected,
-                                       'ismember' => in_array($group['id'], $member_of),
+                       if ($editmode == 'full') {
+                               $groupedit = [
+                                       'href' => 'group/' . $group['id'],
+                                       'title' => t('edit'),
                                ];
+                       } else {
+                               $groupedit = null;
                        }
+
+                       $display_groups[] = [
+                               'id'   => $group['id'],
+                               'cid'  => $cid,
+                               'text' => $group['name'],
+                               'href' => $each . '/' . $group['id'],
+                               'edit' => $groupedit,
+                               'selected' => $selected,
+                               'ismember' => in_array($group['id'], $member_of),
+                       ];
                }
 
                $tpl = get_markup_template('group_side.tpl');