dukt /
twitter
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @link https://dukt.net/twitter/ |
||
| 4 | * @copyright Copyright (c) Dukt |
||
| 5 | * @license https://github.com/dukt/twitter/blob/master/LICENSE.md |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace dukt\twitter\widgets; |
||
| 9 | |||
| 10 | use Craft; |
||
| 11 | use craft\base\Widget; |
||
| 12 | use craft\helpers\Json; |
||
| 13 | use craft\helpers\UrlHelper; |
||
| 14 | use dukt\twitter\models\Tweet; |
||
| 15 | use dukt\twitter\Plugin; |
||
| 16 | use dukt\twitter\web\assets\searchwidget\SearchWidgetAsset; |
||
| 17 | use GuzzleHttp\Exception\ClientException; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Search Widget |
||
| 21 | * |
||
| 22 | * @author Dukt <[email protected]> |
||
| 23 | * @since 3.0 |
||
| 24 | */ |
||
| 25 | class SearchWidget extends Widget |
||
| 26 | { |
||
| 27 | // Properties |
||
| 28 | // ========================================================================= |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var |
||
| 32 | */ |
||
| 33 | public $query; |
||
| 34 | /** |
||
| 35 | * @var |
||
| 36 | */ |
||
| 37 | public $count; |
||
| 38 | |||
| 39 | // Static |
||
| 40 | // ========================================================================= |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @inheritdoc |
||
| 44 | */ |
||
| 45 | public static function displayName(): string |
||
| 46 | { |
||
| 47 | return Craft::t('twitter', 'Twitter Search'); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @inheritdoc |
||
| 52 | */ |
||
| 53 | public static function icon() |
||
| 54 | { |
||
| 55 | return Craft::getAlias('@dukt/twitter/icons/twitter.svg'); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 56 | } |
||
| 57 | |||
| 58 | // Public Methods |
||
| 59 | // ========================================================================= |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @inheritdoc |
||
| 63 | */ |
||
| 64 | public function rules() |
||
| 65 | { |
||
| 66 | $rules = parent::rules(); |
||
| 67 | $rules[] = [['query', 'count'], 'required']; |
||
| 68 | $rules[] = [['query'], 'string']; |
||
| 69 | $rules[] = [['count'], 'integer', 'min' => 1]; |
||
| 70 | |||
| 71 | return $rules; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @inheritdoc |
||
| 76 | */ |
||
| 77 | public function getTitle(): string |
||
| 78 | { |
||
| 79 | $settings = $this->getSettings(); |
||
| 80 | |||
| 81 | if (!empty($settings['query'])) { |
||
| 82 | return Craft::t('twitter', 'Tweets for “{query}”', ['query' => $settings['query']]); |
||
| 83 | } |
||
| 84 | |||
| 85 | return Craft::t('twitter', 'Twitter Search'); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @inheritdoc |
||
| 90 | */ |
||
| 91 | public function getBodyHtml() |
||
| 92 | { |
||
| 93 | $variables = []; |
||
| 94 | $settings = $this->getSettings(); |
||
| 95 | |||
| 96 | $searchQuery = $settings['query']; |
||
| 97 | $count = $settings['count']; |
||
| 98 | |||
| 99 | $token = Plugin::getInstance()->getOauth()->getToken(); |
||
| 100 | |||
| 101 | if ($token) { |
||
| 102 | if (!empty($searchQuery)) { |
||
| 103 | try { |
||
| 104 | $q = $searchQuery; |
||
| 105 | |||
| 106 | if (Plugin::getInstance()->getSettings()->searchWidgetExtraQuery) { |
||
| 107 | $q .= ' '.Plugin::getInstance()->getSettings()->searchWidgetExtraQuery; |
||
| 108 | } |
||
| 109 | |||
| 110 | $response = Plugin::getInstance()->getApi()->get('search/tweets', [ |
||
| 111 | 'q' => $q, |
||
| 112 | 'count' => $count, |
||
| 113 | 'tweet_mode' => 'extended' |
||
| 114 | ]); |
||
| 115 | |||
| 116 | $tweets = []; |
||
| 117 | |||
| 118 | foreach ($response['statuses'] as $tweetData) { |
||
| 119 | $tweet = new Tweet(); |
||
| 120 | Plugin::getInstance()->getApi()->populateTweetFromData($tweet, $tweetData); |
||
| 121 | array_push($tweets, $tweet); |
||
| 122 | } |
||
| 123 | |||
| 124 | $variables['tweets'] = $tweets; |
||
| 125 | |||
| 126 | Craft::$app->getView()->registerAssetBundle(SearchWidgetAsset::class); |
||
| 127 | Craft::$app->getView()->registerJs("new Craft.Twitter_SearchWidget('".$this->id."');"); |
||
| 128 | |||
| 129 | return Craft::$app->getView()->renderTemplate('twitter/_components/widgets/Search/body', $variables); |
||
| 130 | } catch (ClientException $e) { |
||
| 131 | $errorMsg = $e->getMessage(); |
||
| 132 | $data = Json::decodeIfJson($e->getResponse()->getBody()->getContents()); |
||
| 133 | |||
| 134 | if (isset($data['errors'][0]['message'])) { |
||
| 135 | $errorMsg = $data['errors'][0]['message']; |
||
| 136 | } |
||
| 137 | |||
| 138 | Craft::error('Couldn’t retrieve tweets: '.$e->getTraceAsString(), __METHOD__); |
||
| 139 | |||
| 140 | return Craft::$app->getView()->renderTemplate('twitter/_components/widgets/Search/_error', [ |
||
| 141 | 'errorMsg' => $errorMsg |
||
| 142 | ]); |
||
| 143 | } |
||
| 144 | } else { |
||
| 145 | $variables['infoMsg'] = Craft::t('twitter', 'Please enter a search query in the widget’s settings.'); |
||
| 146 | |||
| 147 | return Craft::$app->getView()->renderTemplate('twitter/_components/widgets/Search/_error', $variables); |
||
| 148 | } |
||
| 149 | } else { |
||
| 150 | $variables['infoMsg'] = Craft::t('twitter', 'Twitter is not configured, please check the <a href="{url}">plugin’s settings</a>.', [ |
||
| 151 | 'url' => UrlHelper::url('twitter/settings') |
||
| 152 | ]); |
||
| 153 | |||
| 154 | return Craft::$app->getView()->renderTemplate('twitter/_components/widgets/Search/_error', $variables); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @inheritdoc |
||
| 160 | */ |
||
| 161 | public function getSettingsHtml() |
||
| 162 | { |
||
| 163 | return Craft::$app->getView()->renderTemplate('twitter/_components/widgets/Search/settings', [ |
||
| 164 | 'settings' => $this->getSettings() |
||
| 165 | ]); |
||
| 166 | } |
||
| 167 | } |
||
| 168 |