root/efio/autoname.class.php

Revision 8, 4.9 kB (checked in by teiko, 1 year ago)

Расширенный режим efio/admin. Можно выбрать несколько элементов из базы и применить к ним общее действие

Line 
1 <?php
2 defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');
3 setlocale(LC_CTYPE, 'ru_RU.UTF-8');
4 mb_internal_encoding('UTF-8');
5 // --------------------------------------------------------------------------
6
7 class tbAutoName extends mosDBTable {
8     var $name           = '';
9     var $correct    = 0;    // tinyint(1)
10
11     function tbAutoName($table, $key, &$_db) {
12     $this->mosDBTable($table ,$key, $_db);
13     }
14
15     function add($name) {
16     if (!$this->check($name)) return false;
17     $name = str_replace("'", "''", $name);
18     $query = "INSERT INTO `$this->_tbl`(`name`) VALUES(UPPER('$name'));";
19     $this->_db->setQuery($query);
20     if(!$this->_db->query()) {
21         echo $this->_db->stderr();
22         return false;
23     }
24     return true;
25     }
26     
27     // Проверяет, можно ли добавить имя в таблицу
28     function check($name) {
29     if(empty($name)) return false;
30     $name = str_replace("'", "''", $name);
31     $where_q = "WHERE $this->_tbl_key = UPPER('$name')";
32     $query = "SELECT COUNT(*) FROM $this->_tbl $where_q LIMIT 1";
33     $this->_db->setQuery($query);
34     $cnt = $this->_db->loadResult();
35     if(!$this->_db->query()) {
36         echo $this->_db->stderr();
37         return false;
38     }
39     if($cnt == 0)
40         return true;
41         
42     return false;
43     }
44
45     // Проверяет, заблокировано ли имя
46     function blocked($name) {
47     if(empty($name)) return true;
48     $name = str_replace("'", "''", $name);
49     $where_q = "WHERE $this->_tbl_key = UPPER('$name') AND correct=2";
50     $query = "SELECT COUNT(*) FROM $this->_tbl $where_q LIMIT 1";
51     $this->_db->setQuery($query);
52     $cnt = $this->_db->loadResult();
53     if(!$this->_db->query()) {
54         echo $this->_db->stderr();
55         return true;
56     }
57     if($cnt == 0)
58         return false;
59     
60     return true;
61     }
62     
63     // Возвращает список слов, похожих на $name       
64     function suggest($name) {
65     if (empty($name)) return array();
66     $name = str_replace("'", "''", $name);
67     $where_q = "WHERE $this->_tbl_key LIKE UPPER('$name%') AND correct<>2"; // show all except blocked
68     $order_q = "ORDER BY $this->_tbl_key";
69     
70     $query = "SELECT * FROM $this->_tbl $where_q $order_q LIMIT 10";
71     $this->_db->setQuery($query);
72     $rows = $this->_db->loadRowList();
73     $res = array();
74     foreach($rows as $row) {
75         array_push($res, mb_convert_case($row["name"], MB_CASE_TITLE, 'UTF-8'));
76     }
77     if (!$this->_db->query()) {
78         echo $this->_db->stderr();
79         return array();
80     }
81     return $res;       
82     }
83     
84 // --------------------------------------------------------------------------
85 // Admin functions:
86
87     function listUnchecked() {
88         $where_q = "WHERE correct=0"; // show all except blocked
89         $order_q = "ORDER BY $this->_tbl_key";
90         
91         $query = "SELECT * FROM $this->_tbl $where_q $order_q";
92         $this->_db->setQuery($query);
93         $rows = $this->_db->loadRowList();
94         $res = array();
95         foreach($rows as $row) {
96             array_push($res,
97             array("name" =>  mb_convert_case($row["name"], MB_CASE_TITLE, 'UTF-8'),
98                   "correct" => $row["correct"]));
99         }
100         if (!$this->_db->query()) {
101             echo $this->_db->stderr();
102             return array();
103         }
104         return $res;       
105     }
106
107     function listAll() {
108         $order_q = "ORDER BY $this->_tbl_key";
109         
110         $query = "SELECT * FROM $this->_tbl $order_q";
111         $this->_db->setQuery($query);
112         $rows = $this->_db->loadRowList();
113         $res = array();
114         foreach($rows as $row) {
115             array_push($res,
116             array("name" =>  mb_convert_case($row["name"], MB_CASE_TITLE, 'UTF-8'),
117                   "correct" => $row["correct"]));
118         }
119         if (!$this->_db->query()) {
120             echo $this->_db->stderr();
121             return array();
122         }
123         return $res;       
124     }
125     
126     function accept($names) {
127     if (empty($names)) return;
128     foreach($names as $n) {
129         $n = str_replace("'", "''", $n);
130         $req = "UPDATE $this->_tbl SET correct=1 WHERE $this->_tbl_key=UPPER('$n') LIMIT 1;";
131             $this->_db->setQuery($req);
132         if (!$this->_db->query()) {
133         echo $this->_db->stderr();
134         }
135     }
136     }
137
138     function block($names) {
139     if (empty($names)) return;
140     foreach($names as $n) {
141         $n = str_replace("'", "''", $n);
142         $req = "UPDATE $this->_tbl SET correct=2 WHERE $this->_tbl_key=UPPER('$n') LIMIT 1;";
143             $this->_db->setQuery($req);
144         if (!$this->_db->query()) {
145             echo $this->_db->stderr();
146         }
147     }
148     }
149
150     function uncheck($names) {
151         if (empty($names)) return;
152         foreach($names as $n) {
153             $n = str_replace("'", "''", $n);
154             $req = "UPDATE $this->_tbl SET correct=null WHERE $this->_tbl_key = UPPER('$n') LIMIT 1;";
155                 $this->_db->setQuery($req);
156             if (!$this->_db->query()) {
157               echo $this->_db->stderr();
158             }
159         }
160     }
161
162     function remove($names) {
163     if (empty($names)) return;
164     foreach($names as $n) {
165             $n = str_replace("'", "''", $n);
166         $req = "DELETE FROM $this->_tbl WHERE $this->_tbl_key=UPPER('$n') LIMIT 1;";
167             $this->_db->setQuery($req);
168         if (!$this->_db->query()) {
169         echo $this->_db->stderr();
170         }
171     }
172     }
173 }
174 ?>
175
Note: See TracBrowser for help on using the browser.