products.class.php
5.08 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
<?php
Class Products{
private $db;
private $lang;
private $path_pic = array('pic'=>"./pics/products/",
'pic_big'=>"./pics/products/big/");
function __construct($lang){
$this->db = sdb::getInstance();
$this->lang = $lang;
}
public function valid($data){
$error = array();
if(!preg_match("/.{1,50}/i",$data['name']))$error[] = "error name";
return $error;
}
public function save($data,$upload){
$table_name = "catalogs_products";
$DB_AUTOQUERY = ($data['update_id']>0) ? DB_AUTOQUERY_UPDATE : DB_AUTOQUERY_INSERT;
$fields_values = array(
'catalog_id' => $data['catalog_id'],
'brend_id' => $data['brend_id'],
'status' => $data['status'],
'name' => $data['name'],
'code' => $data['code'],
'translit' => ($data['translit']) ? $data['translit'] : translitIt($data['name']),
'text' => $data['text'],
'features' => $data['features'],
'video_code' => $data['video_code'],
'cost' => $data['cost'],
'recommended' => $data['recommended'],
'meta_title' => $data['meta_title'],
'meta_description' => $data['meta_description'],
'meta_keywords' => $data['meta_keywords'],
'meta_about' => $data['meta_about']
);
if(isset($data['hit']))$fields_values['hit'] = 1;else $fields_values['hit'] = 0;
if(isset($data['top']))$fields_values['top'] = 1;else $fields_values['top'] = 0;
if(isset($data['delete_pic']) && $data['delete_pic']==1){
if($data['update_id']>0)$this->deletePic($data['update_id']);
$fields_values['pic'] = null; $fields_values['pic_big'] = null;
}
if($upload['pic']['tmp_name']){
if($data['update_id']>0)$this->deletePic($data['update_id']);
$fields_values['pic'] = upload_ImageResize($upload['pic'],array('crop'=>true,'width'=>"147",'height'=>"147",'upload_path'=>$this->path_pic['pic']));
$fields_values['pic_big'] = upload_ImageResize($upload['pic'],array('width'=>"700",'height'=>"700",'upload_path'=>$this->path_pic['pic_big']));
}
$res = $this->db->autoExecute($table_name,$fields_values,$DB_AUTOQUERY,"id={$data['update_id']}");
if (PEAR::isError($res)) die($res->getMessage());
$id = ($data['update_id']>0) ? $data['update_id'] : mysql_insert_id();
$filters = new Filters($this->lang);
$filters->saveKeys($id,$data['filter'],$data['filter_sort']);
}
private function deletePic($id){
$sql = "select pic,pic_big from catalogs_products where id=?";
$row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
@unlink($this->path_pic['pic'] . $row['pic']);
@unlink($this->path_pic['pic_big'] . $row['pic_big']);
}
public function getView($catalog_id,$params = array()){
$search = array();
$sort = array();
if($catalog_id>0)$search[] = sprintf("catalog_id='%d'", $catalog_id);
$sql = "select * from catalogs_products where 1=1 ";
if(count($search))$sql .= "AND " . implode(" AND ",$search)." ";
$sql .= "order by ";
$sort[] = (isset($params['sort'])) ? $params['sort'] : "id";
$sort[] = (isset($params['order'])) ? $params['order'] : " desc";
$sql .= implode(" ",$sort);
$pagerOptions = Array(
'mode' => 'Sliding',
'delta' => 6,
'perPage' => 10,
'spacesBeforeSeparator' => 1,
'spacesAfterSeparator' => 1
);
return Pager_Wrapper_DB($this->db, $sql, $pagerOptions, false, DB_FETCHMODE_ASSOC, array());
}
public function getProduct($id){
return $this->db->getRow("select * from catalogs_products where id=?",array($id),DB_FETCHMODE_ASSOC);
}
public function deleteProduct($id){
$this->deletePic($id);
$this->db->query("delete from catalogs_products where id=?",array($id));
$filters = new Filters($this->lang);
$filters->deleteKeysProduct($id);
}
public function deleteProducts($catalog_id){
$res = $this->db->getAll("select * from catalogs_products where catalog_id=?",array($catalog_id),DB_FETCHMODE_ASSOC);
foreach($res as $row){
$this->deleteProduct($row['id']);
}
}
public function export(){
$tmpfname = "tmp/products.csv";
$handle = fopen($tmpfname, "w");
$res = $this->db->getAll("select code,name,text,'',cost from catalogs_products",array(),DB_FETCHMODE_ASSOC);
foreach($res as $row){
fputcsv($handle,$row,';');
}
fclose($handle);
$params = array(
'file' => $tmpfname,
'contenttype' => 'application/xls',
'contentdisposition' => array(HTTP_DOWNLOAD_ATTACHMENT, 'products.csv'),
'cache' => false
);
$error = HTTP_Download::staticSend($params, false); //отдаём файл
if ($error === true) {
//если файл загружен успешно, обновляем дату последней закачки
}
unlink($tmpfname);
}
public function import($file){
if (($handle = fopen($file['tmp_name'], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
//print $data[4]."<br />";
$code = trim($data[0]);
if(strlen($code)>0){
$this->db->autoExecute("catalogs_products", array('cost'=>$data[4]), DB_AUTOQUERY_UPDATE,"code='$code'");
}
}
fclose($handle);
}
}
}
?>