]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/NotificationsManager.php
Replace "q" calls
[friendica.git] / src / Core / NotificationsManager.php
index d582f21596688c16145ee28ee9cc3ec5c2a79de6..be6fae6cb36e76caed543256d3bb1cec8163a6af 100644 (file)
@@ -55,47 +55,25 @@ class NotificationsManager extends BaseObject
         * @brief Get all notifications for local_user()
         *
         * @param array  $filter optional Array "column name"=>value: filter query by columns values
-        * @param string $order  optional Space separated list of column to sort by.
-        *                       Prepend name with "+" to sort ASC, "-" to sort DESC. Default to "-date"
+        * @param array  $order  optional Array to order by
         * @param string $limit  optional Query limits
         *
-        * @return array of results or false on errors
+        * @return array|bool of results or false on errors
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function getAll($filter = [], $order = "-date", $limit = "")
+       public function getAll($filter = [], $order = ['date' => 'DESC'], $limit = "")
        {
-               $filter_str = [];
-               $filter_sql = "";
-               foreach ($filter as $column => $value) {
-                       $filter_str[] = sprintf("`%s` = '%s'", $column, DBA::escape($value));
-               }
-               if (count($filter_str) > 0) {
-                       $filter_sql = "AND " . implode(" AND ", $filter_str);
-               }
+               $params = [];
 
-               $aOrder = explode(" ", $order);
-               $asOrder = [];
-               foreach ($aOrder as $o) {
-                       $dir = "asc";
-                       if ($o[0] === "-") {
-                               $dir = "desc";
-                               $o = substr($o, 1);
-                       }
-                       if ($o[0] === "+") {
-                               $dir = "asc";
-                               $o = substr($o, 1);
-                       }
-                       $asOrder[] = "$o $dir";
-               }
-               $order_sql = implode(", ", $asOrder);
+               $params['order'] = $order;
 
-               if ($limit != "") {
-                       $limit = " LIMIT " . $limit;
+               if (!empty($limit)) {
+                       $params['limit'] = $limit;
                }
-               $r = q(
-                       "SELECT * FROM `notify` WHERE `uid` = %d $filter_sql ORDER BY $order_sql $limit",
-                       intval(local_user())
-               );
+
+               $dbFilter = array_merge($filter, ['uid' => local_user()]);
+
+               $r = DBA::select('notify', [], $dbFilter, $order, $params);
 
                if (DBA::isResult($r)) {
                        return $this->_set_extra($r);
@@ -113,11 +91,7 @@ class NotificationsManager extends BaseObject
         */
        public function getByID($id)
        {
-               $r = q(
-                       "SELECT * FROM `notify` WHERE `id` = %d AND `uid` = %d LIMIT 1",
-                       intval($id),
-                       intval(local_user())
-               );
+               $r = DBA::selectFirst('notify', ['id' => $id, 'uid' => local_user()]);
                if (DBA::isResult($r)) {
                        return $this->_set_extra($r)[0];
                }
@@ -134,14 +108,7 @@ class NotificationsManager extends BaseObject
         */
        public function setSeen($note, $seen = true)
        {
-               return q(
-                       "UPDATE `notify` SET `seen` = %d WHERE (`link` = '%s' OR (`parent` != 0 AND `parent` = %d AND `otype` = '%s')) AND `uid` = %d",
-                       intval($seen),
-                       DBA::escape($note['link']),
-                       intval($note['parent']),
-                       DBA::escape($note['otype']),
-                       intval(local_user())
-               );
+               return DBA::update('notify', ['seen' => $seen], ['link' => $note['link'], 'parent' => $note['parent'], 'otype' => $note['otype'], 'uid' => local_user()]);
        }
 
        /**
@@ -153,11 +120,7 @@ class NotificationsManager extends BaseObject
         */
        public function setAllSeen($seen = true)
        {
-               return q(
-                       "UPDATE `notify` SET `seen` = %d WHERE `uid` = %d",
-                       intval($seen),
-                       intval(local_user())
-               );
+               return DBA::update('notify', ['seen' => $seen], ['uid' => local_user()]);
        }
 
        /**
@@ -461,19 +424,21 @@ class NotificationsManager extends BaseObject
                $sql_seen = "";
 
                if ($seen === 0) {
-                       $sql_seen = " AND NOT `seen` ";
+                       $filter = ['`uid` = ? AND NOT `seen`', local_user()];
+               } else {
+                       $filter = ['uid' => local_user()];
                }
 
-               $r = q(
-                       "SELECT `id`, `url`, `photo`, `msg`, `date`, `seen`, `verb` FROM `notify`
-                               WHERE `uid` = %d $sql_seen ORDER BY `date` DESC LIMIT %d, %d ",
-                       intval(local_user()),
-                       intval($start),
-                       intval($limit)
-               );
+               $params = [];
+               $params['limit'] = [$start, $limit];
+
+               $r = DBA::select('notify',
+                       ['id', 'url', 'photo', 'msg', 'date', 'seen', 'verb'],
+                       $filter,
+                       $params);
 
                if (DBA::isResult($r)) {
-                       $notifs = $this->formatNotifs($r, $ident);
+                       $notifs = $this->formatNotifs(DBA::toArray($r), $ident);
                }
 
                $arr = [