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