Skip to content

Commit 7c7eee3

Browse files
committed
Clickatell SMS notification module for HostBill
1 parent 38c9e10 commit 7c7eee3

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
3+
/**
4+
* Clickatell notification module for HostBill
5+
*
6+
* required methods:
7+
* notifyClient
8+
* notifyAdmin
9+
*
10+
* everything else is optional
11+
*
12+
* HostBill 2012
13+
*/
14+
class Clickatell_SMS extends NotificationModule {
15+
16+
protected $modname = 'Clickatell SMS Notifications';
17+
protected $description = 'Notify staff members and/or customers about events in HostBill trough Clickatell SMS gateway.
18+
<br> Phone number for staff member can be set in his profile';
19+
/**
20+
* Module configuration, visible in Settings->modules
21+
* @var array
22+
*/
23+
protected $configuration = array(
24+
'Api ID' => array(
25+
'value' => '',
26+
'type' => 'input',
27+
'description' => 'Clickatell HTTP API ID'
28+
), 'Username' => array(
29+
'value' => '',
30+
'type' => 'input',
31+
'description' => 'Clickatell Username'
32+
), 'Password' => array(
33+
'value' => '',
34+
'type' => 'input',
35+
'description' => 'Clickatell Password'
36+
),
37+
'Client Field' => array(
38+
'value' => 'mobilephone',
39+
'type' => 'input',
40+
'description' => 'Provide variable name from Clients->Registration fields responsible for holding client mobile phone number.
41+
If this field is empty no sms notifications will be sent to client trough Clickatell'
42+
)
43+
);
44+
45+
/**
46+
* Install module.
47+
* We need to add custom admin field for keeping his mobile number
48+
* We also need add custom client field (it can be later removed / updated by admin)
49+
*/
50+
public function install() {
51+
52+
$admin_field = array(
53+
'name' => 'Mobile phone number',
54+
'code' => 'mobilephone',
55+
'type' => 'input'
56+
);
57+
$fieldsmanager = HBLoader::LoadModel('EditAdmins/AdminFields');
58+
$fieldsmanager->addField($admin_field);
59+
60+
61+
$client_field = array(
62+
'name' => 'Mobile phone number',
63+
'code' => 'mobilephone',
64+
'field_type' => 'input',
65+
'editable'=>true,
66+
'type'=>'All',
67+
'description' => 'To receive SMS notifications about your services with us please provide your mobile phone number, starting with country code prefix, ie. +1'
68+
);
69+
$clientfieldsmanager = HBLoader::LoadModel('Clients');
70+
$clientfieldsmanager->addCustomField($client_field);
71+
}
72+
73+
/**
74+
* Send notification to admin.
75+
* HostBill will automatically execute this function if admin needs
76+
* to be notified and is allowed to be notified about something
77+
*
78+
* @param integer $admin_id Administrator ID to notify (see hb_admin_* tables)
79+
* @param string $subject Subject (for sms it may be omited)
80+
* @param string $message Message to send
81+
*/
82+
public function notifyAdmin($admin_id, $subject, $message) {
83+
84+
//1. get related admin details, and check if he have mobile phone added
85+
$editadmins = HBLoader::LoadModel('EditAdmins');
86+
$admin = $editadmins->getAdminDetails($admin_id);
87+
88+
if (!$admin) { //admin not found
89+
return false;
90+
} elseif (!$admin['mobilephone']) { //admin mobile phone not found
91+
return false;
92+
}
93+
94+
//send message
95+
return $this->_send($admin['mobilephone'], $message);
96+
}
97+
98+
/**
99+
* Send notification to client
100+
* HostBill will automatically execute this function if client needs
101+
* to be notified and is allowed to be notified about something
102+
*
103+
*
104+
* @param integer $client_id Client ID to notify (see hb_client_* tables)
105+
* @param string $subject Subject (for sms it may be omited)
106+
* @param string $message Message to send
107+
*/
108+
public function notifyClient($client_id, $subject, $message) {
109+
110+
$mobile_phone_field = $this->configuration['Client Field']['value'];
111+
112+
if (!$mobile_phone_field) { //no client field configured->do not notify clients
113+
return false;
114+
}
115+
116+
//. get client details and check for mobile phone field
117+
$clients = HBLoader::LoadModel('Clients');
118+
$client_details = $clients->getClient($client_id);
119+
120+
if (!$client_details) {
121+
return false;
122+
} elseif (!$client_details[$mobile_phone_field]) {
123+
//no mobile phone num provided
124+
return false;
125+
}
126+
127+
//send message
128+
return $this->_send($client_details[$mobile_phone_field], $message);
129+
}
130+
131+
/**
132+
* Helper function to send actual SMS message to clickatell
133+
* @param string $number Phone number
134+
* @param string $message SMS message to send
135+
*/
136+
private function _send($number, $message) {
137+
$ch = curl_init();
138+
139+
$params = '';
140+
141+
$number = str_replace(array('+', ' ', '-', '.'), "", $number);
142+
143+
$params.="&to={$number}&text=" . urlencode($message);
144+
145+
$url = 'http://api.clickatell.com/http/sendmsg';
146+
$fullparams = "api_id={$this->configuration['Api ID']['value']}&user={$this->configuration['Username']['value']}&password={$this->configuration['Password']['value']}";
147+
$fullparams .= $params;
148+
149+
150+
$chOptions = array(
151+
CURLOPT_URL => $url,
152+
CURLOPT_POST => true,
153+
CURLOPT_RETURNTRANSFER => true,
154+
CURLOPT_SSL_VERIFYPEER => false,
155+
CURLOPT_SSL_VERIFYHOST => false,
156+
CURLOPT_TIMEOUT => 10,
157+
CURLOPT_POSTFIELDS => $fullparams
158+
);
159+
curl_setopt_array($ch, $chOptions);
160+
$response = curl_exec($ch);
161+
curl_close($ch);
162+
163+
if (substr($response, 0, 3) == 'ERR') {
164+
$this->addError(substr($response, 4));
165+
return false;
166+
}
167+
return true;
168+
}
169+
170+
}

0 commit comments

Comments
 (0)