1<?php
2
3/* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * Wiki page template
7 *
8 * @author Alex Killing <alex.killing@gmx.de>
9 * @version $Id$
10 * @ingroup ModulesWiki
11 */
12class ilWikiPageTemplate
13{
14    /**
15     * @var ilDB
16     */
17    protected $db;
18
19    const TYPE_ALL = 0;
20    const TYPE_NEW_PAGES = 1;
21    const TYPE_ADD_TO_PAGE = 2;
22
23    protected $wiki_id;
24    protected $ilDB;
25
26    /**
27     * Constructor
28     *
29     * @param int $a_wiki_id wiki id
30     */
31    public function __construct($a_wiki_id)
32    {
33        global $DIC;
34
35        $ilDB = $DIC->database();
36
37        $this->wiki_id = $a_wiki_id;
38        $this->db = $ilDB;
39    }
40
41    /**
42     * Get all info
43     */
44    public function getAllInfo($a_type = self::TYPE_ALL)
45    {
46        $and = "";
47        if ($a_type == self::TYPE_NEW_PAGES) {
48            $and = " AND t.new_pages = " . $this->db->quote(1, "integer");
49        }
50        if ($a_type == self::TYPE_ADD_TO_PAGE) {
51            $and = " AND t.add_to_page = " . $this->db->quote(1, "integer");
52        }
53
54        $set = $this->db->query(
55            $q = "SELECT t.wiki_id, t.wpage_id, p.title, t.new_pages, t.add_to_page FROM wiki_page_template t JOIN il_wiki_page p ON " .
56            " (t.wpage_id = p.id) " .
57            " WHERE t.wiki_id = " . $this->db->quote($this->wiki_id, "integer") .
58            $and
59        );
60        $templates = array();
61        while ($rec = $this->db->fetchAssoc($set)) {
62            $templates[] = $rec;
63        }
64        return $templates;
65    }
66
67    /**
68     * Add wiki page template
69     *
70     * @param int $a_id wiki page id
71     */
72    public function save($a_id, $a_new_pages = 0, $a_add_to_page = 0)
73    {
74        if ($a_id <= 0) {
75            return;
76        }
77
78        $set = $this->db->query(
79            "SELECT * FROM wiki_page_template " .
80            " WHERE wiki_id = " . $this->db->quote($this->wiki_id, "integer") .
81            " AND wpage_id = " . $this->db->quote($a_id, "integer")
82        );
83        if (!$this->db->fetchAssoc($set)) {
84            $this->db->manipulate("INSERT INTO wiki_page_template " .
85                "(wiki_id, wpage_id, new_pages, add_to_page) VALUES (" .
86                $this->db->quote($this->wiki_id, "integer") . "," .
87                $this->db->quote($a_id, "integer") . "," .
88                $this->db->quote($a_new_pages, "integer") . "," .
89                $this->db->quote($a_add_to_page, "integer") .
90                ")");
91        } else {
92            $this->db->manipulate(
93                "UPDATE wiki_page_template SET " .
94                " new_pages = " . $this->db->quote($a_new_pages, "integer") . "," .
95                " add_to_page = " . $this->db->quote($a_add_to_page, "integer") .
96                " WHERE wiki_id = " . $this->db->quote($this->wiki_id, "integer") .
97                " AND wpage_id = " . $this->db->quote($a_id, "integer")
98            );
99        }
100    }
101
102    /**
103     * Remove template status of a page
104     *
105     * @param int $a_id wiki page id
106     */
107    public function remove($a_id)
108    {
109        $this->db->manipulate(
110            "DELETE FROM wiki_page_template WHERE " .
111            " wiki_id = " . $this->db->quote($this->wiki_id, "integer") .
112            " AND wpage_id = " . $this->db->quote($a_id, "integer")
113        );
114    }
115
116    /**
117     * Is page set as template?
118     *
119     * @param int $a_id wiki page id
120     * @return type bool
121     */
122    public function isPageTemplate($a_id)
123    {
124        $set = $this->db->query("SELECT t.wpage_id" .
125            " FROM wiki_page_template t" .
126            " JOIN il_wiki_page p ON " .
127            " (t.wpage_id = p.id) " .
128            " WHERE t.wiki_id = " . $this->db->quote($this->wiki_id, "integer") .
129            " AND p.id = " . $this->db->quote($a_id, "integer"));
130        return (bool) $this->db->numRows($set);
131    }
132}
133