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

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

Added auto-update link

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