| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 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 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Report_Form_Walklist_Walklist extends CRM_Report_Form { |
| 36 | protected $_addressField = FALSE; |
| 37 | |
| 38 | protected $_emailField = FALSE; |
| 39 | |
| 40 | protected $_phoneField = FALSE; |
| 41 | |
| 42 | protected $_summary = NULL; |
| 43 | |
| 44 | public $_drilldownReport = array('contact/detail' => 'Link to Detail Report'); |
| 45 | |
| 46 | protected $_customGroupExtends = array( |
| 47 | 'Contact', |
| 48 | 'Individual', |
| 49 | 'Household', |
| 50 | 'Organization', |
| 51 | ); |
| 52 | |
| 53 | /** |
| 54 | */ |
| 55 | /** |
| 56 | */ |
| 57 | public function __construct() { |
| 58 | $this->_columns = array( |
| 59 | 'civicrm_contact' => array( |
| 60 | 'dao' => 'CRM_Contact_DAO_Contact', |
| 61 | 'fields' => array( |
| 62 | 'id' => array( |
| 63 | 'title' => ts('Contact ID'), |
| 64 | 'no_display' => TRUE, |
| 65 | 'required' => TRUE, |
| 66 | ), |
| 67 | 'sort_name' => array( |
| 68 | 'title' => ts('Contact Name'), |
| 69 | 'required' => TRUE, |
| 70 | 'no_repeat' => TRUE, |
| 71 | ), |
| 72 | ), |
| 73 | 'filters' => array( |
| 74 | 'sort_name' => array( |
| 75 | 'title' => ts('Contact Name'), |
| 76 | 'operator' => 'like', |
| 77 | ), |
| 78 | ), |
| 79 | 'grouping' => 'contact-fields', |
| 80 | 'order_bys' => array( |
| 81 | 'sort_name' => array( |
| 82 | 'title' => ts('Contact Name'), |
| 83 | 'required' => TRUE, |
| 84 | ), |
| 85 | ), |
| 86 | ), |
| 87 | 'civicrm_address' => array( |
| 88 | 'dao' => 'CRM_Core_DAO_Address', |
| 89 | 'fields' => array( |
| 90 | 'street_number' => array( |
| 91 | 'title' => ts('Street Number'), |
| 92 | 'type' => 1, |
| 93 | ), |
| 94 | 'street_address' => NULL, |
| 95 | 'city' => NULL, |
| 96 | 'postal_code' => NULL, |
| 97 | 'state_province_id' => array( |
| 98 | 'title' => ts('State/Province'), |
| 99 | 'default' => TRUE, |
| 100 | ), |
| 101 | 'country_id' => array( |
| 102 | 'title' => ts('Country'), |
| 103 | ), |
| 104 | ), |
| 105 | 'filters' => array( |
| 106 | 'street_number' => array( |
| 107 | 'title' => ts('Street Number'), |
| 108 | 'type' => 1, |
| 109 | 'name' => 'street_number', |
| 110 | ), |
| 111 | 'street_address' => NULL, |
| 112 | 'city' => NULL, |
| 113 | ), |
| 114 | 'grouping' => 'location-fields', |
| 115 | ), |
| 116 | 'civicrm_email' => array( |
| 117 | 'dao' => 'CRM_Core_DAO_Email', |
| 118 | 'fields' => array('email' => array('default' => TRUE)), |
| 119 | 'grouping' => 'location-fields', |
| 120 | ), |
| 121 | 'civicrm_phone' => array( |
| 122 | 'dao' => 'CRM_Core_DAO_Phone', |
| 123 | 'fields' => array('phone' => NULL), |
| 124 | 'grouping' => 'location-fields', |
| 125 | ), |
| 126 | ); |
| 127 | parent::__construct(); |
| 128 | } |
| 129 | |
| 130 | public function preProcess() { |
| 131 | parent::preProcess(); |
| 132 | } |
| 133 | |
| 134 | public function select() { |
| 135 | $select = array(); |
| 136 | |
| 137 | $this->_columnHeaders = array(); |
| 138 | foreach ($this->_columns as $tableName => $table) { |
| 139 | foreach ($table['fields'] as $fieldName => $field) { |
| 140 | if (!empty($field['required']) || |
| 141 | !empty($this->_params['fields'][$fieldName]) |
| 142 | ) { |
| 143 | if ($tableName == 'civicrm_address') { |
| 144 | $this->_addressField = TRUE; |
| 145 | } |
| 146 | elseif ($tableName == 'civicrm_email') { |
| 147 | $this->_emailField = TRUE; |
| 148 | } |
| 149 | elseif ($tableName == 'civicrm_phone') { |
| 150 | $this->_phoneField = TRUE; |
| 151 | } |
| 152 | |
| 153 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; |
| 154 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; |
| 155 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type']; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | $this->_select = "SELECT " . implode(",\n", $select) . " "; |
| 161 | } |
| 162 | |
| 163 | public function from() { |
| 164 | $this->_from = NULL; |
| 165 | |
| 166 | $this->_from = " |
| 167 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} |
| 168 | "; |
| 169 | if ($this->_addressField) { |
| 170 | $this->_from .= "LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n"; |
| 171 | } |
| 172 | |
| 173 | if ($this->_emailField) { |
| 174 | $this->_from .= "LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n"; |
| 175 | } |
| 176 | |
| 177 | if ($this->_phoneField) { |
| 178 | $this->_from .= "LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND {$this->_aliases['civicrm_phone']}.is_primary = 1\n"; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | public function where() { |
| 183 | $clauses = array(); |
| 184 | foreach ($this->_columns as $tableName => $table) { |
| 185 | if (array_key_exists('filters', $table)) { |
| 186 | foreach ($table['filters'] as $fieldName => $field) { |
| 187 | $clause = NULL; |
| 188 | |
| 189 | if ($field['type'] & CRM_Utils_Type::T_DATE) { |
| 190 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); |
| 191 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
| 192 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); |
| 193 | |
| 194 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); |
| 195 | } |
| 196 | else { |
| 197 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); |
| 198 | if ($op) { |
| 199 | $clause = $this->whereClause($field, |
| 200 | $op, |
| 201 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), |
| 202 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), |
| 203 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) |
| 204 | ); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if (!empty($clause)) { |
| 209 | $clauses[] = $clause; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if (empty($clauses)) { |
| 216 | $this->_where = "WHERE ( 1 ) "; |
| 217 | } |
| 218 | else { |
| 219 | $this->_where = "WHERE " . implode(' AND ', $clauses); |
| 220 | } |
| 221 | |
| 222 | if ($this->_aclWhere) { |
| 223 | $this->_where .= " AND {$this->_aclWhere} "; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | public function orderBy() { |
| 228 | $this->_orderBy = ""; |
| 229 | foreach ($this->_columns as $tableName => $table) { |
| 230 | if (array_key_exists('order_bys', $table)) { |
| 231 | foreach ($table['order_bys'] as $fieldName => $field) { |
| 232 | $this->_orderBy[] = $field['dbAlias']; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | $this->_orderBy = "ORDER BY " . implode(', ', $this->_orderBy) . " "; |
| 237 | } |
| 238 | |
| 239 | public function postProcess() { |
| 240 | // get the acl clauses built before we assemble the query |
| 241 | $this->buildACLClause($this->_aliases['civicrm_contact']); |
| 242 | parent::postProcess(); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Alter display of rows. |
| 247 | * |
| 248 | * Iterate through the rows retrieved via SQL and make changes for display purposes, |
| 249 | * such as rendering contacts as links. |
| 250 | * |
| 251 | * @param array $rows |
| 252 | * Rows generated by SQL, with an array for each row. |
| 253 | */ |
| 254 | public function alterDisplay(&$rows) { |
| 255 | $entryFound = FALSE; |
| 256 | foreach ($rows as $rowNum => $row) { |
| 257 | // handle state province |
| 258 | if (array_key_exists('civicrm_address_state_province_id', $row)) { |
| 259 | if ($value = $row['civicrm_address_state_province_id']) { |
| 260 | $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value); |
| 261 | } |
| 262 | $entryFound = TRUE; |
| 263 | } |
| 264 | |
| 265 | // handle country |
| 266 | if (array_key_exists('civicrm_address_country_id', $row)) { |
| 267 | if ($value = $row['civicrm_address_country_id']) { |
| 268 | $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value); |
| 269 | } |
| 270 | $entryFound = TRUE; |
| 271 | } |
| 272 | |
| 273 | // convert display name to links |
| 274 | if (array_key_exists('civicrm_contact_sort_name', $row) && |
| 275 | array_key_exists('civicrm_contact_id', $row) |
| 276 | ) { |
| 277 | $url = CRM_Report_Utils_Report::getNextUrl('contact/detail', |
| 278 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'], |
| 279 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport |
| 280 | ); |
| 281 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; |
| 282 | $entryFound = TRUE; |
| 283 | } |
| 284 | |
| 285 | // skip looking further in rows, if first row itself doesn't |
| 286 | // have the column we need |
| 287 | if (!$entryFound) { |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | } |