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

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

Updated storing association physical 2 virtual

File size: 4.8 KB
RevLine 
[27096]1<?php
2/*
3Plugin Name: physical2virtual
[27330]4Version: 2.6.e
[27096]5Description: Autoconvert physical albums to virtual
[27182]6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=730
[27096]7Author: JanisV
8*/
9
10if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
11
[27330]12global $prefixeTable; 
13
[27096]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);
[27330]17define('PHY2VIRT_CATEGORIES_TABLE', $prefixeTable . 'phy2virt_categories');
[27096]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
[27330]36function find_or_create_virtual_category($phy_category, $parent_id=null, $options=array())
[27164]37{
[27330]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'];
[27183]50  // process empty album name
51  if (preg_match('/^\s*$/', $category_name))
52  {
53    $category_name = '_';
54  }
55
[27164]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
[27330]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;
[27164]76}
77
[27096]78function physical2virtual_convert()
79{
80  global $conf;
[27229]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'];
[27096]86
[27330]87  // Remove virtual without physical
[27096]88  $query = '
[27330]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 = '
[27164]111    SELECT id, name, id_uppercat
[27096]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  {
[27164]118    $parent = isset($conf['physical2virtual']['parent_cat']) ? $conf['physical2virtual']['parent_cat'] : null;
119       
120    if ($conf['physical2virtual']['store_structure'])
[27096]121    {
[27164]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)
[27096]128      {
[27330]129        $parent = find_or_create_virtual_category($upper, $parent, $options)['id'];
[27096]130      }
[27164]131    }
132
[27330]133    $virt_cat = find_or_create_virtual_category($row, $parent, $options);
[27164]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)
[27147]139      {
[27164]140        set_cat_visible(array($row['id']), false);
[27147]141      }
[27096]142    }
[27164]143    if (isset($conf['physical2virtual']['private_physical']) and $conf['physical2virtual']['private_physical'])
[27096]144    {
[27164]145      set_cat_status(array($row['id']), 'private'); 
[27096]146    }
147
148    $query = '
149      INSERT IGNORE INTO '.IMAGE_CATEGORY_TABLE.' (category_id, image_id)
[27164]150        SELECT '.$virt_cat['id'].', image_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$row['id'].'
[27096]151    ;';
152    pwg_query($query);
[27146]153
[27330]154    $query = '
155UPDATE '.PHY2VIRT_CATEGORIES_TABLE.'
156  SET updated = NOW()
157  WHERE virt_category_id = '.$virt_cat['id'].'
158    ;';
159    pwg_query($query);
160
[27164]161    if (!(isset($virt_cat['representative_picture_id']) and $virt_cat['representative_picture_id']))
[27146]162    {
[27164]163      set_random_representant(array($virt_cat['id']));
[27146]164    }
[27096]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.