commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / drupal / modules / civicrm_engage / Engage / Report / Form / CallList.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 * @copyright DharmaTech (c) 2009
33 * $Id$
34 *
35 */
36
37 require_once 'Engage/Report/Form/List.php';
38
39 /**
40 * Generate a phone call list report
41 */
42 class Engage_Report_Form_CallList extends Engage_Report_Form_List {
43 function __construct() {
44
45 parent::__construct();
46
47 $this->_columns = array(
48 'civicrm_phone' =>
49 array(
50 'dao' => 'CRM_Core_DAO_Phone',
51 'fields' =>
52 array(
53 'phone' => array('default' => TRUE,
54 'required' => TRUE,
55 )),
56 'grouping' => 'location-fields',
57 'order_bys' =>
58 array('phone' => array('title' => ts('Phone'),
59 'required' => TRUE,
60 )),
61 ),
62 'civicrm_address' =>
63 array(
64 'dao' => 'CRM_Core_DAO_Address',
65 'fields' =>
66 array(
67 'street_address' =>
68 array('default' => TRUE),
69 'city' =>
70 array('default' => TRUE),
71 'postal_code' =>
72 array(
73 'title' => 'Zip',
74 'default' => TRUE,
75 ),
76 'state_province_id' =>
77 array('title' => ts('State/Province'),
78 'default' => TRUE,
79 ),
80 'country_id' =>
81 array('title' => ts('Country'),
82 ),
83 ),
84 'filters' =>
85 array(
86 'street_address' => NULL,
87 'city' => NULL,
88 'postal_code' => array('title' => 'Zip'),
89 ),
90 'grouping' => 'location-fields',
91 ),
92 'civicrm_email' =>
93 array(
94 'dao' => 'CRM_Core_DAO_Email',
95 'fields' =>
96 array('email' => NULL),
97 'grouping' => 'location-fields',
98 ),
99 'civicrm_contact' =>
100 array(
101 'dao' => 'CRM_Contact_DAO_Contact',
102 'fields' =>
103 array(
104 'id' =>
105 array('title' => ts('Contact ID'),
106 'required' => TRUE,
107 ),
108 'display_name' =>
109 array('title' => ts('Contact Name'),
110 'required' => TRUE,
111 'no_repeat' => TRUE,
112 ),
113 'gender_id' =>
114 array('title' => ts('Sex'),
115 'required' => TRUE,
116 ),
117 'birth_date' =>
118 array('title' => ts('Age'),
119 'required' => TRUE,
120 'type' => CRM_Report_FORM::OP_INT,
121 ),
122 ),
123 'filters' =>
124 array(
125 'sort_name' =>
126 array('title' => ts('Contact Name'),
127 'operator' => 'like',
128 ),
129 ),
130 'grouping' => 'contact-fields',
131 ),
132 $this->_demoTable =>
133 array(
134 'dao' => 'CRM_Contact_DAO_Contact',
135 'fields' =>
136 array(
137 $this->_demoLangCol =>
138 array(
139 'type' => CRM_Report_FORM::OP_STRING,
140 'required' => TRUE,
141 'title' => ts('Language'),
142 ),
143 ),
144 'filters' =>
145 array(
146 $this->_demoLangCol =>
147 array(
148 'title' => ts('Language'),
149 'operatorType' => CRM_Report_FORM::OP_SELECT,
150 'type' => CRM_Report_FORM::OP_STRING,
151 'methodName' => 'selector',
152 'options' => $this->_languages,
153 ),
154 ),
155 'grouping' => 'contact-fields',
156 ),
157 $this->_coreInfoTable =>
158 array(
159 'dao' => 'CRM_Contact_DAO_Contact',
160 'fields' =>
161 array(
162 $this->_coreTypeCol =>
163 array(
164 'type' => CRM_Report_FORM::OP_STRING,
165 'required' => TRUE,
166 'title' => ts('Constituent Type'),
167 ),
168 $this->_coreOtherCol =>
169 array(
170 'no_display' => TRUE,
171 'type' => CRM_Report_Form::OP_STRING,
172 'required' => TRUE,
173 ),
174 ),
175 'grouping' => 'contact-fields',
176 ),
177 'civicrm_group' =>
178 array(
179 'dao' => 'CRM_Contact_DAO_GroupContact',
180 'alias' => 'cgroup',
181 'filters' =>
182 array(
183 'gid' =>
184 array(
185 'name' => 'group_id',
186 'title' => ts('Group'),
187 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
188 'group' => TRUE,
189 'options' => CRM_Core_PseudoConstant::group(),
190 ),
191 ),
192 ),
193 );
194 }
195
196 function preProcess() {
197 parent::preProcess();
198 $reportDate = CRM_Utils_Date::customFormat(date('Y-m-d H:i'));
199 $this->assign('reportTitle', "{$this->_orgName} - Call List <br /> {$reportDate}");
200 }
201
202 /**
203 * Generate WHERE clauses for SQL SELECT
204 */
205 function where() {
206 // Don't list anybody who doesn't have a phone
207 // or has do_not_phone = 1
208 $clauses = array(
209 "{$this->_aliases['civicrm_contact']}.do_not_phone != 1",
210 "NOT ISNULL({$this->_aliases['civicrm_phone']}.phone)",
211 );
212
213 foreach ($this->_columns as $tableName => $table) {
214 //echo "where: table name $tableName<br>";
215
216 // Treatment of normal filters
217 if (array_key_exists('filters', $table)) {
218 foreach ($table['filters'] as $fieldName => $field) {
219 //echo "&nbsp;&nbsp;&nbsp;field name $fieldName<br>";
220 $clause = NULL;
221
222 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
223 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
224 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
225 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
226
227 $clause = $this->dateClause($field['name'], $relative, $from, $to);
228 }
229 elseif ($fieldName == $this->_demoLangCol) {
230 if (!empty($this->_params[$this->_demoLangCol . '_value'])) {
231 $clause = "{$field['dbAlias']}='" . $this->_params[$this->_demoLangCol . '_value'] . "'";
232 }
233 }
234 else {
235 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
236 if ($op == 'mand') {
237 $clause = TRUE;
238 }
239 elseif ($op) {
240 $clause = $this->whereClause($field,
241 $op,
242 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
243 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
244 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
245 );
246 }
247 }
248 //echo "&nbsp;&nbsp;&nbsp;clause=";
249 //var_dump($clause);
250 if (!empty($clause)) {
251 if (!empty($field['group'])) {
252 $clauses[] = $this->engageWhereGroupClause($clause);
253 }
254 else {
255 $clauses[] = $clause;
256 }
257 }
258 }
259 }
260 }
261
262 if (empty($clauses)) {
263 $this->_where = "WHERE ( 1 ) ";
264 }
265 else {
266 $this->_where = "WHERE " . implode(' AND ', $clauses);
267 }
268 //echo $this->_where . "<br>";
269 }
270
271 /**
272 * Process submitted form
273 */
274 function postProcess() {
275 parent::postProcess();
276 }
277
278 /**
279 * Convert contact type info from fields separated by \x01 to a
280 * string of fields separated by commas
281 */
282 function type2str($type) {
283 $typeArray = explode("\x01", $type);
284 foreach ($typeArray as $key => $value) {
285 if (empty($value)) {
286 unset($typeArray[$key]);
287 }
288 }
289 return implode(', ', $typeArray);
290 }
291
292 function alterDisplay(&$rows) {
293 // custom code to alter rows
294 $genderList = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
295 $entryFound = FALSE;
296 foreach ($rows as $rowNum => $row) {
297 // handle state province
298 if (array_key_exists('civicrm_address_state_province_id', $row)) {
299 if ($value = $row['civicrm_address_state_province_id']) {
300 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value);
301 }
302 $entryFound = TRUE;
303 }
304
305 // handle country
306 if (array_key_exists('civicrm_address_country_id', $row)) {
307 if ($value = $row['civicrm_address_country_id']) {
308 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value);
309 }
310 $entryFound = TRUE;
311 }
312
313 // Handle contactType
314 if (!empty($row[$this->_coreInfoTable . '_' . $this->_coreTypeCol])) {
315 $rows[$rowNum][$this->_coreInfoTable . '_' . $this->_coreTypeCol] = $this->type2str($rows[$rowNum][$this->_coreInfoTable . '_' . $this->_coreTypeCol]);
316 $entryFound = TRUE;
317 }
318
319 // date of birth to age
320 if (!empty($row['civicrm_contact_birth_date'])) {
321 $rows[$rowNum]['civicrm_contact_birth_date'] = $this->dob2age($row['civicrm_contact_birth_date']);
322 $entryFound = TRUE;
323 }
324
325 // gender label
326 if (!empty($row['civicrm_contact_gender_id'])) {
327 $rows[$rowNum]['civicrm_contact_gender_id'] = $genderList[$row['civicrm_contact_gender_id']];
328 $entryFound = TRUE;
329 }
330
331 if (($this->_outputMode != 'html') && !empty($row[$this->_coreInfoTable . '_' . $this->_coreOtherCol])) {
332 $rows[$rowNum]['civicrm_contact_display_name'] .= "<br />" . $row[$this->_coreInfoTable . '_' . $this->_coreOtherCol];
333 $entryFound = TRUE;
334 }
335
336 // skip looking further in rows, if first row itself doesn't
337 // have the column we need
338 if (!$entryFound) {
339 break;
340 }
341 }
342
343 $columnOrder = array(
344 'civicrm_phone_phone',
345 'civicrm_contact_display_name',
346 'civicrm_address_street_address',
347 'civicrm_contact_birth_date',
348 'civicrm_contact_gender_id',
349 $this->_demoTable . '_' . $this->_demoLangCol,
350 $this->_coreInfoTable . '_' . $this->_coreTypeCol,
351 'civicrm_contact_id',
352 );
353 if ($this->_outputMode == 'print' || $this->_outputMode == 'pdf') {
354 $this->_columnHeaders = array(
355 'civicrm_phone_phone' => array(
356 'title' => 'PHONE',
357 'type' => CRM_Utils_Type::T_STRING,
358 'class' => 'width=68',
359 ),
360 'civicrm_contact_display_name' => array(
361 'title' => 'NAME',
362 'type' => CRM_Utils_Type::T_STRING,
363 'class' => 'width=83',
364 ),
365 'civicrm_address_street_address' => array(
366 'title' => 'ADDRESS',
367 'type' => CRM_Utils_Type::T_STRING,
368 'class' => 'width=117',
369 ),
370 'civicrm_contact_birth_date' => array(
371 'title' => 'AGE',
372 'type' => CRM_Utils_Type::T_STRING,
373 'class' => 'width=25',
374 ),
375 'civicrm_contact_gender_id' => array(
376 'title' => 'SEX',
377 'type' => CRM_Utils_Type::T_STRING,
378 'class' => 'width=18',
379 ),
380 $this->_demoTable . '_' . $this->_demoLangCol =>
381 array(
382 'title' => 'Lang',
383 'type' => CRM_Utils_Type::T_STRING,
384 'class' => 'width=27',
385 ),
386 $this->_coreInfoTable . '_' . $this->_coreTypeCol =>
387 array(
388 'title' => 'Contact Type',
389 'type' => CRM_Utils_Type::T_STRING,
390 'class' => 'width=48',
391 ),
392 'notes' => array(
393 'title' => 'NOTES',
394 'type' => CRM_Utils_Type::T_STRING,
395 'class' => 'width=48',
396 ),
397 'response_codes' => array(
398 'title' => 'RESPONSE CODES',
399 'type' => CRM_Utils_Type::T_STRING,
400 'class' => 'width=91',
401 ),
402 'status' => array(
403 'title' => 'STATUS',
404 'type' => CRM_Utils_Type::T_STRING,
405 'class' => 'width=70',
406 ),
407 'civicrm_contact_id' => array(
408 'title' => 'ID',
409 'type' => CRM_Utils_Type::T_STRING,
410 'class' => 'width=100',
411 ),
412 );
413 $newRows = array();
414 foreach ($columnOrder as $col) {
415 foreach ($rows as $rowNum => $row) {
416 $newRows[$rowNum][$col] = $row[$col];
417 $newRows[$rowNum]['notes'] = '&nbsp;';
418 $newRows[$rowNum]['status'] = 'NH&nbsp;MV&nbsp;D&nbsp;WN';
419 $newRows[$rowNum]['response_codes'] = '
420 Q1&nbsp;&nbsp;&nbsp;&nbsp;Y&nbsp;&nbsp;&nbsp;&nbsp;N&nbsp;&nbsp;&nbsp;&nbsp;U&nbsp;&nbsp;&nbsp;&nbsp;D<br />
421 Q2&nbsp;&nbsp;&nbsp;&nbsp;Y&nbsp;&nbsp;&nbsp;&nbsp;N&nbsp;&nbsp;&nbsp;&nbsp;U&nbsp;&nbsp;&nbsp;&nbsp;D<br />
422 Q3&nbsp;&nbsp;&nbsp;&nbsp;Y&nbsp;&nbsp;&nbsp;&nbsp;N&nbsp;&nbsp;&nbsp;&nbsp;U&nbsp;&nbsp;&nbsp;&nbsp;D<br />
423 Q4&nbsp;&nbsp;&nbsp;&nbsp;Y&nbsp;&nbsp;&nbsp;&nbsp;N&nbsp;&nbsp;&nbsp;&nbsp;U&nbsp;&nbsp;&nbsp;&nbsp;D';
424 }
425 }
426 $rows = $newRows;
427 $this->assign('pageTotal', ceil((count($newRows) / 7)));
428 }
429 else {
430 // make sure column order is same as in print mode
431 $tempHeaders = $this->_columnHeaders;
432 $this->_columnHeaders = array();
433 foreach ($columnOrder as $col) {
434 if (array_key_exists($col, $tempHeaders)) {
435 $this->_columnHeaders[$col] = $tempHeaders[$col];
436 unset($tempHeaders[$col]);
437 }
438 }
439 $this->_columnHeaders = $this->_columnHeaders + $tempHeaders;
440 }
441 }
442 }
443