1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Craft; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* YouTube Plugin. |
7
|
|
|
* |
8
|
|
|
* Upload video assets to YouTube. |
9
|
|
|
* |
10
|
|
|
* @author Bob Olde Hampsink <[email protected]> |
11
|
|
|
* @copyright Copyright (c) 2015, Itmundi |
12
|
|
|
* @license MIT |
13
|
|
|
* |
14
|
|
|
* @link http://github.com/boboldehampsink |
15
|
|
|
*/ |
16
|
|
|
class YouTubePlugin extends BasePlugin |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Get plugin name. |
20
|
|
|
* |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
public function getName() |
24
|
|
|
{ |
25
|
|
|
return Craft::t('YouTube Upload'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get plugin version. |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getVersion() |
34
|
|
|
{ |
35
|
|
|
return '0.6.5'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get plugin developer. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getDeveloper() |
44
|
|
|
{ |
45
|
|
|
return 'Bob Olde Hampsink'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get plugin developer url. |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getDeveloperUrl() |
54
|
|
|
{ |
55
|
|
|
return 'http://github.com/boboldehampsink'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add user to task manager table. |
60
|
|
|
* |
61
|
|
|
* @param array $attributes |
62
|
|
|
* @param string|null $source |
63
|
|
|
*/ |
64
|
|
|
public function modifyTaskManagerTableAttributes(array &$attributes, $source) |
65
|
|
|
{ |
66
|
|
|
if ($source == 'type:YouTube_Upload') { |
67
|
|
|
$attributes['user'] = Craft::t('User'); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get task manager table attribute html. |
73
|
|
|
* |
74
|
|
|
* @param BaseElementModel $element |
75
|
|
|
* @param string $attribute |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getTaskManagerTableAttributeHtml(BaseElementModel $element, $attribute) |
80
|
|
|
{ |
81
|
|
|
if ($attribute == 'user') { |
82
|
|
|
|
83
|
|
|
// Get user |
84
|
|
|
$user = craft()->users->getUserById($element->settings['user']); |
85
|
|
|
|
86
|
|
|
// Get name |
87
|
|
|
$name = !empty($user->fullName) ? $user->fullName : $user->username; |
88
|
|
|
|
89
|
|
|
// Return name and link |
90
|
|
|
return '<a href="'.$user->getCpEditUrl().'">'.$name.'</a>'; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Define plugin settings. |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function defineSettings() |
100
|
|
|
{ |
101
|
|
|
return array( |
102
|
|
|
'tokenId' => AttributeType::Number, |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get settings html. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getSettingsHtml() |
112
|
|
|
{ |
113
|
|
|
// Set default options |
114
|
|
|
$options = array( |
115
|
|
|
'gateway' => 'google', |
116
|
|
|
'provider' => false, |
117
|
|
|
'account' => false, |
118
|
|
|
'token' => false, |
119
|
|
|
'error' => false, |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
// Get provider |
123
|
|
|
$provider = craft()->oauth->getProvider('google', false); |
124
|
|
|
if ($provider) { |
125
|
|
|
if ($provider->isConfigured()) { |
126
|
|
|
|
127
|
|
|
// Get token |
128
|
|
|
$token = craft()->youTube_oauth->getToken(); |
129
|
|
|
if ($token) { |
130
|
|
|
|
131
|
|
|
// Get account |
132
|
|
|
try { |
133
|
|
|
$account = $provider->getAccount($token); |
134
|
|
|
|
135
|
|
|
if ($account) { |
136
|
|
|
$options['account'] = $account; |
137
|
|
|
$options['settings'] = $this->getSettings(); |
138
|
|
|
} |
139
|
|
|
} catch (\Exception $e) { |
140
|
|
|
$options['error'] = $e->getMessage(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
$options['token'] = $token; |
144
|
|
|
} |
145
|
|
|
$options['provider'] = $provider; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return craft()->templates->render('youtube/settings/_plugin', $options); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Remove tokens on uninstall. |
153
|
|
|
*/ |
154
|
|
|
public function onBeforeUninstall() |
155
|
|
|
{ |
156
|
|
|
if (isset(craft()->oauth)) { |
157
|
|
|
craft()->oauth->deleteTokensByPlugin('youtube'); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Initialize plugin. |
163
|
|
|
*/ |
164
|
|
|
public function init() |
165
|
|
|
{ |
166
|
|
|
// Initialize parent |
167
|
|
|
parent::init(); |
168
|
|
|
|
169
|
|
|
// Autoload dependencies |
170
|
|
|
require_once dirname(__FILE__).'/vendor/autoload.php'; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|