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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
<?php
// +---------------------------------------------------------------------------+
// This file is part of Stud.IP
//
// Copyright (C) 2014 Arne Schröder <schroeder@data-quest>,
// Suchi & Berg GmbH <info@data-quest.de>
// +---------------------------------------------------------------------------+
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or any later version.
// +---------------------------------------------------------------------------+
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// +---------------------------------------------------------------------------+
//require_once 'lib/object.inc.php';
/**
* HelpContent.php - model class for Stud.IP help content
*
* @author Arne Schröder <schroeder@data-quest>
* @access public
*
* @property string $id alias column for content_id
* @property string $global_content_id database column
* @property string $content_id database column
* @property string $language database column
* @property string $content database column
* @property string $route database column
* @property string $studip_version database column
* @property int $position database column
* @property int $custom database column
* @property int $visible database column
* @property string $author_email database column
* @property string $installation_id database column
* @property int $mkdate database column
* @property int $chdate database column
* @property string|null $comment database column
* @property User $author has_one User
*/
class HelpContent extends SimpleORMap
{
/**
* configure SORM
*
* @param array $config configuration
*/
protected static function configure($config = [])
{
$config['db_table'] = 'help_content';
$config['has_one']['author'] = [
'class_name' => User::class,
'foreign_key' => 'author_email',
'assoc_func' => 'findOneByEmail',
];
$config['registered_callbacks']['before_store'][] = 'cbUpdateStudipVersion';
parent::configure($config);
}
/**
* fetches set of content from database for given route
*
* @param string $route route for help content
* @param string $language language
* @return array set of help content
*/
public static function GetContentByRoute($route = '', $language = '')
{
$language = $language ?: mb_substr($GLOBALS['user']->preferred_language, 0, 2);
if (!$language) {
$language = mb_substr(Config::get()->DEFAULT_LANGUAGE, 0, 2);
}
$route = get_route($route);
$query = "SELECT *
FROM help_content
WHERE route LIKE CONCAT(?, '%') AND language = ? AND visible = 1";
$statement = DBManager::get()->prepare($query);
$statement->execute([$route, $language]);
$ret = $statement->fetchGrouped(PDO::FETCH_ASSOC);
foreach ($ret as $index => $data)
if (! match_route($data['route'], $route))
unset($ret[$index]);
return $ret;
}
/**
* fetches content for given content_id
*
* @param string $id id of help content
* @return array help content object
*/
public static function GetContentByID($id = '')
{
$query = "SELECT content_id AS idx, help_content.*
FROM help_content
WHERE content_id = ?";
$statement = DBManager::get()->prepare($query);
$statement->execute([$id]);
$ret = $statement->fetchGrouped(PDO::FETCH_ASSOC);
return current(HelpContent::GetContentObjects($ret));
}
/**
* fetches set of help content from database filtered by parameters
*
* @param string $term search term for content
* @param boolean $as_objects include HelpContent objects in result array
* @return array set of help content
*/
public static function GetContentByFilter($term = '')
{
$params = [];
$condition = '';
if (mb_strlen(trim($term)) >= 3) {
$condition = "WHERE content LIKE CONCAT('%', ?, '%')";
$params[] = $term;
}
$query = "SELECT content_id AS idx, help_content.*
FROM help_content
$condition
ORDER BY route ASC";
$statement = DBManager::get()->prepare($query);
$statement->execute($params);
$ret = $statement->fetchGrouped(PDO::FETCH_ASSOC);
return HelpContent::GetContentObjects($ret);
}
/**
* fetches help content conflicts
*
* @return array set of help content
*/
public static function GetConflicts()
{
$conflicts = [];
$query = "SELECT content_id AS idx, help_content.*
FROM help_content
WHERE installation_id = ?
ORDER BY route";
$statement = DBManager::get()->prepare($query);
$statement->execute([Config::get()->STUDIP_INSTALLATION_ID]);
$ret = $statement->fetchGrouped(PDO::FETCH_ASSOC);
foreach ($ret as $index => $data) {
$query = "SELECT content_id AS idx, help_content.*
FROM help_content
WHERE global_content_id = ? AND language = ? AND studip_version >= ? AND installation_id <> ?
ORDER BY studip_version DESC LIMIT 1";
$statement = DBManager::get()->prepare($query);
$statement->execute([$data['global_content_id'], $data['language'], $data['studip_version'], Config::get()->STUDIP_INSTALLATION_ID]);
$ret2 = $statement->fetchGrouped(PDO::FETCH_ASSOC);
if (count($ret2)) {
$conflicts[] = HelpContent::GetContentObjects(array_merge([$index => $data], $ret2));
}
}
return $conflicts;
}
/**
* builds help content objects for given set of content data
*
* @param array $content_result content set
* @return array set of content objects
*/
public static function GetContentObjects($content_result)
{
$objects = [];
if (is_array($content_result)){
foreach($content_result as $id => $result){
$objects[$id] = new HelpContent();
$objects[$id]->setData($result, true);
$objects[$id]->setNew(false);
}
}
return $objects;
}
public function cbUpdateStudipVersion()
{
$this->studip_version = StudipVersion::getStudipVersion();
}
}
|