]> git.mxchange.org Git - friendica.git/commitdiff
Replace q() call with Profile::get() method
authorPhilipp Holzer <admin+github@philipp.info>
Fri, 26 Jul 2019 13:53:57 +0000 (15:53 +0200)
committerPhilipp Holzer <admin+github@philipp.info>
Sat, 27 Jul 2019 21:54:12 +0000 (23:54 +0200)
include/api.php
src/Model/Profile.php

index 23afa72de9c3c2edbdd0a812ac86089f133753cc..ee788775e53ea9698107e8e017afbd5ddc1a232f 100644 (file)
@@ -27,6 +27,7 @@ use Friendica\Model\Group;
 use Friendica\Model\Item;
 use Friendica\Model\Mail;
 use Friendica\Model\Photo;
+use Friendica\Model\Profile;
 use Friendica\Model\User;
 use Friendica\Network\FKOAuth1;
 use Friendica\Network\HTTPException;
@@ -6205,47 +6206,39 @@ function api_friendica_profile_show($type)
 
        // get data of the specified profile id or all profiles of the user if not specified
        if ($profile_id != 0) {
-               $r = q(
-                       "SELECT * FROM `profile` WHERE `uid` = %d AND `id` = %d",
-                       intval(api_user()),
-                       intval($profile_id)
-               );
-
+               $r = Profile::get(api_user(), $profile_id);
                // error message if specified gid is not in database
                if (!DBA::isResult($r)) {
                        throw new BadRequestException("profile_id not available");
                }
        } else {
-               $r = q(
-                       "SELECT * FROM `profile` WHERE `uid` = %d",
-                       intval(api_user())
-               );
+               $r = Profile::get(api_user());
        }
        // loop through all returned profiles and retrieve data and users
        $k = 0;
        $profiles = [];
-       foreach ($r as $rr) {
-               $profile = api_format_items_profiles($rr);
+       if (DBA::isResult($r)) {
+               foreach ($r as $rr) {
+                       $profile = api_format_items_profiles($rr);
 
-               // select all users from contact table, loop and prepare standard return for user data
-               $users = [];
-               $nurls = q(
-                       "SELECT `id`, `nurl` FROM `contact` WHERE `uid`= %d AND `profile-id` = %d",
-                       intval(api_user()),
-                       intval($rr['id'])
-               );
+                       // select all users from contact table, loop and prepare standard return for user data
+                       $users = [];
+                       $nurls = Contact::select(['id', 'nurl'], ['uid' => api_user(), 'profile-id' => $rr['id']]);
 
-               foreach ($nurls as $nurl) {
-                       $user = api_get_user($a, $nurl['nurl']);
-                       ($type == "xml") ? $users[$k++ . ":user"] = $user : $users[] = $user;
-               }
-               $profile['users'] = $users;
+                       if (DBA::isResult($nurls)) {
+                               foreach ($nurls as $nurl) {
+                                       $user = api_get_user($a, $nurl['nurl']);
+                                       ($type == "xml") ? $users[$k++ . ":user"] = $user : $users[] = $user;
+                               }
+                       }
+                       $profile['users'] = $users;
 
-               // add prepared profile data to array for final return
-               if ($type == "xml") {
-                       $profiles[$k++ . ":profile"] = $profile;
-               } else {
-                       $profiles[] = $profile;
+                       // add prepared profile data to array for final return
+                       if ($type == "xml") {
+                               $profiles[$k++ . ":profile"] = $profile;
+                       } else {
+                               $profiles[] = $profile;
+                       }
                }
        }
 
@@ -6275,15 +6268,17 @@ function api_saved_searches_list($type)
        $terms = DBA::select('search', ['id', 'term'], ['uid' => local_user()]);
 
        $result = [];
-       while ($term = $terms->fetch()) {
-               $result[] = [
-                       'created_at' => api_date(time()),
-                       'id' => intval($term['id']),
-                       'id_str' => $term['id'],
-                       'name' => $term['term'],
-                       'position' => null,
-                       'query' => $term['term']
-               ];
+       if (DBA::isResult($terms)) {
+               while ($term = $terms->fetch()) {
+                       $result[] = [
+                               'created_at' => api_date(time()),
+                               'id'         => intval($term['id']),
+                               'id_str'     => $term['id'],
+                               'name'       => $term['term'],
+                               'position'   => null,
+                               'query'      => $term['term']
+                       ];
+               }
        }
 
        DBA::close($terms);
index 5a2adea9dba85cf43dcb8c8bcc1d23169199f35c..8038392b1405c116d5de75408184262147288dab 100644 (file)
@@ -45,6 +45,25 @@ class Profile
                return $profile;
        }
 
+       /**
+        * @brief Returns the profile based on a ID
+        *
+        * @param int $uid The User ID
+        * @param int $id The id of the profile (optional)
+        * @param array $fields The fields to retrieve
+        *
+        * @return array Array of profile data
+        * @throws \Exception
+        */
+       public static function get(int $uid, int $id = null, array $fields = [])
+       {
+               if (empty($id)) {
+                       return DBA::select('profile', $fields, ['uid' => $uid]);
+               } else {
+                       return DBA::select('profile', $fields, ['uid' => $uid, 'id' => $id]);
+               }
+       }
+
        /**
         * @brief Returns a formatted location string from the given profile array
         *