[27096] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | Plugin Name: physical2virtual |
---|
[27707] | 4 | Version: 2.6.j |
---|
[27096] | 5 | Description: Autoconvert physical albums to virtual |
---|
[27182] | 6 | Plugin URI: http://piwigo.org/ext/extension_view.php?eid=730 |
---|
[27096] | 7 | Author: JanisV |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
| 11 | |
---|
[27330] | 12 | global $prefixeTable; |
---|
| 13 | |
---|
[27096] | 14 | define('PHYSICAL2VIRTUAL_ID', basename(dirname(__FILE__))); |
---|
| 15 | define('PHYSICAL2VIRTUAL_PATH' , PHPWG_PLUGINS_PATH . PHYSICAL2VIRTUAL_ID . '/'); |
---|
| 16 | define('PHYSICAL2VIRTUAL_ADMIN', get_root_url() . 'admin.php?page=plugin-' . PHYSICAL2VIRTUAL_ID); |
---|
[27330] | 17 | define('PHY2VIRT_CATEGORIES_TABLE', $prefixeTable . 'phy2virt_categories'); |
---|
[27096] | 18 | |
---|
| 19 | add_event_handler('init', 'physical2virtual_init'); |
---|
| 20 | if (defined('IN_ADMIN')) |
---|
| 21 | { |
---|
| 22 | add_event_handler('get_admin_plugin_menu_links', 'physical2virtual_admin_menu'); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | function physical2virtual_init() |
---|
| 26 | { |
---|
| 27 | global $conf; |
---|
| 28 | |
---|
| 29 | load_language('plugin.lang', PHYSICAL2VIRTUAL_PATH); |
---|
| 30 | |
---|
| 31 | $conf['physical2virtual'] = unserialize($conf['physical2virtual']); |
---|
| 32 | |
---|
| 33 | // add_event_handler('invalidate_user_cache', 'physical2virtual_convert'); |
---|
| 34 | } |
---|
| 35 | |
---|
[27330] | 36 | function find_or_create_virtual_category($phy_category, $parent_id=null, $options=array()) |
---|
[27164] | 37 | { |
---|
[27330] | 38 | $query = ' |
---|
[27414] | 39 | SELECT DISTINCT virt_category_id AS id, representative_picture_id |
---|
[27330] | 40 | FROM '.PHY2VIRT_CATEGORIES_TABLE.' |
---|
[27414] | 41 | JOIN '.CATEGORIES_TABLE.' ON '.PHY2VIRT_CATEGORIES_TABLE.'.virt_category_id = '.CATEGORIES_TABLE.'.id |
---|
[27330] | 42 | WHERE phy_category_id = '.$phy_category['id'].' |
---|
| 43 | ;'; |
---|
| 44 | $virt_category = pwg_db_fetch_assoc(pwg_query($query)); |
---|
| 45 | if (!empty($virt_category)) |
---|
| 46 | { |
---|
[27414] | 47 | return $virt_category; |
---|
[27330] | 48 | } |
---|
| 49 | |
---|
| 50 | $category_name = $phy_category['name']; |
---|
[27183] | 51 | // process empty album name |
---|
| 52 | if (preg_match('/^\s*$/', $category_name)) |
---|
| 53 | { |
---|
| 54 | $category_name = '_'; |
---|
| 55 | } |
---|
| 56 | |
---|
[27164] | 57 | if (!is_null($parent_id)) |
---|
| 58 | { |
---|
| 59 | $sub_ids = get_subcat_ids(array($parent_id)); |
---|
| 60 | foreach ($sub_ids as $id) |
---|
| 61 | { |
---|
| 62 | $cat_info = get_cat_info($id); |
---|
| 63 | if ($cat_info['name'] == $category_name and !$cat_info['dir']) |
---|
| 64 | return $cat_info; |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
[27330] | 68 | $cat = create_virtual_category($category_name, $parent_id, $options); |
---|
| 69 | $insert = array( |
---|
| 70 | 'phy_category_id' => $phy_category['id'], |
---|
| 71 | 'virt_category_id' => $cat['id'], |
---|
| 72 | // 'updated' => date('Y-m-d H:i:s'), |
---|
| 73 | ); |
---|
| 74 | single_insert(PHY2VIRT_CATEGORIES_TABLE, $insert); |
---|
| 75 | |
---|
| 76 | return $cat; |
---|
[27164] | 77 | } |
---|
| 78 | |
---|
[27096] | 79 | function physical2virtual_convert() |
---|
| 80 | { |
---|
| 81 | global $conf; |
---|
[27229] | 82 | |
---|
| 83 | $options=array(); |
---|
| 84 | if (isset($conf['physical2virtual']['unlock_virtual']) and $conf['physical2virtual']['unlock_virtual']) |
---|
| 85 | $options['visible'] = true; |
---|
| 86 | $options['inherit'] = $conf['physical2virtual']['inherit']; |
---|
[27096] | 87 | |
---|
[27330] | 88 | // Remove virtual without physical |
---|
[27096] | 89 | $query = ' |
---|
[27330] | 90 | SELECT virt_category_id |
---|
| 91 | FROM '.PHY2VIRT_CATEGORIES_TABLE.' |
---|
| 92 | WHERE phy_category_id NOT IN ( |
---|
| 93 | SELECT cat.id |
---|
| 94 | FROM '.CATEGORIES_TABLE.' cat |
---|
| 95 | WHERE cat.dir IS NOT NULL |
---|
| 96 | ) |
---|
| 97 | ;'; |
---|
| 98 | $category_ids = array_from_query($query, 'virt_category_id'); |
---|
| 99 | delete_categories($category_ids); |
---|
| 100 | |
---|
| 101 | $query = ' |
---|
| 102 | DELETE FROM '.PHY2VIRT_CATEGORIES_TABLE.' |
---|
| 103 | WHERE phy_category_id NOT IN ( |
---|
| 104 | SELECT cat.id |
---|
| 105 | FROM '.CATEGORIES_TABLE.' cat |
---|
| 106 | WHERE cat.dir IS NOT NULL |
---|
| 107 | ) |
---|
| 108 | ;'; |
---|
| 109 | pwg_query($query); |
---|
[27414] | 110 | $query = ' |
---|
| 111 | DELETE FROM '.PHY2VIRT_CATEGORIES_TABLE.' |
---|
| 112 | WHERE virt_category_id NOT IN ( |
---|
| 113 | SELECT cat.id |
---|
| 114 | FROM '.CATEGORIES_TABLE.' cat |
---|
[27707] | 115 | WHERE cat.dir IS NULL |
---|
[27414] | 116 | ) |
---|
| 117 | ;'; |
---|
| 118 | pwg_query($query); |
---|
[27330] | 119 | |
---|
| 120 | $query = ' |
---|
[27164] | 121 | SELECT id, name, id_uppercat |
---|
[27096] | 122 | FROM '.CATEGORIES_TABLE.' |
---|
| 123 | WHERE dir IS NOT NULL |
---|
| 124 | ;'; // get physical |
---|
| 125 | $result = pwg_query($query); |
---|
| 126 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 127 | { |
---|
[27164] | 128 | $parent = isset($conf['physical2virtual']['parent_cat']) ? $conf['physical2virtual']['parent_cat'] : null; |
---|
| 129 | |
---|
| 130 | if ($conf['physical2virtual']['store_structure']) |
---|
[27096] | 131 | { |
---|
[27164] | 132 | // get array of physical parents |
---|
| 133 | $cat_info = get_cat_info($row['id']); |
---|
| 134 | $uppers = $cat_info['upper_names']; |
---|
| 135 | array_pop($uppers); // remove themself |
---|
| 136 | |
---|
| 137 | foreach ($uppers as $upper) |
---|
[27096] | 138 | { |
---|
[27656] | 139 | $t = find_or_create_virtual_category($upper, $parent, $options); |
---|
| 140 | $parent = $t['id']; |
---|
[27096] | 141 | } |
---|
[27164] | 142 | } |
---|
| 143 | |
---|
[27330] | 144 | $virt_cat = find_or_create_virtual_category($row, $parent, $options); |
---|
[27164] | 145 | |
---|
| 146 | if (isset($conf['physical2virtual']['lock_physical']) and $conf['physical2virtual']['lock_physical']) |
---|
| 147 | { |
---|
| 148 | $cat_info = get_cat_info($row['id']); |
---|
| 149 | if ($cat_info['visible'] == true) |
---|
[27147] | 150 | { |
---|
[27164] | 151 | set_cat_visible(array($row['id']), false); |
---|
[27147] | 152 | } |
---|
[27096] | 153 | } |
---|
[27164] | 154 | if (isset($conf['physical2virtual']['private_physical']) and $conf['physical2virtual']['private_physical']) |
---|
[27096] | 155 | { |
---|
[27164] | 156 | set_cat_status(array($row['id']), 'private'); |
---|
[27096] | 157 | } |
---|
| 158 | |
---|
| 159 | $query = ' |
---|
| 160 | INSERT IGNORE INTO '.IMAGE_CATEGORY_TABLE.' (category_id, image_id) |
---|
[27164] | 161 | SELECT '.$virt_cat['id'].', image_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$row['id'].' |
---|
[27096] | 162 | ;'; |
---|
| 163 | pwg_query($query); |
---|
[27146] | 164 | |
---|
[27330] | 165 | $query = ' |
---|
| 166 | UPDATE '.PHY2VIRT_CATEGORIES_TABLE.' |
---|
| 167 | SET updated = NOW() |
---|
| 168 | WHERE virt_category_id = '.$virt_cat['id'].' |
---|
| 169 | ;'; |
---|
| 170 | pwg_query($query); |
---|
| 171 | |
---|
[27164] | 172 | if (!(isset($virt_cat['representative_picture_id']) and $virt_cat['representative_picture_id'])) |
---|
[27146] | 173 | { |
---|
[27164] | 174 | set_random_representant(array($virt_cat['id'])); |
---|
[27146] | 175 | } |
---|
[27096] | 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | function physical2virtual_admin_menu($menu) |
---|
| 180 | { |
---|
| 181 | $menu[] = array( |
---|
| 182 | 'NAME' => 'physical2virtual', |
---|
| 183 | 'URL' => PHYSICAL2VIRTUAL_ADMIN, |
---|
| 184 | ); |
---|
| 185 | |
---|
| 186 | return $menu; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | ?> |
---|