OSDN Git Service

git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@882 1ca29b6e-896d...
[nucleus-jp/nucleus-plugins.git] / NP_UpdatePingServer / trunk / updatepingserver / admin / PlugTemplate.php
1 <?php
2
3 class PlugTemplate
4 {
5         var $table;
6         var $primarykey;
7         var $idkey;
8         var $desckey;
9         
10         function PlugTemplate($table, $primarykey, $namekey, $descriptionkey='')
11         {
12                 $this->table = $table;
13                 $this->idkey = '`'.$primarykey.'`';
14                 $this->namekey = '`'.$namekey.'`';
15                 $this->desckey = '`'.$descriptionkey.'`';
16         }
17
18         function getIdFromName($name)
19         {
20                 return quickQuery('SELECT '.$this->idkey.' as result FROM '.$this->table.' WHERE '.$this->namekey.'="'.addslashes($name).'"');
21         }
22
23         function getNameFromID($id)
24         {
25                 return quickQuery('SELECT '.$this->namekey.' as result FROM '.$this->table.' WHERE '.$this->idkey.'='.intval($id));
26         }
27         
28         function getDataFromID($dataname, $id)
29         {
30                 return quickQuery('SELECT `'.addslashes($dataname).'` as result FROM '.$this->table.' WHERE '.$this->idkey.'='.intval($id));
31         }
32         
33         function exists($name)
34         {
35                 $res = sql_query('SELECT * FROM '.$this->table.' WHERE '.$this->namekey.'="'.addslashes($name).'"');
36                 return (mysql_num_rows($res) != 0);
37         }
38         
39         function existsID($id)
40         {
41                 $res = sql_query('select * FROM '.$this->table.' WHERE '.$this->idkey.'='.intval($id));
42                 return (mysql_num_rows($res) != 0);
43         }
44
45         function getTemplateList($w='', $s='')
46         {
47                 $where = '';
48                 if ($w != '') $where = ' WHERE '.$w;
49                 
50                 $query = 'SELECT '.$this->idkey.' as id, '.$this->namekey.' as name';
51                 if ($this->desckey) $query .= ', '.$this->desckey.' as description';
52                 if ($s) $query .= ', '.$s;
53                 $query .= ' FROM '.$this->table. $where .' ORDER BY '.$this->namekey;
54
55                 $res = sql_query($query);
56                 $templates = array();
57                 while ($a = mysql_fetch_assoc($res)) {
58                         $templates[] = $a;
59                 }
60                 return $templates;
61         }
62
63         function getTemplateDesc($id)
64         {
65                 $where = ' WHERE '.$this->idkey.'='.intval($id);
66                 $query = 'SELECT '.$this->idkey.' as id, '.$this->namekey.' as name';
67                 if ($this->desckey) $query .= ', '.$this->desckey.' as description';
68                 $query .= ' FROM '.$this->table. $where .' ORDER BY '.$this->namekey;
69
70                 $res = sql_query($query);
71                 return mysql_fetch_assoc($res);
72         }
73
74         function read($name)
75         {
76                 $query = 'SELECT * FROM '.$this->table.' WHERE '.$this->namekey.'="'.addslashes($name).'"';
77                 $res = sql_query($query);
78                 return mysql_fetch_assoc($res);
79         }
80
81         function readFromID($id)
82         {
83                 $query = 'SELECT * FROM '.$this->table.' WHERE '.$this->idkey.'='.intval($id);
84                 $res = sql_query($query);
85                 return mysql_fetch_assoc($res);
86         }
87
88         function createTemplate($name, $desc='')
89         {
90                 $query = 'INSERT INTO '.$this->table.' SET '.$this->namekey.'="'. addslashes($name).'"';
91                 if ($desc && $this->desckey) $query .= ', '.$this->desckey.'="'.addslashes($desc).'"';
92                 sql_query($query);
93                 $newid = mysql_insert_id();
94                 return $newid;
95         }
96
97         function updateTemplate($id, $template)
98         {
99                 $query = 'UPDATE '.$this->table.' SET ';
100                 foreach ($template as $k => $v) {
101                         $query .= $k.'="'.addslashes($v).'",';
102                 }
103                 $query = substr($query,0,-1);
104                 $query .= ' WHERE '.$this->idkey.'='.intval($id);
105                 sql_query($query);
106         }
107
108         function deleteTemplate($id)
109         {
110                 sql_query('DELETE FROM '.$this->table.' WHERE '.$this->idkey.'=' . intval($id));
111         }
112
113 }
114
115 ?>