Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-02-09-11-44-07
[civicrm-core.git] / tools / extensions / org.civicrm.search.basic / Basic.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
36 require_once 'CRM/Contact/Form/Search/Custom/Base.php';
37
38 /**
39 * Class org_civicrm_search_basic
40 */
41 class org_civicrm_search_basic extends CRM_Contact_Form_Search_Custom_BaseimplementsCRM_Contact_Form_Search_Interface {
42
43 protected $_query;
44
45 /**
46 * @param $formValues
47 */
48 function __construct(&$formValues) {
49 parent::__construct($formValues);
50
51 $this->normalize();
52 $this->_columns = array(ts('') => 'contact_type',
53 ts('') => 'contact_sub_type',
54 ts('Name') => 'sort_name',
55 ts('Address') => 'street_address',
56 ts('City') => 'city',
57 ts('State') => 'state_province',
58 ts('Postal') => 'postal_code',
59 ts('Country') => 'country',
60 ts('Email') => 'email',
61 ts('Phone') => 'phone',
62 );
63
64 $params = &CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
65 $returnProperties = array();
66 foreach ($this->_columns as $name => $field) {
67 $returnProperties[$field] = 1;
68 }
69
70 $this->_query = new CRM_Contact_BAO_Query($params, $returnProperties, NULL,
71 FALSE, FALSE, 1, FALSE, FALSE
72 );
73 }
74
75 /**
76 * normalize the form values to make it look similar to the advanced form values
77 * this prevents a ton of work downstream and allows us to use the same code for
78 * multiple purposes (queries, save/edit etc)
79 *
80 * @return void
81 * @access private
82 */
83 function normalize() {
84 $contactType = CRM_Utils_Array::value('contact_type', $this->_formValues);
85 if ($contactType && !is_array($contactType)) {
86 unset($this->_formValues['contact_type']);
87 $this->_formValues['contact_type'][$contactType] = 1;
88 }
89
90 $group = CRM_Utils_Array::value('group', $this->_formValues);
91 if ($group && !is_array($group)) {
92 unset($this->_formValues['group']);
93 $this->_formValues['group'][$group] = 1;
94 }
95
96 $tag = CRM_Utils_Array::value('tag', $this->_formValues);
97 if ($tag && !is_array($tag)) {
98 unset($this->_formValues['tag']);
99 $this->_formValues['tag'][$tag] = 1;
100 }
101
102 return;
103 }
104
105 /**
106 * @param CRM_Core_Form $form
107 */
108 function buildForm(&$form) {
109
110 //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form
111 // this is loaded onto then replace with something like '__' & test
112 $separator = CRM_Core_DAO::VALUE_SEPARATOR;
113 $contactTypes = array('' => ts('- any contact type -')) + CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, $separator);
114 $form->add('select', 'contact_type', ts('Find...'), $contactTypes);
115
116 // add select for groups
117 $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant::group();
118 $form->addElement('select', 'group', ts('in'), $group);
119
120 // add select for categories
121 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
122 $form->addElement('select', 'tag', ts('Tagged'), $tag);
123
124 // text for sort_name
125 $form->add('text', 'sort_name', ts('Name'));
126
127 $form->assign('elements', array('sort_name', 'contact_type', 'group', 'tag'));
128 }
129
130 /**
131 * @return CRM_Contact_DAO_Contact
132 */
133 function count() {
134 return $this->_query->searchQuery(0, 0, NULL, TRUE);
135 }
136
137 /**
138 * @param int $offset
139 * @param int $rowCount
140 * @param null $sort
141 * @param bool $includeContactIDs
142 *
143 * @return CRM_Contact_DAO_Contact
144 */
145 function all($offset = 0, $rowCount = 0, $sort = NULL,
146 $includeContactIDs = FALSE
147 ) {
148 return $this->_query->searchQuery($offset, $rowCount, $sort,
149 FALSE, $includeContactIDs,
150 FALSE, FALSE, TRUE
151 );
152 }
153
154 /**
155 * @return string
156 */
157 function from() {
158 return $this->_query->_fromClause;
159 }
160
161 /**
162 * @param bool $includeContactIDs
163 *
164 * @return string
165 */
166 function where($includeContactIDs = FALSE) {
167 if ($whereClause = $this->_query->whereClause()) {
168 return $whereClause;
169 }
170 return ' (1) ';
171 }
172
173 /**
174 * @return string
175 */
176 function templateFile() {
177 return 'CRM/Contact/Form/Search/Basic.tpl';
178 }
179 }
180