source: extensions/community/maintain.inc.php @ 22040

Last change on this file since 22040 was 21289, checked in by plg, 11 years ago

bug fixed: table creation crashed with mysqli

specific code for pgsql/sqlite removed

replace $conflocal_data_dir by $confdata_location (introduced in Piwigo 2.4)

File size: 7.2 KB
Line 
1<?php
2
3if (!defined("COMMUNITY_PATH"))
4{
5  define('COMMUNITY_PATH', PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)));
6}
7
8function plugin_install()
9{
10  global $conf, $prefixeTable;
11
12  $query = '
13CREATE TABLE '.$prefixeTable.'community_permissions (
14  id int(11) NOT NULL AUTO_INCREMENT,
15  type varchar(255) NOT NULL,
16  group_id smallint(5) unsigned DEFAULT NULL,
17  user_id smallint(5) DEFAULT NULL,
18  category_id smallint(5) unsigned DEFAULT NULL,
19  recursive enum(\'true\',\'false\') NOT NULL DEFAULT \'true\',
20  create_subcategories enum(\'true\',\'false\') NOT NULL DEFAULT \'false\',
21  moderated enum(\'true\',\'false\') NOT NULL DEFAULT \'true\',
22  PRIMARY KEY (id)
23) ENGINE=MyISAM DEFAULT CHARACTER SET utf8
24;';
25  pwg_query($query);
26
27  $query = '
28CREATE TABLE '.$prefixeTable.'community_pendings (
29  image_id mediumint(8) unsigned NOT NULL,
30  state varchar(255) NOT NULL,
31  added_on datetime NOT NULL,
32  validated_by smallint(5) DEFAULT NULL
33) ENGINE=MyISAM DEFAULT CHARACTER SET utf8
34;';
35  pwg_query($query);
36}
37
38function plugin_uninstall()
39{
40  global $prefixeTable;
41 
42  $query = 'DROP TABLE '.$prefixeTable.'community_permissions;';
43  pwg_query($query);
44
45  $query = 'DROP TABLE '.$prefixeTable.'community_pendings;';
46  pwg_query($query);
47}
48
49function plugin_activate()
50{
51  global $prefixeTable;
52
53  $are_new_tables_installed = false;
54 
55  $query = 'SHOW TABLES;';
56  $result = pwg_query($query);
57  while ($row = pwg_db_fetch_row($result))
58  {
59    if ($prefixeTable.'community_permissions' == $row[0])
60    {
61      $are_new_tables_installed = true;
62    }
63  }
64
65  if (!$are_new_tables_installed)
66  {
67    plugin_install();
68  }
69
70  community_get_data_from_core21();
71  community_get_data_from_community21();
72
73  $query = '
74SELECT
75    COUNT(*)
76  FROM '.$prefixeTable.'community_permissions
77;';
78  list($counter) = pwg_db_fetch_row(pwg_query($query));
79  if (0 == $counter)
80  {
81    community_create_default_permission();
82  }
83
84  include_once(dirname(__FILE__).'/include/functions_community.inc.php');
85  community_update_cache_key();
86}
87
88function community_get_data_from_core21()
89{
90  global $conf, $prefixeTable;
91 
92  $from_piwigo21_file = $conf['data_location'].'/plugins/core_user_upload_to_community.php';
93  if (is_file($from_piwigo21_file))
94  {
95    include($from_piwigo21_file);
96    $user_upload_conf = unserialize($user_upload_conf);
97   
98    $user_upload_conf['upload_user_access'] = 1;
99
100    if (isset($user_upload_conf['uploadable_categories']) and is_array($user_upload_conf['uploadable_categories']))
101    {
102      $type = 'any_registered_user';
103      if (isset($user_upload_conf['upload_user_access']) and 1 == $user_upload_conf['upload_user_access'])
104      {
105        $type = 'any_visitor';
106      }
107
108      $inserts = array();
109   
110      foreach ($user_upload_conf['uploadable_categories'] as $category_id)
111      {
112        array_push(
113          $inserts,
114          array(
115            'type' => $type,
116            'category_id' => $category_id,
117            'recursive' => 'false',
118            'create_subcategories' => 'false',
119            'moderated' => 'true',
120            )
121          );
122      }
123     
124      if (count($inserts) > 0)
125      {
126        mass_inserts(
127          $prefixeTable.'community_permissions',
128          array_keys($inserts[0]),
129          $inserts
130          );
131      }
132    }
133   
134    if (isset($user_upload_conf['waiting_rows']) and is_array($user_upload_conf['waiting_rows']))
135    {
136      $id_of_user = array();
137     
138      $query = '
139SELECT
140    '.$conf['user_fields']['id'].' AS id,
141    '.$conf['user_fields']['username'].' AS username
142  FROM '.USERS_TABLE.'
143;';
144      $result = pwg_query($query);
145      while ($row = pwg_db_fetch_assoc($result))
146      {
147        $id_of_user[ $row['username'] ] = $row['id'];
148      }
149     
150      $inserts = array();
151     
152      foreach ($user_upload_conf['waiting_rows'] as $pending)
153      {
154        $source_path = get_complete_dir($pending['storage_category_id']).$pending['file'];
155       
156        if (is_file($source_path))
157        {
158          $image_id = add_uploaded_file($source_path, $pending['file'], array($pending['storage_category_id']), 16);
159         
160          array_push(
161            $inserts,
162            array(
163              'image_id' => $image_id,
164              'added_on' => date ('Y-m-d H:i:s', $pending['date']),
165              'state' => 'moderation_pending',
166              )
167            );
168
169          $data = array();
170         
171          if (isset($pending['username']) and isset($id_of_user[ $pending['username'] ]))
172          {
173            $data['added_by'] = $id_of_user[ $pending['username'] ];
174          }
175         
176          foreach (array('date_creation', 'author', 'name', 'comment') as $field)
177          {
178            $value = getAttribute($pending['infos'], $field);
179            if (!empty($value))
180            {
181            $data[$field] = pwg_db_real_escape_string($value);
182            }
183          }
184         
185          if (count($data) > 0)
186          {
187            $data['id'] = $image_id;
188           
189            mass_updates(
190              IMAGES_TABLE,
191              array(
192                'primary' => array('id'),
193                'update'  => array_keys($data)
194                ),
195              array($data)
196              );
197          }
198         
199          // deletion
200          unlink($source_path);
201          if (!isset($pending['tn_ext']))
202          {
203            $pending['tn_ext'] = 'jpg';
204          }
205          @unlink(get_thumbnail_path(array('path'=>$source_path, 'tn_ext'=>$pending['tn_ext'])));
206        }
207      }
208     
209      if (count($inserts) > 0)
210      {
211        mass_inserts(
212          $prefixeTable.'community_pendings',
213          array_keys($inserts[0]),
214          $inserts
215          );
216      }
217    }
218    unlink($from_piwigo21_file);
219  }
220}
221
222function community_get_data_from_community21()
223{
224  global $prefixeTable;
225 
226  $old_community_table = $prefixeTable.'community';
227  $query = 'SHOW TABLES;';
228  $result = pwg_query($query);
229  while ($row = pwg_db_fetch_row($result))
230  {
231    if ($old_community_table == $row[0])
232    {
233      $inserts = array();
234     
235      $query = '
236SELECT
237    *
238  FROM '.$old_community_table.'
239;';
240      $result = pwg_query($query);
241      while ($row = pwg_db_fetch_assoc($result))
242      {
243        array_push(
244          $inserts,
245          array(
246            'type' => 'user',
247            'user_id' => $row['user_id'],
248            'category_id' => null,
249            'recursive' => 'true',
250            'create_subcategories' => $row['permission_level'] == 2 ? 'true' : 'false',
251            'moderated' => 'false',
252            )
253          );
254      }
255     
256      if (count($inserts) > 0)
257      {
258        mass_inserts(
259          $prefixeTable.'community_permissions',
260          array_keys($inserts[0]),
261          $inserts
262          );
263      }
264     
265      $query = 'DROP TABLE '.$old_community_table.';';
266      pwg_query($query);
267     
268      break;
269    }
270  }
271}
272
273function community_create_default_permission()
274{
275  global $prefixeTable;
276 
277  // create an album "Community"
278  $category_info = create_virtual_category('Community');
279
280  $insert = array(
281    'type' => 'any_registered_user',
282    'category_id' => $category_info['id'],
283    'recursive' => 'true',
284    'create_subcategories' => 'true',
285    'moderated' => 'true',
286    );
287
288  mass_inserts($prefixeTable.'community_permissions', array_keys($insert), array($insert));
289}
290?>
Note: See TracBrowser for help on using the repository browser.