Merge pull request #2006 from civicrm/4.3
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / PostalMailing.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35 class CRM_Contact_Form_Search_Custom_PostalMailing extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
36 function __construct(&$formValues) {
37 parent::__construct($formValues);
38
39 $this->_columns = array(
40 ts('Contact Id') => 'contact_id',
41 ts('Address') => 'address',
42 ts('Contact Type') => 'contact_type',
43 ts('Name') => 'sort_name',
44 ts('State') => 'state_province',
45 );
46 }
47
48 function buildForm(&$form) {
49 $groups = array('' => ts('- select group -')) + CRM_Core_PseudoConstant::allGroup();
50 $form->addElement('select', 'group_id', ts('Group'), $groups);
51
52 /**
53 * if you are using the standard template, this array tells the template what elements
54 * are part of the search criteria
55 */
56 $form->assign('elements', array('group_id'));
57 }
58
59 function all($offset = 0, $rowcount = 0, $sort = NULL,
60 $includeContactIDs = FALSE, $justIDs = FALSE
61 ) {
62 if ($justIDs) {
63 $selectClause = "contact_a.id as contact_id";
64 }
65 else {
66 $selectClause = "
67 DISTINCT contact_a.id as contact_id ,
68 contact_a.contact_type as contact_type,
69 contact_a.sort_name as sort_name,
70 address.street_address as address,
71 state_province.name as state_province
72 ";
73 }
74
75 return $this->sql($selectClause,
76 $offset, $rowcount, $sort,
77 $includeContactIDs, NULL
78 );
79 }
80
81 function from() {
82 return "
83 FROM civicrm_group_contact as cgc,
84 civicrm_contact as contact_a
85 LEFT JOIN civicrm_address address ON (address.contact_id = contact_a.id AND
86 address.is_primary = 1 )
87 LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id
88 ";
89 }
90
91 function where($includeContactIDs = FALSE) {
92 $params = array();
93
94 $count = 1;
95 $clause = array();
96 $groupID = CRM_Utils_Array::value('group_id',
97 $this->_formValues
98 );
99 if ($groupID) {
100 $params[$count] = array($groupID, 'Integer');
101 $clause[] = "cgc.group_id = %{$count}";
102 }
103
104 $clause[] = "cgc.status = 'Added'";
105 $clause[] = "contact_a.id = IF( EXISTS(select cr.id from civicrm_relationship cr where (cr.contact_id_a = cgc.contact_id AND (cr.relationship_type_id = 7 OR cr.relationship_type_id = 6))),
106 (select cr.contact_id_b from civicrm_relationship cr where (cr.contact_id_a = cgc.contact_id AND (cr.relationship_type_id = 7 OR cr.relationship_type_id = 6))),
107 cgc.contact_id )";
108 $clause[] = "contact_a.contact_type IN ('Individual','Household')";
109
110 if (!empty($clause)) {
111 $where = implode(' AND ', $clause);
112 }
113
114 return $this->whereClause($where, $params);
115 }
116
117 function templateFile() {
118 return 'CRM/Contact/Form/Search/Custom.tpl';
119 }
120 }
121