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

Last change on this file since 27413 was 27413, checked in by JanisV, 10 years ago

Added method for remote updating

File size: 4.8 KB
Line 
1<?php
2/*
3Plugin Name: physical2virtual
4Version: 2.6.f
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
40  FROM '.PHY2VIRT_CATEGORIES_TABLE.'
41  WHERE phy_category_id = '.$phy_category['id'].'
42;';
43  $virt_category = pwg_db_fetch_assoc(pwg_query($query));
44  if (!empty($virt_category))
45  {
46    return $virt_category; /// add $virt_cat['representative_picture_id']
47  }
48 
49  $category_name = $phy_category['name'];
50  // process empty album name
51  if (preg_match('/^\s*$/', $category_name))
52  {
53    $category_name = '_';
54  }
55
56  if (!is_null($parent_id))
57  {
58    $sub_ids = get_subcat_ids(array($parent_id));
59    foreach ($sub_ids as $id)
60    {
61      $cat_info = get_cat_info($id);
62      if ($cat_info['name'] == $category_name and !$cat_info['dir'])
63        return $cat_info;
64    }
65  }
66
67  $cat = create_virtual_category($category_name, $parent_id, $options);
68  $insert = array(
69    'phy_category_id' => $phy_category['id'],
70    'virt_category_id' => $cat['id'],
71//    'updated' => date('Y-m-d H:i:s'),
72    );
73  single_insert(PHY2VIRT_CATEGORIES_TABLE, $insert);
74
75  return $cat;
76}
77
78function physical2virtual_convert()
79{
80  global $conf;
81 
82  $options=array();
83  if (isset($conf['physical2virtual']['unlock_virtual']) and $conf['physical2virtual']['unlock_virtual'])
84    $options['visible'] = true;
85  $options['inherit'] = $conf['physical2virtual']['inherit'];
86
87  // Remove virtual without physical
88  $query = '
89    SELECT virt_category_id
90    FROM '.PHY2VIRT_CATEGORIES_TABLE.'
91    WHERE phy_category_id NOT IN (
92      SELECT cat.id
93      FROM '.CATEGORIES_TABLE.' cat
94      WHERE cat.dir IS NOT NULL
95    )
96  ;';
97  $category_ids = array_from_query($query, 'virt_category_id');
98  delete_categories($category_ids);
99
100  $query = '
101    DELETE FROM '.PHY2VIRT_CATEGORIES_TABLE.'
102    WHERE phy_category_id NOT IN (
103      SELECT cat.id
104      FROM '.CATEGORIES_TABLE.' cat
105      WHERE cat.dir IS NOT NULL
106    )
107  ;';
108  pwg_query($query);
109 
110  $query = '
111    SELECT id, name, id_uppercat
112    FROM '.CATEGORIES_TABLE.'
113    WHERE dir IS NOT NULL
114  ;';  // get physical
115  $result = pwg_query($query);
116  while ($row = pwg_db_fetch_assoc($result))
117  {
118    $parent = isset($conf['physical2virtual']['parent_cat']) ? $conf['physical2virtual']['parent_cat'] : null;
119       
120    if ($conf['physical2virtual']['store_structure'])
121    {
122      // get array of physical parents
123      $cat_info = get_cat_info($row['id']);
124      $uppers = $cat_info['upper_names'];
125      array_pop($uppers); // remove themself
126     
127      foreach ($uppers as $upper)
128      {
129        $parent = find_or_create_virtual_category($upper, $parent, $options)['id'];
130      }
131    }
132
133    $virt_cat = find_or_create_virtual_category($row, $parent, $options);
134
135    if (isset($conf['physical2virtual']['lock_physical']) and $conf['physical2virtual']['lock_physical'])
136    {
137      $cat_info = get_cat_info($row['id']);
138      if ($cat_info['visible'] == true)
139      {
140        set_cat_visible(array($row['id']), false);
141      }
142    }
143    if (isset($conf['physical2virtual']['private_physical']) and $conf['physical2virtual']['private_physical'])
144    {
145      set_cat_status(array($row['id']), 'private'); 
146    }
147
148    $query = '
149      INSERT IGNORE INTO '.IMAGE_CATEGORY_TABLE.' (category_id, image_id)
150        SELECT '.$virt_cat['id'].', image_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$row['id'].'
151    ;';
152    pwg_query($query);
153
154    $query = '
155UPDATE '.PHY2VIRT_CATEGORIES_TABLE.'
156  SET updated = NOW()
157  WHERE virt_category_id = '.$virt_cat['id'].'
158    ;';
159    pwg_query($query);
160
161    if (!(isset($virt_cat['representative_picture_id']) and $virt_cat['representative_picture_id']))
162    {
163      set_random_representant(array($virt_cat['id']));
164    }
165  }
166}
167
168function physical2virtual_admin_menu($menu)
169{
170  $menu[] = array(
171    'NAME' => 'physical2virtual',
172    'URL'  => PHYSICAL2VIRTUAL_ADMIN,
173  );
174
175  return $menu;
176}
177
178?>
Note: See TracBrowser for help on using the repository browser.