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

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

New options (unlock new virtual albums, inherit permissions)

File size: 3.6 KB
RevLine 
[27096]1<?php
2/*
3Plugin Name: physical2virtual
[27229]4Version: 2.6.d
[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
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
[27164]33function find_or_create_virtual_category($category_name, $parent_id=null, $options=array())
34{
[27183]35  // process empty album name
36  if (preg_match('/^\s*$/', $category_name))
37  {
38    $category_name = '_';
39  }
40
[27164]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
[27096]55function physical2virtual_convert()
56{
57  global $conf;
[27229]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'];
[27096]63
64  $query = '
[27164]65    SELECT id, name, id_uppercat
[27096]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'];
[27164]73
74    $parent = isset($conf['physical2virtual']['parent_cat']) ? $conf['physical2virtual']['parent_cat'] : null;
75       
76    if ($conf['physical2virtual']['store_structure'])
[27096]77    {
[27164]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)
[27096]84      {
[27229]85        $parent = find_or_create_virtual_category($upper['name'], $parent, $options)['id'];
[27096]86      }
[27164]87    }
88
[27229]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
[27164]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)
[27147]96      {
[27164]97        set_cat_visible(array($row['id']), false);
[27147]98      }
[27096]99    }
[27164]100    if (isset($conf['physical2virtual']['private_physical']) and $conf['physical2virtual']['private_physical'])
[27096]101    {
[27164]102      set_cat_status(array($row['id']), 'private'); 
[27096]103    }
104
105    $query = '
106      INSERT IGNORE INTO '.IMAGE_CATEGORY_TABLE.' (category_id, image_id)
[27164]107        SELECT '.$virt_cat['id'].', image_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$row['id'].'
[27096]108    ;';
109    pwg_query($query);
[27146]110
[27164]111    if (!(isset($virt_cat['representative_picture_id']) and $virt_cat['representative_picture_id']))
[27146]112    {
[27164]113      set_random_representant(array($virt_cat['id']));
[27146]114    }
[27096]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.