]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Implemented Flattr changes
authorThomas Citharel <thomas.citharet@gmail.com>
Sun, 8 Sep 2013 18:54:11 +0000 (20:54 +0200)
committerThomas Citharel <thomas.citharet@gmail.com>
Sun, 8 Sep 2013 18:54:11 +0000 (20:54 +0200)
Added a button to say if the article is flattrable or not and how many
people have flattred this object.

inc/poche/Poche.class.php
tpl/view.twig

index a8f64151d59aecaa67922fdd6dc00ff6776ca9d6..5dab10dffc411aef22912c79462fa2957a38c1ef 100644 (file)
@@ -247,10 +247,15 @@ class Poche
                         $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
                         $tidy->cleanRepair();
                         $content = $tidy->value;
-                    }
+
+                        // flattr checking
+                    $flattr = new FlattrItem();
+                    $flattr->checkitem($entry['url']);
+
                     $tpl_vars = array(
                         'entry' => $entry,
                         'content' => $content,
+                        'flattr' => $flattr,
                     );
                 }
                 else {
@@ -558,4 +563,54 @@ class Poche
         }
         return $version;
     }
-}
\ No newline at end of file
+}
+
+/* class for Flattr querying. Should be put in a separate file
+*   Or maybe just create an array instead of a complete class... My mistake. :-°
+*/
+class FlattrItem{
+    public $status;
+    public $urltoflattr;
+    public $flattrItemURL;
+    public $numflattrs;
+
+    public function checkitem($urltoflattr){
+        $this->cacheflattrfile($urltoflattr);
+        $flattrResponse = file_get_contents("cache/flattr/".base64_encode($urltoflattr).".cache");
+        var_dump($flattrResponse);
+        if($flattrResponse != FALSE){
+            $result = json_decode($flattrResponse);
+            if (isset($result->message)){
+                if ($result->message == "flattrable"){
+                $this->status = "flattrable";
+                        }
+                    } 
+            elseif ($result->link) {
+                            $this->status = "flattred";
+                            $this->flattrItemURL = $result->link;
+                            $this->numflattrs = $result->flattrs_user_count;
+                        }
+            else{
+                $this->status = "not flattrable";
+            }
+        }
+        else
+        {
+            $this->status = "FLATTR_ERR_CONNECTION";
+        }
+    }
+
+    private function cacheflattrfile($urltoflattr){
+        if (!is_dir('cache/flattr')){
+            mkdir('./cache/flattr', 0700);
+        }
+        // if a cache flattr file for this url already exists and it's been less than one day than it have been updated, see in /cache
+        if ((!file_exists("cache/flattr/".base64_encode($urltoflattr).".cache")) || (time() - filemtime("cache/flattr/".base64_encode($urltoflattr).".cache") > 86400))
+        {
+            $askForFlattr = Tools::getFile("https://api.flattr.com/rest/v2/things/lookup/?url=".$urltoflattr);
+             $flattrCacheFile = fopen("cache/flattr/".base64_encode($urltoflattr).".cache", 'w+');
+             fwrite($flattrCacheFile, $askForFlattr);
+             fclose($flattrCacheFile);
+        }
+    }
+}
index 285087723b2e008b4be4bac3bd2090b4fa13d957..7b15c7c76c2978c79c8303366b5a3ad91d494fa6 100644 (file)
@@ -31,6 +31,7 @@
                     {% if constant('SHARE_TWITTER') == 1 %}<li><a href="https://twitter.com/home?status={{entry.title|url_encode}}%20{{ entry.url|url_encode }}%20via%20@getpoche" target="_blank" class="tool twitter" title="{% trans "tweet" %}"><span>{% trans "tweet" %}</span></a></li>{% endif %}
                     {% if constant('SHARE_MAIL') == 1 %}<li><a href="mailto:?subject={{ entry.title|url_encode }}&amp;body={{ entry.url|url_encode }}%20via%20@getpoche" class="tool email" title="{% trans "email" %}"><span>{% trans "email" %}</span></a></li>{% endif %}
                     {% if constant('SHARE_SHAARLI') == 1 %}<li><a href="{{ constant('SHAARLI_URL') }}/index.php?post={{ entry.url|url_encode }}&amp;title={{ entry.title|url_encode }}" target="_blank" class="tool shaarli" title="{% trans "shaarli" %}"><span>{% trans "shaarli" %}</span></a></li>{% endif %}
+                    <li>{% if flattr.status == "flattrable" %} This thing is flattrable !{% elseif flattr.status == "flattred" %} <a href="{{ flattr.flattrItemURL }}" >This thing has already been flattred by {{ flattr.numflattrs }} users and can be flattred !</a>{% else %}This article cannot be flattred{% endif %}</li>
                 </ul>
                 <p>{% trans "this article appears wrong?" %} <a href="https://github.com/inthepoche/poche/issues/new">{% trans "create an issue" %}</a> {% trans "or" %} <a href="mailto:support@inthepoche.com?subject=Wrong%20display%20in%20poche&amp;body={{ entry.url|url_encode }}">{% trans "contact us by mail" %}</a></p>
             </div>