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