-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPagesSum.module
60 lines (51 loc) · 2.18 KB
/
PagesSum.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
class PagesSum extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'Pages Sum',
'version' => '1.0.0',
'summary' => 'Adds a $pages->sum($selectorString, $fieldName) method to sum the value of a specific field over a list of pages selected by a selector string.',
'singular' => true,
'autoload' => true,
'author' => 'ESRCH',
'href' => 'https://processwire.com/talk/topic/8524-creating-a-fast-pages-sum-function/?do=findComment&comment=82519',
'requires' => 'ProcessWire>2.7.0',
);
}
public function init() {
$this->addHook('Pages::sum', function ($event) {
$selectorString = $event->arguments(0);
$fieldName = $event->arguments(1);
// Find all the pages associated with the selector string
$pageFinder = new PageFinder();
$idQuery = $pageFinder->findIDs(new Selectors($selectorString), array('returnQuery' => true));
$idQuery->set('orderby', array());
$idQuery->set('groupby', array());
$idQuery->set('limit', array());
$stmt = $idQuery->execute();
$idArray = $stmt->fetchAll(PDO::FETCH_COLUMN);
// If no pages were found, return 0
if (count($idArray) == 0) {
$event->return = 0;
return;
}
$idString = implode(',', $idArray);
// Get the table name for the given field name
$field = $this->fields->get($fieldName);
// If no field with this name is found, return 0;
if (!$field) {
$event->return = 0;
return;
}
$tableName = $field->getTable();
// Run the SUM query
$sumQuery = new DatabaseQuerySelect();
$sumQuery->select("SUM(data)");
$sumQuery->from($tableName);
$sumQuery->where("pages_id IN ($idString)");
$stmt2 = $sumQuery->execute();
list($total) = $stmt2->fetch(PDO::FETCH_NUM);
$event->return = $total;
});
}
}