[10819] | 1 | <?php |
---|
| 2 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
| 3 | |
---|
[10852] | 4 | include_once(B2F_PATH.'functions.inc.php'); |
---|
| 5 | |
---|
[10819] | 6 | /* |
---|
| 7 | * Add verso link on picture page |
---|
| 8 | */ |
---|
| 9 | function Back2Front_picture_content($content, $image) |
---|
| 10 | { |
---|
[10852] | 11 | global $template, $user, $conf; |
---|
[10819] | 12 | |
---|
| 13 | /* search for a verso picture */ |
---|
| 14 | $query = " |
---|
| 15 | SELECT |
---|
| 16 | i.id, |
---|
| 17 | i.path, |
---|
[10852] | 18 | i.has_high, |
---|
| 19 | i.width, |
---|
| 20 | i.height |
---|
[10819] | 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); |
---|
[10852] | 32 | $conf['back2front'] = explode(',',$conf['back2front']); |
---|
| 33 | |
---|
[10819] | 34 | /* websize picture */ |
---|
[11219] | 35 | $template->assign('VERSO_URL', $verso['path']); |
---|
[10819] | 36 | |
---|
| 37 | /* admin link */ |
---|
| 38 | if (is_admin()) |
---|
| 39 | { |
---|
[10828] | 40 | $template->assign('VERSO_U_ADMIN', get_root_url().'admin.php?page=picture_modify&image_id='.$verso['id']); |
---|
[10819] | 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 | } |
---|
[11219] | 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 | } |
---|
[10819] | 79 | |
---|
| 80 | /* template & output */ |
---|
[11219] | 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 | )); |
---|
[10819] | 90 | |
---|
[11219] | 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 | } |
---|
[10819] | 103 | } |
---|
[11219] | 104 | |
---|
| 105 | return $content; |
---|
[10819] | 106 | } |
---|
| 107 | |
---|
[11219] | 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 | } |
---|
[10819] | 114 | |
---|
[11219] | 115 | |
---|
[10819] | 116 | /* |
---|
| 117 | * Add field on picture modify page |
---|
| 118 | */ |
---|
[11219] | 119 | function Back2Front_picture_modify() |
---|
[10819] | 120 | { |
---|
[10821] | 121 | global $page, $template, $conf; |
---|
[10852] | 122 | |
---|
[11219] | 123 | if ($page['page'] != 'picture_modify') return; |
---|
[10821] | 124 | $conf['back2front'] = explode(',',$conf['back2front']); |
---|
[10819] | 125 | |
---|
[10823] | 126 | /* SAVE VALUES */ |
---|
| 127 | if (isset($_POST['b2f_submit'])) |
---|
| 128 | { |
---|
[10852] | 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 | |
---|
[10823] | 148 | /* picture is verso */ |
---|
| 149 | if (isset($_POST['b2f_is_verso'])) |
---|
[10852] | 150 | { |
---|
[10823] | 151 | /* verso don't exists */ |
---|
| 152 | if (!picture_exists($_POST['b2f_front_id'])) |
---|
[10819] | 153 | { |
---|
[11599] | 154 | array_push( |
---|
| 155 | $page['errors'], |
---|
| 156 | sprintf( |
---|
| 157 | l10n('Unknown id %d for frontside picture'), |
---|
| 158 | $_POST['b2f_front_id'] |
---|
| 159 | ) |
---|
| 160 | ); |
---|
[10823] | 161 | } |
---|
| 162 | /* verso same as recto */ |
---|
| 163 | else if ($_POST['b2f_front_id'] == $_GET['image_id']) |
---|
| 164 | { |
---|
[10852] | 165 | array_push($page['errors'], l10n('Backside and frontside can\'t be the same picture')); |
---|
[10823] | 166 | } |
---|
| 167 | /* recto has already a verso */ |
---|
[10852] | 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']) |
---|
[10823] | 169 | { |
---|
| 170 | $recto_current_verso['id'] = $all_recto_verso[$_POST['b2f_front_id']]; |
---|
[10828] | 171 | $recto_current_verso['link'] = get_root_url().'admin.php?page=picture_modify&image_id='.$recto_current_verso['id']; |
---|
[11599] | 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 | ); |
---|
[10823] | 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']; |
---|
[10828] | 185 | $recto_is_verso['link'] = get_root_url().'admin.php?page=picture_modify&image_id='.$recto_is_verso['id']; |
---|
[11599] | 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 | ); |
---|
[10823] | 193 | } |
---|
| 194 | /* everything is fine */ |
---|
| 195 | else |
---|
| 196 | { |
---|
[10852] | 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)) |
---|
[10819] | 199 | { |
---|
[10852] | 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 | |
---|
[10823] | 204 | pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE." |
---|
| 205 | WHERE image_id = ".$_GET['image_id'].";"); |
---|
[10981] | 206 | pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."(image_id, category_id) |
---|
| 207 | VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].");"); |
---|
[10821] | 208 | |
---|
[10823] | 209 | // random representant for each categories |
---|
| 210 | set_random_representant($verso_categories); |
---|
[10852] | 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"'); |
---|
[10819] | 214 | } |
---|
[10852] | 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.";"); |
---|
[10823] | 236 | |
---|
| 237 | $template->assign(array( |
---|
| 238 | 'B2F_IS_VERSO' => 'checked="checked"', |
---|
| 239 | 'B2F_FRONT_ID' => $_POST['b2f_front_id'], |
---|
| 240 | )); |
---|
| 241 | |
---|
[10852] | 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>'))); |
---|
[10823] | 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 | { |
---|
[10852] | 259 | $item['verso_id'] = $_GET['image_id']; |
---|
[10823] | 260 | list($item['categories']) = pwg_db_fetch_row($result); |
---|
[10852] | 261 | back2front_restaure_categories($item); |
---|
[10819] | 262 | |
---|
[10823] | 263 | pwg_query("DELETE FROM ".B2F_TABLE." |
---|
| 264 | WHERE verso_id = ".$_GET['image_id'].";"); |
---|
[10821] | 265 | |
---|
[10852] | 266 | array_push($page['infos'], l10n('This picture is no longer a backside')); |
---|
[10819] | 267 | } |
---|
| 268 | } |
---|
[10823] | 269 | } |
---|
| 270 | |
---|
| 271 | /* GET SAVED VALUES */ |
---|
| 272 | if ($template->get_template_vars('B2F_IS_VERSO') == null) |
---|
| 273 | { |
---|
[10852] | 274 | $template->assign('B2F_MOVE_VERSO', 'checked="checked"'); |
---|
| 275 | |
---|
[10823] | 276 | /* is the picture a verso ? */ |
---|
| 277 | $query = " |
---|
[10852] | 278 | SELECT image_id, categories |
---|
[10823] | 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 | { |
---|
[10852] | 286 | list($recto_id, $cats) = pwg_db_fetch_row($result); |
---|
[10823] | 287 | $template->assign(array( |
---|
| 288 | 'B2F_IS_VERSO' => 'checked="checked"', |
---|
| 289 | 'B2F_FRONT_ID' => $recto_id, |
---|
[10852] | 290 | 'B2F_MOVE_VERSO' => $cats != NULL ? 'checked="checked"' : '', |
---|
[10823] | 291 | )); |
---|
| 292 | } |
---|
| 293 | /* is the picture a front ? */ |
---|
[10819] | 294 | else |
---|
| 295 | { |
---|
[10823] | 296 | $query = "SELECT verso_id |
---|
[10819] | 297 | FROM ".B2F_TABLE." |
---|
[10823] | 298 | WHERE image_id = ".$_GET['image_id'].";"; |
---|
[10819] | 299 | $result = pwg_query($query); |
---|
| 300 | |
---|
| 301 | if (pwg_db_num_rows($result)) |
---|
[10852] | 302 | { |
---|
[10823] | 303 | $item = pwg_db_fetch_assoc($result); |
---|
[10852] | 304 | |
---|
[10819] | 305 | $template->assign(array( |
---|
[10852] | 306 | 'B2F_VERSO_ID' => $item['verso_id'], |
---|
| 307 | 'B2F_VERSO_URL' => get_root_url().'admin.php?page=picture_modify&image_id='.$item['verso_id'], |
---|
[10819] | 308 | )); |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | } |
---|
[10823] | 312 | |
---|
[10852] | 313 | $template->set_prefilter('picture_modify', 'Back2front_picture_modify_prefilter'); |
---|
[10819] | 314 | } |
---|
| 315 | |
---|
[10852] | 316 | |
---|
| 317 | function Back2front_picture_modify_prefilter($content, &$smarty) |
---|
[10819] | 318 | { |
---|
[10852] | 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); |
---|
[10819] | 322 | } |
---|
| 323 | |
---|
| 324 | ?> |
---|