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