This repository has been archived by the owner on Feb 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordpress-slideshow-slide.class.php
182 lines (124 loc) · 4.39 KB
/
wordpress-slideshow-slide.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
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
174
175
176
177
178
179
180
181
182
<?php
class WordpressSlideshow_Slide{
public $id;
public $name; /* The name of the slide */
public $url; /* The url the slide links to */
public $image_url; /* The image to display */
public $text; /* The text to display */
public $no; /* The N° of the slide */
public $slideshow_id;
protected $_slideshow;
protected function get_slideshow(){
return $_slideshow;
}
protected function set_slideshow($value){
$this->_slideshow = $value;
if(empty($value)){
$this->slideshow_id = null;
}
else{
$this->slideshow_id = $this->_slideshow->id;
}
}
public function save(){
if($this->id == null){
$this->insert();
}
else{
$this->update();
}
}
private function insert(){
global $wpdb;
$query = $wpdb->prepare('INSERT INTO '.WORDPRESS_SLIDESHOW_SLIDE_TABLE. '(slide_name, slide_url, slide_image_url, slide_text, slideshow_id) VALUES(%s,%s,%s,%s,%s);',
$this->name, $this->url, $this->image_url, $this->text, $this->slideshow_id);
$result = $wpdb->query($query);
if(!$result){
throw new Exception(__('An error occured during slide creation','wordpress-slideshow'));
}
$this->id = $wpdb->insert_id;
}
private function update(){
global $wpdb;
$query = $wpdb->prepare('UPDATE '.WORDPRESS_SLIDESHOW_SLIDE_TABLE ." SET slide_name = %s, slide_text = %s, slide_url = %s, slide_image_url = %s, slide_no = %d WHERE id=%d",
$this->name, $this->text,$this->url,$this->image_url,$this->no, $this->id);
$result = $wpdb->query($query);
if(!$result){
echo $wpdb->last_error;
throw new Exception(__('An error occured during slide update','wordpress-slideshow').$wpdb->last_error);
}
}
public function delete(){
global $wpdb;
echo 'Deleting slide';
if(!empty($this->id)){
$query = $wpdb->prepare('DELETE FROM '.WORDPRESS_SLIDESHOW_SLIDE_TABLE.' WHERE id=%s',$this->id);
$result = $wpdb->query($query);
if(!$result){
throw new Exception(__('An error occured when removing the slide','wordpress-slideshow'));
}
$this->id = null;
}
}
public static function find($id){
if(empty($id)){return;}
global $wpdb;
$query = $wpdb->prepare('SELECT * FROM '.WORDPRESS_SLIDESHOW_SLIDE_TABLE.' WHERE id=%s;',$id);
$results = $wpdb->get_results($query);
if(!is_array($results)){
throw new Exception(__('An error occured looking up for the slides','wordpress-slideshow'));
}
if(empty($results)){
return null;
}
$slide = WordpressSlideshow_Slide::fromQueryResult($results[0]);
$slide->slideshow = WordpressSlideshow::find($slide->slideshow_id);
return $slide;
}
public static function findBySlideshow($slideshow){
if(empty($slideshow)){return;}
global $wpdb;
$query = $wpdb->prepare('SELECT * FROM '.WORDPRESS_SLIDESHOW_SLIDE_TABLE.' WHERE slideshow_id=%s ORDER BY slide_no ASC;',$slideshow->id);
$results = $wpdb->get_results($query);
if(!is_array($results)){
throw new Exception(__('An error occured looking up for the slides','wordpress-slideshow'));
}
$slides = array();
foreach($results as $result){
$slide = WordpressSlideshow_Slide::fromQueryResult($result);
$slide->slideshow = $slideshow;
array_push($slides,$slide);
}
return $slides;
}
protected static function fromQueryResult($result){
$slide = new WordpressSlideshow_Slide();
$slide->id = $result->id;
$slide->name = stripslashes($result->slide_name);
$slide->url = stripslashes($result->slide_url);
$slide->image_url = stripslashes($result->slide_image_url);
$slide->text = stripslashes($result->slide_text);
$slide->no = $result->slide_no;
$slide->slideshow_id = $result->slideshow_id;
return $slide;
}
public static function updateOrder($slide_positions){
global $wpdb;
foreach($slide_positions as $slide_position){
var_dump($slide_position);
$query = $wpdb->prepare('UPDATE '.WORDPRESS_SLIDESHOW_SLIDE_TABLE.' SET slide_no=%d WHERE id=%d',$slide_position['slide_no'],$slide_position['slide_id']);
$wpdb->query($query);
}
}
public function __get($name){
if($name == 'slideshow'){
return $this->get_slideshow();
}
}
public function __set($name, $value){
if($name == 'slideshow'){
$this->set_slideshow($value);
}
}
}
?>