Newer
Older
<?php
/*********************************************************************
class.category.php
Backend support for article categories.
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
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 Category {
var $id;
var $ht;
function Category($id) {
$this->id=0;
$this->load($id);
}
function load($id) {
$sql=' SELECT cat.*,count(faq.faq_id) as faqs '
.' FROM '.FAQ_CATEGORY_TABLE.' cat '
.' LEFT JOIN '.FAQ_TABLE.' faq ON(faq.category_id=cat.category_id) '
.' WHERE cat.category_id='.db_input($id)
.' GROUP BY cat.category_id';
if (!($res=db_query($sql)) || !db_num_rows($res))
return false;
$this->ht = db_fetch_array($res);
$this->id = $this->ht['category_id'];
return true;
}
function reload() {
return $this->load($this->getId());
}
/* ------------------> Getter methods <--------------------- */
function getId() { return $this->id; }
function getName() { return $this->ht['name']; }
function getNumFAQs() { return $this->ht['faqs']; }
function getDescription() { return $this->ht['description']; }
function getNotes() { return $this->ht['notes']; }
function getCreateDate() { return $this->ht['created']; }
function getUpdateDate() { return $this->ht['updated']; }
function isPublic() { return ($this->ht['ispublic']); }
function getHashtable() { return $this->ht; }
/* ------------------> Setter methods <--------------------- */
function setName($name) { $this->ht['name']=$name; }
function setNotes($notes) { $this->ht['notes']=$notes; }
function setDescription($desc) { $this->ht['description']=$desc; }
/* --------------> Database access methods <---------------- */
function update($vars, &$errors) {
if(!$this->save($this->getId(), $vars, $errors))
return false;
//TODO: move FAQs if requested.
$this->reload();
return true;
}
function delete() {
$sql='DELETE FROM '.FAQ_CATEGORY_TABLE
.' WHERE category_id='.db_input($this->getId())
.' LIMIT 1';
if(db_query($sql) && ($num=db_affected_rows())) {
db_query('DELETE FROM '.FAQ_TABLE
.' WHERE category_id='.db_input($this->getId()));
}
return $num;
}
/* ------------------> Static methods <--------------------- */
function lookup($id) {
return ($id && is_numeric($id) && ($c = new Category($id)))?$c:null;
}
function findIdByName($name) {
$sql='SELECT category_id FROM '.FAQ_CATEGORY_TABLE.' WHERE name='.db_input($name);
list($id) = db_fetch_row(db_query($sql));
return $id;
}
function findByName($name) {
if(($id=self::findIdByName($name)))
return new Category($id);
return false;
}
function validate($vars, &$errors) {
return self::save(0, $vars, $errors,true);
}
function create($vars, &$errors) {
return self::save(0, $vars, $errors);
}
function save($id, $vars, &$errors, $validation=false) {
//Cleanup.
$vars['name']=Format::striptags(trim($vars['name']));
$errors['err']=__('Internal error occurred');
$errors['name']=__('Category name is required');
$errors['name']=__('Name is too short. 3 chars minimum');
elseif(($cid=self::findIdByName($vars['name'])) && $cid!=$id)
$errors['name']=__('Category already exists');
$errors['description']=__('Category description is required');
if($errors) return false;
/* validation only */
if($validation) return true;
//save
$sql=' updated=NOW() '.
',ispublic='.db_input(isset($vars['ispublic'])?$vars['ispublic']:0).
',name='.db_input($vars['name']).
',description='.db_input(Format::sanitize($vars['description'])).
',notes='.db_input(Format::sanitize($vars['notes']));
if($id) {
$sql='UPDATE '.FAQ_CATEGORY_TABLE.' SET '.$sql.' WHERE category_id='.db_input($id);
if(db_query($sql))
return true;
$errors['err']=sprintf(__('Unable to update %s.'), __('this FAQ category'));
} else {
$sql='INSERT INTO '.FAQ_CATEGORY_TABLE.' SET '.$sql.',created=NOW()';
if(db_query($sql) && ($id=db_insert_id()))
return $id;
$errors['err']=sprintf(__('Unable to create %s.'), __('this FAQ category'))
.' '.__('Internal error occurred');