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

Last change on this file since 29217 was 27707, checked in by JanisV, 11 years ago

Fixed association with physical album

File size: 5.1 KB
Line 
1<?php
2/*
3Plugin Name: physical2virtual
4Version: 2.6.j
5Description: Autoconvert physical albums to virtual
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=730
7Author: JanisV
8*/
9
10if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
11
12global $prefixeTable; 
13
14define('PHYSICAL2VIRTUAL_ID',       basename(dirname(__FILE__)));
15define('PHYSICAL2VIRTUAL_PATH' ,    PHPWG_PLUGINS_PATH . PHYSICAL2VIRTUAL_ID . '/');
16define('PHYSICAL2VIRTUAL_ADMIN',    get_root_url() . 'admin.php?page=plugin-' . PHYSICAL2VIRTUAL_ID);
17define('PHY2VIRT_CATEGORIES_TABLE', $prefixeTable . 'phy2virt_categories');
18
19add_event_handler('init', 'physical2virtual_init');
20if (defined('IN_ADMIN'))
21{
22  add_event_handler('get_admin_plugin_menu_links', 'physical2virtual_admin_menu');
23} 
24
25function physical2virtual_init()
26{
27  global $conf;
28 
29  load_language('plugin.lang', PHYSICAL2VIRTUAL_PATH);
30
31  $conf['physical2virtual'] = unserialize($conf['physical2virtual']);
32
33//  add_event_handler('invalidate_user_cache', 'physical2virtual_convert');
34}
35
36function find_or_create_virtual_category($phy_category, $parent_id=null, $options=array())
37{
38  $query = '
39SELECT DISTINCT virt_category_id AS id, representative_picture_id
40  FROM '.PHY2VIRT_CATEGORIES_TABLE.'
41  JOIN '.CATEGORIES_TABLE.' ON '.PHY2VIRT_CATEGORIES_TABLE.'.virt_category_id = '.CATEGORIES_TABLE.'.id
42  WHERE phy_category_id = '.$phy_category['id'].'
43;';
44  $virt_category = pwg_db_fetch_assoc(pwg_query($query));
45  if (!empty($virt_category))
46  {
47    return $virt_category;
48  }
49 
50  $category_name = $phy_category['name'];
51  // process empty album name
52  if (preg_match('/^\s*$/', $category_name))
53  {
54    $category_name = '_';
55  }
56
57  if (!is_null($parent_id))
58  {
59    $sub_ids = get_subcat_ids(array($parent_id));
60    foreach ($sub_ids as $id)
61    {
62      $cat_info = get_cat_info($id);
63      if ($cat_info['name'] == $category_name and !$cat_info['dir'])
64        return $cat_info;
65    }
66  }
67
68  $cat = create_virtual_category($category_name, $parent_id, $options);
69  $insert = array(
70    'phy_category_id' => $phy_category['id'],
71    'virt_category_id' => $cat['id'],
72//    'updated' => date('Y-m-d H:i:s'),
73    );
74  single_insert(PHY2VIRT_CATEGORIES_TABLE, $insert);
75
76  return $cat;
77}
78
79function physical2virtual_convert()
80{
81  global $conf;
82 
83  $options=array();
84  if (isset($conf['physical2virtual']['unlock_virtual']) and $conf['physical2virtual']['unlock_virtual'])
85    $options['visible'] = true;
86  $options['inherit'] = $conf['physical2virtual']['inherit'];
87
88  // Remove virtual without physical
89  $query = '
90    SELECT virt_category_id
91    FROM '.PHY2VIRT_CATEGORIES_TABLE.'
92    WHERE phy_category_id NOT IN (
93      SELECT cat.id
94      FROM '.CATEGORIES_TABLE.' cat
95      WHERE cat.dir IS NOT NULL
96    )
97  ;';
98  $category_ids = array_from_query($query, 'virt_category_id');
99  delete_categories($category_ids);
100
101  $query = '
102    DELETE FROM '.PHY2VIRT_CATEGORIES_TABLE.'
103    WHERE phy_category_id NOT IN (
104      SELECT cat.id
105      FROM '.CATEGORIES_TABLE.' cat
106      WHERE cat.dir IS NOT NULL
107    )
108  ;';
109  pwg_query($query);
110  $query = '
111    DELETE FROM '.PHY2VIRT_CATEGORIES_TABLE.'
112    WHERE virt_category_id NOT IN (
113      SELECT cat.id
114      FROM '.CATEGORIES_TABLE.' cat
115      WHERE cat.dir IS NULL
116    )
117  ;';
118  pwg_query($query);
119 
120  $query = '
121    SELECT id, name, id_uppercat
122    FROM '.CATEGORIES_TABLE.'
123    WHERE dir IS NOT NULL
124  ;';  // get physical
125  $result = pwg_query($query);
126  while ($row = pwg_db_fetch_assoc($result))
127  {
128    $parent = isset($conf['physical2virtual']['parent_cat']) ? $conf['physical2virtual']['parent_cat'] : null;
129       
130    if ($conf['physical2virtual']['store_structure'])
131    {
132      // get array of physical parents
133      $cat_info = get_cat_info($row['id']);
134      $uppers = $cat_info['upper_names'];
135      array_pop($uppers); // remove themself
136     
137      foreach ($uppers as $upper)
138      {
139        $t = find_or_create_virtual_category($upper, $parent, $options);
140        $parent = $t['id'];
141      }
142    }
143
144    $virt_cat = find_or_create_virtual_category($row, $parent, $options);
145
146    if (isset($conf['physical2virtual']['lock_physical']) and $conf['physical2virtual']['lock_physical'])
147    {
148      $cat_info = get_cat_info($row['id']);
149      if ($cat_info['visible'] == true)
150      {
151        set_cat_visible(array($row['id']), false);
152      }
153    }
154    if (isset($conf['physical2virtual']['private_physical']) and $conf['physical2virtual']['private_physical'])
155    {
156      set_cat_status(array($row['id']), 'private'); 
157    }
158
159    $query = '
160      INSERT IGNORE INTO '.IMAGE_CATEGORY_TABLE.' (category_id, image_id)
161        SELECT '.$virt_cat['id'].', image_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$row['id'].'
162    ;';
163    pwg_query($query);
164
165    $query = '
166UPDATE '.PHY2VIRT_CATEGORIES_TABLE.'
167  SET updated = NOW()
168  WHERE virt_category_id = '.$virt_cat['id'].'
169    ;';
170    pwg_query($query);
171
172    if (!(isset($virt_cat['representative_picture_id']) and $virt_cat['representative_picture_id']))
173    {
174      set_random_representant(array($virt_cat['id']));
175    }
176  }
177}
178
179function physical2virtual_admin_menu($menu)
180{
181  $menu[] = array(
182    'NAME' => 'physical2virtual',
183    'URL'  => PHYSICAL2VIRTUAL_ADMIN,
184  );
185
186  return $menu;
187}
188
189?>
Note: See TracBrowser for help on using the repository browser.