source: extensions/physical2virtual/main.inc.php @ 27096

Revision 27096, 2.4 KB checked in by JanisV, 6 years ago (diff)

Initial commit

Line 
1<?php
2/*
3Plugin Name: physical2virtual
4Version: 2.6.a
5Description: Autoconvert physical albums to virtual
6Author: JanisV
7*/
8
9if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
10
11define('PHYSICAL2VIRTUAL_ID',       basename(dirname(__FILE__)));
12define('PHYSICAL2VIRTUAL_PATH' ,    PHPWG_PLUGINS_PATH . PHYSICAL2VIRTUAL_ID . '/');
13define('PHYSICAL2VIRTUAL_ADMIN',    get_root_url() . 'admin.php?page=plugin-' . PHYSICAL2VIRTUAL_ID);
14
15add_event_handler('init', 'physical2virtual_init');
16if (defined('IN_ADMIN'))
17{
18  add_event_handler('get_admin_plugin_menu_links', 'physical2virtual_admin_menu');
19} 
20
21function physical2virtual_init()
22{
23  global $conf;
24 
25  load_language('plugin.lang', PHYSICAL2VIRTUAL_PATH);
26
27  $conf['physical2virtual'] = unserialize($conf['physical2virtual']);
28
29//  add_event_handler('invalidate_user_cache', 'physical2virtual_convert');
30}
31
32function physical2virtual_convert()
33{
34  global $conf;
35
36  $query = '
37    SELECT name
38    FROM '.CATEGORIES_TABLE.'
39    WHERE dir IS NULL
40  ;';  // get virtual
41  $virt_cats = array_from_query($query, 'name'); 
42 
43  $query = '
44    SELECT id, name
45    FROM '.CATEGORIES_TABLE.'
46    WHERE dir IS NOT NULL
47  ;';  // get physical
48  $result = pwg_query($query);
49  while ($row = pwg_db_fetch_assoc($result))
50  {
51    $virt_name = $row['name'];
52    if (!in_array($virt_name, $virt_cats))
53    {
54      $parent = isset($conf['physical2virtual']['parent_cat']) ? $conf['physical2virtual']['parent_cat'] : null;
55      $virt_id = create_virtual_category($virt_name, $parent)['id'];
56
57      if (isset($conf['physical2virtual']['lock_physical']) and $conf['physical2virtual']['lock_physical'])
58      {
59        $cat_info = get_cat_info($row['id']);
60        if ($cat_info['visible'] == true)
61        {
62          set_cat_visible(array($row['id']), false);
63        }
64      }
65    }
66    else
67    {
68      // Get existen virt category ID
69      $query = '
70        SELECT id FROM '.CATEGORIES_TABLE.' WHERE dir IS NULL AND name = \''.pwg_db_real_escape_string($virt_name).'\'
71      ;';
72      list($virt_id) = pwg_db_fetch_row(pwg_query($query));
73    }
74
75    $query = '
76      INSERT IGNORE INTO '.IMAGE_CATEGORY_TABLE.' (category_id, image_id)
77        SELECT '.$virt_id.', image_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$row['id'].'
78    ;';
79    pwg_query($query);
80  }
81}
82
83function physical2virtual_admin_menu($menu)
84{
85  $menu[] = array(
86    'NAME' => 'physical2virtual',
87    'URL'  => PHYSICAL2VIRTUAL_ADMIN,
88  );
89
90  return $menu;
91}
92
93?>
Note: See TracBrowser for help on using the repository browser.