1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\ShortURL\Model; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\ReadonlyField; |
9
|
|
|
use SilverStripe\ORM\DataObject; |
10
|
|
|
use SilverStripe\ORM\ValidationResult; |
11
|
|
|
|
12
|
|
|
class ShortURL extends DataObject |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private static $singular_name = 'Short URL'; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private static $plural_name = 'Short URLs'; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private static $db = [ |
|
|
|
|
28
|
|
|
'Title' => 'Varchar(255)', |
29
|
|
|
'URL' => 'Varchar(255)', |
30
|
|
|
'CampaignSource' => 'Varchar(255)', |
31
|
|
|
'CampaignMedium' => 'Varchar(255)', |
32
|
|
|
'CampaignName' => 'Varchar(255)', |
33
|
|
|
'CampaignTerm' => 'Varchar(255)', |
34
|
|
|
'CampaignContent' => 'Varchar(255)', |
35
|
|
|
'ShortURL' => 'Varchar(255)', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private static $summary_fields = [ |
|
|
|
|
42
|
|
|
'Title', |
43
|
|
|
'URL', |
44
|
|
|
'ShortURL', |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private static $searchable_fields = [ |
|
|
|
|
51
|
|
|
'Title', |
52
|
|
|
'URL', |
53
|
|
|
'CampaignSource', |
54
|
|
|
'CampaignMedium', |
55
|
|
|
'CampaignName', |
56
|
|
|
'CampaignTerm', |
57
|
|
|
'CampaignContent', |
58
|
|
|
'ShortURL', |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private static $table_name = 'ShortURL'; |
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return FieldList |
68
|
|
|
*/ |
69
|
|
|
public function getCMSFields() |
70
|
|
|
{ |
71
|
|
|
$fields = parent::getCMSFields(); |
72
|
|
|
|
73
|
|
|
$fields->dataFieldByName('URL') |
74
|
1 |
|
->setDescription('URL to shorten and tag'); |
75
|
|
|
|
76
|
1 |
|
$fields->dataFieldByName('CampaignSource') |
77
|
|
|
->setDescription( |
78
|
1 |
|
'Use to identify a search engine, newsletter name, or other source. Example: google' |
79
|
1 |
|
); |
80
|
|
|
|
81
|
1 |
|
$fields->dataFieldByName('CampaignMedium') |
82
|
1 |
|
->setDescription( |
83
|
1 |
|
'Use utm_medium to identify a medium such as email or cost-per- click. Example: cpc' |
84
|
|
|
); |
85
|
|
|
|
86
|
1 |
|
$fields->dataFieldByName('CampaignName') |
87
|
1 |
|
->setDescription( |
88
|
1 |
|
'Used for keyword analysis. Use utm_campaign to identify a specific product promotion or |
89
|
|
|
strategic campaign. Example: utm_campaign=spring_sale' |
90
|
|
|
); |
91
|
1 |
|
|
92
|
1 |
|
$fields->dataFieldByName('CampaignTerm') |
93
|
1 |
|
->setDescription( |
94
|
|
|
'Used for paid search. Use utm_term to note the keywords for this ad. Example: running+shoes' |
95
|
|
|
); |
96
|
|
|
|
97
|
1 |
|
$fields->dataFieldByName('CampaignContent') |
98
|
1 |
|
->setDescription( |
99
|
1 |
|
'Used for A/B testing and content-targeted ads. Use utm_content to differentiate ads or links |
100
|
|
|
that point to the same URL. Examples: logolink or textlink' |
101
|
|
|
); |
102
|
1 |
|
|
103
|
1 |
|
$fields->addFieldToTab('Root.Main', ReadonlyField::create('LongURL', 'Long URL', $this->getLongURL())); |
104
|
1 |
|
|
105
|
|
|
$short = $fields->dataFieldByName('ShortURL'); |
106
|
|
|
$short = $short->performReadonlyTransformation(); |
107
|
|
|
$fields->addFieldToTab('Root.Main', $short); |
108
|
1 |
|
|
109
|
|
|
return $fields; |
110
|
1 |
|
} |
111
|
1 |
|
|
112
|
1 |
|
/** |
113
|
|
|
* @return ValidationResult |
114
|
1 |
|
*/ |
115
|
|
|
public function validate() |
116
|
|
|
{ |
117
|
|
|
$result = parent::validate(); |
118
|
|
|
|
119
|
|
|
if (!$this->Title) { |
120
|
1 |
|
$result->addError('A Title is required before you can save'); |
121
|
|
|
} |
122
|
1 |
|
|
123
|
|
|
if (!$this->URL) { |
|
|
|
|
124
|
1 |
|
$result->addError('A URL is required before you can save'); |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
if (!$this->CampaignSource) { |
|
|
|
|
128
|
1 |
|
$result->addError('A Campaign Source is required before you can save'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $result; |
132
|
1 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return mixed |
136
|
1 |
|
*/ |
137
|
|
|
public function getToken() |
138
|
|
|
{ |
139
|
|
|
return Config::inst()->get(ShortURL::class, 'bitly_token'); |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
/** |
143
|
|
|
* @return mixed |
144
|
1 |
|
*/ |
145
|
|
|
public function getDomain() |
146
|
|
|
{ |
147
|
|
|
return Config::inst()->get(ShortURL::class, 'bitly_domain'); |
148
|
|
|
} |
149
|
|
|
|
150
|
2 |
|
/** |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
2 |
|
public function getLongURL() |
154
|
2 |
|
{ |
155
|
2 |
|
$vars = [ |
156
|
2 |
|
'utm_source' => $this->CampaignSource, |
|
|
|
|
157
|
2 |
|
'utm_medium' => $this->CampaignMedium, |
|
|
|
|
158
|
|
|
'utm_campaign' => $this->CampaignName, |
|
|
|
|
159
|
2 |
|
'utm_term' => $this->CampaignTerm, |
|
|
|
|
160
|
2 |
|
'utm_content' => $this->CampaignContent, |
|
|
|
|
161
|
|
|
]; |
162
|
|
|
$LongURL = $this->URL . '?' . http_build_query($vars); |
|
|
|
|
163
|
|
|
return $LongURL; |
164
|
|
|
} |
165
|
|
|
|
166
|
1 |
|
/** |
167
|
|
|
* @return bool |
168
|
1 |
|
*/ |
169
|
|
|
public function getLinkChanged() |
170
|
|
|
{ |
171
|
|
|
$isChanged = false; |
172
|
|
|
|
173
|
|
|
if ( |
174
|
|
|
$this->isChanged('URL', self::CHANGE_VALUE) || |
175
|
|
|
$this->isChanged('CampaignMedium', self::CHANGE_VALUE) || |
176
|
|
|
$this->isChanged('CampaignName', self::CHANGE_VALUE) || |
177
|
|
|
$this->isChanged('CampaignTerm', self::CHANGE_VALUE) || |
178
|
|
|
$this->isChanged('CampaignContent', self::CHANGE_VALUE) |
179
|
1 |
|
) { |
180
|
|
|
$isChanged = true; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$this->extend('updateIsLinkChanged', $isChanged); |
184
|
|
|
return $isChanged; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* |
189
|
|
|
*/ |
190
|
|
|
public function onBeforeWrite() |
191
|
|
|
{ |
192
|
|
|
// do not call api if link is not changed |
193
|
|
|
if (!$this->getLinkChanged()) { |
194
|
|
|
return parent::onBeforeWrite(); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if ($token = $this->getToken()) { |
|
|
|
|
198
|
|
|
$client = new Client([ |
199
|
|
|
'base_uri' => 'https://api-ssl.bitly.com/v4/', |
200
|
|
|
'timeout' => $this->config()->get('timeout'), |
201
|
|
|
'http_errors' => false, |
202
|
|
|
'verify' => true, |
203
|
|
|
'headers' => [ |
204
|
|
|
'Authorization' => 'Bearer ' . $this->getToken(), |
205
|
|
|
'Content-Type' => 'application/json', |
206
|
|
|
], |
207
|
|
|
]); |
208
|
|
|
|
209
|
|
|
$data = [ |
210
|
|
|
'long_url' => $this->getLongURL(), |
211
|
|
|
]; |
212
|
|
|
|
213
|
|
|
if ($domain = $this->getDomain()) { |
214
|
|
|
$data['domain'] = $domain; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$response = $client->post('shorten', [ |
218
|
|
|
'json' => $data, |
219
|
|
|
]); |
220
|
|
|
|
221
|
|
|
$responseData = json_decode($response->getBody(), true); |
222
|
|
|
if ($response->getStatusCode() != 200 && $response->getStatusCode() != 201) { |
223
|
|
|
throw new \Exception($responseData['message'] . ' : ' . $responseData['description']); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$this->ShortURL = $responseData['link']; |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
parent::onBeforeWrite(); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|