Skip to content

Commit 44d71ec

Browse files
committed
Merge remote-tracking branch 'origin/issue66-add-file-searchapi' into issue66-add-template
2 parents 63e5b99 + bf17c2b commit 44d71ec

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

classes/search/cmsfield.php

+56
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,60 @@ protected function get_data($id) {
158158
}
159159
return $this->cmsdata[$id];
160160
}
161+
162+
/**
163+
* Returns true if this area uses file indexing.
164+
*
165+
* @return bool
166+
*/
167+
public function uses_file_indexing() {
168+
return true;
169+
}
170+
171+
/**
172+
* Return the context info required to index files for
173+
* this search area.
174+
*
175+
* @return array
176+
*/
177+
public function get_search_fileareas() {
178+
return ['value'];
179+
}
180+
181+
/**
182+
* Add the cms file attachments.
183+
*
184+
* @param document $document The current document
185+
* @return null
186+
*/
187+
public function attach_files($document) {
188+
global $DB;
189+
190+
$fileareas = $this->get_search_fileareas();
191+
// File is in "customfield_file" for component, "value" for filearea, and for customfield data id for itemid.
192+
$contextid = \context_system::instance()->id;
193+
$component = 'customfield_file';
194+
$cmsid = $document->get('itemid');
195+
196+
// Search customfield data from cms record.
197+
$sql = "SELECT mcd.id
198+
FROM {cms} mc
199+
JOIN {customfield_data} mcd ON mc.id = mcd.instanceid
200+
JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid
201+
JOIN {customfield_category} mcc ON mcf.categoryid = mcc.id
202+
WHERE mc.id = ? AND mcc.component = 'mod_cms' AND mcc.area = 'cmsfield' AND mcf.type = 'file'";
203+
$param = [$cmsid];
204+
$filedata = $DB->get_records_sql($sql, $param);
205+
206+
foreach ($fileareas as $filearea) {
207+
foreach ($filedata as $data) {
208+
$fs = get_file_storage();
209+
$files = $fs->get_area_files($contextid, $component, $filearea, $data->id, '', false);
210+
211+
foreach ($files as $file) {
212+
$document->add_stored_file($file);
213+
}
214+
}
215+
}
216+
}
161217
}

0 commit comments

Comments
 (0)