|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\ShortURL; |
|
4
|
|
|
|
|
5
|
|
|
use \Hpatoio\Bitly\Client; |
|
6
|
|
|
|
|
7
|
|
|
class ShortURL extends \DataObject |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
private static $singular_name = 'Short URL'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private static $plural_name = 'Short URLs'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var |
|
21
|
|
|
*/ |
|
22
|
|
|
private $bitly_token; |
|
|
|
|
|
|
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
|
|
|
* @return FieldList |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getCMSFields() |
|
65
|
|
|
{ |
|
66
|
|
|
$fields = parent::getCMSFields(); |
|
67
|
|
|
|
|
68
|
|
|
$fields->dataFieldByName('URL') |
|
69
|
|
|
->setDescription('URL to shorten and tag'); |
|
70
|
|
|
|
|
71
|
|
|
$fields->dataFieldByName('CampaignSource') |
|
72
|
|
|
->setDescription('Use to identify a search engine, newsletter name, or other source. Example: google'); |
|
73
|
|
|
|
|
74
|
|
|
$fields->dataFieldByName('CampaignMedium') |
|
75
|
|
|
->setDescription('Use utm_medium to identify a medium such as email or cost-per- click. Example: cpc'); |
|
76
|
|
|
|
|
77
|
|
|
$fields->dataFieldByName('CampaignName') |
|
78
|
|
|
->setDescription('Used for keyword analysis. Use utm_campaign to identify a specific product promotion or strategic campaign. Example: utm_campaign=spring_sale'); |
|
79
|
|
|
|
|
80
|
|
|
$fields->dataFieldByName('CampaignTerm') |
|
81
|
|
|
->setDescription('Used for paid search. Use utm_term to note the keywords for this ad. Example: running+shoes'); |
|
82
|
|
|
|
|
83
|
|
|
$fields->dataFieldByName('CampaignContent') |
|
84
|
|
|
->setDescription('Used for A/B testing and content-targeted ads. Use utm_content to differentiate ads or links that point to the same URL. Examples: logolink or textlink'); |
|
85
|
|
|
|
|
86
|
|
|
$fields->addFieldToTab('Root.Main', \ReadonlyField::create('LongURL', 'Long URL', $this->getLongURL())); |
|
87
|
|
|
|
|
88
|
|
|
$short = $fields->dataFieldByName('ShortURL'); |
|
89
|
|
|
$short = $short->performReadonlyTransformation(); |
|
90
|
|
|
$fields->addFieldToTab('Root.Main', $short); |
|
91
|
|
|
|
|
92
|
|
|
return $fields; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return ValidationResult |
|
97
|
|
|
*/ |
|
98
|
|
|
public function validate() |
|
99
|
|
|
{ |
|
100
|
|
|
$result = parent::validate(); |
|
101
|
|
|
|
|
102
|
|
|
if (!$this->Title) { |
|
|
|
|
|
|
103
|
|
|
$result->error('A Title is required before you can save'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (!$this->URL) { |
|
|
|
|
|
|
107
|
|
|
$result->error('A URL is required before you can save'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if (!$this->CampaignSource) { |
|
|
|
|
|
|
111
|
|
|
$result->error('A Campaign Source is required before you can save'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $result; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return array|\scalar |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getToken() |
|
121
|
|
|
{ |
|
122
|
|
|
return \Config::inst()->get('Dynamic\ShortURL\ShortURL', 'bitly_token'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getLongURL() |
|
129
|
|
|
{ |
|
130
|
|
|
$vars = [ |
|
131
|
|
|
'utm_source' => $this->CampaignSource, |
|
|
|
|
|
|
132
|
|
|
'utm_medium' => $this->CampaignMedium, |
|
|
|
|
|
|
133
|
|
|
'utm_campaign' => $this->CampaignName, |
|
|
|
|
|
|
134
|
|
|
'utm_term' => $this->CampaignTerm, |
|
|
|
|
|
|
135
|
|
|
'utm_content' => $this->CampaignTerm, |
|
|
|
|
|
|
136
|
|
|
]; |
|
137
|
|
|
$LongURL = $this->URL . '?' . http_build_query($vars); |
|
|
|
|
|
|
138
|
|
|
return $LongURL; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* |
|
143
|
|
|
*/ |
|
144
|
|
|
public function onBeforeWrite() |
|
145
|
|
|
{ |
|
146
|
|
|
parent::onBeforeWrite(); |
|
147
|
|
|
|
|
148
|
|
|
if ($token = $this->getToken()) { |
|
149
|
|
|
$bitly = new Client($token); |
|
150
|
|
|
|
|
151
|
|
|
$response = $bitly->Shorten([ |
|
152
|
|
|
'longUrl' => $this->getLongURL(), |
|
153
|
|
|
'format' => 'txt' |
|
154
|
|
|
]); |
|
155
|
|
|
|
|
156
|
|
|
$this->ShortURL = $response['url']; |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.