| 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 | */ |
| 33 | |
| 34 | /** |
| 35 | * This class is used to retrieve and display a range of contacts that match the given criteria. |
| 36 | */ |
| 37 | class CRM_Campaign_Selector_Search extends CRM_Core_Selector_Base implements CRM_Core_Selector_API { |
| 38 | |
| 39 | /** |
| 40 | * This defines two actions- View and Edit. |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | static $_links = NULL; |
| 45 | |
| 46 | /** |
| 47 | * We use desc to remind us what that column is, name is used in the tpl |
| 48 | * |
| 49 | * @var array |
| 50 | */ |
| 51 | static $_columnHeaders; |
| 52 | |
| 53 | /** |
| 54 | * Properties of contact we're interested in displaying |
| 55 | * @var array |
| 56 | */ |
| 57 | static $_properties = array( |
| 58 | 'contact_id', |
| 59 | 'sort_name', |
| 60 | 'street_unit', |
| 61 | 'street_name', |
| 62 | 'street_number', |
| 63 | 'street_address', |
| 64 | 'city', |
| 65 | 'postal_code', |
| 66 | 'state_province', |
| 67 | 'country', |
| 68 | 'email', |
| 69 | 'phone', |
| 70 | 'campaign_id', |
| 71 | 'survey_activity_id', |
| 72 | 'survey_activity_target_id', |
| 73 | 'survey_activity_target_contact_id', |
| 74 | ); |
| 75 | |
| 76 | /** |
| 77 | * Are we restricting ourselves to a single contact |
| 78 | * |
| 79 | * @var boolean |
| 80 | */ |
| 81 | protected $_single = FALSE; |
| 82 | |
| 83 | /** |
| 84 | * Are we restricting ourselves to a single contact |
| 85 | * |
| 86 | * @var boolean |
| 87 | */ |
| 88 | protected $_limit = NULL; |
| 89 | |
| 90 | /** |
| 91 | * What context are we being invoked from |
| 92 | * |
| 93 | * @var string |
| 94 | */ |
| 95 | protected $_context = NULL; |
| 96 | |
| 97 | /** |
| 98 | * QueryParams is the array returned by exportValues called on |
| 99 | * the HTML_QuickForm_Controller for that page. |
| 100 | * |
| 101 | * @var array |
| 102 | */ |
| 103 | public $_queryParams; |
| 104 | |
| 105 | /** |
| 106 | * Represent the type of selector. |
| 107 | * |
| 108 | * @var int |
| 109 | */ |
| 110 | protected $_action; |
| 111 | |
| 112 | /** |
| 113 | * The additional clause that we restrict the search with. |
| 114 | * |
| 115 | * @var string |
| 116 | */ |
| 117 | protected $_surveyClause = NULL; |
| 118 | |
| 119 | /** |
| 120 | * The query object. |
| 121 | * |
| 122 | * @var string |
| 123 | */ |
| 124 | protected $_query; |
| 125 | |
| 126 | /** |
| 127 | * Class constructor. |
| 128 | * |
| 129 | * @param array $queryParams |
| 130 | * Array of parameters for query. |
| 131 | * @param \const|int $action - action of search basic or advanced. |
| 132 | * @param string $surveyClause |
| 133 | * If the caller wants to further restrict the search. |
| 134 | * @param bool $single |
| 135 | * Are we dealing only with one contact?. |
| 136 | * @param int $limit |
| 137 | * How many voters do we want returned. |
| 138 | * |
| 139 | * @param string $context |
| 140 | * |
| 141 | * @return \CRM_Campaign_Selector_Search |
| 142 | */ |
| 143 | public function __construct( |
| 144 | &$queryParams, |
| 145 | $action = CRM_Core_Action::NONE, |
| 146 | $surveyClause = NULL, |
| 147 | $single = FALSE, |
| 148 | $limit = NULL, |
| 149 | $context = 'search' |
| 150 | ) { |
| 151 | // submitted form values |
| 152 | $this->_queryParams = &$queryParams; |
| 153 | |
| 154 | $this->_single = $single; |
| 155 | $this->_limit = $limit; |
| 156 | $this->_context = $context; |
| 157 | |
| 158 | $this->_campaignClause = $surveyClause; |
| 159 | $this->_campaignFromClause = CRM_Utils_Array::value('fromClause', $surveyClause); |
| 160 | $this->_campaignWhereClause = CRM_Utils_Array::value('whereClause', $surveyClause); |
| 161 | |
| 162 | // type of selector |
| 163 | $this->_action = $action; |
| 164 | |
| 165 | $this->_query = new CRM_Contact_BAO_Query($this->_queryParams, |
| 166 | NULL, NULL, FALSE, FALSE, |
| 167 | CRM_Contact_BAO_Query::MODE_CAMPAIGN, |
| 168 | TRUE |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * This method returns the links that are given for each search row. |
| 174 | * currently the links added for each row are |
| 175 | * |
| 176 | * - View |
| 177 | * - Edit |
| 178 | * |
| 179 | * @return array |
| 180 | */ |
| 181 | static public function &links() { |
| 182 | return self::$_links = array(); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Getter for array of the parameters required for creating pager. |
| 187 | * |
| 188 | * @param $action |
| 189 | * @param array $params |
| 190 | */ |
| 191 | public function getPagerParams($action, &$params) { |
| 192 | $params['csvString'] = NULL; |
| 193 | $params['status'] = ts('Respondents') . ' %%StatusMessage%%'; |
| 194 | $params['rowCount'] = ($this->_limit) ? $this->_limit : CRM_Utils_Pager::ROWCOUNT; |
| 195 | $params['buttonTop'] = 'PagerTopButton'; |
| 196 | $params['buttonBottom'] = 'PagerBottomButton'; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns total number of rows for the query. |
| 201 | * |
| 202 | * @param |
| 203 | * |
| 204 | * @return int |
| 205 | * Total number of rows |
| 206 | */ |
| 207 | public function getTotalCount($action) { |
| 208 | return $this->_query->searchQuery(0, 0, NULL, |
| 209 | TRUE, FALSE, |
| 210 | FALSE, FALSE, FALSE, |
| 211 | $this->_campaignWhereClause, |
| 212 | NULL, |
| 213 | $this->_campaignFromClause |
| 214 | ); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Returns all the rows in the given offset and rowCount. |
| 219 | * |
| 220 | * @param string $action |
| 221 | * The action being performed. |
| 222 | * @param int $offset |
| 223 | * The row number to start from. |
| 224 | * @param int $rowCount |
| 225 | * The number of rows to return. |
| 226 | * @param string $sort |
| 227 | * The sql string that describes the sort order. |
| 228 | * @param string $output |
| 229 | * What should the result set include (web/email/csv). |
| 230 | * |
| 231 | * @return int |
| 232 | * the total number of rows for this action |
| 233 | */ |
| 234 | public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { |
| 235 | $result = $this->_query->searchQuery($offset, $rowCount, $sort, |
| 236 | FALSE, FALSE, |
| 237 | FALSE, FALSE, |
| 238 | FALSE, $this->_campaignWhereClause, |
| 239 | NULL, |
| 240 | $this->_campaignFromClause |
| 241 | ); |
| 242 | |
| 243 | // process the result of the query |
| 244 | $rows = array(); |
| 245 | |
| 246 | while ($result->fetch()) { |
| 247 | $this->_query->convertToPseudoNames($result); |
| 248 | $row = array(); |
| 249 | // the columns we are interested in |
| 250 | foreach (self::$_properties as $property) { |
| 251 | if (property_exists($result, $property)) { |
| 252 | $row[$property] = $result->$property; |
| 253 | } |
| 254 | } |
| 255 | $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contact_id; |
| 256 | $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_type, FALSE, $result->contact_id); |
| 257 | |
| 258 | $rows[] = $row; |
| 259 | } |
| 260 | $this->buildPrevNextCache($sort); |
| 261 | |
| 262 | return $rows; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * @param $sort |
| 267 | */ |
| 268 | public function buildPrevNextCache($sort) { |
| 269 | //for prev/next pagination |
| 270 | $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject); |
| 271 | |
| 272 | if (!$crmPID) { |
| 273 | $cacheKey = "civicrm search {$this->_key}"; |
| 274 | CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey, 'civicrm_contact'); |
| 275 | |
| 276 | $sql = $this->_query->searchQuery(0, 0, $sort, |
| 277 | FALSE, FALSE, |
| 278 | FALSE, FALSE, |
| 279 | TRUE, $this->_campaignWhereClause, |
| 280 | NULL, |
| 281 | $this->_campaignFromClause |
| 282 | ); |
| 283 | list($select, $from) = explode(' FROM ', $sql); |
| 284 | $insertSQL = " |
| 285 | INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) |
| 286 | SELECT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name |
| 287 | FROM {$from} |
| 288 | "; |
| 289 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
| 290 | $result = CRM_Core_DAO::executeQuery($insertSQL); |
| 291 | unset($errorScope); |
| 292 | |
| 293 | if (is_a($result, 'DB_Error')) { |
| 294 | return; |
| 295 | } |
| 296 | // also record an entry in the cache key table, so we can delete it periodically |
| 297 | CRM_Core_BAO_Cache::setItem($cacheKey, 'CiviCRM Search PrevNextCache', $cacheKey); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * @return array |
| 303 | * which contains an array of strings |
| 304 | */ |
| 305 | public function getQILL() { |
| 306 | return $this->_query->qill(); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Returns the column headers as an array of tuples: |
| 311 | * (name, sortName (key to the sort array)) |
| 312 | * |
| 313 | * @param string $action |
| 314 | * The action being performed. |
| 315 | * @param string $output |
| 316 | * What should the result set include (web/email/csv). |
| 317 | * |
| 318 | * @return array |
| 319 | * the column headers that need to be displayed |
| 320 | */ |
| 321 | public function &getColumnHeaders($action = NULL, $output = NULL) { |
| 322 | self::$_columnHeaders = array(); |
| 323 | |
| 324 | if (!$this->_single) { |
| 325 | $contactDetails = array( |
| 326 | array( |
| 327 | 'name' => ts('Contact Name'), |
| 328 | 'sort' => 'sort_name', |
| 329 | 'direction' => CRM_Utils_Sort::ASCENDING, |
| 330 | ), |
| 331 | array( |
| 332 | 'name' => ts('Street Number'), |
| 333 | 'sort' => 'street_number', |
| 334 | ), |
| 335 | array( |
| 336 | 'name' => ts('Street Name'), |
| 337 | 'sort' => 'street_name', |
| 338 | ), |
| 339 | array('name' => ts('Street Address')), |
| 340 | array( |
| 341 | 'name' => ts('City'), |
| 342 | 'sort' => 'city', |
| 343 | ), |
| 344 | array( |
| 345 | 'name' => ts('Postal Code'), |
| 346 | 'sort' => 'postal_code', |
| 347 | ), |
| 348 | array( |
| 349 | 'name' => ts('State'), |
| 350 | 'sort' => 'state_province_name', |
| 351 | ), |
| 352 | array('name' => ts('Country')), |
| 353 | array('name' => ts('Email')), |
| 354 | array('name' => ts('Phone')), |
| 355 | ); |
| 356 | self::$_columnHeaders = array_merge($contactDetails, self::$_columnHeaders); |
| 357 | } |
| 358 | |
| 359 | return self::$_columnHeaders; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * @return string |
| 364 | */ |
| 365 | public function &getQuery() { |
| 366 | return $this->_query; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Name of export file. |
| 371 | * |
| 372 | * @param string $output |
| 373 | * Type of output. |
| 374 | * |
| 375 | * @return string |
| 376 | * name of the file |
| 377 | */ |
| 378 | public function getExportFileName($output = 'csv') { |
| 379 | return ts('CiviCRM Respondent Search'); |
| 380 | } |
| 381 | |
| 382 | } |