1 | <?php |
---|
2 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
3 | |
---|
4 | include_once(B2F_PATH.'functions.inc.php'); |
---|
5 | |
---|
6 | /* |
---|
7 | * Add verso link on picture page |
---|
8 | */ |
---|
9 | function Back2Front_picture_content($content, $image) |
---|
10 | { |
---|
11 | global $template, $user, $conf; |
---|
12 | |
---|
13 | /* search for a verso picture */ |
---|
14 | $query = " |
---|
15 | SELECT |
---|
16 | i.id, |
---|
17 | i.path, |
---|
18 | i.has_high, |
---|
19 | i.width, |
---|
20 | i.height |
---|
21 | FROM ".IMAGES_TABLE." as i |
---|
22 | INNER JOIN ".B2F_TABLE." as v |
---|
23 | ON i.id = v.verso_id |
---|
24 | WHERE |
---|
25 | v.image_id = ".$image['id']." |
---|
26 | ;"; |
---|
27 | $result = pwg_query($query); |
---|
28 | |
---|
29 | if (pwg_db_num_rows($result)) |
---|
30 | { |
---|
31 | $verso = pwg_db_fetch_assoc($result); |
---|
32 | $conf['back2front'] = explode(',',$conf['back2front']); |
---|
33 | |
---|
34 | /* websize picture */ |
---|
35 | $template->assign('VERSO_URL', $verso['path']); |
---|
36 | |
---|
37 | /* admin link */ |
---|
38 | if (is_admin()) |
---|
39 | { |
---|
40 | $template->assign('VERSO_U_ADMIN', get_root_url().'admin.php?page=picture_modify&image_id='.$verso['id']); |
---|
41 | $template->set_filename('B2F_admin_button', dirname(__FILE__).'/template/admin_button.tpl'); |
---|
42 | $template->concat('PLUGIN_PICTURE_ACTIONS', $template->parse('B2F_admin_button', true)); |
---|
43 | } |
---|
44 | |
---|
45 | /* high picture */ |
---|
46 | if ($verso['has_high']) |
---|
47 | { |
---|
48 | $template->assign('VERSO_HD', get_high_url($verso)); |
---|
49 | } |
---|
50 | |
---|
51 | /* link name */ |
---|
52 | $conf['back2front'][4] = unserialize($conf['back2front'][4]); |
---|
53 | if (!empty($conf['back2front'][4][$user['language']])) |
---|
54 | { |
---|
55 | if (strpos($conf['back2front'][4][$user['language']], '|') !== false) |
---|
56 | { |
---|
57 | $conf['back2front'][4] = explode('|', $conf['back2front'][4][$user['language']]); |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | $conf['back2front'][4] = array($conf['back2front'][4][$user['language']], $conf['back2front'][4][$user['language']]); |
---|
62 | } |
---|
63 | } |
---|
64 | else if (!empty($conf['back2front'][4]['default'])) |
---|
65 | { |
---|
66 | if (strpos($conf['back2front'][4]['default'], '|') != false) |
---|
67 | { |
---|
68 | $conf['back2front'][4] = explode('|', $conf['back2front'][4]['default']); |
---|
69 | } |
---|
70 | else |
---|
71 | { |
---|
72 | $conf['back2front'][4] = array($conf['back2front'][4]['default'], $conf['back2front'][4]['default']); |
---|
73 | } |
---|
74 | } |
---|
75 | else |
---|
76 | { |
---|
77 | $conf['back2front'][4] = array(l10n('See back'), l10n('See front')); |
---|
78 | } |
---|
79 | |
---|
80 | /* template & output */ |
---|
81 | $template->set_filenames(array('B2F_picture_content' => dirname(__FILE__).'/template/picture_content.tpl') ); |
---|
82 | $template->assign(array( |
---|
83 | 'B2F_PATH' => B2F_PATH, |
---|
84 | 'b2f_switch_mode' => $conf['back2front'][1], |
---|
85 | 'b2f_transition' => $conf['back2front'][2], |
---|
86 | 'b2f_position' => $conf['back2front'][3], |
---|
87 | 'b2f_see_back' => $conf['back2front'][4][0], |
---|
88 | 'b2f_see_front' => $conf['back2front'][4][1], |
---|
89 | )); |
---|
90 | |
---|
91 | switch ($conf['back2front'][3]) |
---|
92 | { |
---|
93 | case 'toolbar': |
---|
94 | $template->set_prefilter('picture', 'Back2Front_toolbar_prefilter'); |
---|
95 | break; |
---|
96 | case 'top': |
---|
97 | return $template->parse('B2F_picture_content', true)."\n".$content; |
---|
98 | break; |
---|
99 | case 'bottom': |
---|
100 | return $content."\n".$template->parse('B2F_picture_content', true); |
---|
101 | break; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | return $content; |
---|
106 | } |
---|
107 | |
---|
108 | function Back2Front_toolbar_prefilter($content, &$smarty) |
---|
109 | { |
---|
110 | $search = '{/if}{/strip}{*caddie management END*}'; |
---|
111 | $replacement = $search."\n".file_get_contents(B2F_PATH.'template/picture_content.tpl'); |
---|
112 | return str_replace($search, $replacement, $content); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | /* |
---|
117 | * Add field on picture modify page |
---|
118 | */ |
---|
119 | function Back2Front_picture_modify() |
---|
120 | { |
---|
121 | global $page, $template, $conf; |
---|
122 | |
---|
123 | if ($page['page'] != 'picture_modify') return; |
---|
124 | $conf['back2front'] = explode(',',$conf['back2front']); |
---|
125 | |
---|
126 | /* SAVE VALUES */ |
---|
127 | if (isset($_POST['b2f_submit'])) |
---|
128 | { |
---|
129 | /* catch all verso and recto ids and original categories */ |
---|
130 | $query = "SELECT image_id, verso_id, categories |
---|
131 | FROM ".B2F_TABLE.";"; |
---|
132 | $rectos = array_from_query($query, 'image_id'); |
---|
133 | $versos = array_from_query($query, 'verso_id'); |
---|
134 | $cats = array_from_query($query, 'categories'); |
---|
135 | |
---|
136 | if (count($rectos) != 0) |
---|
137 | { |
---|
138 | $all_recto_verso = array_combine($rectos, $versos); |
---|
139 | $verso_cats = array_combine($versos, $cats); |
---|
140 | } |
---|
141 | else |
---|
142 | { |
---|
143 | $all_recto_verso = array(0=>0); |
---|
144 | $verso_cats = array(0=>NULL); |
---|
145 | } |
---|
146 | unset($rectos, $versos, $cats); |
---|
147 | |
---|
148 | /* picture is verso */ |
---|
149 | if (isset($_POST['b2f_is_verso'])) |
---|
150 | { |
---|
151 | /* verso don't exists */ |
---|
152 | if (!picture_exists($_POST['b2f_front_id'])) |
---|
153 | { |
---|
154 | array_push( |
---|
155 | $page['errors'], |
---|
156 | sprintf( |
---|
157 | l10n('Unknown id %d for frontside picture'), |
---|
158 | $_POST['b2f_front_id'] |
---|
159 | ) |
---|
160 | ); |
---|
161 | } |
---|
162 | /* verso same as recto */ |
---|
163 | else if ($_POST['b2f_front_id'] == $_GET['image_id']) |
---|
164 | { |
---|
165 | array_push($page['errors'], l10n('Backside and frontside can\'t be the same picture')); |
---|
166 | } |
---|
167 | /* recto has already a verso */ |
---|
168 | else if (in_array($_POST['b2f_front_id'], array_keys($all_recto_verso)) AND $all_recto_verso[$_POST['b2f_front_id']] != $_GET['image_id']) |
---|
169 | { |
---|
170 | $recto_current_verso['id'] = $all_recto_verso[$_POST['b2f_front_id']]; |
---|
171 | $recto_current_verso['link'] = get_root_url().'admin.php?page=picture_modify&image_id='.$recto_current_verso['id']; |
---|
172 | array_push( |
---|
173 | $page['errors'], |
---|
174 | sprintf( |
---|
175 | l10n('The picture n°%d has already a backside : %s'), |
---|
176 | $_POST['b2f_front_id'], |
---|
177 | '<a href="'.$recto_current_verso['link'].'">'.$recto_current_verso['id'].'</a>' |
---|
178 | ) |
---|
179 | ); |
---|
180 | } |
---|
181 | /* recto is already a verso */ |
---|
182 | else if (in_array($_POST['b2f_front_id'], array_values($all_recto_verso))) |
---|
183 | { |
---|
184 | $recto_is_verso['id'] = $_POST['b2f_front_id']; |
---|
185 | $recto_is_verso['link'] = get_root_url().'admin.php?page=picture_modify&image_id='.$recto_is_verso['id']; |
---|
186 | array_push( |
---|
187 | $page['errors'], |
---|
188 | sprintf( |
---|
189 | l10n('The picture n°%s is already a backside'), |
---|
190 | '<a href="'.$recto_is_verso['link'].'">'.$recto_is_verso['id'].'</a>' |
---|
191 | ) |
---|
192 | ); |
---|
193 | } |
---|
194 | /* everything is fine */ |
---|
195 | else |
---|
196 | { |
---|
197 | // move the verso - if first save |
---|
198 | if (isset($_POST['b2f_move_verso']) AND (!array_key_exists($_GET['image_id'], $verso_cats) OR $verso_cats[$_GET['image_id']] == NULL)) |
---|
199 | { |
---|
200 | // get current categories |
---|
201 | $query = "SELECT category_id FROM ".IMAGE_CATEGORY_TABLE." WHERE image_id = ".$_GET['image_id'].";"; |
---|
202 | $verso_categories = array_from_query($query, 'category_id'); |
---|
203 | |
---|
204 | pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE." |
---|
205 | WHERE image_id = ".$_GET['image_id'].";"); |
---|
206 | pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."(image_id, category_id) |
---|
207 | VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].");"); |
---|
208 | |
---|
209 | // random representant for each categories |
---|
210 | set_random_representant($verso_categories); |
---|
211 | |
---|
212 | $verso_categories = isset($verso_cats[$_GET['image_id']]) ? $verso_cats[$_GET['image_id']] : implode(',',$verso_categories); |
---|
213 | $template->assign('B2F_MOVE_VERSO', 'checked="checked"'); |
---|
214 | } |
---|
215 | // restore the verso - if precedently moved |
---|
216 | else if (!isset($_POST['b2f_move_verso']) AND array_key_exists($_GET['image_id'], $verso_cats) AND $verso_cats[$_GET['image_id']] != NULL) |
---|
217 | { |
---|
218 | $item['verso_id'] = $_GET['image_id']; |
---|
219 | $item['categories'] = $verso_cats[$_GET['image_id']]; |
---|
220 | back2front_restaure_categories($item); |
---|
221 | |
---|
222 | $verso_categories = 'NULL'; |
---|
223 | $template->assign('B2F_MOVE_VERSO', ''); |
---|
224 | } |
---|
225 | // leave the verso |
---|
226 | else |
---|
227 | { |
---|
228 | $verso_categories = isset($verso_cats[$_GET['image_id']]) ? $verso_cats[$_GET['image_id']] : 'NULL'; |
---|
229 | $template->assign('B2F_MOVE_VERSO', isset($verso_cats[$_GET['image_id']]) ? 'checked="checked"' : ''); |
---|
230 | } |
---|
231 | |
---|
232 | // insert or update verso associations |
---|
233 | pwg_query("INSERT INTO ".B2F_TABLE." |
---|
234 | VALUES(".$_POST['b2f_front_id'].", ".$_GET['image_id'].", '".$verso_categories."') |
---|
235 | ON DUPLICATE KEY UPDATE image_id = ".$_POST['b2f_front_id'].", categories = ".$verso_categories.";"); |
---|
236 | |
---|
237 | $template->assign(array( |
---|
238 | 'B2F_IS_VERSO' => 'checked="checked"', |
---|
239 | 'B2F_FRONT_ID' => $_POST['b2f_front_id'], |
---|
240 | )); |
---|
241 | |
---|
242 | $verso['id'] = $_POST['b2f_front_id']; |
---|
243 | $verso['link'] = get_root_url().'admin.php?page=picture_modify&image_id='.$verso['id']; |
---|
244 | array_push($page['infos'], l10n_args(get_l10n_args('This picture is now the backside of the picture n°%s', '<a href="'.$verso['link'].'">'.$verso['id'].'</a>'))); |
---|
245 | } |
---|
246 | } |
---|
247 | /* picture isn't verso */ |
---|
248 | else |
---|
249 | { |
---|
250 | /* search if it was a verso */ |
---|
251 | $query = "SELECT categories |
---|
252 | FROM ".B2F_TABLE." |
---|
253 | WHERE verso_id = ".$_GET['image_id'].";"; |
---|
254 | $result = pwg_query($query); |
---|
255 | |
---|
256 | /* it must be restored to its original categories (see criteria on maintain.inc) */ |
---|
257 | if (pwg_db_num_rows($result)) |
---|
258 | { |
---|
259 | $item['verso_id'] = $_GET['image_id']; |
---|
260 | list($item['categories']) = pwg_db_fetch_row($result); |
---|
261 | back2front_restaure_categories($item); |
---|
262 | |
---|
263 | pwg_query("DELETE FROM ".B2F_TABLE." |
---|
264 | WHERE verso_id = ".$_GET['image_id'].";"); |
---|
265 | |
---|
266 | array_push($page['infos'], l10n('This picture is no longer a backside')); |
---|
267 | } |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | /* GET SAVED VALUES */ |
---|
272 | if ($template->get_template_vars('B2F_IS_VERSO') == null) |
---|
273 | { |
---|
274 | $template->assign('B2F_MOVE_VERSO', 'checked="checked"'); |
---|
275 | |
---|
276 | /* is the picture a verso ? */ |
---|
277 | $query = " |
---|
278 | SELECT image_id, categories |
---|
279 | FROM ".B2F_TABLE." |
---|
280 | WHERE verso_id = ".$_GET['image_id']." |
---|
281 | ;"; |
---|
282 | $result = pwg_query($query); |
---|
283 | |
---|
284 | if (pwg_db_num_rows($result)) |
---|
285 | { |
---|
286 | list($recto_id, $cats) = pwg_db_fetch_row($result); |
---|
287 | $template->assign(array( |
---|
288 | 'B2F_IS_VERSO' => 'checked="checked"', |
---|
289 | 'B2F_FRONT_ID' => $recto_id, |
---|
290 | 'B2F_MOVE_VERSO' => $cats != NULL ? 'checked="checked"' : '', |
---|
291 | )); |
---|
292 | } |
---|
293 | /* is the picture a front ? */ |
---|
294 | else |
---|
295 | { |
---|
296 | $query = "SELECT verso_id |
---|
297 | FROM ".B2F_TABLE." |
---|
298 | WHERE image_id = ".$_GET['image_id'].";"; |
---|
299 | $result = pwg_query($query); |
---|
300 | |
---|
301 | if (pwg_db_num_rows($result)) |
---|
302 | { |
---|
303 | $item = pwg_db_fetch_assoc($result); |
---|
304 | |
---|
305 | $template->assign(array( |
---|
306 | 'B2F_VERSO_ID' => $item['verso_id'], |
---|
307 | 'B2F_VERSO_URL' => get_root_url().'admin.php?page=picture_modify&image_id='.$item['verso_id'], |
---|
308 | )); |
---|
309 | } |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | $template->set_prefilter('picture_modify', 'Back2front_picture_modify_prefilter'); |
---|
314 | } |
---|
315 | |
---|
316 | |
---|
317 | function Back2front_picture_modify_prefilter($content, &$smarty) |
---|
318 | { |
---|
319 | $search = '<form id="associations" method="post" action="{$F_ACTION}#associations">'; |
---|
320 | $replacement = file_get_contents(B2F_PATH.'template/picture_modify.tpl')."\n".$search; |
---|
321 | return str_replace($search, $replacement, $content); |
---|
322 | } |
---|
323 | |
---|
324 | ?> |
---|