]> git.mxchange.org Git - friendica-addons.git/blobdiff - windowsphonepush/windowsphonepush.php
Merge pull request #1095 from tobiasd/20210406-lng
[friendica-addons.git] / windowsphonepush / windowsphonepush.php
index 3e574586ab2bf1b943903991ed9078d60fd57df5..bf160f0a4b1fcb949e0641cc482b6222202faf59 100644 (file)
@@ -30,12 +30,11 @@ use Friendica\App;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
 use Friendica\Core\Hook;
-use Friendica\Core\L10n;
 use Friendica\Core\Logger;
-use Friendica\Core\PConfig;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Item;
+use Friendica\Model\Post;
 use Friendica\Model\User;
 
 function windowsphonepush_install()
@@ -57,18 +56,6 @@ function windowsphonepush_install()
        Logger::log("installed windowsphonepush");
 }
 
-function windowsphonepush_uninstall()
-{
-       /* uninstall unregisters any hooks created with register_hook
-        * during install. Don't delete data in table `pconfig`.
-        */
-       Hook::unregister('cron', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_cron');
-       Hook::unregister('addon_settings', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings');
-       Hook::unregister('addon_settings_post', 'addon/windowsphonepush/windowsphonepush.php', 'windowsphonepush_settings_post');
-
-       Logger::log("removed windowsphonepush");
-}
-
 /* declare the windowsphonepush function so that /windowsphonepush url requests will land here */
 function windowsphonepush_module()
 {
@@ -86,15 +73,13 @@ function windowsphonepush_settings_post($a, $post)
                return;
        }
        $enable = intval($_POST['windowsphonepush']);
-       PConfig::set(local_user(), 'windowsphonepush', 'enable', $enable);
+       DI::pConfig()->set(local_user(), 'windowsphonepush', 'enable', $enable);
 
        if ($enable) {
-               PConfig::set(local_user(), 'windowsphonepush', 'counterunseen', 0);
+               DI::pConfig()->set(local_user(), 'windowsphonepush', 'counterunseen', 0);
        }
 
-       PConfig::set(local_user(), 'windowsphonepush', 'senditemtext', intval($_POST['windowsphonepush-senditemtext']));
-
-       info(L10n::t('WindowsPhonePush settings updated.') . EOL);
+       DI::pConfig()->set(local_user(), 'windowsphonepush', 'senditemtext', intval($_POST['windowsphonepush-senditemtext']));
 }
 
 /* Called from the Addon Setting form.
@@ -120,20 +105,20 @@ function windowsphonepush_settings(&$a, &$s)
 
        /* Add some HTML to the existing form */
        $s .= '<div class="settings-block">';
-       $s .= '<h3>' . L10n::t('WindowsPhonePush Settings') . '</h3>';
+       $s .= '<h3>' . DI::l10n()->t('WindowsPhonePush Settings') . '</h3>';
 
        $s .= '<div id="windowsphonepush-enable-wrapper">';
-       $s .= '<label id="windowsphonepush-enable-label" for="windowsphonepush-enable-chk">' . L10n::t('Enable WindowsPhonePush Addon') . '</label>';
+       $s .= '<label id="windowsphonepush-enable-label" for="windowsphonepush-enable-chk">' . DI::l10n()->t('Enable WindowsPhonePush Addon') . '</label>';
        $s .= '<input id="windowsphonepush-enable-chk" type="checkbox" name="windowsphonepush" value="1" ' . $checked_enabled . '/>';
        $s .= '</div><div class="clear"></div>';
 
        $s .= '<div id="windowsphonepush-senditemtext-wrapper">';
-       $s .= '<label id="windowsphonepush-senditemtext-label" for="windowsphonepush-senditemtext-chk">' . L10n::t('Push text of new item') . '</label>';
+       $s .= '<label id="windowsphonepush-senditemtext-label" for="windowsphonepush-senditemtext-chk">' . DI::l10n()->t('Push text of new item') . '</label>';
        $s .= '<input id="windowsphonepush-senditemtext-chk" type="checkbox" name="windowsphonepush-senditemtext" value="1" ' . $checked_senditemtext . '/>';
        $s .= '</div><div class="clear"></div>';
 
        /* provide a submit button - enable und senditemtext can be changed by the user */
-       $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="windowsphonepush-submit" name="windowsphonepush-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div><div class="clear"></div>';
+       $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="windowsphonepush-submit" name="windowsphonepush-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div><div class="clear"></div>';
 
        /* provide further read-only information concerning the addon (useful for */
        $s .= '<div id="windowsphonepush-device_url-wrapper">';
@@ -165,7 +150,7 @@ function windowsphonepush_cron()
                        } else {
                                // retrieve the number of unseen items and the id of the latest one (if there are more than
                                // one new entries since last poller run, only the latest one will be pushed)
-                               $count = q("SELECT count(`id`) as count, max(`id`) as max FROM `item` WHERE `unseen` = 1 AND `type` <> 'activity' AND `uid` = %d", intval($rr['uid']));
+                               $count = q("SELECT count(`id`) as count, max(`id`) as max FROM `post-view` WHERE `unseen` = 1 AND `type` <> 'activity' AND `uid` = %d", intval($rr['uid']));
 
                                // send number of unseen items to the device (the number will be displayed on Start screen until
                                // App will be started by user) - this update will be sent every 10 minutes to update the number to 0 if
@@ -174,7 +159,7 @@ function windowsphonepush_cron()
                                switch (trim($res_tile)) {
                                        case "Received":
                                                // ok, count has been pushed, let's save it in personal settings
-                                               PConfig::set($rr['uid'], 'windowsphonepush', 'counterunseen', $count[0]['count']);
+                                               DI::pConfig()->set($rr['uid'], 'windowsphonepush', 'counterunseen', $count[0]['count']);
                                                break;
                                        case "QueueFull":
                                                // maximum of 30 messages reached, server rejects any further push notification until device reconnects
@@ -200,7 +185,7 @@ function windowsphonepush_cron()
                                        $senditemtext = DI::pConfig()->get($rr['uid'], 'windowsphonepush', 'senditemtext');
                                        if ($senditemtext == 1) {
                                                // load item with the max id
-                                               $item = Item::selectFirst(['author-name', 'body'], ['id' => $count[0]['max']]);
+                                               $item = Post::selectFirst(['author-name', 'body'], ['id' => $count[0]['max']]);
 
                                                // as user allows to send the item, we want to show the sender of the item in the toast
                                                // toasts are limited to one line, therefore place is limited - author shall be in
@@ -216,7 +201,7 @@ function windowsphonepush_cron()
                                                if (substr($body, 0, 4) == "[url") {
                                                        $body = "URL/Image ...";
                                                } else {
-                                                       $body = BBCode::convert($body, false, 2, true);
+                                                       $body = BBCode::convert($body, false, BBCode::API, true);
                                                        $body = HTML::toPlaintext($body, 0);
                                                        $body = ((strlen($body) > 137) ? substr($body, 0, 137) . "..." : $body);
                                                }
@@ -232,7 +217,7 @@ function windowsphonepush_cron()
                                        // further log information done on count pushing with send_tile (see above)
                                        $res_toast = send_toast($device_url, $author, $body);
                                        if (trim($res_toast) === 'Received') {
-                                               PConfig::set($rr['uid'], 'windowsphonepush', 'lastpushid', $count[0]['max']);
+                                               DI::pConfig()->set($rr['uid'], 'windowsphonepush', 'lastpushid', $count[0]['max']);
                                        }
                                }
                        }
@@ -306,7 +291,7 @@ function send_push($device_url, $headers, $msg)
        // and log this fact
        $subscriptionStatus = get_header_value($output, 'X-SubscriptionStatus');
        if ($subscriptionStatus == "Expired") {
-               PConfig::set(local_user(), 'windowsphonepush', 'device_url', "");
+               DI::pConfig()->set(local_user(), 'windowsphonepush', 'device_url', "");
                Logger::log("ERROR: the stored Device-URL " . $device_url . "returned an 'Expired' error, it has been deleted now.");
        }
 
@@ -422,12 +407,12 @@ function windowsphonepush_updatesettings()
                                                `v` = '" . $device_url . "'");
        if (count($r)) {
                foreach ($r as $rr) {
-                       PConfig::set($rr['uid'], 'windowsphonepush', 'device_url', '');
+                       DI::pConfig()->set($rr['uid'], 'windowsphonepush', 'device_url', '');
                        Logger::log("WARN: the sent URL was already registered with user '" . $rr['uid'] . "'. Deleted for this user as we expect to be correct now for user '" . local_user() . "'.");
                }
        }
 
-       PConfig::set(local_user(), 'windowsphonepush', 'device_url', $device_url);
+       DI::pConfig()->set(local_user(), 'windowsphonepush', 'device_url', $device_url);
        // output the successfull update of the device URL to the logger for error analysis if necessary
        Logger::log("INFO: Device-URL for user '" . local_user() . "' has been updated with '" . $device_url . "'");
        return "Device-URL updated successfully!";
@@ -446,7 +431,7 @@ function windowsphonepush_updatecounterunseen()
                return "Plug-in not enabled";
        }
 
-       PConfig::set(local_user(), 'windowsphonepush', 'counterunseen', 0);
+       DI::pConfig()->set(local_user(), 'windowsphonepush', 'counterunseen', 0);
        return "Counter set to zero";
 }