Merge pull request #3229 from eileenmcnaughton/e-notice
[civicrm-core.git] / tests / extensions / test.extension.manager.searchtest / main.php
1 <?php
2
3 require_once 'CRM/Contact/Form/Search/Custom/Base.php';
4 class test_extension_manager_searchtest extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
5 function __construct(&$formValues) {
6 parent::__construct($formValues);
7 }
8
9 /**
10 * Prepare a set of search fields
11 *
12 * @param CRM_Core_Form $form modifiable
13 * @return void
14 */
15 function buildForm(&$form) {
16 CRM_Utils_System::setTitle(ts('My Search Title'));
17
18 $form->add('text',
19 'household_name',
20 ts('Household Name'),
21 TRUE
22 );
23
24 $stateProvince = array('' => ts('- any state/province -')) + CRM_Core_PseudoConstant::stateProvince();
25 $form->addElement('select', 'state_province_id', ts('State/Province'), $stateProvince);
26
27 // Optionally define default search values
28 $form->setDefaults(array(
29 'household_name' => '',
30 'state_province_id' => NULL,
31 ));
32
33 /**
34 * if you are using the standard template, this array tells the template what elements
35 * are part of the search criteria
36 */
37 $form->assign('elements', array('household_name', 'state_province_id'));
38 }
39
40 /**
41 * Get a list of summary data points
42 *
43 * @return mixed; NULL or array with keys:
44 * - summary: string
45 * - total: numeric
46 */
47 function summary() {
48 return NULL;
49 // return array(
50 // 'summary' => 'This is a summary',
51 // 'total' => 50.0,
52 // );
53 }
54
55 /**
56 * Get a list of displayable columns
57 *
58 * @return array, keys are printable column headers and values are SQL column names
59 */
60 function &columns() {
61 // return by reference
62 $columns = array(
63 ts('Contact Id') => 'contact_id',
64 ts('Contact Type') => 'contact_type',
65 ts('Name') => 'sort_name',
66 ts('State') => 'state_province',
67 );
68 return $columns;
69 }
70
71 /**
72 * Construct a full SQL query which returns one page worth of results
73 *
74 * @param int $offset
75 * @param int $rowcount
76 * @param null $sort
77 * @param bool $includeContactIDs
78 *
79 * @return string, sql
80 */
81 function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE) {
82 // delegate to $this->sql(), $this->select(), $this->from(), $this->where(), etc.
83 return $this->sql($this->select(), $offset, $rowcount, $sort, $includeContactIDs, NULL);
84 }
85
86 /**
87 * Construct a SQL SELECT clause
88 *
89 * @return string, sql fragment with SELECT arguments
90 */
91 function select() {
92 return "
93 contact_a.id as contact_id ,
94 contact_a.contact_type as contact_type,
95 contact_a.sort_name as sort_name,
96 state_province.name as state_province
97 ";
98 }
99
100 /**
101 * Construct a SQL FROM clause
102 *
103 * @return string, sql fragment with FROM and JOIN clauses
104 */
105 function from() {
106 return "
107 FROM civicrm_contact 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_email ON ( civicrm_email.contact_id = contact_a.id AND
111 civicrm_email.is_primary = 1 )
112 LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id
113 ";
114 }
115
116 /**
117 * Construct a SQL WHERE clause
118 *
119 * @param bool $includeContactIDs
120 *
121 * @return string, sql fragment with conditional expressions
122 */
123 function where($includeContactIDs = FALSE) {
124 $params = array();
125 $where = "contact_a.contact_type = 'Household'";
126
127 $count = 1;
128 $clause = array();
129 $name = CRM_Utils_Array::value('household_name',
130 $this->_formValues
131 );
132 if ($name != NULL) {
133 if (strpos($name, '%') === FALSE) {
134 $name = "%{$name}%";
135 }
136 $params[$count] = array($name, 'String');
137 $clause[] = "contact_a.household_name LIKE %{$count}";
138 $count++;
139 }
140
141 $state = CRM_Utils_Array::value('state_province_id',
142 $this->_formValues
143 );
144 if (!$state &&
145 $this->_stateID
146 ) {
147 $state = $this->_stateID;
148 }
149
150 if ($state) {
151 $params[$count] = array($state, 'Integer');
152 $clause[] = "state_province.id = %{$count}";
153 }
154
155 if (!empty($clause)) {
156 $where .= ' AND ' . implode(' AND ', $clause);
157 }
158
159 return $this->whereClause($where, $params);
160 }
161
162 /**
163 * Determine the Smarty template for the search screen
164 *
165 * @return string, template path (findable through Smarty template path)
166 */
167 function templateFile() {
168 return 'CRM/Contact/Form/Search/Custom.tpl';
169 }
170
171 /**
172 * Modify the content of each row
173 *
174 * @param array $row modifiable SQL result row
175 * @return void
176 */
177 function alterRow(&$row) {
178 $row['sort_name'] .= ' ( altered )';
179 }
180
181 }