| 1 |
<?php
|
| 2 |
// $Id: citizenspeak.module,v 1.14 2005/11/12 18:02:09 georgehotelling Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file The main CitizenSpeak module file
|
| 6 |
*
|
| 7 |
* @author George Hotelling <http://george.hotelling.net/>
|
| 8 |
*
|
| 9 |
* @todo Create SimpleTest tests.
|
| 10 |
*
|
| 11 |
* @todo Create CiviCRM integration
|
| 12 |
*/
|
| 13 |
|
| 14 |
require_once(dirname(__FILE__) . '/citizenspeak.node.php');
|
| 15 |
require_once(dirname(__FILE__) . '/citizenspeak.theme.php');
|
| 16 |
require_once(dirname(__FILE__) . '/citizenspeak.lib.php');
|
| 17 |
require_once(dirname(__FILE__) . '/citizenspeak.reports.php');
|
| 18 |
require_once(dirname(__FILE__) . '/citizenspeak.user.php');
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Implementation of hook_help().
|
| 22 |
*
|
| 23 |
* @param $section
|
| 24 |
* Section which section of the site we're displaying help
|
| 25 |
* @return
|
| 26 |
* Help text for section
|
| 27 |
*/
|
| 28 |
|
| 29 |
function citizenspeak_help($section) {
|
| 30 |
switch ($section) {
|
| 31 |
case 'admin/modules#description':
|
| 32 |
// This description is shown in the listing at admin/modules.
|
| 33 |
return t('Allows email action campaigns to be created');
|
| 34 |
case 'node/add#citizenspeak':
|
| 35 |
// This description shows up when users click "create content."
|
| 36 |
return t('A CitizenSpeak campaign is a form that allows people to send pre-made emails to a specific address.');
|
| 37 |
}
|
| 38 |
} // function citizenspeak_help
|
| 39 |
|
| 40 |
/**
|
| 41 |
* Implementation of hook_menu().
|
| 42 |
*
|
| 43 |
* Creates:
|
| 44 |
* A menu item to add a campaign at node/add/citizenspeak
|
| 45 |
* A local task for citizenspeak_report() at node/###/report
|
| 46 |
* A menu callback for citizenspeak_send() at node/###/send
|
| 47 |
* A menu callback for citizenspeak_thank_you() at node/###/thank_you
|
| 48 |
*/
|
| 49 |
function citizenspeak_menu($may_cache) {
|
| 50 |
$items = array();
|
| 51 |
|
| 52 |
if ($may_cache) {
|
| 53 |
$items[] = array('path' => 'node/add/citizenspeak',
|
| 54 |
'title' => t('citizenspeak campaign'),
|
| 55 |
'access' => user_access('create campaigns'));
|
| 56 |
$items[] = array('path' => 'citizenspeak',
|
| 57 |
'title' => t('all citizenspeak campaigns'),
|
| 58 |
'access' => user_access('access content'),
|
| 59 |
'callback' => 'citizenspeak_page',
|
| 60 |
'type' => MENU_SUGGESTED_ITEM);
|
| 61 |
}
|
| 62 |
else {
|
| 63 |
if (arg(0) == 'node' && is_numeric(arg(1))) {
|
| 64 |
$node = node_load(array('nid' => arg(1)));
|
| 65 |
if ($node->type == 'citizenspeak') {
|
| 66 |
global $user;
|
| 67 |
|
| 68 |
$items[] = array('path' => 'node/'.arg(1).'/report',
|
| 69 |
'title' => t('view campaign reports'),
|
| 70 |
'callback' => 'citizenspeak_report',
|
| 71 |
'callback arguments' => arg(1),
|
| 72 |
'access' => ($node->uid == $user->uid) && user_access('collect contact information'),
|
| 73 |
'weight' => 3,
|
| 74 |
'type' => MENU_LOCAL_TASK);
|
| 75 |
|
| 76 |
$items[] = array('path' => 'node/'.arg(1).'/send',
|
| 77 |
'title' => t('send citizenspeak response'),
|
| 78 |
'callback' => 'citizenspeak_send',
|
| 79 |
'callback arguments' => arg(1),
|
| 80 |
'access' => user_access('participate in campaigns'),
|
| 81 |
'type' => MENU_CALLBACK);
|
| 82 |
|
| 83 |
$items[] = array('path' => 'node/'.arg(1).'/thank_you',
|
| 84 |
'title' => t('thank you'),
|
| 85 |
'callback' => 'citizenspeak_thank_you',
|
| 86 |
'callback arguments' => arg(1),
|
| 87 |
'access' => user_access('participate in campaigns'),
|
| 88 |
'type' => MENU_CALLBACK);
|
| 89 |
}
|
| 90 |
}
|
| 91 |
}
|
| 92 |
|
| 93 |
return $items;
|
| 94 |
} // function citizenspeak_menu
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Implementation of hook_perm().
|
| 98 |
*
|
| 99 |
* @return An array of valid permissions for the citizenspeak module
|
| 100 |
*/
|
| 101 |
function citizenspeak_perm() {
|
| 102 |
return array('create campaigns', 'edit own campaigns', 'participate in campaigns', 'collect contact information', 'customize thank you page', 'administer citizenspeak');
|
| 103 |
} // function citizenspeak_perm
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Implementation of hook_link().
|
| 107 |
*/
|
| 108 |
function citizenspeak_link($type, $node = 0, $main) {
|
| 109 |
$links = array();
|
| 110 |
|
| 111 |
if ($type == 'node' && $node->type == 'citizenspeak') {
|
| 112 |
// Don't display a redundant edit link if they are node administrators.
|
| 113 |
if (citizenspeak_access('update', $node) && !user_access('administer nodes')) {
|
| 114 |
$links[] = l(t('edit this citizenspeak campaign'), "node/$node->nid/edit");
|
| 115 |
}
|
| 116 |
}
|
| 117 |
|
| 118 |
return $links;
|
| 119 |
} // function citizenspeak_link
|
| 120 |
|
| 121 |
define("CITIZENSPEAK_DEFAULT_REMINDER_SUBJECT", t('Reminder about your campaign'));
|
| 122 |
define("CITIZENSPEAK_DEFAULT_REMINDER_TEMPLATE", t('Your campaign "%title" has received %count responses. You can download reports about your campaign at %url'));
|
| 123 |
|
| 124 |
/**
|
| 125 |
* Implementation of hook_settings().
|
| 126 |
*
|
| 127 |
* Module configuration settings
|
| 128 |
* @return settings HTML or deny access
|
| 129 |
*/
|
| 130 |
function citizenspeak_settings() {
|
| 131 |
$output = '';
|
| 132 |
|
| 133 |
$output .= form_checkbox(t('Turn on debugging?'), 'citizenspeak_debug', 1, variable_get('citizenspeak_debug', 0), t('When debugging is on, campaign messages will be displayed on the screen instead of emailed to the recipient.'));
|
| 134 |
|
| 135 |
$output .= form_textarea(t('Email Signature'), 'citizenspeak_signature', variable_get('citizenspeak_signature', ""), 60, 6, t('If set, "-- " and this message will be added to the end of every campaign message sent. These variables will be replaced with information about the campaign: %title, %url, %nid'));
|
| 136 |
|
| 137 |
$output .= form_checkbox(t('Show recipient?'), 'citizenspeak_show_to', 1, variable_get('citizenspeak_show_to', 1), t('Should the campaign display where it will be sent?'));
|
| 138 |
|
| 139 |
// Reminder email settings
|
| 140 |
$reminders = form_checkbox(t('Remind user to download report after 15 participants'), 'citizenspeak_remind_15', 1, variable_get('citizenspeak_remind_15', 0));
|
| 141 |
$reminders .= form_checkbox(t('Remind user to download report after 50 participants'), 'citizenspeak_remind_50', 1, variable_get('citizenspeak_remind_50', 0));
|
| 142 |
$reminders .= form_checkbox(t('Remind user to download report after 100 participants'), 'citizenspeak_remind_100', 1, variable_get('citizenspeak_remind_100', 0));
|
| 143 |
$reminders .= form_textfield(t('Reminder email subject'), 'citizenspeak_reminder_subject', variable_get('citizenspeak_reminder_subject', CITIZENSPEAK_DEFAULT_REMINDER_SUBJECT), '', '');
|
| 144 |
$reminders .= form_textarea(t('Reminder email template'), 'citizenspeak_reminder_template', variable_get('citizenspeak_reminder_template', CITIZENSPEAK_DEFAULT_REMINDER_TEMPLATE), 60, 6, t('These variables will be replaced with information about the campaign: %title, %count, %url'));
|
| 145 |
|
| 146 |
$output .= form_group(t("Reminder Emails"), $reminders, t("Campaign creators can be sent emails to remind them to download their reports."));
|
| 147 |
|
| 148 |
return $output;
|
| 149 |
} // function citizenspeak_settings
|
| 150 |
|
| 151 |
/**
|
| 152 |
* Implementation of hook_user().
|
| 153 |
*
|
| 154 |
* @param $op
|
| 155 |
* What kind of action is being performed
|
| 156 |
* @param $edit
|
| 157 |
* The array of form values submitted by the user
|
| 158 |
* @param $user
|
| 159 |
* The user object on which the operation is being performed
|
| 160 |
* @param $category
|
| 161 |
* The active category of user information being edited
|
| 162 |
* @return see hook_user() docs.
|
| 163 |
*/
|
| 164 |
function citizenspeak_user($op, &$edit, &$user, $category = false) {
|
| 165 |
$function = "citizenspeak_user_" . $op;
|
| 166 |
if (function_exists($function)) {
|
| 167 |
return call_user_func_array($function, array($edit, $user, $category));
|
| 168 |
}
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Implementation of hook_block().
|
| 173 |
*
|
| 174 |
* @param $op
|
| 175 |
* The operation from the URL
|
| 176 |
* @param $delta
|
| 177 |
* Offset
|
| 178 |
* @return block list array or block content array
|
| 179 |
*/
|
| 180 |
function citizenspeak_block($op = 'list', $delta = 0) {
|
| 181 |
// listing of blocks, such as on the admin/block page
|
| 182 |
if ($op == "list") {
|
| 183 |
$block[0]["info"] = t('popular citizenspeak campaigns');
|
| 184 |
return $block;
|
| 185 |
}
|
| 186 |
else {
|
| 187 |
$most_popular = db_query(db_rewrite_sql('SELECT n.nid, n.title, COUNT(*) AS participants FROM {node} n LEFT JOIN citizenspeak_participants AS cp ON n.nid = cp.nid WHERE n.type = \'citizenspeak\' AND DATE_SUB(CURDATE(), INTERVAL 1 MONTH) < cp.sent_at AND n.promote = 1 AND n.status = 1 GROUP BY n.nid ORDER BY participants DESC LIMIT 5'));
|
| 188 |
if (db_num_rows($most_popular)) {
|
| 189 |
$block['subject'] = t('popular campaigns');
|
| 190 |
|
| 191 |
while($campaign = db_fetch_object($most_popular)) {
|
| 192 |
$campaigns[] = l($campaign->title, "node/$campaign->nid");
|
| 193 |
}
|
| 194 |
|
| 195 |
$block['content'] = theme('item_list', $campaigns) . l(t("all campaigns"), "citizenspeak");
|
| 196 |
return $block;
|
| 197 |
}
|
| 198 |
}
|
| 199 |
} // end citizenspeak_block
|
| 200 |
|
| 201 |
/**
|
| 202 |
* Handles sending the campaign participation
|
| 203 |
*
|
| 204 |
* @param $nid
|
| 205 |
* Node ID of the campaign
|
| 206 |
*/
|
| 207 |
function citizenspeak_send($nid) {
|
| 208 |
$edit = $_POST['edit'];
|
| 209 |
|
| 210 |
// Validate form
|
| 211 |
if (!_citizenspeak_validate_participation($edit)) {
|
| 212 |
drupal_goto('node/'.$nid);
|
| 213 |
}
|
| 214 |
|
| 215 |
// Log response
|
| 216 |
$id = db_next_id('citizenspeak_participants');
|
| 217 |
db_query("INSERT INTO {citizenspeak_participants} (nid, id, name, organization, email, address, city, state, zip, phone, fax, personal_statement, sent_at) VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%05d', '%s', '%s', '%s', NOW())", $nid, $id, $edit['name'], $edit['organization'], $edit['email'], $edit['address'], $edit['city'], $edit['state'], $edit['zip'], $edit['phone'], $edit['fax'], $edit['personal_statement']);
|
| 218 |
|
| 219 |
// Make email content
|
| 220 |
$node = node_load(array("nid" => $nid));
|
| 221 |
$participant = db_fetch_object(db_query("SELECT * FROM {citizenspeak_participants} WHERE id = %d", $id));
|
| 222 |
$message = theme("citizenspeak_message", $node, $participant);
|
| 223 |
$headers = theme("citizenspeak_message_headers", $node, $participant);
|
| 224 |
|
| 225 |
// Send reminder emails if necessary
|
| 226 |
_citizenspeak_send_reminders($node);
|
| 227 |
|
| 228 |
// Call hook_citizenspeak_send
|
| 229 |
module_invoke_all("citizenspeak_send", $node, $participant);
|
| 230 |
|
| 231 |
// Send email (or display debugging page)
|
| 232 |
if (!variable_get('citizenspeak_debug', 0)) {
|
| 233 |
mail($node->email_recipients, $node->title, $message, $headers);
|
| 234 |
// Redirect to thank you page
|
| 235 |
drupal_goto("node/". $nid."/thank_you");
|
| 236 |
}
|
| 237 |
else {
|
| 238 |
echo theme('page', theme('citizenspeak_debug_page', $node, $message, $headers));
|
| 239 |
}
|
| 240 |
}
|
| 241 |
|
| 242 |
/**
|
| 243 |
* Displays the campaign thank you page
|
| 244 |
*
|
| 245 |
* @param $nid
|
| 246 |
* Node ID of the campaign
|
| 247 |
*/
|
| 248 |
function citizenspeak_thank_you($nid) {
|
| 249 |
$node = node_load(array("nid" => $nid));
|
| 250 |
$owner = user_load(array("uid" => $node->uid));
|
| 251 |
if ($owner->use_redirect) {
|
| 252 |
header("Location: ". $node->redirect_url);
|
| 253 |
exit();
|
| 254 |
}
|
| 255 |
$output = theme('citizenspeak_thank_you', $node, $owner);
|
| 256 |
echo theme('page', $output);
|
| 257 |
}
|
| 258 |
|
| 259 |
/**
|
| 260 |
* Displays all published campaigns on the site
|
| 261 |
*/
|
| 262 |
function citizenspeak_page() {
|
| 263 |
drupal_set_title(t('all citizenspeak campaigns'));
|
| 264 |
$result = pager_query(db_rewrite_sql("SELECT n.nid, n.sticky, n.created FROM {node} n WHERE n.promote = 1 AND n.status = 1 AND n.type = 'citizenspeak' ORDER BY n.sticky DESC, n.created DESC"), variable_get('default_nodes_main', 10));
|
| 265 |
|
| 266 |
$output = '';
|
| 267 |
if (db_num_rows($result)) {
|
| 268 |
while ($node = db_fetch_object($result)) {
|
| 269 |
$output .= node_view(node_load(array('nid' => $node->nid)), 1);
|
| 270 |
}
|
| 271 |
$output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
|
| 272 |
}
|
| 273 |
print theme('page', $output);
|
| 274 |
}
|
| 275 |
?>
|