[FEATURE] Add "Show lists for username" 01/23201/2
authorNicole Cordes <cordes@cps-it.de>
Tue, 20 Aug 2013 20:45:47 +0000 (22:45 +0200)
committerNicole Cordes <typo3@cordes.co>
Tue, 20 Aug 2013 20:46:35 +0000 (22:46 +0200)
Add a new plugin function to show all lists an user is member of.

Releases: 1.0
Change-Id: Icd6bb3ecde0182549d56090bb0063f58df328ae1
Reviewed-on: https://review.typo3.org/23201
Reviewed-by: Nicole Cordes
Tested-by: Nicole Cordes
Classes/Controller/TwitterController.php
Classes/Domain/Repository/TweetRepository.php
Classes/Twitter/Api.php
Configuration/Flexforms/flexform.xml
Resources/Private/Language/locallang_db.xml
Resources/Private/Templates/Twitter/List.html

index 687fdbf..841fa4e 100644 (file)
@@ -87,6 +87,9 @@ class Tx_WtTwitter_Controller_TwitterController extends Tx_Extbase_MVC_Controlle
                        case 'showListsFrom':
                                $tweets = $this->tweetRepository->getListsFromUser($this->settings, $response);
                                break;
+                       case 'showListsFor':
+                               $tweets = $this->tweetRepository->getListsForUser($this->settings, $response);
+                               break;
                }
                // Look up for any errors
                if ($response !== '') {
index 4efda0d..de6bd0e 100644 (file)
@@ -166,6 +166,60 @@ class Tx_WtTwitter_Domain_Repository_TweetRepository {
        }
 
        /**
+        * @param $settings
+        * @param NULL $response
+        * @return array
+        */
+       public function getListsForUser($settings, &$response = NULL) {
+               $lists = array();
+               $cursor = -1;
+
+               if ($this->isTwitterSigned()&& $this->isCurlActivated()) {
+                       $parameter = array();
+
+                       // Get screen name
+                       if (Tx_WtTwitter_Utility_Compatibility::testInt($settings['account'])) {
+                               $parameter['user_id'] = $settings['account'];
+                       } else {
+                               $parameter['screen_name'] = $settings['account'];
+                       }
+
+                       while (!empty($cursor) && ($settings['limit'] == 0 || $settings['limit'] > count($lists))) {
+                               $parameter['cursor'] = $cursor;
+                               $result = $this->callApi(Tx_WtTwitter_Twitter_Api::getListsMembershipsUrl(), 'GET', $parameter, $response);
+                               $lists = array_merge($lists, (array) $result->lists);
+                               $cursor = $result->next_cursor_str;
+                       }
+
+                       usort($lists, function($a, $b) use ($settings) {
+                               switch ($settings['orderby']) {
+                                       case 'subscriberCount':
+                                               $valueA = (int) $a->subscriber_count;
+                                               $valueB = (int) $b->subscriber_count;
+                                               break;
+                                       case 'memberCount':
+                                               $valueA = (int) $a->member_count;
+                                               $valueB = (int) $b->member_count;
+                                               break;
+                                       default:
+                                               $dateA = new DateTime($a->created_at);
+                                               $valueA = $dateA->getTimestamp();
+                                               $dateB = new DateTime($b->created_at);
+                                               $valueB = $dateB->getTimestamp();
+                               }
+
+                               if ($valueA === $valueB) {
+                                       return 0;
+                               }
+
+                               return ($valueA > $valueB) ? -1 : 1;
+                       });
+               }
+
+               return $this->sliceArray($lists, $settings['limit']);
+       }
+
+       /**
         * @return boolean
         */
        protected function isTwitterSigned() {
index 3fd10ec..8f4b453 100644 (file)
@@ -57,6 +57,13 @@ final class Tx_WtTwitter_Twitter_Api {
        /**
         * @return string
         */
+       public static function getListsMembershipsUrl() {
+               return 'https://api.twitter.com/1.1/lists/memberships.json';
+       }
+
+       /**
+        * @return string
+        */
        public static function getListsOwnershipsUrl() {
                return 'https://api.twitter.com/1.1/lists/ownerships.json';
        }
index 206fd9e..bf915e0 100644 (file)
                                                                                <numIndex index="0">LLL:EXT:wt_twitter/Resources/Private/Language/locallang_db.xml:pi_flexform.mode.4</numIndex>
                                                                                <numIndex index="1">showListsFrom</numIndex>
                                                                        </numIndex>
+                                                                       <numIndex index="5" type="array">
+                                                                               <numIndex index="0">LLL:EXT:wt_twitter/Resources/Private/Language/locallang_db.xml:pi_flexform.mode.5</numIndex>
+                                                                               <numIndex index="1">showListsFor</numIndex>
+                                                                       </numIndex>
                                                                </items>
                                                                <maxitems>1</maxitems>
                                                                <size>1</size>
@@ -49,7 +53,7 @@
                                                <TCEforms>
                                                        <exclude>1</exclude>
                                                        <label>LLL:EXT:wt_twitter/Resources/Private/Language/locallang_db.xml:pi_flexform.account</label>
-                                                       <displayCond>FIELD:settings.flexform.mode:IN:showOwn,showListsFrom</displayCond>
+                                                       <displayCond>FIELD:settings.flexform.mode:IN:showOwn,showListsFrom,showListsFor</displayCond>
                                                        <config>
                                                                <type>input</type>
                                                                <size>20</size>
@@ -71,7 +75,7 @@
                                                <TCEforms>
                                                        <exclude>1</exclude>
                                                        <label>LLL:EXT:wt_twitter/Resources/Private/Language/locallang_db.xml:pi_flexform.orderby</label>
-                                                       <displayCond>FIELD:settings.flexform.mode:IN:showListsFrom</displayCond>
+                                                       <displayCond>FIELD:settings.flexform.mode:IN:showListsFrom,showListsFor</displayCond>
                                                        <config>
                                                                <type>select</type>
                                                                <items type="array">
index d39f8fa..1082cb9 100644 (file)
@@ -17,6 +17,7 @@
                        <label index="pi_flexform.mode.2">Show Friends Tweets</label>
                        <label index="pi_flexform.mode.3">Show Tweets from search term</label>
                        <label index="pi_flexform.mode.4">Show lists from username</label>
+                       <label index="pi_flexform.mode.5">Show lists for username</label>
                        <label index="pi_flexform.hashtag">Enter a hashtag</label>
                        <label index="pi_flexform.orderby">Order by</label>
                        <label index="pi_flexform.orderby.0">Creation date</label>
@@ -36,6 +37,7 @@
                        <label index="pi_flexform.mode.2">Zeige befreundete Tweets</label>
                        <label index="pi_flexform.mode.3">Zeige Tweets anhand von Suchbegriff</label>
                        <label index="pi_flexform.mode.4">Zeige Listen von einem Benutzer</label>
+                       <label index="pi_flexform.mode.5">Zeige Listen zu einem Benutzer</label>
                        <label index="pi_flexform.hashtag">Suche nach Hashtag</label>
                        <label index="pi_flexform.orderby">Sortieren nach</label>
                        <label index="pi_flexform.orderby.0">Erstellungsdatum</label>
index 802d44e..a5ada5f 100644 (file)
@@ -1,20 +1,25 @@
-<div id="wt_twitter_newsticker">\r
-       <f:flashMessages class="wt_twitter_errors" style="background-color: #FBB19B; padding: 5px 30px; font-weight: bold; border: 1px solid #DC4C42; margin-bottom: 20px; font-family: arial, verdana; color: #444; font-size: 12px;" />\r
-       <ul class="wttwitter_feed" style="list-style-type: none;">\r
-               <f:if condition="{0: settings.mode} == {0: 'showOwn'}">\r
-                       <f:then>\r
-                               <f:render partial="Tweets/Show" arguments="{tweets: tweets}" />\r
-                       </f:then>\r
-               </f:if>\r
-               <f:if condition="{0: settings.mode} == {0: 'showFromSearch'}">\r
-                       <f:then>\r
-                               <f:render partial="Tweets/Show" arguments="{tweets: tweets}" />\r
-                       </f:then>\r
-               </f:if>\r
-               <f:if condition="{0: settings.mode} == {0: 'showListsFrom'}">\r
-                       <f:then>\r
-                               <f:render partial="Lists/Show" arguments="{lists: tweets}" />\r
-                       </f:then>\r
-               </f:if>\r
-       </ul>\r
+<div id="wt_twitter_newsticker">
+       <f:flashMessages class="wt_twitter_errors" style="background-color: #FBB19B; padding: 5px 30px; font-weight: bold; border: 1px solid #DC4C42; margin-bottom: 20px; font-family: arial, verdana; color: #444; font-size: 12px;" />
+       <ul class="wttwitter_feed" style="list-style-type: none;">
+               <f:if condition="{0: settings.mode} == {0: 'showOwn'}">
+                       <f:then>
+                               <f:render partial="Tweets/Show" arguments="{tweets: tweets}" />
+                       </f:then>
+               </f:if>
+               <f:if condition="{0: settings.mode} == {0: 'showFromSearch'}">
+                       <f:then>
+                               <f:render partial="Tweets/Show" arguments="{tweets: tweets}" />
+                       </f:then>
+               </f:if>
+               <f:if condition="{0: settings.mode} == {0: 'showListsFrom'}">
+                       <f:then>
+                               <f:render partial="Lists/Show" arguments="{lists: tweets}" />
+                       </f:then>
+               </f:if>
+               <f:if condition="{0: settings.mode} == {0: 'showListsFor'}">
+                       <f:then>
+                               <f:render partial="Lists/Show" arguments="{lists: tweets}" />
+                       </f:then>
+               </f:if>
+       </ul>
 </div>
\ No newline at end of file