aboutsummaryrefslogtreecommitdiff
path: root/app/class/Modelhome.php
blob: 943f4bd3a1f592cec8f9346842dd8c3a8cdb399e (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php

namespace Wcms;

class Modelhome extends Modelpage
{
 	
	public function __construct() {
		parent::__construct();
	}

    public function optinit($table)
    {

        $opt = new Opt(Page::classvarlist());
        $opt->setcol(['id', 'tag', 'linkfrom', 'linkto', 'description', 'title', 'datemodif', 'datecreation', 'date', 'secure', 'authors', 'visitcount', 'editcount', 'affcount']);
        $opt->settaglist($table);
        $opt->setauthorlist($table);
        $opt->submit();

        return $opt;
    }

    /**
     * Initialise Optlist object using
     * 
     * @param array $table the list of all pages objects
     * 
     * @return Optlist Object initialized
     */
    public function Optlistinit(array $table)
    {
        $optlist = new Optlist(Page::classvarlist());
        $optlist->settaglist($table);
        $optlist->setauthorlist($table);

        return $optlist;
    }




    /**
     * @param array $table
     * @param Opt $opt
     * @param string $regex
     */
    public function table2(array $table, Opt $opt, string $regex = "", array $searchopt = [])
    {


        $filtertagfilter = $this->filtertagfilter($table, $opt->tagfilter(), $opt->tagcompare());
        $filterauthorfilter = $this->filterauthorfilter($table, $opt->authorfilter(), $opt->authorcompare());
        $filtersecure = $this->filtersecure($table, $opt->secure());

        $filter = array_intersect($filtertagfilter, $filtersecure, $filterauthorfilter);
        $table2 = [];
        $table2invert = [];
        foreach ($table as $page) {
            if (in_array($page->id(), $filter)) {
                $table2[] = $page;
            } else {
                $table2invert[] = $page;
            }


        }

        if (!empty($opt->invert())) {
            $table2 = $table2invert;
        }

        if(!empty($regex)) {
            $table2 = $this->deepsearch($regex, $searchopt, $table2);
        }

        $this->pagelistsort($table2, $opt->sortby(), $opt->order());

        if($opt->limit() !== 0) {
            $table2 = array_slice($table2, 0, $opt->limit());
        }


        return $table2;
    }


	/**
	 * Search for regex and count occurences
	 * 
	 * @param string $regex Regex to match.
	 * @param array $options Option search, could be `content` `title` `description`.
	 * @param array $page list Array of Pages.
	 * 
	 * @return array associative array of Pages
	 */
	public function deepsearch(string $regex, array $options, array $pagelist) : array
	{
        if($options['casesensitive']) {
            $case = '';
        } else {
            $case = 'i';
        }
        $regex = '/' . $regex . '/' . $case;
        $pageselected = [];
		foreach ($pagelist as $page) {
			$count = 0;
			if($options['content']) {
				$count += preg_match($regex, $page->main());
				$count += preg_match($regex, $page->nav());
				$count += preg_match($regex, $page->aside());
				$count += preg_match($regex, $page->header());
				$count += preg_match($regex, $page->footer());
			}
			if ($options['other']) {
				$count += preg_match($regex, $page->body());
				$count += preg_match($regex, $page->css());
				$count += preg_match($regex, $page->javascript());
			}
			if ($options['title']) {
				$count += preg_match($regex, $page->title());
			}
			if ($options['description']) {
				$count += preg_match($regex, $page->description());
			}
			if ($count !== 0) {
				$pageselected[] = $page;
			}
		}
		return $pageselected;
	}


    /**
     * @param array array of the columns to show from the user
     * 
     * @return array assoc each key columns to a boolean value to show or not
     */
    public function setcolumns(array $columns) : array
    {
        foreach (Model::COLUMNS as $col) {
            if(in_array($col, $columns)) {
                $showcols[$col] = true;
            } else {
                $showcols[$col] = false;
            }
        }
        return $showcols;
    }
}








?>