Merge pull request #4979 from xurizaemon/codingstandards-12
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / PostalMailing.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Contact_Form_Search_Custom_PostalMailing extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
86538308
EM
36 /**
37 * @param $formValues
38 */
00be9182 39 public function __construct(&$formValues) {
6a488035
TO
40 parent::__construct($formValues);
41
42 $this->_columns = array(
7b99ead3 43 ts('Contact ID') => 'contact_id',
6a488035
TO
44 ts('Address') => 'address',
45 ts('Contact Type') => 'contact_type',
46 ts('Name') => 'sort_name',
47 ts('State') => 'state_province',
48 );
49 }
50
86538308 51 /**
c490a46a 52 * @param CRM_Core_Form $form
86538308 53 */
00be9182 54 public function buildForm(&$form) {
24431f7b
CW
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'));
6a488035
TO
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
00be9182 65 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
7c34ab11 66 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
67 }
68
86538308
EM
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 */
59f4c9ee 78 public function all(
51ccfbbe 79 $offset = 0, $rowcount = 0, $sort = NULL,
6a488035
TO
80 $includeContactIDs = FALSE, $justIDs = FALSE
81 ) {
82 if ($justIDs) {
83 $selectClause = "contact_a.id as contact_id";
7c34ab11 84 $sort = 'contact_a.id';
6a488035
TO
85 }
86 else {
51ccfbbe 87 $selectClause = "
6a488035
TO
88DISTINCT contact_a.id as contact_id ,
89contact_a.contact_type as contact_type,
90contact_a.sort_name as sort_name,
91address.street_address as address,
92state_province.name as state_province
93";
94 }
95
96 return $this->sql($selectClause,
97 $offset, $rowcount, $sort,
98 $includeContactIDs, NULL
99 );
100 }
101
86538308
EM
102 /**
103 * @return string
104 */
00be9182 105 public function from() {
6a488035
TO
106 return "
107FROM civicrm_group_contact as cgc,
108 civicrm_contact as contact_a
109LEFT JOIN civicrm_address address ON (address.contact_id = contact_a.id AND
110 address.is_primary = 1 )
111LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id
112";
113 }
114
86538308
EM
115 /**
116 * @param bool $includeContactIDs
117 *
118 * @return string
119 */
00be9182 120 public function where($includeContactIDs = FALSE) {
6a488035
TO
121 $params = array();
122
353ffa53
TO
123 $count = 1;
124 $clause = array();
6a488035
TO
125 $groupID = CRM_Utils_Array::value('group_id',
126 $this->_formValues
127 );
128 if ($groupID) {
129 $params[$count] = array($groupID, 'Integer');
130 $clause[] = "cgc.group_id = %{$count}";
131 }
132
133 $clause[] = "cgc.status = 'Added'";
134 $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))),
135 (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))),
136 cgc.contact_id )";
137 $clause[] = "contact_a.contact_type IN ('Individual','Household')";
138
139 if (!empty($clause)) {
140 $where = implode(' AND ', $clause);
141 }
142
143 return $this->whereClause($where, $params);
144 }
145
86538308
EM
146 /**
147 * @return string
148 */
00be9182 149 public function templateFile() {
6a488035
TO
150 return 'CRM/Contact/Form/Search/Custom.tpl';
151 }
152}