Newer
Older
<?php
/*********************************************************************
class.page.php
Page class
Copyright (c) 2006-2013 osTicket
http://www.osticket.com
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
class Page {
var $id;
var $ht;
function Page($id, $lang=false) {
function load($id=0, $lang=false) {
if(!$id && !($id=$this->getId()))
return false;
$sql='SELECT page.*, count(topic.page_id) as topics '
.' FROM '.PAGE_TABLE.' page '
.' LEFT JOIN '.TOPIC_TABLE. ' topic ON(topic.page_id=page.id) '
. ' WHERE page.content_id='.db_input($id)
. ($lang ? ' AND lang='.db_input($lang) : '')
.' GROUP By page.id';
if (!($res=db_query($sql)) || !db_num_rows($res))
return false;
$this->ht = db_fetch_array($res);
$this->id = $this->ht['id'];
$this->attachments = new GenericAttachments($this->id, 'P');
return true;
}
function reload() {
return $this->load();
}
function getId() {
return $this->id;
}
function getHashtable() {
return $this->ht;
}
function getType() {
return $this->ht['type'];
}
function getName() {
return $this->ht['name'];
}
function getBody() {
return $this->ht['body'];
}
function getLocalBody() {
$tag = $this->getTranslateTag('body');
$T = CustomDataTranslation::translateArticle($tag);
return $T != $tag ? $T : $this->getBody();
}
function getBodyWithImages() {
return Format::viewableImages($this->getLocalBody(), ROOT_PATH.'image.php');
function getNotes() {
return $this->ht['notes'];
}
function isActive() {
return ($this->ht['isactive']);
}
function isInUse() {
global $cfg;
return ($this->getNumTopics()
|| in_array($this->getId(), $cfg->getDefaultPages()));
}
function getCreateDate() {
return $this->ht['created'];
}
function getUpdateDate() {
return $this->ht['updated'];
}
function getNumTopics() {
return $this->ht['topics'];
}
function getTranslateTag($subtag) {
return _H(sprintf('page.%s.%s', $subtag, $this->id));
}
function getLocal($subtag) {
$tag = $this->getTranslateTag($subtag);
$T = CustomDataTranslation::translate($tag);
return $T != $tag ? $T : $this->ht[$subtag];
}
function update($vars, &$errors) {
if(!$vars['isactive'] && $this->isInUse()) {
$errors['err'] = __('A page currently in-use CANNOT be disabled!');
$errors['isactive'] = __('Page is in-use!');
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
}
if($errors || !$this->save($this->getId(), $vars, $errors))
return false;
$this->reload();
return true;
}
function disable() {
if(!$this->isActive())
return true;
if($this->isInUse())
return false;
$sql=' UPDATE '.PAGE_TABLE.' SET isactive=0 '
.' WHERE id='.db_input($this->getId());
if(!db_query($sql) || !db_affected_rows())
return false;
$this->reload();
return true;
}
function delete() {
if($this->isInUse())
return false;
$sql='DELETE FROM '.PAGE_TABLE
.' WHERE id='.db_input($this->getId())
.' LIMIT 1';
if(!db_query($sql) || !db_affected_rows())
return false;
db_query('UPDATE '.TOPIC_TABLE.' SET page_id=0 WHERE page_id='.db_input($this->getId()));
return true;
}
/* ------------------> Static methods <--------------------- */
function add($vars, &$errors) {
if(!($id=self::create($vars, $errors)))
return false;
return self::lookup($id);
}
function create($vars, &$errors) {
return self::save(0, $vars, $errors);
}
function getPages($criteria=array()) {
$sql = ' SELECT id FROM '.PAGE_TABLE.' WHERE 1';
if(isset($criteria['active']))
$sql.=' AND isactive='.db_input($criteria['active']?1:0);
if(isset($criteria['type']))
$sql.=' AND `type`='.db_input($criteria['type']);
$sql.=' ORDER BY name';
$pages = array();
if(($res=db_query($sql)) && db_num_rows($res))
while(list($id) = db_fetch_row($res))
$pages[] = Page::lookup($id);
return array_filter($pages);
}
function getActivePages($criteria=array()) {
$criteria = array_merge($criteria, array('active'=>true));
return self::getPages($criteria);
}
function getActiveThankYouPages() {
return self::getActivePages(array('type' => 'thank-you'));
}
function getIdByName($name, $lang=false) {
$id = 0;
$sql = ' SELECT id FROM '.PAGE_TABLE.' WHERE name='.db_input($name);
if ($lang)
$sql .= ' AND lang='.db_input($lang);
if(($res=db_query($sql)) && db_num_rows($res))
list($id) = db_fetch_row($res);
return $id;
}
function getIdByType($type, $lang=false) {
$id = 0;
$sql = ' SELECT id FROM '.PAGE_TABLE.' WHERE `type`='.db_input($type);
if ($lang)
$sql .= ' AND lang='.db_input($lang);
if(($res=db_query($sql)) && db_num_rows($res))
list($id) = db_fetch_row($res);
return $id;
}
function lookup($id) {
return ($id
&& is_numeric($id)
&& ($p= new Page($id))
&& $p->getId()==$id)
? $p : null;
}
function save($id, $vars, &$errors) {
//Cleanup.
$vars['name']=Format::striptags(trim($vars['name']));
//validate
if($id && $id!=$vars['id'])
$errors['err'] = __('Internal error. Try again');
$errors['type'] = __('Type is required');
$errors['name'] = __('Name is required');
elseif(($pid=self::getIdByName($vars['name'])) && $pid!=$id)
$errors['name'] = __('Name already exists');
$errors['body'] = __('Page body is required');
if($errors) return false;
//save
$sql=' updated=NOW() '
.', `type`='.db_input($vars['type'])
.', name='.db_input($vars['name'])
.', body='.db_input(Format::sanitize($vars['body']))
.', isactive='.db_input($vars['isactive'] ? 1 : 0)
.', notes='.db_input(Format::sanitize($vars['notes']));
if($id) {
$sql='UPDATE '.PAGE_TABLE.' SET '.$sql.' WHERE id='.db_input($id);
if (db_query($sql))
return $this->saveTranslations($vars, $errors);
$errors['err']=sprintf(__('Unable to update %s.'), __('this site page'));
} else {
$sql='INSERT INTO '.PAGE_TABLE.' SET '.$sql.', created=NOW()';
if (!db_query($sql) || !($id=db_insert_id())) {
$errors['err']=sprintf(__('Unable to create %s.'), __('this site page'))
.' '.__('Internal error occurred');
$sql = 'UPDATE '.PAGE_TABLE.' SET `content_id`=`id`'
.' WHERE id='.db_input($id);
if (!db_query($sql))
return false;
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
function saveTranslations($vars, &$errors) {
global $thisstaff;
$tag = $this->getTranslateTag('body');
$translations = CustomDataTranslation::allTranslations($tag,'article');
foreach ($translations as $t) {
foreach ($vars['trans'] as $lang=>$content) {
if (strcasecmp($lang, $t->lang) !== 0)
continue;
$content = Format::sanitize($content);
unset($vars['trans'][$lang]);
if ($content == $t->text)
continue;
$t->text = $content;
$t->agent_id = $thisstaff->getId();
if (!$t->save())
return false;
}
}
// New translations (?)
foreach ($vars['trans'] as $lang=>$content) {
$content = Format::sanitize($content);
if (!$content)
continue;
$t = CustomDataTranslation::create(array(
'type' => 'article',
'object_hash' => $tag,
'lang' => $lang,
'text' => $content,
'revision' => 1,
'agent_id' => $thisstaff->getId(),
'updated' => new SqlFunction('NOW'),
));
if (!$t->save())
return false;
}
return true;
}