Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | require_once 'CRM/Contact/Form/Search/Custom/Base.php'; | |
aba1cd8b EM |
4 | |
5 | /** | |
6 | * Class test_extension_manager_searchtest | |
7 | */ | |
6a488035 | 8 | class test_extension_manager_searchtest extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface { |
39b959db | 9 | |
627456b5 EM |
10 | /** |
11 | * @param $formValues | |
12 | */ | |
608e6658 | 13 | public function __construct(&$formValues) { |
6a488035 TO |
14 | parent::__construct($formValues); |
15 | } | |
16 | ||
17 | /** | |
eceb18cc | 18 | * Prepare a set of search fields. |
6a488035 | 19 | * |
e16033b4 TO |
20 | * @param CRM_Core_Form $form |
21 | * Modifiable. | |
6a488035 TO |
22 | * @return void |
23 | */ | |
608e6658 | 24 | public function buildForm(&$form) { |
6a488035 TO |
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 | /** | |
eceb18cc | 50 | * Get a list of summary data points. |
6a488035 | 51 | * |
39b959db SL |
52 | * @return mixed |
53 | * - NULL or array with keys: | |
39b9dbb3 TO |
54 | * - summary: string |
55 | * - total: numeric | |
6a488035 | 56 | */ |
608e6658 | 57 | public function summary() { |
6a488035 TO |
58 | return NULL; |
59 | // return array( | |
60 | // 'summary' => 'This is a summary', | |
61 | // 'total' => 50.0, | |
62 | // ); | |
63 | } | |
64 | ||
65 | /** | |
eceb18cc | 66 | * Get a list of displayable columns. |
6a488035 TO |
67 | * |
68 | * @return array, keys are printable column headers and values are SQL column names | |
69 | */ | |
608e6658 | 70 | public function &columns() { |
6a488035 TO |
71 | // return by reference |
72 | $columns = array( | |
7b99ead3 | 73 | ts('Contact ID') => 'contact_id', |
6a488035 TO |
74 | ts('Contact Type') => 'contact_type', |
75 | ts('Name') => 'sort_name', | |
76 | ts('State') => 'state_province', | |
77 | ); | |
78 | return $columns; | |
79 | } | |
80 | ||
81 | /** | |
eceb18cc | 82 | * Construct a full SQL query which returns one page worth of results. |
6a488035 | 83 | * |
dd244018 EM |
84 | * @param int $offset |
85 | * @param int $rowcount | |
86 | * @param null $sort | |
87 | * @param bool $includeContactIDs | |
88 | * | |
6a488035 TO |
89 | * @return string, sql |
90 | */ | |
608e6658 | 91 | public function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE) { |
6a488035 TO |
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 | /** | |
eceb18cc | 97 | * Construct a SQL SELECT clause. |
6a488035 TO |
98 | * |
99 | * @return string, sql fragment with SELECT arguments | |
100 | */ | |
608e6658 | 101 | public function select() { |
6a488035 TO |
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 | /** | |
eceb18cc | 111 | * Construct a SQL FROM clause. |
6a488035 TO |
112 | * |
113 | * @return string, sql fragment with FROM and JOIN clauses | |
114 | */ | |
608e6658 | 115 | public function from() { |
6a488035 TO |
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 | /** | |
eceb18cc | 127 | * Construct a SQL WHERE clause. |
6a488035 | 128 | * |
2a6da8d7 EM |
129 | * @param bool $includeContactIDs |
130 | * | |
6a488035 TO |
131 | * @return string, sql fragment with conditional expressions |
132 | */ | |
608e6658 | 133 | public function where($includeContactIDs = FALSE) { |
affcc9d2 | 134 | $params = []; |
6a488035 TO |
135 | $where = "contact_a.contact_type = 'Household'"; |
136 | ||
137 | $count = 1; | |
affcc9d2 | 138 | $clause = []; |
6a488035 TO |
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 | /** | |
eceb18cc | 173 | * Determine the Smarty template for the search screen. |
6a488035 TO |
174 | * |
175 | * @return string, template path (findable through Smarty template path) | |
176 | */ | |
608e6658 | 177 | public function templateFile() { |
6a488035 TO |
178 | return 'CRM/Contact/Form/Search/Custom.tpl'; |
179 | } | |
180 | ||
181 | /** | |
eceb18cc | 182 | * Modify the content of each row. |
6a488035 | 183 | * |
e16033b4 TO |
184 | * @param array $row |
185 | * Modifiable SQL result row. | |
6a488035 TO |
186 | * @return void |
187 | */ | |
608e6658 | 188 | public function alterRow(&$row) { |
6a488035 TO |
189 | $row['sort_name'] .= ' ( altered )'; |
190 | } | |
191 | ||
192 | } |