]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Contact.php
Some more API functions moved
[friendica.git] / src / Module / Contact.php
index 3b253b85c158f451c825d8a2148cb3161e6e5906..4f13b284334d10d7d8032fd6cebb81e86d9ec512 100644 (file)
@@ -69,7 +69,8 @@ class Contact extends BaseModule
                $count_actions = 0;
                foreach ($orig_records as $orig_record) {
                        $cdata = Model\Contact::getPublicAndUserContactID($orig_record['id'], local_user());
-                       if (empty($cdata)) {
+                       if (empty($cdata) || public_contact() === $cdata['public']) {
+                               // No action available on your own contact
                                continue;
                        }
 
@@ -79,7 +80,7 @@ class Contact extends BaseModule
                        }
 
                        if (!empty($_POST['contacts_batch_block'])) {
-                               self::toggleBlockContact($cdata['public']);
+                               self::toggleBlockContact($cdata['public'], local_user());
                                $count_actions++;
                        }
 
@@ -204,12 +205,13 @@ class Contact extends BaseModule
         * Toggles the blocked status of a contact identified by id.
         *
         * @param int $contact_id Id of the contact with uid = 0
+        * @param int $owner_id   Id of the user we want to block the contact for
         * @throws \Exception
         */
-       private static function toggleBlockContact(int $contact_id)
+       private static function toggleBlockContact(int $contact_id, int $owner_id)
        {
-               $blocked = !Model\Contact\User::isBlocked($contact_id, local_user());
-               Model\Contact\User::setBlocked($contact_id, local_user(), $blocked);
+               $blocked = !Model\Contact\User::isBlocked($contact_id, $owner_id);
+               Model\Contact\User::setBlocked($contact_id, $owner_id, $blocked);
        }
 
        /**
@@ -232,10 +234,10 @@ class Contact extends BaseModule
 
                $a = DI::app();
 
-               $search = Strings::escapeTags(trim($_GET['search'] ?? ''));
-               $nets   = Strings::escapeTags(trim($_GET['nets']   ?? ''));
-               $rel    = Strings::escapeTags(trim($_GET['rel']    ?? ''));
-               $group  = Strings::escapeTags(trim($_GET['group']  ?? ''));
+               $search = trim($_GET['search'] ?? '');
+               $nets   = trim($_GET['nets']   ?? '');
+               $rel    = trim($_GET['rel']    ?? '');
+               $group  = trim($_GET['group']  ?? '');
 
                $accounttype = $_GET['accounttype'] ?? '';
                $accounttypeid = User::getAccountTypeByString($accounttype);
@@ -358,13 +360,13 @@ class Contact extends BaseModule
 
                        if ($cmd === 'update' && $cdata['user']) {
                                self::updateContactFromPoll($cdata['user']);
-                               DI::baseUrl()->redirect('contact/' . $cdata['public']);
+                               DI::baseUrl()->redirect('contact/' . $contact_id);
                                // NOTREACHED
                        }
 
                        if ($cmd === 'updateprofile' && $cdata['user']) {
                                self::updateContactFromProbe($cdata['user']);
-                               DI::baseUrl()->redirect('contact/' . $cdata['public']);
+                               DI::baseUrl()->redirect('contact/' . $contact_id);
                                // NOTREACHED
                        }
 
@@ -373,12 +375,12 @@ class Contact extends BaseModule
                                        throw new BadRequestException(DI::l10n()->t('You can\'t block yourself'));
                                }
 
-                               self::toggleBlockContact($cdata['public']);
+                               self::toggleBlockContact($cdata['public'], local_user());
 
                                $blocked = Model\Contact\User::isBlocked($contact_id, local_user());
                                info(($blocked ? DI::l10n()->t('Contact has been blocked') : DI::l10n()->t('Contact has been unblocked')));
 
-                               DI::baseUrl()->redirect('contact/' . $cdata['public']);
+                               DI::baseUrl()->redirect('contact/' . $contact_id);
                                // NOTREACHED
                        }
 
@@ -392,7 +394,7 @@ class Contact extends BaseModule
                                $ignored = Model\Contact\User::isIgnored($cdata['public'], local_user());
                                info(($ignored ? DI::l10n()->t('Contact has been ignored') : DI::l10n()->t('Contact has been unignored')));
 
-                               DI::baseUrl()->redirect('contact/' . $cdata['public']);
+                               DI::baseUrl()->redirect('contact/' . $contact_id);
                                // NOTREACHED
                        }
                }
@@ -521,7 +523,7 @@ class Contact extends BaseModule
                                '$submit'         => DI::l10n()->t('Submit'),
                                '$lbl_info1'      => $lbl_info1,
                                '$lbl_info2'      => DI::l10n()->t('Their personal note'),
-                               '$reason'         => trim(Strings::escapeTags($contact['reason'])),
+                               '$reason'         => trim($contact['reason']),
                                '$infedit'        => DI::l10n()->t('Edit contact notes'),
                                '$common_link'    => 'contact/' . $contact['id'] . '/contacts/common',
                                '$relation_text'  => $relation_text,
@@ -662,42 +664,25 @@ class Contact extends BaseModule
                }
 
                if ($group) {
-                       $sql_extra = " AND EXISTS(SELECT `id` FROM `group_member` WHERE `gid` = ? AND `contact`.`id` = `contact-id`)";
+                       $sql_extra .= " AND EXISTS(SELECT `id` FROM `group_member` WHERE `gid` = ? AND `contact`.`id` = `contact-id`)";
                        $sql_values[] = $group;
                }
 
-               $total = 0;
-               $stmt = DBA::p("SELECT COUNT(*) AS `total`
-                       FROM `contact`
-                       WHERE `uid` = ?
-                       AND `self` = 0
-                       AND NOT `deleted`
-                       $sql_extra
-                       " . Widget::unavailableNetworks(),
-                       $sql_values
-               );
-               if (DBA::isResult($stmt)) {
-                       $total = DBA::fetch($stmt)['total'];
-               }
-               DBA::close($stmt);
+               $networks = Widget::unavailableNetworks();
+               $sql_extra .= " AND NOT `network` IN (" . substr(str_repeat("?, ", count($networks)), 0, -2) . ")";
+               $sql_values = array_merge($sql_values, $networks);
 
-               $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
+               $condition = ["`uid` = ? AND NOT `self` AND NOT `deleted`" . $sql_extra];
+               $condition = array_merge($condition, $sql_values);
 
-               $sql_values[] = $pager->getStart();
-               $sql_values[] = $pager->getItemsPerPage();
+               $total = DBA::count('contact', $condition);
+
+               $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
 
                $contacts = [];
 
-               $stmt = DBA::p("SELECT *
-                       FROM `contact`
-                       WHERE `uid` = ?
-                       AND `self` = 0
-                       AND NOT `deleted`
-                       $sql_extra
-                       ORDER BY `name` ASC
-                       LIMIT ?, ?",
-                       $sql_values
-               );
+               $stmt = DBA::select('contact', [], $condition, ['order' => ['name'], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]]);
+
                while ($contact = DBA::fetch($stmt)) {
                        $contact['blocked'] = Model\Contact\User::isBlocked($contact['id'], local_user());
                        $contact['readonly'] = Model\Contact\User::isIgnored($contact['id'], local_user());