PageRenderTime 24ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/admin/tags.php

https://github.com/xmirror/piwigo
PHP | 531 lines | 413 code | 62 blank | 56 comment | 34 complexity | 35abccde6ce7cb0af9b0c748f52f559c MD5 | raw file
  1. <?php
  2. // +-----------------------------------------------------------------------+
  3. // | Piwigo - a PHP based photo gallery |
  4. // +-----------------------------------------------------------------------+
  5. // | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org |
  6. // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net |
  7. // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick |
  8. // +-----------------------------------------------------------------------+
  9. // | This program is free software; you can redistribute it and/or modify |
  10. // | it under the terms of the GNU General Public License as published by |
  11. // | the Free Software Foundation |
  12. // | |
  13. // | This program is distributed in the hope that it will be useful, but |
  14. // | WITHOUT ANY WARRANTY; without even the implied warranty of |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
  16. // | General Public License for more details. |
  17. // | |
  18. // | You should have received a copy of the GNU General Public License |
  19. // | along with this program; if not, write to the Free Software |
  20. // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
  21. // | USA. |
  22. // +-----------------------------------------------------------------------+
  23. if( !defined("PHPWG_ROOT_PATH") )
  24. {
  25. die ("Hacking attempt!");
  26. }
  27. include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
  28. check_status(ACCESS_ADMINISTRATOR);
  29. if (!empty($_POST))
  30. {
  31. check_pwg_token();
  32. }
  33. // +-----------------------------------------------------------------------+
  34. // | edit tags |
  35. // +-----------------------------------------------------------------------+
  36. if (isset($_POST['submit']))
  37. {
  38. $query = '
  39. SELECT name
  40. FROM '.TAGS_TABLE.'
  41. ;';
  42. $existing_names = array_from_query($query, 'name');
  43. $current_name_of = array();
  44. $query = '
  45. SELECT id, name
  46. FROM '.TAGS_TABLE.'
  47. WHERE id IN ('.$_POST['edit_list'].')
  48. ;';
  49. $result = pwg_query($query);
  50. while ($row = pwg_db_fetch_assoc($result))
  51. {
  52. $current_name_of[ $row['id'] ] = $row['name'];
  53. }
  54. $updates = array();
  55. // we must not rename tag with an already existing name
  56. foreach (explode(',', $_POST['edit_list']) as $tag_id)
  57. {
  58. $tag_name = stripslashes($_POST['tag_name-'.$tag_id]);
  59. if ($tag_name != $current_name_of[$tag_id])
  60. {
  61. if (in_array($tag_name, $existing_names))
  62. {
  63. array_push(
  64. $page['errors'],
  65. sprintf(
  66. l10n('Tag "%s" already exists'),
  67. $tag_name
  68. )
  69. );
  70. }
  71. else if (!empty($tag_name))
  72. {
  73. array_push(
  74. $updates,
  75. array(
  76. 'id' => $tag_id,
  77. 'name' => addslashes($tag_name),
  78. 'url_name' => trigger_event('render_tag_url', $tag_name),
  79. )
  80. );
  81. }
  82. }
  83. }
  84. mass_updates(
  85. TAGS_TABLE,
  86. array(
  87. 'primary' => array('id'),
  88. 'update' => array('name', 'url_name'),
  89. ),
  90. $updates
  91. );
  92. }
  93. // +-----------------------------------------------------------------------+
  94. // | dulicate tags |
  95. // +-----------------------------------------------------------------------+
  96. if (isset($_POST['duplic_submit']))
  97. {
  98. $query = '
  99. SELECT name
  100. FROM '.TAGS_TABLE.'
  101. ;';
  102. $existing_names = array_from_query($query, 'name');
  103. $current_name_of = array();
  104. $query = '
  105. SELECT id, name
  106. FROM '.TAGS_TABLE.'
  107. WHERE id IN ('.$_POST['edit_list'].')
  108. ;';
  109. $result = pwg_query($query);
  110. while ($row = pwg_db_fetch_assoc($result))
  111. {
  112. $current_name_of[ $row['id'] ] = $row['name'];
  113. }
  114. $updates = array();
  115. // we must not rename tag with an already existing name
  116. foreach (explode(',', $_POST['edit_list']) as $tag_id)
  117. {
  118. $tag_name = stripslashes($_POST['tag_name-'.$tag_id]);
  119. if ($tag_name != $current_name_of[$tag_id])
  120. {
  121. if (in_array($tag_name, $existing_names))
  122. {
  123. array_push(
  124. $page['errors'],
  125. sprintf(
  126. l10n('Tag "%s" already exists'),
  127. $tag_name
  128. )
  129. );
  130. }
  131. else if (!empty($tag_name))
  132. {
  133. mass_inserts(
  134. TAGS_TABLE,
  135. array('name', 'url_name'),
  136. array(
  137. array(
  138. 'name' => $tag_name,
  139. 'url_name' => trigger_event('render_tag_url', $tag_name),
  140. )
  141. )
  142. );
  143. $query = '
  144. SELECT id
  145. FROM '.TAGS_TABLE.'
  146. WHERE name = \''.$tag_name.'\'
  147. ;';
  148. $destination_tag = array_from_query($query, 'id');
  149. $destination_tag_id = $destination_tag[0];
  150. $query = '
  151. SELECT
  152. image_id
  153. FROM '.IMAGE_TAG_TABLE.'
  154. WHERE tag_id = '.$tag_id.'
  155. ;';
  156. $destination_tag_image_ids = array_from_query($query, 'image_id');
  157. $inserts = array();
  158. foreach ($destination_tag_image_ids as $image_id)
  159. {
  160. array_push(
  161. $inserts,
  162. array(
  163. 'tag_id' => $destination_tag_id,
  164. 'image_id' => $image_id
  165. )
  166. );
  167. }
  168. if (count($inserts) > 0)
  169. {
  170. mass_inserts(
  171. IMAGE_TAG_TABLE,
  172. array_keys($inserts[0]),
  173. $inserts
  174. );
  175. }
  176. array_push(
  177. $page['infos'],
  178. sprintf(
  179. l10n('Tag "%s" is now a duplicate of "%s"'),
  180. stripslashes($tag_name),
  181. $current_name_of[$tag_id]
  182. )
  183. );
  184. }
  185. }
  186. }
  187. mass_updates(
  188. TAGS_TABLE,
  189. array(
  190. 'primary' => array('id'),
  191. 'update' => array('name', 'url_name'),
  192. ),
  193. $updates
  194. );
  195. }
  196. // +-----------------------------------------------------------------------+
  197. // | merge tags |
  198. // +-----------------------------------------------------------------------+
  199. if (isset($_POST['confirm_merge']))
  200. {
  201. if (!isset($_POST['destination_tag']))
  202. {
  203. array_push(
  204. $page['errors'],
  205. l10n('No destination tag selected')
  206. );
  207. }
  208. else
  209. {
  210. $destination_tag_id = $_POST['destination_tag'];
  211. $tag_ids = explode(',', $_POST['merge_list']);
  212. if (is_array($tag_ids) and count($tag_ids) > 1)
  213. {
  214. $name_of_tag = array();
  215. $query = '
  216. SELECT
  217. id,
  218. name
  219. FROM '.TAGS_TABLE.'
  220. WHERE id IN ('.implode(',', $tag_ids).')
  221. ;';
  222. $result = pwg_query($query);
  223. while ($row = pwg_db_fetch_assoc($result))
  224. {
  225. $name_of_tag[ $row['id'] ] = trigger_event('render_tag_name', $row['name']);
  226. }
  227. $tag_ids_to_delete = array_diff(
  228. $tag_ids,
  229. array($destination_tag_id)
  230. );
  231. $query = '
  232. SELECT
  233. DISTINCT(image_id)
  234. FROM '.IMAGE_TAG_TABLE.'
  235. WHERE tag_id IN ('.implode(',', $tag_ids_to_delete).')
  236. ;';
  237. $image_ids = array_from_query($query, 'image_id');
  238. delete_tags($tag_ids_to_delete);
  239. $query = '
  240. SELECT
  241. image_id
  242. FROM '.IMAGE_TAG_TABLE.'
  243. WHERE tag_id = '.$destination_tag_id.'
  244. ;';
  245. $destination_tag_image_ids = array_from_query($query, 'image_id');
  246. $image_ids_to_link = array_diff(
  247. $image_ids,
  248. $destination_tag_image_ids
  249. );
  250. $inserts = array();
  251. foreach ($image_ids_to_link as $image_id)
  252. {
  253. array_push(
  254. $inserts,
  255. array(
  256. 'tag_id' => $destination_tag_id,
  257. 'image_id' => $image_id
  258. )
  259. );
  260. }
  261. if (count($inserts) > 0)
  262. {
  263. mass_inserts(
  264. IMAGE_TAG_TABLE,
  265. array_keys($inserts[0]),
  266. $inserts
  267. );
  268. }
  269. $tags_deleted = array();
  270. foreach ($tag_ids_to_delete as $tag_id)
  271. {
  272. $tags_deleted[] = $name_of_tag[$tag_id];
  273. }
  274. array_push(
  275. $page['infos'],
  276. sprintf(
  277. l10n('Tags <em>%s</em> merged into tag <em>%s</em>'),
  278. implode(', ', $tags_deleted),
  279. $name_of_tag[$destination_tag_id]
  280. )
  281. );
  282. }
  283. }
  284. }
  285. // +-----------------------------------------------------------------------+
  286. // | delete tags |
  287. // +-----------------------------------------------------------------------+
  288. if (isset($_POST['delete']) and isset($_POST['tags']))
  289. {
  290. $query = '
  291. SELECT name
  292. FROM '.TAGS_TABLE.'
  293. WHERE id IN ('.implode(',', $_POST['tags']).')
  294. ;';
  295. $tag_names = array_from_query($query, 'name');
  296. delete_tags($_POST['tags']);
  297. array_push(
  298. $page['infos'],
  299. l10n_dec(
  300. 'The following tag was deleted',
  301. 'The %d following tags were deleted',
  302. count($tag_names)).' : '.
  303. implode(', ', $tag_names)
  304. );
  305. }
  306. // +-----------------------------------------------------------------------+
  307. // | delete orphan tags |
  308. // +-----------------------------------------------------------------------+
  309. if (isset($_GET['action']) and 'delete_orphans' == $_GET['action'])
  310. {
  311. check_pwg_token();
  312. delete_orphan_tags();
  313. $_SESSION['page_infos'] = array(l10n('Orphan tags deleted'));
  314. redirect(get_root_url().'admin.php?page=tags');
  315. }
  316. // +-----------------------------------------------------------------------+
  317. // | add a tag |
  318. // +-----------------------------------------------------------------------+
  319. if (isset($_POST['add']) and !empty($_POST['add_tag']))
  320. {
  321. $tag_name = $_POST['add_tag'];
  322. // does the tag already exists?
  323. $query = '
  324. SELECT id
  325. FROM '.TAGS_TABLE.'
  326. WHERE name = \''.$tag_name.'\'
  327. ;';
  328. $existing_tags = array_from_query($query, 'id');
  329. if (count($existing_tags) == 0)
  330. {
  331. mass_inserts(
  332. TAGS_TABLE,
  333. array('name', 'url_name'),
  334. array(
  335. array(
  336. 'name' => $tag_name,
  337. 'url_name' => trigger_event('render_tag_url', $tag_name),
  338. )
  339. )
  340. );
  341. array_push(
  342. $page['infos'],
  343. sprintf(
  344. l10n('Tag "%s" was added'),
  345. stripslashes($tag_name)
  346. )
  347. );
  348. }
  349. else
  350. {
  351. array_push(
  352. $page['errors'],
  353. sprintf(
  354. l10n('Tag "%s" already exists'),
  355. stripslashes($tag_name)
  356. )
  357. );
  358. }
  359. }
  360. // +-----------------------------------------------------------------------+
  361. // | template init |
  362. // +-----------------------------------------------------------------------+
  363. $template->set_filenames(array('tags' => 'tags.tpl'));
  364. $template->assign(
  365. array(
  366. 'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=tags',
  367. 'PWG_TOKEN' => get_pwg_token(),
  368. )
  369. );
  370. // +-----------------------------------------------------------------------+
  371. // | orphan tags |
  372. // +-----------------------------------------------------------------------+
  373. $orphan_tags = get_orphan_tags();
  374. $orphan_tag_names = array();
  375. foreach ($orphan_tags as $tag)
  376. {
  377. array_push($orphan_tag_names, trigger_event('render_tag_name', $tag['name']));
  378. }
  379. if (count($orphan_tag_names) > 0)
  380. {
  381. array_push(
  382. $page['warnings'],
  383. sprintf(
  384. l10n('You have %d orphan tags: %s.').' <a href="%s">'.l10n('Delete orphan tags').'</a>',
  385. count($orphan_tag_names),
  386. implode(', ', $orphan_tag_names),
  387. get_root_url().'admin.php?page=tags&amp;action=delete_orphans&amp;pwg_token='.get_pwg_token()
  388. )
  389. );
  390. }
  391. // +-----------------------------------------------------------------------+
  392. // | form creation |
  393. // +-----------------------------------------------------------------------+
  394. // tag counters
  395. $query = '
  396. SELECT tag_id, COUNT(image_id) AS counter
  397. FROM '.IMAGE_TAG_TABLE.'
  398. GROUP BY tag_id';
  399. $tag_counters = simple_hash_from_query($query, 'tag_id', 'counter');
  400. // all tags
  401. $query = '
  402. SELECT *
  403. FROM '.TAGS_TABLE.'
  404. ;';
  405. $result = pwg_query($query);
  406. $all_tags = array();
  407. while ($tag = pwg_db_fetch_assoc($result))
  408. {
  409. $raw_name = $tag['name'];
  410. $tag['name'] = trigger_event('render_tag_name', $raw_name);
  411. $tag['counter'] = intval(@$tag_counters[ $tag['id'] ]);
  412. $tag['U_VIEW'] = make_index_url(array('tags'=>array($tag)));
  413. $tag['U_EDIT'] = 'admin.php?page=batch_manager&amp;tag='.$tag['id'];
  414. $alt_names = trigger_event('get_tag_alt_names', array(), $raw_name);
  415. $alt_names = array_diff( array_unique($alt_names), array($tag['name']) );
  416. if (count($alt_names))
  417. {
  418. $tag['alt_names'] = implode(', ', $alt_names);
  419. }
  420. $all_tags[] = $tag;
  421. }
  422. usort($all_tags, 'tag_alpha_compare');
  423. $template->assign(
  424. array(
  425. 'all_tags' => $all_tags,
  426. )
  427. );
  428. if ((isset($_POST['edit']) or isset($_POST['duplicate']) or isset($_POST['merge'])) and isset($_POST['tags']))
  429. {
  430. $list_name = 'EDIT_TAGS_LIST';
  431. if (isset($_POST['duplicate']))
  432. {
  433. $list_name = 'DUPLIC_TAGS_LIST';
  434. }
  435. elseif (isset($_POST['merge']))
  436. {
  437. $list_name = 'MERGE_TAGS_LIST';
  438. }
  439. $template->assign(
  440. array(
  441. $list_name => implode(',', $_POST['tags']),
  442. )
  443. );
  444. $query = '
  445. SELECT id, name
  446. FROM '.TAGS_TABLE.'
  447. WHERE id IN ('.implode(',', $_POST['tags']).')
  448. ;';
  449. $result = pwg_query($query);
  450. while ($row = pwg_db_fetch_assoc($result))
  451. {
  452. $name_of[ $row['id'] ] = $row['name'];
  453. }
  454. foreach ($_POST['tags'] as $tag_id)
  455. {
  456. $template->append(
  457. 'tags',
  458. array(
  459. 'ID' => $tag_id,
  460. 'NAME' => $name_of[$tag_id],
  461. )
  462. );
  463. }
  464. }
  465. // +-----------------------------------------------------------------------+
  466. // | sending html code |
  467. // +-----------------------------------------------------------------------+
  468. $template->assign_var_from_handle('ADMIN_CONTENT', 'tags');
  469. ?>