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