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

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

Set thumbnails for new virtual albums
Make physical albums private
Processing folder structure

File size: 3.2 KB
Line 
1<?php
2/*
3Plugin Name: physical2virtual
4Version: 2.6.b
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 find_or_create_virtual_category($category_name, $parent_id=null, $options=array())
33{
34  if (!is_null($parent_id))
35  {
36    $sub_ids = get_subcat_ids(array($parent_id));
37    foreach ($sub_ids as $id)
38    {
39      $cat_info = get_cat_info($id);
40      if ($cat_info['name'] == $category_name and !$cat_info['dir'])
41        return $cat_info;
42    }
43  }
44
45  return create_virtual_category($category_name, $parent_id, $options);
46}
47
48function physical2virtual_convert()
49{
50  global $conf;
51
52  $query = '
53    SELECT id, name, id_uppercat
54    FROM '.CATEGORIES_TABLE.'
55    WHERE dir IS NOT NULL
56  ;';  // get physical
57  $result = pwg_query($query);
58  while ($row = pwg_db_fetch_assoc($result))
59  {
60    $virt_name = $row['name'];
61
62    $parent = isset($conf['physical2virtual']['parent_cat']) ? $conf['physical2virtual']['parent_cat'] : null;
63       
64    if ($conf['physical2virtual']['store_structure'])
65    {
66      // get array of physical parents
67      $cat_info = get_cat_info($row['id']);
68      $uppers = $cat_info['upper_names'];
69      array_pop($uppers); // remove themself
70     
71      foreach ($uppers as $upper)
72      {
73        $parent = find_or_create_virtual_category($upper['name'], $parent)['id'];
74      }
75    }
76
77    $virt_cat = find_or_create_virtual_category($virt_name, $parent);
78//    $virt_cat = create_virtual_category($virt_name, $parent)['id']; // use this for duplicate folders name
79
80    if (isset($conf['physical2virtual']['lock_physical']) and $conf['physical2virtual']['lock_physical'])
81    {
82      $cat_info = get_cat_info($row['id']);
83      if ($cat_info['visible'] == true)
84      {
85        set_cat_visible(array($row['id']), false);
86      }
87    }
88    if (isset($conf['physical2virtual']['private_physical']) and $conf['physical2virtual']['private_physical'])
89    {
90      set_cat_status(array($row['id']), 'private'); 
91    }
92
93    $query = '
94      INSERT IGNORE INTO '.IMAGE_CATEGORY_TABLE.' (category_id, image_id)
95        SELECT '.$virt_cat['id'].', image_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$row['id'].'
96    ;';
97    pwg_query($query);
98
99    if (!(isset($virt_cat['representative_picture_id']) and $virt_cat['representative_picture_id']))
100    {
101      set_random_representant(array($virt_cat['id']));
102    }
103  }
104}
105
106function physical2virtual_admin_menu($menu)
107{
108  $menu[] = array(
109    'NAME' => 'physical2virtual',
110    'URL'  => PHYSICAL2VIRTUAL_ADMIN,
111  );
112
113  return $menu;
114}
115
116?>
Note: See TracBrowser for help on using the repository browser.