| 1 |
<?php
|
| 2 |
function smssend_help($section) {
|
| 3 |
switch ($section) {
|
| 4 |
case 'admin/help#sendsms':
|
| 5 |
return t('This module helps you to send SMS messages to a selectable drupal user or phone via email gateways.');
|
| 6 |
}
|
| 7 |
}
|
| 8 |
|
| 9 |
function smssend_menu($may_cache) {
|
| 10 |
global $user;
|
| 11 |
$items = array();
|
| 12 |
if (!$may_cache) {
|
| 13 |
$items[] = array(
|
| 14 |
'path' => 'sms',
|
| 15 |
'title' => t('Send SMS'),
|
| 16 |
'description' => t('Send SMS via email gateways for mobile operators.'),
|
| 17 |
'access' => user_access('send sms'),
|
| 18 |
'callback' => 'drupal_get_form',
|
| 19 |
'callback arguments' => array('smssend_send_form'),
|
| 20 |
'required' => TRUE,
|
| 21 |
'type' => MENU_NORMAL_ITEM,
|
| 22 |
);
|
| 23 |
}
|
| 24 |
else {
|
| 25 |
$items[] = array(
|
| 26 |
'path' => 'admin/settings/smssend',
|
| 27 |
'title' => t('SMS Sending form'), // Note that this isn't translated on purpose since it is for the admin
|
| 28 |
'description' => t('SMS sending form settings'),
|
| 29 |
'callback' => 'drupal_get_form',
|
| 30 |
'callback arguments' => 'smssend_admin_settings',
|
| 31 |
'access' => user_access('administer site configuration'),
|
| 32 |
);
|
| 33 |
}
|
| 34 |
return $items;
|
| 35 |
}
|
| 36 |
|
| 37 |
function smssend_admin_settings() {
|
| 38 |
$form = array();
|
| 39 |
$form['smssend_allow_send_user'] = array(
|
| 40 |
'#type' => 'checkbox',
|
| 41 |
'#title' => t('Allow users to send SMS to other users, who entered their phone at their profile'),
|
| 42 |
'#default_value' => variable_get('smssend_allow_send_user', FALSE),
|
| 43 |
'#description' => t('Check this box if you want users to send an SMS to the other users.')
|
| 44 |
);
|
| 45 |
$options = _sendsms_get_all_profile_fields();
|
| 46 |
$options[0] = '<none>';
|
| 47 |
$form['smssend_profile_field'] = array(
|
| 48 |
'#type' => 'select',
|
| 49 |
'#title' => t('Select profile field with phone'),
|
| 50 |
'#options' => $options,
|
| 51 |
'#default_value' => variable_get('smssend_profile_field', array(0 => '<no fields defined>')),
|
| 52 |
'#description' => t('Select user profile field with the phone in correct format (+38xxxxxxxxxx).')
|
| 53 |
);
|
| 54 |
|
| 55 |
return system_settings_form($form);
|
| 56 |
}
|
| 57 |
|
| 58 |
function smssend_send_form() {
|
| 59 |
global $user;
|
| 60 |
$fid = variable_get('smssend_profile_field', 0);
|
| 61 |
if (!$fid) {
|
| 62 |
form_set_error('to', t('SMSSend module is not configured! Please select a profile field with phone in module settings.'));
|
| 63 |
drupal_goto();
|
| 64 |
}
|
| 65 |
$sql = 'SELECT u.uid, u.name, pv.value FROM {users} u INNER JOIN {profile_values} pv ON pv.uid = u.uid WHERE pv.fid = %d AND pv.value != "" ORDER BY u.name';
|
| 66 |
$result = db_query($sql, $fid);
|
| 67 |
while ($u = db_fetch_object($result)) {
|
| 68 |
$options[$u->uid] = $u->name;
|
| 69 |
}
|
| 70 |
$form[] = array(
|
| 71 |
'#type' => 'markup',
|
| 72 |
'#value' => '<big><b><font color=green>'.t('BETA').'</font></b></big><br />',
|
| 73 |
);
|
| 74 |
if ($user->uid == 1) {
|
| 75 |
$form[] = array(
|
| 76 |
'#type' => 'markup',
|
| 77 |
'#value' => '<small>SMS sended from the site: </small>'.variable_get('sms_sended_count', 0),
|
| 78 |
);
|
| 79 |
}
|
| 80 |
if (variable_get('smssend_allow_send_user', 0)) {
|
| 81 |
$form[] = array(
|
| 82 |
'#type' => 'markup',
|
| 83 |
'#value' => '<br />Select user from the list or enter the phone number for sending sms. Users without phone in profile are NOT shown.',
|
| 84 |
);
|
| 85 |
$form['to'] = array(
|
| 86 |
'#type' => 'select',
|
| 87 |
'#title' => t('Select user'),
|
| 88 |
'#options' => $options,
|
| 89 |
'#description' => t('Send SMS to this user'),
|
| 90 |
);
|
| 91 |
}
|
| 92 |
$form['from'] = array(
|
| 93 |
'#type' => 'value',
|
| 94 |
'#value' => $user->uid,
|
| 95 |
);
|
| 96 |
$form['phone'] = array(
|
| 97 |
'#type' => 'textfield',
|
| 98 |
'#title' => 'Enter a phone number',
|
| 99 |
'#description' => t('Enter phone number like this: 38097xxxxxxx')
|
| 100 |
);
|
| 101 |
$form['line'] = array(
|
| 102 |
'#type' => 'markup',
|
| 103 |
'#value' => '<hr />',
|
| 104 |
);
|
| 105 |
$form['body'] = array(
|
| 106 |
'#type' => 'textarea',
|
| 107 |
'#title' => t('Text'),
|
| 108 |
'#required' => TRUE,
|
| 109 |
'#rows' => 15,
|
| 110 |
'#description' => t('Enter an sms body')
|
| 111 |
);
|
| 112 |
$form['submit'] = array(
|
| 113 |
'#type' => 'submit',
|
| 114 |
'#value' => t('Send SMS')
|
| 115 |
);
|
| 116 |
return $form;
|
| 117 |
}
|
| 118 |
|
| 119 |
function smssend_send_form_submit($form_id, $form_values) {
|
| 120 |
if ($form_values['phone'] != '') {
|
| 121 |
if (smsemail_send($form_values['phone'], $form_values['body'], TRUE)) {
|
| 122 |
variable_set('sms_sended_count', variable_get('sms_sended_count', 0) + 1);
|
| 123 |
drupal_set_message(t('SMS successfully sended!'));
|
| 124 |
}
|
| 125 |
else {
|
| 126 |
form_set_error('', t('SMS sending failed'));
|
| 127 |
}
|
| 128 |
}
|
| 129 |
else {
|
| 130 |
$uid_to = $form_values['to'];
|
| 131 |
$uid_from = $form_values['from'];
|
| 132 |
$fid = variable_get('smssend_profile_field', 0);
|
| 133 |
$user_from = user_load(array('uid' => $uid_from));
|
| 134 |
$user_to = user_load(array('uid' => $uid_to));
|
| 135 |
if ($uid_to == 0 || $uid_from == 0) {
|
| 136 |
form_set_error('to', t('Shit happens.'));
|
| 137 |
return;
|
| 138 |
}
|
| 139 |
if ($fid == 0) {
|
| 140 |
form_set_error('to', t('SMSSend module is not configured! Please select a profile field with phone in module settings.'));
|
| 141 |
return;
|
| 142 |
}
|
| 143 |
$sql = 'SELECT value FROM {profile_values} WHERE uid = %d AND fid = %d';
|
| 144 |
$result = db_query($sql, $uid_to, $fid);
|
| 145 |
$phone = db_fetch_object($result);
|
| 146 |
if (smsemail_send($phone->value, $user_from->name.': '.$form_values['body'], TRUE)) {
|
| 147 |
variable_set('sms_sended_count', variable_get('sms_sended_count', 0) + 1);
|
| 148 |
drupal_set_message(t('SMS successfully sended to '.$user_to->name));
|
| 149 |
}
|
| 150 |
else {
|
| 151 |
form_set_error('', t('SMS sending to %user% failed', array('%user%' => $user_to->name)));
|
| 152 |
}
|
| 153 |
}
|
| 154 |
}
|
| 155 |
|
| 156 |
function smssend_perm() {
|
| 157 |
return array('send sms');
|
| 158 |
}
|
| 159 |
|
| 160 |
/**
|
| 161 |
* Returns all profile fields
|
| 162 |
*/
|
| 163 |
function _sendsms_get_all_profile_fields() {
|
| 164 |
$sql = 'SELECT fid, title FROM {profile_fields}';
|
| 165 |
$result = db_query($sql);
|
| 166 |
$options = array();
|
| 167 |
while ($row = db_fetch_object($result)) {
|
| 168 |
$options[$row->fid] = $row->title;
|
| 169 |
}
|
| 170 |
return $options;
|
| 171 |
}
|
| 172 |
?>
|