1<?php
2
3namespace dokuwiki\plugin\struct\types;
4
5use dokuwiki\plugin\struct\meta\QueryBuilder;
6use dokuwiki\plugin\struct\meta\QueryBuilderWhere;
7
8//We prefixing this type with "Abstract" to hide it in Schema Editor
9class AutoSummary extends AbstractBaseType
10{
11    /**
12     * When handling `%lastsummary%` get the data from the `titles` table instead the `data_` table.
13     *
14     * @param QueryBuilder $QB
15     * @param string $tablealias
16     * @param string $colname
17     * @param string $alias
18     */
19    public function select(QueryBuilder $QB, $tablealias, $colname, $alias)
20    {
21        $rightalias = $QB->generateTableAlias();
22        $QB->addLeftJoin($tablealias, 'titles', $rightalias, "$tablealias.pid = $rightalias.pid");
23        $QB->addSelectStatement("$rightalias.lastsummary", $alias);
24    }
25
26    /**
27     * When sorting `%lastsummary%`, then sort the data from the `titles` table instead the `data_` table.
28     *
29     * @param QueryBuilder $QB
30     * @param string $tablealias
31     * @param string $colname
32     * @param string $order
33     */
34    public function sort(QueryBuilder $QB, $tablealias, $colname, $order)
35    {
36        $rightalias = $QB->generateTableAlias();
37        $QB->addLeftJoin($tablealias, 'titles', $rightalias, "$tablealias.pid = $rightalias.pid");
38        $QB->addOrderBy("$rightalias.lastsummary $order");
39    }
40
41    /**
42     * When using `%lastsummary%`, we need to compare against the `title` table.
43     *
44     * @param QueryBuilderWhere $add
45     * @param string $tablealias
46     * @param string $colname
47     * @param string $comp
48     * @param string|\string[] $value
49     * @param string $op
50     */
51    public function filter(QueryBuilderWhere $add, $tablealias, $colname, $comp, $value, $op)
52    {
53        $QB = $add->getQB();
54        $rightalias = $QB->generateTableAlias();
55        $QB->addLeftJoin($tablealias, 'titles', $rightalias, "$tablealias.pid = $rightalias.pid");
56
57        // compare against page and title
58        $sub = $add->where($op);
59        $pl = $QB->addValue($value);
60        $sub->whereOr("$rightalias.lastsummary $comp $pl");
61    }
62}
63