Merge pull request #3833 from NileemaJadhav/CRM-HR-master
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / PostalMailing.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 /**
37 * @param $formValues
38 */
39 function __construct(&$formValues) {
40 parent::__construct($formValues);
41
42 $this->_columns = array(
43 ts('Contact ID') => 'contact_id',
44 ts('Address') => 'address',
45 ts('Contact Type') => 'contact_type',
46 ts('Name') => 'sort_name',
47 ts('State') => 'state_province',
48 );
49 }
50
51 /**
52 * @param $form
53 */
54 function buildForm(&$form) {
55 $groups = array('' => ts('- select group -')) + CRM_Core_PseudoConstant::nestedGroup(FALSE);
56 $form->addElement('select', 'group_id', ts('Group'), $groups, array('class' => 'crm-select2 huge'));
57
58 /**
59 * if you are using the standard template, this array tells the template what elements
60 * are part of the search criteria
61 */
62 $form->assign('elements', array('group_id'));
63 }
64
65 function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
66 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
67 }
68
69 /**
70 * @param int $offset
71 * @param int $rowcount
72 * @param null $sort
73 * @param bool $includeContactIDs
74 * @param bool $justIDs
75 *
76 * @return string
77 */
78 function all($offset = 0, $rowcount = 0, $sort = NULL,
79 $includeContactIDs = FALSE, $justIDs = FALSE
80 ) {
81 if ($justIDs) {
82 $selectClause = "contact_a.id as contact_id";
83 $sort = 'contact_a.id';
84 }
85 else {
86 $selectClause = "
87 DISTINCT contact_a.id as contact_id ,
88 contact_a.contact_type as contact_type,
89 contact_a.sort_name as sort_name,
90 address.street_address as address,
91 state_province.name as state_province
92 ";
93 }
94
95 return $this->sql($selectClause,
96 $offset, $rowcount, $sort,
97 $includeContactIDs, NULL
98 );
99 }
100
101 /**
102 * @return string
103 */
104 function from() {
105 return "
106 FROM civicrm_group_contact as cgc,
107 civicrm_contact as contact_a
108 LEFT JOIN civicrm_address address ON (address.contact_id = contact_a.id AND
109 address.is_primary = 1 )
110 LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id
111 ";
112 }
113
114 /**
115 * @param bool $includeContactIDs
116 *
117 * @return string
118 */
119 function where($includeContactIDs = FALSE) {
120 $params = array();
121
122 $count = 1;
123 $clause = array();
124 $groupID = CRM_Utils_Array::value('group_id',
125 $this->_formValues
126 );
127 if ($groupID) {
128 $params[$count] = array($groupID, 'Integer');
129 $clause[] = "cgc.group_id = %{$count}";
130 }
131
132 $clause[] = "cgc.status = 'Added'";
133 $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))),
134 (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))),
135 cgc.contact_id )";
136 $clause[] = "contact_a.contact_type IN ('Individual','Household')";
137
138 if (!empty($clause)) {
139 $where = implode(' AND ', $clause);
140 }
141
142 return $this->whereClause($where, $params);
143 }
144
145 /**
146 * @return string
147 */
148 function templateFile() {
149 return 'CRM/Contact/Form/Search/Custom.tpl';
150 }
151 }
152