-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf_reports.module
276 lines (226 loc) · 8.92 KB
/
sf_reports.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
// $Id$
/**
* Implementation of hook_perm(). Registers necessary user role permissions.
*/
function sf_reports_perm() {
return array('view presidents report - public', 'view presidents report - private');
}
/**
* Implementation of hook_menu(). Registers the page where the list of presidents will be displayed.
*/
function sf_reports_menu() {
$items = array();
$items['sfreport/presidents'] = array(
'title' => t('Presidents'),
'page callback' => 'sf_reports_presidents',
'page arguments' => array(2),
'access callback' => 'sf_reports_presidents_access',
'description' => t('Displays list of presidents in the current year'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_access(). Sets access to the module node.
*/
function sf_reports_presidents_access() {
return user_access('view presidents report - public') || user_access('view presidents report - private');
}
/**
* Implementation of hook_form().
*
* Creates form which enables users to specify database query.
*/
function sf_reports_form($form_state) {
// getting default date information based on the current date
list ($datefrom, $dateto, $yearfrom) = sf_reports_datespan();
$form['date'] = array(
'#type' => 'fieldset',
'#title' => t('List settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['date']['date_from'] = array(
'#type' => 'date',
'#title' => t('Start date'),
'#description' => t('Please specify the start date'),
'#default_value' => array('year' => $yearfrom, 'month' => 9, 'day' => 1),
);
$form['date']['date_to'] = array(
'#type' => 'date',
'#title' => t('End date'),
'#description' => t('Please specify the end date'),
'#default_value' => array('year' => ($yearfrom + 1), 'month' => 8, 'day' => 31),
);
$form['date']['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
/**
* Implementation of hook_submit() for sf_reports_form().
*
* Handles sf_reports_form($form_state) form submission
*/
function sf_reports_form_submit($form, &$form_state) {
drupal_set_message(t('The form has been submitted.'));
$_SESSION['date_from_s'] = $form_state['values']['date_from'];
$_SESSION['date_to_s'] = $form_state['values']['date_to'];
$_SESSION['submitted'] = TRUE;
}
/**
* Calculates dates used for database query based on the current server date.
*
* @return three-value array
* $datefrom - string in format YYYY-MM-DD
* $dateto - string in format YYYY-MM-DD
* $yearfrom - int
*/
function sf_reports_datespan() {
// Preparing current date
$year_this = date("Y");
$month_this = (int) date("m");
// Calculating date parameters for query, so the appropriate records
// are dispayed to the user.
if ($month_this < 9) {
$datefrom = ($year_this - 1).'-09-01';
$dateto = $year_this.'-08-31';
$yearfrom = ($year_this - 1);
}
else {
$datefrom = $year_this.'-09-01';
$dateto = ($year_this + 1).'-08-31';
$yearfrom = $year_this;
}
return array ($datefrom, $dateto, $yearfrom);
}
/**
* Implementation of the callback function to $items['sfreport/presidents']
*
* This function does all the listing of presidents for the current year/date.
*/
function sf_reports_presidents($html = FALSE) {
// If requested URL is .../sfreport/presidents/html, set $html to TRUE
if ($html === 'html') {
$html = TRUE;
}
else {
$html = FALSE;
}
// if the user has the necessary permission to see private list of presidents, set $private to TRUE.
if (user_access('view presidents report - private')) {
$private = TRUE;
}
else {
$private = FALSE;
}
// connecting to Salesforce
try {
$sf = salesforce_api_connect();
if (!$sf) {
$link = l('Please verify that you have completed your SalesForce credentials', SALESFORCE_PATH_ADMIN);
drupal_set_message(t('Unable to connect to SalesForce. !link', array('!link' => $link)), 'error' );
return;
}
}
catch (Exception $e) {
DrupalSalesforce::watchdog(SALESFORCE_LOG_SOME, 'Unable to establish Salesforce connection while issuing describeSObjects API call.', array(), WATCHDOG_ERROR);
}
// if the form has not been submitted, use the default
// dates returned by sf_reports_datespan()
if (!$_SESSION['submitted']) {
list ($datefrom, $dateto) = sf_reports_datespan();
$_SESSION['date_from_s'] = $datefrom;
$_SESSION['date_to_s'] = $dateto;
}
else { // form has been submitted - process data entered by the user
$form_date_from = $_SESSION['date_from_s'];
$form_date_to = $_SESSION['date_to_s'];
// dates correction for db query (adding leading zeros, ...)
$datefrom = $form_date_from['year']."-".str_pad($form_date_from['month'], 2, "0", STR_PAD_LEFT)."-".str_pad($form_date_from['day'], 2, "0", STR_PAD_LEFT);
$dateto = $form_date_to['year']."-".str_pad($form_date_to['month'], 2, "0", STR_PAD_LEFT)."-".str_pad($form_date_to['day'], 2, "0", STR_PAD_LEFT);
$_SESSION['submitted'] = FALSE; // unset flag
}
if ($html) { // if preparing HTML output, add some CSS
$css = '<style type="text/css">
table.sfreport-results-list {
font-size: 12px;
border-collapse: separate;
border-spacing: 4px;
}
div.sfreport-table-title {
font-size: 13px;
}
</style>';
$css.= drupal_get_css();
$output.= $css;
}
if (!$html) { // if not preparing HTML, show the full form
$output.= drupal_get_form('sf_reports_form');
}
// Salesforce query
$query = "SELECT Id, Contact__c, Contact_name__c, Contact_email__c, Contact_mobile__c, Organisation_name__c, Organisation_email__c, date_end__c, date_start__c, Name FROM Roles_del__c WHERE Name like '%President%' AND Organisation_name__c like '%ORGNAME%' AND date_start__c >= $datefrom AND date_end__c <= $dateto";
$result = $sf->client->query($query);
if ($result->records[0]->Id == NULL) { // if there's no result...
if ($html) {
return 0;
}
else {
drupal_set_message(t('There are no records in the specified time interval.'), 'warning');
}
}
else { // else, prepare output HTML string
$output.= '<div class="sfreport-table-title"><br>Listing presidents between <b>'.$datefrom. '</b> and <b>'.$dateto.'</b>.';
if ($html) {
global $base_url;
$link = $base_url.'/sfreport/presidents';
$output.= '<a href="'.$link.'" target="_parent" title="specify list settings">[more]</a>';
}
$output.='</div>';
// Building output table
$output.= '<br><table class="sfreport-results-list">
<tr>
<td><b>Organisation</b></td>
<td><b>Last name, First name</b></td>
<td><b>Start date</b></td>
<td><b>End date</b></td>';
// Private list of presidents shows email addresses.
if ($private) {
$output.= '<td><b>E-mail</b></td>';
$output.= '<td><b>Phone</b></td>';
}
else {
$output.= '<td><b>E-mail</b></td>';
}
$output.= '</tr>';
$i = 0;
while (($result->records[$i]->Id != NULL)) {
// @ to [at] change in email addresses
//$result->records[$i]->Organisation_email__c = str_replace('@', ' [at] ', $result->records[$i]->Organisation_email__c);
//$result->records[$i]->Contact_email__c = str_replace('@', ' [at] ', $result->records[$i]->Contact_email__c);
$output.=
'<tr>'.
'<td>'.$result->records[$i]->Organisation_name__c .'</td>'.
'<td>'.$result->records[$i]->Contact_name__c.'</td>'.
'<td>'.$result->records[$i]->date_start__c.'</td>'.
'<td>'.$result->records[$i]->date_end__c.'</td>';
if ($private) {
$output.= '<td>'.$result->records[$i]->Contact_email__c.'</td>';
$output.= '<td>'.$result->records[$i]->Contact_mobile__c.'</td>';
}
else {
$output.= '<td>'.$result->records[$i]->Organisation_email__c.'</td>';
}
$output.= '</tr>';
$i++;
}
$output.='</table>';
if ($html) {
print ($output);
return 0;
}
}
return $output;
}