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

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

Added processing for empty (white-space/underscope) physical album name

File size: 3.4 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  // 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  $query = '
60    SELECT id, name, id_uppercat
61    FROM '.CATEGORIES_TABLE.'
62    WHERE dir IS NOT NULL
63  ;';  // get physical
64  $result = pwg_query($query);
65  while ($row = pwg_db_fetch_assoc($result))
66  {
67    $virt_name = $row['name'];
68
69    $parent = isset($conf['physical2virtual']['parent_cat']) ? $conf['physical2virtual']['parent_cat'] : null;
70       
71    if ($conf['physical2virtual']['store_structure'])
72    {
73      // get array of physical parents
74      $cat_info = get_cat_info($row['id']);
75      $uppers = $cat_info['upper_names'];
76      array_pop($uppers); // remove themself
77     
78      foreach ($uppers as $upper)
79      {
80        $parent = find_or_create_virtual_category($upper['name'], $parent)['id'];
81      }
82    }
83
84    $virt_cat = find_or_create_virtual_category($virt_name, $parent);
85//    $virt_cat = create_virtual_category($virt_name, $parent)['id']; // use this for duplicate folders name
86
87    if (isset($conf['physical2virtual']['lock_physical']) and $conf['physical2virtual']['lock_physical'])
88    {
89      $cat_info = get_cat_info($row['id']);
90      if ($cat_info['visible'] == true)
91      {
92        set_cat_visible(array($row['id']), false);
93      }
94    }
95    if (isset($conf['physical2virtual']['private_physical']) and $conf['physical2virtual']['private_physical'])
96    {
97      set_cat_status(array($row['id']), 'private'); 
98    }
99
100    $query = '
101      INSERT IGNORE INTO '.IMAGE_CATEGORY_TABLE.' (category_id, image_id)
102        SELECT '.$virt_cat['id'].', image_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$row['id'].'
103    ;';
104    pwg_query($query);
105
106    if (!(isset($virt_cat['representative_picture_id']) and $virt_cat['representative_picture_id']))
107    {
108      set_random_representant(array($virt_cat['id']));
109    }
110  }
111}
112
113function physical2virtual_admin_menu($menu)
114{
115  $menu[] = array(
116    'NAME' => 'physical2virtual',
117    'URL'  => PHYSICAL2VIRTUAL_ADMIN,
118  );
119
120  return $menu;
121}
122
123?>
Note: See TracBrowser for help on using the repository browser.