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

Revision 27229, 3.6 KB checked in by JanisV, 6 years ago (diff)

New options (unlock new virtual albums, inherit permissions)

Line 
1<?php
2/*
3Plugin Name: physical2virtual
4Version: 2.6.d
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
12define('PHYSICAL2VIRTUAL_ID',       basename(dirname(__FILE__)));
13define('PHYSICAL2VIRTUAL_PATH' ,    PHPWG_PLUGINS_PATH . PHYSICAL2VIRTUAL_ID . '/');
14define('PHYSICAL2VIRTUAL_ADMIN',    get_root_url() . 'admin.php?page=plugin-' . PHYSICAL2VIRTUAL_ID);
15
16add_event_handler('init', 'physical2virtual_init');
17if (defined('IN_ADMIN'))
18{
19  add_event_handler('get_admin_plugin_menu_links', 'physical2virtual_admin_menu');
20} 
21
22function physical2virtual_init()
23{
24  global $conf;
25 
26  load_language('plugin.lang', PHYSICAL2VIRTUAL_PATH);
27
28  $conf['physical2virtual'] = unserialize($conf['physical2virtual']);
29
30//  add_event_handler('invalidate_user_cache', 'physical2virtual_convert');
31}
32
33function find_or_create_virtual_category($category_name, $parent_id=null, $options=array())
34{
35  // process empty album name
36  if (preg_match('/^\s*$/', $category_name))
37  {
38    $category_name = '_';
39  }
40
41  if (!is_null($parent_id))
42  {
43    $sub_ids = get_subcat_ids(array($parent_id));
44    foreach ($sub_ids as $id)
45    {
46      $cat_info = get_cat_info($id);
47      if ($cat_info['name'] == $category_name and !$cat_info['dir'])
48        return $cat_info;
49    }
50  }
51
52  return create_virtual_category($category_name, $parent_id, $options);
53}
54
55function physical2virtual_convert()
56{
57  global $conf;
58 
59  $options=array();
60  if (isset($conf['physical2virtual']['unlock_virtual']) and $conf['physical2virtual']['unlock_virtual'])
61    $options['visible'] = true;
62  $options['inherit'] = $conf['physical2virtual']['inherit'];
63
64  $query = '
65    SELECT id, name, id_uppercat
66    FROM '.CATEGORIES_TABLE.'
67    WHERE dir IS NOT NULL
68  ;';  // get physical
69  $result = pwg_query($query);
70  while ($row = pwg_db_fetch_assoc($result))
71  {
72    $virt_name = $row['name'];
73
74    $parent = isset($conf['physical2virtual']['parent_cat']) ? $conf['physical2virtual']['parent_cat'] : null;
75       
76    if ($conf['physical2virtual']['store_structure'])
77    {
78      // get array of physical parents
79      $cat_info = get_cat_info($row['id']);
80      $uppers = $cat_info['upper_names'];
81      array_pop($uppers); // remove themself
82     
83      foreach ($uppers as $upper)
84      {
85        $parent = find_or_create_virtual_category($upper['name'], $parent, $options)['id'];
86      }
87    }
88
89    $virt_cat = find_or_create_virtual_category($virt_name, $parent, $options);
90//    $virt_cat = create_virtual_category($virt_name, $parent, $options); // use this for duplicate folders name
91
92    if (isset($conf['physical2virtual']['lock_physical']) and $conf['physical2virtual']['lock_physical'])
93    {
94      $cat_info = get_cat_info($row['id']);
95      if ($cat_info['visible'] == true)
96      {
97        set_cat_visible(array($row['id']), false);
98      }
99    }
100    if (isset($conf['physical2virtual']['private_physical']) and $conf['physical2virtual']['private_physical'])
101    {
102      set_cat_status(array($row['id']), 'private'); 
103    }
104
105    $query = '
106      INSERT IGNORE INTO '.IMAGE_CATEGORY_TABLE.' (category_id, image_id)
107        SELECT '.$virt_cat['id'].', image_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$row['id'].'
108    ;';
109    pwg_query($query);
110
111    if (!(isset($virt_cat['representative_picture_id']) and $virt_cat['representative_picture_id']))
112    {
113      set_random_representant(array($virt_cat['id']));
114    }
115  }
116}
117
118function physical2virtual_admin_menu($menu)
119{
120  $menu[] = array(
121    'NAME' => 'physical2virtual',
122    'URL'  => PHYSICAL2VIRTUAL_ADMIN,
123  );
124
125  return $menu;
126}
127
128?>
Note: See TracBrowser for help on using the repository browser.