banners.class.php
6.86 KB
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
<?php
class Banners{
private $db;
private $lang;
function __construct($lang){
$this->db = sdb::getInstance();
$this->lang = $lang;
}
function isAdminUser($user = array()){
if((isset($user['login']) && $user['login']==ADMIN_BN_USER) && (isset($user['psw']) && $user['psw']==ADMIN_BN_PSW)){
$_SESSION['user_bn']['login'] = $user['login'];
$_SESSION['user_bn']['psw'] = $user['psw'];
}
if((isset($_SESSION['user_bn']['login']) && $_SESSION['user_bn']['login']==ADMIN_BN_USER) && (isset($_SESSION['user_bn']['psw']) && $_SESSION['user_bn']['psw']==ADMIN_BN_PSW))return true;
return false;
}
function trim(&$data){
foreach($data as $key=>$value){
if(!is_array($data[$key]))$data[$key] = trim($value);
}
}
function valid($data,$upload = null){
if(isset($data['place'])){
if( !preg_match("/^[0-9]+$/",$data['place']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Ìåñòî.";
}
if(isset($data['city_id'])){
if( !preg_match("/^[0-9]+$/",$data['city_id']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Ãîðîä.";
}
if(isset($data['url'])){
if( preg_match("/^(http:\/\/)(.*)?/i",$data['url']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ URL.";
}
if(isset($data['type'])){
if( !preg_match("/^(flash|image|code)$/",$data['type']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Òèï.";
}
if(isset($data['type'],$data['code']) && $data['type']=="code"){
if( !preg_match("/.+/is",$data['code']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Êîä.";
}
if(isset($data['type'],$upload['upload']['name']) && $upload['upload']['name']!=null && ($data['type']=="flash" || $data['type']=="image") ){
$type = substr(strrchr($upload['upload']['name'],"."),1);
if( !preg_match("/^(jpeg|jpg|gif|png|swf)$/i",$type) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Ôàéë, òîëüêî jpg,png,gif,swf.";
}
return ( count($this->error) ) ? true : false;
}
function SaveBanner($data,$upload = null) {
$table_name = "banners";
if ($data['update_id'] > 0) {
$DB_AUTOQUERY = DB_AUTOQUERY_UPDATE;
$id = $data['update_id'];
$where = "id=$id";
} else {
$DB_AUTOQUERY = DB_AUTOQUERY_INSERT;
$where = null;
}
if (isset($data['rubrics'])) {
$action = '|'.implode("|",$data['rubrics']).'|';
} else {
$action = "|begin|";
}
$fields_values = array( "place" => $data['place'],
"n" => $data['n'],
"city_id" => 1,
"action" => $action,
"type" => $data['type'],
"code" => $data['code'],
"url" => $data['url'],
"width" => $data['width'],
"height" => $data['height'],
);
if (isset($data['sub_place']) && is_numeric($data['sub_place'])) {
$fields_values['sub_place'] = $data['sub_place'];
}
if ($upload['upload']['tmp_name']) {
if ($data['update_id'] > 0) {
$this->deleteFile($data['update_id']);
}
$uploadfile = "./pics/banners/";
$type = substr(strrchr($upload['upload']['name'], "."), 1);
$uploadname = mktime() . "." . $type;
if (!move_uploaded_file($_FILES['upload']['tmp_name'], $uploadfile . $uploadname)) {
print"Îøèáêà ïðè çàêà÷êå ôàéëà!";
}
$fields_values['file'] = $uploadname;
}
$this->db->autoExecute($table_name, $fields_values, $DB_AUTOQUERY,$where);
}
function deleteFile($banner_id){
$sql = "select file from banners where id=?";
$file = $this->db->getOne($sql,array($banner_id));
@unlink("./pics/banners/".$file);
}
function deleteBanner($id){
$this->deleteFile($id);
$sql = "delete from banners where id=?";
$this->db->query($sql,array($id));
}
function viewBanner($action,$cityID,$place,$n){
$sql = "select * from banners Where city_id=? and place=? and n=? Limit 1";
$banner = $this->db->getRow($sql,array($cityID,$place,$n),DB_FETCHMODE_ASSOC);
$banner['rubrics_checked'] = explode('|',substr($banner['action'],1,-1));
//print_r($banner);
return $banner;
}
function viewBannerList($action,$cityID,$place){
$sql = "select * from banners Where action like '%|$action|%' and city_id=? and place=?";
$res = $this->db->getAll($sql,array($cityID,$place),DB_FETCHMODE_ASSOC);
//print_r($res);
return $res;
}
function viewBannerBlock($action,$cityID,$place){
$limit = 1;
$count = $this->db->getOne("select count(*) from banners Where action like '%|$action|%' and city_id=? and place=?",array($cityID,$place));
$round = rand(0,($count-$limit));
$sql = "select * from banners Where action like '%|$action|%' and city_id=? and place=?";
if ($place == 10) {
$rubrics = $this->db->getAll($sql,array($cityID,$place),DB_FETCHMODE_ASSOC);
} else {
$sql .= " Limit $round, $limit ";
$rubrics = $this->db->getRow($sql,array($cityID,$place),DB_FETCHMODE_ASSOC);
}
//print_r($rubrics);
//$this->tpl->assign('banner_'.$place,$rubrics);
return $rubrics;
}
function viewBannerN($action,$cityID,$place){
$sql = "select * from banners where city_id=? and place=? order by n";
return $this->db->getAll($sql,array($cityID,$place),DB_FETCHMODE_ASSOC);
}
function addBannerN($action,$cityID,$place){
$max_n = $this->db->getOne("select n from banners where city_id=? and place=? order by n desc",array($cityID,$place),DB_FETCHMODE_ASSOC);
$fields_values = array("place"=>$place,"city_id"=>$cityID,"n"=>($max_n+1));
$this->db->autoExecute("banners", $fields_values, DB_AUTOQUERY_INSERT);
return $fields_values["n"];
}
function error(){
$this->tpl->assign('error',$this->error);
}
function displayAdmin(){
$this->tpl->display("banners_admin.tpl");
}
function displayBanners(){
$this->tpl->assign('tpl',"banners.tpl");
}
}
?>