Comment fixes
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / Basic.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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
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_Basic extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
36
430ae6dd
TO
37 protected $_query;
38
86538308
EM
39 /**
40 * @param $formValues
41 */
42 /**
43 * @param $formValues
44 */
00be9182 45 public function __construct(&$formValues) {
6a488035
TO
46 parent::__construct($formValues);
47
48 $this->normalize();
49 $this->_columns = array(
50 ts('') => 'contact_type',
51 ts('Name') => 'sort_name',
52 ts('Address') => 'street_address',
53 ts('City') => 'city',
54 ts('State') => 'state_province',
55 ts('Postal') => 'postal_code',
56 ts('Country') => 'country',
57 ts('Email') => 'email',
58 ts('Phone') => 'phone',
59 );
60
61 $params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
62 $returnProperties = array();
63 $returnProperties['contact_sub_type'] = 1;
64
65 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
66 'address_options', TRUE, NULL, TRUE
67 );
68
69 foreach ($this->_columns as $name => $field) {
70 if (in_array($field, array(
353ffa53
TO
71 'street_address',
72 'city',
73 'state_province',
74 'postal_code',
389bcebf 75 'country',
353ffa53
TO
76 )) && empty($addressOptions[$field])
77 ) {
6a488035
TO
78 unset($this->_columns[$name]);
79 continue;
80 }
81 $returnProperties[$field] = 1;
82 }
83
84 $this->_query = new CRM_Contact_BAO_Query($params, $returnProperties, NULL,
85 FALSE, FALSE, 1, FALSE, FALSE
86 );
87 }
88
89 /**
100fef9d 90 * Normalize the form values to make it look similar to the advanced form values
6a488035
TO
91 * this prevents a ton of work downstream and allows us to use the same code for
92 * multiple purposes (queries, save/edit etc)
93 *
94 * @return void
6a488035 95 */
00be9182 96 public function normalize() {
6a488035
TO
97 $contactType = CRM_Utils_Array::value('contact_type', $this->_formValues);
98 if ($contactType && !is_array($contactType)) {
99 unset($this->_formValues['contact_type']);
100 $this->_formValues['contact_type'][$contactType] = 1;
101 }
102
103 $group = CRM_Utils_Array::value('group', $this->_formValues);
104 if ($group && !is_array($group)) {
105 unset($this->_formValues['group']);
106 $this->_formValues['group'][$group] = 1;
107 }
108
109 $tag = CRM_Utils_Array::value('tag', $this->_formValues);
110 if ($tag && !is_array($tag)) {
111 unset($this->_formValues['tag']);
112 $this->_formValues['tag'][$tag] = 1;
113 }
114
389bcebf 115 return NULL;
6a488035
TO
116 }
117
86538308 118 /**
c490a46a 119 * @param CRM_Core_Form $form
86538308 120 */
00be9182 121 public function buildForm(&$form) {
b500fbea
EM
122 //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form
123 // this is loaded onto then replace with something like '__' & test
124 $separator = CRM_Core_DAO::VALUE_SEPARATOR;
125 $contactTypes = array('' => ts('- any contact type -')) + CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, $separator);
24431f7b 126 $form->add('select', 'contact_type', ts('Find...'), $contactTypes, FALSE, array('class' => 'crm-select2 huge'));
6a488035
TO
127
128 // add select for groups
24431f7b
CW
129 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant::nestedGroup();
130 $form->addElement('select', 'group', ts('in'), $group, array('class' => 'crm-select2 huge'));
6a488035
TO
131
132 // add select for categories
cd43c5e3 133 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
24431f7b 134 $form->addElement('select', 'tag', ts('Tagged'), $tag, array('class' => 'crm-select2 huge'));
6a488035
TO
135
136 // text for sort_name
137 $form->add('text', 'sort_name', ts('Name'));
138
139 $form->assign('elements', array('sort_name', 'contact_type', 'group', 'tag'));
140 }
141
86538308
EM
142 /**
143 * @return CRM_Contact_DAO_Contact
144 */
00be9182 145 public function count() {
6a488035
TO
146 return $this->_query->searchQuery(0, 0, NULL, TRUE);
147 }
148
86538308
EM
149 /**
150 * @param int $offset
151 * @param int $rowCount
152 * @param null $sort
153 * @param bool $includeContactIDs
154 * @param bool $justIDs
155 *
156 * @return CRM_Contact_DAO_Contact
157 */
389bcebf 158 public function all(
6a488035
TO
159 $offset = 0,
160 $rowCount = 0,
161 $sort = NULL,
162 $includeContactIDs = FALSE,
163 $justIDs = FALSE
164 ) {
165 return $this->_query->searchQuery(
166 $offset,
167 $rowCount,
168 $sort,
169 FALSE,
170 $includeContactIDs,
171 FALSE,
172 $justIDs,
173 TRUE
174 );
175 }
176
86538308
EM
177 /**
178 * @return string
179 */
00be9182 180 public function from() {
6a488035
TO
181 return $this->_query->_fromClause;
182 }
183
86538308
EM
184 /**
185 * @param bool $includeContactIDs
186 *
187 * @return string|void
188 */
00be9182 189 public function where($includeContactIDs = FALSE) {
6a488035
TO
190 if ($whereClause = $this->_query->whereClause()) {
191 return $whereClause;
192 }
193 return ' (1) ';
194 }
195
86538308
EM
196 /**
197 * @return string
198 */
00be9182 199 public function templateFile() {
6a488035
TO
200 return 'CRM/Contact/Form/Search/Basic.tpl';
201 }
6a488035 202
86538308
EM
203 /**
204 * @return CRM_Contact_BAO_Query
205 */
00be9182 206 public function getQueryObj() {
d9ab802d
PJ
207 return $this->_query;
208 }
96025800 209
232624b1 210}