forked from mazedigital/author_sort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.driver.php
152 lines (124 loc) · 4.84 KB
/
extension.driver.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
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
<?php
Class extension_author_sort extends Extension {
public function getSubscribedDelegates() {
return array(
array(
'page' => '/publish/',
'delegate' => 'getSortingField',
'callback' => 'getSortingField'
),
array('page' => '/publish/',
'delegate' => 'setSortingField',
'callback' => 'setSortingField'
),
array('page' => '/publish/',
'delegate' => 'getSortingOrder',
'callback' => 'getSortingOrder'
),
array('page' => '/publish/',
'delegate' => 'setSortingOrder',
'callback' => 'setSortingOrder'
),
);
}
public function install() {
Symphony::Database()->query("
CREATE TABLE IF NOT EXISTS `tbl_author_sort` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT(11) UNSIGNED NOT NULL,
`direction` ENUM('asc', 'desc') DEFAULT 'asc',
`field_id` INT(11) UNSIGNED NOT NULL,
`section_handle` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
)
");
return true;
}
public function getUser(){
try {
if (Symphony::Engine() instanceof Frontend){
// use config sorting if frontend
return false;
} else {
return Symphony::Author()->get('id');
}
} catch (Exception $e) {
return false;
}
}
public function checkUserSection($section){
//check if user has a field set in the specific section
$query = "SELECT user_id
FROM tbl_author_sort
WHERE user_id = '{$this->getUser()}'
AND section_handle = '{$section}'
LIMIT 1
";
if(Symphony::Database()->fetchVar('user_id',0,$query)){
return true;
}
return false;
}
public function getSortingField($context){
$user = $this->getUser();
if (!$user){
return;
}
$query = "SELECT field_id
FROM tbl_author_sort
WHERE user_id = '{$user}' AND section_handle = '{$context['section-handle']}'
LIMIT 1
";
$context['field'] = Symphony::Database()->fetchVar('field_id',0,$query);
}
public function setSortingField($context){
if($this->checkUserSection($context['section-handle'])){
//user has a field set, update the field id.
$update = [];
$update['field_id'] = $context['sort'];
$where = [];
$where['user_id'] = $this->getUser();
$where['section_handle'] = $context['section-handle'];
$context['updated'] = Symphony::Database()->update($update,'tbl_author_sort','user_id = '.$this->getUser().' AND section_handle = "'.$context["section-handle"].'"');
}
else{
//user does not have a field set, set a new one.
$insert = [];
$insert['user_id'] = $this->getUser();
$insert['field_id'] = $context['sort'];
$insert['section_handle'] = $context['section-handle'];
$context['updated'] = Symphony::Database()->insert($insert,'tbl_author_sort','true');
}
}
public function getSortingOrder($context){
$user = $this->getUser();
if (!$user){
return;
}
$query = "SELECT direction
FROM tbl_author_sort
WHERE user_id = '{$user}' AND section_handle = '{$context['section-handle']}'
LIMIT 1
";
$context['order'] = Symphony::Database()->fetchVar('direction',0,$query);
}
public function setSortingOrder($context){
if($this->checkUserSection($context['section-handle'])){
//user has a field set, update the field id.
$update = [];
$update['direction'] = $context['order'];
$where = [];
$where['user_id'] = $this->getUser();
$where['section_handle'] = $context['section-handle'];
$context['updated'] = Symphony::Database()->update($update,'tbl_author_sort','user_id = '.$this->getUser().' AND section_handle = "'.$context["section-handle"].'"');
}
else{
//user does not have a field set, set a new one.
$insert = [];
$insert['user_id'] = $this->getUser();
$insert['direction'] = $context['order'];
$insert['section_handle'] = $context['section-handle'];
$context['updated'] = Symphony::Database()->insert($insert,'tbl_author_sort','true');
}
}
}