-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLinkManager.class.php
117 lines (109 loc) · 3.2 KB
/
LinkManager.class.php
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
<?php
require_once('util4p/CRObject.class.php');
require_once('util4p/MysqlPDO.class.php');
require_once('util4p/SQLBuilder.class.php');
class LinkManager
{
/*
* do add link
*/
public static function add(CRObject $link)
{
$token = $link->get('token', '');
$url = $link->get('url', '');
$remark = $link->get('remark');
$valid_from = $link->getInt('valid_from');
$valid_to = $link->getInt('valid_to');
$owner = $link->get('owner');
$key_values = array(
'token' => '?', 'url' => '?', 'remark' => '?',
'valid_from' => '?', 'valid_to' => '?', 'time' => '?', 'owner' => '?'
);
$builder = new SQLBuilder();
$builder->insert('ls_link', $key_values);
$sql = $builder->build();
$params = array($token, $url, $remark, $valid_from, $valid_to, time(), $owner);
return (new MysqlPDO())->execute($sql, $params);
}
/* */
public static function gets(CRObject $rule)
{
$owner = $rule->get('owner', '');
$offset = $rule->getInt('offset', 0);
$limit = $rule->getInt('limit', -1);
$selected_rows = array();
$where = array();
$params = array();
$opts = array();
if ($owner) {
$where['owner'] = '?';
$params[] = $owner;
$where['status'] = '3';
$opts['status'] = '!=';
}
$order_by = array('time' => 'DESC');
$builder = new SQLBuilder();
$builder->select('ls_link', $selected_rows);
$builder->where($where, $opts);
$builder->order($order_by);
$builder->limit($offset, $limit);
$sql = $builder->build();
$links = (new MysqlPDO())->executeQuery($sql, $params);
return $links;
}
/* */
public static function count(CRObject $rule)
{
$owner = $rule->get('owner', '');
$selected_rows = array('COUNT(1) as cnt');
$where = array();
$params = array();
$opts = array();
if ($owner) {
$where['owner'] = '?';
$params[] = $owner;
$where['status'] = '3';
$opts['status'] = '!=';
}
$builder = new SQLBuilder();
$builder->select('ls_link', $selected_rows);
$builder->where($where, $opts);
$sql = $builder->build();
$res = (new MysqlPDO())->executeQuery($sql, $params);
return $res === null ? 0 : intval($res[0]['cnt']);
}
/* get link by token */
public static function get(CRObject $rule)
{
$token = $rule->get('token', '');
$selected_rows = array();
$where = array('token' => '?');
$params = array($token);
$builder = new SQLBuilder();
$builder->select('ls_link', $selected_rows);
$builder->where($where);
$sql = $builder->build();
$links = (new MysqlPDO())->executeQuery($sql, $params);
return $links !== null && count($links) === 1 ? $links[0] : null;
}
/* */
public static function update(CRObject $link)
{
$token = $link->get('token', '');
$url = $link->get('url', '');
$remark = $link->get('remark');
$valid_from = $link->getInt('valid_from');
$valid_to = $link->getInt('valid_to');
$status = $link->getInt('status', 0);
$key_values = array(
'url' => '?', 'remark' => '?', 'valid_from' => '?', 'valid_to' => '?', 'status' => '?'
);
$where = array('token' => '?');
$builder = new SQLBuilder();
$builder->update('ls_link', $key_values);
$builder->where($where);
$sql = $builder->build();
$params = array($url, $remark, $valid_from, $valid_to, $status, $token);
return (new MysqlPDO())->execute($sql, $params);
}
}