/[drupal]/contributions/modules/SMSPlug/smsblaster.module
ViewVC logotype

Contents of /contributions/modules/SMSPlug/smsblaster.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Tue Apr 28 12:48:15 2009 UTC (6 months, 3 weeks ago) by marcob
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, DRUPAL-6--2-0, HEAD
File MIME type: text/x-php
Initial import
1 <?php
2 // $Id:$
3 /**
4 * @author Marco Bischoff
5 * @file
6 *
7 */
8
9 //TODO: send sms to more than one number
10
11 /**
12 * Global variable which contains the default value of max numbers of sms
13 *
14 */
15 define('smsplug_maxsmspermonth', 'five');
16
17 /**
18 * Implementation of hook_help().
19 */
20 function smsplug_help($path, $arg)
21 {
22 $output = '';
23 include_once drupal_get_path('module', 'smsplug').'/class/sms.php';
24
25 $oSMS = new SMS(variable_get('smsplug_user', ''),variable_get('smsplug_password', ''));
26 $aResult = $oSMS->showCredits();
27 $result = $aResult['data'][0];
28
29 switch ($path) {
30 case "admin/help#smsplug":
31 $output = '<p>'. t("Connects to the SMSBlaster server and allows to send via the service sms.") .'</p>';
32 break;
33 case "admin/settings/smsplug":
34 $output = '<p>' . t('Currently you have total SMS to be send: ') .$result. '</p>';
35 break;
36 }
37 return $output;
38 }
39
40 /**
41 * Implementation of hook_perm().
42 */
43 function smsplug_perm()
44 {
45 return array('access smsplug', 'administer smsplug');
46 }
47
48 /**
49 * Implementation of hook_form().
50 *
51 * This hook displays the form necessary to send the sms.
52 */
53 function smsplug_smsform()
54 {
55 global $user;
56
57 $sName = t(' by: ').$user->name;
58
59 $iLength = 160 - strlen($sName);
60
61 $form['smsplug_number']=array(
62 '#type'=>'textfield',
63 '#title'=>t('Handynumber'),
64 '#description' => t('079 xxx xx xx or name of the user, if they had entered a handy number.'),
65 //'#description' => t('079 xxx xx xx or name of the user, if they had entered a handy number. For more than one number use the semicolon.'),
66 '#size'=>25,
67 '#weight' => 0,
68 '#disabled' => !smsplug_checkUserSendSMS(),
69 '#autocomplete_path' =>'smsplug/autocomplete'
70 );
71
72 $form['smsplug_message'] = array (
73 '#title' => t('Message'),
74 '#type' => 'textarea',
75 '#description' => t('Here you can enter the SMS. Maximum length of each SMS is: ') .$iLength,
76 '#cols' => 10,
77 '#rows' => 3,
78 '#disabled' => !smsplug_checkUserSendSMS(),
79 '#default_value' => variable_get('smsplug_cutsms_'.$user->uid, ''),
80 '#resizable' => TRUE
81 );
82 $form['submit'] = array (
83 '#type' => 'submit',
84 '#value' => t('Submit'),
85 '#submit' => array('smsplug_my_submit')
86 );
87
88 return $form;
89 }
90
91 /**
92 * Implementation of hook_block().
93 */
94 function smsplug_block($op = 'list', $delta = 0)
95 {
96 global $user;
97
98 switch($op) {
99 case 'list':
100 $blocks[0]["info"] = t('SMSPlug');
101 break;
102 case 'view':
103 if(user_access('access smsplug', $account) == 1)
104 {
105 $blocks['subject'] = t('SMSPlug');
106 $iMaxSMS = variable_get('smsplug_maxsmspermonth', '0');
107
108 $iUserMaxSMS = $iMaxSMS - smsplug_currentSMSByUser();
109
110 if(smsplug_checkUserSendSMS() == true)
111 {
112 $blocks['content'] = '<p>'.t('Currently you have total SMS to be send: ') .$iUserMaxSMS. '</p>' . drupal_get_form("smsplug_smsform");
113 }
114 else
115 {
116 $blocks['content'] = '<p>' . t('For this month no more SMS available') . '</p>' . drupal_get_form("smsplug_smsform");
117 }
118 }
119 break;
120 default:
121 $block['subject'] = "SMSPlug";
122 $block['content'] = $block_content;
123 break;
124 }
125 return $blocks;
126 }
127 /**
128 * Implementation of hook_admin().
129 */
130 function smsplug_admin()
131 {
132 $form['smsplug_sms'] = array(
133 '#type' => 'fieldset',
134 '#title' => t('SMSPlug settings'),
135 '#weight' => 0,
136 '#collapsible' => TRUE,
137 '#collapsed' => FALSE,
138 );
139
140 $form['smsplug_sms']['smsplug_maxsmspermonth'] = array (
141 '#type' => 'select',
142 '#title' => t('Numbers of SMS per month'),
143 '#default_value' => variable_get('smsplug_maxsmspermonth', '5'),
144 '#options' => array(
145 '5' => '5',
146 '10' => '10',
147 '15' => '15',
148 '20' => '20',
149 '25' => '25',
150 '30' => '30'
151 ),
152 '#description' => t('Setting for the maximum numbers of SMS which can be send during a month pro user'),
153 '#required' => TRUE
154 );
155
156 $form['smsplug_sms']['smsplug_originator'] = array (
157 '#type' => 'textfield',
158 '#title' => t('Originator'),
159 '#default_value' => variable_get('site_name', 'Originator'),
160 '#description' => t('This is value will be displayed instead of the handynumber'),
161 '#required' => TRUE
162 );
163
164 $form['smsplug_sms']['smsplug_user'] = array (
165 '#type' => 'textfield',
166 '#title' => t('Username for SMSPlug'),
167 '#default_value' => variable_get('smsplug_user', 'XXXXXXXXXXXX'),
168 '#description' => t('The userkey, which will be provided by the smsblaster.ch website.'),
169 '#required' => TRUE
170 );
171
172 $form['smsplug_sms']['smsplug_password'] = array (
173 '#type' => 'password_confirm',
174 '#description' => t('Password to login on the SMSBlaster website'),
175 '#size' => 15,
176 '#required' => TRUE
177 );
178
179 if(module_exists('dblog'))
180 {
181 $form['smsplug_log'] = array(
182 '#type' => 'fieldset',
183 '#title' => t('SMSPlug log settings'),
184 '#weight' => 0,
185 '#collapsible' => TRUE,
186 '#collapsed' => TRUE,
187 );
188
189 $form['smsplug_log']['smsplug_loguser'] = array (
190 '#type' => 'checkbox',
191 '#title' => t('Log user'),
192 '#description' => t('Log the user which has an SMS send'),
193 '#default_value' => variable_get('smsplug_loguser',0)
194 );
195
196 $form['smsplug_log']['smsplug_logmessage'] = array (
197 '#type' => 'checkbox',
198 '#title' => t('Log SMS message'),
199 '#description' => t('Log the send messages from each user'),
200 '#default_value' => variable_get('smsplug_logmessage',0)
201 );
202
203 $form['smsplug_log']['smsplug_lognumber'] = array (
204 '#type' => 'checkbox',
205 '#title' => t('Log handy number'),
206 '#description' => t('Log the handy number to were the SMS is send'),
207 '#default_value' => variable_get('smsplug_lognumber',0)
208 );
209
210 $form['smsplug_log']['smsplug_satus'] = array (
211 '#type' => 'checkbox',
212 '#title' => t('Log SMS status'),
213 '#description' => t('Log if the SMS was send successful'),
214 '#default_value' => variable_get('smsplug_satus',0)
215 );
216 }
217 return system_settings_form($form);
218 }
219
220 /**
221 * Implementation of hook_menu().
222 */
223 function smsplug_menu()
224 {
225 $item = array();
226
227 $item['admin/settings/smsplug'] = array(
228 'title' => 'SMSPlug',
229 'description' => 'Make here your settings for the SMSPlug (username, password, max. sms per month)',
230 'page callback' => 'drupal_get_form',
231 'page arguments' => array('smsplug_admin'),
232 'access arguments' => array('access administration pages'),
233 'access' => TRUE,
234 'type' => MENU_NORMAL_ITEM,
235 );
236
237 $item['smsplug/autocomplete'] = array(
238 'path' => 'smsplug/autocomplete',
239 'title' => t('user autcomplete'),
240 'page callback' => 'smsplug_autocomplete',
241 'access callback' => TRUE,
242 'access arguments' => array('access content'),
243 'type' => MENU_CALLBACK
244 );
245 return $item;
246 }
247
248 /**
249 * Implementation of autocomplete.
250 */
251 function smsplug_autocomplete($string = "")
252 {
253 $matches = array();
254
255 if ($string)
256 {
257 $string = split(";", $string);
258 foreach($string as $key) {
259 $query = db_query(db_rewrite_sql("
260 select users.name as username, pv.value as handynumber
261 from
262 profile_values as pv, profile_fields as ps, users as users
263 where lower(ps.name) = 'profile_handynumber' and pv.value !='' and ps.fid = pv.fid and pv.uid=users.uid and
264 lower(users.name) like '".$key."%'
265 "));
266
267 while($user = db_fetch_object($query))
268 {
269 $matches[$user->handynumber] = check_plain($user->username) . " (" . check_plain($user->handynumber) . ")";
270 }
271 }
272 }
273 drupal_json($matches);
274 }
275
276 /**
277 * Implementation of submit.
278 */
279 function smsplug_my_submit($form, &$form_state)
280 {
281 global $user;
282
283 $sName = t(' by: ').$user->name;
284
285 $content = $form_state['values']['smsplug_message'];
286 $content .= $sName;
287
288 $iLength = 160 - strlen($content);
289
290 if(smsplug_checkUserSendSMS() && $form_state['values']['smsplug_number'] != "")
291 {
292 if($iLength >= 0)
293 {
294 variable_del('smsplug_cutsms_' . $user->uid);
295 include_once drupal_get_path('module', 'smsplug').'/class/sms.php';
296
297 $oSMS = new SMS(variable_get('smsplug_user', ''),variable_get('smsplug_password', ''));
298
299 $oSMS->setOriginator(variable_get('smsplug_originator', 'not set'));
300
301 $sNumbers = $form_state['values']['smsplug_number'];
302 $aNumbers = split(";", $sNumbers);
303
304 foreach($aNumbers as $iNumber) {
305 $oSMS->addRecipient($iNumber);
306 }
307
308 $oSMS->setContent($content);
309
310 if(variable_get('smsplug_loguser','') == 1)
311 {
312 watchdog('smsplug', t('Following user has send a sms: ') . $user->name, NULL, WATCHDOG_NOTICE, NULL);
313 }
314
315 if(variable_get('smsplug_logmessage','') == 1)
316 {
317 watchdog('smsplug', t('Following message was send: ') . $form_state['values']['smsplug_message'], NULL, WATCHDOG_NOTICE, NULL);
318 }
319
320 if(variable_get('smsplug_lognumber','') == 1)
321 {
322 watchdog('smsplug', t('The user send the message to the following number: ') . $form_state['values']['smsplug_number'], NULL, WATCHDOG_NOTICE, NULL);
323 }
324
325 $aResult = $oSMS->sendSMS();
326 $result = $aResult['data'][0];
327
328 if($result == "Ok")
329 {
330 $now = time();
331 $yearmonth = date('Ym', $now);
332
333 $iSendSMS = smsplug_currentSMSByUser();
334 foreach($aNumbers as $iNumber) {
335 ++$iSendSMS;
336 }
337
338 $smsBlasterArray = array(''.$yearmonth.'' => array("smscount" => $iSendSMS, "date" => $now ));
339
340 variable_set('smsplug_' . $user->uid, $smsBlasterArray);
341
342 drupal_set_message(t('SMS was sent sucessful'),'status', TRUE);
343 }
344 else
345 {
346 drupal_set_message(t('SMSPlug generated an error: ').$result, 'error', TRUE);
347 }
348
349 if(variable_get('smsplug_logstatus','') == 1)
350 {
351 watchdog('smsplug', t('SMSPlug had the following sendSMS status: ') . $result, NULL, WATCHDOG_NOTICE, NULL);
352 }
353 }
354 else
355 {
356 smsplug_cutSMS($form_state['values']['smsplug_message']);
357 drupal_set_message(t('Your SMS has more than 160 characters, therefore it will be cut of, plesae resend it'), 'warning', TRUE);
358 }
359 }
360 }
361
362 /**
363 * Helper function for storing the cutted sms
364 * @return void
365 */
366 function smsplug_cutSMS($content)
367 {
368 global $user;
369 $addContent = t(' by: ') . $user->name;
370 $maxLength = 160 - strlen($addContent);
371 $newContent = substr($content, 0, $maxLength);
372
373 variable_set('smsplug_cutsms_' . $user->uid, $newContent);
374 }
375
376 /**
377 * Helper function to get the numbers of sms to be used by the user
378 * @return integer sms count
379 */
380 function smsplug_currentSMSByUser()
381 {
382 global $user;
383
384 $now = time();
385 $yearmonth = date('Ym', $now);
386
387 $aCurrentSMS = variable_get('smsplug_' . $user->uid, 0);
388 $aCurrentSMS = $aCurrentSMS[$yearmonth];
389 $iCurrentSMS = $aCurrentSMS["smscount"];
390
391 return $iCurrentSMS;
392
393 }
394
395 /**
396 * Helper function to determine if a user can send sms or not
397 * @return boolean true | false
398 */
399 function smsplug_checkUserSendSMS()
400 {
401 global $user;
402
403 $bool = false;
404
405 $iMaxSMS = variable_get('smsplug_maxsmspermonth',0);
406
407 $iCurrentSMS = smsplug_currentSMSByUser();
408
409 if($iCurrentSMS < $iMaxSMS)
410 {
411 $bool = true;
412 }
413
414 if($iCurrentSMS > $iMaxSMS)
415 {
416 $bool = false;
417 }
418
419 return $bool;
420 }

  ViewVC Help
Powered by ViewVC 1.1.2