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

Last change on this file since 11217 was 10773, checked in by plg, 13 years ago

bug 2298 fixed: if missing, new tables are created during plugin activation

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